From f72fb9ca31fbf27b4abbc0156e60ea162f58df81 Mon Sep 17 00:00:00 2001
From: dsmzx <dsmzx@123.com>
Date: 星期四, 13 六月 2024 15:30:50 +0800
Subject: [PATCH] 出入库调拨产品明细增加备注字段(同步产品的备注字段)
---
controllers/warehouse.go | 223 +++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 191 insertions(+), 32 deletions(-)
diff --git a/controllers/warehouse.go b/controllers/warehouse.go
index f4a297a..f9b54c9 100644
--- a/controllers/warehouse.go
+++ b/controllers/warehouse.go
@@ -6,6 +6,9 @@
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"gorm.io/gorm"
+ "strconv"
+ "strings"
+ "wms/constvar"
"wms/extend/code"
"wms/extend/util"
"wms/models"
@@ -39,7 +42,61 @@
util.ResponseFormat(c, code.RequestParamError, err.Error())
return
}
- if err := models.NewWarehouseSearch().Create(¶ms); err != nil {
+ //鍒涘缓瑙嗗浘
+ view := &models.Location{
+ Name: params.Code,
+ JointName: params.Code,
+ Type: constvar.LocationTypeView,
+ }
+ id, err := models.NewLocationSearch().CreateReturnId(view)
+ if err != nil {
+ util.ResponseFormat(c, code.SaveFail, "瑙嗗浘鍒涘缓澶辫触")
+ return
+ }
+ //鍒涘缓榛樿浣嶇疆
+ location := &models.Location{
+ Name: "榛樿浣嶇疆",
+ JointName: params.Code + "/榛樿浣嶇疆",
+ Type: constvar.LocationTypeInternal,
+ ReplenishLocation: true,
+ ParentId: id,
+ }
+ locationId, err := models.NewLocationSearch().CreateReturnId(location)
+ if err != nil {
+ util.ResponseFormat(c, code.SaveFail, "浣嶇疆鍒涘缓澶辫触")
+ return
+ }
+ params.LocationId = locationId
+ err = models.WithTransaction(func(tx *gorm.DB) error {
+ err := models.NewWarehouseSearch().SetOrm(tx).Create(¶ms)
+ if err != nil {
+ return err
+ }
+ //鍒涘缓涓変釜榛樿鎿嶄綔绫诲瀷
+ var types []*models.OperationType
+ inType := &models.OperationType{
+ Name: params.Name + "-鍏ュ簱",
+ BaseOperationType: constvar.BaseOperationTypeIncoming,
+ WarehouseId: params.Id,
+ }
+ types = append(types, inType)
+ outType := &models.OperationType{
+ Name: params.Name + "-鍑哄簱",
+ BaseOperationType: constvar.BaseOperationTypeOutgoing,
+ WarehouseId: params.Id,
+ }
+ types = append(types, outType)
+ internalType := &models.OperationType{
+ Name: params.Name + "-鍐呴儴璋冩嫧",
+ BaseOperationType: constvar.BaseOperationTypeInternal,
+ WarehouseId: params.Id,
+ }
+ types = append(types, internalType)
+ err = models.NewOperationTypeSearch().SetOrm(tx).CreateBatch(types)
+ return err
+ })
+ if err != nil {
+ _ = models.NewLocationSearch().SetID(locationId).Delete()
logx.Errorf("warehouse create err: %v", err)
util.ResponseFormat(c, code.SaveFail, "鎻掑叆澶辫触")
return
@@ -48,39 +105,51 @@
util.ResponseFormat(c, code.Success, "娣诲姞鎴愬姛")
}
-// Update
+// UpdateWarehouse
// @Tags 浠撳簱
// @Summary 缂栬緫浠撳簱
// @Produce application/json
-// @Param object body request.UpdateWarehouse true "浠撳簱淇℃伅"
-// @Param id path string true "浠撳簱id"
+// @Param object body models.Warehouse true "浠撳簱淇℃伅"
// @Success 200 {object} util.Response "鎴愬姛"
-// @Router /api-wms/v1/warehouse/warehouse/{id} [put]
-func (slf WarehouseController) Update(c *gin.Context) {
- id := cast.ToUint(c.Param("id"))
- if id == 0 {
- util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
- return
- }
- var (
- reqParams request.UpdateWarehouse
- params models.Warehouse
- )
- if err := c.BindJSON(&reqParams); err != nil {
+// @Router /api-wms/v1/warehouse/updateWarehouse [post]
+func (slf WarehouseController) UpdateWarehouse(c *gin.Context) {
+ var params models.Warehouse
+ if err := c.BindJSON(¶ms); err != nil {
util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("鍙傛暟瑙f瀽澶辫触: %v"+err.Error()))
return
}
- if err := structx.AssignTo(reqParams, ¶ms); err != nil {
- util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("鏁版嵁杞崲閿欒: %v", err.Error()))
- return
- }
- params.ID = id
if err := slf.ParamsCheck(params); err != nil {
util.ResponseFormat(c, code.RequestParamError, err.Error())
return
}
-
- err := models.NewWarehouseSearch().SetID(params.ID).Update(¶ms)
+ warehouse, err := models.NewWarehouseSearch().SetID(params.Id).First()
+ if err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "浠撳簱涓嶅瓨鍦�")
+ return
+ }
+ err = models.WithTransaction(func(db *gorm.DB) error {
+ //鏇存柊浣滀笟绫诲瀷鍚嶇О
+ if params.Name != warehouse.Name {
+ m := make(map[string]interface{})
+ m["name"] = params.Name + "-鍏ュ簱"
+ err := models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeIncoming).SetWarehouseId(params.Id).UpdateByMap(m)
+ if err != nil {
+ return err
+ }
+ m["name"] = params.Name + "-鍑哄簱"
+ err = models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeOutgoing).SetWarehouseId(params.Id).UpdateByMap(m)
+ if err != nil {
+ return err
+ }
+ m["name"] = params.Name + "-鍐呴儴璋冩嫧"
+ err = models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeInternal).SetWarehouseId(params.Id).UpdateByMap(m)
+ if err != nil {
+ return err
+ }
+ }
+ err = models.NewWarehouseSearch().SetID(params.Id).Update(¶ms)
+ return err
+ })
if err != nil {
util.ResponseFormat(c, code.RequestParamError, "淇敼澶辫触")
@@ -92,19 +161,49 @@
func (slf WarehouseController) ParamsCheck(params models.Warehouse) (err error) {
var oldRecord *models.Warehouse
- if params.ID != 0 {
- oldRecord, err = models.NewWarehouseSearch().SetID(params.ID).First()
+ if params.Id != 0 {
+ oldRecord, err = models.NewWarehouseSearch().SetID(params.Id).First()
if err == gorm.ErrRecordNotFound {
return errors.New("璁板綍涓嶅瓨鍦�")
}
}
- if oldRecord == nil || params.Code != oldRecord.Code {
- _, err = models.NewWarehouseSearch().SetCode(params.Code).First()
- if err != gorm.ErrRecordNotFound {
+ //鏇存柊浣嶇疆淇℃伅
+ if oldRecord != nil && params.Code != oldRecord.Code {
+ locations, err := models.NewLocationSearch().SetJointName(oldRecord.Code).FindNotTotal()
+ if err != nil {
+ return errors.New("鏌ヨ浣嶇疆淇℃伅澶辫触")
+ }
+
+ err = models.WithTransaction(func(db *gorm.DB) error {
+ for _, location := range locations {
+ m := make(map[string]interface{})
+ index := strings.Index(location.JointName, "/")
+ if index > 0 {
+ m["joint_name"] = params.Code + location.JointName[index:]
+ } else {
+ m["joint_name"] = params.Code
+ }
+ err = models.NewLocationSearch().SetOrm(db).SetID(location.Id).UpdateByMap(m)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+ if err != nil {
+ return errors.New("鏇存柊浣嶇疆淇℃伅澶辫触")
+ }
+ }
+ if oldRecord == nil {
+ record, err := models.NewWarehouseSearch().SetCode(params.Code).First()
+ if record != nil && record.Code == params.Code {
+ fmt.Println(err)
return errors.New("浠撳簱缂栧彿閲嶅")
}
}
-
+ if params.Code == "" {
+ return errors.New("缂╁啓涓嶈兘涓虹┖")
+ }
return nil
}
@@ -126,6 +225,24 @@
util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
return
}
+ //鑾峰彇浣嶇疆淇℃伅
+ ids := make([]int, 0)
+ for _, warehouse := range list {
+ ids = append(ids, warehouse.LocationId)
+ }
+ locations, err := models.NewLocationSearch().SetIds(ids).FindNotTotal()
+ if err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "浣嶇疆淇℃伅鏌ユ壘澶辫触")
+ return
+ }
+ for _, warehouse := range list {
+ for _, location := range locations {
+ if warehouse.LocationId == location.Id {
+ warehouse.WarehouseLocation = warehouse.Code + "/" + location.Name
+ break
+ }
+ }
+ }
util.ResponseFormatList(c, code.Success, list, cast.ToInt(total))
}
@@ -138,16 +255,58 @@
// @Success 200 {object} util.Response "鎴愬姛"
// @Router /api-wms/v1/warehouse/warehouse/{id} [delete]
func (slf WarehouseController) Delete(c *gin.Context) {
- id := cast.ToUint(c.Param("id"))
+ id, _ := strconv.Atoi(c.Param("id"))
if id == 0 {
util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
return
}
- err := models.NewWarehouseSearch().SetID(id).Delete()
+ first, err := models.NewWarehouseSearch().SetID(id).First()
if err != nil {
- util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎澶辫触")
+ util.ResponseFormat(c, code.RequestParamError, "鑾峰彇浠撳簱淇℃伅澶辫触")
return
}
+ err = models.WithTransaction(func(tx *gorm.DB) error {
+ //鍒犻櫎浣嶇疆淇℃伅
+ err = models.NewLocationSearch().SetOrm(tx).SetJointName(first.Code).Delete()
+ if err != nil {
+ return err
+ }
+ //鍒犻櫎鎿嶄綔绫诲瀷
+ err = models.NewOperationTypeSearch().SetOrm(tx).SetWarehouseId(first.Id).Delete()
+ if err != nil {
+ return err
+ }
+ //鍒犻櫎浠撳簱
+ err = models.NewWarehouseSearch().SetOrm(tx).SetID(id).Delete()
+ return err
+ })
+ if err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎浠撳簱澶辫触")
+ return
+ }
+
util.ResponseFormat(c, code.UpdateSuccess, "鍒犻櫎鎴愬姛")
}
+
+// GetWarehouseDetails
+// @Tags 浠撳簱
+// @Summary 鑾峰彇浠撳簱璇︽儏
+// @Produce application/json
+// @Param id path string true "浠撳簱id"
+// @Success 200 {object} util.Response{data=models.Warehouse} "鎴愬姛"
+// @Router /api-wms/v1/warehouse/getWarehouseDetails/{id} [get]
+func (slf WarehouseController) GetWarehouseDetails(c *gin.Context) {
+ id, _ := strconv.Atoi(c.Param("id"))
+ if id == 0 {
+ util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
+ return
+ }
+
+ first, err := models.NewWarehouseSearch().SetID(id).First()
+ if err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "鑾峰彇浠撳簱淇℃伅澶辫触")
+ return
+ }
+ util.ResponseFormat(c, code.UpdateSuccess, first)
+}
--
Gitblit v1.8.0