main.go 12 KB

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