package com.tofly.watermeter.custom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.entity.enums.ReadData;
import com.tofly.watermeter.custom.service.ReadDataCheckService;
import com.tofly.watermeter.entity.Qfcbqfmxls;
import com.tofly.watermeter.entity.Qfkhjbxx;
import com.tofly.watermeter.entity.Readmetereashis;
import com.tofly.watermeter.entity.Readmeterhis;
import com.tofly.watermeter.mapper.QfkhjbxxMapper;
import com.tofly.watermeter.service.QfcbqfmxlsService;
import com.tofly.watermeter.service.QfkhjbxxService;
import com.tofly.watermeter.service.ReadmetereashisService;
import com.tofly.watermeter.service.ReadmeterhisService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author FlashWJL
 * @date 2021/6/10 10:31
 */
@Service
@AllArgsConstructor
public class ReadDataCheckServiceImpl implements ReadDataCheckService {


    private final QfkhjbxxService qfkhjbxxService;
    private final QfkhjbxxMapper qfkhjbxxMapper;
    private final ReadmeterhisService readmeterhisService;
    private final ReadmetereashisService readmetereashisService;
    private final QfcbqfmxlsService qfcbqfmxlsService;

    @Override
    public Map getYhInfo(String yhbh) {
//        List<String> yhbhList = Arrays.asList(yhbh.split(","));
        Date currentTime = Date8Util.getCurrentDate();
        //查询客户基本信息
        Qfkhjbxx qfkhjbxx = qfkhjbxxService.getOne(Wrappers.<Qfkhjbxx>lambdaQuery().eq(Qfkhjbxx::getYhbh, yhbh)
                .select(Qfkhjbxx::getYhbh, Qfkhjbxx::getYhxm, Qfkhjbxx::getLxdh, Qfkhjbxx::getLxsj, Qfkhjbxx::getYsxz,
                        Qfkhjbxx::getSbbh, Qfkhjbxx::getSbkj, Qfkhjbxx::getYhdz));
        if (StringUtil.isNull(qfkhjbxx)) {
            throw new ToflyDeniedException("暂无该用户信息");
        }
        //查询水量信息
        Readmeterhis readmeterhis = readmeterhisService.getOne(Wrappers.<Readmeterhis>lambdaQuery().eq(Readmeterhis::getYhbh, yhbh)
                .eq(Readmeterhis::getMonthPlan, Date8Util.getYearMonth()).eq(Readmeterhis::getIsGeneral, "0").eq(Readmeterhis::getCbbz , "1")
                .select(Readmeterhis::getCbsl, Readmeterhis::getMonth1, Readmeterhis::getMonth2, Readmeterhis::getMonth3,Readmeterhis::getCbryName,
                        Readmeterhis::getYearonyear1, Readmeterhis::getYearonyear2, Readmeterhis::getYearonyear3, Readmeterhis::getMonthPlan));
        //查询水量信息，关联抄表历史表
//        List<Readmetereashis> mapList = qfkhjbxxMapper.getYhInfoAndCbslInfo(yhbhList , Date8Util.getYearMonth());
        //同比
        BigDecimal yearOnYear = null;
        //环比
        BigDecimal chainRatio = null;
        //上月水量
        BigDecimal lastSl = ReadData.CALCULATE_ZERO.getYield();
        Long bcsl = 0L;
        String cbryName = null;
        if (StringUtil.isNotNull(readmeterhis)) {
            cbryName = readmeterhis.getCbryName();
            //当月水量
            bcsl = readmeterhis.getCbsl();
            if (StringUtil.isNotNull(readmeterhis.getMonth1())){
                lastSl = new BigDecimal(readmeterhis.getMonth1());
            }
            yearOnYear = yearOnYear(yhbh, currentTime, bcsl, readmeterhis);
            chainRatio = chainRatio(readmeterhis, bcsl);
        }
        Map map = new HashMap();
        map.put("bcsl", bcsl);
        if (StringUtil.isNull(bcsl)){
            map.put("sjsl", null);
        }else {
            map.put("sjsl", new BigDecimal(bcsl).subtract(lastSl));
        }

        map.put("lastSl", lastSl);
        map.put("yearOnYear", yearOnYear);
        map.put("chainRatio", chainRatio);
        map.put("yhxm", qfkhjbxx.getYhxm());
        map.put("yhdz", qfkhjbxx.getYhdz());
        map.put("lxdh", qfkhjbxx.getLxdh());
        map.put("sbbh", qfkhjbxx.getSbbh());
        map.put("lxsj", qfkhjbxx.getLxsj());
        map.put("sbkj", qfkhjbxx.getSbkjName());
        map.put("cbryName" , cbryName);
        return map;
    }

    /**
     * 本次与另外三次抄表数据的平均值比较
     *
     * @param sl1
     * @param sl2
     * @param sl3
     * @param bcslBig
     * @return
     */
    private BigDecimal readDataDispose(BigDecimal sl1, BigDecimal sl2, BigDecimal sl3, BigDecimal bcslBig) {
        BigDecimal zero = ReadData.CALCULATE_ZERO.getYield();
        //前三次水量数据之和
        BigDecimal sum = sl1.add(sl2).add(sl3);
        //除以 3 求平均值用的
        BigDecimal three = ReadData.CALCULATE_THREE.getYield();
        //平均值
        BigDecimal average = sum.divide(three, 2, BigDecimal.ROUND_HALF_UP);
        if (average.compareTo(zero) == 0) {
            return zero;
        }
        //如果平均量小于5，并且本次水量小于5，则系统不做同比
        BigDecimal five = ReadData.CALCULATE_FIVE.getYield();
        if (average.compareTo(five) == -1 && bcslBig.compareTo(five) == -1) {
            return zero;
        }
        //本次水量与平均值的差(绝对值.abs()) 可以得到突升或突降
        BigDecimal diff = average.subtract(bcslBig).abs();
        if (diff.compareTo(zero) == 0) {
            return zero;
        }
        //百分比
        BigDecimal percent = diff.divide(average, 2, BigDecimal.ROUND_HALF_UP);
        return percent;
    }


    /**
     * 同比
     * 同比数据平均量与本月抄表水量的比较，超过或者低于的具体百分比
     *
     * @param yhbh     用户编号
     * @param readTime 抄表时间
     * @param bcsl     本次水量
     * @return //如果返回的是 0 ，则代表前三年中存在着没有抄表数据
     */
    private BigDecimal yearOnYear(String yhbh, Date readTime, Long bcsl, Readmeterhis readmeterhis) {
        Long yearonyear1 = readmeterhis.getYearonyear1();
        Long yearonyear2 = readmeterhis.getYearonyear2();
        Long yearonyear3 = readmeterhis.getYearonyear3();
        if (StringUtil.isNull(yearonyear1) || StringUtil.isNull(yearonyear2) || StringUtil.isNull(yearonyear3) || StringUtil.isNull(bcsl)) {
            return null;
        }
        BigDecimal sl1 = new BigDecimal(yearonyear1);
        BigDecimal sl2 = new BigDecimal(yearonyear2);
        BigDecimal sl3 = new BigDecimal(yearonyear3);
        BigDecimal bcslBig = new BigDecimal(bcsl);
        return readDataDispose(sl1, sl2, sl3, bcslBig);
    }


    /**
     * 环比
     * 环比数据平均量与本月抄表水量的比较，超过或者低于的具体百分比
     *
     * @param bcsl 本次水量
     * @return 如果返回的是 0 ，则代表前三次抄表数据中存在着没有抄表数据
     */
    private BigDecimal chainRatio(Readmeterhis readmeterhis, Long bcsl) {
        Long month1 = readmeterhis.getMonth1();
        Long month2 = readmeterhis.getMonth2();
        Long month3 = readmeterhis.getMonth3();
        if (StringUtil.isNull(month1) || StringUtil.isNull(month2) || StringUtil.isNull(month3) || StringUtil.isNull(bcsl)) {
            return null;
        }
        BigDecimal monthBig1 = new BigDecimal(month1);
        BigDecimal monthBig2 = new BigDecimal(month2);
        BigDecimal monthBig3 = new BigDecimal(month3);
        BigDecimal bcslBig = new BigDecimal(bcsl);
        return readDataDispose(monthBig1, monthBig2, monthBig3, bcslBig);
    }


    @Override
    public ResultRespone eastone() {
        int count = readmetereashisService.count(Wrappers.<Readmetereashis>lambdaQuery().eq(Readmetereashis::getIsModel, "0"));
        if (count > 0) {
            int size = (int) Math.ceil(Double.valueOf(count) / 20d);
            for (int i = 0; i < size; i++) {
                Page page = new Page();
                page.setCurrent(i + 1);
                page.setSize(20);
                List<Readmetereashis> readmetereashisList = readmetereashisService
                        .page(page, Wrappers.<Readmetereashis>lambdaQuery().eq(Readmetereashis::getIsModel, "0")).getRecords();
                for (Readmetereashis readmetereashis : readmetereashisList) {
                    Date cbrq = readmetereashis.getCbrq();
                    String cbyf = readmetereashis.getCbyf();
                    String yhbh = readmetereashis.getYhbh();
                    Long cbsl = readmetereashis.getCbsl();

                    Qfcbqfmxls qfcbqfmxls = qfcbqfmxlsService.getOne(Wrappers.<Qfcbqfmxls>lambdaQuery().eq(Qfcbqfmxls::getYhbh, yhbh)
                            .lt(Qfcbqfmxls::getCbrq, cbrq).orderByDesc(Qfcbqfmxls::getCbrq).apply("ROWNUM = 1"));
                    //远传表都是实抄
                    eastoneAbnormal(qfcbqfmxls, cbsl, 66L, cbrq, cbyf, yhbh);
                }
            }
        }
        return null;
    }


    /**
     * 远传表异常算法
     *
     * @param qfcbqfmxls 上次抄表信息
     * @param cbsl       本次抄表水量
     * @param cbfsCode   抄表方式；远传表默认实抄
     * @param cbrq       抄表日期
     * @param cbyf       抄表月份
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String eastoneAbnormal(Qfcbqfmxls qfcbqfmxls, Long cbsl, Long cbfsCode, Date cbrq, String cbyf, String yhbh) {
        // TODO : 注释掉以下远传表的模型算法，远传表暂时数据还没有同步，并且以下算法验证还没有优化好，但是跟抄表异常算法模型的是一模一样的，除了更改的数据库哪里不一样
//        //修改远传表算法状态
//        readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf).set(Readmetereashis::getIsModel, "1"));
//        String ysxz = qfcbqfmxls.getYsxz();
//        Long bczl = cbsl;
//        String sbkj = qfcbqfmxls.getSbkjName();
//        //前三次水量数据
//        BigDecimal month1 = null;
//        if (StringUtil.isNotNull(qfcbqfmxls.getCbsl())) {
//            month1 = new BigDecimal(qfcbqfmxls.getCbsl());
//        }
//        BigDecimal month2 = null;
//        if (StringUtil.isNotNull(qfcbqfmxls.getMonth1())) {
//            month2 = new BigDecimal(qfcbqfmxls.getMonth1());
//        }
//        BigDecimal month3 = null;
//        if (StringUtil.isNotNull(qfcbqfmxls.getMonth2())) {
//            month3 = new BigDecimal(qfcbqfmxls.getMonth2());
//        }
//        Long yearonyear1 = qfcbqfmxls.getYearonyear1();
//        Long yearonyear2 = qfcbqfmxls.getYearonyear2();
//        Long yearonyear3 = qfcbqfmxls.getYearonyear3();
//        BigDecimal bigBcsl = new BigDecimal(bczl);
//        List<Readabnormal> readabnormalList = new ArrayList<>();
//        Readabnormal readabnormal = new Readabnormal();
//        if (StringUtil.isNotNull(qfcbqfmxls)) {
//            //估水模型
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery().eq(Readabnormal::getTypeCode, AbnormalEnum.GSMX.getTypeCode()));
//            if (StringUtil.isNotNull(readabnormal)) {
//                //说明
//                String describe = readabnormal.getDescribe();
//                //估水模型
//                if (cbfsCode.equals(CbfsEnum.GC.getCbfsId())) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe));
//                    return describe;
//                }
//            }
//
//            //换表模型
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery().eq(Readabnormal::getTypeCode, AbnormalEnum.HBMX.getTypeCode()));
//            if (StringUtil.isNotNull(readabnormal)) {
//                //说明
//                String describe = readabnormal.getDescribe();
//                if (cbfsCode.equals(CbfsEnum.BMDZJHB.getCbfsId()) || cbfsCode.equals(CbfsEnum.ZCZJHB.getCbfsId())
//                        || cbfsCode.equals(CbfsEnum.YHBRQGZHB.getCbfsId()) || cbfsCode.equals(CbfsEnum.WHBRQGZHB.getCbfsId())) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe));
//                    return describe;
//                }
//            }
//
//            //水量突降模型
//            readabnormalList = readabnormalService.list(Wrappers.<Readabnormal>lambdaQuery()
//                    .and(v -> v.eq(Readabnormal::getTypeCode, AbnormalEnum.SLTJMX.getTypeCode())
//                            .or().eq(Readabnormal::getTypeCode, AbnormalEnum.SLTSMX.getTypeCode()))
//                    .eq(Readabnormal::getYsxzBm, ysxz)
//                    .eq(Readabnormal::getSbkjBm, sbkj));
//            if (readabnormalList.size() > 0) {
//                for (Readabnormal readab : readabnormalList) {
//                    String describe = readab.getDescribe();
//                    Long nosediveValue = Long.valueOf(readab.getNosediveValue());
//                    String typeCode = readab.getTypeCode();
//                    BigDecimal average = null;
//                    //前三次平均水量
//                    if (StringUtil.isNotNull(month1) && StringUtil.isNotNull(month2) && StringUtil.isNotNull(month3)) {
//                        average = month1.add(month2).add(month3).divide(ReadData.CALCULATE_THREE.getYield(), 2, BigDecimal.ROUND_UP);
//                    } else if (StringUtil.isNotNull(month1) && StringUtil.isNotNull(month2)) {
//                        average = month1.add(month2).divide(ReadData.CALCULATE_TWO.getYield(), 2, BigDecimal.ROUND_UP);
//                    } else if (StringUtil.isNotNull(month1)) {
//                        average = month1;
//                    }
//                    if (StringUtil.isNotNull(average)) {
//                        //前三次平均水量是否大于基准值 如果是则进入筛选，如果本次水量小于等于突降值则异常，
//                        // 这里定义的突降值居然是本次水量小于突降值就异常，我觉得正常的理解应该是和上次水量的比较只差，这个差值突降的比较
//                        if (average.compareTo(new BigDecimal(readab.getMinAverageValue())) != -1) {
//
//                            if (AbnormalEnum.SLTJMX.getTypeCode().equals(typeCode)) {
//                                Long tjDiff = month1.subtract(bigBcsl).divide(month1, 3, BigDecimal.ROUND_UP)
//                                        .multiply(ReadData.CALCULATE_HUNDRED.getYield()).longValue();
//                                //突降值是百分比
//                                if (nosediveValue.compareTo(tjDiff) == -1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次突降为：" + tjDiff + "%"));
//                                    return describe + "，本次突降为：" + tjDiff + "%";
//                                }
//                            }
//                            if (AbnormalEnum.SLTSMX.getTypeCode().equals(typeCode)) {
//                                Long tsDiff = bigBcsl.subtract(month1).divide(month1, 3, BigDecimal.ROUND_UP)
//                                        .multiply(ReadData.CALCULATE_HUNDRED.getYield()).longValue();
//                                //突降值是百分比
//                                if (nosediveValue.compareTo(tsDiff) == -1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次突升为：" + tsDiff + "%"));
//                                    return describe + "，本次突升为：" + tsDiff + "%";
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//            Integer zeroCount = 0;
//            Integer upCount = 0;
//            Integer downCount = 0;
//            Integer equalCount = 0;
//            //上次抄表水量
//            Long lastCbsl = qfcbqfmxls.getCbsl();
//            //升降水量
//            Long sjsl = cbsl;
//            //只要没有上次读数，则表示第一次抄表
//            if (StringUtil.isNotNull(lastCbsl)) {
//                sjsl = cbsl - lastCbsl;
//                //比较本次抄表水量与上一次抄表水量
//                int compareTo = cbsl.compareTo(lastCbsl);
//                //如果有上一次记录（0水量次数、水量连续上升/下降/相同次数）的情况
//                //本次上升
//                if (compareTo == 1) {
//                    upCount = qfcbqfmxls.getContinuityUp() + 1;
//                }//本次下降
//                else if (compareTo == -1) {
//                    downCount = qfcbqfmxls.getContinuityDown() + 1;
//                }//本次相同
//                else if (compareTo == 0) {
//                    equalCount = qfcbqfmxls.getContinuityEqual() + 1;
//                }
//            }
//            //0水量
//            if (cbsl.equals(0L)) {
//                zeroCount = qfcbqfmxls.getZeroCount() + 1;
//            }
//
//            //0水量模型
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery()
//                    .eq(Readabnormal::getTypeCode, AbnormalEnum.LSLMX.getTypeCode()).eq(Readabnormal::getYsxzBm, ysxz)
//                    .eq(Readabnormal::getSbkjBm, sbkj));
//            if (StringUtil.isNotNull(readabnormal)) {
//                String describe = readabnormal.getDescribe();
//                Integer count = readabnormal.getCount();
//                if (zeroCount > count) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe + "，现有：" + zeroCount + "次0水量抄表"));
//                    return describe + "，现有：" + zeroCount + "次0水量抄表";
//                }
//            }
//
//            //水量上升
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery().eq(Readabnormal::getTypeCode, AbnormalEnum.SLLXSSMX.getTypeCode()));
//            if (StringUtil.isNotNull(readabnormal)) {
//                String describe = readabnormal.getDescribe();
//                Integer count = readabnormal.getCount();
//                if (upCount >= count) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe + "，现有：" + upCount + "次连续上升水量"));
//                    return describe + "，现有：" + upCount + "次连续上升水量";
//                }
//            }
//            //水量下降
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery().eq(Readabnormal::getTypeCode, AbnormalEnum.SLLXXJMX.getTypeCode()));
//            if (StringUtil.isNotNull(readabnormal)) {
//                String describe = readabnormal.getDescribe();
//                Integer count = readabnormal.getCount();
//                if (downCount >= count) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe + "，现有：" + upCount + "次连续下降水量"));
//                    return describe + "，现有：" + upCount + "次连续下降水量";
//                }
//            }
//
//            //水量相同
//            readabnormal = readabnormalService.getOne(Wrappers.<Readabnormal>lambdaQuery().eq(Readabnormal::getTypeCode, AbnormalEnum.LXSLXTMX.getTypeCode()));
//            if (StringUtil.isNotNull(readabnormal)) {
//                String describe = readabnormal.getDescribe();
//                Integer count = readabnormal.getCount();
//                if (equalCount >= count) {
//                    //修改抄表类型原因
//                    Long typeId = readabnormal.getType();
//                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                            .set(Readmetereashis::getAbnomalId, typeId)
//                            .set(Readmetereashis::getShbz, "0")
//                            .set(Readmetereashis::getAuditReason, describe + "，现有：" + equalCount + "次水量相同"));
//                    return describe + "，现有：" + equalCount + "次水量相同";
//                }
//            }
//
//            //环比升 、环比降
//            readabnormalList = readabnormalService.list(Wrappers.<Readabnormal>lambdaQuery()
//                    .and(v -> v.eq(Readabnormal::getTypeCode, AbnormalEnum.HBSMX.getTypeCode())
//                            .or().eq(Readabnormal::getTypeCode, AbnormalEnum.HBJMX.getTypeCode()))
//                    .eq(Readabnormal::getYsxzBm, ysxz).eq(Readabnormal::getSbkjBm, sbkj));
//            if (readabnormalList.size() > 0) {
//                //只要其中有一个为null，那么就不满足算法条件
//                if (StringUtil.isNotNull(month1) && StringUtil.isNotNull(month2) && StringUtil.isNotNull(month3)) {
//                    for (Readabnormal readab : readabnormalList) {
//                        String describe = readab.getDescribe();
//                        String typeCode = readab.getTypeCode();
//                        Integer minAverageValue = readab.getMinAverageValue();
//                        Float ratio = readab.getRatio();
//                        if (AbnormalEnum.HBSMX.getTypeCode().equals(typeCode)) {
//                            //前三次水量平均值
//                            BigDecimal average = getAverage(month1, month2, month3);
//                            //水量平均值必须大于等于最小基准值 才进入算法筛选
//                            if (average.compareTo(new BigDecimal(minAverageValue)) != -1) {
//                                BigDecimal upRatio = getUpRatio(average, bigBcsl);
//                                BigDecimal standardRatio = new BigDecimal(ratio);
//                                if (upRatio.compareTo(standardRatio) == 1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次环比升比例为：" + upRatio + "%"));
//                                    return describe + "，本次环比升比例为：" + upRatio + "%";
//                                }
//                            }
//                        } else if (AbnormalEnum.HBJMX.getTypeCode().equals(typeCode)) {
//                            //前三次水量平均值
//                            BigDecimal average = getAverage(month1, month2, month3);
//                            if (average.compareTo(new BigDecimal(minAverageValue)) != -1) {
//                                BigDecimal downRatio = getDownRatio(average, bigBcsl);
//                                BigDecimal standardRatio = new BigDecimal(ratio);
//                                if (downRatio.compareTo(standardRatio) == 1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次环比降比例为：" + downRatio + "%"));
//                                    return describe + "，本次环比降比例为：" + downRatio + "%";
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//            //同比升 、同比降
//            readabnormalList = readabnormalService.list(Wrappers.<Readabnormal>lambdaQuery()
//                    .and(v -> v.eq(Readabnormal::getTypeCode, AbnormalEnum.TBSMX.getTypeCode())
//                            .or().eq(Readabnormal::getTypeCode, AbnormalEnum.TBJMX.getTypeCode()))
//                    .eq(Readabnormal::getYsxzBm, ysxz).eq(Readabnormal::getSbkjBm, sbkj));
//            if (readabnormalList.size() > 0) {
//                //只要其中有一个为null，那么就不满足算法条件
//                if (StringUtil.isNotNull(yearonyear1) && StringUtil.isNotNull(yearonyear2) && StringUtil.isNotNull(yearonyear3)) {
//                    BigDecimal yearSl1 = new BigDecimal(yearonyear1);
//                    BigDecimal yearSl2 = new BigDecimal(yearonyear2);
//                    BigDecimal yearSl3 = new BigDecimal(yearonyear3);
//                    for (Readabnormal readab : readabnormalList) {
//                        String describe = readab.getDescribe();
//                        String typeCode = readab.getTypeCode();
//                        Integer minAverageValue = readab.getMinAverageValue();
//                        Float ratio = readab.getRatio();
//                        if (AbnormalEnum.TBSMX.getTypeCode().equals(typeCode)) {
//                            //前三年同月水量平均值
//                            BigDecimal average = getAverage(yearSl1, yearSl2, yearSl3);
//                            //水量平均值必须大于等于最小基准值 才进入算法筛选
//                            if (average.compareTo(new BigDecimal(minAverageValue)) != -1) {
//                                BigDecimal upRatio = getUpRatio(average, bigBcsl);
//                                BigDecimal standardRatio = new BigDecimal(ratio);
//                                if (upRatio.compareTo(standardRatio) == 1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次同比升比例为：" + upRatio + "%"));
//                                    return describe + "，本次同比升比例为：" + upRatio + "%";
//                                }
//                            }
//                        } else if (AbnormalEnum.TBJMX.getTypeCode().equals(typeCode)) {
//                            //前三年同月水量平均值
//                            BigDecimal average = getAverage(yearSl1, yearSl2, yearSl3);
//                            if (average.compareTo(new BigDecimal(minAverageValue)) != -1) {
//                                BigDecimal downRatio = getDownRatio(average, bigBcsl);
//                                BigDecimal standardRatio = new BigDecimal(ratio);
//                                if (downRatio.compareTo(standardRatio) == 1) {
//                                    //修改抄表类型原因
//                                    Long typeId = readab.getType();
//                                    readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                            .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                            .set(Readmetereashis::getAbnomalId, typeId)
//                                            .set(Readmetereashis::getShbz, "0")
//                                            .set(Readmetereashis::getAuditReason, describe + "，本次环比降比例为：" + downRatio + "%"));
//                                    return describe + "，本次环比降比例为：" + downRatio + "%";
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//
//            //水量微幅波动
//            readabnormalList = readabnormalService.list(Wrappers.<Readabnormal>lambdaQuery()
//                    .eq(Readabnormal::getTypeCode, AbnormalEnum.SLWFBDMX.getTypeCode())
//                    .eq(Readabnormal::getYsxzBm, ysxz).eq(Readabnormal::getSbkjBm, sbkj));
//            if (readabnormalList.size() > 0) {
//                for (Readabnormal readab : readabnormalList) {
//                    String describe = readab.getDescribe();
//                    Float ratio = readab.getRatio();
//                    //同比环比
//                    String isYearorchain = readab.getIsYearorchain();
//                    //升降比例
//                    String isUpordown = readab.getIsUpordown();
//                    //基准值
//                    Long fiducialValue = Long.valueOf(readab.getFiducialValue());
//                    //当月水量大于基准值纳入筛选
//                    if (bczl.compareTo(fiducialValue) == 1) {
//                        //同比
//                        if ("0".equals(isYearorchain)) {
//                            //只要其中有一个为null，那么就不满足算法条件
//                            if (StringUtil.isNotNull(yearonyear1) && StringUtil.isNotNull(yearonyear2) && StringUtil.isNotNull(yearonyear3)) {
//                                BigDecimal yearSl1 = new BigDecimal(yearonyear1);
//                                BigDecimal yearSl2 = new BigDecimal(yearonyear2);
//                                BigDecimal yearSl3 = new BigDecimal(yearonyear3);
//                                //升
//                                if ("0".equals(isUpordown)) {
//                                    //前三年同月水量平均值
//                                    BigDecimal average = getAverage(yearSl1, yearSl2, yearSl3);
//                                    BigDecimal upRatio = getUpRatio(average, bigBcsl);
//                                    BigDecimal standardRatio = new BigDecimal(ratio);
//                                    if (upRatio.compareTo(standardRatio) == 1) {
//                                        //修改抄表类型原因
//                                        Long typeId = readab.getType();
//                                        readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                                .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                                .set(Readmetereashis::getAbnomalId, typeId)
//                                                .set(Readmetereashis::getShbz, "0")
//                                                .set(Readmetereashis::getAuditReason, describe + "，本次同比升比例为：" + upRatio + "%"));
//                                        return describe + "，本次同比升比例为：" + upRatio + "%";
//                                    }
//                                }
//                                //降
//                                else if ("1".equals(isUpordown)) {
//                                    //前三年同月水量平均值
//                                    BigDecimal average = getAverage(yearSl1, yearSl2, yearSl3);
//                                    BigDecimal downRatio = getDownRatio(average, bigBcsl);
//                                    BigDecimal standardRatio = new BigDecimal(ratio);
//                                    if (downRatio.compareTo(standardRatio) == 1) {
//                                        //修改抄表类型原因
//                                        Long typeId = readab.getType();
//                                        readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                                .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                                .set(Readmetereashis::getAbnomalId, typeId)
//                                                .set(Readmetereashis::getShbz, "0")
//                                                .set(Readmetereashis::getAuditReason, describe + "，本次环比降比例为：" + downRatio + "%"));
//                                        return describe + "，本次环比降比例为：" + downRatio + "%";
//                                    }
//                                }
//                            }
//                        }
//                        //环比
//                        else if ("1".equals(isYearorchain)) {
//                            if (StringUtil.isNotNull(month1) && StringUtil.isNotNull(month2) && StringUtil.isNotNull(month3)) {
//                                //升
//                                if ("0".equals(isUpordown)) {
//                                    //前三次水量平均值
//                                    BigDecimal average = getAverage(month1, month2, month3);
//                                    BigDecimal upRatio = getUpRatio(average, bigBcsl);
//                                    BigDecimal standardRatio = new BigDecimal(ratio);
//                                    if (upRatio.compareTo(standardRatio) == 1) {
//                                        //修改抄表类型原因
//                                        Long typeId = readab.getType();
//                                        readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                                .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                                .set(Readmetereashis::getAbnomalId, typeId)
//                                                .set(Readmetereashis::getShbz, "0")
//                                                .set(Readmetereashis::getAuditReason, describe + "，本次环比升比例为：" + upRatio + "%"));
//                                        return describe + "，本次环比升比例为：" + upRatio + "%";
//                                    }
//                                }
//                                //降
//                                else if ("1".equals(isUpordown)) {
//                                    //前三次水量平均值
//                                    BigDecimal average = getAverage(month1, month2, month3);
//                                    BigDecimal downRatio = getDownRatio(average, bigBcsl);
//                                    BigDecimal standardRatio = new BigDecimal(ratio);
//                                    if (downRatio.compareTo(standardRatio) == 1) {
//                                        //修改抄表类型原因
//                                        Long typeId = readab.getType();
//                                        readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                                .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                                .set(Readmetereashis::getAbnomalId, typeId)
//                                                .set(Readmetereashis::getShbz, "0")
//                                                .set(Readmetereashis::getAuditReason, describe + "，本次环比降比例为：" + downRatio + "%"));
//                                        return describe + "，本次环比降比例为：" + downRatio + "%";
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//
//        } else {
//            //首次抄表模型
//            String isFirst = "1";
//            if (StringUtil.isNull(qfcbqfmxls)) {
//                isFirst = "0";
//            }
//            if (isFirst.equals("1")) {
//                readabnormalList = readabnormalService.list(Wrappers.<Readabnormal>lambdaQuery()
//                        .eq(Readabnormal::getTypeCode, AbnormalEnum.SCSLMX.getTypeCode()));
//                if (readabnormalList.size() > 0) {
//                    for (Readabnormal readab : readabnormalList) {
//                        String describe = readab.getDescribe();
//                        //是否首次抄表
//                        String firstRead = readab.getFirstRead();
//                        //首次抄表水量为0
//                        if ("0".equals(firstRead)) {
//                            if (bczl.equals(0L)) {
//                                //修改抄表类型原因
//                                Long typeId = readab.getType();
//                                readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                        .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                        .set(Readmetereashis::getAbnomalId, typeId)
//                                        .set(Readmetereashis::getShbz, "0")
//                                        .set(Readmetereashis::getAuditReason, describe));
//                                return describe;
//                            }
//                        }
//                        //首次非正常抄表方式
//                        else {
//                            if (!cbfsCode.equals(66L)) {
//                                //修改抄表类型原因
//                                Long typeId = readab.getType();
//                                readmetereashisService.update(Wrappers.<Readmetereashis>lambdaUpdate().eq(Readmetereashis::getYhbh, yhbh)
//                                        .eq(Readmetereashis::getCbrq, cbrq).eq(Readmetereashis::getCbyf, cbyf)
//                                        .set(Readmetereashis::getAbnomalId, typeId)
//                                        .set(Readmetereashis::getShbz, "0")
//                                        .set(Readmetereashis::getAuditReason, describe));
//                                return describe;
//                            }
//                        }
//                    }
//                }
//            }
//        }
        return null;
    }




}
