lishihai
2024-06-19 96236f07009f7138af765633fe3ac30c8ba88b94
产品-产品-Excel导入产品
2个文件已添加
4个文件已修改
242 ■■■■■ 已修改文件
controllers/product_controller.go 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attribute.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/material.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
response/Material.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/material.go 199 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/product_controller.go
@@ -18,6 +18,8 @@
    "wms/pkg/mysqlx"
    "wms/pkg/structx"
    "wms/request"
    "wms/response"
    "wms/service"
)
type ProductController struct {
@@ -874,3 +876,28 @@
    util.ResponseFormat(c, code.Success, "添加成功")
}
// InputProduct
//
//    @Tags        物料管理
//    @Summary    导入物料
//    @Produce    application/xlsx
//    @Success    200        {object}    util.Response     "成功"
//    @Router        /api-wms/v1/product/inputProduct [post]
func (slf ProductController) InputProduct(c *gin.Context) {
    file, _, err := c.Request.FormFile("file")
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    defer file.Close()
    resp := response.MaterialInputRes{InputCount: 0, ErrCount: 0, FileAddress: ""}
    userInfo := middleware.GetUserInfo(c)
    insertCount, err := service.InputMaterial(file, userInfo.Username)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    resp.InputCount = insertCount
    util.ResponseFormat(c, code.Success, resp)
}
models/attribute.go
@@ -108,6 +108,10 @@
    slf.Name = name
    return slf
}
func (slf *AttributeSearch) SetEntityType(entityType EntityType) *AttributeSearch {
    slf.EntityType = entityType
    return slf
}
func (slf *AttributeSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
@@ -119,6 +123,9 @@
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.EntityType != 0 {
        db = db.Where("entity_type = ?", slf.EntityType)
    }
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
models/material.go
@@ -96,6 +96,7 @@
        MinInventoryRule decimal.Decimal `json:"minInventoryRule" gorm:"-"` //最小库存
        MaxInventoryRule decimal.Decimal `json:"maxInventoryRule" gorm:"-"` //最大库存
        CreateBy string `gorm:"type:varchar(255);comment:导入人、创建人" json:"createBy"` //创建人
    }
    MaterialSearch struct {
response/Material.go
New file
@@ -0,0 +1,7 @@
package response
type MaterialInputRes struct {
    InputCount  int    `json:"inputCount"`
    ErrCount    int    `json:"errCount"`
    FileAddress string `json:"fileAddress"`
}
router/router.go
@@ -135,6 +135,7 @@
        productAPI.GET("getUserInfo", productController.GetUserInfo)       //获取登录用户信息
        productAPI.GET("getUnitInfo", productController.GetUnitInfo)       //获取单位信息
        productAPI.POST("saveUnitDict", productController.SaveUnitDict)    //更新计量单位字典
        productAPI.POST("inputProduct", productController.InputProduct)    //更新计量单位字典
    }
service/material.go
New file
@@ -0,0 +1,199 @@
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)
    for _, v := range existMaterials {
        mapExistMaterial[v.ID] = true
    }
    attributes, err := models.NewAttributeSearch().SetEntityType(models.EntityTypeProduct).FindNotTotal()
    if err != nil {
        return 0, err
    }
    for _, v := range attributes {
        mapAttribute[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:len(rows)]
    for index, insert := range inserts {
        errMsg := ""
        if len(insert) < 4 || insert[0] == "" || insert[1] == "" || insert[2] == "" || insert[3] == "" {
            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 len(insert) > 4 && insert[3] != "" {
            //errMsg = "第" + strconv.Itoa(index+1) + "行,产品编码:" + insert[0] + ",未填写单位"
            //return 0, errors.New(errMsg)
            material.Unit = insert[3]
        }
        var moreUnit = true
        if len(insert) > 5 && insert[4] != "" {
            material.MoreUnit = &moreUnit
            var ut models.UnitItems
            ut.Unit = insert[4]
            if len(insert) < 13 || insert[12] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位1相关的单位换算比例1"
                return 0, errors.New(errMsg)
            }
            ut.Amount = decimal.RequireFromString(insert[12])
            if len(insert) < 14 || insert[13] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位1相关的是否浮动换算1"
                return 0, errors.New(errMsg)
            }
            if strings.Trim(insert[13], " ") == "是" {
                ut.Floating = true
            } else {
                ut.Floating = false
            }
            material.MoreUnitList = append(material.MoreUnitList, ut)
        }
        if len(insert) > 6 && insert[5] != "" {
            material.MoreUnit = &moreUnit
            var ut models.UnitItems
            ut.Unit = insert[5]
            if len(insert) < 15 || insert[14] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位2相关的单位换算比例2"
                return 0, errors.New(errMsg)
            }
            ut.Amount = decimal.RequireFromString(insert[14])
            if len(insert) < 16 || insert[15] == "" {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",请填写与辅计量单位2相关的是否浮动换算2"
                return 0, errors.New(errMsg)
            }
            if strings.Trim(insert[15], " ") == "是" {
                ut.Floating = true
            } else {
                ut.Floating = false
            }
            material.MoreUnitList = append(material.MoreUnitList, ut)
        }
        if len(insert) > 7 && insert[6] != "" {
            material.Specs = insert[6] //规格
        }
        if len(insert) > 8 && insert[7] != "" {
            material.Type = insert[7] //型号
        }
        if len(insert) > 9 && insert[8] != "" {
            if mapAttribute[insert[8]] == 0 {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",未能识别材质属性,请先添加该属性"
                return 0, errors.New(errMsg)
            }
            //material.Quality = insert[8] //材质
            attributeValue1 := new(models.AttributeValue)
            attributeValue1.EntityID = material.ID
            attributeValue1.AttributeID = mapAttribute[insert[8]]
            attributeValue1.Value = insert[8]
            attributeValueList = append(attributeValueList, attributeValue1)
        }
        if len(insert) > 10 && insert[9] != "" {
            if mapAttribute[insert[9]] == 0 {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",未能识别品牌属性,请先添加该属性"
                return 0, errors.New(errMsg)
            }
            //material.Brand = insert[9]   //品牌
            attributeValue2 := new(models.AttributeValue)
            attributeValue2.EntityID = material.ID
            attributeValue2.AttributeID = mapAttribute[insert[9]]
            attributeValue2.Value = insert[9]
            attributeValueList = append(attributeValueList, attributeValue2)
        }
        if len(insert) > 11 && insert[10] != "" {
            if mapAttribute[insert[10]] == 0 {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",未能识别等级属性,请先添加该属性"
                return 0, errors.New(errMsg)
            }
            //material.Level = insert[10]  //等级
            attributeValue3 := new(models.AttributeValue)
            attributeValue3.EntityID = material.ID
            attributeValue3.AttributeID = mapAttribute[insert[10]]
            attributeValue3.Value = insert[10]
            attributeValueList = append(attributeValueList, attributeValue3)
        }
        if len(insert) > 12 && insert[11] != "" {
            if mapAttribute[insert[11]] == 0 {
                errMsg = "第" + strconv.Itoa(index+2) + "行,产品编码:" + insert[0] + ",未能识别庄口属性,请先添加该属性"
                return 0, errors.New(errMsg)
            }
            //material.From = insert[11]   //庄口
            attributeValue4 := new(models.AttributeValue)
            attributeValue4.EntityID = material.ID
            attributeValue4.AttributeID = mapAttribute[insert[11]]
            attributeValue4.Value = insert[11]
            attributeValueList = append(attributeValueList, attributeValue4)
        }
    }
    err = models.WithTransaction(func(db *gorm.DB) error {
        if err := models.NewMaterialSearch().CreateBatch(materialList); err != nil {
            return err
        }
        if err := models.NewAttributeValueSearch().CreateBatch(attributeValueList); err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        return 0, errors.New("导入失败")
    }
    return len(inserts), err
}