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 WrapRechargeRecordHandler(ctx *gin.Context) {
	handler := newRechargeRecordHandler(ctx)
	baseRun(handler)
}

type RechargeRecordHandler struct {
	ctx  *gin.Context
	req  *http_model.RechargeRecordRequest
	resp *http_model.CommonResponse
}

func (r RechargeRecordHandler) getContext() *gin.Context {
	return r.ctx
}

func (r RechargeRecordHandler) getResponse() interface{} {
	return r.resp
}

func (r RechargeRecordHandler) getRequest() interface{} {
	return r.req
}

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

func (r RechargeRecordHandler) checkParam() error {
	return nil
}

func newRechargeRecordHandler(ctx *gin.Context) *RechargeRecordHandler {
	return &RechargeRecordHandler{
		ctx:  ctx,
		req:  http_model.NewRechargeRecordRequest(),
		resp: http_model.NewRechargeRecordResponse(),
	}
}
