package controllers import ( "github.com/gin-gonic/gin" "github.com/spf13/cast" "wms/extend/code" "wms/extend/util" "wms/models" "wms/request" "wms/utils" ) type ProductController struct { } // AddProduct // @Tags 产品 // @Summary 添加产品 // @Produce application/json // @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.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 } params.ID = utils.GetUUID() err := models.NewMaterialSearch().Create(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "产品信息保存失败") return } util.ResponseFormat(c, code.Success, "保存成功") } // GetProductList // @Tags 产品 // @Summary 获取产品列表 // @Produce application/json // @Param object body request.GetProductList true "查询参数" // @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 if err := c.BindJSON(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } 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() 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)) } // GetProductDetails // @Tags 产品 // @Summary 获取产品详情 // @Produce application/json // @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, "删除成功") }