package v1 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 shop services.Shop = &imp.Shop{} // AddShop // @Tags 店铺管理 // @Summary 添加节点 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body models.ShopDto true "店铺数据" // @Accept application/json // @Router /shop [post] func AddShop(c *gin.Context) { var shops models.ShopDto err := c.ShouldBindJSON(&shops) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } uid := utils.GetUId(c) rescode := shop.AddShop(uid, shops) if errors.Is(rescode, e.SUCCESS) { e.ResponseSuccess(c, "添加成功") return } e.ResponseError(c, rescode) } // DeleteShopById // @Tags 店铺管理 // @Summary 根据Id删除节点 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param id query string true "id" // @Accept application/json // @Router /shop [delete] func DeleteShopById(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.Shop{}) if rescode != nil { e.ResponseWithMsg(c, e.DeleteFail, e.DeleteFail.GetMsg()) return } e.ResponseSuccess(c, "删除成功") } // UpdateShop // @Tags 店铺管理 // @Summary 修改节点 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param id query string true "id" // @Param req body models.ShopDto true "店铺数据" // @Accept application/json // @Router /shop [put] func UpdateShop(c *gin.Context) { id := c.Query("id") var shops models.ShopDto validate := validator.New() atoi, _ := strconv.Atoi(id) err := c.ShouldBindJSON(&shops) 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 := shop.UpdateShop(uId, atoi, shops) if errors.Is(rescode, e.SUCCESS) { e.ResponseSuccess(c, "修改成功") return } e.ResponseError(c, rescode) } // GetShopById // @Tags 店铺管理 // @Summary 获取自己的商品信息 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body unity.PageParams true "分页数据" // @Accept application/json // @Router /shop [get] func GetShopById(c *gin.Context) { var pages unity.PageParams uId := utils.GetUId(c) err := c.ShouldBindJSON(&pages) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } result, total, err := unity.PaginateByID(uId, pages, models.Shop{}) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } e.ResPonsePage(c, result, total, pages) } // GetShopByName // @Tags 店铺管理 // @Summary 根据商品名称模糊查询 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body unity.PageParams true "分页数据" // @Param product_name query string true "product_name" // @Accept application/json // @Router /shopByName [get] func GetShopByName(c *gin.Context) { value := c.Query("product_name") var pages unity.PageParams validate := validator.New() err := validate.Var(value, "required") if err != nil { e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg()) return } err = c.ShouldBindJSON(&pages) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } uId := utils.GetUId(c) shops, total, err := shop.GetShopByName(uId, value, pages) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } e.ResPonsePage(c, shops, total, pages) } // GetAllProduct // @Tags 店铺管理 // @Summary 查询所有类型 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body unity.PageParams true "分页数据" // @Accept application/json // @Router /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) }