package com.css.simulation.resource.server.app.service;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.scene.SystemScenePackageParam;
import api.common.pojo.param.scene.SystemUserSceneParam;
import api.common.pojo.po.scene.*;
import api.common.pojo.vo.scene.SystemScenePackageVO;
import api.common.pojo.vo.scene.SystemUserSceneVO;
import api.common.pojo.vo.scene.UserSceneVO;
import api.common.util.CollectionUtil;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import api.common.util.TimeUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SystemScenePackageService {

    @Resource
    private SystemScenePackageMapper systemScenePackageMapper;
    @Resource
    private SystemScenePackageSublistMapper systemScenePackageSublistMapper;
    @Resource
    private SystemUserSceneMapper systemUserSceneMapper;
    @Resource
    private LogService logService;
    @Resource
    private SceneAccidentMapper sceneAccidentMapper;
    @Resource
    private SceneGeneralTemplateMapper sceneGeneralTemplateMapper;
    @Resource
    private SceneNaturalMapper sceneNaturalMapper;
    @Resource
    private  SceneStandardsRegulationsMapper sceneStandardsRegulationsMapper;

    /**
     * 场景包录入
     */
    @SneakyThrows
    public ResponseBodyVO<String> saveSystemScenePackage(SystemScenePackageNewPO params) {

        SystemScenePackagePO po = new SystemScenePackagePO();
        //场景包名称不允许重复
        String packageId = systemScenePackageMapper.queryScenePackageByPackageName(params);
        if (ObjectUtil.isNotNull(packageId)) {
            return new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "该场景包名称已存在，请检查");
        }
        po.setPackageName(params.getPackageName());
        po.setSceneDescribe(params.getSceneDescribe());
        String packId = StringUtil.getRandomUUID();
        po.setId(packId);
        po.setCreateTime(TimeUtil.getNowForMysql());
        po.setModifyTime(TimeUtil.getNowForMysql());
        po.setCreateUserId(AuthUtil.getCurrentUserId());
        po.setIsDeleted("0");
        //zr
        List<String> zrSceneNames = params.getZrSceneNames();
        if (ObjectUtil.isNotNull(zrSceneNames)) {
            List<SystemScenePackageSublistPO> zrList = new ArrayList<>();
            for (String zrSceneName : zrSceneNames) {
                SystemScenePackageSublistPO zrPo = new SystemScenePackageSublistPO();
                zrPo.setSublistId(StringUtil.getRandomUUID());
                zrPo.setSceneType("1");
                zrPo.setSceneName(zrSceneName);
                zrPo.setSceneAndPackage(packId);
                zrPo.setCreateTime(TimeUtil.getNowForMysql());
                zrPo.setModifyTime(TimeUtil.getNowForMysql());
                zrPo.setCreateUserId(AuthUtil.getCurrentUserId());
                zrPo.setIsDeleted("0");
                zrList.add(zrPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(zrList);
        }
//zr
        List<String> bzSceneNames = params.getBzSceneNames();
        if (ObjectUtil.isNotNull(bzSceneNames)) {
            List<SystemScenePackageSublistPO> bzList = new ArrayList<>();
            for (String bzSceneName : bzSceneNames) {
                SystemScenePackageSublistPO bzPo = new SystemScenePackageSublistPO();
                bzPo.setSublistId(StringUtil.getRandomUUID());
                bzPo.setSceneType("2");
                bzPo.setSceneName(bzSceneName);
                bzPo.setSceneAndPackage(packId);
                bzPo.setCreateTime(TimeUtil.getNowForMysql());
                bzPo.setModifyTime(TimeUtil.getNowForMysql());
                bzPo.setIsDeleted("0");
                bzPo.setCreateUserId(AuthUtil.getCurrentUserId());
                bzList.add(bzPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(bzList);
        }
        //zr
        List<String> jtSceneNames = params.getJtSceneNames();
        if (ObjectUtil.isNotNull(jtSceneNames)) {
            List<SystemScenePackageSublistPO> jtList = new ArrayList<>();
            for (String jtSceneName : jtSceneNames) {
                SystemScenePackageSublistPO jtPo = new SystemScenePackageSublistPO();
                jtPo.setSublistId(StringUtil.getRandomUUID());
                jtPo.setSceneType("3");
                jtPo.setSceneName(jtSceneName);
                jtPo.setSceneAndPackage(packId);
                jtPo.setCreateTime(TimeUtil.getNowForMysql());
                jtPo.setModifyTime(TimeUtil.getNowForMysql());
                jtPo.setCreateUserId(AuthUtil.getCurrentUserId());
                jtPo.setIsDeleted("0");
                jtList.add(jtPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(jtList);
        }
        //zr
        List<String> fhSceneNames = params.getFhSceneNames();
        if (ObjectUtil.isNotNull(fhSceneNames)) {
            List<SystemScenePackageSublistPO> fhList = new ArrayList<>();
            for (String fhSceneName : fhSceneNames) {
                SystemScenePackageSublistPO fhPo = new SystemScenePackageSublistPO();
                fhPo.setSublistId(StringUtil.getRandomUUID());
                fhPo.setIsDeleted("0");
                fhPo.setSceneType("4");
                fhPo.setSceneName(fhSceneName);
                fhPo.setSceneAndPackage(packId);
                fhPo.setCreateTime(TimeUtil.getNowForMysql());
                fhPo.setModifyTime(TimeUtil.getNowForMysql());
                fhPo.setCreateUserId(AuthUtil.getCurrentUserId());
                fhList.add(fhPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(fhList);
        }
        systemScenePackageMapper.saveSystemScenePackage(po);

        //记录场景包新增
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_INSERT, po, null);

        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
    }

    //场景包列表
    @SneakyThrows
    public List<SystemScenePackageVO> querySystemScenePackageList(SystemScenePackageParam params) {
        List<SystemScenePackageVO> list = systemScenePackageMapper.querySystemScenePackageList(params);
        for (SystemScenePackageVO vo : list) {
            SystemScenePackageSublistPO sublistPo = new SystemScenePackageSublistPO();
            sublistPo.setSceneAndPackage(vo.getId());
            sublistPo.setSceneType("1");
            List<String> zrList = systemScenePackageSublistMapper.querySceneNaturalList(sublistPo);
            sublistPo.setSceneType("2");
            List<String> bzList = systemScenePackageSublistMapper.querySceneStandardsRegulationsList(sublistPo);
            sublistPo.setSceneType("3");
            List<String> jtList = systemScenePackageSublistMapper.querySceneAccidentList(sublistPo);
            sublistPo.setSceneType("4");
            List<String> fhList = systemScenePackageSublistMapper.querySceneGeneralTemplateList(sublistPo);
            vo.setZrCount(zrList.size());
            vo.setBzCount(bzList.size());
            vo.setJtCount(jtList.size());
            vo.setFhCount(fhList.size());
        }


        return list;
    }

    //
    @SneakyThrows
    public SystemScenePackageNewPO querySystemScenePackageById(SystemScenePackageParam params) {
        SystemScenePackageNewPO newPo = new SystemScenePackageNewPO();
        SystemScenePackagePO po = systemScenePackageMapper.querySystemScenePackageById(params);
        SystemScenePackageSublistPO sublistPo = new SystemScenePackageSublistPO();
        sublistPo.setSceneAndPackage(params.getId());
        sublistPo.setSceneType("1");
        List<String> zrList = systemScenePackageSublistMapper.querySceneNaturalList(sublistPo);
        sublistPo.setSceneType("2");
        List<String> bzList = systemScenePackageSublistMapper.querySceneStandardsRegulationsList(sublistPo);
        sublistPo.setSceneType("3");
        List<String> jtList = systemScenePackageSublistMapper.querySceneAccidentList(sublistPo);
        sublistPo.setSceneType("4");
        List<String> fhList = systemScenePackageSublistMapper.querySceneGeneralTemplateList(sublistPo);
        BeanUtils.copyProperties(po, newPo);
        newPo.setZrSceneNames(zrList);
        newPo.setBzSceneNames(bzList);
        newPo.setJtSceneNames(jtList);
        newPo.setFhSceneNames(fhList);

        //场景包多少用户
        List<UserSceneVO> userIds = systemUserSceneMapper.queryUserByPackageId(params.getId());
        newPo.setUserIds(userIds);
        return newPo;
    }

    //编辑场景包
    @SneakyThrows
    public ResponseBodyVO<String> updateSystemScenePackage(SystemScenePackageNewPO params) {
        //场景包名称不允许重复
        String packageId = systemScenePackageMapper.queryScenePackageByPackageName(params);
        if (ObjectUtil.isNotNull(packageId)) {
            return new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "该场景包名称已存在，请检查");
        }
        SystemScenePackagePO po = new SystemScenePackagePO();
        po.setPackageName(params.getPackageName());
        po.setSceneDescribe(params.getSceneDescribe());
        String packId = params.getId();
        po.setId(packId);
        //删除之前的子表
        SystemScenePackageSublistPO sublistPO = new SystemScenePackageSublistPO();
        sublistPO.setModifyTime(TimeUtil.getNowForMysql());
        sublistPO.setModifyUserId(AuthUtil.getCurrentUserId());
        sublistPO.setSceneAndPackage(packId);
        sublistPO.setIsDeleted("1");
        systemScenePackageSublistMapper.deleteSystemScenePackageSublist(sublistPO);
        //zr
        List<String> zrIds = params.getZrSceneNames();
        if (ObjectUtil.isNotNull(zrIds)) {
            List<SystemScenePackageSublistPO> zrList = new ArrayList<>();
            for (String id : zrIds) {
                //加共有
                SceneNaturalPO paramPo = new SceneNaturalPO();
                paramPo.setShare("0");
                paramPo.setNaturalName(id);
                SceneNaturalPO syPo = sceneNaturalMapper.querySceneNaturaByName(paramPo);
                syPo.setShare("2");
                SceneNaturalPO gyPo = sceneNaturalMapper.querySceneNaturaByName(syPo);
                if (ObjectUtil.isNull(gyPo)) {
                    log.info("场景包新增场景----" + syPo.getShare());
                    // -------------------------------- 保存数据到 mysql --------------------------------
                    syPo.setCreateTime(TimeUtil.getNowForMysql());
                    syPo.setCreateUserId(AuthUtil.getCurrentUserId());
                    syPo.setModifyTime(TimeUtil.getNowForMysql());
                    syPo.setModifyUserId("");
                    syPo.setNaturalId(StringUtil.getRandomUUID());
                    sceneNaturalMapper.saveSceneNatural(syPo);
                } else {

                    // -------------------------------- 修改数据到 mysql --------------------------------
                    syPo.setModifyTime(TimeUtil.getNowForMysql());
                    syPo.setModifyUserId(AuthUtil.getCurrentUserId());
                    syPo.setShare(gyPo.getShare());
                    syPo.setNaturalId(gyPo.getNaturalId());
                    sceneNaturalMapper.updateSceneNatural(syPo);
                    log.info("场景包修改场景----" + syPo.getShare());
                }


                SystemScenePackageSublistPO zrPo = new SystemScenePackageSublistPO();
                zrPo.setSublistId(StringUtil.getRandomUUID());
                zrPo.setSceneType("1");
                zrPo.setSceneName(id);
                zrPo.setSceneAndPackage(packId);
                zrPo.setCreateTime(TimeUtil.getNowForMysql());
                zrPo.setModifyTime(TimeUtil.getNowForMysql());
                zrPo.setCreateUserId(AuthUtil.getCurrentUserId());
                zrPo.setIsDeleted("0");
                zrList.add(zrPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(zrList);
        }
//zr
        List<String> bzIds = params.getBzSceneNames();
        if (ObjectUtil.isNotNull(bzIds)) {
            List<SystemScenePackageSublistPO> bzList = new ArrayList<>();
            for (String id : bzIds) {
                StandardsRegulationsPO standardsRegulationsPO = new StandardsRegulationsPO();
                standardsRegulationsPO.setSceneName(id);
                standardsRegulationsPO.setShare("0");
                StandardsRegulationsPO standardsRegulationsPOSy = sceneStandardsRegulationsMapper.queryStandardsRegulationsByName(standardsRegulationsPO);
                standardsRegulationsPOSy.setShare("2");
                StandardsRegulationsPO StandardsRegulationsByName = sceneStandardsRegulationsMapper.queryStandardsRegulationsByName(standardsRegulationsPOSy);
                // standardsRegulationsPOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(StandardsRegulationsByName)) {
                    standardsRegulationsPOSy.setRegulationsId(StandardsRegulationsByName.getRegulationsId());
                    standardsRegulationsPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    standardsRegulationsPOSy.setShare(StandardsRegulationsByName.getShare());
                    sceneStandardsRegulationsMapper.updateStandardsRegulations(standardsRegulationsPOSy);
                } else {
                    standardsRegulationsPOSy.setRegulationsId(StringUtil.getRandomUUID());
                    standardsRegulationsPOSy.setCreateTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    standardsRegulationsPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setModifyUserId("");
                    sceneStandardsRegulationsMapper.saveStandardsRegulations(standardsRegulationsPOSy);
                }


                SystemScenePackageSublistPO bzPo = new SystemScenePackageSublistPO();
                bzPo.setSublistId(StringUtil.getRandomUUID());
                bzPo.setSceneType("2");
                bzPo.setIsDeleted("0");
                bzPo.setSceneName(id);
                bzPo.setSceneAndPackage(packId);
                bzPo.setCreateTime(TimeUtil.getNowForMysql());
                bzPo.setModifyTime(TimeUtil.getNowForMysql());
                bzPo.setCreateUserId(AuthUtil.getCurrentUserId());
                bzList.add(bzPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(bzList);
        }
        //zr
        List<String> jtIds = params.getJtSceneNames();
        if (ObjectUtil.isNotNull(jtIds)) {
            List<SystemScenePackageSublistPO> jtList = new ArrayList<>();
            for (String id : jtIds) {
                SceneAccidentPO sceneAccidentPO = new SceneAccidentPO();
                sceneAccidentPO.setSceneName(id);
                sceneAccidentPO.setShare("0");
                SceneAccidentPO sceneAccidentPOSy = sceneAccidentMapper.querySceneAccidentByNmae(sceneAccidentPO);
                sceneAccidentPOSy.setShare("2");
                SceneAccidentPO sceneAccidentPOByName = sceneAccidentMapper.querySceneAccidentByNmae(sceneAccidentPOSy);
                //sceneAccidentPOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(sceneAccidentPOByName)) {
                    sceneAccidentPOSy.setAccidentId(sceneAccidentPOByName.getAccidentId());
                    sceneAccidentPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    sceneAccidentPOSy.setShare(sceneAccidentPOByName.getShare());
                    sceneAccidentMapper.updateSceneAccident(sceneAccidentPOSy);
                } else {
                    sceneAccidentPOSy.setAccidentId(StringUtil.getRandomUUID());
                    sceneAccidentPOSy.setCreateTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    sceneAccidentPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setModifyUserId("");
                    sceneAccidentMapper.saveSceneAccident(sceneAccidentPOSy);
                }

                SystemScenePackageSublistPO jtPo = new SystemScenePackageSublistPO();
                jtPo.setSublistId(StringUtil.getRandomUUID());
                jtPo.setSceneType("3");
                jtPo.setIsDeleted("0");
                jtPo.setSceneName(id);
                jtPo.setSceneAndPackage(packId);
                jtPo.setCreateTime(TimeUtil.getNowForMysql());
                jtPo.setModifyTime(TimeUtil.getNowForMysql());
                jtPo.setCreateUserId(AuthUtil.getCurrentUserId());
                jtList.add(jtPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(jtList);
        }
        //zr
        List<String> fhIds = params.getFhSceneNames();
        if (ObjectUtil.isNotNull(fhIds)) {
            List<SystemScenePackageSublistPO> fhList = new ArrayList<>();
            for (String id : fhIds) {
                SceneGeneralTemplatePO sceneGeneralTemplatePO = new SceneGeneralTemplatePO();
                sceneGeneralTemplatePO.setSceneId(id);
                sceneGeneralTemplatePO.setShare("0");
                SceneGeneralTemplatePO sceneGeneralTemplatePOSy = sceneGeneralTemplateMapper.querySceneGeneralTemplateByName(sceneGeneralTemplatePO);
                sceneGeneralTemplatePOSy.setShare("2");
                SceneGeneralTemplatePO sceneGeneralTemplatePOByName = sceneGeneralTemplateMapper.querySceneGeneralTemplateByName(sceneGeneralTemplatePOSy);
                // sceneGeneralTemplatePOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(sceneGeneralTemplatePOByName)) {
                    sceneGeneralTemplatePOSy.setShare(sceneGeneralTemplatePOByName.getShare());
                    sceneGeneralTemplatePOSy.setId(sceneGeneralTemplatePOByName.getId());
                    sceneGeneralTemplatePOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    sceneGeneralTemplateMapper.updateSceneGeneralTemplate(sceneGeneralTemplatePOSy);
                } else {
                    sceneGeneralTemplatePOSy.setId(StringUtil.getRandomUUID());
                    sceneGeneralTemplatePOSy.setCreateTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    sceneGeneralTemplatePOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setModifyUserId("");
                    sceneGeneralTemplateMapper.saveSceneGeneralTemplate(sceneGeneralTemplatePOSy);
                }


                SystemScenePackageSublistPO fhPo = new SystemScenePackageSublistPO();
                fhPo.setSublistId(StringUtil.getRandomUUID());
                fhPo.setSceneType("4");
                fhPo.setIsDeleted("0");
                fhPo.setSceneName(id);
                fhPo.setSceneAndPackage(packId);
                fhPo.setCreateTime(TimeUtil.getNowForMysql());
                fhPo.setModifyTime(TimeUtil.getNowForMysql());
                fhPo.setCreateUserId(AuthUtil.getCurrentUserId());
                fhList.add(fhPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(fhList);
        }

        po.setModifyTime(TimeUtil.getNowForMysql());
        po.setModifyUserId(AuthUtil.getCurrentUserId());
        systemScenePackageMapper.updateSystemScenePackage(po);

        //记录场景包修改
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_UPDATE, po, null);

        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
    }

    //删除场景包
    @SneakyThrows
    public void deleteSystemScenePackage(SystemScenePackagePO params) {
        params.setIsDeleted("1");
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        systemScenePackageMapper.deleteSystemScenePackage(params);
        SystemUserScenePO po = new SystemUserScenePO();
        po.setPackageId(params.getId());
        po.setIsDeleted("1");
        po.setModifyTime(TimeUtil.getNowForMysql());
        po.setModifyUserId(AuthUtil.getCurrentUserId());
        systemUserSceneMapper.deleteSystemUserScene(po);
        //记录场景包删除
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_DELETE, params, null);


    }

    //根据场景包查询配置账号
    @SneakyThrows
    public SystemUserSceneVO queryUserByPackageId(SystemScenePackageNewPO params) {
        SystemUserSceneVO VO = new SystemUserSceneVO();
        List<UserSceneVO> userIds = systemUserSceneMapper.queryUserByPackageId(params.getId());
        VO.setPackageId(params.getId());
        VO.setUserIds(userIds);
        return VO;
    }

    //根据场景包添加配置账号
    @SneakyThrows
    public void saveUserByPackageId(SystemUserSceneParam params) {
        List<UserSceneVO> userIds = params.getUserIds();
        List<SystemUserScenePO> list = new ArrayList<>();
        for (UserSceneVO userScene : userIds) {
            SystemUserScenePO po = new SystemUserScenePO();
            po.setPackageId(params.getPackageId());
            po.setId(StringUtil.getRandomUUID());
            po.setCreateTime(TimeUtil.getNowForMysql());
            po.setModifyTime(TimeUtil.getNowForMysql());
            po.setCreateUserId(AuthUtil.getCurrentUserId());
            po.setIsDeleted("0");
            po.setUserId(userScene.getUserId());
            list.add(po);
        }
        systemUserSceneMapper.saveSystemUserScene(list);
        //记录日志
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_DISTRIBUTION, null, params);
    }

    //根据场景包删除配置账号
    @SneakyThrows
    public void deleteUserByPackageId(SystemUserSceneParam params) {
        List<UserSceneVO> userIds = params.getUserIds();
        for (UserSceneVO userScene : userIds) {
            SystemUserScenePO po = new SystemUserScenePO();
            po.setPackageId(params.getPackageId());
            po.setModifyTime(TimeUtil.getNowForMysql());
            po.setModifyUserId(AuthUtil.getCurrentUserId());
            po.setIsDeleted("1");
            po.setUserId(userScene.getUserId());
            systemUserSceneMapper.deleteSystemUserScene(po);
        }
        //记录日志
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_DISTRIBUTION_DELETE, null, params);
    }

    //人员表查询测试包详情
    @SneakyThrows
    public Map queryPackageByUserId(SystemScenePackageParam param) {
        Map map = systemScenePackageMapper.queryPackageByUserId(param);
        if (ObjectUtil.isNotNull(map.get("packageNames"))) {
            String ids = map.get("packageNames").toString();
            if (ObjectUtil.isNotNull(ids)) {
                String[] idss = ids.split(",");
                map.put("packageNames", idss);
            }
        }

        return map;
    }


    //根据配置账号添加场景包
    @SneakyThrows
    public void savePackageByUserId(SystemUserSceneParam params) {
        //先删除后添加
        deletePackageByUserId(params);

        List<String> packageNames = params.getPackageNames();
        List<SystemUserScenePO> list = new ArrayList<>();
        SystemScenePackageNewPO systemScenePackageNewPO = new SystemScenePackageNewPO();
        String packageCount = null;
        for (String packageName : packageNames) {
            //
            systemScenePackageNewPO.setPackageName(packageName);
            String packageId = systemScenePackageMapper.queryScenePackageByPackageName(systemScenePackageNewPO);
            SystemUserScenePO po = new SystemUserScenePO();
            po.setPackageId(packageId);
            po.setId(StringUtil.getRandomUUID());
            po.setCreateTime(TimeUtil.getNowForMysql());
            po.setModifyTime(TimeUtil.getNowForMysql());
            po.setCreateUserId(AuthUtil.getCurrentUserId());
            po.setIsDeleted("0");
            po.setUserId(params.getUserId());
            list.add(po);

            if (ObjectUtil.isNull(packageCount)) {
                packageCount = packageName + "(ID:" + packageId + ")";
            } else {
                packageCount = packageCount + "、" + packageName + "(ID:" + packageId + ")";
            }
        }
        systemUserSceneMapper.saveSystemUserScene(list);

        //记录场景包
        logService.logSystemUserSceneByPackageId(LogConstants.SYS_LOG_PACKAGE_GRANT_INSERT, params, packageCount);
    }

    //根据人员删除配置账号
    @SneakyThrows
    public void deletePackageByUserId(SystemUserSceneParam params) {

        SystemUserScenePO po = new SystemUserScenePO();
        po.setModifyTime(TimeUtil.getNowForMysql());
        po.setModifyUserId(AuthUtil.getCurrentUserId());
        po.setIsDeleted("1");
        po.setUserId(params.getUserId());
        systemUserSceneMapper.deleteSystemUserScene(po);
    }

    //配置人员场景包列表
    @SneakyThrows
    public List<SystemScenePackageVO> queryPackageByUserIdList(SystemScenePackageParam params) {
        return systemScenePackageMapper.queryPackageByUserIdList(params);
    }


    //查询勾选场景数量
    @SneakyThrows
    public ResponseBodyVO<List<Map<String, String>>> querySystemScenePackageListByTJ(SystemScenePackageParam params) {
        List<String> zrList = systemScenePackageMapper.querySceneNaturalListByTJ(params);
        List<String> bzList = systemScenePackageMapper.querySceneStandardsRegulationsListByTJ(params);
        List<String> jtList = systemScenePackageMapper.querySceneAccidentListByTJ(params);
        List<String> fhList = systemScenePackageMapper.querySceneGeneralTemplateListByTJ(params);
        Map<String, String> map = new HashMap<>();
        map.put("scene_type", "1");
        map.put("aaa", zrList.size() + "");
        Map<String, String> map1 = new HashMap<>();
        map1.put("scene_type", "2");
        map1.put("aaa", bzList.size() + "");
        Map<String, String> map2 = new HashMap<>();
        map2.put("scene_type", "3");
        map2.put("aaa", jtList.size() + "");
        Map<String, String> map3 = new HashMap<>();
        map3.put("scene_type", "4");
        map3.put("aaa", fhList.size() + "");
        ArrayList<Map<String, String>> arrayList = CollectionUtil.createArrayList(map1, map2, map3, map);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, arrayList);
    }


    /**
     * 场景包录入
     */
    @SneakyThrows
    public ResponseBodyVO<String> saveSystemScenePackageNew(SystemScenePackageNewPO params) {

        SystemScenePackagePO po = new SystemScenePackagePO();
        //场景包名称不允许重复
        String packageId = systemScenePackageMapper.queryScenePackageByPackageName(params);
        if (ObjectUtil.isNotNull(packageId)) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "该场景包名称已存在，请检查");
        }
        po.setPackageName(params.getPackageName());
        po.setSceneDescribe(params.getSceneDescribe());
        String packId = StringUtil.getRandomUUID();
        po.setId(packId);
        po.setCreateTime(TimeUtil.getNowForMysql());
        po.setModifyTime(TimeUtil.getNowForMysql());
        po.setCreateUserId(AuthUtil.getCurrentUserId());
        po.setIsDeleted("0");
        //zr
        List<String> zrSceneNames = params.getZrSceneNames();
        if (ObjectUtil.isNotNull(zrSceneNames)) {
            List<SystemScenePackageSublistPO> zrList = new ArrayList<>();
            for (String zrSceneName : zrSceneNames) {
                //加共有
                SceneNaturalPO paramPo = new SceneNaturalPO();
                paramPo.setShare("0");
                paramPo.setNaturalName(zrSceneName);
                SceneNaturalPO syPo = sceneNaturalMapper.querySceneNaturaByName(paramPo);
                syPo.setShare("2");
                SceneNaturalPO gyPo = sceneNaturalMapper.querySceneNaturaByName(syPo);
                if (ObjectUtil.isNull(gyPo)) {
                    // -------------------------------- 保存数据到 mysql --------------------------------
                    syPo.setCreateTime(TimeUtil.getNowForMysql());
                    syPo.setCreateUserId(AuthUtil.getCurrentUserId());
                    syPo.setModifyTime(TimeUtil.getNowForMysql());
                    syPo.setModifyUserId("");
                    syPo.setNaturalId(StringUtil.getRandomUUID());
                    sceneNaturalMapper.saveSceneNatural(syPo);
                    log.info("场景包新增场景1----" + syPo.getShare());
                } else {
                    // -------------------------------- 修改数据到 mysql --------------------------------
                    syPo.setModifyTime(TimeUtil.getNowForMysql());
                    syPo.setModifyUserId(AuthUtil.getCurrentUserId());
                    syPo.setShare(gyPo.getShare());
                    syPo.setNaturalId(gyPo.getNaturalId());
                    sceneNaturalMapper.updateSceneNatural(syPo);
                    log.info("场景包修改场景1----" + syPo.getShare());
                }


                SystemScenePackageSublistPO zrPo = new SystemScenePackageSublistPO();
                zrPo.setSublistId(StringUtil.getRandomUUID());
                zrPo.setSceneType("1");
                zrPo.setSceneName(syPo.getNaturalName());
                zrPo.setSceneAndPackage(packId);
                zrPo.setCreateTime(TimeUtil.getNowForMysql());
                zrPo.setModifyTime(TimeUtil.getNowForMysql());
                zrPo.setCreateUserId(AuthUtil.getCurrentUserId());
                zrPo.setIsDeleted("0");
                zrList.add(zrPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(zrList);
        }
//zr
        List<String> bzSceneNames = params.getBzSceneNames();
        if (ObjectUtil.isNotNull(bzSceneNames)) {
            List<SystemScenePackageSublistPO> bzList = new ArrayList<>();
            for (String bzSceneName : bzSceneNames) {
                StandardsRegulationsPO standardsRegulationsPO = new StandardsRegulationsPO();
                standardsRegulationsPO.setSceneName(bzSceneName);
                standardsRegulationsPO.setShare("0");
                StandardsRegulationsPO standardsRegulationsPOSy = sceneStandardsRegulationsMapper.queryStandardsRegulationsByName(standardsRegulationsPO);
                standardsRegulationsPOSy.setShare("2");
                StandardsRegulationsPO StandardsRegulationsByName = sceneStandardsRegulationsMapper.queryStandardsRegulationsByName(standardsRegulationsPOSy);
                //  standardsRegulationsPOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(StandardsRegulationsByName)) {
                    standardsRegulationsPOSy.setRegulationsId(StandardsRegulationsByName.getRegulationsId());
                    standardsRegulationsPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    standardsRegulationsPOSy.setShare(StandardsRegulationsByName.getShare());
                    sceneStandardsRegulationsMapper.updateStandardsRegulations(standardsRegulationsPOSy);
                } else {
                    standardsRegulationsPOSy.setRegulationsId(StringUtil.getRandomUUID());
                    standardsRegulationsPOSy.setCreateTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    standardsRegulationsPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    standardsRegulationsPOSy.setModifyUserId("");
                    sceneStandardsRegulationsMapper.saveStandardsRegulations(standardsRegulationsPOSy);
                }


                SystemScenePackageSublistPO bzPo = new SystemScenePackageSublistPO();
                bzPo.setSublistId(StringUtil.getRandomUUID());
                bzPo.setSceneType("2");
                bzPo.setSceneName(standardsRegulationsPOSy.getSceneName());
                bzPo.setSceneAndPackage(packId);
                bzPo.setCreateTime(TimeUtil.getNowForMysql());
                bzPo.setModifyTime(TimeUtil.getNowForMysql());
                bzPo.setIsDeleted("0");
                bzPo.setCreateUserId(AuthUtil.getCurrentUserId());
                bzList.add(bzPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(bzList);
        }
        //zr
        List<String> jtSceneNames = params.getJtSceneNames();
        if (ObjectUtil.isNotNull(jtSceneNames)) {
            List<SystemScenePackageSublistPO> jtList = new ArrayList<>();
            for (String jtSceneName : jtSceneNames) {
                SceneAccidentPO sceneAccidentPO = new SceneAccidentPO();
                sceneAccidentPO.setSceneName(jtSceneName);
                sceneAccidentPO.setShare("0");
                SceneAccidentPO sceneAccidentPOSy = sceneAccidentMapper.querySceneAccidentByNmae(sceneAccidentPO);
                sceneAccidentPOSy.setShare("2");
                SceneAccidentPO sceneAccidentPOByName = sceneAccidentMapper.querySceneAccidentByNmae(sceneAccidentPOSy);
                // sceneAccidentPOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(sceneAccidentPOByName)) {
                    sceneAccidentPOSy.setAccidentId(sceneAccidentPOByName.getAccidentId());
                    sceneAccidentPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    sceneAccidentPOSy.setShare(sceneAccidentPOByName.getShare());
                    sceneAccidentMapper.updateSceneAccident(sceneAccidentPOSy);
                } else {
                    sceneAccidentPOSy.setAccidentId(StringUtil.getRandomUUID());
                    sceneAccidentPOSy.setCreateTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    sceneAccidentPOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneAccidentPOSy.setModifyUserId("");
                    sceneAccidentMapper.saveSceneAccident(sceneAccidentPOSy);
                }


                SystemScenePackageSublistPO jtPo = new SystemScenePackageSublistPO();
                jtPo.setSublistId(StringUtil.getRandomUUID());
                jtPo.setSceneType("3");
                jtPo.setSceneName(sceneAccidentPOSy.getSceneName());
                jtPo.setSceneAndPackage(packId);
                jtPo.setCreateTime(TimeUtil.getNowForMysql());
                jtPo.setModifyTime(TimeUtil.getNowForMysql());
                jtPo.setCreateUserId(AuthUtil.getCurrentUserId());
                jtPo.setIsDeleted("0");
                jtList.add(jtPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(jtList);
        }
        //zr
        List<String> fhSceneNames = params.getFhSceneNames();
        if (ObjectUtil.isNotNull(fhSceneNames)) {
            List<SystemScenePackageSublistPO> fhList = new ArrayList<>();
            for (String fhSceneName : fhSceneNames) {
                SceneGeneralTemplatePO sceneGeneralTemplatePO = new SceneGeneralTemplatePO();
                sceneGeneralTemplatePO.setSceneId(fhSceneName);
                sceneGeneralTemplatePO.setShare("0");
                SceneGeneralTemplatePO sceneGeneralTemplatePOSy = sceneGeneralTemplateMapper.querySceneGeneralTemplateByName(sceneGeneralTemplatePO);
                sceneGeneralTemplatePOSy.setShare("2");
                SceneGeneralTemplatePO sceneGeneralTemplatePOByName = sceneGeneralTemplateMapper.querySceneGeneralTemplateByName(sceneGeneralTemplatePOSy);
                // sceneGeneralTemplatePOSy.setShare("1");
                //是否已存在
                if (ObjectUtil.isNotNull(sceneGeneralTemplatePOByName)) {
                    sceneGeneralTemplatePOSy.setShare(sceneGeneralTemplatePOByName.getShare());
                    sceneGeneralTemplatePOSy.setId(sceneGeneralTemplatePOByName.getId());
                    sceneGeneralTemplatePOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setModifyUserId(AuthUtil.getCurrentUserId());
                    sceneGeneralTemplateMapper.updateSceneGeneralTemplate(sceneGeneralTemplatePOSy);
                } else {
                    sceneGeneralTemplatePOSy.setId(StringUtil.getRandomUUID());
                    sceneGeneralTemplatePOSy.setCreateTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setCreateUserId(AuthUtil.getCurrentUserId());
                    sceneGeneralTemplatePOSy.setModifyTime(TimeUtil.getNowForMysql());
                    sceneGeneralTemplatePOSy.setModifyUserId("");
                    sceneGeneralTemplateMapper.saveSceneGeneralTemplate(sceneGeneralTemplatePOSy);
                }


                SystemScenePackageSublistPO fhPo = new SystemScenePackageSublistPO();
                fhPo.setSublistId(StringUtil.getRandomUUID());
                fhPo.setIsDeleted("0");
                fhPo.setSceneType("4");
                fhPo.setSceneName(sceneGeneralTemplatePOSy.getSceneId());
                fhPo.setSceneAndPackage(packId);
                fhPo.setCreateTime(TimeUtil.getNowForMysql());
                fhPo.setModifyTime(TimeUtil.getNowForMysql());
                fhPo.setCreateUserId(AuthUtil.getCurrentUserId());
                fhList.add(fhPo);
            }
            systemScenePackageSublistMapper.saveSystemScenePackageSublist(fhList);
        }
        systemScenePackageMapper.saveSystemScenePackage(po);

        //记录场景包新增
        logService.logScenePackage(LogConstants.SYS_LOG_PACKAGE_INSERT, po, null);

        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
    }
}
