| | |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "io" |
| | | "net/url" |
| | | "strconv" |
| | | "time" |
| | | "wms/constvar" |
| | |
| | | "wms/request" |
| | | "wms/response" |
| | | "wms/service" |
| | | "wms/utils/http" |
| | | ) |
| | | |
| | | type ProductController struct { |
| | |
| | | if params.Unit == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "单位不能为空") |
| | | return |
| | | } |
| | | if params.BarCode != "" { |
| | | m, err := models.NewMaterialSearch().SetBarCode(params.BarCode).First() |
| | | if err == nil && m.ID != "" { //查出物料表是物料已存在 |
| | | util.ResponseFormat(c, code.RequestParamError, "条形码已经被使用") |
| | | return |
| | | } |
| | | } |
| | | //params.ID = utils.GetUUID() |
| | | |
| | |
| | | // @Summary 获取产品列表 |
| | | // @Produce application/json |
| | | // @Param object body request.GetProductList true "查询参数" |
| | | // @Param Authorization header string true "token" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductList [post] |
| | | func (slf ProductController) GetProductList(c *gin.Context) { |
| | |
| | | if params.PageInfo.Check() { |
| | | search.SetPage(params.Page, params.PageSize) |
| | | } |
| | | products, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetCategoryId(params.CategoryId).SetOrder("created_at desc").Find() |
| | | products, total, err := search.SetPreload(true). |
| | | SetKeyword(params.KeyWord). |
| | | SetCategoryId(params.CategoryId). |
| | | SetCategoryIds(params.CategoryIds). |
| | | SetOrder("created_at desc"). |
| | | Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "产品类型查找失败") |
| | | return |
| | | } |
| | | categoryMap := models.CategoryMap(categories) |
| | | for _, product := range products { |
| | | for _, category := range categories { |
| | | if product.CategoryId == int(category.ID) { |
| | | product.CategoryName = category.Name |
| | | } |
| | | if product.CategoryId != 0 && categoryMap[product.CategoryId] != nil { |
| | | product.CategoryName = categoryMap[product.CategoryId].Name |
| | | } |
| | | var reorderAmount request.ProductStatisticsAmount |
| | | if err := models.NewOperationSearch().Orm. |
| | | Table("wms_operation_details"). |
| | | var totalAmount decimal.Decimal |
| | | db := models.NewOperationSearch().Orm |
| | | if err := db.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"). |
| | | Select("IFNULL(SUM(wms_operation_details.amount), 0) 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 { |
| | | Scan(&totalAmount).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) |
| | | product.PredictionAmount = product.Amount.Add(totalAmount) |
| | | |
| | | var statisticsList []*request.ProductStatistics |
| | | if err := models.NewOperationSearch().Orm.Table("wms_operation"). |
| | | db2 := models.NewOperationSearch().Orm |
| | | if err := db2.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}). |
| | |
| | | |
| | | // GetProductDetails |
| | | // @Tags 产品 |
| | | // @Summary 获取产品详情 |
| | | // @Summary 通过产品/商品/物料 ID获取产品详情 |
| | | // @Produce application/json |
| | | // @Param Authorization header string true "token" |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response{data=models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductDetails/{id} [get] |
| | |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | attributeValues, err := models.NewAttributeValueSearch().SetEntityID(material.ID).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | aids := make([]uint, 0) |
| | | for _, v := range attributeValues { |
| | | aids = append(aids, v.AttributeID) |
| | | } |
| | | attributes, err := models.NewAttributeSearch().SetIDs(aids).FindNotTotal() |
| | | attributesMap := make(map[uint]*models.Attribute, len(attributes)) |
| | | for _, v := range attributes { |
| | | attributesMap[v.ID] = v |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "未知动态属性") |
| | | } |
| | | for _, v1 := range attributeValues { |
| | | attribute := attributesMap[v1.AttributeID] |
| | | if attribute == nil { |
| | | continue |
| | | } |
| | | //product.Attributes = append(product.Attributes,v1) |
| | | material.Attributes = append(material.Attributes, models.Attribute{ |
| | | Model: gorm.Model{ID: v1.ID, CreatedAt: v1.CreatedAt, UpdatedAt: v1.UpdatedAt, DeletedAt: v1.DeletedAt}, |
| | | Name: attribute.Name, |
| | | DataType: attribute.DataType, |
| | | EntityType: attribute.EntityType, |
| | | SelectValues: attribute.SelectValues, |
| | | SelectValue: attribute.SelectValue, |
| | | //Value: v1.Value, |
| | | Value: v1.Value, |
| | | }) |
| | | } |
| | | util.ResponseFormat(c, code.Success, material) |
| | | } |
| | | |
| | | // GetProductDetailsByBarCode |
| | | // @Tags 产品 |
| | | // @Summary 通过产品/商品/物料 条形码 获取产品详情 |
| | | // @Produce application/json |
| | | // @Param Authorization header string true "token" |
| | | // @Param barCode path string true "barCode" "查询参数" |
| | | // @Success 200 {object} util.Response{data=models.Material} "成功" |
| | | // @Router /api-wms/v1/product/getProductDetailsByBarCode/{barCode} [get] |
| | | func (slf ProductController) GetProductDetailsByBarCode(c *gin.Context) { |
| | | barCode := c.Param("barCode") |
| | | if barCode == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效条形码") |
| | | return |
| | | } |
| | | material, err := models.NewMaterialSearch().SetBarCode(barCode).SetPreload(true).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | attributeValues, err := models.NewAttributeValueSearch().SetEntityID(material.ID).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | aids := make([]uint, 0) |
| | | for _, v := range attributeValues { |
| | | aids = append(aids, v.AttributeID) |
| | | } |
| | | attributes, err := models.NewAttributeSearch().SetIDs(aids).FindNotTotal() |
| | | attributesMap := make(map[uint]*models.Attribute, len(attributes)) |
| | | for _, v := range attributes { |
| | | attributesMap[v.ID] = v |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "未知动态属性") |
| | | } |
| | | for _, v1 := range attributeValues { |
| | | attribute := attributesMap[v1.AttributeID] |
| | | if attribute == nil { |
| | | continue |
| | | } |
| | | //product.Attributes = append(product.Attributes,v1) |
| | | material.Attributes = append(material.Attributes, models.Attribute{ |
| | | Model: gorm.Model{ID: v1.ID, CreatedAt: v1.CreatedAt, UpdatedAt: v1.UpdatedAt, DeletedAt: v1.DeletedAt}, |
| | | Name: attribute.Name, |
| | | DataType: attribute.DataType, |
| | | EntityType: attribute.EntityType, |
| | | SelectValues: attribute.SelectValues, |
| | | SelectValue: attribute.SelectValue, |
| | | //Value: v1.Value, |
| | | Value: v1.Value, |
| | | }) |
| | | } |
| | | util.ResponseFormat(c, code.Success, material) |
| | | } |
| | |
| | | if params.Unit == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "单位不能为空") |
| | | return |
| | | } |
| | | if params.BarCode != "" { |
| | | m, err := models.NewMaterialSearch().SetBarCode(params.BarCode).First() |
| | | if err == nil && m.ID != params.ID { //查出物料且ID不一样, |
| | | util.ResponseFormat(c, code.RequestParamError, "条形码已经被使用") |
| | | return |
| | | } |
| | | } |
| | | err := models.NewMaterialSearch().SetID(params.ID).Save(¶ms) |
| | | if err != nil { |
| | |
| | | |
| | | // DeleteProduct |
| | | // @Tags 产品 |
| | | // @Summary 删除产品 |
| | | // @Summary 通过产品/商品/物料 ID删除产品 |
| | | // @Produce application/json |
| | | // @Param id path string true "id" "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | err := models.NewMaterialSearch().SetID(id).Delete() |
| | | err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | if err := models.NewMaterialSearch().SetOrm(tx).SetID(id).Delete(); err != nil { |
| | | return err |
| | | } |
| | | if err := models.NewAttributeValueSearch().SetOrm(tx).SetEntityID(id).Delete(); err != nil { //删除物料对应的动态属性 |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "删除成功") |
| | | } |
| | | |
| | | // DeleteProductByBarCode |
| | | // @Tags 产品 |
| | | // @Summary 通过产品/商品/物料 条形码删除产品 |
| | | // @Produce application/json |
| | | // @Param barCode path string true "barCode" "查询参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/deleteProductByBarCode/{barCode} [delete] |
| | | func (slf ProductController) DeleteProductByBarCode(c *gin.Context) { |
| | | barCode := c.Param("barCode") |
| | | if barCode == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | err := models.NewMaterialSearch().SetBarCode(barCode).Delete() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | return |
| | |
| | | return errors.New(fmt.Sprintf("产品:%v,库存:%v,报废:%v,数量不够,无法完成报废操作", v.Product.Name, v.Product.Amount.String(), v.Amount.String())) |
| | | } |
| | | listDetails[k].Product.Amount = listDetails[k].Product.Amount.Sub(v.Amount) |
| | | listDetails[k].Product.StockMoreUnitList = service.SubMoreUnit(listDetails[k].Product.StockMoreUnitList, v.MoreUnitList) |
| | | if err := tx.Save(&listDetails[k].Product).Error; err != nil { |
| | | return err |
| | | } |
| | |
| | | v.Sort = i + 1 |
| | | } |
| | | |
| | | //校验重复 |
| | | m := make(map[string]struct{}) |
| | | for _, v := range params { |
| | | if _, ok := m[v.Name]; ok { |
| | | util.ResponseFormat(c, code.RequestParamError, "单位重复:"+v.Name) |
| | | return |
| | | } |
| | | m[v.Name] = struct{}{} |
| | | } |
| | | |
| | | err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | err := models.NewUnitDictSearch().SetOrm(tx).Delete() |
| | | if err != nil { |
| | |
| | | return nil |
| | | }) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | util.ResponseFormat(c, code.RequestParamError, "保存失败") |
| | | return |
| | | } |
| | | |
| | |
| | | |
| | | // InputProduct |
| | | // |
| | | // @Tags 物料管理 |
| | | // @Summary 导入物料 |
| | | // @Tags 产品 |
| | | // @Summary 导入物料/产品 |
| | | // |
| | | // @Accept multipart/form-data |
| | | // @Param file formData file true "file" |
| | | // @Param Authorization header string true "token" |
| | | // |
| | | // @Produce application/xlsx |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/inputProduct [post] |
| | |
| | | resp.InputCount = insertCount |
| | | util.ResponseFormat(c, code.Success, resp) |
| | | } |
| | | |
| | | // DownloadInputFormat |
| | | // |
| | | // @Tags 产品 |
| | | // @Summary 下载导入物料/产品模板 |
| | | // |
| | | // @Param Authorization header string true "token" |
| | | // |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/product/downloadInputFormat [get] |
| | | func (slf ProductController) DownloadInputFormat(c *gin.Context) { |
| | | template, err := models.NewFileTemplateAttachmentSearch().SetCategory(constvar.FileWarehouseCategory_JialianInput3).First() //物料导入模板 |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.NoTemplateError, "获取模版记录失败:"+err.Error()) |
| | | return |
| | | } |
| | | readerCloser, err := http.HttpGetWithReadCloser(template.FileUrl) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.NoTemplateError, "获取模版记录失败:"+err.Error()) |
| | | return |
| | | } |
| | | |
| | | fileContentDisposition := "attachment;filename=\"" + url.QueryEscape("物料导入.xlsx") + "\"" |
| | | w := c.Writer |
| | | w.Header().Set("Content-Type", "application/octet-stream") |
| | | w.Header().Set("Content-Disposition", fileContentDisposition) |
| | | w.Header().Set("Content-Transfer-Encoding", "binary") |
| | | w.Header().Set("Cache-Control", "no-cache") |
| | | _, err = io.Copy(w, readerCloser) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.NoTemplateError, "下载失败:"+err.Error()) |
| | | return |
| | | } |
| | | w.Flush() |
| | | _ = readerCloser.Close() |
| | | util.ResponseFormat(c, code.Success, "") |
| | | } |