package com.jiayue.ipp.idp.job;

import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
import com.jiayue.ipp.idp.config.Constant;
import com.jiayue.ipp.idp.service.an.DownloadService;
import com.jiayue.ipp.idp.service.an.FTPService;
import com.jiayue.ipp.idp.service.an.ParsingChannelService;
//import com.jiayue.ipp.idp.util.RedisUtil;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@AllArgsConstructor
public class ParsingJob {
    private final ParsingChannelService parsingChannelService;
    private final DownloadService downloadService;
    private final FTPService ftpService;
//    private final RedisUtil redisUtil;

//    @XxlJob("parsingJob")
    void p(){
//        if(redisUtil.hasKey(Constant.PKEY)){
//            if(redisUtil.get(Constant.PKEY).toString().equals("false")){
//                start();
//            }
//        }else {
            start();
//        }

    }

//    @Scheduled(fixedRate = 900000)
    @Scheduled(fixedDelay = 60000)
    void start(){
//        redisUtil.setWithExpire(Constant.PKEY,"true",300l, TimeUnit.SECONDS);

        List<ParsingChannel> parsingChannels = parsingChannelService.list();

        //循环ftp解析通道
        for (ParsingChannel parsingChannel : parsingChannels) {
            if(parsingChannel.getUseStatus().name().equals("E1")){
                switch (parsingChannel.getAnChannelType().name()){
                    case "E1":
                        ftpService.ftp(parsingChannel);
                        break;
                    case "E2":
//                        downloadService.v3DataFilesDownload(parsingChannel);
                        break;
                    case "E3":
                        // 解析自己家的云端预测文件
                        downloadService.parsing(parsingChannel);
                        break;
                    default:
                        break;
                }
            }
        }

//        redisUtil.set(Constant.PKEY,"false");
    }
}
