package card_apple_order

import (
	"context"
	"errors"
	"fmt"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/net/gtrace"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
	"kami/internal/consts"
	"kami/internal/dao"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/model/entity"
	"kami/internal/service"
	"kami/utility/cache"
	"kami/utility/config"
	"kami/utility/integration/apple"
	"time"

	"github.com/duke-git/lancet/v2/slice"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/shopspring/decimal"
)

// checkAccountConsecutiveRevoke 检查账号连续撤销次数
func (h *sAppleOrder) checkAccountConsecutiveRevoke(ctx context.Context, accountInfo *entity.V1CardAppleAccountInfo, orderInfo *entity.V1CardAppleRechargeInfo) error {
	// 获取该账号最近的历史记录，多查询几条以便准确判断连续性
	revokeCount, err := dao.V1CardAppleHistoryInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardAppleHistoryInfo.Columns().AccountId, accountInfo.Id).
		Where(dao.V1CardAppleHistoryInfo.Columns().OrderNo, orderInfo.OrderNo).
		Where(dao.V1CardAppleHistoryInfo.Columns().Operation, consts.AppleRechargeOperationItunesRefund).
		Count()

	if err != nil {
		return nil // 查询失败不影响主流程
	}

	// 如果历史记录太少，不进行检查
	if revokeCount <= consts.AppleAccountMaxConsecutiveRevokeCount {
		return nil
	}

	_ = service.AppleAccount().ModifyStatus(ctx, accountInfo.Id, consts.AppleAccountForbiddenBySafetyReason, nil)

	return nil
}
func (h *sAppleOrder) queryBalance(ctx context.Context, orderInfo *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, position string) (balance float64, err error) {
	ctx, span := gtrace.NewSpan(ctx, "queryBalance")
	defer span.End()
	queryResp, err := apple.NewClient().QueryBalance(ctx, &apple.QueryBalanceReq{
		Account:  accountInfo.Account,
		Password: accountInfo.Password,
		OrderId:  orderInfo.OrderNo,
	})
	span.AddEvent("查询订单")
	if err != nil {
		_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
			RechargeId:  int(orderInfo.Id),
			OrderNo:     orderInfo.OrderNo,
			AccountID:   accountInfo.Id,
			AccountName: accountInfo.Account,
			Operation:   consts.AppleRechargeOperationQuery,
			Remark:      fmt.Sprintf("账号【%s】，卡密【%s】查询充值余额失败，失败原因：网络错误", accountInfo.Account, orderInfo.CardPass),
		}, nil)
		return 0, gerror.New("查询充值余额失败")
	}
	if queryResp.Code != apple.CodeSuccess {
		_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
			RechargeId:  int(orderInfo.Id),
			OrderNo:     orderInfo.OrderNo,
			AccountID:   accountInfo.Id,
			AccountName: accountInfo.Account,
			Operation:   consts.AppleRechargeOperationQuery,
			Remark:      fmt.Sprintf("账号【%s】，卡密【%s】查询充值余额失败，失败原因：%s", accountInfo.Account, orderInfo.CardPass, queryResp.Message),
		}, nil)
		return 0, errors.New(queryResp.Code.String())
	}
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		RechargeId:  int(orderInfo.Id),
		OrderNo:     orderInfo.OrderNo,
		AccountID:   accountInfo.Id,
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationQuery,
		Remark:      fmt.Sprintf("账号【%s】充值%s余额：%.2f", accountInfo.Account, position, queryResp.Data.Balance),
	}, nil)
	return queryResp.Data.Balance, nil
}

// handleRedeemResult 处理核销结果，根据状态码分类进行对应处理
func (h *sAppleOrder) handleRedeemResult(ctx context.Context, orderInfo *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo) error {
	ctx, span := gtrace.NewSpan(ctx, "handleRedeemResult")
	defer span.End()
	// 准备推送请求
	redeemReq := &apple.RedeemReq{
		Account:        accountInfo.Account,
		Password:       accountInfo.Password,
		OrderId:        orderInfo.OrderNo,
		RedemptionCode: orderInfo.CardPass,
	}

	balance, err := h.queryBalance(ctx, orderInfo, accountInfo, "前")
	if err != nil {
		return h.handleRedeemUnknown(ctx, orderInfo, accountInfo, err.Error())
	}

	resp, err := apple.NewClient().Redeem(ctx, redeemReq)
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		RechargeId:  int(orderInfo.Id),
		OrderNo:     orderInfo.OrderNo,
		AccountID:   accountInfo.Id,
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationItunesComplete,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值【%.2f】元，充值结束，正在处理结果", orderInfo.CardPass, accountInfo.Account, orderInfo.Balance),
	}, nil)
	balanceAfter, _ := h.queryBalance(ctx, orderInfo, accountInfo, "后")
	if err != nil {
		if balanceAfter > balance {
			return h.handleBalanceAnomalyError(ctx, orderInfo, accountInfo, balanceAfter-balance, balanceAfter)
		}
		return h.handleRedeemUnknown(ctx, orderInfo, accountInfo, "苹果充值网络错误"+err.Error())
	}
	span.AddEvent(fmt.Sprintf("兑换订单 %s", resp.Code.String()))

	// 根据状态码分类处理
	switch {
	// 1. 成功状态（CodeSuccess = 0）
	case resp.Code == apple.CodeSuccess:
		return h.handleRedeemSuccess(ctx, orderInfo, accountInfo, resp.Data.Amount, resp.Data.BalanceAfter)

	// 2. 网络请求或系统资源错误（5000-5999）
	case resp.Code >= 5000 && resp.Code < 6000:
		if balanceAfter > balance {
			return h.handleBalanceAnomalyError(ctx, orderInfo, accountInfo, balanceAfter-balance, balanceAfter)
		}
		return h.handleSystemError(ctx, orderInfo, accountInfo, resp.Code)

	// 3. 苹果账户原因错误（8001-8005）
	case resp.Code >= 8001 && resp.Code <= 8006:
		return h.handleAccountError(ctx, orderInfo, accountInfo, resp.Code)

	// 4. 充值限制错误（8010-8012）
	case resp.Code >= 8010 && resp.Code <= 8012:
		return h.handleRedeemLimitError(ctx, orderInfo, accountInfo, resp.Code)

	// 5. 卡密错误（8013-8014）
	case resp.Code >= 8013 && resp.Code <= 8015:
		return h.handleCardCodeError(ctx, orderInfo, accountInfo, resp.Code)

	// 未知错误
	default:
		if balanceAfter > balance {
			return h.handleBalanceAnomalyError(ctx, orderInfo, accountInfo, balanceAfter-balance, balanceAfter)
		}
		return h.handleRedeemUnknown(ctx, orderInfo, accountInfo, resp.Code.String())
	}
}

// handleRedeemSuccess 处理核销成功的情况
func (h *sAppleOrder) handleRedeemSuccess(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, amount, balanceAfter float64) error {

	// 判断金额是否一致
	var orderStatus consts.AppleRechargeOrderStatus
	var historyOperation consts.AppleOrderOperation
	var orderRemark string
	var orderOperationRemark string

	if amount == orderEntity.CardAmount {
		orderStatus = consts.AppleRechargeOrderSuccess
		historyOperation = consts.AppleRechargeOperationItunesSucceed
		orderRemark = "核销成功"
		orderOperationRemark = fmt.Sprintf("充值成功:使用卡密【%s】为账号【%s】充值【%.2f】元,卡密已核销成功,余额【%.2f】", orderEntity.CardPass, accountInfo.Account, amount, balanceAfter)
	} else {
		orderStatus = consts.AppleRechargeOrderAmountDifferent
		historyOperation = consts.AppleRechargeOperationItunesSucceedButWrongAmount
		orderRemark = fmt.Sprintf("金额不一致，拉单金额%.2f元，实际金额%.2f元", orderEntity.CardAmount, amount)
		orderOperationRemark = fmt.Sprintf("充值失败:使用卡密【%s】为账号【%s】充值时，拉单金额【%.2f】与实际金额【%.2f】不一致,卡密已核销成功,因为金额不一致,所以订单不给回调.", orderEntity.CardPass, accountInfo.Account, orderEntity.CardAmount, amount)
	}

	// 更新订单和账户余额
	err := h.UpdateActualAmountAndHistoryAndWallet(ctx, &model.AppleAccountUpdateAmountAndHistoryRecord{
		OrderInfo: &model.AppleAccountUpdateAmountRecord{
			OrderNo: orderEntity.OrderNo,
			Amount:  amount,
			Status:  orderStatus,
			Remark:  orderRemark,
		},
		AccountId:         accountInfo.Id,
		HistoryRemark:     orderOperationRemark,
		HistoryOperation:  historyOperation,
		BalanceFromItunes: balanceAfter,
	})

	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单成功记录失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	glog.Info(ctx, fmt.Sprintf("订单核销成功处理完毕 - 订单号: %s", orderEntity.OrderNo))
	return nil
}

// handleRedeemUnknown 处理核销失败的情况
func (h *sAppleOrder) handleRedeemUnknown(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, errMsg string) error {
	// 订单退回待处理状态
	err := h.ModifyOrderStatus(ctx, orderEntity.OrderNo, consts.AppleRechargeOrderWaiting, "核销失败，重新处理", nil)
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单失败状态失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	// 添加历史记录
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		AccountID:   accountInfo.Id,
		OrderNo:     orderEntity.OrderNo,
		RechargeId:  int(orderEntity.Id),
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationItunesRefund,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值失败，核销失败：%s", orderEntity.CardPass, accountInfo.Account, errMsg),
	}, nil)

	glog.Info(ctx, fmt.Sprintf("订单核销失败，已退回待处理 - 订单号: %s", orderEntity.OrderNo))
	return nil
}

// handleSystemError 处理系统资源错误（5000-5999）
// 订单退回待处理，等待系统重新调度重试
func (h *sAppleOrder) handleSystemError(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, code apple.Code) error {
	// 订单退回待处理状态
	err := h.ModifyOrderStatus(ctx, orderEntity.OrderNo, consts.AppleRechargeOrderWaiting, "网络错误，退回重新处理", nil)
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单状态失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	// 减少分配计数
	_ = h.DecrementDistributionCount(ctx, orderEntity.OrderNo)

	// 添加历史记录
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		AccountID:   accountInfo.Id,
		OrderNo:     orderEntity.OrderNo,
		RechargeId:  int(orderEntity.Id),
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationItunesFail,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值失败，网络原因错误：%s，订单退回重新处理", orderEntity.CardPass, accountInfo.Account, code.String()),
	}, nil)

	glog.Warning(ctx, fmt.Sprintf("系统错误，订单已退回待处理 - 订单号: %s, 状态码: %d", orderEntity.OrderNo, code))
	return nil
}

// handleAccountError 处理苹果账户原因错误（8001-8005）
// 标记账户失效，订单退回待处理，等待重新分配其他有效账户
func (h *sAppleOrder) handleAccountError(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, code apple.Code) error {
	// 根据错误码标记不同的账户失效状态
	var accountStatus consts.AppleAccountStatus
	switch code {
	case apple.CodeApplePasswordError, apple.CodeApplePasswordExpired:
		accountStatus = consts.AppleAccountWrongPassword // 账号密码错误
	case apple.CodeAppleAccountForbidden:
		accountStatus = consts.AppleAccountForbidden // 账号被禁用
	case apple.CodeAppleAccountLocked:
		accountStatus = consts.AppleAccountLimited // 账号被封锁（作为受限处理）
	case apple.CodeAppleAccountCountryMismatch:
		accountStatus = consts.AppleAccountForbidden // 状态未知，保持正常以便后续重试
	default:
		accountStatus = consts.AppleAccountWrongPassword
	}

	// 标记账户为失效状态
	_ = service.AppleAccount().ModifyStatus(ctx, accountInfo.Id, accountStatus, nil)

	// 订单重新设为待调度状态
	err := h.ModifyOrderStatus(ctx, orderEntity.OrderNo, consts.AppleRechargeOrderWaiting, "退回，账户原因重新处理", nil)
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单状态失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	// 减少分配计数，允许重新分配
	_ = h.DecrementDistributionCount(ctx, orderEntity.OrderNo)
	// 添加历史记录
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		AccountID:   accountInfo.Id,
		OrderNo:     orderEntity.OrderNo,
		RechargeId:  int(orderEntity.Id),
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationWrongPassword,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值失败，订单退回重新充值，因为苹果账户错误，原因：%s", orderEntity.CardPass, accountInfo.Account, code.String()),
	}, nil)

	glog.Warning(ctx, fmt.Sprintf("苹果账户原因导致失效，订单已退回待重新分配 - 订单号: %s, 账号: %s", orderEntity.OrderNo, accountInfo.Account))
	return nil
}

// handleRedeemLimitError 处理充值限制错误（8010-8012）
// 根据限制类型标记账户，订单根据业务规则决定重试或失败
func (h *sAppleOrder) handleRedeemLimitError(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, code apple.Code) error {
	// 根据限制类型标记账户状态
	var accountStatus consts.AppleAccountStatus
	var accountRemark string

	switch code {
	case apple.CodeAppleRedeemLimitExceeded:
		// 充值次数限制 - 永久受限
		accountStatus = consts.AppleAccountForbiddenByTooManyRecharge
		accountRemark = "苹果账户充值次数限制（永久）"
	case apple.CodeAppleRedeemLimitExceededTemporarily:
		// 临时限制 - 标记为临时冻结，暂停兑换
		accountStatus = consts.AppleAccountTmpStoppedByTooManyRequest
		accountRemark = "苹果账户临时充值限制（2分钟后恢复）"
	case apple.CodeAppleRedeemLimitExceededPerMinute:
		accountStatus = consts.AppleAccountTmpLimited
		accountRemark = "苹果账户一分钟充值限制（1分钟后恢复）"
	default:
		accountStatus = consts.AppleAccountLimited
		accountRemark = "苹果账户充值限制"
	}

	// 标记账户限制状态
	_ = service.AppleAccount().ModifyStatus(ctx, accountInfo.Id, accountStatus, nil)

	// 订单退回待处理，后续可根据限制类型进行重试或转移到其他账户
	err := h.ModifyOrderStatus(ctx, orderEntity.OrderNo, consts.AppleRechargeOrderWaiting, "退回，账户原因重新处理", nil)
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单状态失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	// 减少分配计数
	_ = h.DecrementDistributionCount(ctx, orderEntity.OrderNo)

	// 添加历史记录
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		AccountID:   accountInfo.Id,
		OrderNo:     orderEntity.OrderNo,
		RechargeId:  int(orderEntity.Id),
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationItunesFail,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值失败，订单退回重新充值，账户受限：%s", orderEntity.CardPass, accountInfo.Account, accountRemark),
	}, nil)

	glog.Warning(ctx, fmt.Sprintf("账户充值限制，订单已退回待处理 - 订单号: %s, 账号: %s, 限制类型: %d", orderEntity.OrderNo, accountInfo.Account, code))
	return nil
}

// handleCardCodeError 处理卡密错误（8013-8014）
// 直接按订单失败处理，更新订单状态为失败
func (h *sAppleOrder) handleCardCodeError(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, code apple.Code) error {
	// 根据卡密错误类型确定订单失败状态
	var orderStatus = consts.AppleRechargeOrderFail

	// 更新订单为失败状态
	err := h.ModifyOrderStatus(ctx, orderEntity.OrderNo, orderStatus, code.String(), nil)
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单失败状态失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}

	// 添加历史记录
	_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
		AccountID:   accountInfo.Id,
		OrderNo:     orderEntity.OrderNo,
		RechargeId:  int(orderEntity.Id),
		AccountName: accountInfo.Account,
		Operation:   consts.AppleRechargeOperationItunesFail,
		Remark:      fmt.Sprintf("使用卡密【%s】为账号【%s】充值失败，%s", orderEntity.CardPass, accountInfo.Account, code.String()),
	}, nil)

	glog.Warning(ctx, fmt.Sprintf("卡密错误，订单已失败 - 订单号: %s, 账号: %s, 错误: %s", orderEntity.OrderNo, accountInfo.Account, code.String()))
	return nil
}

// handleBalanceAnomalyError 处理余额异常情况（充值成功但余额不符）
// 卡密已被核销，但充值前后余额异常，直接按订单失败处理
func (h *sAppleOrder) handleBalanceAnomalyError(ctx context.Context, orderEntity *entity.V1CardAppleRechargeInfo, accountInfo *entity.V1CardAppleAccountInfo, diffAmount float64, itunesBalance float64) error {
	var orderStatus consts.AppleRechargeOrderStatus
	var historyOperation consts.AppleOrderOperation
	var orderRemark string

	if diffAmount == orderEntity.CardAmount {
		orderRemark = "支付成功"
		historyOperation = consts.AppleRechargeOperationItunesSucceed
		orderStatus = consts.AppleRechargeOrderSuccess
	} else {
		orderRemark = fmt.Sprintf("金额不一致，拉单金额%.2f元，实际金额%.2f元", orderEntity.CardAmount, diffAmount)
		historyOperation = consts.AppleRechargeOperationItunesSucceedButWrongAmount
		orderStatus = consts.AppleRechargeOrderAmountDifferent
	}

	operationRemark := fmt.Sprintf("使用卡密【%s】给账户【%s】充值，出现网络异常后，对比前后余额，发现前后余额相差%.2f，与拉单金额一致，所以本单掉单%.2f", orderEntity.CardPass, accountInfo.Account, diffAmount, diffAmount)

	// 更新订单和账户余额
	err := h.UpdateActualAmountAndHistoryAndWallet(ctx, &model.AppleAccountUpdateAmountAndHistoryRecord{
		OrderInfo: &model.AppleAccountUpdateAmountRecord{
			OrderNo: orderEntity.OrderNo,
			Amount:  diffAmount,
			Status:  orderStatus,
			Remark:  orderRemark,
		},
		AccountId:         accountInfo.Id,
		HistoryRemark:     operationRemark,
		HistoryOperation:  historyOperation,
		BalanceFromItunes: itunesBalance,
	})
	if err != nil {
		glog.Error(ctx, fmt.Sprintf("更新订单和账户余额失败 - 订单号: %s, 错误: %v", orderEntity.OrderNo, err))
		return err
	}
	glog.Warning(ctx, fmt.Sprintf("充值异常订单掉单处理 - 订单号: %s, 账号: %s, 详情: %s", orderEntity.OrderNo, accountInfo.Account, operationRemark))
	return nil
}

func (h *sAppleOrder) processOrderWithAccount(ctx context.Context, orderInfo *entity.V1CardAppleRechargeInfo) (err error) {
	keys := cache.NewCache().GetPrefixKey(ctx, cache.PrefixAppleAccount.String())
	excludeAccountInfos := slice.Map(slice.Filter(keys, func(index int, item any) bool {
		_, ok := item.(string)
		return ok
	}), func(index int, item any) string {
		return item.(string)
	})
	accountInfo, err := service.AppleAccount().GetAccordingAccount(ctx, decimal.NewFromFloat(orderInfo.Balance), excludeAccountInfos)
	if err != nil || accountInfo == nil {
		return
	}
	ctx, span := gtrace.NewSpan(ctx, "processOrderWithAccount", trace.WithAttributes(attribute.String("orderId", orderInfo.OrderNo)))
	defer span.End()
	// 检查账号连续撤销次数
	if err = h.checkAccountConsecutiveRevoke(ctx, accountInfo, orderInfo); err != nil {
		glog.Warning(ctx, fmt.Sprintf("账号连续iTunes退回次数超限 - 账号: %s, 错误: %v", accountInfo.Account, err))
		// 禁用账号
		_ = service.AppleAccount().ModifyStatus(ctx, accountInfo.Id, consts.AppleAccountForbidden, nil)
		return
	}
	span.AddEvent("获取账号", trace.WithAttributes(attribute.String("account", accountInfo.Account)))

	cacheVar, err := cache.NewCache().Get(ctx, cache.PrefixAppleAccount.Key(accountInfo.Id))
	if err == nil && !cacheVar.IsNil() {
		span.AddEvent("账户正在处理中")
		return
	}
	//3分钟超时
	_ = cache.NewCache().Set(ctx, cache.PrefixAppleAccount.Key(accountInfo.Id), 1, time.Minute*3)
	defer func() {
		span.AddEvent("处理完成")
		_, _ = cache.NewCache().Remove(ctx, cache.PrefixAppleAccount.Key(accountInfo.Id))
	}()
	span.AddEvent("处理订单")
	if err = h.DistributionAccordingAccount(ctx, accountInfo, orderInfo); err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "分配订单账户失败")
		return
	}
	err = config.GetDatabaseV1().Transaction(ctx, func(ctx2 context.Context, tx gdb.TX) error {
		err = service.AppleOrder().ModifyOrderStatus(ctx2, orderInfo.OrderNo, consts.AppleRechargeOrderProcessing, "", tx)
		if err != nil {
			return err
		}
		err = service.AppleOrder().AddHistory(ctx2, &model.AppleCardRechargeHistoryInput{
			AccountID:   accountInfo.Id,
			OrderNo:     orderInfo.OrderNo,
			RechargeId:  int(orderInfo.Id),
			AccountName: accountInfo.Account,
			Operation:   consts.AppleRechargeOperationStartRechargeByItunes,
			Remark:      fmt.Sprintf("分配账户：%s", accountInfo.Account),
		}, tx)
		return err
	})
	if err != nil {
		return err
	}
	return h.handleRedeemResult(ctx, orderInfo, accountInfo)
}
