package handler

import (
	"github.com/gin-gonic/gin"
	"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 WrapFeeDetailHandler(ctx *gin.Context) {
	handler := newFeeDetailHandler(ctx)
	baseRun(handler)
}

type feeDetailHandler struct {
	ctx  *gin.Context
	req  *http_model.FeeDetailRequest
	resp *http_model.CommonResponse
}

func (f feeDetailHandler) getContext() *gin.Context {
	return f.ctx
}

func (f feeDetailHandler) getResponse() interface{} {
	return f.resp
}

func (f feeDetailHandler) getRequest() interface{} {
	return f.req
}

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

func (f feeDetailHandler) checkParam() error {
	return nil
}

func newFeeDetailHandler(ctx *gin.Context) *feeDetailHandler {
	return &feeDetailHandler{
		ctx:  ctx,
		req:  http_model.NewFeeDetailRequest(),
		resp: http_model.NewFeeDetailResponse(),
	}
}
