面包店
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

266 lignes
8.4 KiB

  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. }
  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 = ? and goods_type=0", 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. data[enterpriseName][v.Oid]["ord_no"] = vv.OrdNo
  160. skuData := make([]md.Sku, 0)
  161. json.Unmarshal([]byte(vv.Sku), &skuData)
  162. skuStr := ""
  163. for _, v1 := range skuData {
  164. if skuStr != "" {
  165. skuStr += ";"
  166. }
  167. skuStr += v1.Value
  168. }
  169. data[enterpriseName][v.Oid]["sku_str"] = skuStr
  170. data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
  171. data[enterpriseName][v.Oid]["price"] = vv.Price
  172. }
  173. }
  174. }
  175. sheetIndex := 2 //第一个表是模板表
  176. for k, v := range data {
  177. xlsx.NewSheet(k) //新建表格
  178. xlsx.CopySheet(1, sheetIndex)
  179. xlsx.SetSheetRow(k, "A"+strconv.Itoa(2), &[]interface{}{
  180. "客户: " + k,
  181. })
  182. xlsx.SetSheetRow(k, "A"+strconv.Itoa(5), &[]interface{}{
  183. "对账单日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
  184. })
  185. j := 7 //表头前三行被占用了,只能从第四行开始
  186. var total, totalAmount float64
  187. for kk, vv := range v {
  188. if vv["ord_no"] == "" {
  189. vv["ord_no"] = utils.Int64ToStr(kk)
  190. }
  191. amount := utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"])
  192. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  193. vv["date"],
  194. vv["ord_no"],
  195. k,
  196. vv["goods_name"],
  197. vv["sku_str"],
  198. vv["num"],
  199. vv["price"],
  200. utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"]),
  201. })
  202. xlsx.SetRowHeight("k", j, 18)
  203. total += utils.StrToFloat64(vv["num"])
  204. totalAmount += amount
  205. style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center", "vertical": "center"}}`)
  206. if err != nil {
  207. logx.Error(err)
  208. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  209. panic(err)
  210. }
  211. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style)
  212. j++
  213. }
  214. style1, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
  215. if err != nil {
  216. logx.Error(err)
  217. println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
  218. panic(err)
  219. }
  220. xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style1)
  221. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
  222. "合计",
  223. "",
  224. "",
  225. "",
  226. "",
  227. total,
  228. "",
  229. totalAmount,
  230. })
  231. xlsx.SetRowHeight("k", j, 20)
  232. sheetIndex++
  233. }
  234. xlsx.DeleteSheet("template") //删除模板表
  235. //将文件保存至服务器
  236. xlsx.SaveAs(downloadPath)
  237. break
  238. }
  239. //新增数据
  240. exportRecordsDb := db.ExportRecordsDb{}
  241. exportRecordsDb.Set()
  242. marshal, _ := json.Marshal(req)
  243. exportRecordsDb.ExportRecordsExportRecordsInsert(&model.ExportRecords{
  244. Name: fileName,
  245. DownloadPath: downloadPath,
  246. Kind: req.Kind,
  247. ReqContent: string(marshal),
  248. CreateAt: now.Format("2006-01-02 15:04:05"),
  249. UpdateAt: now.Format("2006-01-02 15:04:05"),
  250. })
  251. }