You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
9.7 KiB

package main
/*
思路:
按照三个SQL进行查询结果封装到指定结构体并对比三个查询结果按照姓名和身份证作为关联项任意其他项值不等则保留下来
*/
import (
"database/sql"
"fmt"
"github.com/xuri/excelize/v2"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
var SQLJck = "select ifnull(A0184,'') IDNumber, ifnull(A0101,'') Name, ifnull(substr(a0107, 1, 6),'') BirthDate, ifnull(A0101,'') Gender, ifnull(A0117,'') Ethnicity, ifnull(substr(a0134, 1, 6),'') WorkStartDate, ifnull(A0141,'') PoliticalStatus, ifnull((select MIN(A0801B) from A08 where A0831=1 and A01.A0000=A08.A0000), '') FullTimeEducation, ifnull((select MIN(A0801B) from A08 where A0834=1 and A01.A0000=A08.A0000), '') HighestEducation, ifnull((select MIN(A0901B) from A08 where A0835=1 and A01.A0000=A08.A0000), '') HighestDegree, ifnull(A0123,'') JobCategory, ifnull((select A0501B from A05 where A0531=1 and A0524=1 and A01.A0000=A05.A0000), '') CurrentPositionLevel, ifnull((select substr(A0504, 1, 6) from A05 where A0531=1 and A0524=1 and A01.A0000=A05.A0000), '') CurrentPositionStartDate, ifnull((select A0501B from A05 where A0531=0 and A0524=1 and A01.A0000=A05.A0000), '') CurrentRankLevel, ifnull((select DATE_FORMAT(DATE_SUB(STR_TO_DATE(substr(A0504, 1, 6), '%Y%m'), INTERVAL ifnull(A0532, 0) MONTH), '%Y%m') from A05 where A0531=1 and A0524=1 and A01.A0000=A05.A0000), '') CurrentRankStartDate, ifnull(A0197,'0') Has2YearsGrassrootsExp, ifnull((select MIN(A1517) from A15 where A1521='2023' and A01.A0000=A15.A0000), '') AnnualAssessment2023, ifnull(A0160,'') PersonCategory, ifnull((select 1 from A99Z1 where A99Z101=1 and A99Z103=1 and A99Z102 like '2024%' and A01.A0000=A99Z1.A0000), 0) SelectedFor2024 from A01 where A0163='1'"
var SQLZjk = "select ifnull(M0203,'') IDNumber, ifnull(M0201,'') Name, ifnull(M0204,'') BirthDate, ifnull(M0202,'') Gender, ifnull(M0205,'') Ethnicity, ifnull(M0216,'') WorkStartDate, ifnull(M0206,'') PoliticalStatus, ifnull(M0242,'') FullTimeEducation, ifnull(M0212,'') HighestEducation, ifnull(M0213,'') HighestDegree, ifnull(M0246,'') JobCategory, ifnull(M0208,'') CurrentPositionLevel, ifnull(M0233,'') CurrentPositionStartDate, ifnull(M0243,'') CurrentRankLevel, ifnull(M0244,'') CurrentRankStartDate, ifnull(M0215,'') Has2YearsGrassrootsExp, ifnull(M0201,'') AnnualAssessment2023, ifnull((select case M0104 when 2 then 5 when 3 then 6 else 1 end from M_M01 where M_M01.UNID=M_M02.UNID),'') PersonCategory, ifnull((select 1 from M_M05 where M0503=M0203 and rpt='2024'),0) SelectedFor2024 from M_M02 where rpt='2024'"
var SQLGzk = "select ifnull(S0203,'') IDNumber, ifnull(S0201,'') Name, '' BirthDate, '' Gender, '' Ethnicity, '' WorkStartDate, '' PoliticalStatus, '' FullTimeEducation, ifnull(S0225,'') HighestEducation, '' HighestDegree, '' JobCategory, ifnull(S0204,'') CurrentPositionLevel, '' CurrentPositionStartDate, ifnull(S0205,'') CurrentRankLevel, '' CurrentRankStartDate, '' Has2YearsGrassrootsExp, '' AnnualAssessment2023, '' PersonCategory, '' SelectedFor2024 from m_s02 where rpt='2024' union all select ifnull(S0403,'') IDNumber, ifnull(S0401,'') Name, '' BirthDate, '' Gender, '' Ethnicity, '' WorkStartDate, '' PoliticalStatus, '' FullTimeEducation, ifnull(S0425,'') HighestEducation, '' HighestDegree, '' JobCategory, ifnull(S0404,'') CurrentPositionLevel, '' CurrentPositionStartDate, ifnull(S0405,'') CurrentRankLevel, '' CurrentRankStartDate, '' Has2YearsGrassrootsExp, '' AnnualAssessment2023, '' PersonCategory, '' SelectedFor2024 from m_s04 where rpt='2024'"
// 存在所有对象信息 - 集合
func main() {
db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
fmt.Println("操作数据库连接:", db)
// 创建临时文件夹
dir := time.Now().Format("2006102150405")
file := fmt.Sprintf("对比结果-%s.xlsx", dir)
peoplesJck := queryBySQL(db, SQLJck)
peoplesZjk := queryBySQL(db, SQLZjk)
peoplesGzk := queryBySQL(db, SQLGzk)
results := mergeResults(peoplesJck, peoplesZjk, peoplesGzk)
// 创建一个新的 Excel 文件
f := excelize.NewFile()
sheetName := "Sheet1"
// 设置表头
headers := []string{"身份证号", "姓名", "项名称", "基础库", "中间库", "工资库"}
for i, header := range headers {
cell := fmt.Sprintf("%c1", 'A'+i)
_ = f.SetCellValue(sheetName, cell, header)
}
// 写入数据
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)
_ = f.SetCellValue(sheetName, fmt.Sprintf("F%d", row), diff.Gzk)
}
// 保存文件
if err := f.SaveAs(file); err != nil {
fmt.Println("保存文件失败:", err)
} else {
fmt.Printf("文件已保存为 %s", file)
}
fmt.Println("执行结束!")
}
func queryBySQL(db *sql.DB, SQLJck string) []Person {
rows, err := db.Query(SQLJck)
if err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
fmt.Printf("错误")
}
}(rows)
var people []Person
for rows.Next() {
var p Person
err := rows.Scan(
&p.IDNumber,
&p.Name,
&p.BirthDate,
&p.Gender,
&p.Ethnicity,
&p.WorkStartDate,
&p.PoliticalStatus,
&p.FullTimeEducation,
&p.HighestEducation,
&p.HighestDegree,
&p.JobCategory,
&p.CurrentPositionLevel,
&p.CurrentPositionStartDate,
&p.CurrentRankLevel,
&p.CurrentRankStartDate,
&p.Has2YearsGrassrootsExp,
&p.AnnualAssessment2023,
&p.PersonCategory,
&p.SelectedFor2024,
)
if err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
people = append(people, p)
}
if err = rows.Err(); err != nil {
_ = fmt.Errorf("发生报错:%s", err)
}
return people
}
func mergeResults(peoplesJck, peoplesZjk, peoplesGzk []Person) []Diff {
jckMap := mapPersonToDiff(peoplesJck, "基础库")
zjkMap := mapPersonToDiff(peoplesZjk, "中间库")
gzkMap := mapPersonToDiff(peoplesGzk, "工资库")
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 gzkMap {
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
}
if jckMap[key][colName] != zjkMap[key][colName] ||
jckMap[key][colName] != gzkMap[key][colName] ||
zjkMap[key][colName] != gzkMap[key][colName] {
diff := Diff{
IDNumber: idNumber,
Name: name,
ColName: colName,
Jck: jckMap[key][colName],
Zjk: zjkMap[key][colName],
Gzk: gzkMap[key][colName],
}
diffs = append(diffs, diff)
}
}
}
return diffs
}
func mapPersonToDiff(people []Person, 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.BirthDate
result[key]["性别"] = person.Gender
result[key]["民族"] = person.Ethnicity
result[key]["参加工作时间"] = person.WorkStartDate
result[key]["政治面貌"] = person.PoliticalStatus
result[key]["全日制学历"] = person.FullTimeEducation
result[key]["最高学历"] = person.HighestEducation
result[key]["最高学位"] = person.HighestDegree
result[key]["职位类别"] = person.JobCategory
result[key]["现职务层次"] = person.CurrentPositionLevel
result[key]["任现职务层次时间"] = person.CurrentPositionStartDate
result[key]["现职级层次"] = person.CurrentRankLevel
result[key]["任现职级层次时间"] = person.CurrentRankStartDate
result[key]["是否具有2年以上基层经验"] = person.Has2YearsGrassrootsExp
result[key]["2023年度考核结果"] = person.AnnualAssessment2023
result[key]["人员类别"] = person.PersonCategory
result[key]["2024年考录选调生"] = person.SelectedFor2024
}
return result
}
type Person struct {
IDNumber string // 身份证号
Name string // 姓名
BirthDate string // 出生时间
Gender string // 性别
Ethnicity string // 民族
WorkStartDate string // 参加工作时间
PoliticalStatus string // 政治面貌
FullTimeEducation string // 全日制学历
HighestEducation string // 最高学历
HighestDegree string // 最高学位
JobCategory string // 职位类别
CurrentPositionLevel string // 现职务层次
CurrentPositionStartDate string // 任现职务层次时间
CurrentRankLevel string // 现职级层次
CurrentRankStartDate string // 任现职级层次时间
Has2YearsGrassrootsExp string // 是否具有2年以上基层经验
AnnualAssessment2023 string // 2023年度考核结果
PersonCategory string // 人员类别
SelectedFor2024 string // 2024年考录选调生
}
type Diff struct {
IDNumber string `json:"身份证号"` // 身份证号
Name string `json:"姓名"` // 姓名
ColName string `json:"项名称"` // 项名称
Jck string `json:"基础库"` // 基础库
Zjk string `json:"中间库"` // 中间库
Gzk string `json:"工资库"` // 工资库
}