From ea02a2a9a8219d44b103f0a31cf9fc81ff8495e1 Mon Sep 17 00:00:00 2001 From: liujiandao <274878379@qq.com> Date: 星期六, 23 九月 2023 16:57:48 +0800 Subject: [PATCH] 产品列表添加类型名称 --- controllers/operation.go | 171 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 148 insertions(+), 23 deletions(-) diff --git a/controllers/operation.go b/controllers/operation.go index 581f085..32d16bb 100644 --- a/controllers/operation.go +++ b/controllers/operation.go @@ -2,10 +2,12 @@ import ( "errors" + "fmt" "github.com/gin-gonic/gin" - "github.com/spf13/cast" + "github.com/shopspring/decimal" "gorm.io/gorm" "strconv" + "wms/constvar" "wms/extend/code" "wms/extend/util" "wms/models" @@ -39,12 +41,54 @@ util.ResponseFormat(c, code.RequestParamError, err.Error()) return } + if err := slf.FormatLocation(¶ms); err != nil { + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } + + params.Status = constvar.OperationStatus_Ready if err := models.NewOperationSearch().Create(¶ms); err != nil { logx.Errorf("Operation create err: %v", err) util.ResponseFormat(c, code.SaveFail, "娣诲姞澶辫触锛�"+err.Error()) return } util.ResponseFormat(c, code.Success, "娣诲姞鎴愬姛") +} + +func (slf OperationController) FormatLocation(params *models.Operation) error { + operationType, err := models.NewOperationTypeSearch().SetID(uint(params.OperationTypeId)).First() + if err != nil { + return err + } + if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming { + if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil { + return err + } else { + params.FromLocationID = location.Id + } + if params.ToLocationID == 0 { + return errors.New("璇烽�夋嫨鐩爣浣嶇疆") + } + } + if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing { + if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeCustomer)).First(); err != nil { + return err + } else { + params.ToLocationID = location.Id + } + if params.FromLocationID == 0 { + return errors.New("璇烽�夋嫨婧愪綅缃�") + } + } + if operationType.BaseOperationType == constvar.BaseOperationTypeInternal { + if params.ToLocationID == 0 { + return errors.New("璇烽�夋嫨鐩爣浣嶇疆") + } + if params.FromLocationID == 0 { + return errors.New("璇烽�夋嫨婧愪綅缃�") + } + } + return nil } func (slf OperationController) CheckParams(params models.Operation) error { @@ -54,14 +98,6 @@ if params.OperationTypeId == 0 { return errors.New("operationTypeId涓�0") - } - - if params.FromLocationId == 0 { - return errors.New("璇烽�夋嫨婧愪綅缃�") - } - - if params.ToLocationId == 0 { - return errors.New("璇烽�夋嫨鐩爣浣嶇疆") } if params.OperationDate == "" { @@ -74,16 +110,13 @@ //妫�鏌ユ槑缁嗛儴鍒� for _, v := range params.Details { - if v.ProductId == 0 { - return errors.New("productID涓�0") + if v.ProductId == "" { + return errors.New("productID涓虹┖") } if v.ProductName == "" { return errors.New("浜у搧鍚嶇О寮傚父") } - if v.Quantity.IsNegative() { - return errors.New("浜у搧鏁伴噺鍑洪敊") - } - if v.FinishQuantity.IsNegative() { + if v.Amount.IsNegative() { return errors.New("浜у搧鏁伴噺鍑洪敊") } } @@ -136,19 +169,17 @@ // @Summary 淇敼鍏ュ簱/鍑哄簱淇℃伅 // @Produce application/json // @Param object body request.UpdateOperation true "鍏ュ簱淇℃伅" -// @Param id path int true "鍏ュ簱淇℃伅id" // @Success 200 {object} util.Response "鎴愬姛" -// @Router /api-wms/v1/operation/operation/{id} [put] +// @Router /api-wms/v1/operation/update [post] func (slf OperationController) Update(c *gin.Context) { - id := cast.ToUint(c.Param("id")) - if id == 0 { - util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id") - return - } var reqParams request.UpdateOperation var params models.Operation if err := c.BindJSON(&reqParams); err != nil { util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇細"+err.Error()) + return + } + if reqParams.Status != constvar.OperationStatus_Ready { + util.ResponseFormat(c, code.RequestParamError, "璇ュ叆搴撲俊鎭凡瀹屾垚锛屾棤娉曡繘琛屼慨鏀�") return } if err := structx.AssignTo(reqParams, ¶ms); err != nil { @@ -159,8 +190,16 @@ util.ResponseFormat(c, code.RequestParamError, err.Error()) return } + if err := slf.FormatLocation(¶ms); err != nil { + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } if err := models.WithTransaction(func(tx *gorm.DB) error { if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(params.Id).Delete(); err != nil { + return err + } + operationSearch := models.NewOperationSearch().SetOrm(tx) + if err := operationSearch.Orm.Model(¶ms).Association("Details").Replace(params.Details); err != nil { return err } if err := models.NewOperationSearch().SetOrm(tx).SetID(params.Id).Save(¶ms); err != nil { @@ -190,7 +229,16 @@ return } if id == 0 { - util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id") + util.ResponseFormat(c, code.RequestParamError, "id涓�0") + return + } + operation, err := models.NewOperationSearch().SetID(id).First() + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error()) + return + } + if operation.Status != constvar.OperationStatus_Ready { + util.ResponseFormat(c, code.RequestError, "璇ュ叆搴撲俊鎭棤娉曡繘琛屽垹闄�") return } if err := models.WithTransaction(func(tx *gorm.DB) error { @@ -207,3 +255,80 @@ } util.ResponseFormat(c, code.Success, "鍒犻櫎鎴愬姛") } + +// DeleteDevice +// +// @Tags 鍏ュ簱/鍑哄簱 +// @Summary 鏇存敼璁板綍鐘舵�� +// @Produce application/json +// @Param id path int true "id" +// @Success 200 {object} util.Response "鎴愬姛" +// @Router /api-wms/v1/operation/finish/{id} [put] +func (slf OperationController) Finish(c *gin.Context) { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "閿欒鐨刬d鍊�") + return + } + if id == 0 { + util.ResponseFormat(c, code.RequestParamError, "id涓�0") + return + } + operation, err := models.NewOperationSearch().SetID(id).First() + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error()) + return + } + if operation.Status != constvar.OperationStatus_Ready { + util.ResponseFormat(c, code.RequestError, "璇ュ嚭鍏ュ簱淇℃伅鏃犳硶瀹屾垚") + return + } + operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First() + if err != nil { + util.ResponseFormat(c, code.RequestError, err.Error()) + return + } + if err := models.WithTransaction(func(tx *gorm.DB) error { + + if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil { + return err + } + if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming { + if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil { + return err + } + } else if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing { + var listProdtId []string + var listProdt []*models.Material + mapProdt := make(map[string]decimal.Decimal) + listDetails, err := models.NewOperationDetailsSearch().SetOperationId(operation.Id).FindAll() + if err != nil { + return err + } + for _, v := range listDetails { + listProdtId = append(listProdtId, v.ProductId) + mapProdt[v.ProductId] = v.Amount + } + if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil { + return err + } + for _, v := range listProdt { + if value, ok := mapProdt[v.ID]; !ok { + return errors.New("浜у搧绉嶇被寮傚父") + } else { + if v.Amount.LessThan(value) { + return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,鍑哄簱锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愬嚭搴撴搷浣�", v.Name, v.Amount.String(), value.String())) + } + } + } + if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount - wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil { + return err + } + } + return nil + }); err != nil { + util.ResponseFormat(c, code.RequestError, err.Error()) + return + } + util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛") +} -- Gitblit v1.8.0