icps

notes

IDBarcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main

import (
    "github.com/harry1453/go-common-file-dialog/cfd"
    "github.com/harry1453/go-common-file-dialog/cfdutil"
    "github.com/makiuchi-d/gozxing"
    "github.com/makiuchi-d/gozxing/oned"
    "github.com/xuri/excelize/v2"

    "fmt"
    "image/png"
    "os"
    "os/exec"
    "path/filepath"
    "runtime"
    "strconv"
    "strings"
    "syscall"
    "unsafe"
)

var (
    temp = "temp"
)

func main() {
    targetPath := openFile()
    buildTemp()
    process(targetPath)
    rmTemp()
}

func openFile() string {
    result, _ := cfdutil.ShowOpenFileDialog(cfd.DialogConfig{
        Title: "Open A xlsx File",
        Role:  "Open A xlsx File",
        FileFilters: []cfd.FileFilter{
            {
                DisplayName: "*.xlsx",
                Pattern:     "*.xlsx",
            },
            {
                DisplayName: "All Files (*.*)",
                Pattern:     "*.*",
            },
        },
        SelectedFileFilterIndex: 0,
        FileName:                "*.xlsx",
        DefaultExtension:        "xlsx",
    })

    return result
}

func process(targetPath string) {
    targetDir := filepath.Dir(targetPath) + "\\"
    targetFileName := filepath.Base(targetPath)
    outputPath := targetDir + "barcode_" + targetFileName
    f, err := excelize.OpenFile(targetPath)
    if err != nil {
        rmTemp()
    }
    printError(err)
    //選第一個工作表
    var sheet string
    for _, value := range f.GetSheetMap() {
        sheet = value
        break
    }
    //產生圖片
    rows, _ := f.GetRows(sheet)
    for i, row := range rows {
        if len(row) > 1 {
            id := row[1]
            if checkIDCode(id) != "" {
                f.SetRowHeight(sheet, i+1, 30)
                genCode128(id)
            }
        }
    }
    //插入圖片
    for i, row := range rows {
        for j, cell := range row {
            l := rune(j + 65 + 2)
            location := fmt.Sprintf("%c%v", l, i+1)
            if j == 1 {
                f.AddPicture(sheet, location, genPath(cell), `{"y_offset": 6, "x_scale": 0.5, "y_scale": 0.5, "positioning": "oneCell"}`)
            }
        }
    }
    err = f.SaveAs(outputPath)
    if err == nil {
        //MessageBox("產生", "檔案已產生  " + outputPath)
        cmd := fmt.Sprintf(`explorer /select, %s`, outputPath)
        runCmd(cmd)
    } else {
        MessageBox("錯誤 !!!", err.Error())
    }
}

func checkIDCode(num string) string {
    var sum int
    idTableCh := strings.Split("FBEAHDNTPJKQMGOCUIVWXZ", "")
    idTableValue := []int{15, 11, 14, 10, 17, 13, 22, 27, 23, 18, 19, 24, 21, 16, 35, 12, 28, 34, 29, 32, 30, 33}

    for idx, c := range idTableCh {
        if c == string(num[:1]) {
            v := idTableValue[idx]
            s := (v%10)*9 + (v / 10)
            sum += s
            break
        }
    }

    for idx, n := range num[1:9] {
        i, _ := strconv.Atoi(string(n))
        sum += i * (8 - idx)
    }
    i, _ := strconv.Atoi(string(num[9:]))
    sum += i
    if sum%10 == 0 {
        return num
    }
    return ""
}

func genCode128(id string) {
    enc := oned.NewCode128Writer()
    img, _ := enc.Encode(id, gozxing.BarcodeFormat_CODE_128, 250, 50, nil)
    path := genPath(id)
    file, _ := os.Create(path)
    defer file.Close()
    png.Encode(file, img)
}

func genPath(id string) string {
    return fmt.Sprintf(`%s\%s.png`, temp, id)
}

func MessageBox(title, caption string) {
    syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
        uintptr(0),
        uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
        uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
        uintptr(0))
}

func runCmd(cmd string) {
    exec.Command("cmd.exe", "/C", cmd).CombinedOutput()
}

func buildTemp() {
    cmd := fmt.Sprintf(`mkdir %s`, temp)
    runCmd(cmd)
}

func rmTemp() {
    cmd := fmt.Sprintf(`del /q %s`, temp)
    runCmd(cmd)

    cmd = fmt.Sprintf(`rmdir %s`, temp)
    runCmd(cmd)
}

func printError(err error) {
    if err != nil {
        pc, _, _, _ := runtime.Caller(1)
        fmt.Println(runtime.FuncForPC(pc).Name())
        fmt.Println(err)
    }
}