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

type AddInvoiceRecordHandler struct {
	ctx  *gin.Context
	req  *http_model.AddInvoiceRecordRequest
	resp *http_model.CommonResponse
}

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

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

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

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

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

func newAddInvoiceRecordHandler(ctx *gin.Context) *AddInvoiceRecordHandler {
	return &AddInvoiceRecordHandler{
		ctx:  ctx,
		req:  http_model.NewAddInvoiceRecordRequest(),
		resp: http_model.NewAddInvoiceRecordResponse(),
	}
}
