package handler

import (
	"youngee_b_api/consts"
	"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"
)

func WrapFindProductHandler(ctx *gin.Context) {
	handler := newFindProductHandler(ctx)
	baseRun(handler)
}

func newFindProductHandler(ctx *gin.Context) *FindProductHandler {
	return &FindProductHandler{
		req:  http_model.NewFindProductRequest(),
		resp: http_model.NewFindProductResponse(),
		ctx:  ctx,
	}
}

type FindProductHandler struct {
	req  *http_model.FindProductRequest
	resp *http_model.CommonResponse
	ctx  *gin.Context
}

func (h *FindProductHandler) getRequest() interface{} {
	return h.req
}
func (h *FindProductHandler) getContext() *gin.Context {
	return h.ctx
}
func (h *FindProductHandler) getResponse() interface{} {
	return h.resp
}
func (h *FindProductHandler) run() {
	data := *&http_model.FindProductRequest{}
	data = *h.req
	res, err := service.Product.FindByID(h.ctx, data.ProductID)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[FindProductHandler] call FindByID err:%+v\n", err)
		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
		log.Info("FindProduct fail,req:%+v", h.req)
		return
	}
	h.resp.Data = res
}
func (h *FindProductHandler) checkParam() error {
	return nil
}
