main.go 15 KB

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