package com.huatek.busi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.XParamMarkDao;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.ParameterMakeMapper;
import com.huatek.busi.service.ParameterMakeService;
import com.huatek.busi.vo.ParameterMakeVo;
import com.huatek.busi.vo.ParameterVo;
import com.huatek.busi.vo.ParameterResultVo;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Slf4j
@Component
@Service(interfaceClass = ParameterMakeService.class)
@Transactional(rollbackFor = Exception.class)
public class ParameterMakeServiceImpl implements ParameterMakeService {

    @Autowired
    private ParameterMakeMapper parameterMakeMapper;

    @Override
    public List<ParameterMakeVo> insertTransformerParameterMake() {
        //获取变压器所有达标字段Id
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllTransformerMake();
        for (ParameterMakeVo parameterMakeVo: findAllMake) {
            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllTransformerRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllTransformerLast(makeId);

            if(ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first= parameterMakeMapper.findAllTransformerFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);
                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                //获取上一条数据平均值
                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRTransformerAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLTransformerAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFTransformerAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, RoundingMode.HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (findRValue.compareTo(BigDecimal.ZERO) == 0) {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }else{
                    if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {

                        //计算绝对增长率
                        BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                        parameterMakeVo.setRelativeRate(doRelative);
                    } else {
                        parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                    }
                }

                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }

        }
        return findAllMake;
    }



    @Override
    public List<ParameterMakeVo> insertGisParameterMake() {
        //获取Gis所有达标字段
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllGisMake();

        for (ParameterMakeVo parameterMakeVo: findAllMake) {
            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllGisRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllGisLast(makeId);

            if(ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first = parameterMakeMapper.findAllGisFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);

                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRGisAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLGisAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFGisAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算绝对增长率
                    BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setRelativeRate(doRelative);
                } else {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }
                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }
        }
        return findAllMake;
    }

    @Override
    public List<ParameterMakeVo> insertArresterParameterMake() {
        //获取避雷器所有达标字段
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllArresterMake();
        for (ParameterMakeVo parameterMakeVo:findAllMake) {

            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllArresterRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllArresterLast(makeId);

            if(!ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first = parameterMakeMapper.findAllArresterFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);

                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRArresterAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLArresterAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFArresterAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算绝对增长率
                    BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setRelativeRate(doRelative);
                } else {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }
                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }
        }
        return findAllMake;
    }

    @Override
    public List<ParameterMakeVo> insertSwitchgearParameterMake() {

        //获取所有开关柜打标字段
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllSwitchgearMake();
        for (ParameterMakeVo parameterMakeVo:findAllMake) {
            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllSwitchgearRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllSwitchgearLast(makeId);

            if(!ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first = parameterMakeMapper.findAllSwitchgearFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);

                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRSwitchgearAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLSwitchgearAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFSwitchgearAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算绝对增长率
                    BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setRelativeRate(doRelative);
                } else {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }
                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }
        }
        return findAllMake;
    }

    @Override
    public List<ParameterMakeVo> insertCableParameterMake() {
        //获取所有电缆打标字段
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllCableMake();
        for (ParameterMakeVo parameterMakeVo:findAllMake) {
            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllCableRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllCableLast(makeId);

            if(!ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first = parameterMakeMapper.findAllCableFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);

                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRCableAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLCableAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFCableAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算绝对增长率
                    BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setRelativeRate(doRelative);
                } else {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }
                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }
        }
        return findAllMake;
    }

    @Override
    public List<ParameterMakeVo> insertGeneratorParameterMake() {

        //获取所有发电机打标字段
        List<ParameterMakeVo> findAllMake = parameterMakeMapper.findAllGeneratorMake();
        for (ParameterMakeVo parameterMakeVo:findAllMake) {
            //获取打标字段id
            String makeId = parameterMakeVo.getMarkId();

            //获取最近一条数据
            ParameterMakeVo recently= parameterMakeMapper.findAllGeneratorRecently(makeId);

            //获取上一条数据
            ParameterMakeVo last= parameterMakeMapper.findAllGeneratorLast(makeId);

            if(!ObjectUtil.isNotEmpty(last)) {
                //获取第一条数据
                ParameterMakeVo first = parameterMakeMapper.findAllGeneratorFirst(makeId);

                //将LocalDateTime类型转为String类型
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //最近一条时间并转为String类型
                LocalDateTime RTestDate = recently.getTestDate();
                String dateRStr = RTestDate.format(fmt);

                //上一条时间并转为String类型
                LocalDateTime LTestDate = last.getTestDate();
                String dateLStr = LTestDate.format(fmt);

                //第一条时间并转为String类型
                LocalDateTime FTestDate = first.getTestDate();
                String dateFStr = FTestDate.format(fmt);

                ParameterMakeVo findRMakeAvg = parameterMakeMapper.findRGeneratorAvg(makeId, dateRStr);
                BigDecimal findRValue = new BigDecimal(findRMakeAvg.getCurrentValue());


                //获取上一条数据平均值
                ParameterMakeVo findLMakeAvg = parameterMakeMapper.findLGeneratorAvg(makeId, dateLStr);
                BigDecimal findLValue = new BigDecimal(findLMakeAvg.getCurrentValue());

                //获取第一条数据平均值
                ParameterMakeVo findFMakeAvg = parameterMakeMapper.findFGeneratorAvg(makeId, dateFStr);
                BigDecimal findFValue = new BigDecimal(findFMakeAvg.getCurrentValue());

                //计算相对增长率偏差值
                BigDecimal doXSubtract = findRValue.subtract(findLValue);

                //计算绝对增长率偏差值
                BigDecimal doJSubtract = findRValue.subtract(findFValue);

                //判断除数是否为0
                if (doXSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算相对增长率
                    BigDecimal doAbsolute = doXSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setAbsoluteRate(doAbsolute);
                } else {
                    parameterMakeVo.setAbsoluteRate(BigDecimal.ZERO);
                }

                //判断除数是否为0
                if (doJSubtract.compareTo(BigDecimal.ZERO) != 0) {
                    //计算绝对增长率
                    BigDecimal doRelative = doJSubtract.divide(findRValue, 4, BigDecimal.ROUND_HALF_UP);
                    parameterMakeVo.setRelativeRate(doRelative);
                } else {
                    parameterMakeVo.setRelativeRate(BigDecimal.ZERO);
                }
                parameterMakeVo.setId(Uuid.getUUID());
                parameterMakeMapper.addXparamMake(parameterMakeVo);
            }
        }
        return findAllMake;
    }

    // ==========================================================================================
        @Override
        public List<ParameterResultVo> findTransformerParameterMake() {
            //获取变压器所有报告
            List<ParameterResultVo> recently = new ArrayList<>();
            List<ParameterVo> findAll = parameterMakeMapper.findAllReport();
             if (findAll.size()>3){
                 //获取最新一条报告id
              ParameterVo refNewId = parameterMakeMapper.finRefNewId();
              String lastId = refNewId.getRefId();
                 //获取最新一条报告报告
               List<ParameterMake> addAll = new ArrayList<>();
               List<ParameterMake> findMakeMax = parameterMakeMapper.findTransformerParameterMakeMax(lastId);
               List<ParameterMake> findMakeMin = parameterMakeMapper.findTransformerParameterMakeMin(lastId);
               List<ParameterMake> findAvg = parameterMakeMapper.findTransformerParameterMakeAvg(lastId);
               addAll.addAll(findMakeMax);
               addAll.addAll(findMakeMin);
               addAll.addAll(findAvg);

                //获取前一条报告id
               ParameterVo refSecond = parameterMakeMapper.finRefSecondId();
               String nextId=refSecond.getRefId();
               //获取上一条报告
               List<ParameterMake> addSecondAll = new ArrayList<>();
               List<ParameterMake> findSecondMakeMax = parameterMakeMapper.findTransformerSecondParameterMakeMax(nextId);
               List<ParameterMake> findSecondMakeMin = parameterMakeMapper.findTransformerSecondParameterMakeMin(nextId);
               List<ParameterMake> findSecondAvg = parameterMakeMapper.findTransformerSecondParameterMakeAvg(nextId);
               addSecondAll.addAll(findSecondMakeMax);
               addSecondAll.addAll(findSecondMakeMin);
               addSecondAll.addAll(findSecondAvg);

               //获取第一条报告id
               ParameterVo refFirst = parameterMakeMapper.finRefFirstId();
               String oldId = refFirst.getRefId();
               //获取第一条报告
               List<ParameterMake> addFirstAll = new ArrayList<>();
               List<ParameterMake> findFirstMakeMax = parameterMakeMapper.findTransformerFirstParameterMakeMax(oldId);
               List<ParameterMake> findFirstMakeMin = parameterMakeMapper.findTransformerFirstParameterMakeMin(oldId);
               List<ParameterMake> findFirstAvg = parameterMakeMapper.findTransformerFirstParameterMakeAvg(oldId);
               addFirstAll.addAll(findFirstMakeMax);
               addFirstAll.addAll(findFirstMakeMin);
               addFirstAll.addAll(findFirstAvg);

        for (int i=0; i<addAll.size(); i++){
            ParameterMake One = addAll.get(i);
            ParameterResultVo doRecently = new ParameterResultVo();
            for (int j=0;j<addSecondAll.size();j++){
                ParameterMake Two = addSecondAll.get(j);
                if (One.getEParam().equals(Two.getEParam())){
                    BigDecimal OneValue=new BigDecimal(One.getCurrentValue());
                    BigDecimal TwoValue=new BigDecimal(Two.getCurrentValue());
                    //计算偏差值
                    BigDecimal doSubtract = OneValue.subtract(TwoValue);
                    BeanUtil.copyProperties(Two,doRecently);
                    doRecently.setRefId(One.getRefId());
                    //判断除数是否为0
                    if (doSubtract.compareTo(BigDecimal.ZERO)!=0){
                        //计算偏差率
                        BigDecimal doDivide = doSubtract.divide(TwoValue,4,BigDecimal.ROUND_HALF_UP);
                        doRecently.setDeviateValue(doDivide);
                        //计算相对增长率
                        BigDecimal doAbsolute = doSubtract.divide(TwoValue,4,BigDecimal.ROUND_HALF_UP);
                        doRecently.setAbsoluteRate(doAbsolute);
                    }
                }
            }
            for (int n=0;n<addFirstAll.size();n++){
                    ParameterMake Three = addSecondAll.get(n);
                if (One.getEParam().equals(Three.getEParam())){
                    BigDecimal OneValue=new BigDecimal(One.getCurrentValue());
                    BigDecimal ThreeValue=new BigDecimal(Three.getCurrentValue());
                    //计算偏差值
                    BigDecimal doSubtract = OneValue.subtract(ThreeValue);
                    //判断除数是否为0
                    if (doSubtract.compareTo(BigDecimal.ZERO)!=0){
                        //计算绝对增长率
                        BigDecimal doRelative = doSubtract.divide(ThreeValue,4,BigDecimal.ROUND_HALF_UP);
                        doRecently.setRelativeRate(doRelative);
                    }

                }
            }
                 recently.add(doRecently);
        }

             }
        for (int i=0;i<recently.size();i++){
            ParameterResultVo parameterResultVo = recently.get(i);
            String uUID = Uuid.getUUID();
            parameterResultVo.setId(uUID);
            parameterMakeMapper.addParamValue(parameterResultVo);

        }
       return recently;
    }



    @Override
    public List<ParameterResultVo> findTransformerParameterRate(String refId) {
        List<ParameterResultVo> findRate = parameterMakeMapper.findTransformerRate(refId);
        return findRate;
    }

    @Override
    public List<ParameterResultVo> findGisParameterMake() {
        //获取Gis所有报告
        List<ParameterResultVo> recently = new ArrayList<>();
        List<ParameterVo> findAll = parameterMakeMapper.findGisAllReport();
        if (findAll.size()>3){
            //获取最新一条报告id
            ParameterVo refNewId = parameterMakeMapper.finGisNewId();
            String lastId = refNewId.getRefId();
            //获取最新一条报告
            List<ParameterMake> addAll = new ArrayList<>();
            List<ParameterMake> findMakeMax = parameterMakeMapper.findGisMakeMax(lastId);
            List<ParameterMake> findMakeMin = parameterMakeMapper.findGisMakeMin(lastId);
            List<ParameterMake> findAvg = parameterMakeMapper.findGisMakeAvg(lastId);
            addAll.addAll(findMakeMax);
            addAll.addAll(findMakeMin);
            addAll.addAll(findAvg);

            //获取前一条报告id
            ParameterVo refSecond = parameterMakeMapper.finGisSecondId();
            String nextId=refSecond.getRefId();
            //获取上一条报告
            List<ParameterMake> addSecondAll = new ArrayList<>();
            List<ParameterMake> findSecondMakeMax = parameterMakeMapper.findGisSecondMakeMax(nextId);
            List<ParameterMake> findSecondMakeMin = parameterMakeMapper.findGisSecondMakeMin(nextId);
            List<ParameterMake> findSecondAvg = parameterMakeMapper.findGisSecondMakeAvg(nextId);
            addSecondAll.addAll(findSecondMakeMax);
            addSecondAll.addAll(findSecondMakeMin);
            addSecondAll.addAll(findSecondAvg);

            //获取第一条报告id
            ParameterVo refFirst = parameterMakeMapper.finGisFirstId();
            String oldId = refFirst.getRefId();
            //获取第一条报告
            List<ParameterMake> addFirstAll = new ArrayList<>();
            List<ParameterMake> findFirstMakeMax = parameterMakeMapper.findGisFirstMakeMax(oldId);
            List<ParameterMake> findFirstMakeMin = parameterMakeMapper.findGisFirstMakeMin(oldId);
            List<ParameterMake> findFirstAvg = parameterMakeMapper.findGisFirstMakeAvg(oldId);
            addFirstAll.addAll(findFirstMakeMax);
            addFirstAll.addAll(findFirstMakeMin);
            addFirstAll.addAll(findFirstAvg);

            for (int i=0; i<addAll.size(); i++){
                ParameterMake One = addAll.get(i);
                ParameterResultVo doRecently = new ParameterResultVo();
                for (int j=0;j<addSecondAll.size();j++){
                    ParameterMake Two = addSecondAll.get(j);
                    if (One.getEParam().equals(Two.getEParam())){
                        BigDecimal OneValue=new BigDecimal(One.getCurrentValue());
                        BigDecimal TwoValue=new BigDecimal(Two.getCurrentValue());
                        //计算偏差值
                        BigDecimal doSubtract = OneValue.subtract(TwoValue);
                        BeanUtil.copyProperties(Two,doRecently);
                        doRecently.setRefId(One.getRefId());
                        //判断除数是否为0
                        if (doSubtract.compareTo(BigDecimal.ZERO)!=0){
                            //计算偏差率
                            BigDecimal doDivide = doSubtract.divide(TwoValue,4,BigDecimal.ROUND_HALF_UP);
                            doRecently.setDeviateValue(doDivide);
                            //计算相对增长率
                            BigDecimal doAbsolute = doSubtract.divide(TwoValue,4,BigDecimal.ROUND_HALF_UP);
                            doRecently.setAbsoluteRate(doAbsolute);
                        }
                    }
                }
                for (int n=0;n<addFirstAll.size();n++){
                    ParameterMake Three = addSecondAll.get(n);
                    if (One.getEParam().equals(Three.getEParam())){
                        BigDecimal OneValue=new BigDecimal(One.getCurrentValue());
                        BigDecimal ThreeValue=new BigDecimal(Three.getCurrentValue());
                        //计算偏差值
                        BigDecimal doSubtract = OneValue.subtract(ThreeValue);
                        //判断除数是否为0
                        if (doSubtract.compareTo(BigDecimal.ZERO)!=0){
                            //计算绝对增长率
                            BigDecimal doRelative = doSubtract.divide(ThreeValue,4,BigDecimal.ROUND_HALF_UP);
                            doRecently.setRelativeRate(doRelative);
                        }

                    }
                }
                recently.add(doRecently);
            }
        }
        for (int i=0;i<recently.size();i++){
            ParameterResultVo parameterResultVo = recently.get(i);
            String uUID = Uuid.getUUID();
            parameterResultVo.setId(uUID);
            parameterMakeMapper.addParamValue(parameterResultVo);
        }
        return recently;
    }

    @Override
    public List<ParameterMake> findGisParameterValue() {
        //获取gis所有报告
        List<ParameterVo> findAll = parameterMakeMapper.findGisAllReport();
        //获取每份报告打标参数当前值
        List<ParameterMake> gidList = new ArrayList<>();
        for (ParameterVo parameterVo:findAll) {
            String lastId = parameterVo.getRefId();
            List<ParameterMake> findGisMakeMax = parameterMakeMapper.findGisMakeMax(lastId);
            List<ParameterMake> findGisMakeMin = parameterMakeMapper.findGisMakeMin(lastId);
            List<ParameterMake> findGisMakeAvg = parameterMakeMapper.findGisMakeAvg(lastId);
            gidList.addAll(findGisMakeMax);
            gidList.addAll(findGisMakeMin);
            gidList.addAll(findGisMakeAvg);

        }
        return gidList;
    }


}
