zhangqian
2024-07-02 9d296a1c658c19c6faa51dfef31d025e59a480cb
service/warehouse_month_forms.go
@@ -1,7 +1,10 @@
package service
import (
   "fmt"
   "github.com/shopspring/decimal"
   "github.com/xuri/excelize/v2"
   "strconv"
   "time"
   "wms/constvar"
   "wms/models"
@@ -58,6 +61,101 @@
   return
}
func (slf *WarehouseMonthFormsService) Export(dataList []*models.WarehouseMonthStats) (filename string, err error) {
   // 创建一个新的 Excel 文件
   f := excelize.NewFile()
   if err != nil {
      logx.Errorf("NewSheet err:%v", err)
      return "", err
   }
   headers, headerLen, inputTypes, outputTypes := slf.GetHeaders()
   err = SetExcelHeader(headers, f)
   if err != nil {
      logx.Errorf("SetExcelHeader err:%v", err)
      return "", err
   }
   //表头样式
   style, err := SetHeaderStyle(f)
   if err != nil {
      return "", err
   }
   f.SetCellStyle("Sheet1", "A1", getColumnAlphabet(headerLen)+"2", style)
   // 设置列宽
   f.SetColWidth("Sheet1", "A", "F", 30)
   f.SetColWidth("Sheet1", "G", getColumnAlphabet(headerLen), 15)
   inputStart := 7
   outputStart := 7 + len(inputTypes)
   for i, v := range dataList {
      column := strconv.Itoa(i + 3)
      f.SetCellValue("Sheet1", "A"+column, v.ProductId)
      f.SetCellValue("Sheet1", "B"+column, v.ProductName)
      f.SetCellValue("Sheet1", "C"+column, v.EndAmount)
      f.SetCellValue("Sheet1", "D"+column, v.BeginAmount)
      f.SetCellValue("Sheet1", "E"+column, v.Unit)
      f.SetCellValue("Sheet1", "F"+column, v.SalePrice)
      slf.FillDealerTypeToExcel(v.InputItems, inputStart, i+3, inputTypes, f)
      slf.FillDealerTypeToExcel(v.OutputItems, outputStart, i+3, outputTypes, f)
   }
   fileName := fmt.Sprintf("%s月度统计报表%s.xlsx", "仓库", time.Now().Format("2006-01-02-1504"))
   if err := f.SaveAs(fileName); err != nil {
      return fileName, err
   }
   return fileName, nil
}
func (slf *WarehouseMonthFormsService) GetHeaders() (headers []interface{}, headerLen int, inputTypes, outputTypes []string) {
   // 自定义表头
   //查询入库类型
   inputTypes = GetDictNameListByType(constvar.StorageType)
   inputTypes = append(inputTypes, constvar.InputTotalHeader)
   headerLen += len(inputTypes)
   //查询入库类型
   outputTypes = GetDictNameListByType(constvar.StockoutType)
   outputTypes = append(outputTypes, constvar.OutPutTotalHeader)
   headerLen += len(inputTypes)
   headerLen += 6
   headers = []interface{}{"物料编号", "物料编码", "月末结存", "月初结存", "单位", "单价", map[string][]string{"本月入库": inputTypes}, map[string][]string{"本月出库": outputTypes}}
   return headers, headerLen, inputTypes, outputTypes
}
func (slf *WarehouseMonthFormsService) SumItems(items []*models.WarehouseStatsItems) (sum decimal.Decimal) {
   for _, v := range items {
      sum = sum.Add(v.Amount)
   }
   return sum
}
func (slf *WarehouseMonthFormsService) FillDealerTypeToExcel(items []*models.WarehouseStatsItems, startIndex int, column int, dealerTypes []string, f *excelize.File) {
   columnStr := strconv.Itoa(column)
   sum := slf.SumItems(items)
   detailMap := models.WarehouseStatsItemMap(items)
   for i := 0; i < len(dealerTypes); i++ {
      var amount decimal.Decimal
      if detailMap[dealerTypes[i]] != nil {
         amount = detailMap[dealerTypes[i]].Amount
      } else if dealerTypes[i] == constvar.InputTotalHeader {
         amount = sum
      }
      f.SetCellValue("Sheet1", getColumnAlphabet(startIndex+i)+columnStr, amount)
   }
   return
}
func GetCurrentWarehouseStats(date string, warehouseId int, productIds []string) (statRecords []*models.WarehouseMonthStats, err error) {
   //本月期初数量/上月结余数量
   groupSumList, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).GroupSum("product_id", "amount")