面包店
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

svc_data_statisstics.go 8.3 KiB

7 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 = ?", 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. }
  111. xlsx.DeleteSheet("template") //删除模板表
  112. //将文件保存至服务器
  113. xlsx.SaveAs(downloadPath)
  114. break
  115. case 2:
  116. //对账单
  117. fileName = "对账单_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  118. downloadPath = "./data/export/" + fileName
  119. //查询数据
  120. eg := db.Db
  121. list, _ := db.GetOrderListExport(eg, map[string]string{
  122. "start_at": req.StartDate,
  123. "end_at": req.EndDate,
  124. "enterprise_id": strconv.Itoa(req.EnterpriseId),
  125. "start_at_for_date": utils.TimeParseStd(req.DateForStartDate).Format("20060102"),
  126. "end_at_for_date": utils.TimeParseStd(req.DateForEndDate).Format("20060102"),
  127. })
  128. xlsx, _ := excelize.OpenFile("./static/template/对账单.xlsx")
  129. var data = map[string]map[int64]map[string]string{}
  130. if list != nil {
  131. enterpriseIds := make([]int, 0)
  132. for _, v := range *list {
  133. enterpriseIds = append(enterpriseIds, v.EnterpriseId)
  134. }
  135. enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds)
  136. for _, v := range *list {
  137. enterpriseName := "-"
  138. _, ok := enterpriseMap[v.EnterpriseId]
  139. if ok {
  140. enterpriseName = enterpriseMap[v.EnterpriseId].Name
  141. }
  142. if data[enterpriseName] == nil {
  143. data[enterpriseName] = make(map[int64]map[string]string)
  144. }
  145. if data[enterpriseName][v.Oid] == nil {
  146. data[enterpriseName][v.Oid] = make(map[string]string)
  147. }
  148. //TODO:: 查找 `order_goods`
  149. var orderGoods []model.OrderGoods
  150. err := eg.Where("oid = ?", v.Oid).Find(&orderGoods)
  151. if err != nil {
  152. logx.Error(err)
  153. println("<<<<DataStatisticsExport111111>>>>>Error:::", err.Error())
  154. return
  155. }
  156. for _, vv := range orderGoods {
  157. data[enterpriseName][v.Oid]["date"] = v.CreateAt.Format("2006-01-02")
  158. data[enterpriseName][v.Oid]["goods_name"] = vv.GoodsTitle
  159. skuData := make([]md.Sku, 0)
  160. json.Unmarshal([]byte(vv.Sku), &skuData)
  161. skuStr := ""
  162. for _, v1 := range skuData {
  163. if skuStr != "" {
  164. skuStr += ";"
  165. }
  166. skuStr += v1.Value
  167. }
  168. data[enterpriseName][v.Oid]["sku_str"] = skuStr
  169. data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
  170. data[enterpriseName][v.Oid]["price"] = vv.Price
  171. }
  172. }
  173. }
  174. sheetIndex := 2 //第一个表是模板表
  175. for k, v := range data {
  176. xlsx.NewSheet(k) //新建表格
  177. xlsx.CopySheet(1, sheetIndex)
  178. xlsx.SetSheetRow(k, "A"+strconv.Itoa(2), &[]interface{}{
  179. "客户: " + k,
  180. })
  181. xlsx.SetSheetRow(k, "A"+strconv.Itoa(5), &[]interface{}{
  182. "对账单日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
  183. })
  184. j := 7 //表头前三行被占用了,只能从第四行开始
  185. var total, totalAmount float64
  186. for kk, vv := range v {
  187. amount := utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"])
  188. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  189. vv["date"],
  190. utils.Int64ToStr(kk),
  191. k,
  192. vv["goods_name"],
  193. vv["sku_str"],
  194. vv["num"],
  195. vv["price"],
  196. utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"]),
  197. })
  198. xlsx.SetRowHeight("k", j, 18)
  199. total += utils.StrToFloat64(vv["num"])
  200. totalAmount += amount
  201. style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center", "vertical": "center"}}`)
  202. if err != nil {
  203. logx.Error(err)
  204. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  205. panic(err)
  206. }
  207. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style)
  208. j++
  209. }
  210. style1, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
  211. if err != nil {
  212. logx.Error(err)
  213. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  214. panic(err)
  215. }
  216. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style1)
  217. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  218. "合计",
  219. "",
  220. "",
  221. "",
  222. "",
  223. total,
  224. "",
  225. totalAmount,
  226. })
  227. xlsx.SetRowHeight("k", j, 20)
  228. sheetIndex++
  229. }
  230. xlsx.DeleteSheet("template") //删除模板表
  231. //将文件保存至服务器
  232. xlsx.SaveAs(downloadPath)
  233. break
  234. }
  235. //新增数据
  236. exportRecordsDb := db.ExportRecordsDb{}
  237. exportRecordsDb.Set()
  238. marshal, _ := json.Marshal(req)
  239. exportRecordsDb.ExportRecordsExportRecordsInsert(&model.ExportRecords{
  240. Name: fileName,
  241. DownloadPath: downloadPath,
  242. Kind: req.Kind,
  243. ReqContent: string(marshal),
  244. CreateAt: now.Format("2006-01-02 15:04:05"),
  245. UpdateAt: now.Format("2006-01-02 15:04:05"),
  246. })
  247. }