package com.sckw.payment.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.payment.api.model.constant.ChannelEnum;
import com.sckw.payment.api.model.dto.common.R;
import com.sckw.payment.dao.KwpWalletReceiveMapper;
import com.sckw.payment.dao.KwpWalletRefundMapper;
import com.sckw.payment.dao.KwpWalletTransferMapper;
import com.sckw.payment.model.KwpWalletReceive;
import com.sckw.payment.model.KwpWalletRefund;
import com.sckw.payment.model.KwpWalletTransfer;
import com.sckw.payment.model.constant.ReceiveEnum;
import com.sckw.payment.model.constant.RefundEnum;
import com.sckw.payment.model.constant.TransferEnum;
import com.sckw.payment.model.dto.wallet.SplitDto;
import com.sckw.payment.service.PayCenterService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xucaiqin
 * @date 2023-09-05 19:37:00
 */
@Slf4j
@Component
@EnableScheduling
public class RefundTask {
    @Resource
    private PayCenterService payCenterService;
    @Resource
    private KwpWalletRefundMapper kwpWalletRefundMapper;
    @Resource
    private KwpWalletTransferMapper kwpWalletTransferMapper;
    @Resource
    private KwpWalletReceiveMapper kwpWalletReceiveMapper;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void task() {
        LambdaQueryWrapper<KwpWalletRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletRefund::getStatus, RefundEnum.REFUNDING.getStatus());
        List<KwpWalletRefund> kwpWalletRefunds = kwpWalletRefundMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(kwpWalletRefunds)) {
            return;
        }
        try {
            for (KwpWalletRefund kwpWalletRefund : kwpWalletRefunds) {
                //查询清分状态，修改退款单状态
                R<List<SplitDto>> listR = payCenterService.agentPayQuery(kwpWalletRefund.getUid(), ChannelEnum.getByChannel(kwpWalletRefund.getChannel()), kwpWalletRefund.getOrderNo());
                if (listR.getStatus()) {
                    List<SplitDto> data = listR.getData();
                    if (!CollectionUtils.isEmpty(data)) {
                        if (data.stream().allMatch(a -> a.getStatus() == 1)) {
                            kwpWalletRefund.setUpdateTime(LocalDateTime.now());
                            kwpWalletRefund.setStatus(RefundEnum.SUCCESS.getStatus());
                            kwpWalletRefundMapper.updateById(kwpWalletRefund);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.error("定时任务异常：{}", ex, ex);
        }
    }

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void transferTask() {
        LambdaQueryWrapper<KwpWalletTransfer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletTransfer::getStatus, TransferEnum.TRANSFERRING.getStatus());
        List<KwpWalletTransfer> kwpWalletTransferList = kwpWalletTransferMapper.selectList(wrapper);

        LambdaQueryWrapper<KwpWalletReceive> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(KwpWalletReceive::getStatus, ReceiveEnum.TRANSFERRING.getStatus());
        List<KwpWalletReceive> walletReceiveList = kwpWalletReceiveMapper.selectList(wrapper2);


        if (CollectionUtils.isEmpty(kwpWalletTransferList) && CollectionUtils.isEmpty(walletReceiveList)) {
            return;
        }
        try {
            if(!CollectionUtils.isEmpty(kwpWalletTransferList)){
                for (KwpWalletTransfer kwpWalletTransfer : kwpWalletTransferList) {
                    //查询清分状态，修改退款单状态
                    R<List<SplitDto>> listR = payCenterService.agentPayQuery(kwpWalletTransfer.getUid(), ChannelEnum.getByChannel(kwpWalletTransfer.getChannel()), kwpWalletTransfer.getOrderNo());
                    if (listR.getStatus()) {
                        List<SplitDto> data = listR.getData();
                        if (!CollectionUtils.isEmpty(data)) {
                            if (data.stream().allMatch(a -> a.getStatus() == 1)) {
                                kwpWalletTransfer.setUpdateTime(LocalDateTime.now());
                                kwpWalletTransfer.setStatus(TransferEnum.SUCCESS.getStatus());
                                kwpWalletTransferMapper.updateById(kwpWalletTransfer);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.error("转账定时任务异常：{}", ex, ex);
        }
        try {
            if(!CollectionUtils.isEmpty(walletReceiveList)){
                for (KwpWalletReceive kwpWalletReceive : walletReceiveList) {
                    //查询清分状态，修改退款单状态
                    R<List<SplitDto>> listR = payCenterService.agentPayQuery(kwpWalletReceive.getUid(), ChannelEnum.getByChannel(kwpWalletReceive.getChannel()), kwpWalletReceive.getOrderNo());
                    if (listR.getStatus()) {
                        List<SplitDto> data = listR.getData();
                        if (!CollectionUtils.isEmpty(data)) {
                            if (data.stream().allMatch(a -> a.getStatus() == 1)) {
                                kwpWalletReceive.setUpdateTime(LocalDateTime.now());
                                kwpWalletReceive.setStatus(ReceiveEnum.SUCCESS.getStatus());
                                kwpWalletReceiveMapper.updateById(kwpWalletReceive);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.error("货到付款任务异常：{}", ex, ex);
        }
    }
}
