Mercurial > mercurial > hgweb_golang.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/kaigo/qtuti/95y/95y_tmpl.go Fri May 20 06:30:34 2022 +0900 @@ -0,0 +1,359 @@ +/* + 95y.go: Qfuhi Tsuchi (Year Version) + + Last Change: 2021-11-15 月 14:40:46. +*/ + +package main + +/* +#cgo LDFLAGS: -L. -lxdwapi -static +##### C_SOURCE ##### +*/ +import "C" + +import ( + "bufio" + "regexp" + "encoding/json" + "flag" + "fmt" + "log" + "os" + "path/filepath" + "sort" + "strings" + + "golang.org/x/text/encoding/japanese" + "golang.org/x/text/transform" +) + +type Hhs struct { + No string + Name string + Weight int + //Text string + Xdw []string +} + +func(h *Hhs) AppendXdw(xdw string) { + h.Xdw = append(h.Xdw, xdw) +} + +func(h *Hhs) CsvString() string { + return strings.Join([]string{h.No, h.Name}, ",") +} + +func(h *Hhs) Dump() string { + x := strings.Join(h.Xdw, ",") + return strings.Join([]string{h.No, h.Name, fmt.Sprintf("%d", h.Weight), x}, "\t") +} + +type Config struct { + Indir string + Outdir string + Workdir string + Atnfile[] string +} + +var ( + ver = "0.1" + conf Config + confjson = "95y.json" + logfile = "95y.log" + whitexdw = "aiobo.xdw" + split_n = 9000 + + out_q1 = "q1.xdw" + out_q2 = "q2.xdw" + out_q3 = "q3.xdw" + out_l = "l.csv" + + delfile = "del.list" + sortfile = "sort.list" + + tmpfile = "95y.tmp" + + in_xdw []string + re_hhsno, re_name *regexp.Regexp + + // option parameters + version bool +) + +func init() { + /* コンフィグファイルは JSON */ + content, err := os.ReadFile(confjson) + if err != nil { + log.Fatal(err) + } + if err := json.Unmarshal(content, &conf); err != nil { + log.Fatal(err) + } + + out_q1 = filepath.Join(conf.Outdir, out_q1) + out_q2 = filepath.Join(conf.Outdir, out_q2) + out_q3 = filepath.Join(conf.Outdir, out_q3) + out_l = filepath.Join(conf.Outdir, out_l) + + logfile = filepath.Join(conf.Workdir, logfile) + + /* 一時ファイル消去 */ + os.RemoveAll(conf.Outdir) + os.RemoveAll(conf.Workdir) + os.Mkdir(conf.Outdir, 0755) + os.Mkdir(conf.Workdir, 0755) + + /* 変数初期化 */ + re_hhsno = regexp.MustCompile(`0[1238]00\d{6}`) + re_name = regexp.MustCompile(`管理者 老 松 博 行.{14}`) + + /* Docuworksファイル列挙 */ + files, err := os.ReadDir(conf.Indir) + if err != nil { + log.Fatal(err) + } + + for _, file := range files { + if !strings.HasSuffix(file.Name(), ".xdw") { + continue + } + if strings.HasPrefix(file.Name(), "KDPK016G") || strings.HasPrefix(file.Name(), "KDPK126G") { + in_xdw = append(in_xdw, file.Name()) + } + } + + flag.BoolVar(&version, "v", false, "print version") +} + +func main() { + flag.Parse() + + if version { + fmt.Println("95y - version", ver) + os.Exit(0) + } + + /* 重み付けの準備 */ + err, sort_list := file2slice(filepath.Join(conf.Indir, sortfile)) + if err != nil { + log.Fatal(err) + } + + /* 給付費通知を漁り,構造体を初期化 */ + hash_Hhs := make(map[string]Hhs) + for i, file := range in_xdw { + for p, t := range xdw2txtb(filepath.Join(conf.Indir, file), filepath.Join(conf.Workdir, tmpfile)) { + hno := re_hhsno.FindString(t) + _, ok := hash_Hhs[hno] + if strings.Contains(t, "大曲仙北広域市町村圏組合") && !ok { + name := re_name.FindString(t) + name = strings.Replace(name, "管理者 老 松 博 行", "", 1) + + w := 99 + for j, s := range sort_list { + if strings.Contains(t, s) { + w = j + } + } + + h := Hhs{No: hno, Name: name, Weight: w, Xdw: []string{fmt.Sprintf("%02d_%05d.xdw", i, p + 1)}} + hash_Hhs[hno] = h + } else { + h := hash_Hhs[hno] + h.AppendXdw(fmt.Sprintf("%02d_%05d.xdw", i, p + 1)) + hash_Hhs[hno] = h + } + //fmt.Println(file, i, "-", p) // <--- + } + } + fmt.Println("analize done") + + /* バックグラウンドで給付費通知をバラす */ + ch := make(chan int) + go func() { + for i, file := range in_xdw { + qtsuchi := filepath.Join(conf.Indir, file) + C.xdwsplit1(C.CString(qtsuchi), C.CString(conf.Workdir), C.CString(fmt.Sprintf("%02d_", i))) + } + ch <- 1 + fmt.Println("split done") + }() + + /* そのあいだに 不要者削除,ソート,マージ */ + err, del_list := file2slice(filepath.Join(conf.Indir, delfile)) + if err != nil { + log.Fatal(err) + } + for _, hno := range del_list { + delete(hash_Hhs, hno) + } + fmt.Println("delete done") + + /* ソート */ + var slice_Hhs []Hhs + for _, h := range hash_Hhs { + slice_Hhs = append(slice_Hhs, h) + } + sort.Slice(slice_Hhs, func(i, j int) bool { + if slice_Hhs[i].Weight != slice_Hhs[j].Weight { + return slice_Hhs[i].Weight < slice_Hhs[j].Weight + } + if slice_Hhs[i].No != slice_Hhs[j].No { + return slice_Hhs[i].No < slice_Hhs[j].No + } + return false + }) + fmt.Println("sort done") + + /* マージ */ + var list_q1, list_q2 []string + for _, h := range slice_Hhs { + if h.Weight < 99 { + for _, x := range h.Xdw { + buf := filepath.Join(conf.Workdir, x) + list_q1 = append(list_q1, buf) + } + if len(h.Xdw) % 2 == 1 { + list_q1 = append(list_q1, whitexdw) + } + } else { + for _, x := range h.Xdw { + buf := filepath.Join(conf.Workdir, x) + list_q2 = append(list_q2, buf) + } + if len(h.Xdw) % 2 == 1 { + list_q2 = append(list_q2, whitexdw) + } + } + } + + <-ch + xdwmerge(list_q1, out_q1) + if len(list_q2) <= split_n { + xdwmerge(list_q2, out_q2) + } else { + xdwmerge(list_q2[:split_n], out_q2) + xdwmerge(list_q2[split_n:], out_q3) + } + fmt.Println("merge done") + + /* バックグラウンドで給付費通知を校正 */ + ch_q1 := make(chan int) + go func() { + for _, a := range conf.Atnfile { + xdwaddatn(out_q1, a) + } + ch_q1 <- 1 + fmt.Println("correct1 done") + } () + + ch_q2 := make(chan int) + go func() { + for _, a := range conf.Atnfile { + xdwaddatn(out_q2, a) + } + ch_q2 <- 1 + fmt.Println("correct2 done") + } () + + ch_q3 := make(chan int) + if len(list_q2) > split_n { + go func() { + for _, a := range conf.Atnfile { + xdwaddatn(out_q3, a) + } + ch_q3 <- 1 + fmt.Println("correct3 done") + } () + } + + /* リスト出力 & ログダンプ */ + csvtxt := "" + logtxt := "" + for _, h := range slice_Hhs { + csvtxt += h.CsvString() + "\n" + logtxt += h.Dump() + "\n" + } + csvtxt, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), csvtxt) + if err := os.WriteFile(out_l, []byte(csvtxt), 0644); err != nil { + log.Fatal(err) + } + if err := os.WriteFile(logfile, []byte(logtxt), 0644); err != nil { + log.Fatal(err) + } + fmt.Println("logdump done") + + <-ch_q1 + <-ch_q2 + if len(list_q2) > split_n { + <-ch_q3 + } +} + +func file2slice(file string) (err error, list []string) { + f, err := os.Open(file) + if err != nil { + return err, nil + } + defer f.Close() + + buf := bufio.NewScanner(f) + for buf.Scan() { + list = append(list, buf.Text()) + } + return nil, list +} + +func xdw2txt(file string) (txt []string) { + s := C.GoString(C.xdw2txt(C.CString(file))) + r := strings.NewReader(s) + tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder()) + buf := bufio.NewScanner(tr) + for buf.Scan() { + txt = append(txt, buf.Text()) + } + return +} + +func xdw2txtb(xdwfile, txtfile string) (txt []string) { + if _, err := os.Stat(txtfile); os.IsExist(err) { + os.Remove(txtfile) + } + + C.xdw2txtb(C.CString(xdwfile), C.CString(txtfile)) + content, err := os.ReadFile(txtfile) + if err != nil { + return nil + } + + r := strings.NewReader(string(content)) + tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder()) + buf := bufio.NewScanner(tr) + for buf.Scan() { + txt = append(txt, buf.Text()) + } + return +} + +func xdwmerge(list []string, outfile string) (err error) { + order := strings.Join(list, "\n") + order, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), order) + orderfile := fmt.Sprintf("%s_order.txt", outfile) + if err := os.WriteFile(orderfile, []byte(order), 0644); err != nil { + return err + } + C.xdwmerge(C.CString(orderfile), C.CString(outfile)) + return nil +} + +func xdwaddatn(xdwfile, atnfile string) (err error) { + C.xdwaddatn(C.CString(xdwfile), C.CString(atnfile)) + return nil +} + +func xdwaddatntool(xdwfile, toolfile string, n, x, y int) (err error) { + C.xdwaddatntool(C.CString(xdwfile), C.CString(toolfile), C.int(n), C.int(x), C.int(y)) + return nil +} +