package card_info_t_mall_game

import (
	"context"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/util/gconv"

	"kami/api/card_info_t_mall_game/v1"

	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) TMallGameOrderSubmit(ctx context.Context, req *v1.TMallGameOrderSubmitReq) (res *v1.TMallGameOrderSubmitRes, err error) {
	accountService := service.RechargeTMallGameAccount()
	accountSchema, err := accountService.GetOne(ctx, "", req.AccountNumber)
	if err != nil || pointer.IsNil(accountSchema) || accountSchema.Id == "" {
		glog.Error(ctx, "获取账号数据失败", "账号：", req.AccountNumber, err)
		err = errHandler.WrapError(ctx, gcode.CodeNotFound, err, "账号不存在或账号不可用", gconv.String(req))
		return
	}
	orderNo, err := service.RechargeTMallGameOrder().Create(ctx, &model.RechargeTMallGameOrderCreateInput{
		AccountId:          accountSchema.Id,
		AccountNumber:      req.AccountNumber,
		Amount:             req.Amount,
		CallbackType:       req.CallbackType,
		MerchantOrder:      req.MerchantOrder,
		ThirdMerchantOrder: req.ThirdMerchantOrder,
		CallbackUrl:        req.CallbackUrl,
		Attach:             req.Attach,
		ChannelName:        req.ChannelName,
	})
	err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "创建订单错误")
	return &v1.TMallGameOrderSubmitRes{
		OrderNo: orderNo,
		Status:  consts.TMallGameCallbackStatusSuccess,
	}, err
}
