package handler

import (
	"youngee_b_api/consts"
	"youngee_b_api/db"
	"youngee_b_api/middleware"
	"youngee_b_api/model/http_model"
	"youngee_b_api/util"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func WrapGetCreatingNumberHandler(ctx *gin.Context) {
	handler := newGetCreatingNumberHandler(ctx)
	baseRun(handler)
}

type GetCreatingNumberHandler struct {
	ctx  *gin.Context
	req  *http_model.GetCreatingNumberRequest
	resp *http_model.CommonResponse
}

func (g GetCreatingNumberHandler) getContext() *gin.Context {
	return g.ctx
}

func (g GetCreatingNumberHandler) getResponse() interface{} {
	return g.resp
}

func (g GetCreatingNumberHandler) getRequest() interface{} {
	return g.req
}

func (g GetCreatingNumberHandler) run() {
	auth := middleware.GetSessionAuth(g.ctx)
	enterpriseID := auth.EnterpriseID
	data, err := db.GetCreatingNumber(g.ctx, enterpriseID)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[GetCreatingNumberHandler] call GetCreatingNumber err:%+v\n", err)
		util.HandlerPackErrorResp(g.resp, consts.ErrorInternal, "")
		logrus.Info("GetCreatingNumber fail,req:%+v", g.req)
		return
	}
	g.resp.Data = data
}

func (g GetCreatingNumberHandler) checkParam() error {
	return nil
}

func newGetCreatingNumberHandler(ctx *gin.Context) *GetCreatingNumberHandler {
	return &GetCreatingNumberHandler{
		ctx:  ctx,
		req:  http_model.NewGetCreatingNumberRequest(),
		resp: http_model.NewGetCreatingNumberResponse(),
	}
}
