package com.njcn.cloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.cloud.enums.app.TypeCodeEnum;
import com.njcn.cloud.mapper.AppSteadyMsgMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.cloud.mapper.AppSteadyUrlMapper;
import com.njcn.cloud.pojo.dto.AppSteadyAss;
import com.njcn.cloud.pojo.dto.AppSteadyMsg;
import com.njcn.cloud.pojo.dto.AppSteadyUrl;
import com.njcn.cloud.pojo.vo.AppSteadyMsgVO;
import com.njcn.cloud.pojo.vo.SteadyMsgDetailVO;
import com.njcn.cloud.pojo.vo.SteadyTargetVO;
import com.njcn.cloud.pojo.vo.SteadyUrlData;
import com.njcn.cloud.service.IAppSteadyAssService;
import com.njcn.cloud.service.IAppSteadyMsgService;
import com.njcn.cloud.service.IAppSteadyUrlService;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailVO;
import com.njcn.event.file.component.WavePicComponent;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 稳态消息 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-11-03
 */
@Service
@RequiredArgsConstructor
public class AppSteadyMsgServiceImpl extends ServiceImpl<AppSteadyMsgMapper, AppSteadyMsg> implements IAppSteadyMsgService {

    private final IAppSteadyAssService appSteadyAssService;
    private final AppSteadyUrlMapper steadyUrlDataMapper;
    private final IAppSteadyUrlService appSteadyUrlService;
    private final LineFeignClient lineFeignClient;
    private final WavePicComponent wavePicComponent;
    @Override
    public AppSteadyMsgVO getSteadyState(BaseParam param) {
        AppSteadyMsgVO appSteadyMsgVO=new AppSteadyMsgVO();
        Page<AppSteadyMsg> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), new LambdaQueryWrapper<AppSteadyMsg>()
                .eq(AppSteadyMsg::getUserIndex, param.getSearchValue())
                .orderByDesc(AppSteadyMsg::getTimeId)
        );
        int count = this.count(new LambdaQueryWrapper<AppSteadyMsg>()
                .eq(AppSteadyMsg::getUserIndex, param.getSearchValue())
                .eq(AppSteadyMsg::getState, 0)

        );
        appSteadyMsgVO.setPage(page);
        appSteadyMsgVO.setUnstate(count);
        return appSteadyMsgVO;
    }

    @Override
    public List<SteadyMsgDetailVO> getSteadyDetail(String steadyIndex) {
        //初始化集合
        List<SteadyMsgDetailVO> msgDetailList = new ArrayList<>();
        //获取暂态消息详细信息列表
        List<AppSteadyAss> steadyAssList = appSteadyAssService.list(new LambdaQueryWrapper<AppSteadyAss>()
                .eq(AppSteadyAss::getSteadyIndex, steadyIndex)
        );
        if(CollUtil.isEmpty(steadyAssList)){
            return msgDetailList;
        }
        //获取监测点id集合
        List<String> lineIds = steadyAssList.stream().map(AppSteadyAss::getLineIndex).distinct().collect(Collectors.toList());
        //获取统计时间集合
        List<LocalDateTime> times = steadyAssList.stream().map(AppSteadyAss::getTimeId).distinct().collect(Collectors.toList());

        List<AppSteadyUrl> lineDetail = appSteadyUrlService.list(new LambdaQueryWrapper<AppSteadyUrl>()
                .in(AppSteadyUrl::getLineIndex, lineIds)
                .in(AppSteadyUrl::getTimeId, times)
                .orderByAsc(AppSteadyUrl::getTypeCode)

        );
        Map<String, List<AppSteadyUrl>> lineMap = lineDetail.stream()
                .collect(Collectors.groupingBy(x->x.getLineIndex()+"_"+LocalDateTimeUtil.format(x.getTimeId(), DatePattern.UTC_SIMPLE_MS_PATTERN)));
        List<String> steadyLineIdList = lineMap.keySet().stream().map(x->x.split("_")[0]).collect(Collectors.toList());

        List<LineDetailVO.Detail> lineDetailVOS = lineFeignClient.getLineDetailByIds(steadyLineIdList).getData();


        lineMap.forEach((key,value)->{
            String[] split = key.split("_");
            List<LineDetailVO.Detail> line = lineDetailVOS.stream().filter(x -> x.getLineId().equals(split[0])).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(line)){
                SteadyMsgDetailVO detailVO=new SteadyMsgDetailVO();
                detailVO.setLineName(line.get(0).getLineName());
                detailVO.setSubName(line.get(0).getSubName());
                detailVO.setSteadyIndex(steadyIndex);
                detailVO.setLineIndex(line.get(0).getLineId());
                detailVO.setTimeId(LocalDateTimeUtil.parse(split[1],DatePattern.UTC_SIMPLE_MS_PATTERN));
                String describe = "";
                Integer limitNum = 0;
                for (AppSteadyUrl appSteadyUrl : value) {
                    String typename = TypeCodeEnum.getMsgByCode(appSteadyUrl.getTypeCode());
                    if(appSteadyUrl.getTypeCode()== 5){
                        describe = describe + typename + appSteadyUrl.getLimitNum() * 120 + "分钟,";
                    }else{
                        describe = describe + typename + appSteadyUrl.getLimitNum() * line.get(0).getTimeInterval() + "分钟,";
                    }
                    limitNum = limitNum + appSteadyUrl.getLimitNum();
                }
                if(describe.length() > 0){
                    describe = describe.substring(0, describe.length()-1);
                }
                detailVO.setLimitNum(limitNum);
                detailVO.setDescribe(describe);
                msgDetailList.add(detailVO);
            }
        });
        this.update(new LambdaUpdateWrapper<AppSteadyMsg>()
                .set(AppSteadyMsg::getState,1)
                .eq(AppSteadyMsg::getState,0)
                .eq(AppSteadyMsg::getSteadyIndex,steadyIndex)
        );
        return msgDetailList;
    }

    @Override
    public List<SteadyTargetVO> getSteadyTarget(String lineIndex, String timeId) {
        List<SteadyTargetVO> info=new ArrayList<>();
        SteadyTargetVO vo;
        List<AppSteadyUrl> list = appSteadyUrlService.list(new LambdaQueryWrapper<AppSteadyUrl>()
                .eq(AppSteadyUrl::getLineIndex, lineIndex)
                .eq(AppSteadyUrl::getTimeId, timeId)
                .orderByAsc(AppSteadyUrl::getTypeCode)
        );
        if(CollUtil.isNotEmpty(list)){
            for (AppSteadyUrl appSteadyUrl : list) {
                vo=new SteadyTargetVO();
                vo.setTypeCode(appSteadyUrl.getTypeCode());
                vo.setTypeName(TypeCodeEnum.getMsgByCode(appSteadyUrl.getTypeCode()));
                vo.setLimitNum(appSteadyUrl.getLimitNum());
                info.add(vo);
            }

        }
        return info;
    }

    @Override
    public AppSteadyUrl getSteadyTargetUrl(String lineIndex, String timeId,Integer typeCode) {
        AppSteadyUrl one = appSteadyUrlService.getOne(new LambdaQueryWrapper<AppSteadyUrl>()
                .eq(AppSteadyUrl::getLineIndex, lineIndex)
                .eq(AppSteadyUrl::getTimeId, timeId)
                .eq(AppSteadyUrl::getTypeCode, typeCode)
        );
        return one;
    }

    @Override
    public AppSteadyUrl getUrl(String lineIndex, String timeId, Integer typeCode) {
        List<List<SteadyUrlData>> result = new ArrayList<>();
        List<SteadyUrlData> data;
        List<SteadyUrlData> dataa;
        List<SteadyUrlData> datab;
        List<SteadyUrlData> datac;
        AppSteadyUrl ass = new AppSteadyUrl();
        String type="";
        float limit=0;
        //开始时间
        String startTime = DateUtil.beginOfDay(DateUtil.parse(timeId)).toString();
        //结束时间
        String endTime = DateUtil.endOfDay(DateUtil.parse(timeId)).toString();

        Overlimit overLimit = lineFeignClient.getOverLimitData(lineIndex).getData();

        if(typeCode == TypeCodeEnum.FREQ_DEV_OVERTIME.getCode()){
            data = steadyUrlDataMapper.getFreqDev(lineIndex,startTime,endTime);
            type = "频率偏差";
            limit = overLimit.getFreqDev();
            result.add(data);
        }else if(typeCode == TypeCodeEnum.VOLTAGE_DEV_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVUDev(lineIndex,startTime,endTime,"A");
            datab = steadyUrlDataMapper.getVUDev(lineIndex,startTime,endTime,"B");
            datac = steadyUrlDataMapper.getVUDev(lineIndex,startTime,endTime,"C");
            type = "电压偏差";
            limit = overLimit.getVoltageDev();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UBALANCE_OVERTIME.getCode()){
            data = steadyUrlDataMapper.getVUnbalance(lineIndex,startTime,endTime);
            type = "电压不平衡";
            limit = overLimit.getUbalance();
            result.add(data);
        }else if(typeCode == TypeCodeEnum.FLICKER_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getPLT(lineIndex,startTime,endTime,"A");
            datab = steadyUrlDataMapper.getPLT(lineIndex,startTime,endTime,"B");
            datac = steadyUrlDataMapper.getPLT(lineIndex,startTime,endTime,"C");
            type = "长闪";
            limit = overLimit.getFlicker();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UABERRANCE_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVTHD(lineIndex,startTime,endTime,"A");
            datab = steadyUrlDataMapper.getVTHD(lineIndex,startTime,endTime,"B");
            datac = steadyUrlDataMapper.getVTHD(lineIndex,startTime,endTime,"C");
            type = "电压谐波畸变率";
            limit = overLimit.getUaberrance();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_2_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"2","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"2","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"2","C");
            type = "2次电压谐波含有率";
            limit = overLimit.getUharm2();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_3_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"3","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"3","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"3","C");
            type = "3次电压谐波含有率";
            limit = overLimit.getUharm3();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_4_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"4","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"4","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"4","C");
            type = "4次电压谐波含有率";
            limit = overLimit.getUharm4();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_5_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"5","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"5","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"5","C");
            type = "5次电压谐波含有率";
            limit = overLimit.getUharm5();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_6_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"6","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"6","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"6","C");
            type = "6次电压谐波含有率";
            limit = overLimit.getUharm6();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_7_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"7","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"7","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"7","C");
            type = "7次电压谐波含有率";
            limit = overLimit.getUharm7();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_8_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"8","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"8","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"8","C");
            type = "8次电压谐波含有率";
            limit = overLimit.getUharm8();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_9_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"9","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"9","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"9","C");
            type = "9次电压谐波含有率";
            limit = overLimit.getUharm9();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_10_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"10","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"10","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"10","C");
            type = "10次电压谐波含有率";
            limit = overLimit.getUharm10();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_11_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"11","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"11","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"11","C");
            type = "11次电压谐波含有率";
            limit = overLimit.getUharm11();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_12_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"12","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"12","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"12","C");
            type = "12次电压谐波含有率";
            limit = overLimit.getUharm12();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_13_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"13","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"13","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"13","C");
            type = "13次电压谐波含有率";
            limit = overLimit.getUharm13();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_14_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"14","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"14","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"14","C");
            type = "14次电压谐波含有率";
            limit = overLimit.getUharm14();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_15_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"15","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"15","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"15","C");
            type = "15次电压谐波含有率";
            limit = overLimit.getUharm15();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_16_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"16","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"16","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"16","C");
            type = "16次电压谐波含有率";
            limit = overLimit.getUharm16();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_17_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"17","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"17","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"17","C");
            type = "17次电压谐波含有率";
            limit = overLimit.getUharm17();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_18_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"18","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"18","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"18","C");
            type = "18次电压谐波含有率";
            limit = overLimit.getUharm18();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_19_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"19","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"19","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"19","C");
            type = "19次电压谐波含有率";
            limit = overLimit.getUharm19();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_20_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"20","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"20","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"20","C");
            type = "20次电压谐波含有率";
            limit = overLimit.getUharm20();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_21_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"21","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"21","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"21","C");
            type = "21次电压谐波含有率";
            limit = overLimit.getUharm21();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_22_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"22","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"22","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"22","C");
            type = "22次电压谐波含有率";
            limit = overLimit.getUharm22();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_23_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"23","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"23","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"23","C");
            type = "23次电压谐波含有率";
            limit = overLimit.getUharm23();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_24_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"24","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"24","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"24","C");
            type = "24次电压谐波含有率";
            limit = overLimit.getUharm24();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.UHARM_25_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"25","A");
            datab = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"25","B");
            datac = steadyUrlDataMapper.getVHarmRate(lineIndex,startTime,endTime,"25","C");
            type = "25次电压谐波含有率";
            limit = overLimit.getUharm25();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_2_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"2","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"2","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"2","C");
            type = "2次电流谐波幅值";
            limit = overLimit.getIharm2();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_3_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"3","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"3","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"3","C");
            type = "3次电流谐波幅值";
            limit = overLimit.getIharm3();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_4_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"4","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"4","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"4","C");
            type = "4次电流谐波幅值";
            limit = overLimit.getIharm4();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_5_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"5","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"5","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"5","C");
            type = "5次电流谐波幅值";
            limit = overLimit.getIharm5();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_6_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"6","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"6","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"6","C");
            type = "6次电流谐波幅值";
            limit = overLimit.getIharm6();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_7_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"7","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"7","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"7","C");
            type = "7次电流谐波幅值";
            limit = overLimit.getIharm7();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_8_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"8","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"8","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"8","C");
            type = "8次电流谐波幅值";
            limit = overLimit.getIharm8();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_9_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"9","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"9","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"9","C");
            type = "9次电流谐波幅值";
            limit = overLimit.getIharm9();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_10_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"10","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"10","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"10","C");
            type = "10次电流谐波幅值";
            limit = overLimit.getIharm10();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_11_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"11","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"11","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"11","C");
            type = "11次电流谐波幅值";
            limit = overLimit.getIharm11();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_12_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"12","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"12","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"12","C");
            type = "12次电流谐波幅值";
            limit = overLimit.getIharm12();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_13_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"13","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"13","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"13","C");
            type = "13次电流谐波幅值";
            limit = overLimit.getIharm13();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_14_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"14","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"14","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"14","C");
            type = "14次电流谐波幅值";
            limit = overLimit.getIharm14();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_15_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"15","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"15","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"15","C");
            type = "15次电流谐波幅值";
            limit = overLimit.getIharm15();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_16_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"16","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"16","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"16","C");
            type = "16次电流谐波幅值";
            limit = overLimit.getIharm16();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_17_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"17","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"17","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"17","C");
            type = "17次电流谐波幅值";
            limit = overLimit.getIharm17();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_18_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"18","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"18","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"18","C");
            type = "18次电流谐波幅值";
            limit = overLimit.getIharm18();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_19_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"19","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"19","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"19","C");
            type = "19次电流谐波幅值";
            limit = overLimit.getIharm19();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_20_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"20","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"20","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"20","C");
            type = "20次电流谐波幅值";
            limit = overLimit.getIharm20();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_21_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"21","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"21","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"21","C");
            type = "21次电流谐波幅值";
            limit = overLimit.getIharm21();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_22_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"22","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"22","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"22","C");
            type = "22次电流谐波幅值";
            limit = overLimit.getIharm22();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_23_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"23","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"23","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"23","C");
            type = "23次电流谐波幅值";
            limit = overLimit.getIharm23();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_24_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"24","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"24","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"24","C");
            type = "24次电流谐波幅值";
            limit = overLimit.getIharm24();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }else if(typeCode == TypeCodeEnum.IHARM_25_OVERTIME.getCode()){
            dataa = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"25","A");
            datab = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"25","B");
            datac = steadyUrlDataMapper.getIHarmPhasic(lineIndex,startTime,endTime,"25","C");
            type = "25次电流谐波幅值";
            limit = overLimit.getIharm25();
            result.add(dataa);
            result.add(datab);
            result.add(datac);
        }

        if(result != null && result.size()>0){
            String option=saveImg(result,lineIndex,type,limit);
            if(StrUtil.isBlank(option)){
                throw new BusinessException("文件上传异常");
            }
            appSteadyUrlService.update(new LambdaUpdateWrapper<AppSteadyUrl>()
                    .set(AppSteadyUrl::getSteadyPath,option)
                    .eq(AppSteadyUrl::getTypeCode,typeCode)
                    .eq(AppSteadyUrl::getLineIndex, lineIndex)
                    .eq(AppSteadyUrl::getTimeId, timeId));
            ass = appSteadyUrlService.getOne(new LambdaQueryWrapper<AppSteadyUrl>()
                    .eq(AppSteadyUrl::getTypeCode,typeCode)
                    .eq(AppSteadyUrl::getLineIndex, lineIndex)
                    .eq(AppSteadyUrl::getTimeId, timeId));
        };
        return ass;
    }

    public String saveImg(List<List<SteadyUrlData>> result, String lineIndex, String typeName, float limit) {
        // 变量
        List<List<Object>> values = new ArrayList<>();
        List<List<Object>> valueA = new ArrayList<>();
        List<List<Object>> valueB = new ArrayList<>();
        List<List<Object>> valueC = new ArrayList<>();
        List<List<Object>> limits = new ArrayList<>();

        List<LineDetailVO.Detail> lineDetail = lineFeignClient.getLineDetailByIds(Arrays.asList(lineIndex)).getData();
        if(CollUtil.isEmpty(lineDetail)){
            throw new BusinessException("监测点信息不存在，请检查的监测点编号");

        }
        Integer ptType = lineDetail.get(0).getPtType();

        String option="";
        if(result.size() == 1){
            for(SteadyUrlData tmp:result.get(0)){
                List<Object> a = new ArrayList() {{
                    add(tmp.getTime());
                    add(tmp.getValue());
                }};
                values.add(a);
                List<Object> aa = new ArrayList() {{
                    add(tmp.getTime());
                    add(limit);
                }};
                limits.add(aa);
            }
            // 模板参数
            HashMap<String, List<List<Object>>> datas = new LinkedHashMap<>();
            datas.put("实际值", values);
            String limitName="限值(" + limit + ")";
            // 生成返回地址
            option = wavePicComponent.generateSteadyTargetImage(typeName,datas,limitName,limits);
        }else{
            for(SteadyUrlData tmpA:result.get(0)){
                List<Object> a = new ArrayList() {{
                    add(tmpA.getTime());
                    add(tmpA.getValue());
                }};
                valueA.add(a);
                List<Object> aa = new ArrayList() {{
                    add(tmpA.getTime());
                    add(limit);
                }};
                limits.add(aa);
            }

            for(SteadyUrlData tmpB:result.get(1)){
                List<Object> b = new ArrayList() {{
                    add(tmpB.getTime());
                    add(tmpB.getValue());
                }};
                valueB.add(b);
            }

            for(SteadyUrlData tmpC:result.get(2)){
                List<Object> c = new ArrayList() {{
                    add(tmpC.getTime());
                    add(tmpC.getValue());
                }};
                valueC.add(c);
            }
            // 模板参数
            HashMap<String, List<List<Object>>> datas = new LinkedHashMap<>();
            String limitName="限值(" + limit + ")";
            // 生成option字符串
            if(typeName.lastIndexOf("电流谐波幅值") != -1){
                datas.put("A相",valueA);
                datas.put("B相",valueB);
                datas.put("C相",valueC);
                option = wavePicComponent.generateSteadyTargetImage(typeName,datas,limitName,limits);
            }else if(ptType == 0){
                datas.put("A相",valueA);
                datas.put("B相",valueB);
                datas.put("C相",valueC);
                option = wavePicComponent.generateSteadyTargetImage(typeName,datas,limitName,limits);
            }else if(ptType == 1){
                datas.put("AB相",valueA);
                datas.put("BC相",valueB);
                datas.put("CA相",valueC);
                option = wavePicComponent.generateSteadyTargetImage(typeName,datas,limitName,limits);
            }else if(ptType == 2){
                datas.put("AB相",valueA);
                datas.put("BC相",valueB);
                option = wavePicComponent.generateSteadyTargetImage(typeName,datas,limitName,limits);
            }
        }

        return option;
    }

}
