package com.jiayue.ipfcst.common.data.service.uploadfilerule;

import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.SysParameter;
import com.jiayue.ipfcst.common.data.repository.SysParameterRepository;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 湖北文件生成规则
 *
 * @author xsl
 * @version 3.0
 */
@Service
public class E42UploadFileRuleService extends UploadFileRuleCommonService implements IUploadFileRuleService {
    @Autowired
    SysParameterRepository sysParameterRepository;

    /**
     * 指定日期应该上报文件名称
     *
     * @param date
     * @param fileType
     * @return
     */
    public List<String> getShouldFile(ElectricField electricFieldInfo, Date date, String fileType, String cronStr) {
        // 计算开始结束时间
        super.caleTime(date);
        Long startTime = 0L;
        String fileName = "";
        List<String> shouldFileNameList = new ArrayList<>();
        Date currentDate = new Date();
        int cdqUpMin = 0;
        switch (fileType) {
            case "E1":
                if (DateFormatUtils.format(super.endTime, "yyyyMMdd").equals(DateFormatUtils.format(currentDate, "yyyyMMdd"))) {
                    // 当天的
                    if (cronStr != null && !"".equals(cronStr)) {
                        if (!isSpecifiedTimeShouldFile(currentDate, cronStr)) {
                            break;
                        }
                    }
                }
                // 短期
                startTime = DateMomentUtil.getDayStartTime(DateUtils.addDays(date, 1).getTime());
                fileName = getFileNameRule(electricFieldInfo, "E1", startTime);
                shouldFileNameList.add(fileName);
                break;
            case "E2":
                try {
                    Optional<SysParameter> optional = sysParameterRepository.findBySysKey("CDQ_UP_MIN");
                    cdqUpMin = Integer.parseInt(optional.map(SysParameter::getSysValue).orElse("0"));
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
                // 可用超短期
                midNightTime = midNightTime + cdqUpMin * 1000 * 60;
                endTime = endTime + cdqUpMin * 1000 * 60;
                for (Long tempDateTime = midNightTime; tempDateTime < endTime; tempDateTime = tempDateTime + 1 * 60 * 1000) {
                    if (tempDateTime % (15 * 60 * 1000) == 0) {
                        // 整15分钟
                        if (DateFormatUtils.format(super.endTime, "yyyyMMdd").equals(DateFormatUtils.format(currentDate, "yyyyMMdd"))) {
                            // 此判断为了当前时刻应上报的文件还没执行定时，产生的多余的应上报
                            if (cronStr != null && !"".equals(cronStr)) {
                                if (!isEveryTimeShouldFile(tempDateTime, currentDate, cronStr, 15)) {
                                    continue;
                                }
                            }
                        }
                        try {
                            fileName = getFileNameRule(electricFieldInfo, "E2", DateMomentUtil.getMomentTime(tempDateTime + 15 * 60 * 1000, 1, 15 * 60 * 1000L));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        shouldFileNameList.add(fileName);
                    }
                }
                break;
            case "E6":
                // 气象站
                for (Long tempDateTime = super.midNightTime; tempDateTime < super.endTime; tempDateTime = tempDateTime + 5 * 60 * 1000) {
                    if (DateFormatUtils.format(super.endTime, "yyyyMMdd").equals(DateFormatUtils.format(currentDate, "yyyyMMdd"))) {
                        // 此判断为了当前时刻应上报的文件还没执行定时，产生的多余的应上报
                        if (cronStr != null && !"".equals(cronStr)) {
                            if (!isEveryTimeShouldFile(tempDateTime, currentDate, cronStr, 5)) {
                                continue;
                            }
                        }
                    }
                    fileName = getFileNameRule(electricFieldInfo, "E6", tempDateTime);
                    shouldFileNameList.add(fileName);
                }
                break;
            case "E7":
                // 测风塔
                for (Long tempDateTime = super.midNightTime; tempDateTime < super.endTime; tempDateTime = tempDateTime + 5 * 60 * 1000) {
                    if (DateFormatUtils.format(super.endTime, "yyyyMMdd").equals(DateFormatUtils.format(currentDate, "yyyyMMdd"))) {
                        // 此判断为了当前时刻应上报的文件还没执行定时，产生的多余的应上报
                        if (cronStr != null && !"".equals(cronStr)) {
                            if (!isEveryTimeShouldFile(tempDateTime, currentDate, cronStr, 5)) {
                                continue;
                            }
                        }
                    }
                    fileName = getFileNameRule(electricFieldInfo, "E7", tempDateTime);
                    shouldFileNameList.add(fileName);
                }
                break;
            default:
                break;
        }
        return shouldFileNameList;
    }

    /**
     * 文件名称生成规则
     *
     * @param electricFieldInfo
     * @param fileType
     * @param fileNameTime
     * @return
     */
    public String getFileNameRule(ElectricField electricFieldInfo, String fileType, Long fileNameTime) {
        String fileName = "";
        switch (fileType) {
            case "E1":
                // 短期
                fileName = electricFieldInfo.getSign() + "_" + DateFormatUtils.format(fileNameTime, "yyyyMMdd") + "_0000_DQ.PVD";
                break;
            case "E2":
                // 超短期
                fileName = electricFieldInfo.getSign() + "_" + DateFormatUtils.format(fileNameTime, "yyyyMMdd_HHmm") + "_CDQ.PVD";
                break;
            case "E6":
                // 气象站
                fileName = electricFieldInfo.getSign() + "_" + DateFormatUtils.format(fileNameTime, "yyyyMMdd_HHmm") + "_QXZ.PVD";
                break;
            case "E7":
                // 测风塔
                fileName = electricFieldInfo.getSign() + "_" + DateFormatUtils.format(fileNameTime, "yyyyMMdd_HHmm") + "_CFT.PVD";
                break;
            default:
                break;
        }
        return fileName;
    }
}
