main.go 15 KB

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