| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "strconv" |
| | | "time" |
| | | "wms/constvar" |
| | | "wms/extend/code" |
| | | "wms/extend/util" |
| | | "wms/models" |
| | | "wms/pkg/logx" |
| | | "wms/request" |
| | | "wms/utils" |
| | | ) |
| | | |
| | | type ProductController struct { |
| | |
| | | // @Tags 产品 |
| | | // @Summary 添加产品 |
| | | // @Produce application/json |
| | | // @Param object body models.Product true "产品信息" |
| | | // @Param object body models.Material true "产品信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/addProduct [post] |
| | | func (slf ProductController) AddProduct(c *gin.Context) { |
| | | var params models.Product |
| | | var params models.Material |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "产品售价不能小于等于零") |
| | | return |
| | | } |
| | | err := models.NewProductSearch().Create(¶ms) |
| | | if params.Model == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "物料类型不能为空") |
| | | return |
| | | } |
| | | if params.Unit == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "单位不能为空") |
| | | return |
| | | } |
| | | params.ID = utils.GetUUID() |
| | | err := models.NewMaterialSearch().Create(¶ms) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品信息保存失败") |
| | | return |
| | |
| | | // @Summary 获取产品列表 |
| | | // @Produce application/json |
| | | // @Param object body request.GetProductList true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Product} "成功" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductList [post] |
| | | func (slf ProductController) GetProductList(c *gin.Context) { |
| | | var params request.GetProductList |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | search := models.NewProductSearch() |
| | | search := models.NewMaterialSearch() |
| | | if params.PageInfo.Check() { |
| | | search.SetPage(params.Page, params.PageSize) |
| | | } |
| | | products, total, err := search.SetKeyword(params.KeyWord).SetOrder("created_at desc").Find() |
| | | products, total, err := search.SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | |
| | | ids := make([]int, 0) |
| | | for _, product := range products { |
| | | ids = append(ids, product.CategoryId) |
| | | } |
| | | categories, err := models.NewProductCategorySearch().SetIds(ids).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型查找失败") |
| | | return |
| | | } |
| | | for _, product := range products { |
| | | for _, category := range categories { |
| | | if product.CategoryId == int(category.ID) { |
| | | product.CategoryName = category.Name |
| | | } |
| | | } |
| | | } |
| | | util.ResponseFormatList(c, code.Success, products, int(total)) |
| | | } |
| | | |
| | |
| | | // @Tags 产品 |
| | | // @Summary 获取产品详情 |
| | | // @Produce application/json |
| | | // @Param object body request.GetProductList true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Product} "成功" |
| | | // @Router /api-wms/v1/product/getProductList [post] |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response{data=models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductDetails/{id} [get] |
| | | func (slf ProductController) GetProductDetails(c *gin.Context) { |
| | | id := c.Param("id") |
| | | if id == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | material, err := models.NewMaterialSearch().SetID(id).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, material) |
| | | } |
| | | |
| | | // UpdateProduct |
| | | // @Tags 产品 |
| | | // @Summary 修改产品 |
| | | // @Produce application/json |
| | | // @Param object body models.Material true "产品信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/updateProduct [post] |
| | | func (slf ProductController) UpdateProduct(c *gin.Context) { |
| | | var params models.Material |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if params.Name == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品名称不能为空") |
| | | return |
| | | } |
| | | if params.SalePrice.IntPart() <= 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品售价不能小于等于零") |
| | | return |
| | | } |
| | | if params.Model == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "物料类型不能为空") |
| | | return |
| | | } |
| | | if params.Unit == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "单位不能为空") |
| | | return |
| | | } |
| | | err := models.NewMaterialSearch().SetID(params.ID).Save(¶ms) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "更新成功") |
| | | } |
| | | |
| | | // DeleteProduct |
| | | // @Tags 产品 |
| | | // @Summary 删除产品 |
| | | // @Produce application/json |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/deleteProduct/{id} [delete] |
| | | func (slf ProductController) DeleteProduct(c *gin.Context) { |
| | | id := c.Param("id") |
| | | if id == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | err := models.NewMaterialSearch().SetID(id).Delete() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "删除成功") |
| | | } |
| | | |
| | | // AddProductCategory |
| | | // @Tags 产品类型 |
| | | // @Summary 添加产品类型 |
| | | // @Produce application/json |
| | | // @Param object body models.ProductCategory true "产品类型信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/addProductCategory [post] |
| | | func (slf ProductController) AddProductCategory(c *gin.Context) { |
| | | var params models.ProductCategory |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if params.Name == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空") |
| | | return |
| | | } |
| | | err := models.NewProductCategorySearch().Create(¶ms) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型信息保存失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "保存成功") |
| | | } |
| | | |
| | | // GetProductCategoryList |
| | | // @Tags 产品类型 |
| | | // @Summary 获取产品类型列表 |
| | | // @Produce application/json |
| | | // @Param object body request.GetProductList true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.ProductCategory} "成功" |
| | | // @Router /api-wms/v1/product/getProductCategoryList [post] |
| | | func (slf ProductController) GetProductCategoryList(c *gin.Context) { |
| | | var params request.GetProductList |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | search := models.NewProductCategorySearch() |
| | | if params.PageInfo.Check() { |
| | | search.SetPage(params.Page, params.PageSize) |
| | | } |
| | | list, total, err := search.SetKeyword(params.KeyWord).SetOrder("created_at desc").Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormatList(c, code.Success, list, int(total)) |
| | | } |
| | | |
| | | // GetProductCategoryDetails |
| | | // @Tags 产品类型 |
| | | // @Summary 获取产品类型详情 |
| | | // @Produce application/json |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response{data=models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductCategoryDetails/{id} [get] |
| | | func (slf ProductController) GetProductCategoryDetails(c *gin.Context) { |
| | | id := c.Param("id") |
| | | if id == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | |
| | | first, err := models.NewProductCategorySearch().SetID(cast.ToUint(id)).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, first) |
| | | } |
| | | |
| | | // UpdateProductCategory |
| | | // @Tags 产品类型 |
| | | // @Summary 修改产品类型 |
| | | // @Produce application/json |
| | | // @Param object body models.ProductCategory true "产品信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/updateProductCategory [post] |
| | | func (slf ProductController) UpdateProductCategory(c *gin.Context) { |
| | | var params models.ProductCategory |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if params.Name == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空") |
| | | return |
| | | } |
| | | err := models.NewProductCategorySearch().SetID(params.ID).Save(¶ms) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型信息更新失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "更新成功") |
| | | } |
| | | |
| | | // DeleteProductCategory |
| | | // @Tags 产品类型 |
| | | // @Summary 删除产品类型 |
| | | // @Produce application/json |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/deleteProductCategory/{id} [delete] |
| | | func (slf ProductController) DeleteProductCategory(c *gin.Context) { |
| | | id := c.Param("id") |
| | | if id == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | err := models.NewProductCategorySearch().SetID(cast.ToUint(id)).Delete() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "删除成功") |
| | | } |
| | | |
| | | // ListOperation |
| | | // @Tags 产品 |
| | | // @Summary 产品历史出入库信息 |
| | | // @Produce application/json |
| | | // @Param object body request.QueryOperationList true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Operation} "成功" |
| | | // @Router /api-wms/v1/product/listOperaton [post] |
| | | func (slf ProductController) ListOperation(c *gin.Context) { |
| | | var params request.QueryOperationList |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | | } |
| | | if !params.PageInfo.Check() { |
| | | util.ResponseFormat(c, code.RequestParamError, "页码信息错误") |
| | | return |
| | | } |
| | | |
| | | search := models.NewOperationSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetOrder("created_at desc") |
| | | search.SetOrm(search.Orm.InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation_details.product_id=?", params.ProductId)) |
| | | |
| | | list, total, err := search.Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error()) |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize) |
| | | } |
| | | |
| | | // AddDisuse |
| | | // @Tags 产品 |
| | | // @Summary 添加报废信息 |
| | | // @Produce application/json |
| | | // @Param object body request.AddDisuse true "入库/出库信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/addDisuse [post] |
| | | func (slf ProductController) AddDisuse(c *gin.Context) { |
| | | var params request.AddDisuse |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | | } |
| | | if params.Amount.LessThanOrEqual(decimal.NewFromInt(0)) { |
| | | util.ResponseFormat(c, code.RequestParamError, "数量异常") |
| | | return |
| | | } |
| | | if params.FromLocationId == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "请选择源位置") |
| | | return |
| | | } |
| | | if params.ToLocationId == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "请选择报废位置") |
| | | return |
| | | } |
| | | if params.SourceNumber == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "请输入源单据") |
| | | return |
| | | } |
| | | detail := &models.OperationDetails{ |
| | | ProductId: params.ProductId, |
| | | Amount: params.Amount, |
| | | } |
| | | operation := models.Operation{ |
| | | Number: strconv.FormatInt(time.Now().Unix(), 10), |
| | | SourceNumber: params.SourceNumber, |
| | | OperationTypeId: 0, |
| | | Status: constvar.OperationStatus_Ready, |
| | | FromLocationID: params.FromLocationId, |
| | | ToLocationID: params.ToLocationId, |
| | | OperationDate: time.Now().Format("2006-01-02 15:04:05"), |
| | | Details: []*models.OperationDetails{detail}, |
| | | BaseOperationType: constvar.BaseOperationTypeDisuse, |
| | | } |
| | | if err := models.NewOperationSearch().Create(&operation); err != nil { |
| | | logx.Errorf("Operation create err: %v", err) |
| | | util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | } |
| | | |
| | | // ListDisuse |
| | | // @Tags 产品 |
| | | // @Summary 报废列表 |
| | | // @Produce application/json |
| | | // @Param object body request.QueryDisuseList true "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/listDisuse [post] |
| | | func (slf ProductController) ListDisuse(c *gin.Context) { |
| | | var params request.QueryDisuseList |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | | } |
| | | if !params.PageInfo.Check() { |
| | | util.ResponseFormat(c, code.RequestParamError, "数据分页信息错误") |
| | | return |
| | | } |
| | | |
| | | //db := models.NewOperationSearch().Orm.Table("wms_operation").Select("wms_operation.id,wms_operation.number,wms_operation.source_number,wms_operation.status,wms_operation.from_location_id,wms_operation.to_location_id,wms_operation.operation_date,wms_operation.contacter_id,wms_operation.contacter_name,wms_operation.company_id,wms_operation.company_name,wms_operation.comment,wms_operation_details.product_id,wms_operation_details.product_name,wms_operation_details.unit,wms_operation_details.amount").InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id") |
| | | |
| | | db := models.NewOperationSearch().Orm.Table("wms_operation").Select("wms_operation.id,wms_operation.number,wms_operation.source_number,wms_operation.status,wms_operation.from_location_id,wms_operation.to_location_id,wms_operation.operation_date,wms_operation.contacter_id,wms_operation.contacter_name,wms_operation.company_id,wms_operation.company_name,wms_operation.comment,wms_operation_details.product_id,material.name as product_name,material.unit,wms_operation_details.amount").InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").InnerJoins("inner join material on material.id=wms_operation_details.product_id") |
| | | |
| | | if params.Number != "" { |
| | | db = db.Where("wms_operation.number like ? or wms_operation.source_number like ? or material.name like ?", fmt.Sprintf("%%%v%%", params.Number), fmt.Sprintf("%%%v%%", params.Number), fmt.Sprintf("%%%v%%", params.Number)) |
| | | } |
| | | db = db.Where("wms_operation.operation_type_id=?", 0) |
| | | var ( |
| | | records = make([]*models.ResponseDisuseList, 0) |
| | | total int64 |
| | | ) |
| | | |
| | | //list, total, err := search.SetDisuse(true).SetPreload(true).SetOrder("created_at desc").Find() |
| | | |
| | | if err := db.Count(&total).Error; err != nil { |
| | | util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err)) |
| | | return |
| | | } |
| | | db = db.Preload("ToLocation").Preload("FromLocation") |
| | | if params.Page*params.PageSize > 0 { |
| | | db = db.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize) |
| | | } |
| | | if err := db.Order("wms_operation.created_at desc").Find(&records).Error; err != nil { |
| | | util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err)) |
| | | return |
| | | } |
| | | util.ResponseFormatListWithPage(c, code.Success, records, int(total), params.Page, params.PageSize) |
| | | } |
| | | |
| | | // FinishDisuse |
| | | // |
| | | // @Tags 产品 |
| | | // @Summary 验证报废 |
| | | // @Produce application/json |
| | | // @Param id path int true "id" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/finishDisuse/{id} [put] |
| | | func (slf ProductController) FinishDisuse(c *gin.Context) { |
| | | id, err := strconv.Atoi(c.Param("id")) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "错误的id值") |
| | | 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 |
| | | } |
| | | 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 |
| | | } |
| | | 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 k, 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())) |
| | | } |
| | | listProdt[k].Amount = listProdt[k].Amount.Sub(value) |
| | | if err := tx.Save(listProdt[k]).Error; err != nil { |
| | | return err |
| | | } |
| | | var locAmount models.LocationProductAmount |
| | | if err := models.NewLocationProductAmountSearch().Orm. |
| | | Table("wms_location_product_amount"). |
| | | Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id"). |
| | | Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.FromLocationID). |
| | | First(&locAmount).Error; err != nil { |
| | | return err |
| | | } |
| | | if locAmount.Amount.LessThan(value) { |
| | | return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String())) |
| | | } |
| | | locAmount.Amount = locAmount.Amount.Sub(value) |
| | | if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(&locAmount); err != nil { |
| | | return err |
| | | } |
| | | } |
| | | } |
| | | return nil |
| | | }); err != nil { |
| | | util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "操作成功") |
| | | } |
| | | |
| | | // UpdateDisuse |
| | | // @Tags 产品 |
| | | // @Summary 修改报废信息 |
| | | // @Produce application/json |
| | | // @Param object body request.UpdateDisuse true "入库/出库信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/updateDisuse [post] |
| | | func (slf ProductController) UpdateDisuse(c *gin.Context) { |
| | | var params request.UpdateDisuse |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | | } |
| | | if params.Amount.LessThanOrEqual(decimal.NewFromInt(0)) { |
| | | util.ResponseFormat(c, code.RequestParamError, "数量异常") |
| | | return |
| | | } |
| | | if params.FromLocationId == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "请选择源位置") |
| | | return |
| | | } |
| | | if params.ToLocationId == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "请选择报废位置") |
| | | return |
| | | } |
| | | if params.SourceNumber == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "请输入源单据") |
| | | return |
| | | } |
| | | if params.Status != constvar.OperationStatus_Ready { |
| | | util.ResponseFormat(c, code.RequestParamError, "该信息无法修改") |
| | | return |
| | | } |
| | | detail := &models.OperationDetails{ |
| | | ProductId: params.ProductId, |
| | | //ProductName: params.ProductName, |
| | | Amount: params.Amount, |
| | | //Unit: params.Unit, |
| | | } |
| | | operation := models.Operation{ |
| | | Id: params.Id, |
| | | Number: params.Number, |
| | | SourceNumber: params.SourceNumber, |
| | | OperationTypeId: 0, |
| | | Status: params.Status, |
| | | FromLocationID: params.FromLocationId, |
| | | ToLocationID: params.ToLocationId, |
| | | OperationDate: params.OperationDate, |
| | | Details: []*models.OperationDetails{detail}, |
| | | } |
| | | 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(&operation).Association("Details").Replace(operation.Details); err != nil { |
| | | return err |
| | | } |
| | | if err := models.NewOperationSearch().SetOrm(tx).SetID(params.Id).Save(&operation); err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "修改失败:"+err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | } |
| | | |
| | | // ListHistory |
| | | // @Tags 产品 |
| | | // @Summary 产品位置历史信息 |
| | | // @Produce application/json |
| | | // @Param object body request.QueryOperationHistory true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Operation} "成功" |
| | | // @Router /api-wms/v1/product/listHistory [post] |
| | | func (slf ProductController) ListHistory(c *gin.Context) { |
| | | var params request.QueryOperationHistory |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error()) |
| | | return |
| | | } |
| | | if !params.PageInfo.Check() { |
| | | util.ResponseFormat(c, code.RequestParamError, "页码信息错误") |
| | | return |
| | | } |
| | | |
| | | search := models.NewOperationSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetOrder("created_at desc") |
| | | search.SetOrm(search.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.from_location_id=? or wms_operation.to_location_id=?)", params.ProductId, params.LocationId, params.LocationId)) |
| | | |
| | | list, total, err := search.Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error()) |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize) |
| | | } |