| | |
| | | "wms/constvar" |
| | | "wms/extend/code" |
| | | "wms/extend/util" |
| | | "wms/middleware" |
| | | "wms/models" |
| | | "wms/pkg/mysqlx" |
| | | "wms/request" |
| | |
| | | // @Tags 库存盘点 |
| | | // @Summary 库存盘点列表 |
| | | // @Produce application/json |
| | | // @Param object body request.PageInfo true "查询参数" |
| | | // @Param object body request.GetList true "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/locationProductAmount/list [post] |
| | | func (slf LocationProductAmountController) List(c *gin.Context) { |
| | | var params request.PageInfo |
| | | var params request.GetList |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "参数异常") |
| | | return |
| | | } |
| | | productIds := make([]string, 0) |
| | | locationIds := make([]int, 0) |
| | | if params.Keyword != "" { |
| | | materials, _ := models.NewMaterialSearch().SetKeyword(params.Keyword).FindNotTotal() |
| | | for _, material := range materials { |
| | | productIds = append(productIds, material.ID) |
| | | } |
| | | locations, _ := models.NewLocationSearch().SetKeyword(params.Keyword).FindNotTotal() |
| | | for _, location := range locations { |
| | | locationIds = append(locationIds, location.Id) |
| | | } |
| | | } |
| | | |
| | | search := models.NewLocationProductAmountSearch() |
| | | //search.SetPage(params.Page, params.PageSize) |
| | |
| | | //wms_operation_details.amount as adjust_amount,wms_operation.id as operation_id,wms_operation.status |
| | | 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,material.name as product_name,wms_location_product_amount.amount,material.unit,wms_location_product_amount.create_date`). |
| | | Select(`wms_location_product_amount.id as location_product_amount_id,wms_location.id as location_id,wms_location.joint_name as location_name, |
| | | material.id as product_id,material.name as product_name,wms_location_product_amount.amount,material.unit, |
| | | wms_location_product_amount.create_date,material.weight`). |
| | | InnerJoins("inner join material on material.id=wms_location_product_amount.product_id"). |
| | | InnerJoins("inner join wms_location on wms_location.id=wms_location_product_amount.location_id") |
| | | |
| | |
| | | records = make([]*models.LocationProductAmountWithOperation, 0) |
| | | total int64 |
| | | ) |
| | | if len(productIds) > 0 { |
| | | search.Orm = search.Orm.Where("wms_location_product_amount.product_id in (?)", productIds) |
| | | |
| | | } |
| | | if len(locationIds) > 0 { |
| | | search.Orm = search.Orm.Where("wms_location_product_amount.location_id in (?)", locationIds) |
| | | } |
| | | |
| | | if err := search.Orm.Count(&total).Error; err != nil { |
| | | util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err)) |
| | |
| | | util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err)) |
| | | return |
| | | } |
| | | var operation models.Operation |
| | | |
| | | for k, v := range records { |
| | | if res := mysqlx.GetDB().Table("wms_operation").InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation.base_operation_type=? and wms_operation.status=? and wms_operation_details.to_location_id=? and wms_operation_details.product_id=?", constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready, v.LocationId, v.ProductId).First(&operation); res.Error != nil { |
| | | var operation models.Operation |
| | | db := mysqlx.GetDB() |
| | | if res := db.Table("wms_operation").Preload("Details").InnerJoins("inner join wms_operation_details "+ |
| | | "on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation.base_operation_type=? and "+ |
| | | "wms_operation.status=? and wms_operation_details.to_location_id=? and wms_operation_details.product_id=?", constvar.BaseOperationTypeAdjust, |
| | | constvar.OperationStatus_Ready, v.LocationId, v.ProductId).First(&operation); res.Error != nil { |
| | | if errors.Is(res.Error, gorm.ErrRecordNotFound) { |
| | | records[k].Status = constvar.OperationStatus_Finish |
| | | records[k].BaseOperationType = constvar.BaseOperationTypeAdjust |
| | |
| | | } |
| | | } else { |
| | | if len(operation.Details) > 0 { |
| | | records[k].AdjustAmount = operation.Details[0].Amount |
| | | records[k].DifferenceAmount = v.Amount.Add(v.AdjustAmount) |
| | | records[k].DifferenceAmount = operation.Details[0].Amount |
| | | records[k].AdjustAmount = v.DifferenceAmount.Sub(v.Amount) |
| | | } |
| | | records[k].OperationId = operation.Id |
| | | records[k].Status = operation.Status |
| | | } |
| | | records[k].BaseOperationType = constvar.BaseOperationTypeAdjust |
| | | } |
| | | |
| | | productIds = make([]string, 0) |
| | | for _, record := range records { |
| | | productIds = append(productIds, record.ProductId) |
| | | } |
| | | |
| | | products, err := models.NewMaterialSearch().SetIDs(productIds).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestError, "查询产品失败") |
| | | return |
| | | } |
| | | |
| | | productMap := models.MaterialMap(products) |
| | | |
| | | for k, v := range records { |
| | | product := productMap[v.ProductId] |
| | | if product == nil { |
| | | continue |
| | | } |
| | | |
| | | moreUnit := product.MoreUnit |
| | | if moreUnit != nil && *moreUnit { |
| | | records[k].AmountMoreUnits = service.CreateMoreUnit(v.Amount, product.MoreUnitList) |
| | | } |
| | | } |
| | | |
| | | util.ResponseFormatListWithPage(c, code.Success, records, int(total), params.Page, params.PageSize) |
| | | } |
| | | |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | |
| | | if err := AddLocationProductAmount(reqParams); err != nil { |
| | | userInfo := middleware.GetUserInfo(c) |
| | | if err := AddLocationProductAmount(reqParams, userInfo); err != nil { |
| | | util.ResponseFormat(c, code.RequestError, err) |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | } |
| | | |
| | | func AddLocationProductAmount(reqParams request.UpdateLocationProductAmount) error { |
| | | func AddLocationProductAmount(reqParams request.UpdateLocationProductAmount, userInfo *request.CustomClaims) error { |
| | | if reqParams.LocationId == 0 { |
| | | return errors.New("参数异常,位置id信息不存在") |
| | | } |
| | |
| | | if err := models.NewOperationSearch().Orm. |
| | | Table("wms_operation"). |
| | | InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id"). |
| | | Where("wms_operation.location_id=? and wms_operation_details.product_id=? and wms_operation.base_operation_type=? and wms_operation.status=?", reqParams.LocationId, reqParams.ProductId, constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready). |
| | | Where("wms_operation_details.to_location_id=? and wms_operation_details.product_id=? and wms_operation.base_operation_type=? and wms_operation.status=?", reqParams.LocationId, reqParams.ProductId, constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready). |
| | | Count(&existCount).Error; err != nil { |
| | | return err |
| | | } |
| | |
| | | if err != nil { |
| | | return err |
| | | } |
| | | reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount) |
| | | var locAmount models.LocationProductAmount |
| | | if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) { |
| | | locAmount = models.LocationProductAmount{ |
| | |
| | | |
| | | detail := &models.OperationDetails{ |
| | | ProductId: reqParams.ProductId, |
| | | Amount: reqParams.AdjustAmount, |
| | | Amount: reqParams.DifferenceAmount, |
| | | FromLocationID: location.Id, |
| | | ToLocationID: reqParams.LocationId, |
| | | } |
| | |
| | | Comment: "库存盘点", |
| | | BaseOperationType: constvar.BaseOperationTypeAdjust, |
| | | Details: []*models.OperationDetails{detail}, |
| | | LocationID: location.Id, |
| | | OperationTypeName: "库存调整", |
| | | CreatedBy: userInfo.Username, |
| | | } |
| | | |
| | | if err := models.WithTransaction(func(tx *gorm.DB) error { |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | userInfo := middleware.GetUserInfo(c) |
| | | if reqParams.OperationId == 0 { |
| | | if err := AddLocationProductAmount(reqParams); err != nil { |
| | | if err := AddLocationProductAmount(reqParams, userInfo); err != nil { |
| | | util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | return |
| | | } |
| | | reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount) |
| | | operation, err := models.NewOperationSearch().SetID(reqParams.OperationId).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "未找到相关数据") |
| | | return |
| | | } |
| | | if operation.Status != constvar.OperationStatus_Ready { |
| | | util.ResponseFormat(c, code.RequestParamError, "该记录已验证通过,无法进行修改") |
| | | util.ResponseFormat(c, code.RequestParamError, "该记录已被验证,请刷新页面查看") |
| | | return |
| | | } |
| | | |
| | | if err := models.NewOperationDetailsSearch().SetOperationId(reqParams.OperationId).SetProductId(reqParams.ProductId).Update(&models.OperationDetails{Amount: reqParams.AdjustAmount}); err != nil { |
| | | if err := models.NewOperationDetailsSearch().SetOperationId(reqParams.OperationId).SetProductId(reqParams.ProductId).Update(&models.OperationDetails{Amount: reqParams.DifferenceAmount}); err != nil { |
| | | util.ResponseFormat(c, code.RequestError, "修改失败:"+err.Error()) |
| | | return |
| | | } |
| | |
| | | util.ResponseFormat(c, code.Success, "修改成功") |
| | | } |
| | | |
| | | // finish |
| | | // Finish |
| | | // |
| | | // @Tags 库存盘点 |
| | | // @Summary 应用、验证 |
| | |
| | | return |
| | | } |
| | | operationSearch := models.NewOperationSearch().SetPreload(true) |
| | | operationSearch.SetOrm(operationSearch.Orm.InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation_details.product_id=? and wms_operation.id=?", reqParams.ProductId, reqParams.OperationId)) |
| | | operationSearch.SetOrm(operationSearch.Orm.InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id="+ |
| | | "wms_operation.id").Where("wms_operation_details.product_id=? and wms_operation.id=?", reqParams.ProductId, reqParams.OperationId)) |
| | | operation, err := operationSearch.First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "未找到相关信息:"+err.Error()) |
| | |
| | | util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | return |
| | | } |
| | | locProdtAmount, err := models.NewLocationProductAmountSearch().SetID(reqParams.LocationProductAmountId).First() |
| | | locProductAmount, err := models.NewLocationProductAmountSearch().SetID(reqParams.LocationProductAmountId).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | return |
| | | } |
| | | adjustAmount := operation.Details[0].Amount.Sub(locProductAmount.Amount) |
| | | userInfo := middleware.GetUserInfo(c) |
| | | if err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | if err := models.NewOperationSearch().SetOrm(tx).SetID(reqParams.OperationId).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil { |
| | | if err := models.NewOperationSearch().SetOrm(tx).SetID(reqParams.OperationId).Update(&models.Operation{ |
| | | Status: constvar.OperationStatus_Finish, CheckedBy: userInfo.Username, |
| | | AuditDate: time.Now().Format("2006-01-02 15:04:05")}); err != nil { |
| | | return err |
| | | } |
| | | if err := models.NewMaterialSearch().SetOrm(tx).SetID(reqParams.ProductId).Update(&models.Material{Amount: material.Amount.Add(operation.Details[0].Amount)}); err != nil { |
| | | if err := models.NewMaterialSearch().SetOrm(tx).SetID(reqParams.ProductId).Update(&models.Material{Amount: material.Amount.Add(adjustAmount)}); err != nil { |
| | | return err |
| | | } |
| | | if err := models.NewLocationProductAmountSearch().SetOrm(tx).SetID(reqParams.LocationProductAmountId).Update(&models.LocationProductAmount{Amount: locProdtAmount.Amount.Add(operation.Details[0].Amount)}); err != nil { |
| | | if err := models.NewLocationProductAmountSearch().SetOrm(tx).SetID(reqParams.LocationProductAmountId).Update(&models.LocationProductAmount{Amount: operation.Details[0].Amount}); err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | |
| | | } |
| | | util.ResponseFormat(c, code.Success, "操作成功") |
| | | } |
| | | |
| | | // GetLocationProductAmount |
| | | // |
| | | // @Tags 库存盘点 |
| | | // @Summary 获取位置产品信息 |
| | | // @Produce application/json |
| | | // @Param object body request.GetRuleList true "入参" |
| | | // @Success 200 {object} util.Response{data=models.LocationProductAmount} "成功" |
| | | // @Router /api-wms/v1/locationProductAmount/getLocationProductAmount [post] |
| | | func (slf LocationProductAmountController) GetLocationProductAmount(c *gin.Context) { |
| | | var params request.GetRuleList |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if params.LocationId == 0 || params.ProductId == "" { |
| | | util.ResponseFormat(c, code.Success, nil) |
| | | return |
| | | } |
| | | first, err := models.NewLocationProductAmountSearch().SetLocationId(params.LocationId).SetProductId(params.ProductId).First() |
| | | if err != nil { |
| | | if err == gorm.ErrRecordNotFound { |
| | | util.ResponseFormat(c, code.Success, nil) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, first) |
| | | } |