main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  116. row.AddCell().SetValue(id)
  117. } else {
  118. row.AddCell().SetValue(baseInfo[v])
  119. }
  120. }
  121. case 1:
  122. // 拆分标的物
  123. if IsMark == 1 {
  124. if tagInfo["purchasinglist"] != nil {
  125. if baseInfo["purchasinglist"] != nil {
  126. plist := baseInfo["purchasinglist"].([]interface{})
  127. for _, p := range plist {
  128. row := sheet.AddRow()
  129. p1 := p.(map[string]interface{})
  130. for _, v := range FieldsArr {
  131. if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" {
  132. row.AddCell().SetValue(p1[v])
  133. } else if v == "totalprice" {
  134. if p1["totalprice"] != nil {
  135. row.AddCell().SetValue(p1[v])
  136. } else {
  137. if p1["unitprice"] != nil && p1["number"] != nil {
  138. d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"]))))
  139. row.AddCell().SetValue(d1)
  140. } else {
  141. row.AddCell().SetValue("")
  142. }
  143. }
  144. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  145. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  146. str := ""
  147. if baseInfo[v] != nil {
  148. date := util.Int64All(baseInfo[v])
  149. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  150. }
  151. row.AddCell().SetValue(str)
  152. } else if v == "id" {
  153. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  154. row.AddCell().SetValue(id)
  155. } else {
  156. row.AddCell().SetValue(baseInfo[v])
  157. }
  158. }
  159. }
  160. } else {
  161. row := sheet.AddRow()
  162. for _, v := range FieldsArr {
  163. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  164. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  165. str := ""
  166. if baseInfo[v] != nil {
  167. date := util.Int64All(baseInfo[v])
  168. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  169. }
  170. row.AddCell().SetValue(str)
  171. } else if v == "id" {
  172. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  173. row.AddCell().SetValue(id)
  174. } else {
  175. row.AddCell().SetValue(baseInfo[v])
  176. }
  177. }
  178. }
  179. } else {
  180. row := sheet.AddRow()
  181. for _, v := range FieldsArr {
  182. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  183. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  184. str := ""
  185. if baseInfo[v] != nil {
  186. date := util.Int64All(baseInfo[v])
  187. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  188. }
  189. row.AddCell().SetValue(str)
  190. } else if v == "id" {
  191. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  192. row.AddCell().SetValue(id)
  193. } else {
  194. row.AddCell().SetValue(baseInfo[v])
  195. }
  196. }
  197. }
  198. } else {
  199. util.Debug("是否标注isMark字段值有问题~", IsMark)
  200. break
  201. }
  202. case 2:
  203. // 多包拆分
  204. if baseInfo["package"] != nil {
  205. pkg := baseInfo["package"].(map[string]interface{})
  206. for _, p := range pkg {
  207. row := sheet.AddRow()
  208. p1 := p.(map[string]interface{})
  209. winner := []string{}
  210. bidamount := float64(0)
  211. if p1["winner_all"] != nil {
  212. if all := p1["winner_all"].([]interface{}); all != nil {
  213. if len(all) > 0 {
  214. for _, a := range all {
  215. a1 := a.(map[string]interface{})
  216. if util.ObjToString(a1["winner"]) != "" {
  217. winner = append(winner, util.ObjToString(a1["winner"]))
  218. }
  219. bidamount += util.Float64All(a1["bidamount"])
  220. }
  221. }
  222. }
  223. }
  224. for _, v := range FieldsArr {
  225. if v == "s_winner" && len(winner) > 0 {
  226. row.AddCell().SetValue(strings.Join(winner, ","))
  227. } else if v == "bidamount" && bidamount != 0 {
  228. row.AddCell().SetValue(bidamount)
  229. } else if v == "winnerperson" || v == "winnertel" {
  230. row.AddCell().SetValue("")
  231. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  232. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  233. str := ""
  234. if baseInfo[v] != nil {
  235. date := util.Int64All(baseInfo[v])
  236. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  237. }
  238. row.AddCell().SetValue(str)
  239. } else if v == "id" {
  240. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  241. row.AddCell().SetValue(id)
  242. } else {
  243. row.AddCell().SetValue(baseInfo[v])
  244. }
  245. }
  246. }
  247. } else {
  248. row := sheet.AddRow()
  249. for _, v := range FieldsArr {
  250. 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. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  260. row.AddCell().SetValue(id)
  261. } else {
  262. row.AddCell().SetValue(baseInfo[v])
  263. }
  264. }
  265. }
  266. case 3:
  267. // 标的物 采购意向数据拆分
  268. if baseInfo["purchasinglist"] != nil {
  269. plist := baseInfo["purchasinglist"].([]interface{})
  270. for _, p := range plist {
  271. row := sheet.AddRow()
  272. m := make(map[string]interface{})
  273. p1 := p.(map[string]interface{})
  274. for _, v := range FiedlsPurchase {
  275. if v == "projectname_purchase" || v == "projectscope_purchase" || v == "item" || v == "buyer_purchase" ||
  276. v == "totalprice" || v == "expurasingtime" || v == "cgyxxqhref" {
  277. v1 := strings.ReplaceAll(v, "_purchase", "")
  278. row.AddCell().SetValue(p1[v1])
  279. m[v] = p1[v1]
  280. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  281. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  282. str := ""
  283. if baseInfo[v] != nil {
  284. date := util.Int64All(baseInfo[v])
  285. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  286. }
  287. row.AddCell().SetValue(str)
  288. m[v] = str
  289. } else if v == "id" {
  290. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  291. row.AddCell().SetValue(id)
  292. m[v] = id
  293. } else {
  294. row.AddCell().SetValue(baseInfo[v])
  295. m[v] = baseInfo[v]
  296. }
  297. }
  298. Mgo.Save("bidding_v1", m)
  299. }
  300. } else {
  301. row := sheet.AddRow()
  302. m := make(map[string]interface{})
  303. for _, v := range FiedlsPurchase {
  304. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  305. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  306. str := ""
  307. if baseInfo[v] != nil {
  308. date := util.Int64All(baseInfo[v])
  309. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  310. }
  311. row.AddCell().SetValue(str)
  312. m[v] = str
  313. } else if v == "id" {
  314. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  315. row.AddCell().SetValue(id)
  316. m[v] = id
  317. } else {
  318. row.AddCell().SetValue(baseInfo[v])
  319. m[v] = baseInfo[v]
  320. }
  321. }
  322. Mgo.Save("bidding_v1", m)
  323. }
  324. }
  325. }
  326. util.Debug("over ---", count)
  327. fname := fmt.Sprintf("./数据导出%s.xlsx", util.NowFormat(util.DATEFORMAT))
  328. err = file.Save(fname)
  329. if err != nil {
  330. panic(err)
  331. }
  332. }