diff --git a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/build.bat b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/build.bat new file mode 100644 index 0000000..a5d1dbf --- /dev/null +++ b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/build.bat @@ -0,0 +1 @@ +go build -o demo.exe diff --git a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.exe.manifest b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.exe.manifest new file mode 100644 index 0000000..c52acbc --- /dev/null +++ b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.exe.manifest @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.go b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.go index 7a41736..0275b72 100644 --- a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.go +++ b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.go @@ -12,49 +12,55 @@ import ( "time" ) -const sheetName = `全部数据` +const sheetName = `Sheet1` const fileName = `公务员系统数据-` const fileType = `.xlsx` const linkStr = `root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8` const sqlStr = `SELECT concat(code_type,code_value),code_name FROM CODE_VALUE where code_type in('%s') union SELECT concat(code_type,code_value),code_name3 FROM CODE_VALUE where code_type='ZB01'` var CodeValueMap = make(map[string]string) -var allObj = make([][]interface{}, 200) func main() { db, _ := sql.Open("mysql", linkStr) - codes, headers, types, headWidth, yrYhsLen, err := getInit() + codes, headers, types, headWidth, err := getInit() if err { return } QueryMap(db, fmt.Sprintf(sqlStr, strings.Join(types, "','")), CodeValueMap) log.Print("输出内容:", headers) log.Print("输出内容:", codes) - log.Print("输出内容:", CodeValueMap) - - allObj = [][]interface{}{} - ii := make([]interface{}, yrYhsLen, yrYhsLen) - for i := 0; i < yrYhsLen; i++ { - ii[i] = "测试" - } - allObj = append(allObj, ii) - ii = make([]interface{}, yrYhsLen, yrYhsLen) - for i := 0; i < yrYhsLen; i++ { - ii[i] = "输出内容输出内容输出内容" + log.Print("输出内容:", len(CodeValueMap)) + b01s := QueryB01(db) + b01 := dept{ + Id: "-1", + Child: make([]*dept, 0), } - allObj = append(allObj, ii) - log.Print("输出内容:", allObj) + makeTree(b01s, &b01) + var allObj [][]interface{} + makeA01s(b01, db, codes, types, &allObj) + + log.Print("输出内容:", len(allObj), len(headWidth)) getExcelFile(headers, headWidth, allObj) } +func makeA01s(b01 dept, db *sql.DB, codes []string, types []string, allObj *[][]interface{}) { + for _, d := range b01.Child { + log.Print("执行机构:", d.Id, " - ", d.Name) + QueryA01(db, strings.Join(codes, ","), types, d.Id, allObj) + if len(d.Child) > 0 { + makeA01s(*d, db, codes, types, allObj) + } + } +} + // getInit 初始化 JSON 对象,获取猎头 -func getInit() ([]string, []string, []string, []float64, int, bool) { +func getInit() ([]string, []string, []string, []float64, bool) { var yrYhs []YrYh //读取的数据为json格式,需要进行解码 err := json.Unmarshal([]byte(yryh), &yrYhs) if err != nil { log.Fatal("发生异常:", err) - return nil, nil, nil, nil, 0, true + return nil, nil, nil, nil, true } yrYhsLen := len(yrYhs) types := make([]string, yrYhsLen, yrYhsLen) // 输出类型 @@ -65,11 +71,9 @@ func getInit() ([]string, []string, []string, []float64, int, bool) { codes[i1] = i2.Code headers[i1] = i2.Name headWidth[i1] = i2.Width - if i2.Type != "" && i2.Type != "DATE" && i2.Type != "ZB01" { - types[i1] = i2.Type - } + types[i1] = i2.Type } - return codes, headers, types, headWidth, yrYhsLen, false + return codes, headers, types, headWidth, false } func getExcelFile(headers []string, headWidth []float64, values [][]interface{}) { @@ -154,6 +158,61 @@ func getColumnRowName(columnName []byte, rowIndex int) (columnRowName string) { return } +// QueryB01 获取机构树信息 +func QueryB01(db *sql.DB) []*dept { + rows, _ := db.Query("select b0111,b0121,b0101 from b01 where b0111<>'-1' order by b0121,sortid") + defer rows.Close() + var b0111, b0121, b0101 string + depts := make([]*dept, 0) + for rows.Next() { + _ = rows.Scan(&b0111, &b0121, &b0101) + depts = append(depts, &dept{ + Id: b0111, + Pid: b0121, + Name: b0101, + Child: make([]*dept, 0), + }) + } + + return depts +} + +// QueryA01 获取人员信息 +func QueryA01(db *sql.DB, codes string, types []string, b0111 string, allObj *[][]interface{}) { + rows, _ := db.Query(fmt.Sprintf(`select %s from a01 where a0163='1' and exists(select 1 from a02 a02 where a01.a0000=a02.a0000 and a0281='true' and a02.A0201B='%s') order by (select lpad(max(a0225),5,'0') from a02 where a01.a0000=a02.a0000 and a02.a0281='true' and a02.A0201B='%s')`, codes, b0111, b0111)) + defer rows.Close() + columns, _ := rows.Columns() + columnsLen := len(columns) + scanArgs := make([]interface{}, columnsLen) + values := make([]interface{}, columnsLen) + for i := range values { + scanArgs[i] = &values[i] + } + for rows.Next() { + _ = rows.Scan(scanArgs...) + valuer := make([]interface{}, columnsLen) + setValueStr(values, types, valuer) + *allObj = append(*allObj, valuer) + } +} + +func setValueStr(values []interface{}, types []string, valuer []interface{}) { + for i, col := range values { + if col != nil { + s := string(col.([]byte)) + if types[i] == NULL { + valuer[i] = s + } else if types[i] == DATE { + valuer[i] = s[0:4] + DIAN + s[4:6] + } else { + valuer[i] = CodeValueMap[types[i]+s] + } + } else { + valuer[i] = NULL + } + } +} + // 获取表数据 func QueryMap(db *sql.DB, sqlStr string, m map[string]string) { rows, _ := db.Query(sqlStr) @@ -170,7 +229,7 @@ func has(v1 dept, vs []*dept) bool { has = false for _, v2 := range vs { v3 := *v2 - if v1.Pid+v1.Id == v3.Pid { + if v1.Id == v3.Pid { has = true break } @@ -179,18 +238,12 @@ func has(v1 dept, vs []*dept) bool { } +// makeTree 生成机构树对象 func makeTree(vs []*dept, node *dept) { - fmt.Println("the node value in maketree is:", *node) childs := findChild(node, vs) - fmt.Println(" the child we got is :", childs) for _, child := range childs { - fmt.Println("in the childs's for loop, the child's address here is:", &child) node.Child = append(node.Child, child) - fmt.Println("in the child's for loop, after append the child is:", child) if has(*child, vs) { - fmt.Println("i am in if has") - fmt.Println("the child in if has is:", *child) - fmt.Println("the child in if has 's address is:", child) makeTree(vs, child) } } @@ -198,8 +251,11 @@ func makeTree(vs []*dept, node *dept) { } func findChild(v *dept, vs []*dept) (ret []*dept) { + if ret == nil { + ret = make([]*dept, 0) + } for _, v2 := range vs { - if v.Pid+v.Id == v2.Pid { + if v.Id == v2.Pid { ret = append(ret, v2) } } @@ -218,7 +274,7 @@ type YrYh struct { Width float64 `json:"width"` } -var yryh = `[ +const yryh = `[ {"name":"姓名","code":"a01.a0101","type":"","width":11}, {"name":"性别","code":"a01.a0104","type":"GB2261","width":8}, {"name":"民族","code":"a01.a0117","type":"GB3304","width":8}, @@ -234,10 +290,14 @@ var yryh = `[ {"name":"最高学历","code":"a01.zgxl","type":"","width":15}, {"name":"籍贯","code":"a01.a0111","type":"ZB01","width":12} ]` +const DATE = "DATE" +const NULL = "" +const DIAN = "." // 树结构 type dept struct { Id string `json:"id"` Pid string `json:"pid"` + Name string `json:"name"` Child []*dept `json:"child"` } diff --git a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.syso b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.syso new file mode 100644 index 0000000..b620536 Binary files /dev/null and b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/main.syso differ diff --git a/golang_learn/data_func/go_to_excel/go_to_mc/query_all/rc.ico b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/rc.ico new file mode 100644 index 0000000..a06bf77 Binary files /dev/null and b/golang_learn/data_func/go_to_excel/go_to_mc/query_all/rc.ico differ diff --git a/golang_learn/data_office/get_hy_excel/build.bat b/golang_learn/data_office/get_hy_excel/build.bat new file mode 100644 index 0000000..c4ba358 --- /dev/null +++ b/golang_learn/data_office/get_hy_excel/build.bat @@ -0,0 +1 @@ +go build -ldflags="-s -w -H windowsgui" -o demo.exe \ No newline at end of file