From 2716487d13f3521adeb25a48aa07a89f885d32c7 Mon Sep 17 00:00:00 2001 From: liujiandao <274878379@qq.com> Date: 星期一, 09 十月 2023 09:36:08 +0800 Subject: [PATCH] 仓库功能修改 --- controllers/location_product_amount.go | 51 ++++++++++++++++++++++++++++----------------------- 1 files changed, 28 insertions(+), 23 deletions(-) diff --git a/controllers/location_product_amount.go b/controllers/location_product_amount.go index 0e909f3..8d44349 100644 --- a/controllers/location_product_amount.go +++ b/controllers/location_product_amount.go @@ -42,6 +42,7 @@ search.Orm = search.Orm. Table("wms_location_product_amount"). Select(` + wms_location_product_amount.id as location_product_amount_id, wms_location.id as location_id, wms_location.name as location_name, material.id as product_id, @@ -50,14 +51,13 @@ material.unit, wms_location_product_amount.create_date, wms_operation_details.amount as adjust_amount, - wms_operation.id as operation_id`). + wms_operation.id as operation_id, + wms_operation.status`). InnerJoins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id"). + InnerJoins("inner join material on material.id=wms_location_product.product_id"). InnerJoins("inner join wms_location on wms_location.id=wms_location_product.location_id"). - InnerJoins("inner join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id"). - InnerJoins("inner join material on material.id=wms_operation_details.product_id"). - InnerJoins("inner join wms_operation on wms_operation.id=wms_operation_details.operation_id"). - Where("wms_operation.base_operation_type=?", constvar.BaseOperationTypeAdjust) - + Joins("left join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id"). + Joins("left join wms_operation on wms_operation.id=wms_operation_details.operation_id and wms_operation.base_operation_type=? and wms_operation.status=?", constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready) var ( records = make([]*models.LocationProductAmountWithOperation, 0) total int64 @@ -71,12 +71,14 @@ if params.Page*params.PageSize > 0 { search.Orm = search.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize) } - if err := search.Orm.Find(&records).Error; err != nil { + if err := search.Orm.Order("wms_operation.created_at desc").Find(&records).Error; err != nil { util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err)) return } for k, v := range records { - records[k].DifferenceAmount = v.Amount.Sub(v.AdjustAmount) + if v.OperationId != 0 { + records[k].DifferenceAmount = v.Amount.Add(v.AdjustAmount) + } } util.ResponseFormatListWithPage(c, code.Success, records, int(total), params.Page, params.PageSize) } @@ -122,6 +124,11 @@ util.ResponseFormat(c, code.RequestError, "涓嶅瓨鍦ㄨ涓婃灦瑙勫垯锛屾棤娉曡繘琛屽簱瀛樼洏鐐�") return } + existAmount, err := models.NewLocationProductAmountSearch().SetLocationProductId(locProduct.Id).First() + if err != nil { + util.ResponseFormat(c, code.RequestError, err.Error()) + return + } location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeAdjust)).First() if err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) @@ -129,11 +136,15 @@ } //reqParams.AdjustAmount = reqParams.Amount.Sub(reqParams.DifferenceAmount) reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount) - locAmount := models.LocationProductAmount{ - LocationProductId: locProduct.Id, - Amount: decimal.NewFromInt(0), - CreateDate: time.Now().Format("2006-01-02 15:04:05"), + var locAmount models.LocationProductAmount + if existAmount.Id == 0 { + locAmount = models.LocationProductAmount{ + LocationProductId: locProduct.Id, + Amount: decimal.NewFromInt(0), + CreateDate: time.Now().Format("2006-01-02 15:04:05"), + } } + detail := &models.OperationDetails{ ProductId: reqParams.ProductId, Amount: reqParams.AdjustAmount, @@ -149,20 +160,14 @@ ToLocationID: reqParams.LocationId, } - //if reqParams.AdjustAmount.GreaterThanOrEqual(decimal.NewFromInt(0)) { - // operation.FromLocationID = Location.Id - // operation.ToLocationID = reqParams.LocationId - //} else { - // operation.FromLocationID = reqParams.LocationId - // operation.ToLocationID = Location.Id - //} - if err := models.WithTransaction(func(tx *gorm.DB) error { if err := models.NewOperationSearch().SetOrm(tx).Create(&operation); err != nil { return err } - if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil { - return err + if existAmount.Id == 0 { + if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil { + return err + } } return nil }); err != nil { @@ -199,7 +204,7 @@ util.ResponseFormat(c, code.Success, ruleList) } -// Add +// Update // @Tags 搴撳瓨鐩樼偣 // @Summary 淇敼搴撳瓨鐩樼偣淇℃伅 // @Produce application/json -- Gitblit v1.8.0