package com.jiayue.ipp.idp.controller;

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.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.idp.dto.DqInterveneDto;
import com.jiayue.ipp.idp.dto.DqTempDto;
import com.jiayue.ipp.idp.dto.ForecastData;
import com.jiayue.ipp.idp.service.DqInterveneService;
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 com.jiayue.ipp.idp.util.FileUtil;
import com.jiayue.ipp.idp.util.R;
import lombok.extern.slf4j.Slf4j;
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.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 短期干预
 */
@RestController
@RequestMapping("/dqInterveneController")
@Slf4j
public class DqInterveneController {
    @Autowired
    DqInterveneService dqInterveneService;
    @Autowired
    ParsingChannelService parsingChannelService;
    @Autowired
    VelocityEngine velocityEngine;
    @Autowired
    ParsingUrlService parsingUrlService;

    @Autowired
    SendJyDqDataServiceImpl sendDqDownloadFileImpl;

    /**
     * 获取短期干预
     */
    @GetMapping(value = "/getShort")
    public R getPresetsShort(Long startTime, Long endTime,String stationCode) {
        List<DqInterveneDto> presetsShortDtoList = dqInterveneService.getShort(startTime, endTime,stationCode);
        return R.ok(presetsShortDtoList);

    }

    /**
     * 获取短期干预
     */
    @PostMapping(value = "/saveIntervene")
    public R savePresets(@RequestBody DqTempDto dqTempDto) {
        String stationCode = dqTempDto.getStationCode();
        List<DqInterveneDto> list = dqTempDto.getList();
        // 先删除干预表的数据记录
        list.sort(Comparator.comparing(DqInterveneDto::getForecastTime));
        // 干预日期
        String gyrq = DateFormatUtils.format(list.get(0).getForecastTime(), "yyyyMMdd");
        Date startTime = list.get(0).getForecastTime();
        Date endTime = list.get(list.size() - 1).getForecastTime();
        dqInterveneService.deleteShort(startTime, endTime,stationCode);
        // 封装干预实体，保存数据
        List<DqIntervene> presetsShortList = new ArrayList<>();
        for (DqInterveneDto dqInterveneDto : list) {
            if (dqInterveneDto.getPresetsPower() != null) {
                DqIntervene dqIntervene = new DqIntervene();
                dqIntervene.setForecastTime(dqInterveneDto.getForecastTime());
                dqIntervene.setActivePower(dqInterveneDto.getPresetsPower());
                dqIntervene.setStationCode(stationCode);
                presetsShortList.add(dqIntervene);
            }
        }
        dqInterveneService.saveBatch(presetsShortList);
        // 下发短期干预
        log.info(stationCode+"下发人工干预短期数据开始");

        LambdaQueryWrapper<ParsingUrl> urlWrapper = new LambdaQueryWrapper<>();
        urlWrapper.eq(ParsingUrl::getUrlStatus, "1");
        urlWrapper.eq(ParsingUrl::getStationCode, stationCode);
        List<ParsingUrl> parsingUrlList = parsingUrlService.list(urlWrapper);
        if (parsingUrlList.size()>0){
            for (ParsingUrl parsingUrl:parsingUrlList){
                // 获取ftp通道
                LambdaQueryWrapper<ParsingChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(ParsingChannel::getAnChannelType, "E1");
                lambdaQueryWrapper.eq(ParsingChannel::getUseStatus, "E1");
                lambdaQueryWrapper.eq(ParsingChannel::getId, parsingUrl.getCId());
                ParsingChannel parsingChannel = parsingChannelService.getOne(lambdaQueryWrapper);
                if (parsingChannel!=null){
                    sendDqDownloadFileImpl.sendDqDownloadFile(stationCode,parsingChannel.getUsername());
                }
            }
        }
        log.info(stationCode+"下发人工干预短期数据结束");
        return R.ok();

    }
}
