package handler

import (
	"fmt"
	"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"
)

func WrapFindAllProductHandler(ctx *gin.Context) {
	handler := newFindAllProductHandler(ctx)
	baseRun(handler)
}

func newFindAllProductHandler(ctx *gin.Context) *FindAllProductHandler {
	return &FindAllProductHandler{
		req:  http_model.NewFindAllProductRequest(),
		resp: http_model.NewFindAllProductResponse(),
		ctx:  ctx,
	}
}

type FindAllProductHandler struct {
	req  *http_model.FindAllProductRequest
	resp *http_model.CommonResponse
	ctx  *gin.Context
}

func (h *FindAllProductHandler) getRequest() interface{} {
	return h.req
}
func (h *FindAllProductHandler) getContext() *gin.Context {
	return h.ctx
}
func (h *FindAllProductHandler) getResponse() interface{} {
	return h.resp
}
func (h *FindAllProductHandler) run() {
	auth := middleware.GetSessionAuth(h.ctx)
	enterpriseID := auth.EnterpriseID
	fmt.Printf("企业ID %+v", enterpriseID)
	res, err := service.Product.FindAll(h.ctx, enterpriseID)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[FindAllProductHandler] call FindAll err:%+v\n", err)
		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
		log.Info("FindAllProduct fail,req:%+v", h.req)
		return
	}
	// h.resp.Message = "查询成功"
	h.resp.Data = res
}
func (h *FindAllProductHandler) checkParam() error {
	return nil
}
