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

type AddReceiveInfoHandler struct {
	ctx  *gin.Context
	req  *http_model.AddReceiveInfoRequest
	resp *http_model.CommonResponse
}

func (a AddReceiveInfoHandler) getContext() *gin.Context {
	return a.ctx
}

func (a AddReceiveInfoHandler) getResponse() interface{} {
	return a.resp
}

func (a AddReceiveInfoHandler) getRequest() interface{} {
	return a.req
}

func (a AddReceiveInfoHandler) run() {
	auth := middleware.GetSessionAuth(a.ctx)
	enterpriseID := auth.EnterpriseID
	err := db.AddReceiveInfo(a.ctx, enterpriseID, a.req)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[AddReceiveInfoHandler] call AddReceiveInfo err:%+v\n", err)
		util.HandlerPackErrorResp(a.resp, consts.ErrorInternal, "")
		logrus.Info("AddReceiveInfo fail,req:%+v", a.req)
		return
	}
	a.resp.Message = "新增发票信息成功"
}

func (a AddReceiveInfoHandler) checkParam() error {
	return nil
}

func newAddReceiveInfoHandler(ctx *gin.Context) *AddReceiveInfoHandler {
	return &AddReceiveInfoHandler{
		ctx:  ctx,
		req:  http_model.NewAddReceiveInfoRequest(),
		resp: http_model.NewAddReceiveInfoResponse(),
	}
}
