zhangqian
2024-08-01 fc3313955a083c9480e4ea74398f72f9ba6addcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
}