main.go 18 KB

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