面包店
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

svc_data_statisstics.go 8.5 KiB

pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/utils"
  7. "applet/app/utils/logx"
  8. "encoding/json"
  9. "github.com/360EntSecGroup-Skylar/excelize"
  10. "strconv"
  11. "time"
  12. )
  13. func DataStatisticsExport(req md.DataStatisticsExportReq) {
  14. var fileName, downloadPath string
  15. var now = time.Now()
  16. switch req.Kind {
  17. case 1:
  18. //商品销售明细表
  19. fileName = "商品销售明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  20. downloadPath = "./data/export/" + fileName
  21. //查询数据
  22. eg := db.Db
  23. list, _ := db.GetOrderListExport(eg, map[string]string{
  24. "start_at": req.StartDate,
  25. "end_at": req.EndDate,
  26. "enterprise_id": strconv.Itoa(req.EnterpriseId),
  27. "start_at_for_date": utils.TimeParseStd(req.DateForStartDate).Format("20060102"),
  28. "end_at_for_date": utils.TimeParseStd(req.DateForEndDate).Format("20060102"),
  29. })
  30. xlsx, _ := excelize.OpenFile("./static/template/商品销售明细表.xlsx")
  31. var data = map[string]map[int64]map[string]string{}
  32. if list != nil {
  33. enterpriseIds := make([]int, 0)
  34. for _, v := range *list {
  35. enterpriseIds = append(enterpriseIds, v.EnterpriseId)
  36. }
  37. enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds)
  38. for _, v := range *list {
  39. enterpriseName := "-"
  40. _, ok := enterpriseMap[v.EnterpriseId]
  41. if ok {
  42. enterpriseName = enterpriseMap[v.EnterpriseId].Name
  43. }
  44. if data[enterpriseName] == nil {
  45. data[enterpriseName] = make(map[int64]map[string]string)
  46. }
  47. if data[enterpriseName][v.Oid] == nil {
  48. data[enterpriseName][v.Oid] = make(map[string]string)
  49. }
  50. //TODO:: 查找 `order_goods`
  51. var orderGoods []model.OrderGoods
  52. err := eg.Where("oid = ? and goods_type=0", v.Oid).Find(&orderGoods)
  53. if err != nil {
  54. logx.Error(err)
  55. println("<<<<DataStatisticsExport111111>>>>>Error:::", err.Error())
  56. return
  57. }
  58. for _, vv := range orderGoods {
  59. data[enterpriseName][v.Oid]["date"] = v.CreateAt.Format("2006-01-02")
  60. data[enterpriseName][v.Oid]["goods_name"] = vv.GoodsTitle
  61. skuData := make([]md.Sku, 0)
  62. json.Unmarshal([]byte(vv.Sku), &skuData)
  63. skuStr := ""
  64. for _, v1 := range skuData {
  65. if skuStr != "" {
  66. skuStr += ";"
  67. }
  68. skuStr += v1.Value
  69. }
  70. data[enterpriseName][v.Oid]["sku_str"] = skuStr
  71. data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
  72. }
  73. }
  74. }
  75. sheetIndex := 2 //第一个表是模板表
  76. xlsx.NewSheet("Sheet1") //新建表格
  77. xlsx.CopySheet(1, sheetIndex)
  78. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(2), &[]interface{}{
  79. "日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
  80. })
  81. j := 4 //表头前三行被占用了,只能从第四行开始
  82. for k, v := range data {
  83. var total = 0
  84. for kk, vv := range v {
  85. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  86. vv["date"],
  87. utils.Int64ToStr(kk),
  88. k,
  89. vv["goods_name"],
  90. vv["sku_str"],
  91. vv["num"],
  92. })
  93. xlsx.SetRowHeight("Sheet1", j, 18)
  94. total += utils.StrToInt(vv["num"])
  95. j++
  96. }
  97. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
  98. if err != nil {
  99. logx.Error(err)
  100. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  101. panic(err)
  102. }
  103. xlsx.MergeCell("Sheet1", "A"+strconv.Itoa(j), "F"+strconv.Itoa(j)) //合并单元格
  104. xlsx.SetCellStyle("Sheet1", "A"+strconv.Itoa(j), "F"+strconv.Itoa(j), style)
  105. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  106. k + " 汇总: " + utils.IntToStr(total),
  107. })
  108. xlsx.SetRowHeight("Sheet1", j, 20)
  109. j++
  110. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{})
  111. xlsx.SetRowHeight("Sheet1", j, 20)
  112. j++
  113. }
  114. xlsx.DeleteSheet("template") //删除模板表
  115. //将文件保存至服务器
  116. xlsx.SaveAs(downloadPath)
  117. break
  118. case 2:
  119. //对账单
  120. fileName = "对账单_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  121. downloadPath = "./data/export/" + fileName
  122. //查询数据
  123. eg := db.Db
  124. list, _ := db.GetOrderListExport(eg, map[string]string{
  125. "start_at": req.StartDate,
  126. "end_at": req.EndDate,
  127. "enterprise_id": strconv.Itoa(req.EnterpriseId),
  128. "start_at_for_date": utils.TimeParseStd(req.DateForStartDate).Format("20060102"),
  129. "end_at_for_date": utils.TimeParseStd(req.DateForEndDate).Format("20060102"),
  130. })
  131. xlsx, _ := excelize.OpenFile("./static/template/对账单.xlsx")
  132. var data = map[string]map[int64]map[string]string{}
  133. if list != nil {
  134. enterpriseIds := make([]int, 0)
  135. for _, v := range *list {
  136. enterpriseIds = append(enterpriseIds, v.EnterpriseId)
  137. }
  138. enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds)
  139. for _, v := range *list {
  140. enterpriseName := "-"
  141. _, ok := enterpriseMap[v.EnterpriseId]
  142. if ok {
  143. enterpriseName = enterpriseMap[v.EnterpriseId].Name
  144. }
  145. if data[enterpriseName] == nil {
  146. data[enterpriseName] = make(map[int64]map[string]string)
  147. }
  148. if data[enterpriseName][v.Oid] == nil {
  149. data[enterpriseName][v.Oid] = make(map[string]string)
  150. }
  151. //TODO:: 查找 `order_goods`
  152. var orderGoods []model.OrderGoods
  153. err := eg.Where("oid = ? and goods_type=0", v.Oid).Find(&orderGoods)
  154. if err != nil {
  155. logx.Error(err)
  156. println("<<<<DataStatisticsExport111111>>>>>Error:::", err.Error())
  157. return
  158. }
  159. for _, vv := range orderGoods {
  160. data[enterpriseName][v.Oid]["date"] = v.CreateAt.Format("2006-01-02")
  161. data[enterpriseName][v.Oid]["goods_name"] = vv.GoodsTitle
  162. data[enterpriseName][v.Oid]["ord_no"] = vv.OrdNo
  163. skuData := make([]md.Sku, 0)
  164. json.Unmarshal([]byte(vv.Sku), &skuData)
  165. skuStr := ""
  166. for _, v1 := range skuData {
  167. if skuStr != "" {
  168. skuStr += ";"
  169. }
  170. skuStr += v1.Value
  171. }
  172. data[enterpriseName][v.Oid]["sku_str"] = skuStr
  173. data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
  174. data[enterpriseName][v.Oid]["price"] = vv.Price
  175. }
  176. }
  177. }
  178. sheetIndex := 2 //第一个表是模板表
  179. for k, v := range data {
  180. xlsx.NewSheet(k) //新建表格
  181. xlsx.CopySheet(1, sheetIndex)
  182. xlsx.SetSheetRow(k, "A"+strconv.Itoa(2), &[]interface{}{
  183. "客户: " + k,
  184. })
  185. xlsx.SetSheetRow(k, "A"+strconv.Itoa(5), &[]interface{}{
  186. "对账单日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
  187. })
  188. j := 7 //表头前三行被占用了,只能从第四行开始
  189. var total, totalAmount float64
  190. for kk, vv := range v {
  191. if vv["ord_no"] == "" {
  192. vv["ord_no"] = utils.Int64ToStr(kk)
  193. }
  194. amount := utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"])
  195. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  196. vv["date"],
  197. vv["ord_no"],
  198. k,
  199. vv["goods_name"],
  200. vv["sku_str"],
  201. vv["num"],
  202. vv["price"],
  203. utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"]),
  204. })
  205. xlsx.SetRowHeight("k", j, 18)
  206. total += utils.StrToFloat64(vv["num"])
  207. totalAmount += amount
  208. style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center", "vertical": "center"}}`)
  209. if err != nil {
  210. logx.Error(err)
  211. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  212. panic(err)
  213. }
  214. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style)
  215. j++
  216. }
  217. style1, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
  218. if err != nil {
  219. logx.Error(err)
  220. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  221. panic(err)
  222. }
  223. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style1)
  224. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  225. "合计",
  226. "",
  227. "",
  228. "",
  229. "",
  230. total,
  231. "",
  232. totalAmount,
  233. })
  234. xlsx.SetRowHeight("k", j, 20)
  235. sheetIndex++
  236. }
  237. xlsx.DeleteSheet("template") //删除模板表
  238. //将文件保存至服务器
  239. xlsx.SaveAs(downloadPath)
  240. break
  241. }
  242. //新增数据
  243. exportRecordsDb := db.ExportRecordsDb{}
  244. exportRecordsDb.Set()
  245. marshal, _ := json.Marshal(req)
  246. exportRecordsDb.ExportRecordsExportRecordsInsert(&model.ExportRecords{
  247. Name: fileName,
  248. DownloadPath: downloadPath,
  249. Kind: req.Kind,
  250. ReqContent: string(marshal),
  251. CreateAt: now.Format("2006-01-02 15:04:05"),
  252. UpdateAt: now.Format("2006-01-02 15:04:05"),
  253. })
  254. }