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 UpdateCreditUsageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *UpdateCreditUsageLogic) UpdateCreditUsage(req *types.CreditUsageInfo) (*types.BaseMsgResp, error) {
    err := l.svcCtx.DB.CreditUsage.UpdateOneID(*req.Id).
			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).
			Exec(l.ctx)

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

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