package com.zsElectric.boot.business.quartz;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zsElectric.boot.business.mapper.UserRefundsOrderInfoMapper;
import com.zsElectric.boot.business.model.entity.Coupon;
import com.zsElectric.boot.business.model.entity.UserRefundsOrderInfo;
import com.zsElectric.boot.business.service.CouponService;
import com.zsElectric.boot.business.service.UserRefundsOrderInfoService;
import com.zsElectric.boot.business.service.WFTOrderService;
import com.zsElectric.boot.business.service.impl.UserRefundsOrderInfoServiceImpl;
import com.zsElectric.boot.common.util.SpringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 退款订单定时任务
 * 不为SUCCESS的退款订单任务查询结果
 *
 * @author zsElectric
 * @since 2026-01-09
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class QueryRefundOrderJob {

    private final UserRefundsOrderInfoService userRefundsOrderInfoService;

    private final WFTOrderService wftOrderService;

    /**
     * 每天凌晨2点执行一次
     * 不为SUCCESS的退款订单任务查询结果
     */
//    @Scheduled(cron = "0 0 01 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void processExpiredCoupons() {
        log.info("开始执行不为SUCCESS的退款订单任务");

        try {
            // 查询一周内不为SUCCESS的退款订单任务
            List<UserRefundsOrderInfo> refundsOrderInfoList =
                    userRefundsOrderInfoService.list(Wrappers.lambdaQuery(UserRefundsOrderInfo.class).ne(UserRefundsOrderInfo::getStatus, "SUCCESS"));
            log.info("查询到{}条不为SUCCESS的退款订单任务", refundsOrderInfoList.size());

            for (UserRefundsOrderInfo userRefundsOrderInfo : refundsOrderInfoList) {

                if(ObjectUtil.isNotEmpty(userRefundsOrderInfo.getRefundId())){
                    // 获取微信退款结果
                    Map<String, Object> map = wftOrderService.queryRefund(null, null, null, userRefundsOrderInfo.getRefundId());

                    // 获取微信退款结果状态
                    List<Map<String, Object>> refundList = (List<Map<String, Object>>) map.get("refund_list");
                    String refundStatus = refundList.get(0).get("refund_status").toString();
                    if(ObjectUtil.isEmpty(userRefundsOrderInfo.getStatus()) || !ObjectUtil.equal(userRefundsOrderInfo.getStatus(), refundStatus)){
                        userRefundsOrderInfo.setStatus(refundStatus);
                    }
                    if(ObjectUtil.isEmpty(userRefundsOrderInfo.getSuccessTime())){
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                        LocalDateTime dateTime = LocalDateTime.parse(refundList.get(0).get("refund_time").toString(), formatter);
                        userRefundsOrderInfo.setSuccessTime(dateTime);
                    }
                    userRefundsOrderInfoService.updateById(userRefundsOrderInfo);
                    log.info("退款金额：{},退款状态：{}", refundList.get(0).get("refund_fee").toString(), refundStatus);
                }
            }

        } catch (Exception e) {
            log.error("执行查看不为SUCCESS的退款订单任务失败", e);
        }
    }
}