package com.hzya.frame.sysnew.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.buttonConfig.dao.ISysButtonConfigDao;
import com.hzya.frame.sysnew.buttonConfig.entity.SysButtonConfigEntity;
import com.hzya.frame.sysnew.homeComponent.dao.ISysHomeComponentDao;
import com.hzya.frame.sysnew.homeComponent.entity.SysHomeComponentEntity;
import com.hzya.frame.sysnew.menuConfig.dao.ISysMenuConfigDao;
import com.hzya.frame.sysnew.menuConfig.entity.ModuleVo;
import com.hzya.frame.sysnew.menuConfig.entity.SysMenuConfigEntity;
import com.hzya.frame.sysnew.popedomHome.dao.ISysPopedomHomeDao;
import com.hzya.frame.sysnew.popedomHome.entity.SysPopedomHomeEntity;
import com.hzya.frame.sysnew.popedomInterface.dao.ISysPopedomInterfaceDao;
import com.hzya.frame.sysnew.popedomInterface.entity.SysPopedomInterfaceEntity;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.sysnew.popedomMenu.dao.ISysPopedomMenuDao;
import com.hzya.frame.sysnew.popedomMenu.entity.SysPopedomMenuEntity;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
import com.hzya.frame.sysnew.sysInterface.dao.ISysInterfaceDao;
import com.hzya.frame.sysnew.sysInterface.entity.SysInterfaceEntity;
import com.hzya.frame.sysnew.testCaching.service.ITestCachingService;
import com.hzya.frame.sysnew.user.entity.JurisdictionDto;
import com.hzya.frame.sysnew.user.entity.JurisdictionVo;
import com.hzya.frame.sysnew.user.entity.MenuVo;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.service.ISysUserService;
import com.hzya.frame.sysnew.userCompany.dao.ISysUserCompanyDao;
import com.hzya.frame.sysnew.userCompany.entity.SysUserCompanyEntity;
import com.hzya.frame.sysnew.userRoles.dao.ISysUserRolesDao;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import com.hzya.frame.util.AESUtil;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;


import com.hzya.frame.basedao.service.impl.BaseService;

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

/**
 * 用户表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2024-03-26 15:27:46
 */
@Service(value = "sysUserService")
public class SysUserServiceImpl extends BaseService<SysUserEntity, String> implements ISysUserService {

    private ISysUserDao sysUserDao;
    @Resource
    private ISysUserCompanyDao sysUserCompanyDao;
    @Resource
    private ITestCachingService testCachingService;
    @Resource
    private ISysUserRolesDao sysUserRolesDao;
    @Resource
    private ISysMenuConfigDao sysMenuConfigDao;
    @Resource
    private ISysButtonConfigDao sysButtonConfigDao;
    @Resource
    private ISysPopedomMenuDao sysPopedomMenuDao;
    @Resource
    private ISysPopedomOperateDao sysPopedomOperateDao;
    @Resource
    private ISysHomeComponentDao sysHomeComponentDao;
    @Resource
    private ISysPopedomHomeDao sysPopedomHomeDao;
    @Resource
    private ISysInterfaceDao sysInterfaceDao;
    @Resource
    private ISysPopedomInterfaceDao sysPopedomInterfaceDao;


    @Resource
    private InterfaceCache interfaceCache;
    @Autowired
    public void setSysUserDao(ISysUserDao dao) {
        this.sysUserDao = dao;
        this.dao = dao;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询用户列表分页
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntityPage(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<SysUserEntity> list = sysUserDao.queryByLikePerson(entity);
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询用户列表
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            entity = new SysUserEntity();
        }
        List<SysUserEntity> list = sysUserDao.queryByLike(entity);
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存用户
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getPersonId() == null || "".equals(entity.getPersonId())) {
            return BaseResult.getFailureMessageEntity("请先选择人员");
        }
        if (entity.getLoginCode() == null || "".equals(entity.getLoginCode())) {
            return BaseResult.getFailureMessageEntity("请先输入登录账号");
        }
        if (entity.getUserCompanys() == null || entity.getUserCompanys().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先选择用户公司");
        }
        if (entity.getUserRoles() == null || entity.getUserRoles().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先选择用户角色");
        }
        if (!checkPersonIsUse(entity)) {
            return BaseResult.getFailureMessageEntity("当前人员已经配置用户，请勿重复配置");
        }
        entity.setPassword(AESUtil.encrypt(entity.getLoginCode()+"-123456"));
        entity.setCreate();
        entity.setState("0");
        //保存
        sysUserDao.save(entity);
        //保存用户公司
        for (int i = 0; i < entity.getUserCompanys().size(); i++) {
            SysUserCompanyEntity sysUserCompanyEntity = entity.getUserCompanys().get(i);
            sysUserCompanyEntity.setUserId(entity.getId());
            sysUserCompanyEntity.setCreate();
            sysUserCompanyDao.save(sysUserCompanyEntity);
        }
        //保存用户角色
        for (int i = 0; i < entity.getUserRoles().size(); i++) {
            SysUserRolesEntity sysUserRolesEntity = entity.getUserRoles().get(i);
            sysUserRolesEntity.setUserId(entity.getId());
            sysUserRolesEntity.setCreate();
            sysUserRolesDao.save(sysUserRolesEntity);
        }
        interfaceCache.reloadData("1");
        interfaceCache.reloadData("3");
        return BaseResult.getSuccessMessageEntity("保存用户成功", entity);
    }

    /**
     * @param entity
     * @return boolean
     * @Author lvleigang
     * @Description 校验人员是否已经有配置
     * @Date 10:00 上午 2024/4/1
     **/
    private boolean checkPersonIsUse(SysUserEntity entity) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setPersonId(entity.getPersonId());
        //sysUserEntity.setState("0");//启用
        if (sysUserDao.getCount(sysUserEntity) > 0) {
            return false;
        }
        return true;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取用户
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysUserDao.get(entity.getId());
        //entity = testCachingService.getEntity(entity.getId());
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("获取用户失败");
        }
        //获取用户公司
        SysUserCompanyEntity sysUserCompanyEntity = new SysUserCompanyEntity();
        sysUserCompanyEntity.setUserId(entity.getId());
        List<SysUserCompanyEntity> sysUserCompanyEntities = sysUserCompanyDao.queryBase(sysUserCompanyEntity);
        entity.setUserCompanys(sysUserCompanyEntities);

        //获取用户角色
        SysUserRolesEntity sysUserRolesEntity = new SysUserRolesEntity();
        sysUserRolesEntity.setUserId(entity.getId());
        List<SysUserRolesEntity> sysUserRolesEntities = sysUserRolesDao.queryBase(sysUserRolesEntity);
        entity.setUserRoles(sysUserRolesEntities);
        return BaseResult.getSuccessMessageEntity("获取用户成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改用户
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getPersonId() == null || "".equals(entity.getPersonId())) {
            return BaseResult.getFailureMessageEntity("请先选择人员");
        }
        if (entity.getLoginCode() == null || "".equals(entity.getLoginCode())) {
            return BaseResult.getFailureMessageEntity("请先输入登录账号");
        }
        if (entity.getUserCompanys() == null || entity.getUserCompanys().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先选择用户公司");
        }
        if (entity.getUserRoles() == null || entity.getUserRoles().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先选择用户角色");
        }
        entity.setPassword(null);
        entity.setUpdate();
        //保存
        sysUserDao.update(entity);
        SysUserCompanyEntity deleteCompany = new SysUserCompanyEntity();
        deleteCompany.setUserId(entity.getId());
        deleteCompany.setUpdate();
        sysUserCompanyDao.logicRemoveMultiCondition(deleteCompany);
        SysUserRolesEntity deleteRole = new SysUserRolesEntity();
        deleteRole.setUserId(entity.getId());
        deleteRole.setUpdate();
        sysUserRolesDao.logicRemoveMultiCondition(deleteRole);
        //保存用户公司
        for (int i = 0; i < entity.getUserCompanys().size(); i++) {
            SysUserCompanyEntity sysUserCompanyEntity = entity.getUserCompanys().get(i);
            sysUserCompanyEntity.setUserId(entity.getId());
            sysUserCompanyEntity.setCreate();
            sysUserCompanyDao.save(sysUserCompanyEntity);
        }
        //保存用户角色
        for (int i = 0; i < entity.getUserRoles().size(); i++) {
            SysUserRolesEntity sysUserRolesEntity = entity.getUserRoles().get(i);
            sysUserRolesEntity.setUserId(entity.getId());
            sysUserRolesEntity.setCreate();
            sysUserRolesDao.save(sysUserRolesEntity);
        }
        interfaceCache.reloadData("1");
        interfaceCache.reloadData("3");
        return BaseResult.getSuccessMessageEntity("修改用户成功");
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除用户
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        sysUserDao.logicRemove(entity);
        //删除用户公司
        SysUserCompanyEntity deleteCompany = new SysUserCompanyEntity();
        deleteCompany.setUserId(entity.getId());
        deleteCompany.setUpdate();
        sysUserCompanyDao.logicRemoveMultiCondition(deleteCompany);
        //删除用户角色
        SysUserRolesEntity deleteRole = new SysUserRolesEntity();
        deleteRole.setUserId(entity.getId());
        deleteRole.setUpdate();
        sysUserRolesDao.logicRemoveMultiCondition(deleteRole);

        interfaceCache.reloadData("1");
        interfaceCache.reloadData("3");

        return BaseResult.getSuccessMessageEntity("删除用户成功");
    }


    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用用户
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getState() == null || "".equals(entity.getState())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        sysUserDao.update(entity);
        //0启用 1 停用
        interfaceCache.reloadData("1");
        if ("0".equals(entity.getState())) {
            return BaseResult.getSuccessMessageEntity("启用用户成功");
        } else {
            return BaseResult.getSuccessMessageEntity("停用用户成功");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 重置密码
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity resetPassword(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysUserDao.get(entity.getId());
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setPassword(AESUtil.encrypt(entity.getLoginCode()+"-123456"));
        entity.setUpdate();
        //保存密码
        sysUserDao.update(entity);
        interfaceCache.reloadData("1");
        return BaseResult.getSuccessMessageEntity("重置密码成功");
    }
    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 重置密码
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity userResetPassword(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getOldPassword() == null || "".equals(entity.getOldPassword())) {
            return BaseResult.getFailureMessageEntity("请先输入原密码");
        }
        if (entity.getPassword() == null || "".equals(entity.getPassword())) {
            return BaseResult.getFailureMessageEntity("请先输入新密码");
        }
        if(entity.getPassword().equals(entity.getOldPassword())){
            return BaseResult.getFailureMessageEntity("新密码和原密码不能一样");
        }
        SysUserEntity  oldEntity = sysUserDao.get(StpUtil.getLoginIdAsString());
        if (oldEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if(!oldEntity.getPassword().equals(AESUtil.encrypt(oldEntity.getLoginCode()+"-"+entity.getOldPassword()))){
            return BaseResult.getFailureMessageEntity("原密码错误，请重新输入");
        }
        oldEntity.setPassword(AESUtil.encrypt(oldEntity.getLoginCode()+"-"+entity.getPassword()));
        oldEntity.setUpdate();
        //保存密码
        sysUserDao.update(oldEntity);
        interfaceCache.reloadData("1");
        StpUtil.logoutByTokenValue(StpUtil.getTokenValue());
        return BaseResult.getSuccessMessageEntity("重置密码成功");
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 用户权限查询
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity jurisdiction(JSONObject jsonObject) {
        JurisdictionDto entity = getData("jsonStr", jsonObject, JurisdictionDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getKindId() == null || "".equals(entity.getKindId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //1、查询所有菜单和按钮，以及选中的菜单和按钮
        SysMenuConfigEntity sysMenuConfigEntity = new SysMenuConfigEntity();
        //sysMenuConfigEntity.setVisibles("0");
        List<SysMenuConfigEntity> allMenu = sysMenuConfigDao.queryBase(sysMenuConfigEntity);

        SysButtonConfigEntity sysButtonConfigEntity = new SysButtonConfigEntity();
        List<SysButtonConfigEntity> allButton = sysButtonConfigDao.queryBase(sysButtonConfigEntity);
        //使用的菜单
        SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
        sysPopedomMenuEntity.setObjectId(entity.getId());
        sysPopedomMenuEntity.setKindId(entity.getKindId());
        List<SysPopedomMenuEntity> useMenu = sysPopedomMenuDao.queryBase(sysPopedomMenuEntity);

        SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
        sysPopedomOperateEntity.setObjectId(entity.getId());
        sysPopedomOperateEntity.setKindId(entity.getKindId());
        List<SysPopedomOperateEntity> usebutton = sysPopedomOperateDao.queryBase(sysPopedomOperateEntity);
        JurisdictionVo jurisdictionVo = new JurisdictionVo();
        List<MenuVo> menvVoList = setReturnMenu(allMenu,allButton,useMenu,usebutton);
        jurisdictionVo.setMenvVoList(menvVoList);
        //2、数据权限暂时没有，所以不返回
        //全部接口
        SysInterfaceEntity sysInterfaceEntity = new SysInterfaceEntity();
        sysInterfaceEntity.setState("0");
        List<SysInterfaceEntity> allInterface = sysInterfaceDao.queryBase(sysInterfaceEntity);

        //使用的菜单
        SysPopedomInterfaceEntity sysPopedomInterfaceEntity = new SysPopedomInterfaceEntity();
        sysPopedomInterfaceEntity.setObjectId(entity.getId());
        sysPopedomInterfaceEntity.setKindId(entity.getKindId());
        List<SysPopedomInterfaceEntity> useInterface = sysPopedomInterfaceDao.queryBase(sysPopedomInterfaceEntity);
        if(useInterface != null && useInterface.size() > 0){
            for (int i = 0; i < useInterface.size(); i++) {
                for (int a = 0; a < allInterface.size(); a++) {
                    if(useInterface.get(i).getInterfaceId().equals(allInterface.get(a).getId())){
                        allInterface.get(a).setCheck(true);
                    }
                }
            }
        }
        jurisdictionVo.setInterfaceList(allInterface);
        //3、首页拖拽返回全部，并且设置是否选中
        SysHomeComponentEntity sysHomeComponentEntity = new SysHomeComponentEntity();
        sysHomeComponentEntity.setState("0");
        List<SysHomeComponentEntity> sysHomeComponentEntities = sysHomeComponentDao.queryBase(sysHomeComponentEntity);
        SysPopedomHomeEntity sysPopedomHomeEntity = new SysPopedomHomeEntity();
        sysPopedomHomeEntity.setObjectId(entity.getId());
        sysPopedomHomeEntity.setKindId(entity.getKindId());
        List<SysPopedomHomeEntity> useHome = sysPopedomHomeDao.queryBase(sysPopedomHomeEntity);
        for (int i = 0; i < sysHomeComponentEntities.size(); i++) {
            sysHomeComponentEntities.get(i).setCheck(false);
        }
        if(useHome != null && useHome.size() > 0){
            for (int i = 0; i < useHome.size(); i++) {
                for (int a = 0; a < sysHomeComponentEntities.size(); a++) {
                    if(useHome.get(i).getHomeId().equals(sysHomeComponentEntities.get(a).getId())){
                        sysHomeComponentEntities.get(a).setCheck(true);
                    }
                }
            }
        }
        jurisdictionVo.setHomeList(sysHomeComponentEntities);
        return BaseResult.getSuccessMessageEntity("查询权限成功",jurisdictionVo);
    }

    /**
     * @Author lvleigang
     * @Description  保存权限查询
     * @Date 1:46 下午 2024/4/2
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity saveJurisdiction(JSONObject jsonObject) {
        JurisdictionDto entity = getData("jsonStr", jsonObject, JurisdictionDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getKindId() == null || "".equals(entity.getKindId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //保存菜单
        SysPopedomMenuEntity deleteMenu = new SysPopedomMenuEntity();
        deleteMenu.setObjectId(entity.getId());
        deleteMenu.setKindId(entity.getKindId());
        deleteMenu.setUpdate();
        sysPopedomMenuDao.logicRemoveMultiCondition(deleteMenu);

        SysPopedomOperateEntity deleteOperate = new SysPopedomOperateEntity();
        deleteOperate.setObjectId(entity.getId());
        deleteOperate.setKindId(entity.getKindId());
        deleteOperate.setUpdate();
        sysPopedomOperateDao.logicRemoveMultiCondition(deleteOperate);


        SysPopedomInterfaceEntity deleteInterface = new SysPopedomInterfaceEntity();
        deleteInterface.setObjectId(entity.getId());
        deleteInterface.setKindId(entity.getKindId());
        deleteInterface.setUpdate();
        sysPopedomInterfaceDao.logicRemoveMultiCondition(deleteInterface);
        if(entity.getSysPopedomMenuEntities() != null && entity.getSysPopedomMenuEntities().size() > 0){
            for (int i = 0; i < entity.getSysPopedomMenuEntities().size(); i++) {
                SysPopedomMenuEntity sysPopedomMenuEntity = entity.getSysPopedomMenuEntities().get(i);
                sysPopedomMenuEntity.setObjectId(entity.getId());
                sysPopedomMenuEntity.setKindId(entity.getKindId());
                sysPopedomMenuEntity.setCreate();
                sysPopedomMenuDao.save(sysPopedomMenuEntity);
            }
        }
        //保存按钮
        if(entity.getSysPopedomOperateEntities() != null && entity.getSysPopedomOperateEntities().size() > 0){
            for (int i = 0; i < entity.getSysPopedomOperateEntities().size(); i++) {
                SysPopedomOperateEntity sysPopedomOperateEntity = entity.getSysPopedomOperateEntities().get(i);
                sysPopedomOperateEntity.setObjectId(entity.getId());
                sysPopedomOperateEntity.setKindId(entity.getKindId());
                sysPopedomOperateEntity.setCreate();
                sysPopedomOperateDao.save(sysPopedomOperateEntity);
            }
        }

        //保存首页
        SysPopedomHomeEntity deleteHome = new SysPopedomHomeEntity();
        deleteHome.setObjectId(entity.getId());
        deleteHome.setKindId(entity.getKindId());
        deleteHome.setUpdate();
        sysPopedomHomeDao.logicRemoveMultiCondition(deleteHome);
        if(entity.getSysPopedomHomeEntities() != null && entity.getSysPopedomHomeEntities().size() > 0){
            for (int i = 0; i < entity.getSysPopedomHomeEntities().size(); i++) {
                SysPopedomHomeEntity sysPopedomHomeEntity = entity.getSysPopedomHomeEntities().get(i);
                sysPopedomHomeEntity.setObjectId(entity.getId());
                sysPopedomHomeEntity.setKindId(entity.getKindId());
                sysPopedomHomeEntity.setCreate();
                sysPopedomHomeDao.save(sysPopedomHomeEntity);
            }
        }

        if(entity.getSysPopedomInterfaceEntities() != null && entity.getSysPopedomInterfaceEntities().size() > 0){
            for (int i = 0; i < entity.getSysPopedomInterfaceEntities().size(); i++) {
                SysPopedomInterfaceEntity sysPopedomInterfaceEntity = entity.getSysPopedomInterfaceEntities().get(i);
                sysPopedomInterfaceEntity.setObjectId(entity.getId());
                sysPopedomInterfaceEntity.setKindId(entity.getKindId());
                sysPopedomInterfaceEntity.setCreate();
                sysPopedomInterfaceDao.save(sysPopedomInterfaceEntity);
            }
        }
        interfaceCache.reloadData("4");
        interfaceCache.reloadData("5");
        interfaceCache.reloadData("6");

        return BaseResult.getSuccessMessageEntity("保存权限成功");
    }

    /**
     * @Author lvleigang
     * @Description  设置返回的菜单和按钮
     * @Date 11:09 上午 2024/4/2
     * @param allMenu
     * @param allButton
     * @param useMenu
     * @param usebutton
     * @return java.util.List<com.hzya.frame.sysnew.user.entity.MenuVo>
     **/
    private List<MenuVo> setReturnMenu(List<SysMenuConfigEntity> allMenu, List<SysButtonConfigEntity> allButton, List<SysPopedomMenuEntity> useMenu, List<SysPopedomOperateEntity> usebutton) {
        List<MenuVo> menuVos = new ArrayList<>();
        //设置菜单和按钮是否选中
        for (int i = 0; i < allMenu.size(); i++) {
            allMenu.get(i).setCheck(false);
        }
        if(useMenu != null && useMenu.size() > 0){
            for (int i = 0; i < allMenu.size(); i++) {
                for (int a = 0; a < useMenu.size(); a++) {
                    if(allMenu.get(i).getId().equals(useMenu.get(a).getMenuId())){
                        allMenu.get(i).setCheck(true);
                    }
                }
            }
        }
        for (int i = 0; i < allButton.size(); i++) {
            allButton.get(i).setCheck(false);
        }
        if(usebutton != null && usebutton.size() > 0){
            for (int i = 0; i < allButton.size(); i++) {
                for (int a = 0; a < usebutton.size(); a++) {
                    if(allButton.get(i).getId().equals(usebutton.get(a).getOperate())){
                        allButton.get(i).setCheck(true);
                    }
                }
            }
        }


        //组装菜单树
        if (allMenu != null && allMenu.size() > 0) {
            for (int i = 0; i < allMenu.size(); i++) {
                if ("0".equals(allMenu.get(i).getParentMenuId())) {
                    menuVos.add(recursiveSetUseTree(allMenu.get(i), allMenu,allButton));
                }
            }
        }

        return menuVos;
    }

    /**
     * @param sysMenuConfigEntity
     * @param sysMenuConfigEntities
     * @return com.hzya.frame.sysnew.menuConfig.entity.ModuleVo
     * @Author lvleigang
     * @Description
     * @Date 9:21 上午 2024/4/2
     **/
    private MenuVo recursiveSetUseTree(SysMenuConfigEntity sysMenuConfigEntity, List<SysMenuConfigEntity> sysMenuConfigEntities,List<SysButtonConfigEntity> allButton) {
        MenuVo moduleVo = new MenuVo();
        moduleVo.setId(sysMenuConfigEntity.getId());
        moduleVo.setName(sysMenuConfigEntity.getMenuName());
        moduleVo.setCheck(sysMenuConfigEntity.isCheck());
        JSONObject meta = new JSONObject();
        meta.put("title", sysMenuConfigEntity.getMenuName());
        moduleVo.setMeta(meta);
        List<MenuVo> children = new ArrayList<>();
        for (int i = 0; i < sysMenuConfigEntities.size(); i++) {
            if (sysMenuConfigEntity.getId().equals(sysMenuConfigEntities.get(i).getParentMenuId())) {
                children.add(recursiveSetUseTree(sysMenuConfigEntities.get(i),sysMenuConfigEntities,allButton));
            }
        }
        List<SysButtonConfigEntity> sysButtonConfigEntities = new ArrayList<>();
        for (int i = 0; i < allButton.size(); i++) {
            if(allButton.get(i).getMenuId().equals(sysMenuConfigEntity.getId())){
                sysButtonConfigEntities.add(allButton.get(i));
            }
        }
        moduleVo.setButtonList(sysButtonConfigEntities);
        moduleVo.setChildren(children);
        return moduleVo;
    }
}
