comparison src/kaigo/qtuti/95y/95y_tmpl.go @ 65:0369656be06c default tip

many changes.
author pyon@macmini
date Fri, 20 May 2022 06:30:34 +0900
parents
children
comparison
equal deleted inserted replaced
64:ad5c30ee5cf1 65:0369656be06c
1 /*
2 95y.go: Qfuhi Tsuchi (Year Version)
3
4 Last Change: 2021-11-15 月 14:40:46.
5 */
6
7 package main
8
9 /*
10 #cgo LDFLAGS: -L. -lxdwapi -static
11 ##### C_SOURCE #####
12 */
13 import "C"
14
15 import (
16 "bufio"
17 "regexp"
18 "encoding/json"
19 "flag"
20 "fmt"
21 "log"
22 "os"
23 "path/filepath"
24 "sort"
25 "strings"
26
27 "golang.org/x/text/encoding/japanese"
28 "golang.org/x/text/transform"
29 )
30
31 type Hhs struct {
32 No string
33 Name string
34 Weight int
35 //Text string
36 Xdw []string
37 }
38
39 func(h *Hhs) AppendXdw(xdw string) {
40 h.Xdw = append(h.Xdw, xdw)
41 }
42
43 func(h *Hhs) CsvString() string {
44 return strings.Join([]string{h.No, h.Name}, ",")
45 }
46
47 func(h *Hhs) Dump() string {
48 x := strings.Join(h.Xdw, ",")
49 return strings.Join([]string{h.No, h.Name, fmt.Sprintf("%d", h.Weight), x}, "\t")
50 }
51
52 type Config struct {
53 Indir string
54 Outdir string
55 Workdir string
56 Atnfile[] string
57 }
58
59 var (
60 ver = "0.1"
61 conf Config
62 confjson = "95y.json"
63 logfile = "95y.log"
64 whitexdw = "aiobo.xdw"
65 split_n = 9000
66
67 out_q1 = "q1.xdw"
68 out_q2 = "q2.xdw"
69 out_q3 = "q3.xdw"
70 out_l = "l.csv"
71
72 delfile = "del.list"
73 sortfile = "sort.list"
74
75 tmpfile = "95y.tmp"
76
77 in_xdw []string
78 re_hhsno, re_name *regexp.Regexp
79
80 // option parameters
81 version bool
82 )
83
84 func init() {
85 /* コンフィグファイルは JSON */
86 content, err := os.ReadFile(confjson)
87 if err != nil {
88 log.Fatal(err)
89 }
90 if err := json.Unmarshal(content, &conf); err != nil {
91 log.Fatal(err)
92 }
93
94 out_q1 = filepath.Join(conf.Outdir, out_q1)
95 out_q2 = filepath.Join(conf.Outdir, out_q2)
96 out_q3 = filepath.Join(conf.Outdir, out_q3)
97 out_l = filepath.Join(conf.Outdir, out_l)
98
99 logfile = filepath.Join(conf.Workdir, logfile)
100
101 /* 一時ファイル消去 */
102 os.RemoveAll(conf.Outdir)
103 os.RemoveAll(conf.Workdir)
104 os.Mkdir(conf.Outdir, 0755)
105 os.Mkdir(conf.Workdir, 0755)
106
107 /* 変数初期化 */
108 re_hhsno = regexp.MustCompile(`0[1238]00\d{6}`)
109 re_name = regexp.MustCompile(`管理者 老 松 博 行.{14}`)
110
111 /* Docuworksファイル列挙 */
112 files, err := os.ReadDir(conf.Indir)
113 if err != nil {
114 log.Fatal(err)
115 }
116
117 for _, file := range files {
118 if !strings.HasSuffix(file.Name(), ".xdw") {
119 continue
120 }
121 if strings.HasPrefix(file.Name(), "KDPK016G") || strings.HasPrefix(file.Name(), "KDPK126G") {
122 in_xdw = append(in_xdw, file.Name())
123 }
124 }
125
126 flag.BoolVar(&version, "v", false, "print version")
127 }
128
129 func main() {
130 flag.Parse()
131
132 if version {
133 fmt.Println("95y - version", ver)
134 os.Exit(0)
135 }
136
137 /* 重み付けの準備 */
138 err, sort_list := file2slice(filepath.Join(conf.Indir, sortfile))
139 if err != nil {
140 log.Fatal(err)
141 }
142
143 /* 給付費通知を漁り,構造体を初期化 */
144 hash_Hhs := make(map[string]Hhs)
145 for i, file := range in_xdw {
146 for p, t := range xdw2txtb(filepath.Join(conf.Indir, file), filepath.Join(conf.Workdir, tmpfile)) {
147 hno := re_hhsno.FindString(t)
148 _, ok := hash_Hhs[hno]
149 if strings.Contains(t, "大曲仙北広域市町村圏組合") && !ok {
150 name := re_name.FindString(t)
151 name = strings.Replace(name, "管理者 老 松 博 行", "", 1)
152
153 w := 99
154 for j, s := range sort_list {
155 if strings.Contains(t, s) {
156 w = j
157 }
158 }
159
160 h := Hhs{No: hno, Name: name, Weight: w, Xdw: []string{fmt.Sprintf("%02d_%05d.xdw", i, p + 1)}}
161 hash_Hhs[hno] = h
162 } else {
163 h := hash_Hhs[hno]
164 h.AppendXdw(fmt.Sprintf("%02d_%05d.xdw", i, p + 1))
165 hash_Hhs[hno] = h
166 }
167 //fmt.Println(file, i, "-", p) // <---
168 }
169 }
170 fmt.Println("analize done")
171
172 /* バックグラウンドで給付費通知をバラす */
173 ch := make(chan int)
174 go func() {
175 for i, file := range in_xdw {
176 qtsuchi := filepath.Join(conf.Indir, file)
177 C.xdwsplit1(C.CString(qtsuchi), C.CString(conf.Workdir), C.CString(fmt.Sprintf("%02d_", i)))
178 }
179 ch <- 1
180 fmt.Println("split done")
181 }()
182
183 /* そのあいだに 不要者削除,ソート,マージ */
184 err, del_list := file2slice(filepath.Join(conf.Indir, delfile))
185 if err != nil {
186 log.Fatal(err)
187 }
188 for _, hno := range del_list {
189 delete(hash_Hhs, hno)
190 }
191 fmt.Println("delete done")
192
193 /* ソート */
194 var slice_Hhs []Hhs
195 for _, h := range hash_Hhs {
196 slice_Hhs = append(slice_Hhs, h)
197 }
198 sort.Slice(slice_Hhs, func(i, j int) bool {
199 if slice_Hhs[i].Weight != slice_Hhs[j].Weight {
200 return slice_Hhs[i].Weight < slice_Hhs[j].Weight
201 }
202 if slice_Hhs[i].No != slice_Hhs[j].No {
203 return slice_Hhs[i].No < slice_Hhs[j].No
204 }
205 return false
206 })
207 fmt.Println("sort done")
208
209 /* マージ */
210 var list_q1, list_q2 []string
211 for _, h := range slice_Hhs {
212 if h.Weight < 99 {
213 for _, x := range h.Xdw {
214 buf := filepath.Join(conf.Workdir, x)
215 list_q1 = append(list_q1, buf)
216 }
217 if len(h.Xdw) % 2 == 1 {
218 list_q1 = append(list_q1, whitexdw)
219 }
220 } else {
221 for _, x := range h.Xdw {
222 buf := filepath.Join(conf.Workdir, x)
223 list_q2 = append(list_q2, buf)
224 }
225 if len(h.Xdw) % 2 == 1 {
226 list_q2 = append(list_q2, whitexdw)
227 }
228 }
229 }
230
231 <-ch
232 xdwmerge(list_q1, out_q1)
233 if len(list_q2) <= split_n {
234 xdwmerge(list_q2, out_q2)
235 } else {
236 xdwmerge(list_q2[:split_n], out_q2)
237 xdwmerge(list_q2[split_n:], out_q3)
238 }
239 fmt.Println("merge done")
240
241 /* バックグラウンドで給付費通知を校正 */
242 ch_q1 := make(chan int)
243 go func() {
244 for _, a := range conf.Atnfile {
245 xdwaddatn(out_q1, a)
246 }
247 ch_q1 <- 1
248 fmt.Println("correct1 done")
249 } ()
250
251 ch_q2 := make(chan int)
252 go func() {
253 for _, a := range conf.Atnfile {
254 xdwaddatn(out_q2, a)
255 }
256 ch_q2 <- 1
257 fmt.Println("correct2 done")
258 } ()
259
260 ch_q3 := make(chan int)
261 if len(list_q2) > split_n {
262 go func() {
263 for _, a := range conf.Atnfile {
264 xdwaddatn(out_q3, a)
265 }
266 ch_q3 <- 1
267 fmt.Println("correct3 done")
268 } ()
269 }
270
271 /* リスト出力 & ログダンプ */
272 csvtxt := ""
273 logtxt := ""
274 for _, h := range slice_Hhs {
275 csvtxt += h.CsvString() + "\n"
276 logtxt += h.Dump() + "\n"
277 }
278 csvtxt, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), csvtxt)
279 if err := os.WriteFile(out_l, []byte(csvtxt), 0644); err != nil {
280 log.Fatal(err)
281 }
282 if err := os.WriteFile(logfile, []byte(logtxt), 0644); err != nil {
283 log.Fatal(err)
284 }
285 fmt.Println("logdump done")
286
287 <-ch_q1
288 <-ch_q2
289 if len(list_q2) > split_n {
290 <-ch_q3
291 }
292 }
293
294 func file2slice(file string) (err error, list []string) {
295 f, err := os.Open(file)
296 if err != nil {
297 return err, nil
298 }
299 defer f.Close()
300
301 buf := bufio.NewScanner(f)
302 for buf.Scan() {
303 list = append(list, buf.Text())
304 }
305 return nil, list
306 }
307
308 func xdw2txt(file string) (txt []string) {
309 s := C.GoString(C.xdw2txt(C.CString(file)))
310 r := strings.NewReader(s)
311 tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder())
312 buf := bufio.NewScanner(tr)
313 for buf.Scan() {
314 txt = append(txt, buf.Text())
315 }
316 return
317 }
318
319 func xdw2txtb(xdwfile, txtfile string) (txt []string) {
320 if _, err := os.Stat(txtfile); os.IsExist(err) {
321 os.Remove(txtfile)
322 }
323
324 C.xdw2txtb(C.CString(xdwfile), C.CString(txtfile))
325 content, err := os.ReadFile(txtfile)
326 if err != nil {
327 return nil
328 }
329
330 r := strings.NewReader(string(content))
331 tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder())
332 buf := bufio.NewScanner(tr)
333 for buf.Scan() {
334 txt = append(txt, buf.Text())
335 }
336 return
337 }
338
339 func xdwmerge(list []string, outfile string) (err error) {
340 order := strings.Join(list, "\n")
341 order, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), order)
342 orderfile := fmt.Sprintf("%s_order.txt", outfile)
343 if err := os.WriteFile(orderfile, []byte(order), 0644); err != nil {
344 return err
345 }
346 C.xdwmerge(C.CString(orderfile), C.CString(outfile))
347 return nil
348 }
349
350 func xdwaddatn(xdwfile, atnfile string) (err error) {
351 C.xdwaddatn(C.CString(xdwfile), C.CString(atnfile))
352 return nil
353 }
354
355 func xdwaddatntool(xdwfile, toolfile string, n, x, y int) (err error) {
356 C.xdwaddatntool(C.CString(xdwfile), C.CString(toolfile), C.int(n), C.int(x), C.int(y))
357 return nil
358 }
359