|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/utils"
- "applet/app/utils/logx"
- "encoding/json"
- "github.com/360EntSecGroup-Skylar/excelize"
- "strconv"
- "time"
- )
-
- func DataStatisticsExport(req md.DataStatisticsExportReq) {
- var fileName, downloadPath string
- var now = time.Now()
- switch req.Kind {
- case 1:
- //商品销售明细表
- fileName = "商品销售明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "./data/export/" + fileName
-
- //查询数据
- eg := db.Db
- list, _ := db.GetOrderListExport(eg, map[string]string{
- "start_at": req.StartDate,
- "end_at": req.EndDate,
- "enterprise_id": strconv.Itoa(req.EnterpriseId),
- "start_at_for_date": utils.TimeParseStd(req.DateForStartDate).Format("20060102"),
- "end_at_for_date": utils.TimeParseStd(req.DateForEndDate).Format("20060102"),
- })
- xlsx, _ := excelize.OpenFile("./static/template/商品销售明细表.xlsx")
- var data = map[string]map[int64]map[string]string{}
- if list != nil {
- enterpriseIds := make([]int, 0)
- for _, v := range *list {
- enterpriseIds = append(enterpriseIds, v.EnterpriseId)
- }
- enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds)
-
- for _, v := range *list {
- enterpriseName := "-"
- _, ok := enterpriseMap[v.EnterpriseId]
- if ok {
- enterpriseName = enterpriseMap[v.EnterpriseId].Name
- }
- if data[enterpriseName] == nil {
- data[enterpriseName] = make(map[int64]map[string]string)
- }
- if data[enterpriseName][v.Oid] == nil {
- data[enterpriseName][v.Oid] = make(map[string]string)
- }
- //TODO:: 查找 `order_goods`
- var orderGoods []model.OrderGoods
- err := eg.Where("oid = ?", v.Oid).Find(&orderGoods)
- if err != nil {
- logx.Error(err)
- println("<<<<DataStatisticsExport111111>>>>>Error:::", err.Error())
- return
- }
- for _, vv := range orderGoods {
- data[enterpriseName][v.Oid]["date"] = v.CreateAt.Format("2006-01-02")
- data[enterpriseName][v.Oid]["goods_name"] = vv.GoodsTitle
- skuData := make([]md.Sku, 0)
- json.Unmarshal([]byte(vv.Sku), &skuData)
- skuStr := ""
- for _, v1 := range skuData {
- if skuStr != "" {
- skuStr += ";"
- }
- skuStr += v1.Value
- }
- data[enterpriseName][v.Oid]["sku_str"] = skuStr
- data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
- }
- }
- }
- sheetIndex := 2 //第一个表是模板表
- xlsx.NewSheet("Sheet1") //新建表格
- xlsx.CopySheet(1, sheetIndex)
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(2), &[]interface{}{
- "日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
- })
-
- j := 4 //表头前三行被占用了,只能从第四行开始
- for k, v := range data {
- var total = 0
- for kk, vv := range v {
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- vv["date"],
- utils.Int64ToStr(kk),
- k,
- vv["goods_name"],
- vv["sku_str"],
- vv["num"],
- })
- xlsx.SetRowHeight("Sheet1", j, 18)
- total += utils.StrToInt(vv["num"])
- j++
- }
-
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.MergeCell("Sheet1", "A"+strconv.Itoa(j), "F"+strconv.Itoa(j)) //合并单元格
- xlsx.SetCellStyle("Sheet1", "A"+strconv.Itoa(j), "F"+strconv.Itoa(j), style)
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- k + " 汇总: " + utils.IntToStr(total),
- })
- xlsx.SetRowHeight("Sheet1", j, 20)
- j++
- }
-
- xlsx.DeleteSheet("template") //删除模板表
- //将文件保存至服务器
- xlsx.SaveAs(downloadPath)
- break
- case 2:
- //对账单
- fileName = "对账单_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "./data/export/" + fileName
-
- //查询数据
- eg := db.Db
- list, _ := db.GetOrderListExport(eg, map[string]string{
- "start_at": req.StartDate,
- "end_at": req.EndDate,
- "enterprise_id": strconv.Itoa(req.EnterpriseId),
- "start_at_for_date": utils.TimeParseStd(req.DateForStartDate).Format("20060102"),
- "end_at_for_date": utils.TimeParseStd(req.DateForEndDate).Format("20060102"),
- })
- xlsx, _ := excelize.OpenFile("./static/template/对账单.xlsx")
- var data = map[string]map[int64]map[string]string{}
- if list != nil {
- enterpriseIds := make([]int, 0)
- for _, v := range *list {
- enterpriseIds = append(enterpriseIds, v.EnterpriseId)
- }
- enterpriseMap := db.GetEnterpriseMore(eg, enterpriseIds)
-
- for _, v := range *list {
- enterpriseName := "-"
- _, ok := enterpriseMap[v.EnterpriseId]
- if ok {
- enterpriseName = enterpriseMap[v.EnterpriseId].Name
- }
- if data[enterpriseName] == nil {
- data[enterpriseName] = make(map[int64]map[string]string)
- }
- if data[enterpriseName][v.Oid] == nil {
- data[enterpriseName][v.Oid] = make(map[string]string)
- }
- //TODO:: 查找 `order_goods`
- var orderGoods []model.OrderGoods
- err := eg.Where("oid = ?", v.Oid).Find(&orderGoods)
- if err != nil {
- logx.Error(err)
- println("<<<<DataStatisticsExport111111>>>>>Error:::", err.Error())
- return
- }
- for _, vv := range orderGoods {
- data[enterpriseName][v.Oid]["date"] = v.CreateAt.Format("2006-01-02")
- data[enterpriseName][v.Oid]["goods_name"] = vv.GoodsTitle
- skuData := make([]md.Sku, 0)
- json.Unmarshal([]byte(vv.Sku), &skuData)
- skuStr := ""
- for _, v1 := range skuData {
- if skuStr != "" {
- skuStr += ";"
- }
- skuStr += v1.Value
- }
- data[enterpriseName][v.Oid]["sku_str"] = skuStr
- data[enterpriseName][v.Oid]["num"] = utils.IntToStr(vv.Num)
- data[enterpriseName][v.Oid]["price"] = vv.Price
- }
- }
- }
-
- sheetIndex := 2 //第一个表是模板表
- for k, v := range data {
- xlsx.NewSheet(k) //新建表格
- xlsx.CopySheet(1, sheetIndex)
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(2), &[]interface{}{
- "客户: " + k,
- })
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(5), &[]interface{}{
- "对账单日期:" + utils.TimeParseStd(req.StartDate).Format("2006-01-02") + " ~ " + utils.TimeParseStd(req.EndDate).Format("2006-01-02"),
- })
- j := 7 //表头前三行被占用了,只能从第四行开始
- var total, totalAmount float64
- for kk, vv := range v {
- amount := utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"])
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
- vv["date"],
- utils.Int64ToStr(kk),
- k,
- vv["goods_name"],
- vv["sku_str"],
- vv["num"],
- vv["price"],
- utils.StrToFloat64(vv["num"]) * utils.StrToFloat64(vv["price"]),
- })
- xlsx.SetRowHeight("k", j, 18)
- total += utils.StrToFloat64(vv["num"])
- totalAmount += amount
- style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center", "vertical": "center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style)
- j++
- }
-
- style1, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFFCC"],"pattern":1}, "alignment":{"horizontal":"center", "vertical": "center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<SupportForSchoolDataStatisticsExport>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle(k, "A"+strconv.Itoa(j), "H"+strconv.Itoa(j), style1)
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{
- "合计",
- "",
- "",
- "",
- "",
- total,
- "",
- totalAmount,
- })
- xlsx.SetRowHeight("k", j, 20)
- sheetIndex++
- }
-
- xlsx.DeleteSheet("template") //删除模板表
- //将文件保存至服务器
- xlsx.SaveAs(downloadPath)
- break
- }
-
- //新增数据
- exportRecordsDb := db.ExportRecordsDb{}
- exportRecordsDb.Set()
- marshal, _ := json.Marshal(req)
- exportRecordsDb.ExportRecordsExportRecordsInsert(&model.ExportRecords{
- Name: fileName,
- DownloadPath: downloadPath,
- Kind: req.Kind,
- ReqContent: string(marshal),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
|