package com.jiayue.ipp.idp.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.idp.service.DqInterveneService;
import com.jiayue.ipp.idp.service.ForecastPowerShortTermService;
import com.jiayue.ipp.idp.service.an.ParsingChannelService;
import com.jiayue.ipp.idp.service.an.ParsingUrlService;
import com.jiayue.ipp.idp.service.impl.SendJyDqDataServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 下发嘉越短期数据
 *
 * @author jy
 * @since 2024/07/01
 */
@Service
@Slf4j
public class SendJyDqData {
    @Autowired
    ParsingChannelService parsingChannelService;
    @Autowired
    ForecastPowerShortTermService forecastPowerShortTermService;
    @Autowired
    VelocityEngine velocityEngine;
    @Autowired
    DqInterveneService dqInterveneService;
    @Autowired
    SendJyDqDataServiceImpl sendDqDownloadFileImpl;
    @Autowired
    ParsingUrlService parsingUrlService;

    @Scheduled(cron = "0 0 6 * * ?")
    public void sendJyDqData() {
        log.info("中心侧下发DQ数据定时开始");
        // 获取ftp通道
        LambdaQueryWrapper<ParsingChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ParsingChannel::getAnChannelType, "E1");
        lambdaQueryWrapper.eq(ParsingChannel::getUseStatus, "E1");
        List<ParsingChannel> parsingChannels = parsingChannelService.list(lambdaQueryWrapper);
        //遍历解析路径，对文件进行解析
        for (ParsingChannel parsingChannel : parsingChannels) {
            LambdaQueryWrapper<ParsingUrl> urlWrapper = new LambdaQueryWrapper<>();
            urlWrapper.eq(ParsingUrl::getUrlStatus, "1");
            urlWrapper.eq(ParsingUrl::getCId, parsingChannel.getId());
            List<ParsingUrl> parsingUrlList = parsingUrlService.list(urlWrapper);
            if (parsingUrlList.size()>0){
                sendDqDownloadFileImpl.sendDqDownloadFile(parsingUrlList.get(0).getStationCode(),parsingChannel.getUsername());
            }
        }
        log.info("中心侧下发DQ数据定时结束");

    }
}
