zhangqian
2024-07-23 bb455e9304722ad83fd65203c3cd370f5f3844c3
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package service
 
import (
    "errors"
    "github.com/shopspring/decimal"
    "github.com/xuri/excelize/v2"
    "gorm.io/gorm"
    "mime/multipart"
    "strconv"
    "strings"
    "wms/constvar"
    "wms/models"
)
 
func InputMaterial(file multipart.File, username string) (insertCount int, err error) {
    existMaterials, err := models.NewMaterialSearch().FindNotTotal()
    if err != nil {
        return 0, err
    }
    mapExistMaterial := make(map[string]bool)
    mapAttribute := make(map[string]uint)
    mapProductCategory := make(map[string]int)
    for _, v := range existMaterials {
        mapExistMaterial[v.ID] = true
    }
    attributes, err := models.NewAttributeSearch().SetEntityType(models.EntityTypeProduct).FindNotTotal()
    if err != nil {
        return 0, err
    }
    productCategory, err := models.NewProductCategorySearch().FindNotTotal()
    if err != nil {
        return 0, err
    }
    for _, v := range attributes {
        mapAttribute[v.Name] = v.ID
    }
    for _, v := range productCategory {
        mapProductCategory[v.Name] = v.Id
    }
    f, err := excelize.OpenReader(file)
    if err != nil {
        return 0, err
    }
    defer f.Close()
    var materialList []*models.Material
    var attributeValueList []*models.AttributeValue
 
    rows, err := f.GetRows("Sheet1")
    if err != nil {
        return 0, err
    }
    if len(rows) <= 1 {
        return 0, errors.New("改文件没有数据内容")
    }
 
    inserts := rows[1:]
    headers := rows[0]
    attributesColumns := make([]int, 0)
    for i, header := range headers {
        if mapAttribute[header] != 0 {
            attributesColumns = append(attributesColumns, i)
        }
    }
 
    for index, insert := range inserts {
        errMsg := ""
        if len(insert) < 6 || insert[0] == "" || insert[1] == "" || insert[2] == "" || insert[3] == "" || insert[4] == "" || insert[5] == "" {
            errMsg = "第" + strconv.Itoa(index+2) + "行,没有填写必填项项"
            return 0, errors.New(errMsg)
        }
        if mapExistMaterial[strings.Trim(insert[0], " ")] {
            errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",已经存在"
            return 0, errors.New(errMsg)
        }
        material := new(models.Material)
        material.CreateBy = username
        material.ID = strings.Trim(insert[0], " ")
        material.Name = insert[1]
        switch insert[2] {
        case string(constvar.MaterialModeRaw):
            material.Model = constvar.MaterialModeRaw
        case string(constvar.MaterialModeSemi):
            material.Model = constvar.MaterialModeSemi
        case string(constvar.MaterialModeFinished):
            material.Model = constvar.MaterialModeFinished
        case string(constvar.MaterialModeAuxiliary):
            material.Model = constvar.MaterialModeAuxiliary
        case string(constvar.MaterialModeConsumables):
            material.Model = constvar.MaterialModeConsumables
        case string(constvar.MaterialModeOther):
            material.Model = constvar.MaterialModeOther
        default:
            errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",无法识别物料类型"
            return 0, errors.New(errMsg)
        }
        if mapProductCategory[strings.Trim(insert[3], " ")] == 0 {
            errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",无法识别该产品类别"
            return 0, errors.New(errMsg)
        }
        material.CategoryId = mapProductCategory[strings.Trim(insert[3], " ")]
        purchaseTypeList := make([]int, 0)
        purchaseTypes := strings.Split(strings.Trim(insert[4], " "), "/")
        for _, v := range purchaseTypes {
            switch strings.Trim(v, " ") {
            case "采购":
                purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeOutSource))
            case "自制":
                purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeSelf))
            case "委外":
                purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeEntrust))
            default:
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",无法识别采购类型"
                return 0, errors.New(errMsg)
            }
 
        }
        material.PurchaseTypeList = purchaseTypeList
        material.Unit = insert[5]
 
        var moreUnit = true
        if len(insert) > 7 && insert[6] != "" {
            material.MoreUnit = &moreUnit
            var ut models.UnitItems
            ut.Unit = insert[6]
            if len(insert) < 14 || insert[13] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位1相关的单位换算比例1"
                return 0, errors.New(errMsg)
            }
            ut.Amount = decimal.RequireFromString(insert[13])
            if len(insert) < 15 || insert[14] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位1相关的是否浮动换算1"
                return 0, errors.New(errMsg)
            }
            if strings.Trim(insert[14], " ") == "是" {
                ut.Floating = true
            } else {
                ut.Floating = false
            }
            material.MoreUnitList = append(material.MoreUnitList, ut)
        }
        if len(insert) > 8 && insert[7] != "" {
            material.MoreUnit = &moreUnit
            var ut models.UnitItems
            ut.Unit = insert[7]
            if len(insert) < 16 || insert[15] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位2相关的单位换算比例2"
                return 0, errors.New(errMsg)
            }
            ut.Amount = decimal.RequireFromString(insert[15])
            if len(insert) < 17 || insert[16] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位2相关的是否浮动换算2"
                return 0, errors.New(errMsg)
            }
            if strings.Trim(insert[16], " ") == "是" {
                ut.Floating = true
            } else {
                ut.Floating = false
            }
            material.MoreUnitList = append(material.MoreUnitList, ut)
        }
        if len(insert) > 9 && insert[8] != "" {
            material.Specs = insert[8] //规格
        }
        if len(insert) > 10 && insert[9] != "" {
            material.Type = insert[9] //型号
        }
 
        materialList = append(materialList, material)
 
        //保存动态属性
        for _, attributesColumn := range attributesColumns {
            if len(insert) > attributesColumn {
                attributeValueList = append(attributeValueList, &models.AttributeValue{
                    EntityID: material.ID,
                    //AttributeID: mapAttribute[insert[attributesColumn]],
                    AttributeID: mapAttribute[headers[attributesColumn]],
                    Value:       insert[attributesColumn],
                })
            }
        }
 
    }
    if len(materialList) == 0 {
        return 0, errors.New("物料数据为空")
    }
    err = models.WithTransaction(func(db *gorm.DB) error {
        for i := 0; i < len(materialList); i += 500 {
            end := i + 500
            if end > len(materialList) {
                end = len(materialList)
            }
            arr := materialList[i:end]
            if len(arr) != 0 {
                if err := models.NewMaterialSearch().SetOrm(db).CreateBatch(arr); err != nil {
                    return err
                }
            }
        }
        for i := 0; i < len(attributeValueList); i += 500 {
            end := i + 500
            if end > len(attributeValueList) {
                end = len(attributeValueList)
            }
            arr := attributeValueList[i:end]
            if len(arr) != 0 {
                if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(arr); err != nil {
                    return err
                }
                if err != nil { //批量插入失败(可能已经存在值),分开一条一条插入重复的不插入
                    for _, avl := range arr {
                        first, err2 := models.NewAttributeValueSearch().SetAttributeID(avl.AttributeID).SetEntityID(avl.EntityID).First()
                        if err2 == nil && first != nil { //表示已经存在动态属性
                            continue
                        }
                        err2 = models.NewAttributeValueSearch().SetOrm(db).Create(avl)
                        if err2 != nil {
                            return err
                        }
                    }
                }
            }
        }
 
        return nil
    })
 
    if err != nil {
        return 0, errors.New("导入失败:" + err.Error())
    }
    return len(inserts), err
}