package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/issue9/conv"
	"github.com/sirupsen/logrus"
	"youngee_b_api/consts"
	"youngee_b_api/db"
	"youngee_b_api/middleware"
	"youngee_b_api/model/http_model"
	"youngee_b_api/util"
)

func WrapWorkspaceBarNumsHandler(ctx *gin.Context) {
	handler := newWorkspaceBarNumsHandler(ctx)
	baseRun(handler)
}

type WorkspaceBarNumsHandler struct {
	ctx  *gin.Context
	req  *http_model.WorkspaceBarNumsRequest
	resp *http_model.CommonResponse
}

func (w WorkspaceBarNumsHandler) getContext() *gin.Context {
	return w.ctx
}

func (w WorkspaceBarNumsHandler) getResponse() interface{} {
	return w.resp
}

func (w WorkspaceBarNumsHandler) getRequest() interface{} {
	return w.req
}

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

func (w WorkspaceBarNumsHandler) checkParam() error {
	return nil
}

func newWorkspaceBarNumsHandler(ctx *gin.Context) *WorkspaceBarNumsHandler {
	return &WorkspaceBarNumsHandler{
		ctx:  ctx,
		req:  http_model.NewWorkspaceBarNumsRequest(),
		resp: http_model.NewWorkspaceBarNumsResponse(),
	}
}
