main.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "github.com/tealeg/xlsx"
  6. "mongodb"
  7. "qfw/util"
  8. "strings"
  9. )
  10. var (
  11. Sysconfig map[string]interface{}
  12. Mgo *mongodb.MongodbSim
  13. Dbname, DbColl string
  14. ExportType, IsMark int
  15. Search map[string]interface{}
  16. Fields map[string]interface{}
  17. FieldsArr []string
  18. FiedlsPurchase []string
  19. SE = util.SimpleEncrypt{Key: "topJYBX2019"}
  20. )
  21. func init() {
  22. util.ReadConfig(&Sysconfig)
  23. Mgo = &mongodb.MongodbSim{
  24. MongodbAddr: Sysconfig["mgoAddr"].(string),
  25. Size: util.IntAllDef(Sysconfig["mgoSize"], 5),
  26. DbName: Sysconfig["mgoDbName"].(string),
  27. }
  28. Mgo.InitPool()
  29. Dbname = Sysconfig["mgoDbName"].(string)
  30. DbColl = Sysconfig["mgoColl"].(string)
  31. ExportType = util.IntAll(Sysconfig["exportType"])
  32. IsMark = util.IntAll(Sysconfig["isMark"])
  33. Search = Sysconfig["search"].(map[string]interface{})
  34. FieldsArr = util.ObjArrToStringArr(Sysconfig["fieldsSort"].([]interface{}))
  35. Fields = Sysconfig["fields"].(map[string]interface{})
  36. FiedlsPurchase = util.ObjArrToStringArr(Sysconfig["fields_purchase"].([]interface{}))
  37. if ExportType == 1 {
  38. FieldsArr[len(FieldsArr)-1] = "itemname"
  39. FieldsArr = append(FieldsArr, "brandname", "model", "unitname", "unitprice", "number", "totalprice", "id")
  40. Fields["itemname"] = "产品名称"
  41. Fields["brandname"] = "品牌"
  42. Fields["model"] = "规格型号"
  43. Fields["unitname"] = "单位"
  44. Fields["unitprice"] = "单价"
  45. Fields["number"] = "数量"
  46. Fields["totalprice"] = "小计"
  47. } else if ExportType == 3 {
  48. FiedlsPurchase[len(FiedlsPurchase)-1] = "projectname_purchase"
  49. FiedlsPurchase = append(FiedlsPurchase, "projectscope_purchase", "item", "buyer_purchase", "remark", "totalprice", "expurasingtime", "cgyxxqhref", "id")
  50. Fields["projectname_purchase"] = "采购意向名称"
  51. Fields["projectscope_purchase"] = "采购内容"
  52. Fields["item"] = "采购品目"
  53. Fields["remark"] = "备注"
  54. Fields["buyer_purchase"] = "采购单位"
  55. Fields["totalprice"] = "预计采购金额(元)"
  56. Fields["expurasingtime"] = "预计采购时间"
  57. Fields["cgyxxqhref"] = "采购意向详情链接"
  58. } else if ExportType == 4 {
  59. FieldsArr[len(FieldsArr)-1] = "itemname"
  60. FieldsArr = append(FieldsArr, "brandname", "madel", "unitname", "unitprice", "number", "totalprice", "id")
  61. Fields["itemname"] = "产品名称"
  62. Fields["brandname"] = "品牌"
  63. Fields["madel"] = "规格型号"
  64. Fields["unitname"] = "单位"
  65. Fields["unitprice"] = "单价"
  66. Fields["number"] = "数量"
  67. Fields["totalprice"] = "小计"
  68. }
  69. util.Debug(FieldsArr)
  70. }
  71. func main() {
  72. sess := Mgo.GetMgoConn()
  73. defer Mgo.DestoryMongoConn(sess)
  74. file := xlsx.NewFile()
  75. sheet, err := file.AddSheet("sheet1")
  76. if err != nil {
  77. panic(err)
  78. }
  79. row := sheet.AddRow()
  80. if ExportType == 3 {
  81. // 标的物 采购意向
  82. for _, v := range FiedlsPurchase {
  83. row.AddCell().SetValue(Fields[v])
  84. }
  85. } else {
  86. for _, v := range FieldsArr {
  87. row.AddCell().SetValue(Fields[v])
  88. }
  89. }
  90. q := map[string]interface{}{}
  91. if len(Search) > 0 {
  92. q = Search
  93. }
  94. c := Mgo.Count(DbColl, q)
  95. util.Debug("search size result ---", DbColl, q, c)
  96. if c == 0 {
  97. return
  98. }
  99. query := sess.DB(Dbname).C(DbColl).Find(&q).Select(nil).Iter()
  100. count := 0
  101. for tmp := make(map[string]interface{}); query.Next(&tmp); count++ {
  102. if count%500 == 0 {
  103. util.Debug("current ---", count)
  104. }
  105. var baseInfo map[string]interface{}
  106. if tmp["v_baseinfo"] != nil {
  107. baseInfo = tmp["v_baseinfo"].(map[string]interface{})
  108. } else {
  109. baseInfo = tmp
  110. }
  111. tagInfo, _ := tmp["v_taginfo"].(map[string]interface{})
  112. switch ExportType {
  113. case 0:
  114. // 不拆分
  115. row := sheet.AddRow()
  116. for _, v := range FieldsArr {
  117. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  118. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  119. str := ""
  120. if baseInfo[v] != nil {
  121. date := util.Int64All(baseInfo[v])
  122. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  123. }
  124. row.AddCell().SetValue(str)
  125. } else if v == "id" {
  126. if tmp["id"] != nil {
  127. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  128. row.AddCell().SetValue(id)
  129. } else {
  130. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  131. row.AddCell().SetValue(id)
  132. }
  133. } else {
  134. row.AddCell().SetValue(baseInfo[v])
  135. }
  136. }
  137. case 1:
  138. // 拆分标的物
  139. if IsMark == 1 {
  140. if tagInfo["purchasinglist"] != nil {
  141. if baseInfo["purchasinglist"] != nil {
  142. plist := baseInfo["purchasinglist"].([]interface{})
  143. for _, p := range plist {
  144. row := sheet.AddRow()
  145. p1 := p.(map[string]interface{})
  146. for _, v := range FieldsArr {
  147. if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" {
  148. row.AddCell().SetValue(p1[v])
  149. } else if v == "totalprice" {
  150. if p1["totalprice"] != nil {
  151. row.AddCell().SetValue(p1[v])
  152. } else {
  153. if p1["unitprice"] != nil && p1["number"] != nil {
  154. d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"]))))
  155. row.AddCell().SetValue(d1)
  156. } else {
  157. row.AddCell().SetValue("")
  158. }
  159. }
  160. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  161. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  162. str := ""
  163. if baseInfo[v] != nil {
  164. date := util.Int64All(baseInfo[v])
  165. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  166. }
  167. row.AddCell().SetValue(str)
  168. } else if v == "id" {
  169. if tmp["id"] != nil {
  170. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  171. row.AddCell().SetValue(id)
  172. } else {
  173. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  174. row.AddCell().SetValue(id)
  175. }
  176. } else {
  177. row.AddCell().SetValue(baseInfo[v])
  178. }
  179. }
  180. }
  181. } else {
  182. row := sheet.AddRow()
  183. for _, v := range FieldsArr {
  184. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  185. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  186. str := ""
  187. if baseInfo[v] != nil {
  188. date := util.Int64All(baseInfo[v])
  189. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  190. }
  191. row.AddCell().SetValue(str)
  192. } else if v == "id" {
  193. if tmp["id"] != nil {
  194. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  195. row.AddCell().SetValue(id)
  196. } else {
  197. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  198. row.AddCell().SetValue(id)
  199. }
  200. } else {
  201. row.AddCell().SetValue(baseInfo[v])
  202. }
  203. }
  204. }
  205. } else {
  206. row := sheet.AddRow()
  207. for _, v := range FieldsArr {
  208. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  209. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  210. str := ""
  211. if baseInfo[v] != nil {
  212. date := util.Int64All(baseInfo[v])
  213. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  214. }
  215. row.AddCell().SetValue(str)
  216. } else if v == "id" {
  217. if tmp["id"] != nil {
  218. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  219. row.AddCell().SetValue(id)
  220. } else {
  221. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  222. row.AddCell().SetValue(id)
  223. }
  224. } else {
  225. row.AddCell().SetValue(baseInfo[v])
  226. }
  227. }
  228. }
  229. } else {
  230. if baseInfo["purchasinglist"] != nil {
  231. plist := baseInfo["purchasinglist"].([]interface{})
  232. for _, p := range plist {
  233. row := sheet.AddRow()
  234. p1 := p.(map[string]interface{})
  235. for _, v := range FieldsArr {
  236. if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" {
  237. row.AddCell().SetValue(p1[v])
  238. } else if v == "totalprice" {
  239. if p1["totalprice"] != nil {
  240. row.AddCell().SetValue(p1[v])
  241. } else {
  242. if p1["unitprice"] != nil && p1["number"] != nil {
  243. d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"]))))
  244. row.AddCell().SetValue(d1)
  245. } else {
  246. row.AddCell().SetValue("")
  247. }
  248. }
  249. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  250. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  251. str := ""
  252. if baseInfo[v] != nil {
  253. date := util.Int64All(baseInfo[v])
  254. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  255. }
  256. row.AddCell().SetValue(str)
  257. } else if v == "id" {
  258. if tmp["id"] != nil {
  259. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  260. row.AddCell().SetValue(id)
  261. } else {
  262. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  263. row.AddCell().SetValue(id)
  264. }
  265. } else {
  266. row.AddCell().SetValue(baseInfo[v])
  267. }
  268. }
  269. }
  270. } else {
  271. row := sheet.AddRow()
  272. for _, v := range FieldsArr {
  273. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  274. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  275. str := ""
  276. if baseInfo[v] != nil {
  277. date := util.Int64All(baseInfo[v])
  278. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  279. }
  280. row.AddCell().SetValue(str)
  281. } else if v == "id" {
  282. if tmp["id"] != nil {
  283. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  284. row.AddCell().SetValue(id)
  285. } else {
  286. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  287. row.AddCell().SetValue(id)
  288. }
  289. } else {
  290. row.AddCell().SetValue(baseInfo[v])
  291. }
  292. }
  293. }
  294. break
  295. }
  296. case 2:
  297. // 多包拆分
  298. if baseInfo["package"] != nil {
  299. pkg := baseInfo["package"].(map[string]interface{})
  300. for _, p := range pkg {
  301. row := sheet.AddRow()
  302. p1 := p.(map[string]interface{})
  303. winner := []string{}
  304. bidamount := float64(0)
  305. if p1["winner_all"] != nil {
  306. if all := p1["winner_all"].([]interface{}); all != nil {
  307. if len(all) > 0 {
  308. for _, a := range all {
  309. a1 := a.(map[string]interface{})
  310. if util.ObjToString(a1["winner"]) != "" {
  311. winner = append(winner, util.ObjToString(a1["winner"]))
  312. }
  313. bidamount = util.Float64All(a1["bidamount"])
  314. }
  315. }
  316. }
  317. }
  318. for _, v := range FieldsArr {
  319. if v == "s_winner" && len(winner) > 0 {
  320. row.AddCell().SetValue(strings.Join(winner, ","))
  321. } else if v == "bidamount" {
  322. row.AddCell().SetValue(bidamount)
  323. } else if v == "winnerperson" || v == "winnertel" {
  324. row.AddCell().SetValue("")
  325. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  326. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  327. str := ""
  328. if baseInfo[v] != nil {
  329. date := util.Int64All(baseInfo[v])
  330. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  331. }
  332. row.AddCell().SetValue(str)
  333. } else if v == "id" {
  334. if tmp["id"] != nil {
  335. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  336. row.AddCell().SetValue(id)
  337. } else {
  338. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  339. row.AddCell().SetValue(id)
  340. }
  341. } else {
  342. row.AddCell().SetValue(baseInfo[v])
  343. }
  344. }
  345. }
  346. } else {
  347. row := sheet.AddRow()
  348. for _, v := range FieldsArr {
  349. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  350. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  351. str := ""
  352. if baseInfo[v] != nil {
  353. date := util.Int64All(baseInfo[v])
  354. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  355. }
  356. row.AddCell().SetValue(str)
  357. } else if v == "id" {
  358. if tmp["id"] != nil {
  359. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  360. row.AddCell().SetValue(id)
  361. } else {
  362. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  363. row.AddCell().SetValue(id)
  364. }
  365. } else {
  366. row.AddCell().SetValue(baseInfo[v])
  367. }
  368. }
  369. }
  370. case 3:
  371. // 标的物 采购意向数据拆分
  372. if baseInfo["procurementlist"] != nil {
  373. plist := baseInfo["procurementlist"].([]interface{})
  374. for _, p := range plist {
  375. row := sheet.AddRow()
  376. m := make(map[string]interface{})
  377. p1 := p.(map[string]interface{})
  378. for _, v := range FiedlsPurchase {
  379. if v == "projectname_purchase" || v == "projectscope_purchase" || v == "item" || v == "buyer_purchase" ||
  380. v == "totalprice" || v == "expurasingtime" || v == "cgyxxqhref" || v == "remark" {
  381. v1 := strings.ReplaceAll(v, "_purchase", "")
  382. row.AddCell().SetValue(p1[v1])
  383. m[v] = p1[v1]
  384. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  385. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  386. str := ""
  387. if baseInfo[v] != nil {
  388. date := util.Int64All(baseInfo[v])
  389. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  390. }
  391. row.AddCell().SetValue(str)
  392. m[v] = str
  393. } else if v == "id" {
  394. if tmp["id"] != nil {
  395. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  396. row.AddCell().SetValue(id)
  397. m[v] = id
  398. } else {
  399. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  400. row.AddCell().SetValue(id)
  401. m[v] = id
  402. }
  403. } else {
  404. row.AddCell().SetValue(baseInfo[v])
  405. m[v] = baseInfo[v]
  406. }
  407. }
  408. Mgo.Save("bidding_v1", m)
  409. }
  410. } else {
  411. row := sheet.AddRow()
  412. m := make(map[string]interface{})
  413. for _, v := range FiedlsPurchase {
  414. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  415. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  416. str := ""
  417. if baseInfo[v] != nil {
  418. date := util.Int64All(baseInfo[v])
  419. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  420. }
  421. row.AddCell().SetValue(str)
  422. m[v] = str
  423. } else if v == "id" {
  424. if tmp["id"] != nil {
  425. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  426. row.AddCell().SetValue(id)
  427. m[v] = id
  428. } else {
  429. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  430. row.AddCell().SetValue(id)
  431. m[v] = id
  432. }
  433. } else {
  434. row.AddCell().SetValue(baseInfo[v])
  435. m[v] = baseInfo[v]
  436. }
  437. }
  438. Mgo.Save(DbColl+"_cf", m)
  439. }
  440. case 4:
  441. // 多包 标的物拆分
  442. if baseInfo["package"] != nil {
  443. pkg := baseInfo["package"].(map[string]interface{})
  444. for _, p := range pkg {
  445. p1 := p.(map[string]interface{})
  446. winner := []string{}
  447. bidamount := float64(0)
  448. if p1["winner_all"] != nil {
  449. if all := p1["winner_all"].([]interface{}); all != nil {
  450. if len(all) > 0 {
  451. for _, a := range all {
  452. a1 := a.(map[string]interface{})
  453. if util.ObjToString(a1["winner"]) != "" {
  454. winner = append(winner, util.ObjToString(a1["winner"]))
  455. }
  456. bidamount = util.Float64All(a1["bidamount"])
  457. }
  458. }
  459. }
  460. }
  461. if len(winner) <= 0 {
  462. continue
  463. }
  464. if plist, ok := p1["purchasinglist"].([]interface{}); ok {
  465. for _, p := range plist {
  466. row := sheet.AddRow()
  467. p1 := p.(map[string]interface{})
  468. for _, v := range FieldsArr {
  469. if v == "itemname" || v == "brandname" || v == "madel" || v == "unitname" || v == "unitprice" || v == "number" {
  470. row.AddCell().SetValue(p1[v])
  471. } else if v == "totalprice" {
  472. if p1["totalprice"] != nil {
  473. row.AddCell().SetValue(p1[v])
  474. } else {
  475. if p1["unitprice"] != nil && p1["number"] != nil {
  476. d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"]))))
  477. row.AddCell().SetValue(d1)
  478. } else {
  479. row.AddCell().SetValue("")
  480. }
  481. }
  482. } else if v == "bidamount" {
  483. row.AddCell().SetValue(bidamount)
  484. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  485. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  486. str := ""
  487. if baseInfo[v] != nil {
  488. date := util.Int64All(baseInfo[v])
  489. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  490. }
  491. row.AddCell().SetValue(str)
  492. } else if v == "id" {
  493. if tmp["id"] != nil {
  494. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  495. row.AddCell().SetValue(id)
  496. } else {
  497. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  498. row.AddCell().SetValue(id)
  499. }
  500. } else {
  501. row.AddCell().SetValue(baseInfo[v])
  502. }
  503. }
  504. }
  505. }
  506. }
  507. } else {
  508. row := sheet.AddRow()
  509. for _, v := range FieldsArr {
  510. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  511. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  512. str := ""
  513. if baseInfo[v] != nil {
  514. date := util.Int64All(baseInfo[v])
  515. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  516. }
  517. row.AddCell().SetValue(str)
  518. } else if v == "id" {
  519. if tmp["id"] != nil {
  520. id := SE.EncodeString(util.ObjToString(tmp["id"]))
  521. row.AddCell().SetValue(id)
  522. } else {
  523. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  524. row.AddCell().SetValue(id)
  525. }
  526. } else {
  527. row.AddCell().SetValue(baseInfo[v])
  528. }
  529. }
  530. }
  531. }
  532. }
  533. util.Debug("over ---", count)
  534. fname := fmt.Sprintf("./数据导出%s.xlsx", util.NowFormat(util.DATEFORMAT))
  535. err = file.Save(fname)
  536. if err != nil {
  537. panic(err)
  538. }
  539. }