zhangqian
2024-07-29 eae158f8d95df5f49c4e36d5b9ad00b62dbad9ec
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
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 {
    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.Add(unitItem2.Amount),
                    Unit:     unitItem1.Unit,
                    Floating: unitItem1.Floating,
                })
            }
        }
    }
    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(amount decimal.Decimal, units []models.UnitItems, startIndex int, column int, f *excelize.File) {
    columnStr := strconv.Itoa(column)
    for _, v := range units {
        switch v.Unit {
        case "匹":
            f.SetCellValue("Sheet1", getColumnAlphabet(startIndex)+columnStr, v.Amount.Mul(amount))
        case "米":
            f.SetCellValue("Sheet1", getColumnAlphabet(startIndex+1)+columnStr, v.Amount.Mul(amount))
        case "重量":
            f.SetCellValue("Sheet1", getColumnAlphabet(startIndex+2)+columnStr, v.Amount.Mul(amount))
        }
    }
    return
}
 
func SetExcelHeader(headers []interface{}, f *excelize.File) (err error) {
    // 设置表头
    var i int
    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
                }
                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 err
                    }
                    i++
                }
            }
        } else {
            return errors.New("unsupported header value")
        }
    }
    return nil
}
 
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)
}