package com.jiayue.insu.inclientqn.service.client;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.jiayue.insu.inclientqn.constant.TemplateStant;
import com.jiayue.insu.inclientqn.entity.BackForeForecastData;
import com.jiayue.insu.inclientqn.entity.BackForeUsForecastData;
import com.jiayue.insu.inclientqn.entity.BackStatData;
import com.jiayue.insu.inclientqn.inenum.ElectricFieldTypeEnum;
import com.jiayue.insu.inclientqn.inenum.QNHLEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * @description: 回传数据请求生成
 * @author: yuanhao
 * @createDate: 2022/6/17
 * @version: 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@RefreshScope
public class BackDataGenBodyService {

    /**
     * 生成回传实际功率
     *
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateRealPowerData(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中实际功率不为空
        if (backStatDatas.get(0).getRealPower() != null) {
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_RealPower.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_RealPower.getCode());
            String title = TemplateStant.BACK_STAT_REAL_POWER_TITLE;
            // 拼接开始、title、内容、结束
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_STAT_REAL_POWER_TEMPLATE, backStatDatas.get(0).getRealPower().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }
        return content;
    }

    /**
     * 生成回传理论可用功率
     *
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateTheoryPowerData(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中实际功率不为空
        if (backStatDatas.get(0).getTheoryPower() != null && backStatDatas.get(0).getAvailablePower() != null){
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_TheoryPower.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_TheoryPower.getCode());
            String title = TemplateStant.BACK_STAT_THEORY_POWER_TITLE;
            // 拼接开始、title、内容、结束
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_STAT_THEORY_POWER_TEMPLATE, backStatDatas.get(0).getTheoryPower().toString(),backStatDatas.get(0).getAvailablePower().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }else {
            log.warn("回传数据-理论可用功率缺少数据");
        }
        return content;
    }

    /**
     *  生成回传两则准确率
     * @param backStatDatas
     * @return
     */
    public StringBuilder genStateAccuracyRate(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中实际功率不为空
        if (backStatDatas.get(0).getAccuracy() != null){
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_AccuracyRate.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_AccuracyRate.getCode());
            String title = TemplateStant.BACK_STAT_ACCURACY_RATE_TITLE;
            // 拼接开始、title、内容、结束
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_STAT_ACCURACY_RATE_TEMPLATE, backStatDatas.get(0).getAccuracy().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }else {
            log.warn("回传数据-两则准确率缺少数据");
        }
        return content;
    }

    /**
     *  生成回传限电信号
     * @param backStatDatas
     * @return
     */
    public StringBuilder genStateLimitPowerFlag(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中实际功率不为空
        if (backStatDatas.get(0).getPowerLimitSign() != null){
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_LimitPowerFlag.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_LimitPowerFlag.getCode());
            String title = TemplateStant.BACK_STAT_LIMIT_POWER_FLAG_TITLE;
            // 拼接开始、title、内容、结束
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_STAT_LIMIT_POWER_FLAG_TEMPLATE, backStatDatas.get(0).getPowerLimitSign().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }else {
            log.warn("回传数据-限电信号缺少数据");
        }
        return content;
    }

    /**
     * 生成回传实际气象
     *
     * @param stationType   场站类型
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateRealWeatherData(String stationType, List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        try {
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());
            // 拼接开始标签
            content.append(labelStart);
            String title;
            String dataContent;
            // 获取第一条统计数据
            BackStatData backStatData = backStatDatas.get(0);
            // 如果是风电场
            if (stationType.equals(ElectricFieldTypeEnum.E2.name())) {
                // 设置title和内容
                title = TemplateStant.BACK_STAT_REAL_WEATHER_TITLE_E2;
                dataContent = StrUtil.format(TemplateStant.BACK_STAT_REAL_WEATHER_TEMPLATE_E2, backStatData.getWs().toString(), backStatData.getWd().toString()
                        , backStatData.getTemperature().toString(), backStatData.getHumidity().toString(), backStatData.getPressure().toString());
            // 光伏
            } else {
                // 设置title和内容
                title = TemplateStant.BACK_STAT_REAL_WEATHER_TITLE_E1;
                dataContent = StrUtil.format(TemplateStant.BACK_STAT_REAL_WEATHER_TEMPLATE_E1, backStatData.getGlobalR().toString(), backStatData.getDirectR().toString()
                        , backStatData.getDiffuseR().toString(), backStatData.getTemperature().toString(), backStatData.getCellT().toString(),
                        backStatData.getHumidity().toString(), backStatData.getPressure().toString(), backStatData.getWs().toString(), backStatData.getWd().toString()
                );
            }
            // 拼接title、内容和结束标签
            content.append(title);
            content.append(dataContent);
            content.append(labelEnd);
        }catch (Exception e){
            log.warn("回传数据-实际气象信息缺少数据");
            content = new StringBuilder();
        }
        return content;
    }

    /**
     * 生成短期上报
     *
     * @param backStatDatas 结果集
     * @return RequestVo
     */
    public StringBuilder genStateForecastPowerReport(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中上报状态不为空
        if (backStatDatas.get(0).getReportStateForecast() != null) {
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_ForecastPowerReport.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_ForecastPowerReport.getCode());
            String title = TemplateStant.BACK_FORE_FORECAST_TITLE_REPORT;
            // 拼接开始、title、内容、结束标签
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_FORE_FORECAST_TEMPLATE_REPORT, backStatDatas.get(0).getReportStateForecast().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }
        return content;
    }

    /**
     * 生成超短期上报
     *
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateUltraShortTermForecastReport(List<BackStatData> backStatDatas) {
        StringBuilder content = new StringBuilder();
        // 如果统计数据中上报状态不为空
        if (backStatDatas.get(0).getReportStateForecastshort() != null) {
            // 获取开始、结束、title标签
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_UltraShortTermForecastReport.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_UltraShortTermForecastReport.getCode());
            String title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_REPORT;
            // 拼接开始、title、内容、结束标签
            content.append(labelStart);
            content.append(title);
            String dataContent = StrUtil.format(TemplateStant.BACK_FORE_US_FORECAST_TITLE_TEMPLATE, backStatDatas.get(0).getReportStateForecastshort().toString());
            content.append(dataContent);
            content.append(labelEnd);
        }
        return content;
    }

    /**
     * 根据超短期数据拼接字符串
     * @param stationType
     * @param backForeUsForecastDatas
     * @return
     */
    public StringBuilder genForeUltraShortTermForecast(String stationType, List<BackForeUsForecastData> backForeUsForecastDatas) {
        StringBuilder content = new StringBuilder();
        // 根据时间排序
        Collections.sort(backForeUsForecastDatas, Comparator.comparing(BackForeUsForecastData::getForecastTime));
        // 组装开始标签和结束标签
        String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_FORE_TYPE_UltraShortTermForecast.getCode());
        String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_FORE_TYPE_UltraShortTermForecast.getCode());
        String title;
        String template;
        // 拼接开始标签
        content.append(labelStart);
        // 如果是风电场
        if (stationType.equals(ElectricFieldTypeEnum.E2.name())) {
            title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_E2;
            template = TemplateStant.BACK_FORE_US_FORECAST_TEMPLATE_E2;
        // 光伏
        } else {
            title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_E1;
            template = TemplateStant.BACK_FORE_US_FORECAST_TEMPLATE_E1;
        }
        // 拼接title
        content.append(title);
        // 循环拼接超短期数据
        for (int i = 0; i < backForeUsForecastDatas.size(); i++) {
            BackForeUsForecastData b = backForeUsForecastDatas.get(i);
            String format = LocalDateTimeUtil.format(b.getForecastTime(), DatePattern.PURE_DATETIME_PATTERN);
            String dataContent = StrUtil.format(template, i, format, b.getForecastValue(), b.getForecastOpenCap());
            content.append(dataContent);
        }
        // 拼接结束标签
        content.append(labelEnd);
        return content;
    }

    /**
     * 根据短期数据拼接字符串
     * @param backForeForecastDatas
     * @return
     */
    public StringBuilder genForeForecast(List<BackForeForecastData> backForeForecastDatas) {
        StringBuilder content = new StringBuilder();
        // 根据预测时间排序
        Collections.sort(backForeForecastDatas, Comparator.comparing(BackForeForecastData::getForecastTime));
        // 组装开始标签、结束标签、title
        String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_FORE_TYPE_ForecastPower.getCode());
        String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_FORE_TYPE_ForecastPower.getCode());
        String title = TemplateStant.BACK_FORE_FORECAST_TITLE;
        content.append(labelStart);
        content.append(title);
        // 循环拼接短期数据
        for (int i = 0; i < backForeForecastDatas.size(); i++) {
            BackForeForecastData b = backForeForecastDatas.get(i);
            String format = LocalDateTimeUtil.format(b.getForecastTime(), DatePattern.PURE_DATETIME_PATTERN);
            String dataContent = StrUtil.format(TemplateStant.BACK_FORE_FORECAST_TEMPLATE, i + 1, format, b.getForecastValue(), b.getForecastOpenCap());
            content.append(dataContent);
        }
        // 拼接结束标签
        content.append(labelEnd);
        return content;
    }
}
