package main import ( "fmt" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "jy/mongodbutil" "log" "qfw/common/src/github.com/tealeg/xlsx" "qfw/util" "strings" ) var ( SysConfig map[string]interface{} Extractmgo *mgo.Session //抽取 Previousmgo *mongodbutil.Pool //之前抽取 Newmgo *mongodbutil.Pool //最新抽取 ) /** 与上个抽取版本做比较 */ func init() { util.ReadConfig(&SysConfig) if len(SysConfig) < 1 { log.Println("配置文件读取失败") return } session, e := mgo.Dial(util.ObjToString(SysConfig["extractmgo"])) if e != nil { log.Fatal(e) } Extractmgo = session Previousmgo = mongodbutil.MgoFactory(2, 5, 120, util.ObjToString(SysConfig["previousmgo"]), util.ObjToString(SysConfig["previousdb"])) Newmgo = mongodbutil.MgoFactory(2, 5, 120, util.ObjToString(SysConfig["newmgo"]), util.ObjToString(SysConfig["newdb"])) } type versionComparison struct { Id interface{} `json:"_id"` Url string `json:"url"` } func main() { Query(util.IntAll(SysConfig["queryNum"]), util.ObjToString(SysConfig["querySid"])) } func Query(num int, sid string) { xf, err := xlsx.OpenFile("抽取结果对比.xlsx") if err != nil { log.Println("读取文件", err) return } var projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum int //不相等计数器 var projectcodenumXT, bidamountnumXT, winnernumXT, buyernumXT, budgetnumXT, projectnamenumXT int //相等计数器 var pcodeNotNilNumP, bidamountNotNilNumP, winnerNotNilNumP, buyerNotNilNumP, budgetNotNilNumP, pnameNotNilNumP int //不相等计数器 var pcodeNotNilNumN, bidamountNotNilNumN, winnerNotNilNumN, buyerNotNilNumN, budgetNotNilNumN, pnameNotNilNumN int //不相等计数器 log.Println(num, sid) if num < 1 { log.Println("查询数量应该大于0") return } sum := num //总量 //if strings.TrimSpace(gteid) == "" { // gteid = "386cd3000000000000000000" //} var iter *mgo.Iter if sid == "" { iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(nil).Select(bson.M{"_id": 1}).Iter() } else { iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(bson.M{"_id": bson.M{ "$gte": bson.ObjectIdHex(sid)}, }).Select(bson.M{"_id": 1}).Iter() } defer log.Println("关闭 iter:", iter.Close()) var data map[string]bson.ObjectId getdata := make([]bson.ObjectId, 0) for iter.Next(&data) { if num == 0 { break } getdata = append(getdata, data["_id"]) num-- } log.Println(sum, "条数据加载完成") projectnames := make([]*Projectname, 0) buyers := make([]*Buyer, 0) projectcodes := make([]*Projectcode, 0) winners := make([]*Winner, 0) budgets := make([]*Budget, 0) bidamounts := make([]*Bidamount, 0) for _, gv := range getdata { log.Println(gv) gvid := gv.Hex() pdata, b := Previousmgo.FindById(util.ObjToString(SysConfig["previousc"]), gvid, SysConfig["keyfield"]) if !b || len(*pdata) == 0 { log.Println("oldId不存在") continue } log.Println("pdata:", pdata) ndata, b := Newmgo.FindById(util.ObjToString(SysConfig["newc"]), gvid, SysConfig["keyfield"]) if !b || len(*ndata) == 0 { log.Println("nweId不存在") continue } log.Println("ndata:", ndata) versioncomparison := new(versionComparison) versioncomparison.Id = gvid versioncomparison.Url = "https://www.jianyu360.com/article/content/" + util.CommonEncodeArticle("content", gvid) + ".html" for k := range SysConfig["keyfield"].(map[string]interface{}) { var pd interface{} var nd interface{} if k == "budget" || k == "bidamount" { pd = util.Float64All((*pdata)[k]) nd = util.Float64All((*ndata)[k]) if pd.(float64) > 0 { switch k { case "budget": budgetNotNilNumP++ case "bidamount": bidamountNotNilNumP++ } } if nd.(float64) > 0 { switch k { case "budget": budgetNotNilNumN++ case "bidamount": bidamountNotNilNumN++ } } } else { pd = strings.TrimSpace(util.ObjToString((*pdata)[k])) nd = strings.TrimSpace(util.ObjToString((*ndata)[k])) if strings.TrimSpace(pd.(string)) != "" { switch k { case "projectname": pnameNotNilNumP++ case "buyer": buyerNotNilNumP++ case "projectcode": pcodeNotNilNumP++ case "winner": winnerNotNilNumP++ } } if strings.TrimSpace(nd.(string)) != "" { switch k { case "projectname": pnameNotNilNumN++ case "buyer": buyerNotNilNumN++ case "projectcode": pcodeNotNilNumN++ case "winner": winnerNotNilNumN++ } } } if pd != nd { //log.Println(k) switch k { case "projectname": projectname := new(Projectname) projectname.versionComparison = *versioncomparison pd = strings.Trim(fmt.Sprint(pd), "项目") pd = strings.Trim(strings.TrimSpace(fmt.Sprint(pd)), "采购") nd = strings.Trim(fmt.Sprint(nd), "项目") nd = strings.Trim(strings.TrimSpace(fmt.Sprint(nd)), "采购") if pd != nd{ projectname.ProjectnameOld = fmt.Sprint(pd) projectname.ProjectnameNew = fmt.Sprint(nd) projectnames = append(projectnames, projectname) projectnamenum++ }else { projectnamenumXT++ } case "buyer": buyer := new(Buyer) buyer.versionComparison = *versioncomparison buyer.BuyerOld = fmt.Sprint(pd) buyer.BuyerNew = fmt.Sprint(nd) buyers = append(buyers, buyer) buyernum++ case "projectcode": projectcode := new(Projectcode) projectcode.ProjectcodeOld = fmt.Sprint(pd) projectcode.ProjectcodeNew = fmt.Sprint(nd) projectcode.versionComparison = *versioncomparison projectcodes = append(projectcodes, projectcode) projectcodenum++ case "winner": winner := new(Winner) winner.WinnerOld = fmt.Sprint(pd) winner.WinnerNew = fmt.Sprint(nd) winner.versionComparison = *versioncomparison winners = append(winners, winner) winnernum++ case "budget": budget := new(Budget) budget.BudgetOld = fmt.Sprint(pd) budget.BudgetNew = fmt.Sprint(nd) budget.versionComparison = *versioncomparison budgets = append(budgets, budget) budgetnum++ case "bidamount": bidamount := new(Bidamount) bidamount.BidamountOld = fmt.Sprint(pd) bidamount.BidamountNew = fmt.Sprint(nd) bidamount.versionComparison = *versioncomparison bidamounts = append(bidamounts, bidamount) bidamountnum++ } }else { //相同统计 pd = strings.TrimSpace(fmt.Sprint(pd)) nd = strings.TrimSpace(fmt.Sprint(nd)) if pd == ""||pd == "0" || nd == ""|| nd == "0"{ continue } if pd == nd { switch k { case "projectname": projectnamenumXT++ case "buyer": buyernumXT++ case "projectcode": projectcodenumXT++ case "winner": winnernumXT++ case "budget": budgetnumXT++ case "bidamount": bidamountnumXT++ } } } } fmt.Println() } //log.Println(projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum) for ins, ivs := range xf.Sheets { for inr, ivr := range ivs.Rows { for _, ivc := range ivr.Cells { //抽取对比 if ins == 0 { if inr < 3 { continue } switch strings.TrimSpace(ivc.String()) { case "projectname": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(pnameNotNilNumP) ivr.Cells[2].SetValue(pnameNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(projectnamenumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(projectnamenum) ivr.Cells[4].SetStyle(style) case "buyer": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(buyerNotNilNumP) ivr.Cells[2].SetValue(buyerNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(buyernumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(buyernum) ivr.Cells[4].SetStyle(style) case "projectcode": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(pcodeNotNilNumP) ivr.Cells[2].SetValue(pcodeNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(projectcodenumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(projectcodenum) ivr.Cells[4].SetStyle(style) case "winner": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(winnerNotNilNumP) ivr.Cells[2].SetValue(winnerNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(winnernumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(winnernum) ivr.Cells[4].SetStyle(style) case "budget": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(budgetNotNilNumP) ivr.Cells[2].SetValue(budgetNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(budgetnumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(budgetnum) ivr.Cells[4].SetStyle(style) case "bidamount": style := ivr.Cells[1].GetStyle() style.Font.Color = "000000" ivr.Cells[1].SetValue(bidamountNotNilNumP) ivr.Cells[2].SetValue(bidamountNotNilNumN) //结果相同数量 ivr.Cells[3].SetValue(bidamountnumXT) ivr.Cells[3].SetStyle(style) //结果不同数量 ivr.Cells[4].SetValue(bidamountnum) ivr.Cells[4].SetStyle(style) } } } } if ins > 0 { if len(ivs.Rows) == 0 { row := ivs.AddRow() row.AddCell().SetValue("ObjectId") row.AddCell().SetValue("dev3.1.2") row.AddCell().SetValue("dev3.2") row.AddCell().SetValue("URL") } //log.Println(ivs.Name) switch strings.TrimSpace(ivs.Name) { case "projectname": for _, v := range projectnames { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.ProjectnameOld) row.AddCell().SetValue(v.ProjectnameNew) row.AddCell().SetValue(v.Url) } case "buyer": for _, v := range buyers { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.BuyerOld) row.AddCell().SetValue(v.BuyerNew) row.AddCell().SetValue(v.Url) } case "projectcode": for _, v := range projectcodes { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.ProjectcodeOld) row.AddCell().SetValue(v.ProjectcodeNew) row.AddCell().SetValue(v.Url) } case "winner": for _, v := range winners { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.WinnerOld) row.AddCell().SetValue(v.WinnerNew) row.AddCell().SetValue(v.Url) } case "budget": for _, v := range budgets { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.BudgetOld) row.AddCell().SetValue(v.BudgetNew) row.AddCell().SetValue(v.Url) } case "bidamount": for _, v := range bidamounts { row := ivs.AddRow() row.AddCell().SetValue(v.Id) row.AddCell().SetValue(v.BidamountOld) row.AddCell().SetValue(v.BidamountNew) row.AddCell().SetValue(v.Url) } } } } err = xf.Save("resultdata.xlsx") if err != nil { log.Println("保存xlsx失败:", err) return } log.Println("xlsx保存成功") } type Projectname struct { versionComparison ProjectnameOld string ProjectnameNew string } type Buyer struct { versionComparison BuyerOld string BuyerNew string } type Projectcode struct { versionComparison ProjectcodeOld string ProjectcodeNew string } type Winner struct { versionComparison WinnerOld string WinnerNew string } type Budget struct { versionComparison BudgetOld string BudgetNew string } type Bidamount struct { versionComparison BidamountOld string BidamountNew string }