/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.meterRead.AbnormalBig;
import com.tofly.watermeter.custom.entity.meterRead.BingYsxz;
import com.tofly.watermeter.em.AbnormalBigFiledEnum;
import com.tofly.watermeter.em.AbnormalEnum;
import com.tofly.watermeter.entity.*;
import com.tofly.watermeter.service.ReadabnormalService;
import com.tofly.watermeter.service.ReadbigabnormalService;
import com.tofly.watermeter.service.ReadbigysxzService;
import com.tofly.watermeter.service.ReadbingysxzService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.watermeter.mapper.ReadbigysxzMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抄表模型算法--用水性质大类
 *
 * @author wangjieliu
 * @date Mon Jan 10 00:00:00 CST 2022
 */
@Service("readbigysxzService")
public class ReadbigysxzServiceImpl extends ServiceImpl<ReadbigysxzMapper, Readbigysxz> implements ReadbigysxzService {

    @Autowired
    private ReadbingysxzService readbingysxzService;
    @Autowired
    private ReadbigabnormalService readbigabnormalService;
    @Autowired
    private ReadabnormalService readabnormalService;


    @Override
    public IPage<QfmidysxzbmE> getNoBingYsxz(Page<QfmidysxzbmE> page) {
        return baseMapper.getNoBingYsxz(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteReadBig(String ids) {
        List<String> bigYsxzListId = Arrays.asList(ids.split(","));
        //根据用水性质大类查询用水性质
        List<Readbingysxz> readbingysxzList = readbingysxzService
                .list(Wrappers.<Readbingysxz>lambdaQuery().in(Readbingysxz::getBigYsxzid, bigYsxzListId).select(Readbingysxz::getYsxzBm));
        if (StringUtil.isNotNull(readbingysxzList) && readbingysxzList.size() > 0) {
            //删除归类中列表，则需要将模型详情中的配置也删除
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery().in(Readabnormal::getYsxzBm, readbingysxzList.stream().map(Readbingysxz::getYsxzBm).collect(Collectors.toList())));
        }
        //删除归类模型中的配置
        readbigabnormalService.remove(Wrappers.<Readbigabnormal>lambdaQuery().in(Readbigabnormal::getBigYsxzId, bigYsxzListId));
        //删除归类绑定信息
        readbingysxzService.remove(Wrappers.<Readbingysxz>lambdaQuery().in(Readbingysxz::getBigYsxzid, bigYsxzListId));
        //删除大类
        this.removeByIds(bigYsxzListId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone<Object> saveBingYsxz(BingYsxz bingYsxz) {
        String ysxzBm = bingYsxz.getYsxzBm();
        String classifyType = bingYsxz.getClassifyType();
        Long bigYsxz = bingYsxz.getBigYsxz();
        if (StringUtil.isEmpty(ysxzBm)) {
            throw new ToflyDeniedException("请选择用水性质");
        }
//        if (StringUtil.isNull(bigYsxz)) {
//            throw new ToflyDeniedException("请选择归类大类");
//        }
        if (StringUtil.isEmpty(classifyType)) {
            throw new ToflyDeniedException("传递归类类型");
        }
        List<String> ysxzList = Arrays.asList(ysxzBm.split(","));
        //归类
        if ("0".equals(classifyType)) {
            List<Readbingysxz> readbingysxzList = readbingysxzService.list(Wrappers.<Readbingysxz>lambdaQuery()
                    .in(Readbingysxz::getYsxzBm, ysxzList).select(Readbingysxz::getId, Readbingysxz::getYsxzBm));
            if (StringUtil.isNotNull(readbingysxzList) && readbingysxzList.size() > 0) {
                throw new ToflyDeniedException("已存在相同归类，请重新刷新选择");
            }
            Date currentDate = Date8Util.getCurrentDate();
            //由于用水性质归类的时候不多，所以采取了for循环插入数据库
            for (String ysxz : ysxzList) {
                Readbingysxz readbingysxz = new Readbingysxz();
                readbingysxz.setCreateDate(currentDate);
                readbingysxz.setYsxzBm(ysxz);
                readbingysxz.setBigYsxzid(bigYsxz);
                //还需要判断如果大类配置了模型的情况，移入到大类里面，则需要将大类里面的模型参数算法在详情表中默认添加好
                readbingysxzService.save(readbingysxz);
            }
            //查询该大类是否有模型配置
            List<Readbigabnormal> readbigabnormalList = readbigabnormalService.list(Wrappers.<Readbigabnormal>lambdaQuery().eq(Readbigabnormal::getBigYsxzId, bigYsxz));
            if (StringUtil.isNotNull(readbigabnormalList)) {
                for (Readbigabnormal readbigabnormal : readbigabnormalList) {
                    //添加到模型详情配置中
                    insertAbnormal(readbigabnormal, ysxzList);
                }
            }
        }
        //移除归类
        else {
            //删除模型详情配置
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery().in(Readabnormal::getYsxzBm, ysxzList));
            //移除绑定
            readbingysxzService.remove(Wrappers.<Readbingysxz>lambdaQuery().in(Readbingysxz::getYsxzBm, ysxzList));
        }
        return ResultRespone.success(true, "归类成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone<Object> saveModel(Readbigabnormal operateAbnomal) {
        //用水性质大类
        Long ysxzBigId = operateAbnomal.getBigYsxzId();
        //水表口径
        String sbkjName = operateAbnomal.getSbkjName();
        //有值则表示：删除字段中的数据
        String fileName = operateAbnomal.getFieldName();
        //传递的阈值
        Float thresholdValue = operateAbnomal.getThresholdValue();
        if (StringUtil.isEmpty(sbkjName) || StringUtil.isNull(ysxzBigId)) {
            throw new ToflyDeniedException("请选择对应用水性质或水表口径");
        }
        //是否归类，默认未归类
        boolean ysxzFlag = false;
        List<String> ysxzList = null;
        //根据用水性质大类查询用水性质
        List<Readbingysxz> readbingysxzList = readbingysxzService
                .list(Wrappers.<Readbingysxz>lambdaQuery().eq(Readbingysxz::getBigYsxzid, ysxzBigId).select(Readbingysxz::getYsxzBm));
        if (CollectionUtils.isNotEmpty(readbingysxzList)) {
            ysxzFlag = true;
            ysxzList = readbingysxzList.stream().map(Readbingysxz::getYsxzBm).collect(Collectors.toList());
        }
        //默认不存在
        boolean bigabnormalFlag = false;
        //查询是否已存在该配置
        Readbigabnormal existAbnormal = readbigabnormalService.getOne(Wrappers.<Readbigabnormal>lambdaQuery()
                .eq(Readbigabnormal::getBigYsxzId, ysxzBigId).eq(Readbigabnormal::getSbkjName, sbkjName));
        //已保存到数据库的阈值
        Float existThresholdValue = null;
        //存在（true）则修改，不存在（false）则添加
        if (StringUtil.isNotNull(existAbnormal)) {
            bigabnormalFlag = true;
            operateAbnomal.setId(existAbnormal.getId());
            //保存在数据库的阈值
            existThresholdValue = existAbnormal.getThresholdValue();
            operateAbnomal.setThresholdValue(existThresholdValue);
        }
        //如果有传递的阈值：一：一开始传递阈值的情况；二：其他参数已经设置好后，修改阈值的情况（这个才是主要的）
        if (StringUtil.isNotNull(thresholdValue)) {
            //这里表示之前存在了模型配置，本次是配置阈值的情况
            if (StringUtil.isNotNull(existAbnormal)) {
                BeanUtils.copyProperties(existAbnormal, operateAbnomal);
            }
            //设置本次最新阈值
            operateAbnomal.setThresholdValue(thresholdValue);
        }
        //移除值
        if (StringUtil.isNotEmpty(fileName)) {
            Long id = existAbnormal.getId();
            //水量突升比例模型值  可以用switch
            if (fileName.equals(AbnormalBigFiledEnum.WATER_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLTSMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLTSMX.getTypeId()));
                }
            }
            //水量突降比例模型值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLTJMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLTJMX.getTypeId()));
                }
            }
            //同比升比例模型值
            else if (fileName.equals(AbnormalBigFiledEnum.YEAR_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getYearUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.TBSMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.TBSMX.getTypeId()));
                }
            }
            //同比降比例模型值
            else if (fileName.equals(AbnormalBigFiledEnum.YEAR_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getYearDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.TBJMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.TBJMX.getTypeId()));
                }
            }
            //环比升比例模型值
            else if (fileName.equals(AbnormalBigFiledEnum.CHAIN_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getChainUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.HBSMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.HBSMX.getTypeId()));
                }
            }
            //环比降比例模型值
            else if (fileName.equals(AbnormalBigFiledEnum.CHAIN_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getChainDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.HBJMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.HBJMX.getTypeId()));
                }
            }
            //0水量模型值
            else if (fileName.equals(AbnormalBigFiledEnum.ZERO_WATER.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getZeroWater, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.LSLMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.LSLMX.getTypeId()));
                }
            }
            //水量微幅波动同比升值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_YEAR_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterYearUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLWFBDMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLWFBDMX.getTypeId())
                            .eq(Readabnormal::getIsYearorchain , "0").eq(Readabnormal::getIsUpordown , "0"));
                }
            }
            //水量微幅波动同比降值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_YEAR_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterYearDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLWFBDMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLWFBDMX.getTypeId())
                            .eq(Readabnormal::getIsYearorchain , "0").eq(Readabnormal::getIsUpordown , "1"));
                }
            }
            //水量微幅波动环比升值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_CHAIN_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterChainUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLWFBDMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLWFBDMX.getTypeId())
                            .eq(Readabnormal::getIsYearorchain , "1").eq(Readabnormal::getIsUpordown , "1"));
                }
            }
            //水量微幅波动环比降值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_CHAIN_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterChainDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLWFBDMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLWFBDMX.getTypeId())
                            .eq(Readabnormal::getIsYearorchain , "1").eq(Readabnormal::getIsUpordown , "1"));
                }
            }
            //水量连续升次数
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_CONTINUOUS_UP.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterContinuousUp, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLLXSSMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLLXSSMX.getTypeId()));
                }
            }
            //水量连续降次数
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_CONTINUOUS_DOWN.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterContinuousDown, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.SLLXXJMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.SLLXXJMX.getTypeId()));
                }
            }
            //水量连续相同值
            else if (fileName.equals(AbnormalBigFiledEnum.WATER_CONTINUOUS_EQUAL.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getWaterContinuousEqual, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .eq(Readabnormal::getTypeCode , AbnormalEnum.LXSLXTMX.getTypeCode()).eq(Readabnormal::getType , AbnormalEnum.LXSLXTMX.getTypeId()));
                }
            }
            //阈值
            else if (fileName.equals(AbnormalBigFiledEnum.THRESHOLD_VALUE.getFiledName())) {
                readbigabnormalService.update(Wrappers.<Readbigabnormal>lambdaUpdate().set(Readbigabnormal::getThresholdValue, "").eq(Readbigabnormal::getId, id));
                if (ysxzFlag){
                    //有阈值的几种模型配置
                    List<String> stringList =
                            Arrays.asList(AbnormalEnum.SLTSMX.getTypeCode(),AbnormalEnum.SLTJMX.getTypeCode(),AbnormalEnum.TBSMX.getTypeCode(),
                                    AbnormalEnum.TBJMX.getTypeCode(),AbnormalEnum.HBSMX.getTypeCode(),AbnormalEnum.HBJMX.getTypeCode(),AbnormalEnum.SLWFBDMX.getTypeCode());
                    //可以删除详情配置中的模型配置了，因为如果置空了一个数值字段，那么改配置参数条件都不满足了，并且直接删除的好处就是后面不用再次判断数据库中有没有这个配置
                    readabnormalService.remove(Wrappers.<Readabnormal>lambdaUpdate().eq(Readabnormal::getSbkjBm , sbkjName).in(Readabnormal::getYsxzBm , ysxzList)
                            .in(Readabnormal::getTypeCode , stringList));
                }
            } else {
                throw new ToflyDeniedException("请正确传递字段名称");
            }
        } else {
            if (bigabnormalFlag) {
                //修改数据库的大类模型
                readbigabnormalService.updateById(operateAbnomal);
                //只要bigabnormalFlag存在，那么模型详情表中肯定也存在，移除用水性质归类，会删除模型详情配置表，但是移入到用水性质大类中也会默认添加当前bigabnormalFlag中的配置，所以一定和bigabnormalFlag是共同存在的
            } else {
                //保存到数据库的大类模型
                readbigabnormalService.save(operateAbnomal);
            }
            //如果该大类下有用水性质的归类，则进行模型算法异常详情的配置
            if (ysxzFlag) {
                insertAbnormal(operateAbnomal, ysxzList);
            }
        }
        return ResultRespone.success(true, "成功");
    }

    /**
     * 保存异常模型详情
     *
     * @param operateAbnomal 归类大类的模型配置
     * @param ysxzList       用水性质集合
     * @return 返回
     */
    @Transactional(rollbackFor = Exception.class)
    private boolean insertAbnormal(Readbigabnormal operateAbnomal, List<String> ysxzList) {
        String sbkjName = operateAbnomal.getSbkjName();//水表口径
        Float waterUp = operateAbnomal.getWaterUp();//水量突升比例模型
        Float waterDown = operateAbnomal.getWaterDown();//水量突降比例模型值
        Float yearUp = operateAbnomal.getYearUp();//同比升比例模型值
        Float yearDown = operateAbnomal.getYearDown();//同比降比例模型值
        Float chainUp = operateAbnomal.getChainUp();//环比升比例模型值
        Float chainDown = operateAbnomal.getChainDown();//环比降比例模型值
        Integer zeroWater = operateAbnomal.getZeroWater();//0水量模型值
        Float waterYearUp = operateAbnomal.getWaterYearUp();//水量微幅波动同比升值
        Float waterYearDown = operateAbnomal.getWaterYearDown();//水量微幅波动同比降值
        Float waterChainUp = operateAbnomal.getWaterChainUp();//水量微幅波动环比升值
        Float waterChainDown = operateAbnomal.getWaterChainDown();//水量微幅波动环比降值
        Integer waterContinuousUp = operateAbnomal.getWaterContinuousUp();//水量连续升值
        Integer waterContinuousDown = operateAbnomal.getWaterContinuousDown();//水量连续降值
        Integer waterContinuousEqual = operateAbnomal.getWaterContinuousEqual();//水量连续相同值
        Float existThresholdValue = operateAbnomal.getThresholdValue();//阈值
        String ysxzStr = String.join(",", ysxzList);
        //判断模型
        //水量突升模型
        if (StringUtil.isNotNull(waterUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLTSMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));
            //添加
            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            //最小平均基准值
            readabnormal.setMinAverageValue(existThresholdValue);
            //比例
            readabnormal.setRatio(waterUp);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量突降模型
        if (StringUtil.isNotNull(waterDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLTJMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            //最小平均基准值
            readabnormal.setMinAverageValue(existThresholdValue);
            //比例
            readabnormal.setRatio(waterDown);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //同比升模型
        if (StringUtil.isNotNull(yearUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.TBSMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setRatio(yearUp);
            readabnormal.setMinAverageValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //同比降模型
        if (StringUtil.isNotNull(yearDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.TBJMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setRatio(yearDown);
            readabnormal.setMinAverageValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //环比升模型
        if (StringUtil.isNotNull(chainUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.HBSMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setRatio(chainUp);
            readabnormal.setMinAverageValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //环比降模型
        if (StringUtil.isNotNull(chainDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.HBJMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setRatio(chainDown);
            readabnormal.setMinAverageValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //0水量模型
        if (StringUtil.isNotNull(zeroWater)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.LSLMX;
            //先删除，再添加就行
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setCount(zeroWater);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量微幅波动模型
        if (StringUtil.isNotNull(waterYearUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLWFBDMX;
            //先删除，再添加就行  0：同比；1：环比    0：升；1：降
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getIsYearorchain, "0").eq(Readabnormal::getIsUpordown, "0")
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));
            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setIsYearorchain("0");
            readabnormal.setIsUpordown("0");
            readabnormal.setRatio(waterYearUp);
            readabnormal.setFiducialValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量微幅波动模型
        if (StringUtil.isNotNull(waterYearDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLWFBDMX;
            //先删除，再添加就行  0：同比；1：环比    0：升；1：降
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getIsYearorchain, "0").eq(Readabnormal::getIsUpordown, "1")
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setIsYearorchain("0");
            readabnormal.setIsUpordown("1");
            readabnormal.setRatio(waterYearDown);
            readabnormal.setFiducialValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量微幅波动模型
        if (StringUtil.isNotNull(waterChainUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLWFBDMX;
            //先删除，再添加就行  0：同比；1：环比    0：升；1：降
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getIsYearorchain, "1").eq(Readabnormal::getIsUpordown, "0")
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setIsYearorchain("1");
            readabnormal.setIsUpordown("0");
            readabnormal.setRatio(waterChainUp);
            readabnormal.setFiducialValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量微幅波动模型
        if (StringUtil.isNotNull(waterChainDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLWFBDMX;
            //先删除，再添加就行  0：同比；1：环比    0：升；1：降
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getIsYearorchain, "1").eq(Readabnormal::getIsUpordown, "1")
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setIsYearorchain("1");
            readabnormal.setIsUpordown("1");
            readabnormal.setRatio(waterChainDown);
            readabnormal.setFiducialValue(existThresholdValue);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量连续上升模型
        if (StringUtil.isNotNull(waterContinuousUp)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLLXSSMX;
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));
            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setCount(waterContinuousUp);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //水量连续下降模型
        if (StringUtil.isNotNull(waterContinuousDown)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.SLLXXJMX;
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));

            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setCount(waterContinuousDown);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        //连续水量相同模型
        if (StringUtil.isNotNull(waterContinuousEqual)) {
            AbnormalEnum abnormalEnum = AbnormalEnum.LXSLXTMX;
            readabnormalService.remove(Wrappers.<Readabnormal>lambdaQuery()
                    .eq(Readabnormal::getSbkjBm, sbkjName).in(Readabnormal::getYsxzBm, ysxzList)
                    .eq(Readabnormal::getTypeCode, abnormalEnum.getTypeCode()).eq(Readabnormal::getType, abnormalEnum.getTypeId()));
            Readabnormal readabnormal = new Readabnormal();
            readabnormal.setSbkjBm(sbkjName);
            readabnormal.setYsxzBm(ysxzStr);
            readabnormal.setTypeCode(abnormalEnum.getTypeCode());
            readabnormal.setType(abnormalEnum.getTypeId());
            readabnormal.setModelName(abnormalEnum.getModelName());
            readabnormal.setTypeName(abnormalEnum.getTypeState());
            readabnormal.setCount(waterContinuousEqual);
            readabnormalService.saveAbnormalArith2(readabnormal);

        }
        return true;
    }


}
