main.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. SE = util.SimpleEncrypt{Key: "topJYBX2019"}
  19. )
  20. func init() {
  21. util.ReadConfig(&Sysconfig)
  22. Mgo = &mongodb.MongodbSim{
  23. MongodbAddr: Sysconfig["mgoAddr"].(string),
  24. Size: util.IntAllDef(Sysconfig["mgoSize"], 5),
  25. DbName: Sysconfig["mgoDbName"].(string),
  26. }
  27. Mgo.InitPool()
  28. Dbname = Sysconfig["mgoDbName"].(string)
  29. DbColl = Sysconfig["mgoColl"].(string)
  30. ExportType = util.IntAll(Sysconfig["exportType"])
  31. IsMark = util.IntAll(Sysconfig["isMark"])
  32. Search = Sysconfig["search"].(map[string]interface{})
  33. FieldsArr = util.ObjArrToStringArr(Sysconfig["fieldsSort"].([]interface{}))
  34. Fields = Sysconfig["fields"].(map[string]interface{})
  35. if ExportType == 1 {
  36. FieldsArr[len(FieldsArr)-1] = "itemname"
  37. FieldsArr = append(FieldsArr, "brandname", "model", "unitname", "unitprice", "number", "totalprice", "id")
  38. Fields["itemname"] = "产品名称"
  39. Fields["brandname"] = "品牌"
  40. Fields["model"] = "规格型号"
  41. Fields["unitname"] = "单位"
  42. Fields["unitprice"] = "单价"
  43. Fields["number"] = "数量"
  44. Fields["totalprice"] = "小计"
  45. }
  46. util.Debug(FieldsArr)
  47. }
  48. func main() {
  49. sess := Mgo.GetMgoConn()
  50. defer Mgo.DestoryMongoConn(sess)
  51. file := xlsx.NewFile()
  52. sheet, err := file.AddSheet("sheet1")
  53. if err != nil {
  54. panic(err)
  55. }
  56. row := sheet.AddRow()
  57. for _, v := range FieldsArr {
  58. row.AddCell().SetValue(Fields[v])
  59. }
  60. q := map[string]interface{}{}
  61. if len(Search) > 0 {
  62. q = Search
  63. }
  64. c := Mgo.Count(DbColl, q)
  65. util.Debug("search size result ---", DbColl, q, c)
  66. if c == 0 {
  67. return
  68. }
  69. query := sess.DB(Dbname).C(DbColl).Find(&q).Select(nil).Iter()
  70. count := 0
  71. for tmp := make(map[string]interface{}); query.Next(&tmp); count++ {
  72. if count%500 == 0 {
  73. util.Debug("current ---", count)
  74. }
  75. baseInfo := tmp["v_baseinfo"].(map[string]interface{})
  76. tagInfo := tmp["v_taginfo"].(map[string]interface{})
  77. if ExportType == 0 {
  78. row := sheet.AddRow()
  79. for _, v := range FieldsArr {
  80. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  81. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  82. str := ""
  83. if baseInfo[v] != nil {
  84. date := util.Int64All(baseInfo[v])
  85. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  86. }
  87. row.AddCell().SetValue(str)
  88. } else if v == "id" {
  89. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  90. row.AddCell().SetValue(id)
  91. } else {
  92. row.AddCell().SetValue(baseInfo[v])
  93. }
  94. }
  95. } else if ExportType == 1 {
  96. // 拆分标的物
  97. if IsMark == 1 {
  98. if tagInfo["purchasinglist"] != nil {
  99. if baseInfo["purchasinglist"] != nil {
  100. plist := baseInfo["purchasinglist"].([]interface{})
  101. for _, p := range plist {
  102. row := sheet.AddRow()
  103. p1 := p.(map[string]interface{})
  104. for _, v := range FieldsArr {
  105. if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" {
  106. row.AddCell().SetValue(p1[v])
  107. } else if v == "totalprice" {
  108. if p1["totalprice"] != nil {
  109. row.AddCell().SetValue(p1[v])
  110. } else {
  111. if p1["unitprice"] != nil && p1["number"] != nil {
  112. d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"]))))
  113. row.AddCell().SetValue(d1)
  114. } else {
  115. row.AddCell().SetValue("")
  116. }
  117. }
  118. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  119. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  120. str := ""
  121. if baseInfo[v] != nil {
  122. date := util.Int64All(baseInfo[v])
  123. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  124. }
  125. row.AddCell().SetValue(str)
  126. } else if v == "id" {
  127. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  128. row.AddCell().SetValue(id)
  129. } else {
  130. row.AddCell().SetValue(baseInfo[v])
  131. }
  132. }
  133. }
  134. } else {
  135. row := sheet.AddRow()
  136. for _, v := range FieldsArr {
  137. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  138. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  139. str := ""
  140. if baseInfo[v] != nil {
  141. date := util.Int64All(baseInfo[v])
  142. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  143. }
  144. row.AddCell().SetValue(str)
  145. } else if v == "id" {
  146. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  147. row.AddCell().SetValue(id)
  148. } else {
  149. row.AddCell().SetValue(baseInfo[v])
  150. }
  151. }
  152. }
  153. } else {
  154. row := sheet.AddRow()
  155. for _, v := range FieldsArr {
  156. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  157. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  158. str := ""
  159. if baseInfo[v] != nil {
  160. date := util.Int64All(baseInfo[v])
  161. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  162. }
  163. row.AddCell().SetValue(str)
  164. } else if v == "id" {
  165. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  166. row.AddCell().SetValue(id)
  167. } else {
  168. row.AddCell().SetValue(baseInfo[v])
  169. }
  170. }
  171. }
  172. } else {
  173. util.Debug("是否标注isMark字段值有问题~", IsMark)
  174. break
  175. }
  176. } else if ExportType == 2 {
  177. //if IsMark == 1 {
  178. //}
  179. if baseInfo["package"] != nil {
  180. pkg := baseInfo["package"].(map[string]interface{})
  181. for _, p := range pkg {
  182. row := sheet.AddRow()
  183. p1 := p.(map[string]interface{})
  184. winner := []string{}
  185. bidamount := float64(0)
  186. if p1["winner_all"] != nil {
  187. if all := p1["winner_all"].([]interface{}); all != nil {
  188. if len(all) > 0 {
  189. for _, a := range all {
  190. a1 := a.(map[string]interface{})
  191. if util.ObjToString(a1["winner"]) != "" {
  192. winner = append(winner, util.ObjToString(a1["winner"]))
  193. }
  194. bidamount += util.Float64All(a1["bidamount"])
  195. }
  196. }
  197. }
  198. }
  199. for _, v := range FieldsArr {
  200. if v == "s_winner" && len(winner) > 0 {
  201. row.AddCell().SetValue(strings.Join(winner, ","))
  202. } else if v == "bidamount" && bidamount != 0 {
  203. row.AddCell().SetValue(bidamount)
  204. } else if v == "winnerperson" || v == "winnertel" {
  205. row.AddCell().SetValue("")
  206. } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  207. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  208. str := ""
  209. if baseInfo[v] != nil {
  210. date := util.Int64All(baseInfo[v])
  211. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  212. }
  213. row.AddCell().SetValue(str)
  214. } else if v == "id" {
  215. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  216. row.AddCell().SetValue(id)
  217. } else {
  218. row.AddCell().SetValue(baseInfo[v])
  219. }
  220. }
  221. }
  222. } else {
  223. row := sheet.AddRow()
  224. for _, v := range FieldsArr {
  225. if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" ||
  226. v == "signaturedate" || v == "comeintime" || v == "createtime" {
  227. str := ""
  228. if baseInfo[v] != nil {
  229. date := util.Int64All(baseInfo[v])
  230. str = util.FormatDateByInt64(&date, util.Date_Short_Layout)
  231. }
  232. row.AddCell().SetValue(str)
  233. } else if v == "id" {
  234. id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"]))
  235. row.AddCell().SetValue(id)
  236. } else {
  237. row.AddCell().SetValue(baseInfo[v])
  238. }
  239. }
  240. }
  241. }
  242. }
  243. util.Debug("over ---", count)
  244. fname := fmt.Sprintf("./数据导出%s.xlsx", util.NowFormat(util.DATEFORMAT))
  245. err = file.Save(fname)
  246. if err != nil {
  247. panic(err)
  248. }
  249. }