 package com.huatek.busi.service.impl.gis;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.EvaluationNameDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.service.param.BusiParamTransformerService;
import com.huatek.busi.vo.*;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.GisAssessmentResultDao;
import com.huatek.busi.mapper.GisAssessmentResultMapper;
import com.huatek.busi.service.gis.GisAssessmentResultService;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

 /**
 * Gis评估
 * @author buck_guo
 * @date 2019/08/31
 */
@Component
@Slf4j
@Service(interfaceClass = GisAssessmentResultService.class)
@Transactional(rollbackFor = Exception.class)
public class GisAssessmentResultServiceImpl implements GisAssessmentResultService {

    @Autowired
    private GisAssessmentResultDao gisAssessmentResultDao;
    @Autowired
    private GisAssessmentResultMapper gisAssessmentResultMapper;

    @Reference(check = false)
    BusiParamTransformerService busiParamTransformerService;
    @Override
    public int saveGisAssessmentResult(GisAssessmentResult gisAssessmentResult) {
        return gisAssessmentResultDao.add(gisAssessmentResult);
    }

    @Override
    public PageInfo<GisAssessmentResultVo> findGisAssessmentByMainTreeId(String siteId, String mainTreeId, Integer page, Integer limit, LocalDate startTime, LocalDate endTime, String type) {
//        return gisAssessmentResultMapper.findGisAssessmentByMainTreeId(siteId,mainTreeId);
    	return PageHelper.startPage(page == null ? 1 :page , limit == null? 1 : limit)
		        .doSelectPageInfo(() -> gisAssessmentResultMapper.findGisAssessmentByMainTreeId(siteId,mainTreeId,startTime,endTime,type));
    }

     @Override
     public Map<String, Object> syncParams(String token, String siteId, String mainTreeId, String number){
         List<BusiParamTransformerVoluntarilyVo> busiParamGisVo = null;
         String eqType = gisAssessmentResultMapper.findEqType(siteId,mainTreeId);
        if(Objects.equals(eqType, SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())) {
            busiParamGisVo = gisAssessmentResultMapper.findBusiGISParam(siteId, mainTreeId, number);
        }else if (Objects.equals(eqType, SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())){
            busiParamGisVo = gisAssessmentResultMapper.findBusiByqParam(siteId, mainTreeId, number);
        }
         busiParamTransformerService.saveOrUpdateBusiParamTransformer(token, busiParamGisVo);
         return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", "");
     }

//     public static void main(String[] args) {
//         String steps = "[{\"calculateResult\":\"0.1074\",\"inputParam\":{\"权重\":\"0.1074\",\"b值\":\"150.0000\",\"a值\":\"50.0000\",\"输入值\":\"0.1\"},\"name\":\"H2\",\"pname\":\"YSP\",\"stepId\":1,\"stepName\":\"氢气\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.136\",\"inputParam\":{\"权重\":\"0.1360\",\"b值\":\"570.0000\",\"a值\":\"315.0000\",\"输入值\":\"0.6\"},\"name\":\"CO\",\"pname\":\"YSP\",\"stepId\":2,\"stepName\":\"CO\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1862\",\"inputParam\":{\"权重\":\"0.1862\",\"b值\":\"4000.0000\",\"a值\":\"2500.0000\",\"输入值\":\"0.7\"},\"name\":\"CO2\",\"pname\":\"YSP\",\"stepId\":3,\"stepName\":\"CO2\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1146\",\"inputParam\":{\"权重\":\"0.1146\",\"b值\":\"100.0000\",\"a值\":\"10.0000\",\"输入值\":\"0.2\"},\"name\":\"CH4\",\"pname\":\"YSP\",\"stepId\":4,\"stepName\":\"CH4\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1512\",\"inputParam\":{\"权重\":\"0.1814\",\"b值\":\"3.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.5\"},\"name\":\"C2H2\",\"pname\":\"YSP\",\"stepId\":5,\"stepName\":\"C2H2\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0811\",\"inputParam\":{\"权重\":\"0.0812\",\"b值\":\"150.0000\",\"a值\":\"30.0000\",\"输入值\":\"0.4\"},\"name\":\"C2H4\",\"pname\":\"YSP\",\"stepId\":6,\"stepName\":\"C2H4\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0811\",\"inputParam\":{\"权重\":\"0.0812\",\"b值\":\"105.0000\",\"a值\":\"10.0000\",\"输入值\":\"0.3\"},\"name\":\"C2H6\",\"pname\":\"YSP\",\"stepId\":7,\"stepName\":\"C2H6\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1122\",\"inputParam\":{\"权重\":\"0.1122\",\"b值\":\"180.0000\",\"a值\":\"80.0000\",\"输入值\":\"0.8\"},\"name\":\"ZT\",\"pname\":\"YSP\",\"stepId\":8,\"stepName\":\"总烃\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1434\",\"inputParam\":{\"权重\":\"0.1479\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.9697999999999999\"},\"name\":\"YSP\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":9,\"stepName\":\"变压器绝缘试验-油色谱试验\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.2885\",\"inputParam\":{\"权重\":\"0.2885\",\"b值\":\"25.0000\",\"a值\":\"15.0000\",\"输入值\":\"7.6\"},\"name\":\"YZSY_WS\",\"pname\":\"YZSY\",\"stepId\":10,\"stepName\":\"油中微水\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.3365\",\"inputParam\":{\"权重\":\"0.3365\",\"b值\":\"0.0400\",\"a值\":\"0.0100\",\"输入值\":\"0.0\"},\"name\":\"YZSY_YJS\",\"pname\":\"YZSY\",\"stepId\":11,\"stepName\":\"油介损\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.375\",\"inputParam\":{\"权重\":\"0.3750\",\"b值\":\"52.0000\",\"a值\":\"30.0000\",\"输入值\":\"77.7\"},\"name\":\"YZSY_YJCDY\",\"pname\":\"YZSY\",\"stepId\":12,\"stepName\":\"油击穿电压（Kv)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1608\",\"inputParam\":{\"权重\":\"0.1608\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"1.0\"},\"name\":\"YZSY\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":13,\"stepName\":\"变压器绝缘试验-油质试验\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0000\",\"inputParam\":{\"权重\":\"0.1479\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.0\"},\"name\":\"KQZB\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":14,\"stepName\":\"变压器绝缘试验-糠醛指标\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.1294\",\"inputParam\":{\"权重\":\"0.1294\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"1.0\"},\"name\":\"JYZ\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":15,\"stepName\":\"变压器绝缘试验-绝缘纸试验\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.2233\",\"b值\":\"0.0100\",\"a值\":\"0.0000\",\"输入值\":\"0.3\"},\"name\":\"ZDXSY_YJZHSL\",\"pname\":\"ZDXSY\",\"stepId\":16,\"stepName\":\"绝缘纸含水量（质量分数%）\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.1100\",\"b值\":\"0.1000\",\"a值\":\"0.0000\",\"输入值\":\"99.0\"},\"name\":\"ZDXSY_KZDL_KZSHL\",\"pname\":\"ZDXSY\",\"stepId\":17,\"stepName\":\"空载电流、空载损耗变化率(%)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.2333\",\"b值\":\"0.1000\",\"a值\":\"0.0000\",\"输入值\":\"15.2\"},\"name\":\"ZDXSY_DLZKBHL\",\"pname\":\"ZDXSY\",\"stepId\":18,\"stepName\":\"短路阻抗变化率(%)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.2397\",\"inputParam\":{\"权重\":\"0.2667\",\"b值\":\"100.0000\",\"a值\":\"0.0000\",\"输入值\":\"10.1\"},\"name\":\"ZDXSY_TXJDDL\",\"pname\":\"ZDXSY\",\"stepId\":19,\"stepName\":\"铁芯接地电流(mA)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.1667\",\"b值\":\"0.0020\",\"a值\":\"0.0000\",\"输入值\":\"65.0\"},\"name\":\"ZDXSY_RZZL_XLDL\",\"pname\":\"ZDXSY\",\"stepId\":20,\"stepName\":\"绕组直流泄露电流（变化率%）\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0341\",\"inputParam\":{\"权重\":\"0.1423\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.2397\"},\"name\":\"ZDXSY\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":21,\"stepName\":\"变压器绝缘试验-诊断性试验\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.1325\",\"inputParam\":{\"权重\":\"0.1325\",\"b值\":\"2.4700\",\"a值\":\"1.5000\",\"输入值\":\"6.0\"},\"name\":\"LXSY_JHZS\",\"pname\":\"LXSY\",\"stepId\":22,\"stepName\":\"极化指数\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0818\",\"inputParam\":{\"权重\":\"0.0904\",\"b值\":\"300.0000\",\"a值\":\"35.0000\",\"输入值\":\"60.0\"},\"name\":\"LXSY_JFFDL\",\"pname\":\"LXSY\",\"stepId\":23,\"stepName\":\"局放放电量\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.1325\",\"b值\":\"1600.0000\",\"a值\":\"800.0000\",\"输入值\":\"354.0\"},\"name\":\"LXSY_JYDZ\",\"pname\":\"LXSY\",\"stepId\":24,\"stepName\":\"绝缘电阻\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1124\",\"inputParam\":{\"权重\":\"0.1125\",\"b值\":\"1.5000\",\"a值\":\"1.0000\",\"输入值\":\"5.0\"},\"name\":\"LXSY_JYDZ_SHB\",\"pname\":\"LXSY\",\"stepId\":25,\"stepName\":\"绝缘电阻吸收比\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.1345\",\"b值\":\"2.0000\",\"a值\":\"1.0000\",\"输入值\":\"77.0\"},\"name\":\"LXSY_RZZL_DZXJC\",\"pname\":\"LXSY\",\"stepId\":26,\"stepName\":\"绕组直流电阻相间差%\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1767\",\"inputParam\":{\"权重\":\"0.1767\",\"b值\":\"1.0000\",\"a值\":\"0.6000\",\"输入值\":\"0.5\"},\"name\":\"LXSY_RZZL_DZXJC2\",\"pname\":\"LXSY\",\"stepId\":27,\"stepName\":\"绕组直流电阻线间差%\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.0884\",\"b值\":\"0.8000\",\"a值\":\"0.0000\",\"输入值\":\"576.0\"},\"name\":\"LXSY_RZJYJS\",\"pname\":\"LXSY\",\"stepId\":28,\"stepName\":\"绕组绝缘介损(tgδ)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.1325\",\"b值\":\"1800.0000\",\"a值\":\"800.0000\",\"输入值\":\"6778.0\"},\"name\":\"LXSY_TXJDDZ\",\"pname\":\"LXSY\",\"stepId\":29,\"stepName\":\"铁芯接地电阻（MΩ）\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0521\",\"inputParam\":{\"权重\":\"0.1035\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.5034\"},\"name\":\"LXSY\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":30,\"stepName\":\"变压器绝缘试验-例行试验\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0693\",\"inputParam\":{\"权重\":\"0.1333\",\"b值\":\"25.0000\",\"a值\":\"0.0000\",\"输入值\":\"12.0\"},\"name\":\"SJZD_ZXDDL\",\"pname\":\"BYQ_SJZD\",\"stepId\":31,\"stepName\":\"中性点电流\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.2\",\"inputParam\":{\"权重\":\"0.2000\",\"b值\":\"70.0000\",\"a值\":\"30.0000\",\"输入值\":\"11.0\"},\"name\":\"SJZD_ZS\",\"pname\":\"BYQ_SJZD\",\"stepId\":32,\"stepName\":\"噪声\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.3333\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"112.0\"},\"name\":\"SJZD_YBZD\",\"pname\":\"BYQ_SJZD\",\"stepId\":33,\"stepName\":\"原边振动\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.3333\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"112.0\"},\"name\":\"SJZD_FBZD\",\"pname\":\"BYQ_SJZD\",\"stepId\":34,\"stepName\":\"副边振动\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0453\",\"inputParam\":{\"权重\":\"0.1682\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.2693\"},\"name\":\"BYQ_SJZD\",\"pname\":\"LEVEL2_WEIGHT\",\"stepId\":35,\"stepName\":\"变压器绝缘试验-声级振动\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.3333\",\"b值\":\"10.0000\",\"a值\":\"2.0000\",\"输入值\":\"1.0\"},\"name\":\"FJQS_YWXJ\",\"pname\":\"BYQ_BT\",\"stepId\":36,\"stepName\":\"油位下降(%)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.3333\",\"inputParam\":{\"权重\":\"0.3333\",\"b值\":\"10.0000\",\"a值\":\"5.0000\",\"输入值\":\"2.0\"},\"name\":\"FJQS_LYCS\",\"pname\":\"BYQ_BT\",\"stepId\":37,\"stepName\":\"漏油次数\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.3333\",\"inputParam\":{\"权重\":\"0.3333\",\"b值\":\"100.0000\",\"a值\":\"75.0000\",\"输入值\":\"3.0\"},\"name\":\"FJQS_BYQZS\",\"pname\":\"BYQ_BT\",\"stepId\":38,\"stepName\":\"变压器噪声(dB)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1620\",\"inputParam\":{\"权重\":\"0.2431\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.6666\"},\"name\":\"BYQ_BT\",\"pname\":\"LEVEL3_WEIGHT\",\"stepId\":39,\"stepName\":\"变压器器身\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.3077\",\"inputParam\":{\"权重\":\"0.3077\",\"b值\":\"25.0000\",\"a值\":\"15.0000\",\"输入值\":\"4.0\"},\"name\":\"FJLQXT_SW\",\"pname\":\"BYQ_LQXT\",\"stepId\":40,\"stepName\":\"水温(℃)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1538\",\"inputParam\":{\"权重\":\"0.1539\",\"b值\":\"0.5000\",\"a值\":\"0.2000\",\"输入值\":\"5.0\"},\"name\":\"FJLQXT_SY\",\"pname\":\"BYQ_LQXT\",\"stepId\":41,\"stepName\":\"水压\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.3077\",\"inputParam\":{\"权重\":\"0.3077\",\"b值\":\"28.0000\",\"a值\":\"10.0000\",\"输入值\":\"6.0\"},\"name\":\"FJLQXT_HJWD\",\"pname\":\"BYQ_LQXT\",\"stepId\":42,\"stepName\":\"环境温度(℃)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.2\",\"inputParam\":{\"权重\":\"0.2308\",\"b值\":\"20.0000\",\"a值\":\"5.0000\",\"输入值\":\"7.0\"},\"name\":\"FJLQXT_FJSHJXCS\",\"pname\":\"BYQ_LQXT\",\"stepId\":43,\"stepName\":\"风机损坏检修次数\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.1683\",\"inputParam\":{\"权重\":\"0.1736\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.9691999999999998\"},\"name\":\"BYQ_LQXT\",\"pname\":\"LEVEL3_WEIGHT\",\"stepId\":44,\"stepName\":\"变压器冷却系统\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"b值\":\"15.0000\",\"a值\":\"5.0000\",\"输入值\":\"8.0\"},\"name\":\"FJFJKG_YJCDYBHL\",\"pname\":\"BYQ_FJKG\",\"stepId\":45,\"stepName\":\"油击穿电压变化率（Kv，%）\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"输入值\":\"9.0\"},\"name\":\"FJFJKG_CDJGYWWT\",\"pname\":\"BYQ_FJKG\",\"stepId\":46,\"stepName\":\"传动机构有无问题(Y/N)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0000\",\"inputParam\":{\"权重\":\"0.2083\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.0\"},\"name\":\"BYQ_FJKG\",\"pname\":\"LEVEL3_WEIGHT\",\"stepId\":47,\"stepName\":\"分接开关\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.6364\",\"输入值\":\"10.0\"},\"name\":\"FJFDLBH_JDQDZ\",\"pname\":\"BYQ_FDLBHZZ\",\"stepId\":48,\"stepName\":\"继电器动作\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.3636\",\"输入值\":\"11.0\"},\"name\":\"FJFDLBH_YLSFDZ\",\"pname\":\"BYQ_FDLBHZZ\",\"stepId\":49,\"stepName\":\"压力释放阀动作\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0000\",\"inputParam\":{\"权重\":\"0.2083\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.0\"},\"name\":\"BYQ_FDLBHZZ\",\"pname\":\"LEVEL3_WEIGHT\",\"stepId\":50,\"stepName\":\"非电量保护装置\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"2000.0000\",\"a值\":\"1000.0000\",\"输入值\":\"12.0\"},\"name\":\"FJTG_MPJYDZ\",\"pname\":\"BYQ_TG\",\"stepId\":51,\"stepName\":\"末屏绝缘电阻（MΩ）\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0859\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"150.0000\",\"a值\":\"5.0000\",\"输入值\":\"13.0\"},\"name\":\"FJTG_QDL\",\"pname\":\"BYQ_TG\",\"stepId\":52,\"stepName\":\"全电流(mA)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0773\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"85.0000\",\"a值\":\"1.5000\",\"输入值\":\"14.0\"},\"name\":\"FJTG_RXDL\",\"pname\":\"BYQ_TG\",\"stepId\":53,\"stepName\":\"容性电流(mA)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0762\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"85.0000\",\"a值\":\"1.5000\",\"输入值\":\"15.0\"},\"name\":\"FJTG_ZXDL\",\"pname\":\"BYQ_TG\",\"stepId\":54,\"stepName\":\"阻性电流(mA)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0909\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"65.0000\",\"a值\":\"30.0000\",\"输入值\":\"16.0\"},\"name\":\"FJTG_HWWD\",\"pname\":\"BYQ_TG\",\"stepId\":55,\"stepName\":\"红外温度(℃)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"1.0000\",\"a值\":\"0.0000\",\"输入值\":\"18.0\"},\"name\":\"FJTG_MPJS\",\"pname\":\"BYQ_TG\",\"stepId\":56,\"stepName\":\"末屏介损(tgδ)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"1.0000\",\"a值\":\"0.0000\",\"输入值\":\"17.0\"},\"name\":\"FJTG_TGJS\",\"pname\":\"BYQ_TG\",\"stepId\":57,\"stepName\":\"套管介损-例行试验(tgδ)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0909\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"500.0000\",\"a值\":\"150.0000\",\"输入值\":\"20.0\"},\"name\":\"FJTG_TGH2\",\"pname\":\"BYQ_TG\",\"stepId\":58,\"stepName\":\"套管H2(μL/L)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0909\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"100.0000\",\"a值\":\"60.0000\",\"输入值\":\"21.0\"},\"name\":\"FJTG_TGCH4\",\"pname\":\"BYQ_TG\",\"stepId\":59,\"stepName\":\"套管CH4(μL/L)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0\",\"inputParam\":{\"权重\":\"0.0909\",\"b值\":\"2.0000\",\"a值\":\"0.2000\",\"输入值\":\"22.0\"},\"name\":\"FJTG_TGC2H2\",\"pname\":\"BYQ_TG\",\"stepId\":60,\"stepName\":\"套管C2H2(μL/L)\",\"weightLevel\":\"3\"},{\"calculateResult\":\"0.0854\",\"inputParam\":{\"权重\":\"0.1667\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.5121\"},\"name\":\"BYQ_TG\",\"pname\":\"LEVEL3_WEIGHT\",\"stepId\":61,\"stepName\":\"变压器高压套管\",\"weightLevel\":\"2\"},{\"calculateResult\":\"0.429\",\"inputParam\":{\"环境等级\":\"1\",\"设备投运时间\":\"2008-01-01\",\"变压器设计、标牌、运行环境、负荷权重\":\"0.5234\"},\"name\":\"LEVEL1_WEIGHT\",\"stepId\":62,\"stepName\":\"变压器设计、标牌、运行环境、负荷权重\",\"weightLevel\":\"1\"},{\"calculateResult\":\"0.2067\",\"inputParam\":{\"权重\":\"0.3657\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.5651\"},\"name\":\"LEVEL2_WEIGHT\",\"stepId\":63,\"stepName\":\"变压器绝缘试验\",\"weightLevel\":\"1\"},{\"calculateResult\":\"0.0461\",\"inputParam\":{\"权重\":\"0.1109\",\"b值\":\"0.0000\",\"a值\":\"0.0000\",\"输入值\":\"0.4157\"},\"name\":\"LEVEL3_WEIGHT\",\"stepId\":64,\"stepName\":\"变压器附件\",\"weightLevel\":\"1\"},{\"calculateResult\":\"0.6818\",\"inputParam\":{\"变压器设计、标牌、运行环境、负荷最终结果\":\"0.429\",\"变压器绝缘试验最终结果\":\"0.2067\",\"变压器附件最终结果\":\"0.0461\"},\"name\":\"final\",\"stepId\":65,\"stepName\":\"变压器状态最终健康指数计算\",\"weightLevel\":\"1\"}]";
//         List<EvaluationNameDto> stepVo = JSON.parseArray(steps, EvaluationNameDto.class);
//         stepVo.sort(Comparator.comparingInt(a -> Integer.parseInt(a.getWeightLevel())));
//         findEqmCodeList(stepVo);
//     }

     private List<EvaluationNameDto> findEqmCodeList(List<EvaluationNameDto> vo){
         List<EvaluationNameDto> evaluationTreeDtos = vo.stream()
                 .filter( m -> m.getWeightLevel().equals("1"))
                 .peek(m -> m.setChildren(getChildrens(m, vo)))
                 .collect(Collectors.toList());
         return evaluationTreeDtos;
     }

     /**
      * 递归函数 过滤拿到父节点为当前节点的节点，递归加入到当前节点的子节点中，再收集起来
      * @param root
      * @param all   1:变压器，2:GIS
      * @return
      */
     private List<EvaluationNameDto> getChildrens(EvaluationNameDto root, List<EvaluationNameDto> all){
         if(null != root.getChildren()){
             log.info("--->> 当前遍历父节点--[{}] , 子节点长度--[{}]", root.getStepName(),root.getChildren().size());
         }
         List<EvaluationNameDto> childrens = all.stream()
                 .filter( m -> Objects.equals(m.getPname(), root.getName()))
                 // peek方法返回由该流的元素组成的流，并对每个元素执行所提供的 Consumer操作方法
                 // peek和foreach在功能上类似，但foreach执行完函数不做其他操作了，peek完还可以继续对stream进行其它操作
                 .peek(m -> m.setChildren(getChildrens(m, all)))
                 .collect(Collectors.toList());
         return childrens;
     }

     /**
      *
      * @param pid
      * @param type   1:变压器，2:GIS
      * @return
      */
     @Override
     public Map<String, Object> findCalculateStep(String pid,String type){
         BusiGisCaculateStepVo steps = new BusiGisCaculateStepVo();
         steps = gisAssessmentResultMapper.findCalculateStep(pid);
         if(ObjectUtil.isNotEmpty(steps)) {
             String result = steps.getSteps();
             List<EvaluationNameDto> stepVo = JSON.parseArray(result, EvaluationNameDto.class);
             stepVo.sort(Comparator.comparingInt(a -> Integer.parseInt(a.getWeightLevel())));
             List<EvaluationNameDto> stepTree = this.findEqmCodeList(stepVo);
             steps.setStepList(stepTree);
         }
         return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", steps);
     }

     @Override
     public List<TransformerLoadRate> findGisLoadRate() {
        List<BusiSiteTree> mainTreeId = new ArrayList<>();
         List<BusiSiteTree> parentId = gisAssessmentResultMapper.findParentId();
         for(BusiSiteTree busiSiteTree : parentId) {
             String id = busiSiteTree.getParentId();
             List<BusiSiteTree> part = gisAssessmentResultMapper.findMainTreeId(id);
             if(part != null) {
                 for(int i = 0; i < part.size(); i++){
                     BusiSiteTree mainEquipmentId = part.get(i);
                     mainTreeId.add(mainEquipmentId);
                 }
             }
         }
         List<String> dateList = new ArrayList<>();
         List<TransformerLoadRate> results = new ArrayList<>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         for (int i = 0; i < 15; i++) {
             Date date = DateUtils.addDays(new Date(), -i);
             String formatDate = sdf.format(date);
             dateList.add(formatDate);
         }
         for (BusiSiteTree busiSiteTree : mainTreeId) {
             String mainEquipmentId = busiSiteTree.getId();
             for(int i = 0; i < dateList.size(); i++){
                 TransformerLoadRate transformerLoadRate = new TransformerLoadRate();
                 String date = dateList.get(i);
                 transformerLoadRate.setLoadTime(date);
                 TransformerLoadRate yValue = gisAssessmentResultMapper.findAllLoadRate(mainEquipmentId , date);
                 transformerLoadRate.setMainTreeId(mainEquipmentId);
                 transformerLoadRate.setName(busiSiteTree.getName());
                 if(yValue != null) {
                     String value = yValue.getLoadRate();
                     transformerLoadRate.setLoadRate(value);
                     results.add(transformerLoadRate);
                 } else {
                     transformerLoadRate.setLoadRate("0.00");
                     results.add(transformerLoadRate);
                 }
             }
         }
         return results;
     }

}
