| | |
| | | package service |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/xuri/excelize/v2" |
| | | "strconv" |
| | | "time" |
| | | "wms/constvar" |
| | | "wms/models" |
| | | "wms/pkg/logx" |
| | | "wms/request" |
| | |
| | | return "", err |
| | | } |
| | | |
| | | // 设置表头 |
| | | f.SetCellValue("Sheet1", "A1", "产品编码") |
| | | f.SetCellValue("Sheet1", "B1", "产品名称") |
| | | f.SetCellValue("Sheet1", "C1", "期初库存") |
| | | f.MergeCell("Sheet1", "C1", "F1") // 合并单元格 |
| | | f.SetCellValue("Sheet1", "G1", "本月入库") |
| | | f.MergeCell("Sheet1", "G1", "J1") // 合并单元格 |
| | | f.SetCellValue("Sheet1", "K1", "本月出库") |
| | | f.MergeCell("Sheet1", "K1", "N1") // 合并单元格 |
| | | f.SetCellValue("Sheet1", "O1", "期末库存") |
| | | f.MergeCell("Sheet1", "O1", "R1") // 合并单元格 |
| | | unitData, unitIndexMap := GetAllUnits() |
| | | unitLen := len(unitData) |
| | | |
| | | unitData := []string{"件", "匹", "米", "重量"} |
| | | for i, header := range unitData { |
| | | cell := getColumnAlphabet(i+3) + "2" |
| | | f.SetCellValue("Sheet1", cell, header) |
| | | } |
| | | for i, header := range unitData { |
| | | cell := getColumnAlphabet(i+7) + "2" |
| | | f.SetCellValue("Sheet1", cell, header) |
| | | } |
| | | for i, header := range unitData { |
| | | cell := getColumnAlphabet(i+11) + "2" |
| | | f.SetCellValue("Sheet1", cell, header) |
| | | } |
| | | for i, header := range unitData { |
| | | cell := getColumnAlphabet(i+15) + "2" |
| | | f.SetCellValue("Sheet1", cell, header) |
| | | } |
| | | |
| | | // 设置表头样式 |
| | | style := &excelize.Style{ |
| | | Border: nil, |
| | | Fill: excelize.Fill{ |
| | | Type: "pattern", |
| | | Pattern: 1, |
| | | Shading: 0, |
| | | }, |
| | | Font: &excelize.Font{ |
| | | Bold: true, |
| | | }, |
| | | Alignment: &excelize.Alignment{ |
| | | Horizontal: "center", |
| | | }, |
| | | } |
| | | titleStyle, err := f.NewStyle(style) |
| | | // 自定义表头 |
| | | headers := []interface{}{"产品编码", "产品名称", map[string][]string{"期初库存": unitData}, map[string][]string{"本月入库": unitData}, map[string][]string{"本月出库": unitData}, map[string][]string{"期末库存": unitData}} |
| | | lastColumnNumber, err := SetExcelHeaders(headers, f) |
| | | if err != nil { |
| | | return |
| | | return "", err |
| | | } |
| | | f.SetCellStyle("Sheet1", "A1", "R2", titleStyle) |
| | | |
| | | f.MergeCell("Sheet1", "A1", "A2") // 合并单元格 |
| | | f.MergeCell("Sheet1", "B1", "B2") // 合并单元格 |
| | | |
| | | titleStyle, err := SetHeaderStyle(f) |
| | | if err != nil { |
| | | return "", err |
| | | } |
| | | |
| | | lastColumn := getColumnAlphabet(lastColumnNumber) |
| | | f.SetCellStyle("Sheet1", "A1", lastColumn+"2", titleStyle) |
| | | // 设置列宽 |
| | | f.SetColWidth("Sheet1", "A", "B", 30) |
| | | f.SetColWidth("Sheet1", "C", "R", 15) |
| | | f.SetColWidth("Sheet1", "C", lastColumn, 15) |
| | | |
| | | 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.BeginAmount) |
| | | row := strconv.Itoa(i + 3) |
| | | f.SetCellValue("Sheet1", "A"+row, v.ProductId) |
| | | f.SetCellValue("Sheet1", "B"+row, v.ProductName) |
| | | |
| | | v.BeginMoreUnitsArr = append(v.BeginMoreUnitsArr, models.UnitItems{Amount: v.BeginAmount, Unit: v.Unit}) |
| | | for _, v := range v.BeginMoreUnitsArr { |
| | | switch v.Unit { |
| | | case "件": |
| | | f.SetCellValue("Sheet1", "D"+column, v.Amount) |
| | | case "匹": |
| | | f.SetCellValue("Sheet1", "E"+column, v.Amount) |
| | | case "米": |
| | | f.SetCellValue("Sheet1", "F"+column, v.Amount) |
| | | } |
| | | f.SetCellValue("Sheet1", getColumnAlphabet(3+unitIndexMap[v.Unit])+row, v.Amount) |
| | | } |
| | | |
| | | f.SetCellValue("Sheet1", "G"+column, v.InputAmount) |
| | | v.InputMoreUnitsArr = append(v.InputMoreUnitsArr, models.UnitItems{Amount: v.InputAmount, Unit: v.Unit}) |
| | | for _, v := range v.InputMoreUnitsArr { |
| | | switch v.Unit { |
| | | case "件": |
| | | f.SetCellValue("Sheet1", "H"+column, v.Amount) |
| | | case "匹": |
| | | f.SetCellValue("Sheet1", "I"+column, v.Amount) |
| | | case "米": |
| | | f.SetCellValue("Sheet1", "J"+column, v.Amount) |
| | | } |
| | | f.SetCellValue("Sheet1", getColumnAlphabet(3+unitLen*1+unitIndexMap[v.Unit])+row, v.Amount) |
| | | } |
| | | |
| | | f.SetCellValue("Sheet1", "K"+column, v.OutputAmount) |
| | | v.OutputMoreUnitsArr = append(v.OutputMoreUnitsArr, models.UnitItems{Amount: v.OutputAmount, Unit: v.Unit}) |
| | | for _, v := range v.OutputMoreUnitsArr { |
| | | switch v.Unit { |
| | | case "件": |
| | | f.SetCellValue("Sheet1", "L"+column, v.Amount) |
| | | case "匹": |
| | | f.SetCellValue("Sheet1", "M"+column, v.Amount) |
| | | case "米": |
| | | f.SetCellValue("Sheet1", "N"+column, v.Amount) |
| | | } |
| | | f.SetCellValue("Sheet1", getColumnAlphabet(3+unitLen*2+unitIndexMap[v.Unit])+row, v.Amount) |
| | | } |
| | | |
| | | f.SetCellValue("Sheet1", "O"+column, v.EndAmount) |
| | | v.EndMoreUnitsArr = append(v.EndMoreUnitsArr, models.UnitItems{Amount: v.EndAmount, Unit: v.Unit}) |
| | | for _, v := range v.EndMoreUnitsArr { |
| | | switch v.Unit { |
| | | case "件": |
| | | f.SetCellValue("Sheet1", "P"+column, v.Amount) |
| | | case "匹": |
| | | f.SetCellValue("Sheet1", "Q"+column, v.Amount) |
| | | case "米": |
| | | f.SetCellValue("Sheet1", "R"+column, v.Amount) |
| | | } |
| | | f.SetCellValue("Sheet1", getColumnAlphabet(3+unitLen*3+unitIndexMap[v.Unit])+row, v.Amount) |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | return fileName, nil |
| | | } |
| | | |
| | | func GetCurrentStats(date string, productIds []string) (statRecords []*models.MonthStats, err error) { |
| | | //本月期初数量/上月结余数量 |
| | | groupSumList, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).GroupSum("product_id", "amount") |
| | | productIds = make([]string, 0, len(groupSumList)) |
| | | for _, groupSum := range groupSumList { |
| | | productIds = append(productIds, groupSum.Class) |
| | | } |
| | | products, err := models.NewMaterialSearch().SetFields("id, name, unit, weight, more_unit, more_unit_value").SetIDs(productIds).FindNotTotal() |
| | | if err != nil { |
| | | logx.Errorf("MonthStats GetCurrentStats get products err:%v", err) |
| | | return |
| | | } |
| | | productMap := models.MaterialMap(products) |
| | | |
| | | //按配置取开始时间和结束时间 |
| | | beginTime, endTime, err := NewSystemConfigService().GetInventoryCutOffTime() |
| | | if err != nil { |
| | | logx.Errorf("MonthStats GetCurrentStats get GetInventoryCutOffTime err:%v", err) |
| | | return |
| | | } |
| | | inputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeIncoming) |
| | | if err != nil { |
| | | logx.Errorf("MonthStats GetStatsByOperationType input err:%v", err) |
| | | return |
| | | } |
| | | |
| | | outputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeOutgoing) |
| | | if err != nil { |
| | | logx.Errorf("MonthStats GetStatsByOperationType output err:%v", err) |
| | | return |
| | | } |
| | | |
| | | for _, groupSum := range groupSumList { |
| | | productId := groupSum.Class |
| | | if productMap[productId] == nil { |
| | | continue |
| | | } |
| | | product := productMap[productId] |
| | | amount := groupSum.Sum |
| | | record := models.MonthStats{ |
| | | ProductId: productId, |
| | | ProductName: product.Name, |
| | | Unit: product.Unit, |
| | | Weight: product.Weight.Mul(amount), |
| | | Date: date, |
| | | } |
| | | |
| | | var ( |
| | | moreUnits string |
| | | inputMoreUnits string |
| | | outputMoreUnits string |
| | | ) |
| | | if *product.MoreUnit { |
| | | moreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList)) |
| | | inputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList)) |
| | | outputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList)) |
| | | if !amount.IsZero() { |
| | | moreValueArr = CreateMoreUnit(amount, product.MoreUnitList) |
| | | } |
| | | if !inputMap[productId].IsZero() { |
| | | inputMoreValueArr = CreateMoreUnit(inputMap[productId], product.MoreUnitList) |
| | | } |
| | | |
| | | if !outputMap[productId].IsZero() { |
| | | outputMoreValueArr = CreateMoreUnit(outputMap[productId], product.MoreUnitList) |
| | | } |
| | | bys, _ := json.Marshal(moreValueArr) |
| | | moreUnits = string(bys) |
| | | bys, _ = json.Marshal(inputMoreValueArr) |
| | | inputMoreUnits = string(bys) |
| | | bys, _ = json.Marshal(outputMoreValueArr) |
| | | outputMoreUnits = string(bys) |
| | | } |
| | | |
| | | record.InputAmount = inputMap[productId] |
| | | record.InputMoreUnits = inputMoreUnits |
| | | record.OutputAmount = outputMap[productId] |
| | | record.OutputMoreUnits = outputMoreUnits |
| | | record.EndAmount = amount |
| | | record.EndMoreUnits = moreUnits |
| | | statRecords = append(statRecords, &record) |
| | | } |
| | | |
| | | return |
| | | } |
| | | |
| | | func GetStatsByOperationType(beginTime, endTime time.Time, operationType constvar.BaseOperationType) (m map[string]decimal.Decimal, err error) { |
| | | operationIds, err := models.NewOperationSearch().SetBaseOperationType(operationType).SetFields("id").SetTimeBetween(beginTime, endTime).FindIds() |
| | | if err != nil { |
| | | return |
| | | } |
| | | groupSumList, err := models.NewOperationDetailsSearch().SetOperationIds(operationIds).SetFields("product_id, amount").GroupSum("product_id", "amount") |
| | | if err != nil { |
| | | return |
| | | } |
| | | m = make(map[string]decimal.Decimal, len(groupSumList)) |
| | | for _, v := range groupSumList { |
| | | m[v.Class] = v.Sum |
| | | } |
| | | return |
| | | } |