From 96236f07009f7138af765633fe3ac30c8ba88b94 Mon Sep 17 00:00:00 2001
From: lishihai <dslsh@dscom>
Date: 星期三, 19 六月 2024 14:26:11 +0800
Subject: [PATCH] 产品-产品-Excel导入产品
---
controllers/product_controller.go | 27 +++++
service/material.go | 199 +++++++++++++++++++++++++++++++++++++++
models/attribute.go | 7 +
models/material.go | 1
response/Material.go | 7 +
router/router.go | 1
6 files changed, 242 insertions(+), 0 deletions(-)
diff --git a/controllers/product_controller.go b/controllers/product_controller.go
index 3e9ea0c..cb0e7fc 100644
--- a/controllers/product_controller.go
+++ b/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)
+}
diff --git a/models/attribute.go b/models/attribute.go
index f3b0d13..31e6d90 100644
--- a/models/attribute.go
+++ b/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)
diff --git a/models/material.go b/models/material.go
index 031fc32..5350fc2 100644
--- a/models/material.go
+++ b/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 {
diff --git a/response/Material.go b/response/Material.go
new file mode 100644
index 0000000..cf16384
--- /dev/null
+++ b/response/Material.go
@@ -0,0 +1,7 @@
+package response
+
+type MaterialInputRes struct {
+ InputCount int `json:"inputCount"`
+ ErrCount int `json:"errCount"`
+ FileAddress string `json:"fileAddress"`
+}
diff --git a/router/router.go b/router/router.go
index e50c74c..9376dd6 100644
--- a/router/router.go
+++ b/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) //鏇存柊璁¢噺鍗曚綅瀛楀吀
}
diff --git a/service/material.go b/service/material.go
new file mode 100644
index 0000000..ea42769
--- /dev/null
+++ b/service/material.go
@@ -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] + "锛屾湭鑳借瘑鍒簞鍙e睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
+ 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
+}
--
Gitblit v1.8.0