123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package handler
- import (
- "youngee_b_api/consts"
- "youngee_b_api/middleware"
- "youngee_b_api/model/http_model"
- "youngee_b_api/service"
- "youngee_b_api/util"
- "github.com/gin-gonic/gin"
- "github.com/sirupsen/logrus"
- log "github.com/sirupsen/logrus"
- )
- // WrapCreateProductHandler
- // @BasePath /youngee/m/
- // SendCode godoc
- // @Summary CreateProduct 创建商品
- // @Schemes
- // @Description 企业创建商品,添加到商品库
- // @Accept json
- // @Produce json
- // @Param Authorization header string true "登录TOKEN信息"
- // @Param req body http_model.CreateProductRequest true "创建商品请求结构体"
- // @Success 200 {object} http_model.CommonResponse{data=http_model.CreateProductData} "创建商品相应结构体"
- // @Router /product/create [post]
- func WrapCreateProductHandler(ctx *gin.Context) {
- handler := newCreateProductHandler(ctx)
- baseRun(handler)
- }
- func newCreateProductHandler(ctx *gin.Context) *CreateProductHandler {
- return &CreateProductHandler{
- req: http_model.NewCreateProductRequest(),
- resp: http_model.NewCreateProductResponse(),
- ctx: ctx,
- }
- }
- type CreateProductHandler struct {
- req *http_model.CreateProductRequest
- resp *http_model.CommonResponse
- ctx *gin.Context
- }
- func (h *CreateProductHandler) getRequest() interface{} {
- return h.req
- }
- func (h *CreateProductHandler) getContext() *gin.Context {
- return h.ctx
- }
- func (h *CreateProductHandler) getResponse() interface{} {
- return h.resp
- }
- func (h *CreateProductHandler) run() {
- data := http_model.CreateProductRequest{}
- data = *h.req
- auth := middleware.GetSessionAuth(h.ctx)
- enterpriseID := auth.EnterpriseID
- //根据品牌名和商品名查询商品是否存在,若存在则更新,否则新增
- product, err := service.Product.FindByID(h.ctx, data.ProductId)
- if err != nil {
- logrus.Errorf("[CreateProductHandler] call FindByID err:%+v\n", err)
- util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
- log.Info("CreateProduct fail,req:%+v", h.req)
- return
- } else {
- if product != nil {
- // 该商品存在,更新
- data.ProductId = product.ProductID
- res, err := service.Product.Update(h.ctx, data, enterpriseID)
- if err != nil {
- logrus.Errorf("[CreateProductHandler] call Update err:%+v\n", err)
- util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
- log.Info("CreateProduct fail,req:%+v", h.req)
- return
- }
- h.resp.Message = "成功更新商品信息"
- h.resp.Data = res
- } else {
- // 商品不存在,新增
- res, err := service.Product.Create(h.ctx, data, enterpriseID)
- if err != nil {
- logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
- util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
- log.Info("CreateProduct fail,req:%+v", h.req)
- return
- }
- h.resp.Message = "成功新增商品信息"
- h.resp.Data = res
- }
- }
- }
- func (h *CreateProductHandler) checkParam() error {
- return nil
- }
|