From 22a93375823a1fe58e5ca2dcd0545ddf1dfc883c Mon Sep 17 00:00:00 2001 From: jiangshuai <291802688@qq.com> Date: 星期二, 14 十一月 2023 11:47:56 +0800 Subject: [PATCH] 产品列表增加预测和出入库数量 --- controllers/product_controller.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 106 insertions(+), 2 deletions(-) diff --git a/controllers/product_controller.go b/controllers/product_controller.go index da77f6c..fa65e70 100644 --- a/controllers/product_controller.go +++ b/controllers/product_controller.go @@ -52,6 +52,19 @@ util.ResponseFormat(c, code.RequestParamError, "浜у搧淇℃伅淇濆瓨澶辫触") return } + + materialAttachmentList := []*models.MaterialAttachment{} + for _, v := range params.AttachmentIDs { + ma := &models.MaterialAttachment{MaterialID: params.ID, AttachmentID: v} + materialAttachmentList = append(materialAttachmentList, ma) + } + if len(materialAttachmentList) > 0 { + if err := models.NewMaterialAttachmentSearch().CreateBatch(materialAttachmentList); err != nil { + util.ResponseFormat(c, code.SaveFail, "闄勪欢淇濆瓨澶辫触") + return + } + } + util.ResponseFormat(c, code.Success, "淇濆瓨鎴愬姛") } @@ -72,7 +85,7 @@ if params.PageInfo.Check() { search.SetPage(params.Page, params.PageSize) } - products, total, err := search.SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find() + products, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触") return @@ -93,6 +106,46 @@ product.CategoryName = category.Name } } + var reorderAmount request.ProductStatisticsAmount + if err := models.NewOperationSearch().Orm. + Table("wms_operation_details"). + InnerJoins("INNER JOIN wms_operation on wms_operation_details.operation_id=wms_operation.id"). + Select("wms_operation_details.product_id,SUM(wms_operation_details.amount) as total_count"). + Where("wms_operation_details.product_id=? and wms_operation.`status`=? and wms_operation.base_operation_type in (?)", product.ID, constvar.OperationStatus_Ready, []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeDisuse}). + Group("wms_operation_details.product_id"). + //Order("wms_operation_details.product_id"). + First(&reorderAmount).Error; err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + util.ResponseFormat(c, code.RequestParamError, "浜у搧鏁版嵁缁熻澶辫触") + return + } else { + reorderAmount.TotalAmount = decimal.NewFromInt(0) + } + } + product.PredictionAmount = product.Amount.Add(reorderAmount.TotalAmount) + + var statisticsList []*request.ProductStatistics + if err := models.NewOperationSearch().Orm. + Table("wms_operation"). + InnerJoins("INNER JOIN wms_operation_details on wms_operation_details.operation_id=wms_operation.id"). + Select("SUM(wms_operation_details.amount) as total_amount,wms_operation.base_operation_type"). + Where("wms_operation_details.product_id=? and wms_operation.`status`=? and wms_operation.base_operation_type in (?)", product.ID, constvar.OperationStatus_Finish, []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeDisuse}). + Group("wms_operation.base_operation_type"). + Find(&statisticsList).Error; err != nil { + util.ResponseFormat(c, code.RequestParamError, "浜у搧鏁版嵁缁熻澶辫触") + return + } + for _, v := range statisticsList { + if v.BaseOperationType == constvar.BaseOperationTypeIncoming { + product.InputAmount = v.TotalAmount + } + if v.BaseOperationType == constvar.BaseOperationTypeOutgoing { + product.OutputAmount = product.OutputAmount.Add(v.TotalAmount) + } + if v.BaseOperationType == constvar.BaseOperationTypeAdjust { + product.OutputAmount = product.OutputAmount.Add(v.TotalAmount) + } + } } util.ResponseFormatList(c, code.Success, products, int(total)) } @@ -110,7 +163,7 @@ util.ResponseFormat(c, code.RequestParamError, "鏃犳晥id") return } - material, err := models.NewMaterialSearch().SetID(id).First() + material, err := models.NewMaterialSearch().SetID(id).SetPreload(true).First() if err != nil { util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触") return @@ -147,6 +200,22 @@ if err != nil { util.ResponseFormat(c, code.RequestParamError, "浜у搧淇℃伅鏇存柊澶辫触") return + } + + materialAttachmentList := []*models.MaterialAttachment{} + for _, v := range params.AttachmentIDs { + ma := &models.MaterialAttachment{MaterialID: params.ID, AttachmentID: v} + materialAttachmentList = append(materialAttachmentList, ma) + } + if err := models.NewMaterialAttachmentSearch().SetMaterialID(params.ID).Delete(); err != nil { + util.ResponseFormat(c, code.RequestParamError, "浜у搧闄勪欢娓呴櫎澶辫触") + return + } + if len(materialAttachmentList) > 0 { + if err := models.NewMaterialAttachmentSearch().CreateBatch(materialAttachmentList); err != nil { + util.ResponseFormat(c, code.RequestParamError, "浜у搧淇℃伅鏇存柊澶辫触") + return + } } util.ResponseFormat(c, code.Success, "鏇存柊鎴愬姛") } @@ -619,3 +688,38 @@ util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize) } + +// CancelDisuse +// +// @Tags 浜у搧 +// @Summary 鍙栨秷鎶ュ簾 +// @Produce application/json +// @Param id path int true "id" +// @Success 200 {object} util.Response "鎴愬姛" +// @Router /api-wms/v1/product/cancelDisuse/{id} [put] +func (slf ProductController) CancelDisuse(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 + } + operation.Status = constvar.OperationStatus_Cancel + if err := models.NewOperationSearch().SetID(operation.Id).Save(operation); err != nil { + util.ResponseFormat(c, code.SaveFail, err.Error()) + return + } + util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛") +} -- Gitblit v1.8.0