liujiandao
2023-09-20 548030097f2b95dff474c397c7393168c73ab8a2
controllers/product_controller.go
@@ -2,10 +2,12 @@
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 {
@@ -15,11 +17,11 @@
// @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(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
@@ -32,7 +34,16 @@
      util.ResponseFormat(c, code.RequestParamError, "产品售价不能小于等于零")
      return
   }
   err := models.NewProductSearch().Create(&params)
   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(&params)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "产品信息保存失败")
      return
@@ -45,7 +56,7 @@
// @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
@@ -53,7 +64,7 @@
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   search := models.NewProductSearch()
   search := models.NewMaterialSearch()
   if params.PageInfo.Check() {
      search.SetPage(params.Page, params.PageSize)
   }
@@ -70,9 +81,196 @@
// @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(&params); 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(&params)
   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(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if params.Name == "" {
      util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空")
      return
   }
   err := models.NewProductCategorySearch().Create(&params)
   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(&params); 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(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if params.Name == "" {
      util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空")
      return
   }
   err := models.NewProductCategorySearch().SetID(params.ID).Save(&params)
   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, "删除成功")
}