package credit_usage

import (
	"context"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

    "github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateCreditUsageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateCreditUsageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCreditUsageLogic {
	return &CreateCreditUsageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateCreditUsageLogic) CreateCreditUsage(req *types.CreditUsageInfo) (*types.BaseMsgResp, error) {
    _, err := l.svcCtx.DB.CreditUsage.Create().
			SetNotNilUserID(req.UserId).
			SetNotNilNumber(req.Number).
			SetNotNilStatus(req.Status).
			SetNotNilNtype(req.Ntype).
			SetNotNilTable(req.Table).
			SetNotNilOrganizationID(req.OrganizationId).
			SetNotNilNid(req.Nid).
			SetNotNilReason(req.Reason).
			SetNotNilOperator(req.Operator).
			Save(l.ctx)

    if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

    return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
}
