package com.ruoyi.project.business.service.impl;

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

import com.google.common.collect.Lists;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.importExcel.excel.helper.BasicExcelHelper;
import com.ruoyi.common.importExcel.excel.helper.BasicTableInfoHelper;
import com.ruoyi.common.importExcel.excel.tempInfo.ElevatorParaInfoHelper;
import com.ruoyi.common.importExcel.excel.tempInfo.ParaLibraryExcel;
import com.ruoyi.common.importExcel.excel.tempInfo.ParaLibraryExportHelper;
import com.ruoyi.common.importExcel.excel.tempInfo.ParaSelectExcel;
import com.ruoyi.common.importExcel.utils.ExcelUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.business.domain.*;
import com.ruoyi.project.business.domain.TpAreaElevatorModelPara;
import com.ruoyi.project.business.domain.TpAreaParaSelectItem;
import com.ruoyi.project.business.domain.TpAreaParaStandard;
import com.ruoyi.project.business.domain.vo.paramSelectItem.AreaElevatorModelParaVO;
import com.ruoyi.project.business.domain.vo.paramSelectItem.ElevatorModeParamlVo;
import com.ruoyi.project.business.domain.vo.paramSelectItem.ParaSelectItemVo;
import com.ruoyi.project.business.domain.vo.paramSelectItem.TpParaLibraryVo;
import com.ruoyi.project.business.mapper.*;
import com.ruoyi.project.business.service.ITpAreaElevatorModelParaService;
import com.ruoyi.project.business.service.ITpElevatorModelService;
import com.ruoyi.project.common.domain.CheckVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.Tools.splitList;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class TpAreaElevatorModelParaServiceImpl implements ITpAreaElevatorModelParaService
{
    @Resource
    private TpAreaElevatorModelParaMapper tpAreaElevatorModelParaMapper;
    @Resource
    private TpAreaParaSelectItemMapper tpAreaParaSelectItemMapper;
    @Resource
    private TpParaSelectItemMapper tpParaSelectItemMapper;
    @Resource
    private TpAreaParaStandardMapper tpAreaParaStandardMapper;
    @Resource
    private TpParaLibraryMapper tpParaLibraryMapper;
    @Resource
    private ITpElevatorModelService tpElevatorModelService;
    @Resource
    private TpParabarDetailValueMapper tpParabarDetailValueMapper;
    @Resource
    private TpCaseElevatorMapper tpCaseElevatorMapper;
    @Resource
    private TpParaValidationElevatorMapper tpParaValidationElevatorMapper;
    @Resource
    private TpAreaOptionalParaPriceMapper tpAreaOptionalParaPriceMapper;
    @Resource
    private TpElevatorModelMapper tpElevatorModelMapper;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TpAreaElevatorModelPara selectTpAreaElevatorModelParaById(Long id)
    {
        return tpAreaElevatorModelParaMapper.selectTpAreaElevatorModelParaById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tpAreaElevatorModelPara 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TpAreaElevatorModelPara> selectTpAreaElevatorModelParaList(TpAreaElevatorModelPara tpAreaElevatorModelPara)
    {
        return tpAreaElevatorModelParaMapper.selectTpAreaElevatorModelParaList(tpAreaElevatorModelPara);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tpAreaElevatorModelPara 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTpAreaElevatorModelPara(TpAreaElevatorModelPara tpAreaElevatorModelPara)
    {
        tpAreaElevatorModelPara.setCreateTime(DateUtils.getNowDate());
        return tpAreaElevatorModelParaMapper.insertTpAreaElevatorModelPara(tpAreaElevatorModelPara);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tpAreaElevatorModelPara 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTpAreaElevatorModelPara(TpAreaElevatorModelPara tpAreaElevatorModelPara)
    {
        tpAreaElevatorModelPara.setUpdateTime(DateUtils.getNowDate());
        return tpAreaElevatorModelParaMapper.updateTpAreaElevatorModelPara(tpAreaElevatorModelPara);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaElevatorModelParaByIds(Long[] ids)
    {
        return tpAreaElevatorModelParaMapper.deleteTpAreaElevatorModelParaByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaElevatorModelParaById(Long id)
    {
        return tpAreaElevatorModelParaMapper.deleteTpAreaElevatorModelParaById(id);
    }

    @Override
    public ExcelUtils.UploadResult<ElevatorParaInfoHelper> checkElevatorParaInfo(BasicTableInfoHelper tableInfo, List<ElevatorParaInfoHelper> list) {

        //存在传入数据，设定id
        CheckVO cvo = tableInfo.getCheckVO();
        for(ElevatorParaInfoHelper it :list){
            if(!ObjectUtils.isEmpty(cvo)){
                it.setElevatorModelId(cvo.getId());
            }
        }
        //存放所有kv
        List<String> keys = new ArrayList<>();
        //查询所有有选项的list
        List<TpParaLibraryVo> paraList = tpParaLibraryMapper.selectTpParaLibraryWithItem(new TpParaLibrary());
        Map<String,List<TpParaLibraryVo>> codeMap = paraList.stream()
                 .collect(Collectors.groupingBy(it->it.getParaCode()));


        return ExcelUtils.checkData(list, it -> {

            List<String> businessErrors = Lists.newArrayList();
            if (StringUtils.isEmpty(it.getParaCode())) {
                businessErrors.add("参数编码不允许为空");
            } else {
                if (it.getParaCode().length() > 60) {
                    businessErrors.add("参数编码不允许超过60个字符");
                }
            }
            //验证code是否存在
            if(!codeMap.containsKey(it.getParaCode())){
                businessErrors.add("参数编码不存在");
            }else {
                TpParaLibraryVo paramInfo = codeMap.get(it.getParaCode()).get(0);
                //验证是否为下拉框
                if(StringUtils.isNotEmpty(it.getSelectItemStr())){
                    if(!"02".equals(paramInfo.getInputType()) && !"04".equals(paramInfo.getInputType())){
                        businessErrors.add("参数类型不为下拉框，没有参数范围");
                    }else {

                        //判断默认值是否在选项中
                        if(StringUtils.isNotEmpty(it.getDefaultVal())){
                            if(it.getSelectItemStr().indexOf(it.getDefaultVal())<0){
                                businessErrors.add("参数默认值不在取值范围中");
                            }
                        }
                        //验证选项是否存在,逗号可能是全角,全部替换为半角
                        it.setSelectItemStr(it.getSelectItemStr().replaceAll("，",","));

                        String[] items = it.getSelectItemStr().split(",");
                        List<String> selectItems = paramInfo.getItems().stream().map(TpParaSelectItem::getValue).collect(Collectors.toList());
                        Boolean existFlag = false;
                        for (String item:items){
                            if(selectItems.contains(item)){
                                //代表存在
                            }else {
                                //代表不存在
                                existFlag = true;
                            }
                        }
                        if(existFlag){
                            businessErrors.add("参数取值范围不在选项中");
                        }
                    }
                }

                if(StringUtils.isNotEmpty(it.getDefaultVal())){
                    //检验默认值类型
                    if("01".equals(paramInfo.getInputType())){
                        //整数值输入框
                        try {
                            Integer test = Integer.parseInt(it.getDefaultVal());
                            it.setDefaultVal(test.toString());
                        }catch (Exception e){
                            businessErrors.add("参数类型不为数值");
                        }
                    }
                    if("03".equals(paramInfo.getInputType())){
                        //小数值输入框
                        try {
                            BigDecimal test = new BigDecimal(it.getDefaultVal());
                        }catch (Exception e){
                            businessErrors.add("参数类型不为数值");
                        }

                    }
                }

            }


            //验证参数编码重复
            if (keys.contains(it.getParaCode())) {
                businessErrors.add("参数编码已存在");
            } else {
                keys.add(it.getParaCode());
            }
            //todo 执行验证及其他操作
            return businessErrors;
        });
    }

    /**
     * 新增电梯参数数据
     * @param basicTableInfoHelper
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> insertElevatorModelParaExcel(BasicTableInfoHelper basicTableInfoHelper, List<ElevatorParaInfoHelper> list) {
        List<String> errs = new ArrayList<>();
        //1.获取电梯参数实体
        List<TpAreaElevatorModelPara> paraList = new ArrayList<>();
        List<TpAreaParaStandard> stantist = new ArrayList<>();
        Long sort = 0L;
        Long elevatorModelId = list.get(0).getElevatorModelId();
        //新增电梯参数之前先删除所有梯形数据子数据
        tpElevatorModelService.deleteTpElevatorModelChildByIds(new Long[]{elevatorModelId});

        //查询所有参数
        List<TpParaLibrary> allParam = tpParaLibraryMapper.selectTpParaLibraryList(new TpParaLibrary());

        for(ElevatorParaInfoHelper item:list){
            TpParaLibrary library = allParam.stream().filter(para->item.getParaCode().equals(para.getParaCode()))
                    .findFirst()
                    .get();
            if(ObjectUtils.isEmpty(library)){
                //不存在则新增错误信息
                errs.add("参数："+library.getParaCode()+"不存在！");
            }
            //2.查询所有参数数据
            TpAreaElevatorModelPara param = new TpAreaElevatorModelPara();
            param.setElevatorModelId(item.getElevatorModelId());
            param.setParaId(library.getId());
            param.setParaCode(item.getParaCode());
            param.setParaName(library.getParaName());
            param.setSort(sort);
            param.setCreateBy(SecurityUtils.getUserId()+"");
            param.setCreateTime(new Date());
            paraList.add(param);
            sort++;
        }
        if(errs.size()>0){
            //直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return errs;
        }
        //3.拼接实体并插入数据库
        //分批次插入
        int i = 0;
        List<List<TpAreaElevatorModelPara>> listSp = splitList(paraList, 50);
        if (listSp != null && listSp.size() > 0) {
            for (List<TpAreaElevatorModelPara> listSplit : listSp){
                i += tpAreaElevatorModelParaMapper.InsertBySql(listSplit);
            }
        }
//        int i = tpAreaElevatorModelParaMapper.InsertBySql(paraList);


        List<TpAreaParaSelectItem> addSelectList = new ArrayList<>();
        //4.根据参数实体表paraCode字段去查询所有参数选项
        List<ParaSelectItemVo> allList = tpParaSelectItemMapper.selectTpParaSelectItemWithCodeList(new ParaSelectItemVo());
        for(ElevatorParaInfoHelper item:list){
            TpParaLibrary library = allParam.stream().filter(para->item.getParaCode().equals(para.getParaCode()))
                    .findFirst()
                    .get();
            //查询当前参数下的所有选项,并按照value分组
            Map<String, List<ParaSelectItemVo>> valueMap = allList.stream().filter(it-> item.getParaCode().equals(item.getParaCode()))
                    .collect(Collectors.groupingBy(ParaSelectItemVo::getValue));
            //设置默认值的实体
            TpAreaParaStandard standard = new TpAreaParaStandard();
            standard.setElevatorModelId(item.getElevatorModelId());
            standard.setParaId(library.getId());
            standard.setParaCode(item.getParaCode());
            standard.setParaDefaultValue(item.getDefaultVal());
            //如果存在默认值，并且其是选项
            if(!StringUtils.isEmpty(item.getDefaultVal()) && !StringUtils.isEmpty(item.getSelectItemStr())){
                //则需额外设定id
                if(valueMap.containsKey(item.getDefaultVal())){
                    standard.setParaDefaultId(valueMap.get(item.getDefaultVal()).get(0).getId());
                }
            }
            standard.setCreateBy(SecurityUtils.getUserId()+"");
            standard.setCreateTime(new Date());
            stantist.add(standard);

            //如果存在选项数据
            if(!StringUtils.isEmpty(item.getSelectItemStr())){
                //5.根据字段selectItemStr 用，号分割获取选项表value字段-> selectList
                List<String> selectValues = Arrays.asList(item.getSelectItemStr().split(","));

                //6.根据selectList获取插入选项表实体。若selectList中有不存在的参数则报错 不许新增
                //判断是否所有selectValues都在选项list中
                for(String select :selectValues){
                    if(valueMap.containsKey(select)){
                        //如果存在选项值，则拼接选项实体
                        ParaSelectItemVo param = valueMap.get(select).get(0);
                        TpAreaParaSelectItem selectItem = new TpAreaParaSelectItem();
                        selectItem.setElevatorModelId(item.getElevatorModelId());
                        selectItem.setId(param.getId());
                        selectItem.setParaCode(param.getParaCode());
                        selectItem.setParaSelectItemId(param.getId());
                        selectItem.setParaSelectItemValue(select);

                        selectItem.setParaId(library.getId());

                        selectItem.setCreateBy(SecurityUtils.getUserId()+"");
                        selectItem.setCreateTime(new Date());

                        addSelectList.add(selectItem);
                    }else {
                        //不存在则新增错误信息
                        errs.add("选项："+select+"不存在！");
                    }
                }
            }

        }
        if(errs.size()>0){
            //直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return errs;
        }
        //7.新增实体表
        int j = 0;
        List<List<TpAreaParaSelectItem>> listSI = splitList(addSelectList, 50);
        if (listSI != null && listSI.size() > 0) {
            for (List<TpAreaParaSelectItem> listSplit : listSI){
                j += tpAreaParaSelectItemMapper.InsertBySql(listSplit);
            }
        }
//        int j = tpAreaParaSelectItemMapper.InsertBySql(addSelectList);

        //8.新增默认值表
        int k = 0;
        List<List<TpAreaParaStandard>> listST = splitList(stantist, 50);
        if (listST != null && listST.size() > 0) {
            for (List<TpAreaParaStandard> listSplit : listST){
                k += tpAreaParaStandardMapper.InsertBySql(listSplit);
            }
        }
//        int k = tpAreaParaStandardMapper.InsertBySql(stantist);


        return errs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateModelParam(ElevatorModeParamlVo paramlVo) {
        List<AreaElevatorModelParaVO> paraSelect = paramlVo.getParaSelect();
        List<Long> paraIds = new ArrayList<>();
        List<TpAreaElevatorModelPara> paraList = new ArrayList<>();
        List<Long> delselect = new ArrayList<>();
        List<Long> delstand = new ArrayList<>();
        //1.先查询该梯形下所有参数
        TpAreaElevatorModelPara para = new TpAreaElevatorModelPara();
        para.setElevatorModelId(paramlVo.getId());
        List<TpAreaElevatorModelPara> elvPram = tpAreaElevatorModelParaMapper.selectTpAreaElevatorModelParaList(para);
        //2.根据传入实体对比，找出新增参数和删除参数
        if(!ObjectUtils.isEmpty(elvPram)){
            //如果存在，代表该次操作为更新
            //循环比较是否已存在
            for (AreaElevatorModelParaVO select : paraSelect){
                boolean existFlag = false;
                for (TpAreaElevatorModelPara param : elvPram){
                    if(select.getParamId().equals(param.getParaId())){
                        //存在不处理
                        existFlag = true;
                        break;
                    }
                }
                if(!existFlag){
                    //代表不存在，设置新增实体
                    TpAreaElevatorModelPara param = new TpAreaElevatorModelPara();
                    param.setElevatorModelId(paramlVo.getId());
                    param.setParaId(select.getParamId());
                    param.setParaCode(select.getParaCode());
                    param.setParaName(select.getParaName());
                    param.setSort(select.getSort());
                    param.setCreateBy(SecurityUtils.getUserId()+"");
                    param.setCreateTime(new Date());
                    paraList.add(param);
                }
            }

            //再次循环比较，找出需要删除的
            for (TpAreaElevatorModelPara param : elvPram){
                boolean existFlag2 = false;
                for (AreaElevatorModelParaVO select : paraSelect){
                    if(select.getParamId().equals(param.getParaId())){
                        //存在不处理
                        existFlag2 = true;
                        break;
                    }
                }
                if(!existFlag2){
                    paraIds.add(param.getParaId());
                }
            }

        }else {
            //代表操作为新增，新增参数直接新增
            for (AreaElevatorModelParaVO select : paraSelect){
                TpAreaElevatorModelPara param = new TpAreaElevatorModelPara();
                param.setElevatorModelId(paramlVo.getId());
                param.setParaId(select.getParamId());
                param.setParaCode(select.getParaCode());
                param.setParaName(select.getParaName());
                param.setSort(select.getSort());
                param.setCreateBy(SecurityUtils.getUserId()+"");
                param.setCreateTime(new Date());
                paraList.add(param);
            }
        }
        //新增之前先删除选项及默认值表
        if(ObjectUtils.isNotEmpty(paraIds)){
            Long[] ids = paraIds.toArray(new Long[paraIds.size()]);
            if(!ObjectUtils.isEmpty(ids)){
                //验证是否可以删除
                List<String> res = checkParamUsed(ids,paramlVo.getId());
                if(ObjectUtils.isNotEmpty(res)){
                    throw new ServiceException(String.join(",",res));
                }
                //            List<Long> paraIds = paraList.stream().map(TpAreaElevatorModelPara::getParaId).collect(Collectors.toList());
                //逻辑删除
                int j = tpAreaParaSelectItemMapper.deleteTpAreaParaSelectItemByParaIds(ids,paramlVo.getId());
                int k = tpAreaParaStandardMapper.deleteTpAreaParaStandardByParaIds(ids,paramlVo.getId());
                int l = tpAreaElevatorModelParaMapper.deleteTpAreaElevatorModelParaByParaIds(ids,paramlVo.getId());
                //真实删除
//                int j = tpAreaParaSelectItemMapper.realDeleteTpAreaParaSelectItemByParaIds(ids,paramlVo.getId());
//                int k = tpAreaParaStandardMapper.realDeleteTpAreaParaStandardByParaIds(ids,paramlVo.getId());
//                int l = tpAreaElevatorModelParaMapper.realDeleteTpAreaElevatorModelParaByParaIds(ids,paramlVo.getId());
            }
        }
        if(!ObjectUtils.isEmpty(paraList)){
            //新增参数
            int i = tpAreaElevatorModelParaMapper.InsertBySql(paraList);
        }
        //操作主表
        TpElevatorModel elevatorMode  = new TpElevatorModel();
        elevatorMode.setId(paramlVo.getId());
        elevatorMode.setUpdateTime(DateUtils.getNowDate());
        tpElevatorModelMapper.updateTpElevatorModel(elevatorMode);
        return "";
    }

    @Override
    public List<List<? extends BasicExcelHelper>> elevatorModelExcel(Long elevatorModelIds) {
        List<List<? extends BasicExcelHelper>> result = Lists.newArrayList();
        TpAreaParaSelectItem param = new TpAreaParaSelectItem();
        param.setElevatorModelId(elevatorModelIds);
        List<ParaLibraryExcel> listPara = tpParaLibraryMapper.selectModelExport2(param);
        List<ElevatorParaInfoHelper> list = new ArrayList<>();
        for(ParaLibraryExcel item : listPara){
            ElevatorParaInfoHelper res = new ElevatorParaInfoHelper();
            res.setParaCode(item.getParaCode());
            res.setParaName(item.getParaName());
            if(ObjectUtils.isNotEmpty(item.getSelectVOS())){
                String str = "";
                try {
                    str = item.getSelectVOS().stream().map(ParaSelectExcel::getValue).collect(Collectors.joining(","));
                }catch (Exception e){
                    //异常不管直接不赋值
                }
                res.setSelectItemStr(str);
            }
            res.setDefaultVal(null);
            list.add(res);
        }
        //处理数据
        result.add(list.stream().map(item -> {
            ElevatorParaInfoHelper helper = new ElevatorParaInfoHelper();
            BeanUtils.copyProperties(item, helper);
            return helper;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public ExcelUtils.UploadResult<ParaLibraryExcel> checkElevatorParaInfoNew(BasicTableInfoHelper tableInfo, List<ParaLibraryExcel> list) {
        //存在传入数据，设定id
        CheckVO cvo = tableInfo.getCheckVO();
        for(ParaLibraryExcel it :list){
            if(!ObjectUtils.isEmpty(cvo)){
                it.setElevatorModelId(cvo.getId());
            }
        }
        //存放所有kv
        List<String> keys = new ArrayList<>();

        TpElevatorModel  para = tpElevatorModelMapper.selectTpElevatorModelById(cvo.getId());

        //查询指定类型所有有选项的list
        TpParaLibrary param = new TpParaLibrary();
        param.setElevatorType(para.getElevatorType());
        List<TpParaLibraryVo> paraList = tpParaLibraryMapper.selectTpParaLibraryWithItem(param);
        Map<String,List<TpParaLibraryVo>> codeMap = paraList.stream()
                .collect(Collectors.groupingBy(it->it.getParaCode()));


        return ExcelUtils.checkData(list, it -> {

            List<String> businessErrors = Lists.newArrayList();
            if (StringUtils.isEmpty(it.getParaCode())) {
                businessErrors.add("参数编码不允许为空");
            } else {
                if (it.getParaCode().length() > 60) {
                    businessErrors.add("参数编码不允许超过60个字符");
                }
            }
            //验证code是否存在
            if(!codeMap.containsKey(it.getParaCode())){
                businessErrors.add("参数编码不存在");
            }else {
                List<ParaSelectExcel> selectVOS =it.getSelectVOS();
                TpParaLibraryVo paramInfo = codeMap.get(it.getParaCode()).get(0);
                //验证是否为下拉框
                if(ObjectUtils.isNotEmpty(selectVOS) && StringUtils.isNotEmpty(selectVOS.get(0).getValue())){
                    if(!"02".equals(paramInfo.getInputType()) && !"04".equals(paramInfo.getInputType())){
                        businessErrors.add("参数类型不为下拉框，没有参数范围");
                    }else {
                        //验证选项是否存在,逗号可能是全角,全部替换为半角
                        List<String> selectItems = paramInfo.getItems().stream().map(TpParaSelectItem::getValue).collect(Collectors.toList());
                        Boolean existFlag = false;
                        for (ParaSelectExcel item:selectVOS){
                            if(selectItems.contains(item.getValue())){
                                //代表存在
                            }else {
                                //代表不存在
                                existFlag = true;
                            }
                        }
                        if(existFlag){
                            businessErrors.add("参数取值范围不在选项中");
                        }
                    }
                }
            }


            //验证参数编码重复
            if (keys.contains(it.getParaCode())) {
                businessErrors.add("参数编码已存在");
            } else {
                keys.add(it.getParaCode());
            }
            //todo 执行验证及其他操作
            return businessErrors;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> insertElevatorModelParaExcelNew(BasicTableInfoHelper basicTableInfoHelper, List<ParaLibraryExcel> list) {
        List<String> errs = new ArrayList<>();
        //1.获取电梯参数实体
        List<TpAreaElevatorModelPara> paraList = new ArrayList<>();
        List<TpAreaParaStandard> stantist = new ArrayList<>();
        Long sort = 0L;
        Long elevatorModelId = list.get(0).getElevatorModelId();
        //新增电梯参数之前先删除所有梯形数据子数据
        tpElevatorModelService.deleteTpElevatorModelChildByIds(new Long[]{elevatorModelId});

        //查询所有参数
        List<TpParaLibrary> allParam = tpParaLibraryMapper.selectTpParaLibraryList(new TpParaLibrary());

        for(ParaLibraryExcel item:list){
            TpParaLibrary library = allParam.stream().filter(para->item.getParaCode().equals(para.getParaCode()))
                    .findFirst()
                    .get();
            if(ObjectUtils.isEmpty(library)){
                //不存在则新增错误信息
                errs.add("参数："+library.getParaCode()+"不存在！");
            }
            //2.查询所有参数数据
            TpAreaElevatorModelPara param = new TpAreaElevatorModelPara();
            param.setElevatorModelId(item.getElevatorModelId());
            param.setParaId(library.getId());
            param.setParaCode(item.getParaCode());
            param.setParaName(library.getParaName());
            param.setSort(sort);
            param.setCreateBy(SecurityUtils.getUserId()+"");
            param.setCreateTime(new Date());
            paraList.add(param);
            sort++;
        }
        if(errs.size()>0){
            //直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return errs;
        }
        //3.拼接实体并插入数据库
        //分批次插入
        int i = 0;
        List<List<TpAreaElevatorModelPara>> listSp = splitList(paraList, 50);
        if (listSp != null && listSp.size() > 0) {
            for (List<TpAreaElevatorModelPara> listSplit : listSp){
                i += tpAreaElevatorModelParaMapper.InsertBySql(listSplit);
            }
        }
//        int i = tpAreaElevatorModelParaMapper.InsertBySql(paraList);


        List<TpAreaParaSelectItem> addSelectList = new ArrayList<>();
        //4.根据参数实体表paraCode字段去查询所有参数选项
        List<ParaSelectItemVo> allList = tpParaSelectItemMapper.selectTpParaSelectItemWithCodeList(new ParaSelectItemVo());
        for(ParaLibraryExcel item:list){
            TpParaLibrary library = allParam.stream().filter(para->item.getParaCode().equals(para.getParaCode()))
                    .findFirst()
                    .get();
            //查询当前参数下的所有选项,并按照value分组
            Map<String, List<ParaSelectItemVo>> valueMap = allList.stream().filter(it-> item.getParaCode().equals(it.getParaCode()))
                    .collect(Collectors.groupingBy(ParaSelectItemVo::getValue));

            //如果存在选项数据
            if(ObjectUtils.isNotEmpty(item.getSelectVOS()) && StringUtils.isNotEmpty(item.getSelectVOS().get(0).getValue())){
                //5.根据List-SelectVOS字段，获取选项List-> selectList
                //6.根据selectList获取插入选项表实体。若selectList中有不存在的参数则报错 不许新增
                //判断是否所有selectValues都在选项list中
                for(ParaSelectExcel select :item.getSelectVOS()){
                    if(valueMap.containsKey(select.getValue())){
                        //如果存在选项值，则拼接选项实体
                        ParaSelectItemVo param = valueMap.get(select.getValue()).get(0);
                        TpAreaParaSelectItem selectItem = new TpAreaParaSelectItem();
                        selectItem.setElevatorModelId(item.getElevatorModelId());
                        selectItem.setId(param.getId());
                        selectItem.setParaCode(param.getParaCode());
                        selectItem.setParaSelectItemId(param.getId());
                        selectItem.setParaSelectItemValue(select.getValue());
                        selectItem.setParaId(library.getId());

                        selectItem.setCreateBy(SecurityUtils.getUserId()+"");
                        selectItem.setCreateTime(new Date());

                        addSelectList.add(selectItem);
                    }else {
                        //不存在则新增错误信息
                        errs.add("选项："+select+"不存在！");
                    }
                }
            }

        }
        if(errs.size()>0){
            //直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return errs;
        }
        //7.新增实体表
        int j = 0;
        List<List<TpAreaParaSelectItem>> listSI = splitList(addSelectList, 50);
        if (listSI != null && listSI.size() > 0) {
            for (List<TpAreaParaSelectItem> listSplit : listSI){
                j += tpAreaParaSelectItemMapper.InsertBySql(listSplit);
            }
        }
//        int j = tpAreaParaSelectItemMapper.InsertBySql(addSelectList);
        return errs;
    }

    private List<String> checkParamUsed(Long[] ids,Long elevatorModelId){
        List<String> res = new ArrayList<>();
        //检查是否被参数包使用
        int i = tpParabarDetailValueMapper.selectBarCountWithModel(ids,elevatorModelId);
        if(i>0){
            //返回无法删除
//            throw new ServiceException(MessageUtils.message("common.hasBar"));
            //throw new ServiceException("该参数已被参数包使用，无法删除！");
            res.add("该参数已被参数包使用，无法删除！");
        }
        //检查是否被参数联动使用
        int k = tpCaseElevatorMapper.selectCaseCountWithModel(ids,elevatorModelId);
        //模糊查询参数联动
        int j = tpCaseElevatorMapper.selectCaseCountLikeWithModel(ids,elevatorModelId);
        if(k+j>0){
            //返回无法删除
//            throw new ServiceException(MessageUtils.message("common.hasValidationPara"));
            //throw new ServiceException("该参数已被参数联动使用，无法删除！");
            res.add("该参数已被参数联动使用，无法删除！");
        }

        //模糊查询规则校验
        int validationNum = tpParaValidationElevatorMapper.getValidationCountWithModel(ids,elevatorModelId);
        if (validationNum > 0) {
            //返回无法删除
            //throw new ServiceException(MessageUtils.message("elevator.delete.hasValidation"));
            //throw new ServiceException("该梯型存在规则校验数据，无法删除！");
            res.add("该参数已被规则校验使用，无法删除！");
        }
        //模糊查询价格表
        int optNum = tpAreaOptionalParaPriceMapper.getOptionalParaPriceCountWithModel(ids,elevatorModelId);
        if (optNum > 0) {
            //返回无法删除
            //throw new ServiceException(MessageUtils.message("elevator.delete.hasValidation"));
            //throw new ServiceException("该梯型存在规则校验数据，无法删除！");
            res.add("该参数已被价格表使用，无法删除！");
        }
        return res;
    }
}
