main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if pd.(float64) > 0 {
  110. switch k {
  111. case "budget":
  112. budgetNotNilNumP++
  113. case "bidamount":
  114. bidamountNotNilNumP++
  115. }
  116. }
  117. if nd.(float64) > 0 {
  118. switch k {
  119. case "budget":
  120. budgetNotNilNumN++
  121. case "bidamount":
  122. bidamountNotNilNumN++
  123. }
  124. }
  125. } else {
  126. pd = strings.TrimSpace(util.ObjToString((*pdata)[k]))
  127. nd = strings.TrimSpace(util.ObjToString((*ndata)[k]))
  128. if strings.TrimSpace(pd.(string)) != "" {
  129. switch k {
  130. case "projectname":
  131. pnameNotNilNumP++
  132. case "buyer":
  133. buyerNotNilNumP++
  134. case "projectcode":
  135. pcodeNotNilNumP++
  136. case "winner":
  137. winnerNotNilNumP++
  138. }
  139. }
  140. if strings.TrimSpace(nd.(string)) != "" {
  141. switch k {
  142. case "projectname":
  143. pnameNotNilNumN++
  144. case "buyer":
  145. buyerNotNilNumN++
  146. case "projectcode":
  147. pcodeNotNilNumN++
  148. case "winner":
  149. winnerNotNilNumN++
  150. }
  151. }
  152. }
  153. if pd != nd {
  154. //log.Println(k)
  155. switch k {
  156. case "projectname":
  157. projectname := new(Projectname)
  158. projectname.versionComparison = *versioncomparison
  159. projectname.ProjectnameOld = fmt.Sprint(pd)
  160. projectname.ProjectnameNew = fmt.Sprint(nd)
  161. projectnames = append(projectnames, projectname)
  162. projectnamenum++
  163. case "buyer":
  164. buyer := new(Buyer)
  165. buyer.versionComparison = *versioncomparison
  166. buyer.BuyerOld = fmt.Sprint(pd)
  167. buyer.BuyerNew = fmt.Sprint(nd)
  168. buyers = append(buyers, buyer)
  169. buyernum++
  170. case "projectcode":
  171. projectcode := new(Projectcode)
  172. projectcode.ProjectcodeOld = fmt.Sprint(pd)
  173. projectcode.ProjectcodeNew = fmt.Sprint(nd)
  174. projectcode.versionComparison = *versioncomparison
  175. projectcodes = append(projectcodes, projectcode)
  176. projectcodenum++
  177. case "winner":
  178. winner := new(Winner)
  179. winner.WinnerOld = fmt.Sprint(pd)
  180. winner.WinnerNew = fmt.Sprint(nd)
  181. winner.versionComparison = *versioncomparison
  182. winners = append(winners, winner)
  183. winnernum++
  184. case "budget":
  185. budget := new(Budget)
  186. budget.BudgetOld = fmt.Sprint(pd)
  187. budget.BudgetNew = fmt.Sprint(nd)
  188. budget.versionComparison = *versioncomparison
  189. budgets = append(budgets, budget)
  190. budgetnum++
  191. case "bidamount":
  192. bidamount := new(Bidamount)
  193. bidamount.BidamountOld = fmt.Sprint(pd)
  194. bidamount.BidamountNew = fmt.Sprint(nd)
  195. bidamount.versionComparison = *versioncomparison
  196. bidamounts = append(bidamounts, bidamount)
  197. bidamountnum++
  198. }
  199. }
  200. }
  201. fmt.Println()
  202. }
  203. //log.Println(projectcodenum, bidamountnum, winnernum, buyernum, budgetnum, projectnamenum)
  204. for ins, ivs := range xf.Sheets {
  205. for inr, ivr := range ivs.Rows {
  206. for _, ivc := range ivr.Cells {
  207. //抽取对比
  208. if ins == 0 {
  209. if inr < 3 {
  210. continue
  211. }
  212. switch strings.TrimSpace(ivc.String()) {
  213. case "projectname":
  214. style := ivr.Cells[1].GetStyle()
  215. style.Font.Color = "000000"
  216. ivr.Cells[1].SetValue(pnameNotNilNumP)
  217. ivr.Cells[2].SetValue(pnameNotNilNumN)
  218. //结果相同数量
  219. ivr.Cells[3].SetValue(sum - projectnamenum)
  220. ivr.Cells[3].SetStyle(style)
  221. //结果不同数量
  222. ivr.Cells[4].SetValue(projectnamenum)
  223. ivr.Cells[4].SetStyle(style)
  224. case "buyer":
  225. style := ivr.Cells[1].GetStyle()
  226. style.Font.Color = "000000"
  227. ivr.Cells[1].SetValue(buyerNotNilNumP)
  228. ivr.Cells[2].SetValue(buyerNotNilNumN)
  229. //结果相同数量
  230. ivr.Cells[3].SetValue(sum - buyernum)
  231. ivr.Cells[3].SetStyle(style)
  232. //结果不同数量
  233. ivr.Cells[4].SetValue(buyernum)
  234. ivr.Cells[4].SetStyle(style)
  235. case "projectcode":
  236. style := ivr.Cells[1].GetStyle()
  237. style.Font.Color = "000000"
  238. ivr.Cells[1].SetValue(pcodeNotNilNumP)
  239. ivr.Cells[2].SetValue(pcodeNotNilNumN)
  240. //结果相同数量
  241. ivr.Cells[3].SetValue(sum - projectcodenum)
  242. ivr.Cells[3].SetStyle(style)
  243. //结果不同数量
  244. ivr.Cells[4].SetValue(projectcodenum)
  245. ivr.Cells[4].SetStyle(style)
  246. case "winner":
  247. style := ivr.Cells[1].GetStyle()
  248. style.Font.Color = "000000"
  249. ivr.Cells[1].SetValue(winnerNotNilNumP)
  250. ivr.Cells[2].SetValue(winnerNotNilNumN)
  251. //结果相同数量
  252. ivr.Cells[3].SetValue(sum - winnernum)
  253. ivr.Cells[3].SetStyle(style)
  254. //结果不同数量
  255. ivr.Cells[4].SetValue(winnernum)
  256. ivr.Cells[4].SetStyle(style)
  257. case "budget":
  258. style := ivr.Cells[1].GetStyle()
  259. style.Font.Color = "000000"
  260. ivr.Cells[1].SetValue(budgetNotNilNumP)
  261. ivr.Cells[2].SetValue(budgetNotNilNumN)
  262. //结果相同数量
  263. ivr.Cells[3].SetValue(sum - budgetnum)
  264. ivr.Cells[3].SetStyle(style)
  265. //结果不同数量
  266. ivr.Cells[4].SetValue(budgetnum)
  267. ivr.Cells[4].SetStyle(style)
  268. case "bidamount":
  269. style := ivr.Cells[1].GetStyle()
  270. style.Font.Color = "000000"
  271. ivr.Cells[1].SetValue(bidamountNotNilNumP)
  272. ivr.Cells[2].SetValue(bidamountNotNilNumN)
  273. //结果相同数量
  274. ivr.Cells[3].SetValue(sum - bidamountnum)
  275. ivr.Cells[3].SetStyle(style)
  276. //结果不同数量
  277. ivr.Cells[4].SetValue(bidamountnum)
  278. ivr.Cells[4].SetStyle(style)
  279. }
  280. }
  281. }
  282. }
  283. if ins > 0 {
  284. if len(ivs.Rows) == 0 {
  285. row := ivs.AddRow()
  286. row.AddCell().SetValue("ObjectId")
  287. row.AddCell().SetValue("dev3.1.2")
  288. row.AddCell().SetValue("dev3.2")
  289. row.AddCell().SetValue("URL")
  290. }
  291. //log.Println(ivs.Name)
  292. switch strings.TrimSpace(ivs.Name) {
  293. case "projectname":
  294. for _, v := range projectnames {
  295. row := ivs.AddRow()
  296. row.AddCell().SetValue(v.Id)
  297. row.AddCell().SetValue(v.ProjectnameOld)
  298. row.AddCell().SetValue(v.ProjectnameNew)
  299. row.AddCell().SetValue(v.Url)
  300. }
  301. case "buyer":
  302. for _, v := range buyers {
  303. row := ivs.AddRow()
  304. row.AddCell().SetValue(v.Id)
  305. row.AddCell().SetValue(v.BuyerOld)
  306. row.AddCell().SetValue(v.BuyerNew)
  307. row.AddCell().SetValue(v.Url)
  308. }
  309. case "projectcode":
  310. for _, v := range projectcodes {
  311. row := ivs.AddRow()
  312. row.AddCell().SetValue(v.Id)
  313. row.AddCell().SetValue(v.ProjectcodeOld)
  314. row.AddCell().SetValue(v.ProjectcodeNew)
  315. row.AddCell().SetValue(v.Url)
  316. }
  317. case "winner":
  318. for _, v := range winners {
  319. row := ivs.AddRow()
  320. row.AddCell().SetValue(v.Id)
  321. row.AddCell().SetValue(v.WinnerOld)
  322. row.AddCell().SetValue(v.WinnerNew)
  323. row.AddCell().SetValue(v.Url)
  324. }
  325. case "budget":
  326. for _, v := range budgets {
  327. row := ivs.AddRow()
  328. row.AddCell().SetValue(v.Id)
  329. row.AddCell().SetValue(v.BudgetOld)
  330. row.AddCell().SetValue(v.BudgetNew)
  331. row.AddCell().SetValue(v.Url)
  332. }
  333. case "bidamount":
  334. for _, v := range bidamounts {
  335. row := ivs.AddRow()
  336. row.AddCell().SetValue(v.Id)
  337. row.AddCell().SetValue(v.BidamountOld)
  338. row.AddCell().SetValue(v.BidamountNew)
  339. row.AddCell().SetValue(v.Url)
  340. }
  341. }
  342. }
  343. }
  344. err = xf.Save("resultdata.xlsx")
  345. if err != nil {
  346. log.Println("保存xlsx失败:", err)
  347. return
  348. }
  349. log.Println("xlsx保存成功")
  350. }
  351. type Projectname struct {
  352. versionComparison
  353. ProjectnameOld string
  354. ProjectnameNew string
  355. }
  356. type Buyer struct {
  357. versionComparison
  358. BuyerOld string
  359. BuyerNew string
  360. }
  361. type Projectcode struct {
  362. versionComparison
  363. ProjectcodeOld string
  364. ProjectcodeNew string
  365. }
  366. type Winner struct {
  367. versionComparison
  368. WinnerOld string
  369. WinnerNew string
  370. }
  371. type Budget struct {
  372. versionComparison
  373. BudgetOld string
  374. BudgetNew string
  375. }
  376. type Bidamount struct {
  377. versionComparison
  378. BidamountOld string
  379. BidamountNew string
  380. }