main.go 15 KB

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