main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. "jy/mongodbutil"
  7. "log"
  8. "qfw/common/src/github.com/tealeg/xlsx"
  9. "qfw/util"
  10. "strings"
  11. )
  12. var (
  13. SysConfig map[string]interface{}
  14. Extractmgo *mgo.Session //抽取
  15. Previousmgo *mongodbutil.Pool //之前抽取
  16. Newmgo *mongodbutil.Pool //最新抽取
  17. )
  18. /**
  19. 与上个抽取版本做比较
  20. */
  21. func init() {
  22. util.ReadConfig(&SysConfig)
  23. if len(SysConfig) < 1 {
  24. log.Println("配置文件读取失败")
  25. return
  26. }
  27. session, e := mgo.Dial(util.ObjToString(SysConfig["extractmgo"]))
  28. if e != nil {
  29. log.Fatal(e)
  30. }
  31. Extractmgo = session
  32. Previousmgo = mongodbutil.MgoFactory(2, 5, 120, util.ObjToString(SysConfig["previousmgo"]), util.ObjToString(SysConfig["previousdb"]))
  33. Newmgo = mongodbutil.MgoFactory(2, 5, 120, util.ObjToString(SysConfig["newmgo"]), util.ObjToString(SysConfig["newdb"]))
  34. }
  35. type versionComparison struct {
  36. Id interface{} `json:"_id"`
  37. Url string `json:"url"`
  38. }
  39. func main() {
  40. Query(util.IntAll(SysConfig["queryNum"]), util.ObjToString(SysConfig["querySid"]))
  41. }
  42. func Query(num int, sid string) {
  43. xf, err := xlsx.OpenFile("抽取结果对比.xlsx")
  44. if err != nil {
  45. log.Println("读取文件", err)
  46. return
  47. }
  48. var projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum int //不相等计数器
  49. var pcodeNotNilNumP, bidamountNotNilNumP, winnerNotNilNumP, buyerNotNilNumP, budgetNotNilNumP, pnameNotNilNumP int //不相等计数器
  50. var pcodeNotNilNumN, bidamountNotNilNumN, winnerNotNilNumN, buyerNotNilNumN, budgetNotNilNumN, pnameNotNilNumN int //不相等计数器
  51. log.Println(num, sid)
  52. if num < 1 {
  53. log.Println("查询数量应该大于0")
  54. return
  55. }
  56. sum := num //总量
  57. //if strings.TrimSpace(gteid) == "" {
  58. // gteid = "386cd3000000000000000000"
  59. //}
  60. var iter *mgo.Iter
  61. if sid == "" {
  62. iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(nil).Select(bson.M{"_id": 1}).Iter()
  63. } else {
  64. iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(bson.M{"_id": bson.M{
  65. "$gte": bson.ObjectIdHex(sid)},
  66. }).Select(bson.M{"_id": 1}).Iter()
  67. }
  68. defer log.Println("关闭 iter:", iter.Close())
  69. var data map[string]bson.ObjectId
  70. getdata := make([]bson.ObjectId, 0)
  71. for iter.Next(&data) {
  72. if num == 0 {
  73. break
  74. }
  75. getdata = append(getdata, data["_id"])
  76. num--
  77. }
  78. log.Println(sum, "条数据加载完成")
  79. projectnames := make([]*Projectname, 0)
  80. buyers := make([]*Buyer, 0)
  81. projectcodes := make([]*Projectcode, 0)
  82. winners := make([]*Winner, 0)
  83. budgets := make([]*Budget, 0)
  84. bidamounts := make([]*Bidamount, 0)
  85. for _, gv := range getdata {
  86. log.Println(gv)
  87. gvid := gv.Hex()
  88. pdata, b := Previousmgo.FindById(util.ObjToString(SysConfig["previousc"]), gvid, SysConfig["keyfield"])
  89. if !b || len(*pdata) == 0 {
  90. log.Println("oldId不存在")
  91. continue
  92. }
  93. log.Println("pdata:", pdata)
  94. ndata, b := Newmgo.FindById(util.ObjToString(SysConfig["newc"]), gvid, SysConfig["keyfield"])
  95. if !b || len(*ndata) == 0 {
  96. log.Println("nweId不存在")
  97. continue
  98. }
  99. log.Println("ndata:", ndata)
  100. versioncomparison := new(versionComparison)
  101. versioncomparison.Id = gvid
  102. versioncomparison.Url = "https://www.jianyu360.com/article/content/" + util.CommonEncodeArticle("content", gvid) + ".html"
  103. for k := range SysConfig["keyfield"].(map[string]interface{}) {
  104. var pd interface{}
  105. var nd interface{}
  106. if k == "budget" || k == "bidamount" {
  107. pd = util.Float64All((*pdata)[k])
  108. nd = util.Float64All((*ndata)[k])
  109. } else {
  110. pd = strings.TrimSpace(util.ObjToString((*pdata)[k]))
  111. nd = strings.TrimSpace(util.ObjToString((*ndata)[k]))
  112. }
  113. if pd != nd {
  114. //log.Println(k)
  115. switch k {
  116. case "projectname":
  117. projectname := new(Projectname)
  118. projectname.versionComparison = *versioncomparison
  119. projectname.ProjectnameOld = fmt.Sprint(pd)
  120. projectname.ProjectnameNew = fmt.Sprint(nd)
  121. projectnames = append(projectnames, projectname)
  122. projectnamenum++
  123. if strings.TrimSpace(pd.(string)) != "" {
  124. pnameNotNilNumP++
  125. }
  126. if strings.TrimSpace(nd.(string)) != "" {
  127. pnameNotNilNumN++
  128. }
  129. case "buyer":
  130. buyer := new(Buyer)
  131. buyer.versionComparison = *versioncomparison
  132. buyer.BuyerOld = fmt.Sprint(pd)
  133. buyer.BuyerNew = fmt.Sprint(nd)
  134. buyers = append(buyers, buyer)
  135. buyernum++
  136. if strings.TrimSpace(pd.(string)) != "" {
  137. buyerNotNilNumP++
  138. }
  139. if strings.TrimSpace(nd.(string)) != "" {
  140. buyerNotNilNumN++
  141. }
  142. case "projectcode":
  143. projectcode := new(Projectcode)
  144. projectcode.ProjectcodeOld = fmt.Sprint(pd)
  145. projectcode.ProjectcodeNew = fmt.Sprint(nd)
  146. projectcode.versionComparison = *versioncomparison
  147. projectcodes = append(projectcodes, projectcode)
  148. projectcodenum++
  149. if strings.TrimSpace(pd.(string)) != "" {
  150. pcodeNotNilNumP++
  151. }
  152. if strings.TrimSpace(nd.(string)) != "" {
  153. pcodeNotNilNumN++
  154. }
  155. case "winner":
  156. winner := new(Winner)
  157. winner.WinnerOld = fmt.Sprint(pd)
  158. winner.WinnerNew = fmt.Sprint(nd)
  159. winner.versionComparison = *versioncomparison
  160. winners = append(winners, winner)
  161. winnernum++
  162. if strings.TrimSpace(pd.(string)) != "" {
  163. winnerNotNilNumP++
  164. }
  165. if strings.TrimSpace(nd.(string)) != "" {
  166. winnerNotNilNumN++
  167. }
  168. case "budget":
  169. budget := new(Budget)
  170. budget.BudgetOld = fmt.Sprint(pd)
  171. budget.BudgetNew = fmt.Sprint(nd)
  172. budget.versionComparison = *versioncomparison
  173. budgets = append(budgets, budget)
  174. budgetnum++
  175. if pd.(float64) > 0 {
  176. budgetNotNilNumP++
  177. }
  178. if nd.(float64) > 0 {
  179. budgetNotNilNumN++
  180. }
  181. case "bidamount":
  182. bidamount := new(Bidamount)
  183. bidamount.BidamountOld = fmt.Sprint(pd)
  184. bidamount.BidamountNew = fmt.Sprint(nd)
  185. bidamount.versionComparison = *versioncomparison
  186. bidamounts = append(bidamounts, bidamount)
  187. bidamountnum++
  188. if pd.(float64) > 0 {
  189. bidamountNotNilNumP++
  190. }
  191. if nd.(float64) > 0 {
  192. bidamountNotNilNumN++
  193. }
  194. }
  195. }
  196. }
  197. fmt.Println()
  198. }
  199. //log.Println(projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum)
  200. for ins, ivs := range xf.Sheets {
  201. for inr, ivr := range ivs.Rows {
  202. for _, ivc := range ivr.Cells {
  203. //抽取对比
  204. if ins == 0 {
  205. if inr < 3 {
  206. continue
  207. }
  208. switch strings.TrimSpace(ivc.String()) {
  209. case "projectname":
  210. style := ivr.Cells[1].GetStyle()
  211. style.Font.Color = "000000"
  212. ivr.Cells[1].SetValue(pnameNotNilNumP)
  213. ivr.Cells[2].SetValue(pnameNotNilNumN)
  214. //结果相同数量
  215. ivr.Cells[3].SetValue(sum - projectnamenum)
  216. ivr.Cells[3].SetStyle(style)
  217. //结果不同数量
  218. ivr.Cells[4].SetValue(projectnamenum)
  219. ivr.Cells[4].SetStyle(style)
  220. case "buyer":
  221. style := ivr.Cells[1].GetStyle()
  222. style.Font.Color = "000000"
  223. ivr.Cells[1].SetValue(buyerNotNilNumP)
  224. ivr.Cells[2].SetValue(buyerNotNilNumN)
  225. //结果相同数量
  226. ivr.Cells[3].SetValue(sum - buyernum)
  227. ivr.Cells[3].SetStyle(style)
  228. //结果不同数量
  229. ivr.Cells[4].SetValue(buyernum)
  230. ivr.Cells[4].SetStyle(style)
  231. case "projectcode":
  232. style := ivr.Cells[1].GetStyle()
  233. style.Font.Color = "000000"
  234. ivr.Cells[1].SetValue(pcodeNotNilNumP)
  235. ivr.Cells[2].SetValue(pcodeNotNilNumN)
  236. //结果相同数量
  237. ivr.Cells[3].SetValue(sum - projectcodenum)
  238. ivr.Cells[3].SetStyle(style)
  239. //结果不同数量
  240. ivr.Cells[4].SetValue(projectcodenum)
  241. ivr.Cells[4].SetStyle(style)
  242. case "winner":
  243. style := ivr.Cells[1].GetStyle()
  244. style.Font.Color = "000000"
  245. ivr.Cells[1].SetValue(winnerNotNilNumP)
  246. ivr.Cells[2].SetValue(winnerNotNilNumN)
  247. //结果相同数量
  248. ivr.Cells[3].SetValue(sum - winnernum)
  249. ivr.Cells[3].SetStyle(style)
  250. //结果不同数量
  251. ivr.Cells[4].SetValue(winnernum)
  252. ivr.Cells[4].SetStyle(style)
  253. case "budget":
  254. style := ivr.Cells[1].GetStyle()
  255. style.Font.Color = "000000"
  256. ivr.Cells[1].SetValue(budgetNotNilNumP)
  257. ivr.Cells[2].SetValue(budgetNotNilNumN)
  258. //结果相同数量
  259. ivr.Cells[3].SetValue(sum - budgetnum)
  260. ivr.Cells[3].SetStyle(style)
  261. //结果不同数量
  262. ivr.Cells[4].SetValue(budgetnum)
  263. ivr.Cells[4].SetStyle(style)
  264. case "bidamount":
  265. style := ivr.Cells[1].GetStyle()
  266. style.Font.Color = "000000"
  267. ivr.Cells[1].SetValue(bidamountNotNilNumP)
  268. ivr.Cells[2].SetValue(bidamountNotNilNumN)
  269. //结果相同数量
  270. ivr.Cells[3].SetValue(sum - bidamountnum)
  271. ivr.Cells[3].SetStyle(style)
  272. //结果不同数量
  273. ivr.Cells[4].SetValue(bidamountnum)
  274. ivr.Cells[4].SetStyle(style)
  275. }
  276. }
  277. }
  278. }
  279. if ins > 0 {
  280. if len(ivs.Rows) == 0 {
  281. row := ivs.AddRow()
  282. row.AddCell().SetValue("ObjectId")
  283. row.AddCell().SetValue("dev3.1.2")
  284. row.AddCell().SetValue("dev3.2")
  285. row.AddCell().SetValue("URL")
  286. }
  287. //log.Println(ivs.Name)
  288. switch strings.TrimSpace(ivs.Name) {
  289. case "projectname":
  290. for _, v := range projectnames {
  291. row := ivs.AddRow()
  292. row.AddCell().SetValue(v.Id)
  293. row.AddCell().SetValue(v.ProjectnameOld)
  294. row.AddCell().SetValue(v.ProjectnameNew)
  295. row.AddCell().SetValue(v.Url)
  296. }
  297. case "buyer":
  298. for _, v := range buyers {
  299. row := ivs.AddRow()
  300. row.AddCell().SetValue(v.Id)
  301. row.AddCell().SetValue(v.BuyerOld)
  302. row.AddCell().SetValue(v.BuyerNew)
  303. row.AddCell().SetValue(v.Url)
  304. }
  305. case "projectcode":
  306. for _, v := range projectcodes {
  307. row := ivs.AddRow()
  308. row.AddCell().SetValue(v.Id)
  309. row.AddCell().SetValue(v.ProjectcodeOld)
  310. row.AddCell().SetValue(v.ProjectcodeNew)
  311. row.AddCell().SetValue(v.Url)
  312. }
  313. case "winner":
  314. for _, v := range winners {
  315. row := ivs.AddRow()
  316. row.AddCell().SetValue(v.Id)
  317. row.AddCell().SetValue(v.WinnerOld)
  318. row.AddCell().SetValue(v.WinnerNew)
  319. row.AddCell().SetValue(v.Url)
  320. }
  321. case "budget":
  322. for _, v := range budgets {
  323. row := ivs.AddRow()
  324. row.AddCell().SetValue(v.Id)
  325. row.AddCell().SetValue(v.BudgetOld)
  326. row.AddCell().SetValue(v.BudgetNew)
  327. row.AddCell().SetValue(v.Url)
  328. }
  329. case "bidamount":
  330. for _, v := range bidamounts {
  331. row := ivs.AddRow()
  332. row.AddCell().SetValue(v.Id)
  333. row.AddCell().SetValue(v.BidamountOld)
  334. row.AddCell().SetValue(v.BidamountNew)
  335. row.AddCell().SetValue(v.Url)
  336. }
  337. }
  338. }
  339. }
  340. err = xf.Save("resultdata.xlsx")
  341. if err != nil {
  342. log.Println("保存xlsx失败:", err)
  343. return
  344. }
  345. log.Println("xlsx保存成功")
  346. }
  347. type Projectname struct {
  348. versionComparison
  349. ProjectnameOld string
  350. ProjectnameNew string
  351. }
  352. type Buyer struct {
  353. versionComparison
  354. BuyerOld string
  355. BuyerNew string
  356. }
  357. type Projectcode struct {
  358. versionComparison
  359. ProjectcodeOld string
  360. ProjectcodeNew string
  361. }
  362. type Winner struct {
  363. versionComparison
  364. WinnerOld string
  365. WinnerNew string
  366. }
  367. type Budget struct {
  368. versionComparison
  369. BudgetOld string
  370. BudgetNew string
  371. }
  372. type Bidamount struct {
  373. versionComparison
  374. BidamountOld string
  375. BidamountNew string
  376. }