package com.ruoyi.task.reconciliationDate;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.channel.domain.vo.ChannelNameVO;
import com.ruoyi.bst.channel.service.ChannelService;
import com.ruoyi.bst.reconciliationDate.service.ReconciliationDateService;
import com.ruoyi.common.utils.collection.CollectionUtils;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ReconciliationDateTask {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ReconciliationDateService reconciliationDateService;

    public void recordReconciliationDate(Integer lastDays) {
        // 获取昨日日期
        LocalDate now = LocalDate.now();
        LocalDate startDay = now.minusDays(lastDays);

        // 查询所有支付渠道列表
        List<ChannelNameVO> channelList = channelService.selectAllChannelNameList();
        if (CollectionUtils.isEmptyElement(channelList)) {
            log.error("渠道列表为空");
            return;
        }
        // 查询所有运营区ID
        List<Long> areaIdList = areaService.selectIdList(new AreaQuery());
        if (CollectionUtils.isEmptyElement(areaIdList)) {
            log.error("运营区列表为空");
            return;
        }


        while (now.isAfter(startDay)) {
            // 支付渠道
            for (ChannelNameVO channel : channelList) {
                if (channel == null) {
                    continue;
                }
                for (Long areaId : areaIdList) {
                    if (areaId == null) {
                        continue;
                    }
                    reconciliationDateService.insertReconciliationDate(startDay, channel, areaId);
                }
            }

            startDay = startDay.plusDays(1);
        }
    }


}
