Browse Source

实现基本操作

master
张献维 2 weeks ago
parent
commit
5c8de3d9b8
  1. 197
      golang_learn/data_office/get_hy_jctj/main.go

197
golang_learn/data_office/get_hy_jctj/main.go

@ -109,6 +109,24 @@ select
from m_s04 where rpt='2024' from m_s04 where rpt='2024'
` `
var SQLCzk = `
select
A0101 Name,
A0184 IDNumber,
ifnull((select min(code_name) from code_value where A0221=code_value and code_type='ZB09'),'') CurrentPositionLevel,
ifnull((select min(code_name) from code_value where A0192E=code_value and code_type='ZB148'),'') CurrentRankLevel,
ifnull(A0195, '') OldWork from C01 where C0105 like '2024%'
`
var SQLBhb = `
select
M0501 Name,
M0503 IDNumber,
ifnull((select min(code_name) from code_value where M0515=code_value and code_type='ZB09'),'') CurrentPositionLevel,
ifnull((select min(code_name) from code_value where M0515=code_value and code_type='ZB148'),'') CurrentRankLevel,
M0510 OldWork from M_M05 a where M0511=2 and M0514=3
`
// 存在所有对象信息 - 集合 // 存在所有对象信息 - 集合
func main() { func main() {
@ -123,11 +141,36 @@ func main() {
peoplesZjk := queryBySQL(db, SQLZjk) peoplesZjk := queryBySQL(db, SQLZjk)
peoplesGzk := queryBySQL(db, SQLGzk) peoplesGzk := queryBySQL(db, SQLGzk)
results := mergeResults(peoplesJck, peoplesZjk, peoplesGzk) results1 := mergeResults(peoplesJck, peoplesZjk, peoplesGzk)
peoplesCzk := queryByCzSQL(db, SQLCzk)
peoplesBhb := queryByCzSQL(db, SQLBhb)
results2 := mergeCzResults(peoplesCzk, peoplesBhb)
// 创建一个新的 Excel 文件 // 创建一个新的 Excel 文件
f := excelize.NewFile() f := excelize.NewFile()
sheetName := "Sheet1" _ = f.NewSheet("三库对比")
_ = f.NewSheet("辞职人员对比")
saveSheet1(f, results1)
saveSheet2(f, results2)
f.DeleteSheet("Sheet1")
f.SetActiveSheet(1)
// 保存文件
if err := f.SaveAs(file); err != nil {
fmt.Println("保存文件失败:", err)
} else {
fmt.Printf("文件已保存为 %s", file)
}
fmt.Println("执行结束!")
}
/*
*/
func saveSheet1(f *excelize.File, results []Diff) {
sheetName := "三库对比"
// 设置表头 // 设置表头
headers := []string{"身份证号", "姓名", "项名称", "基础库", "中间库", "工资库"} headers := []string{"身份证号", "姓名", "项名称", "基础库", "中间库", "工资库"}
@ -146,17 +189,34 @@ func main() {
_ = f.SetCellValue(sheetName, fmt.Sprintf("E%d", row), diff.Zjk) _ = f.SetCellValue(sheetName, fmt.Sprintf("E%d", row), diff.Zjk)
_ = f.SetCellValue(sheetName, fmt.Sprintf("F%d", row), diff.Gzk) _ = f.SetCellValue(sheetName, fmt.Sprintf("F%d", row), diff.Gzk)
} }
}
// 保存文件 /*
if err := f.SaveAs(file); err != nil { */
fmt.Println("保存文件失败:", err) func saveSheet2(f *excelize.File, results []Diff) {
} else { sheetName := "辞职人员对比"
fmt.Printf("文件已保存为 %s", file)
// 设置表头
headers := []string{"身份证号", "姓名", "项名称", "辞职库", "中间库"}
for i, header := range headers {
cell := fmt.Sprintf("%c1", 'A'+i)
_ = f.SetCellValue(sheetName, cell, header)
} }
fmt.Println("执行结束!") // 写入数据
for i, diff := range results {
row := i + 2 // 数据从第二行开始
_ = f.SetCellValue(sheetName, fmt.Sprintf("A%d", row), diff.IDNumber)
_ = f.SetCellValue(sheetName, fmt.Sprintf("B%d", row), diff.Name)
_ = f.SetCellValue(sheetName, fmt.Sprintf("C%d", row), diff.ColName)
_ = f.SetCellValue(sheetName, fmt.Sprintf("D%d", row), diff.Jck)
_ = f.SetCellValue(sheetName, fmt.Sprintf("E%d", row), diff.Zjk)
}
} }
/*
queryBySQL 按照基础脚本查询
*/
func queryBySQL(db *sql.DB, SQLJck string) []Person { func queryBySQL(db *sql.DB, SQLJck string) []Person {
rows, err := db.Query(SQLJck) rows, err := db.Query(SQLJck)
if err != nil { if err != nil {
@ -205,6 +265,46 @@ func queryBySQL(db *sql.DB, SQLJck string) []Person {
return people return people
} }
/*
queryByCzSQL 按照辞职脚本查询
*/
func queryByCzSQL(db *sql.DB, SQLCz string) []CzPerson {
rows, err := db.Query(SQLCz)
if err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
fmt.Printf("错误")
}
}(rows)
var people []CzPerson
for rows.Next() {
var p CzPerson
err := rows.Scan(
&p.Name,
&p.IDNumber,
&p.CurrentPositionLevel,
&p.CurrentRankLevel,
&p.OldWork,
)
if err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
people = append(people, p)
}
if err = rows.Err(); err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
return people
}
/*
mergeResults 基础人员对比
*/
func mergeResults(peoplesJck, peoplesZjk, peoplesGzk []Person) []Diff { func mergeResults(peoplesJck, peoplesZjk, peoplesGzk []Person) []Diff {
jckMap := mapPersonToDiff(peoplesJck, "基础库") jckMap := mapPersonToDiff(peoplesJck, "基础库")
zjkMap := mapPersonToDiff(peoplesZjk, "中间库") zjkMap := mapPersonToDiff(peoplesZjk, "中间库")
@ -269,6 +369,58 @@ func mergeResults(peoplesJck, peoplesZjk, peoplesGzk []Person) []Diff {
return diffs return diffs
} }
/*
mergeCzResults 辞职人员对比
*/
func mergeCzResults(peoplesCzk, peoplesBhb []CzPerson) []Diff {
jckMap := mapCzPersonToDiff(peoplesCzk, "辞职库")
zjkMap := mapCzPersonToDiff(peoplesBhb, "辞职表")
var diffs []Diff
// 遍历所有可能的键
allKeys := make(map[string]bool)
for key := range jckMap {
allKeys[key] = true
}
for key := range zjkMap {
allKeys[key] = true
}
for key := range allKeys {
idNumber := strings.Split(key, "_")[0]
name := strings.Split(key, "_")[1]
// 获取每个属性的值
for colName := range jckMap[key] {
if colName == "身份证号" || colName == "姓名" {
continue
}
var diff Diff
// 需要公共比较
if colName == "职务层次" || colName == "职级" {
if jckMap[key][colName] == zjkMap[key][colName] ||
jckMap[key]["职务层次"] == zjkMap[key]["职级"] ||
jckMap[key]["职级"] == zjkMap[key]["职务层次"] {
continue
}
}
if jckMap[key][colName] != zjkMap[key][colName] {
diff = Diff{
IDNumber: idNumber,
Name: name,
ColName: colName,
Jck: jckMap[key][colName],
Zjk: zjkMap[key][colName],
}
diffs = append(diffs, diff)
}
}
}
return diffs
}
// 示例数组 // 示例数组
var gzMap map[string]bool var gzMap map[string]bool
@ -281,6 +433,9 @@ func init() {
} }
} }
/*
mapPersonToDiff 基础查询对比
*/
func mapPersonToDiff(people []Person, source string) map[string]map[string]string { func mapPersonToDiff(people []Person, source string) map[string]map[string]string {
result := make(map[string]map[string]string) result := make(map[string]map[string]string)
for _, person := range people { for _, person := range people {
@ -311,6 +466,25 @@ func mapPersonToDiff(people []Person, source string) map[string]map[string]strin
return result return result
} }
/*
mapCzPersonToDiff 辞职查询对比
*/
func mapCzPersonToDiff(people []CzPerson, source string) map[string]map[string]string {
result := make(map[string]map[string]string)
for _, person := range people {
key := person.IDNumber + "_" + person.Name
if result[key] == nil {
result[key] = make(map[string]string)
}
result[key]["身份证号"] = person.IDNumber
result[key]["姓名"] = person.Name
result[key]["职务层次"] = person.CurrentPositionLevel
result[key]["职级"] = person.CurrentRankLevel
result[key]["原职务"] = person.OldWork
}
return result
}
type Person struct { type Person struct {
IDNumber string // 身份证号 IDNumber string // 身份证号
Name string // 姓名 Name string // 姓名
@ -332,6 +506,13 @@ type Person struct {
PersonCategory string // 人员类别 PersonCategory string // 人员类别
SelectedFor2024 string // 2024年考录选调生 SelectedFor2024 string // 2024年考录选调生
} }
type CzPerson struct {
IDNumber string // 身份证号
Name string // 姓名
CurrentPositionLevel string // 职务层次
CurrentRankLevel string // 职级
OldWork string // 原职务
}
type Diff struct { type Diff struct {
IDNumber string `json:"身份证号"` // 身份证号 IDNumber string `json:"身份证号"` // 身份证号

Loading…
Cancel
Save