123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package admin
- import (
- "errors"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator"
- "lot_interlligentControl/app/e"
- "lot_interlligentControl/app/services"
- "lot_interlligentControl/app/services/imp"
- "lot_interlligentControl/models"
- "lot_interlligentControl/unity"
- "lot_interlligentControl/utils"
- "strconv"
- )
- var product services.Product = &imp.Product{}
- // AddProduct
- // @Tags 管理员
- // @Summary 添加产品类型
- // @Success 200 {object} e.R
- // @Fail 400 {object} e.R
- // @Param req body models.ProductDto true "请求参数"
- // @Accept application/json
- // @Router /admin/product [post]
- func AddProduct(c *gin.Context) {
- var produ models.ProductDto
- err := c.ShouldBindJSON(&produ)
- if err != nil {
- e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
- return
- }
- uid := utils.GetUId(c)
- rescode := product.AddProduct(uid, produ)
- if errors.Is(rescode, e.SUCCESS) {
- e.ResponseSuccess(c, "添加成功")
- return
- }
- e.ResponseError(c, rescode)
- }
- // UpdateProduct
- // @Tags 管理员
- // @Summary 修改产品类型
- // @Success 200 {object} e.R
- // @Fail 400 {object} e.R
- // @Param req body models.ProductDto true "请求参数"
- // @Param id query string true "id"
- // @Accept application/json
- // @Router /admin/product [put]
- func UpdateProduct(c *gin.Context) {
- id := c.Query("id")
- var produ models.ProductDto
- validate := validator.New()
- atoi, _ := strconv.Atoi(id)
- err := c.ShouldBindJSON(&produ)
- err = validate.Var(id, "required,numeric")
- if err != nil {
- e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
- return
- }
- if err != nil {
- e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
- return
- }
- uId := utils.GetUId(c)
- rescode := product.UpdateProduct(uId, atoi, produ)
- if errors.Is(rescode, e.SUCCESS) {
- e.ResponseSuccess(c, "修改成功")
- return
- }
- e.ResponseError(c, rescode)
- }
- // GetAllProduct
- // @Tags 管理员
- // @Summary 查询所有类型
- // @Success 200 {object} e.R
- // @Fail 400 {object} e.R
- // @Param req body unity.PageParams true "分页数据"
- // @Accept application/json
- // @Router /admin/productall [post]
- //func GetAllProduct(c *gin.Context) {
- // var pages unity.PageParams
- // err := c.ShouldBindJSON(&pages)
- // if err != nil {
- // e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
- // return
- // }
- // result, total, err := unity.Paginate(pages, models.Product{})
- // if err != nil {
- // e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
- // return
- // }
- // e.ResPonsePage(c, result, total, pages)
- //}
- // DeleteProductById
- // @Tags 管理员
- // @Summary 根据ID删除产品类型
- // @Success 200 {object} e.R
- // @Fail 400 {object} e.R
- // @Param id query string true "id"
- // @Accept application/json
- // @Router /admin/product [delete]
- func DeleteProductById(c *gin.Context) {
- id := c.Query("id")
- validate := validator.New()
- err := validate.Var(id, "required,numeric")
- if err != nil {
- e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
- return
- }
- atoi, _ := strconv.Atoi(id)
- _, rescode := unity.DeleteById(atoi, models.Product{})
- if rescode != nil {
- e.ResponseWithMsg(c, e.DeleteFail, e.DeleteFail.GetMsg())
- return
- }
- e.ResponseSuccess(c, "删除成功")
- }
|