package service
|
|
import (
|
"errors"
|
"github.com/shopspring/decimal"
|
"github.com/xuri/excelize/v2"
|
"strconv"
|
"wms/models"
|
)
|
|
func CreateMoreUnit(amount decimal.Decimal, units []models.UnitItems) []models.UnitItems {
|
moreValueArr := make([]models.UnitItems, 0, len(units))
|
for _, unitItem := range units {
|
if unitItem.Amount.IsZero() {
|
continue
|
}
|
moreValueArr = append(moreValueArr, models.UnitItems{
|
Amount: amount.Div(unitItem.Amount),
|
Unit: unitItem.Unit,
|
Floating: unitItem.Floating,
|
})
|
}
|
return moreValueArr
|
}
|
|
func AddMoreUnit(units []models.UnitItems, units2 []models.UnitItems) []models.UnitItems {
|
unitMap := make(map[string]models.UnitItems)
|
|
// 将 units 中的所有元素添加到 map 中
|
for _, unitItem := range units {
|
unitMap[unitItem.Unit] = unitItem
|
}
|
|
// 遍历 units2,合并相同单位的数量,并将 units2 中不存在于 units 中的单位添加到 map 中
|
for _, unitItem2 := range units2 {
|
if unitItem1, exists := unitMap[unitItem2.Unit]; exists {
|
unitItem1.Amount = unitItem1.Amount.Add(unitItem2.Amount)
|
unitMap[unitItem2.Unit] = unitItem1
|
} else {
|
unitMap[unitItem2.Unit] = unitItem2
|
}
|
}
|
|
// 将 map 转换回 slice
|
moreValueArr := make([]models.UnitItems, 0, len(unitMap))
|
for _, unitItem := range unitMap {
|
moreValueArr = append(moreValueArr, unitItem)
|
}
|
|
return moreValueArr
|
}
|
|
// MoreUnitIsEnough 多单位库存是否足够
|
func MoreUnitIsEnough(units []models.UnitItems, units2 []models.UnitItems) bool {
|
for _, unitItem1 := range units {
|
for _, unitItem2 := range units2 {
|
if unitItem1.Amount.LessThan(unitItem2.Amount) {
|
return false
|
}
|
}
|
}
|
return true
|
}
|
|
func SubMoreUnit(units []models.UnitItems, units2 []models.UnitItems) []models.UnitItems {
|
moreValueArr := make([]models.UnitItems, 0, len(units))
|
for _, unitItem1 := range units {
|
for _, unitItem2 := range units2 {
|
if unitItem1.Unit == unitItem2.Unit {
|
moreValueArr = append(moreValueArr, models.UnitItems{
|
Amount: unitItem1.Amount.Sub(unitItem2.Amount),
|
Unit: unitItem1.Unit,
|
Floating: unitItem1.Floating,
|
})
|
}
|
}
|
}
|
return moreValueArr
|
}
|
|
func FillMoreUnitToExcel(units []models.UnitItems, startIndex int, row int, unitIndexMap map[string]int, f *excelize.File) {
|
rowStr := strconv.Itoa(row)
|
for _, v := range units {
|
f.SetCellValue("Sheet1", getColumnAlphabet(startIndex+unitIndexMap[v.Unit])+rowStr, v.Amount)
|
}
|
return
|
}
|
|
func SetHeaderStyle(f *excelize.File) (styleInt int, err error) {
|
// 设置表头样式
|
style := &excelize.Style{
|
Border: nil,
|
Fill: excelize.Fill{
|
Type: "pattern",
|
Pattern: 1,
|
Shading: 0,
|
},
|
Font: &excelize.Font{
|
Bold: true,
|
},
|
Alignment: &excelize.Alignment{
|
Horizontal: "center",
|
},
|
}
|
return f.NewStyle(style)
|
}
|
|
func GetAllUnits() (allUnits []string, unitIndexMap map[string]int) {
|
units, err := models.NewUnitDictSearch().FindNotTotal()
|
allUnits = make([]string, 0, len(units))
|
unitIndexMap = make(map[string]int)
|
if err != nil {
|
return
|
}
|
|
for k, v := range units {
|
allUnits = append(allUnits, v.Name)
|
unitIndexMap[v.Name] = k
|
}
|
return
|
}
|
|
func SetExcelHeaders(headers []interface{}, f *excelize.File) (lastColumnNumber int, err error) {
|
// 设置表头
|
i := 1
|
for _, h := range headers {
|
if v, ok := h.(string); ok {
|
err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", v)
|
if err != nil {
|
return
|
}
|
i++
|
} else if childHeaders, ok := h.(map[string][]string); ok {
|
for title, list := range childHeaders {
|
err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", title)
|
if err != nil {
|
return
|
}
|
err = f.MergeCell("Sheet1", getColumnAlphabet(i)+"1", getColumnAlphabet(i-1+len(list))+"1") // 合并单元格
|
for _, t := range list {
|
err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"2", t)
|
if err != nil {
|
return
|
}
|
i++
|
}
|
}
|
} else {
|
return i, errors.New("unsupported header value")
|
}
|
}
|
return i, nil
|
}
|