package com.seer.teach.pay.app.refund.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.pay.PayStatusEnum;
import com.seer.teach.common.enums.pay.RefundStatusEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.OrderIdGenerator;
import com.seer.teach.pay.app.refund.service.IAppRefundService;
import com.seer.teach.pay.app.wallet.req.AppCoinRefundReq;
import com.seer.teach.pay.entity.CoinRechargeEntity;
import com.seer.teach.pay.entity.CoinRefundEntity;
import com.seer.teach.pay.entity.CoinRefundLogEntity;
import com.seer.teach.pay.service.ICoinRechargeService;
import com.seer.teach.pay.service.ICoinRefundLogService;
import com.seer.teach.pay.service.ICoinRefundService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class AppRefundServiceImpl implements IAppRefundService {

    private final ICoinRechargeService coinRechargeService;

    private final ICoinRefundService coinRefundService;

    private final ICoinRefundLogService coinRefundLogService;

    @Override
    public boolean applyRefund(Integer rechargeId, AppCoinRefundReq refundReq) {
        String refundReason = refundReq.getRefundReason();
        int userId = StpUtil.getLoginIdAsInt();
        log.info("用户{}申请退款", userId);
        CoinRechargeEntity coinRecharge = coinRechargeService.getById(rechargeId);

        AssertUtils.notNull(coinRecharge, ResultCodeEnum.COIN_RECHARGE_NOT_FOUND);

        // 更新充值记录表中的状态为申请退款
        boolean result = coinRechargeService.update(new LambdaUpdateWrapper<CoinRechargeEntity>()
                .eq(CoinRechargeEntity::getUserId, userId)
                .eq(CoinRechargeEntity::getId, rechargeId)
                .set(CoinRechargeEntity::getPayStatus, PayStatusEnum.APPLY_REFUND.getCode()));

        if(!result){
            log.info("用户 {} 充值记录更新失败", userId);
            return false;
        }
        log.info("用户 {} 支付订单更新成功", userId);

        CoinRefundEntity coinRefund = new CoinRefundEntity();
        coinRefund.setUserId(userId);
        coinRefund.setRechargeId(rechargeId);
        coinRefund.setRefundReason(refundReason);
        String sn = "rs" + OrderIdGenerator.generateOrderId();
        coinRefund.setRefundSn(sn);
        coinRefund.setRefundAmount(coinRecharge.getRechargeAmount());
        coinRefund.setStatus(RefundStatusEnum.PENDING_REVIEW.getCode());
        boolean coinRefundResult = coinRefundService.save(coinRefund);
        log.info("插入退款记录表结果：{}", coinRefundResult);

        CoinRefundLogEntity log = new CoinRefundLogEntity();
        log.setRefundId(coinRefund.getId());
        log.setOperationName("申请退款");
        log.setOperationTime(LocalDateTime.now());
        log.setStatus(RefundStatusEnum.PENDING_REVIEW.getCode());
        log.setRemark(refundReason);
        coinRefundLogService.save(log);
        return coinRefundResult;
    }
}