Sfoglia il codice sorgente

抽取版本对比

zhangjinkun 6 anni fa
parent
commit
755286724b

+ 14 - 19
versioncomparison/config.json

@@ -1,21 +1,16 @@
 {
-  "extractmgo": "192.168.3.207:27081",
-  "extractdb": "qfw",
-  "extractc": "bidding",
-  "previousmgo": "192.168.3.207:27081",
-  "previousdb": "qfw",
-  "previousc": "result_v3",
-  "newmgo": "192.168.3.207:27081",
-  "newdb": "extract_v3",
-  "newc": "result_data2",
-  "keyfield": {
-    "bidamount": 1,
-    "budget": 1,
-    "winner": 1,
-    "projectcode":1,
-    "buyer": 1,
-    "projectname": 1
-  },
-  "queryNum": 1000,
-  "querySid": "57319151edbcdc7b27000aec"
+    "premgo": "192.168.3.207:27081",
+    "predb": "qfw",
+    "prec": "result_v3",
+    "newmgo": "192.168.3.207:27081",
+    "newdb": "extract_v3",
+    "newc": "result_data",
+    "fields": [
+        "projectname",
+        "projectcode",
+        "buyer",
+        "bidamount",
+        "budget",
+        "winner"
+    ]
 }

+ 423 - 0
versioncomparison/main.bak

@@ -0,0 +1,423 @@
+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
+}

+ 142 - 385
versioncomparison/main.go

@@ -1,423 +1,180 @@
+/*
+抽取结果对比
+*/
 package main
 
 import (
+	"flag"
 	"fmt"
-	"gopkg.in/mgo.v2"
-	"gopkg.in/mgo.v2/bson"
 	"jy/mongodbutil"
 	"log"
-	"qfw/common/src/github.com/tealeg/xlsx"
-	"qfw/util"
-	"strings"
+	qu "qfw/util"
+
+	"github.com/tealeg/xlsx"
+	"gopkg.in/mgo.v2/bson"
 )
 
 var (
-	SysConfig   map[string]interface{}
-	Extractmgo  *mgo.Session      //抽取
-	Previousmgo *mongodbutil.Pool //之前抽取
-	Newmgo      *mongodbutil.Pool //最新抽取
+	SysConfig map[string]interface{}
+	Premgo    *mongodbutil.Pool //上个版本库
+	Newmgo    *mongodbutil.Pool //当前版本库
+	FieldData map[string]map[string]*Data
+	Compares  map[string]*Compare
+	Sid, Eid  string
+	Fields    []string
 )
 
-/**
-与上个抽取版本做比较
- */
-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 Compare struct {
+	Field                string //属性
+	PreExtNum, NewExtNum int    //上个版、当前版有值数量
+	PreNilnum, NewNilnum int    //上个版、当前版无值数量
+	EqNum, NEqNum        int    //相等、不等数据量
 }
 
-type versionComparison struct {
-	Id  interface{} `json:"_id"`
-	Url string      `json:"url"`
+type Data struct {
+	Id             string
+	PreVal, NewVal string
 }
 
+func init() {
+	flag.StringVar(&Sid, "sid", "5d348c0ca5cb26b9b76a4bb8", "开始id")
+	flag.StringVar(&Eid, "eid", "5d34ae22a5cb26b9b7850b43", "结束id")
+	flag.Parse()
+	qu.ReadConfig(&SysConfig)
+	Premgo = mongodbutil.MgoFactory(1, 3, 120, qu.ObjToString(SysConfig["premgo"]), qu.ObjToString(SysConfig["predb"]))
+	Newmgo = mongodbutil.MgoFactory(1, 3, 120, qu.ObjToString(SysConfig["newmgo"]), qu.ObjToString(SysConfig["newdb"]))
+	tmp, _ := SysConfig["fields"].([]interface{})
+	for _, v := range tmp {
+		Fields = append(Fields, qu.ObjToString(v))
+	}
+	FieldData = map[string]map[string]*Data{}
+	Compares = map[string]*Compare{}
+}
 func main() {
-	Query(util.IntAll(SysConfig["queryNum"]), util.ObjToString(SysConfig["querySid"]))
+	getVersionData()
+	createXlsx()
 }
-
-func Query(num int, sid string) {
-	xf, err := xlsx.OpenFile("抽取结果对比.xlsx")
+func createXlsx() {
+	xf, err := xlsx.OpenFile("template.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")
+		log.Println(err)
 		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
+	//生成第一个sheet信息
+	sh := xf.Sheets[0]
+	for i, field := range Fields {
+		for k, row := range sh.Rows {
+			if k > 2+i {
+				style := (*row).Cells[1].GetStyle()
+				style.Font.Color = "000000"
+				(*row).Cells[0].SetString(field)
+				(*row).Cells[1].SetInt(Compares[field].PreExtNum)
+				(*row).Cells[1].SetStyle(style)
+				(*row).Cells[2].SetInt(Compares[field].NewExtNum)
+				(*row).Cells[2].SetStyle(style)
+				(*row).Cells[3].SetInt(Compares[field].EqNum)
+				(*row).Cells[3].SetStyle(style)
+				(*row).Cells[4].SetInt(Compares[field].NEqNum)
+				(*row).Cells[4].SetStyle(style)
+			}
+			sh.Rows[k] = row
 		}
-		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
+	//生成信息sheet
+	url := "https://www.jianyu360.com/article/content/%s.html"
+	for _, field := range Fields {
+		sh, _ := xf.AddSheet(field)
+		rowh := sh.AddRow()
+		rowh.AddCell().SetString("id")
+		rowh.AddCell().SetString("preval")
+		rowh.AddCell().SetString("newval")
+		rowh.AddCell().SetString("url")
+		tmp := FieldData[field]
+		for k, v := range tmp {
+			if v.NewVal != v.PreVal {
+				row := sh.AddRow()
+				row.AddCell().SetString(k)
+				row.AddCell().SetString(v.PreVal)
+				row.AddCell().SetString(v.NewVal)
+				row.AddCell().SetString(fmt.Sprintf(url, qu.CommonEncodeArticle("content", v.Id)))
+			}
 		}
-		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++
-					}
-				}
+	}
+	err = xf.Save("result.xlsx")
+	if err != nil {
+		log.Println("保存xlsx失败:", err)
+		return
+	}
+	log.Println("xlsx保存成功")
+}
+func getVersionData() {
+	query := bson.M{"_id": bson.M{"$gte": bson.ObjectIdHex(Sid), "$lte": bson.ObjectIdHex(Eid)}}
+	log.Println(qu.ObjToString(SysConfig["prec"]), query)
+	list1, _ := Premgo.Find(qu.ObjToString(SysConfig["prec"]), query, nil, `{}`, false, -1, -1)
+	for _, v := range *list1 {
+		for _, key := range Fields {
+			rd := FieldData[key]
+			if rd == nil {
+				rd = map[string]*Data{}
 			}
-			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++
-					}
-				}
+			rd[qu.BsonIdToSId(v["_id"])] = &Data{
+				Id:     qu.BsonIdToSId(v["_id"]),
+				PreVal: fmt.Sprint(v[key]),
 			}
+			FieldData[key] = rd
 		}
-		fmt.Println()
 	}
+	log.Println("pre version 加载完成", len(*list1))
 
-	//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)
-					}
+	list2, _ := Newmgo.Find(qu.ObjToString(SysConfig["newc"]), query, nil, `{}`, false, -1, -1)
+	for _, v := range *list2 {
+		for _, field := range Fields {
+			rd := FieldData[field]
+			if rd == nil {
+				rd = map[string]*Data{}
+			}
+			_id := qu.BsonIdToSId(v["_id"])
+			tmp := rd[_id]
+			if tmp != nil {
+				tmp.NewVal = fmt.Sprint(v[field])
+				rd[_id] = tmp
+			} else {
+				rd[_id] = &Data{
+					NewVal: fmt.Sprint(v[field]),
 				}
 			}
+			FieldData[field] = rd
 		}
-		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)
+	}
+	log.Println("new version 加载完成", len(*list2))
+	for k, v := range FieldData {
+		cp := &Compare{Field: k}
+		for _, d := range v {
+			if d.NewVal != "" && d.PreVal != "" {
+				if d.NewVal == d.PreVal {
+					cp.EqNum++
+				} else {
+					cp.NEqNum++
 				}
-			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)
+				cp.PreExtNum++
+				cp.NewExtNum++
+			} else {
+				if d.NewVal == "" {
+					cp.NewNilnum++
+					if d.PreVal != "" {
+						cp.NEqNum++
+						cp.PreExtNum++
+					}
 				}
-			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)
+				if d.PreVal == "" {
+					cp.PreNilnum++
+					if d.NewVal != "" {
+						cp.NewExtNum++
+						cp.NEqNum++
+					}
 				}
 			}
 		}
+		Compares[k] = cp
 	}
-	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
 }

BIN
versioncomparison/template.xlsx


BIN
versioncomparison/抽取结果对比.xlsx