//package com.jiayue.ipp.idp.job;
//
//import cn.hutool.core.io.IoUtil;
//import cn.hutool.core.io.resource.ResourceUtil;
//import cn.hutool.core.util.CharsetUtil;
//import cn.hutool.extra.template.Template;
//import cn.hutool.extra.template.TemplateConfig;
//import cn.hutool.extra.template.TemplateEngine;
//import cn.hutool.extra.template.TemplateUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
//import com.jiayue.ipp.common.data.entity.DqIntervene;
//import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
//import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
//import com.jiayue.ipp.idp.dto.ForecastData;
//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.util.FileUtil;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.lang.time.DateFormatUtils;
//import org.apache.commons.lang.time.DateUtils;
//import org.apache.velocity.VelocityContext;
//import org.apache.velocity.app.VelocityEngine;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.StringWriter;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * 下发嘉越短期数据
// *
// * @author jy
// * @since 2024/07/01
// */
//@Service
//@Slf4j
//public class SendJyDqData {
//
//    @Value("${dqForecastDays}")
//    private Integer dqForecastDays;
//
//    @Autowired
//    ParsingChannelService parsingChannelService;
//    @Autowired
//    ForecastPowerShortTermService forecastPowerShortTermService;
//    @Autowired
//    VelocityEngine velocityEngine;
//    @Autowired
//    DqInterveneService dqInterveneService;
//
//    @Scheduled(cron = "0 0/1 * * * *")
//    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);
//        String dateDir = DateFormatUtils.format(new Date(), "yyyyMMdd");
//        //遍历解析路径，对文件进行解析
//        for (ParsingChannel parsingChannel : parsingChannels) {
//            try{
//                String distPath = FileUtil.getSendJyDataPath()+File.separator+parsingChannel.getUsername()+File.separator+"download"+File.separator+dateDir;
//                File dirFile = new File(distPath);
//                if (!dirFile.exists()) {
//                    dirFile.mkdirs();
//                }
//                // 生成短期文件
//                String stationCode = parsingChannel.getUsername().split("-")[0];
//                // 获取短期实时数据，开始时间
//                Long startTime = DateMomentUtil.getDayStartTime(DateUtils.addDays(new Date(), 1).getTime());
//                // 结束时间（开始加24小时再减去1秒）
//                Long endTime = DateMomentUtil.getDayLastTime(DateUtils.addDays(new Date(), dqForecastDays).getTime());
//
//                // 查询短期预测功率
//                LambdaQueryWrapper<ForecastPowerShortTerm> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(ForecastPowerShortTerm::getForecastManufactor, "SYJY");
//                queryWrapper.eq(ForecastPowerShortTerm::getStationCode, stationCode);
//                queryWrapper.between(ForecastPowerShortTerm::getForecastTime, new Date(startTime), new Date(endTime));
//
//                List<ForecastPowerShortTerm> forecastPowerShortTermList = this.forecastPowerShortTermService.list(queryWrapper);
//                Map<Long, ForecastPowerShortTerm> forecastPowerShortMap = forecastPowerShortTermList.stream()
//                        .collect(Collectors.toMap(
//                                myObject -> myObject.getForecastTime().getTime(), // keyMapper，将Date转换为long
//                                myObject -> myObject, // valueMapper，保持原对象
//                                (existing, replacement) -> existing // mergeFunction，处理重复key的情况，这里简单地保留现有的value
//                        ));
//
//                // 人工干预
//                QueryWrapper<DqIntervene> dqInterveneQueryWrapper = new QueryWrapper<>();
//                dqInterveneQueryWrapper.between("forecast_time", new Date(startTime), new Date(endTime));
//                dqInterveneQueryWrapper.eq("station_code",stationCode);
//                List<DqIntervene> dqInterveneList = dqInterveneService.list(dqInterveneQueryWrapper);
//                Map<Long, DqIntervene> dqInterveneMap = dqInterveneList.stream()
//                        .collect(Collectors.toMap(
//                                myObject -> myObject.getForecastTime().getTime(), // keyMapper，将Date转换为long
//                                myObject -> myObject, // valueMapper，保持原对象
//                                (existing, replacement) -> existing // mergeFunction，处理重复key的情况，这里简单地保留现有的value
//                        ));
//
//                List<ForecastData> vList = new ArrayList<>();
//
//                for (Long tempTime = startTime.longValue(); tempTime <= endTime.longValue(); tempTime = tempTime + 15 * 60 * 1000L) {
//                    ForecastData forecastData = new ForecastData();
//                    forecastData.setTime(DateFormatUtils.format(tempTime, "yyyy-MM-dd HH:mm:ss"));
//                    if (dqInterveneMap.get(tempTime)!=null){
//                        forecastData.setPower(dqInterveneMap.get(tempTime).getActivePower().toString());
//                    }
//                    else if (forecastPowerShortMap.get(tempTime)!=null){
//                        forecastData.setPower(forecastPowerShortMap.get(tempTime).getFpValue().toString());
//                    }
//                    else{
//                        forecastData.setPower("");
//                    }
//                    vList.add(forecastData);
//                }
//
//                VelocityContext velocityContext = new VelocityContext();
//                velocityContext.put("vList", vList);
//                // 系统当前日期
//                velocityContext.put("currentTime", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
//                StringWriter writer = new StringWriter();
//                org.apache.velocity.Template template = this.velocityEngine.getTemplate(FileUtil.getResourceBasePath() + "/templates/send_dq.vm");
//                template.merge(velocityContext, writer);
//                String vmFileName = "send_"+stationCode+"_dq_"+dateDir+".WPD";
//                createSendDqFile(writer,new File(distPath + File.separator+vmFileName));
//
//
////                Map bindingMap = new HashMap();
////                bindingMap.put("vList", forecastPowerShortTermHisList);
////                String content = IoUtil.readUtf8(ResourceUtil.getStream("templates"+File.separator +"send_dq.vm"));
////                Template templateDq = getTemplate(content);
////                //渲染模型数据
////                String vmStr = templateDq.render(bindingMap);
////
////                cn.hutool.core.io.FileUtil.writeString(vmStr, distPath + File.separator+vmFileName, CharsetUtil.UTF_8);
//                log.info(stationCode+"下发嘉越DQ数据成功！");
//            }
//            catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//        log.info("下发嘉越DQ数据结束");
//
//    }
//    public static Template getTemplate(String templateStr) {
//        //构造模板引擎
//        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig());
//        Template template = engine.getTemplate(templateStr);
//        return template;
//    }
//
//    /**
//     * 生成上报文件
//     *
//     * @param writer
//     * @param file
//     */
//    protected void createSendDqFile(StringWriter writer, File file) {
//        FileOutputStream os = null;
//        try {
//            os = new FileOutputStream(file);
//            // 采用UTF-8字符集
//            os.write(writer.toString().getBytes("UTF-8"));
//            os.flush();
//
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (os != null) {
//                try {
//                    os.close();
//                } catch (IOException e) {
//                    log.error("文件生成关闭流失败", e);
//                }
//            }
//        }
//    }
//}
