main.bak 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 projectcodenumXT, bidamountnumXT, winnernumXT, buyernumXT, budgetnumXT, projectnamenumXT int //相等计数器
  50. var pcodeNotNilNumP, bidamountNotNilNumP, winnerNotNilNumP, buyerNotNilNumP, budgetNotNilNumP, pnameNotNilNumP int //不相等计数器
  51. var pcodeNotNilNumN, bidamountNotNilNumN, winnerNotNilNumN, buyerNotNilNumN, budgetNotNilNumN, pnameNotNilNumN int //不相等计数器
  52. log.Println(num, sid)
  53. if num < 1 {
  54. log.Println("查询数量应该大于0")
  55. return
  56. }
  57. sum := num //总量
  58. //if strings.TrimSpace(gteid) == "" {
  59. // gteid = "386cd3000000000000000000"
  60. //}
  61. var iter *mgo.Iter
  62. if sid == "" {
  63. iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(nil).Select(bson.M{"_id": 1}).Iter()
  64. } else {
  65. iter = Extractmgo.DB(util.ObjToString(SysConfig["extractdb"])).C(util.ObjToString(SysConfig["extractc"])).Find(bson.M{"_id": bson.M{
  66. "$gte": bson.ObjectIdHex(sid)},
  67. }).Select(bson.M{"_id": 1}).Iter()
  68. }
  69. defer log.Println("关闭 iter:", iter.Close())
  70. var data map[string]bson.ObjectId
  71. getdata := make([]bson.ObjectId, 0)
  72. for iter.Next(&data) {
  73. if num == 0 {
  74. break
  75. }
  76. getdata = append(getdata, data["_id"])
  77. num--
  78. }
  79. log.Println(sum, "条数据加载完成")
  80. projectnames := make([]*Projectname, 0)
  81. buyers := make([]*Buyer, 0)
  82. projectcodes := make([]*Projectcode, 0)
  83. winners := make([]*Winner, 0)
  84. budgets := make([]*Budget, 0)
  85. bidamounts := make([]*Bidamount, 0)
  86. for _, gv := range getdata {
  87. log.Println(gv)
  88. gvid := gv.Hex()
  89. pdata, b := Previousmgo.FindById(util.ObjToString(SysConfig["previousc"]), gvid, SysConfig["keyfield"])
  90. if !b || len(*pdata) == 0 {
  91. log.Println("oldId不存在")
  92. continue
  93. }
  94. log.Println("pdata:", pdata)
  95. ndata, b := Newmgo.FindById(util.ObjToString(SysConfig["newc"]), gvid, SysConfig["keyfield"])
  96. if !b || len(*ndata) == 0 {
  97. log.Println("nweId不存在")
  98. continue
  99. }
  100. log.Println("ndata:", ndata)
  101. versioncomparison := new(versionComparison)
  102. versioncomparison.Id = gvid
  103. versioncomparison.Url = "https://www.jianyu360.com/article/content/" + util.CommonEncodeArticle("content", gvid) + ".html"
  104. for k := range SysConfig["keyfield"].(map[string]interface{}) {
  105. var pd interface{}
  106. var nd interface{}
  107. if k == "budget" || k == "bidamount" {
  108. pd = util.Float64All((*pdata)[k])
  109. nd = util.Float64All((*ndata)[k])
  110. if pd.(float64) > 0 {
  111. switch k {
  112. case "budget":
  113. budgetNotNilNumP++
  114. case "bidamount":
  115. bidamountNotNilNumP++
  116. }
  117. }
  118. if nd.(float64) > 0 {
  119. switch k {
  120. case "budget":
  121. budgetNotNilNumN++
  122. case "bidamount":
  123. bidamountNotNilNumN++
  124. }
  125. }
  126. } else {
  127. pd = strings.TrimSpace(util.ObjToString((*pdata)[k]))
  128. nd = strings.TrimSpace(util.ObjToString((*ndata)[k]))
  129. if strings.TrimSpace(pd.(string)) != "" {
  130. switch k {
  131. case "projectname":
  132. pnameNotNilNumP++
  133. case "buyer":
  134. buyerNotNilNumP++
  135. case "projectcode":
  136. pcodeNotNilNumP++
  137. case "winner":
  138. winnerNotNilNumP++
  139. }
  140. }
  141. if strings.TrimSpace(nd.(string)) != "" {
  142. switch k {
  143. case "projectname":
  144. pnameNotNilNumN++
  145. case "buyer":
  146. buyerNotNilNumN++
  147. case "projectcode":
  148. pcodeNotNilNumN++
  149. case "winner":
  150. winnerNotNilNumN++
  151. }
  152. }
  153. }
  154. if pd != nd {
  155. //log.Println(k)
  156. switch k {
  157. case "projectname":
  158. projectname := new(Projectname)
  159. projectname.versionComparison = *versioncomparison
  160. pd = strings.Trim(fmt.Sprint(pd), "项目")
  161. pd = strings.Trim(strings.TrimSpace(fmt.Sprint(pd)), "采购")
  162. nd = strings.Trim(fmt.Sprint(nd), "项目")
  163. nd = strings.Trim(strings.TrimSpace(fmt.Sprint(nd)), "采购")
  164. if pd != nd{
  165. projectname.ProjectnameOld = fmt.Sprint(pd)
  166. projectname.ProjectnameNew = fmt.Sprint(nd)
  167. projectnames = append(projectnames, projectname)
  168. projectnamenum++
  169. }else {
  170. projectnamenumXT++
  171. }
  172. case "buyer":
  173. buyer := new(Buyer)
  174. buyer.versionComparison = *versioncomparison
  175. buyer.BuyerOld = fmt.Sprint(pd)
  176. buyer.BuyerNew = fmt.Sprint(nd)
  177. buyers = append(buyers, buyer)
  178. buyernum++
  179. case "projectcode":
  180. projectcode := new(Projectcode)
  181. projectcode.ProjectcodeOld = fmt.Sprint(pd)
  182. projectcode.ProjectcodeNew = fmt.Sprint(nd)
  183. projectcode.versionComparison = *versioncomparison
  184. projectcodes = append(projectcodes, projectcode)
  185. projectcodenum++
  186. case "winner":
  187. winner := new(Winner)
  188. winner.WinnerOld = fmt.Sprint(pd)
  189. winner.WinnerNew = fmt.Sprint(nd)
  190. winner.versionComparison = *versioncomparison
  191. winners = append(winners, winner)
  192. winnernum++
  193. case "budget":
  194. budget := new(Budget)
  195. budget.BudgetOld = fmt.Sprint(pd)
  196. budget.BudgetNew = fmt.Sprint(nd)
  197. budget.versionComparison = *versioncomparison
  198. budgets = append(budgets, budget)
  199. budgetnum++
  200. case "bidamount":
  201. bidamount := new(Bidamount)
  202. bidamount.BidamountOld = fmt.Sprint(pd)
  203. bidamount.BidamountNew = fmt.Sprint(nd)
  204. bidamount.versionComparison = *versioncomparison
  205. bidamounts = append(bidamounts, bidamount)
  206. bidamountnum++
  207. }
  208. }else {
  209. //相同统计
  210. pd = strings.TrimSpace(fmt.Sprint(pd))
  211. nd = strings.TrimSpace(fmt.Sprint(nd))
  212. if pd == ""||pd == "0" || nd == ""|| nd == "0"{
  213. continue
  214. }
  215. if pd == nd {
  216. switch k {
  217. case "projectname":
  218. projectnamenumXT++
  219. case "buyer":
  220. buyernumXT++
  221. case "projectcode":
  222. projectcodenumXT++
  223. case "winner":
  224. winnernumXT++
  225. case "budget":
  226. budgetnumXT++
  227. case "bidamount":
  228. bidamountnumXT++
  229. }
  230. }
  231. }
  232. }
  233. fmt.Println()
  234. }
  235. //log.Println(projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum)
  236. for ins, ivs := range xf.Sheets {
  237. for inr, ivr := range ivs.Rows {
  238. for _, ivc := range ivr.Cells {
  239. //抽取对比
  240. if ins == 0 {
  241. if inr < 3 {
  242. continue
  243. }
  244. switch strings.TrimSpace(ivc.String()) {
  245. case "projectname":
  246. style := ivr.Cells[1].GetStyle()
  247. style.Font.Color = "000000"
  248. ivr.Cells[1].SetValue(pnameNotNilNumP)
  249. ivr.Cells[2].SetValue(pnameNotNilNumN)
  250. //结果相同数量
  251. ivr.Cells[3].SetValue(projectnamenumXT)
  252. ivr.Cells[3].SetStyle(style)
  253. //结果不同数量
  254. ivr.Cells[4].SetValue(projectnamenum)
  255. ivr.Cells[4].SetStyle(style)
  256. case "buyer":
  257. style := ivr.Cells[1].GetStyle()
  258. style.Font.Color = "000000"
  259. ivr.Cells[1].SetValue(buyerNotNilNumP)
  260. ivr.Cells[2].SetValue(buyerNotNilNumN)
  261. //结果相同数量
  262. ivr.Cells[3].SetValue(buyernumXT)
  263. ivr.Cells[3].SetStyle(style)
  264. //结果不同数量
  265. ivr.Cells[4].SetValue(buyernum)
  266. ivr.Cells[4].SetStyle(style)
  267. case "projectcode":
  268. style := ivr.Cells[1].GetStyle()
  269. style.Font.Color = "000000"
  270. ivr.Cells[1].SetValue(pcodeNotNilNumP)
  271. ivr.Cells[2].SetValue(pcodeNotNilNumN)
  272. //结果相同数量
  273. ivr.Cells[3].SetValue(projectcodenumXT)
  274. ivr.Cells[3].SetStyle(style)
  275. //结果不同数量
  276. ivr.Cells[4].SetValue(projectcodenum)
  277. ivr.Cells[4].SetStyle(style)
  278. case "winner":
  279. style := ivr.Cells[1].GetStyle()
  280. style.Font.Color = "000000"
  281. ivr.Cells[1].SetValue(winnerNotNilNumP)
  282. ivr.Cells[2].SetValue(winnerNotNilNumN)
  283. //结果相同数量
  284. ivr.Cells[3].SetValue(winnernumXT)
  285. ivr.Cells[3].SetStyle(style)
  286. //结果不同数量
  287. ivr.Cells[4].SetValue(winnernum)
  288. ivr.Cells[4].SetStyle(style)
  289. case "budget":
  290. style := ivr.Cells[1].GetStyle()
  291. style.Font.Color = "000000"
  292. ivr.Cells[1].SetValue(budgetNotNilNumP)
  293. ivr.Cells[2].SetValue(budgetNotNilNumN)
  294. //结果相同数量
  295. ivr.Cells[3].SetValue(budgetnumXT)
  296. ivr.Cells[3].SetStyle(style)
  297. //结果不同数量
  298. ivr.Cells[4].SetValue(budgetnum)
  299. ivr.Cells[4].SetStyle(style)
  300. case "bidamount":
  301. style := ivr.Cells[1].GetStyle()
  302. style.Font.Color = "000000"
  303. ivr.Cells[1].SetValue(bidamountNotNilNumP)
  304. ivr.Cells[2].SetValue(bidamountNotNilNumN)
  305. //结果相同数量
  306. ivr.Cells[3].SetValue(bidamountnumXT)
  307. ivr.Cells[3].SetStyle(style)
  308. //结果不同数量
  309. ivr.Cells[4].SetValue(bidamountnum)
  310. ivr.Cells[4].SetStyle(style)
  311. }
  312. }
  313. }
  314. }
  315. if ins > 0 {
  316. if len(ivs.Rows) == 0 {
  317. row := ivs.AddRow()
  318. row.AddCell().SetValue("ObjectId")
  319. row.AddCell().SetValue("dev3.1.2")
  320. row.AddCell().SetValue("dev3.2")
  321. row.AddCell().SetValue("URL")
  322. }
  323. //log.Println(ivs.Name)
  324. switch strings.TrimSpace(ivs.Name) {
  325. case "projectname":
  326. for _, v := range projectnames {
  327. row := ivs.AddRow()
  328. row.AddCell().SetValue(v.Id)
  329. row.AddCell().SetValue(v.ProjectnameOld)
  330. row.AddCell().SetValue(v.ProjectnameNew)
  331. row.AddCell().SetValue(v.Url)
  332. }
  333. case "buyer":
  334. for _, v := range buyers {
  335. row := ivs.AddRow()
  336. row.AddCell().SetValue(v.Id)
  337. row.AddCell().SetValue(v.BuyerOld)
  338. row.AddCell().SetValue(v.BuyerNew)
  339. row.AddCell().SetValue(v.Url)
  340. }
  341. case "projectcode":
  342. for _, v := range projectcodes {
  343. row := ivs.AddRow()
  344. row.AddCell().SetValue(v.Id)
  345. row.AddCell().SetValue(v.ProjectcodeOld)
  346. row.AddCell().SetValue(v.ProjectcodeNew)
  347. row.AddCell().SetValue(v.Url)
  348. }
  349. case "winner":
  350. for _, v := range winners {
  351. row := ivs.AddRow()
  352. row.AddCell().SetValue(v.Id)
  353. row.AddCell().SetValue(v.WinnerOld)
  354. row.AddCell().SetValue(v.WinnerNew)
  355. row.AddCell().SetValue(v.Url)
  356. }
  357. case "budget":
  358. for _, v := range budgets {
  359. row := ivs.AddRow()
  360. row.AddCell().SetValue(v.Id)
  361. row.AddCell().SetValue(v.BudgetOld)
  362. row.AddCell().SetValue(v.BudgetNew)
  363. row.AddCell().SetValue(v.Url)
  364. }
  365. case "bidamount":
  366. for _, v := range bidamounts {
  367. row := ivs.AddRow()
  368. row.AddCell().SetValue(v.Id)
  369. row.AddCell().SetValue(v.BidamountOld)
  370. row.AddCell().SetValue(v.BidamountNew)
  371. row.AddCell().SetValue(v.Url)
  372. }
  373. }
  374. }
  375. }
  376. err = xf.Save("resultdata.xlsx")
  377. if err != nil {
  378. log.Println("保存xlsx失败:", err)
  379. return
  380. }
  381. log.Println("xlsx保存成功")
  382. }
  383. type Projectname struct {
  384. versionComparison
  385. ProjectnameOld string
  386. ProjectnameNew string
  387. }
  388. type Buyer struct {
  389. versionComparison
  390. BuyerOld string
  391. BuyerNew string
  392. }
  393. type Projectcode struct {
  394. versionComparison
  395. ProjectcodeOld string
  396. ProjectcodeNew string
  397. }
  398. type Winner struct {
  399. versionComparison
  400. WinnerOld string
  401. WinnerNew string
  402. }
  403. type Budget struct {
  404. versionComparison
  405. BudgetOld string
  406. BudgetNew string
  407. }
  408. type Bidamount struct {
  409. versionComparison
  410. BidamountOld string
  411. BidamountNew string
  412. }