package com.yunzhupaas.permission.service.impl;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.yunzhupaas.base.entity.ModuleEntity;
import com.yunzhupaas.base.entity.SystemEntity;
import com.yunzhupaas.base.service.DbLinkService;
import com.yunzhupaas.base.service.ModuleService;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.Pagination;
import com.yunzhupaas.base.service.SystemService;
import com.yunzhupaas.config.ConfigValueUtil;
import com.yunzhupaas.constant.YunzhupaasConst;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.database.model.dto.PrepSqlDTO;
import com.yunzhupaas.database.sql.util.SqlFrameFastUtil;
import com.yunzhupaas.database.util.DataSourceUtil;
import com.yunzhupaas.constant.PermissionConst;
import com.yunzhupaas.database.util.DbTypeUtil;
import com.yunzhupaas.database.util.JdbcUtil;
import com.yunzhupaas.database.util.TenantDataSourceUtil;
import com.yunzhupaas.model.login.UserSystemVO;
import com.yunzhupaas.model.tenant.TenantAuthorizeModel;
import com.yunzhupaas.permission.entity.OrganizeAdministratorEntity;
import com.yunzhupaas.permission.entity.OrganizeEntity;
import com.yunzhupaas.permission.entity.UserEntity;
import com.yunzhupaas.permission.entity.UserRelationEntity;
import com.yunzhupaas.permission.mapper.OrganizeAdminIsTratorMapper;
import com.yunzhupaas.permission.model.organizeadministrator.OrganizeAdministratorListVo;
import com.yunzhupaas.permission.model.organizeadministrator.OrganizeAdministratorModel;
import com.yunzhupaas.permission.service.*;
import com.yunzhupaas.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 机构分级管理员
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2024-09-26 上午9:18
 */
@Service
public class OrganizeAdministratorServiceImpl
        extends SuperServiceImpl<OrganizeAdminIsTratorMapper, OrganizeAdministratorEntity>
        implements OrganizeAdministratorService {

    @Autowired
    private OrganizeService organizeService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRelationService userRelationService;
    @Autowired
    private DbLinkService dbLinkService;
    @Autowired
    private SystemService systemApi;
    @Autowired
    private ModuleService moduleApi;
    @Autowired
    private ConfigValueUtil configValueUtil;
    @Autowired
    private AuthorizeService authorizeService;
    @Autowired
    private DataSourceUtil dataSourceUtils;

    @Override
    public OrganizeAdministratorEntity getOne(String userId, String organizeId) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getOrganizeId, organizeId);
        // 排序
        queryWrapper.lambda().orderByAsc(OrganizeAdministratorEntity::getSortCode)
                .orderByDesc(OrganizeAdministratorEntity::getCreatorTime);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<OrganizeAdministratorEntity> getOrganizeAdministratorEntity(String userId) {
        return getOrganizeAdministratorEntity(userId, PermissionConst.ORGANIZE, false);
    }

    @Override
    public List<OrganizeAdministratorEntity> getOrganizeAdministratorEntity(String userId, String type,
            boolean filterMain) {
        List<OrganizeAdministratorEntity> list = new ArrayList<>();
        UserEntity entity1 = userService.getInfo(userId);
        List<UserSystemVO> userStanding = authorizeService.getUserStanding(false, entity1.getId());
        boolean isAdmin = userStanding.stream().filter(t -> "1".equals(t.getId())).count() > 0;
        // 查询非组织权限
        if ((entity1 != null && isAdmin) && !PermissionConst.ORGANIZE.equals(type)) {
            List<String> collect = new ArrayList<>();
            List<String> moduleAuthorize = new ArrayList<>();
            List<String> moduleUrlAddressAuthorize = new ArrayList<>();
            if (configValueUtil.isMultiTenancy()) {
                TenantAuthorizeModel tenantAuthorizeModel = TenantDataSourceUtil
                        .getCacheModuleAuthorize(UserProvider.getUser().getTenantId());
                moduleAuthorize = tenantAuthorizeModel.getModuleIdList();
                moduleUrlAddressAuthorize = tenantAuthorizeModel.getUrlAddressList();
            }
            if (PermissionConst.SYSTEM.equals(type)) {
                collect = systemApi.getList(null, false, false, filterMain, false, moduleAuthorize).stream()
                        .map(SystemEntity::getId).collect(Collectors.toList());
            } else if (PermissionConst.MODULE.equals(type)) {
                collect = moduleApi.getList(false, moduleAuthorize, moduleUrlAddressAuthorize).stream()
                        .map(ModuleEntity::getId).collect(Collectors.toList());
            }
            for (String t : collect) {
                OrganizeAdministratorEntity entity = new OrganizeAdministratorEntity();
                entity.setOrganizeId(t);
                entity.setId(RandomUtil.uuId());
                entity.setOrganizeType(type);
                entity.setUserId(userId);
                list.add(entity);
            }
            return list;
        }
        // 组织权限
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        if (PermissionConst.ORGANIZE.equals(type)) {
            queryWrapper.lambda().isNull(OrganizeAdministratorEntity::getOrganizeType);
        } else if (StringUtil.isNotEmpty(type)) {
            queryWrapper.lambda().eq(OrganizeAdministratorEntity::getOrganizeType, type);
        }
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        // 排序
        queryWrapper.lambda().orderByAsc(OrganizeAdministratorEntity::getSortCode)
                .orderByDesc(OrganizeAdministratorEntity::getCreatorTime);
        list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<OrganizeAdministratorEntity> getOrganizeAdministratorEntity(String userId, String type) {
        // 组织权限
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        if (PermissionConst.ORGANIZE.equals(type)) {
            queryWrapper.lambda().isNull(OrganizeAdministratorEntity::getOrganizeType);
        } else if (StringUtil.isNotEmpty(type)) {
            queryWrapper.lambda().eq(OrganizeAdministratorEntity::getOrganizeType, type);
        }
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        // 排序
        queryWrapper.lambda().orderByAsc(OrganizeAdministratorEntity::getSortCode)
                .orderByDesc(OrganizeAdministratorEntity::getCreatorTime);
        List<OrganizeAdministratorEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    @Transactional
    public void create(OrganizeAdministratorEntity entity) {
        // 判断是新建还是删除
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getOrganizeId, entity.getOrganizeId());
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, entity.getUserId());
        // 查出数据是否重复
        OrganizeAdministratorEntity administratorEntity = this.getOne(queryWrapper);
        if (administratorEntity == null) {
            entity.setId(RandomUtil.uuId());
            entity.setCreatorUserId(UserProvider.getLoginUserId());
            entity.setCreatorTime(new Date());
        } else {
            entity.setId(administratorEntity.getId());
            entity.setCreatorUserId(UserProvider.getLoginUserId());
            entity.setLastModifyTime(new Date());
        }
        this.saveOrUpdate(entity);
    }

    @Override
    public void createList(List<OrganizeAdministratorEntity> list, String userId) {
        // QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new
        // QueryWrapper<>();
        // queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId,userId);
        // queryWrapper.lambda().select(OrganizeAdministratorEntity::getCreatorTime);
        // IPage<OrganizeAdministratorEntity> list1 = this.page(new Page<>(1, 1),
        // queryWrapper);
        // boolean isData = !list1.getRecords().isEmpty() &&
        // ObjectUtil.isNotEmpty(list1.getRecords().get(0).getCreatorTime());
        // Date creatorTime = isData?list1.getRecords().get(0).getCreatorTime():new
        // Date();
        // this.remove(queryWrapper);
        // for (OrganizeAdministratorEntity entity : list) {
        // entity.setCreatorTime(creatorTime);
        // this.save(entity);
        // }
        // userService.delCurUser(MsgCode.PS010.get(), ImmutableList.of(userId),2);
        try {
            QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
            Page<OrganizeAdministratorEntity> page = new Page<>(1, 1);
            IPage<OrganizeAdministratorEntity> list1 = this.page(page, queryWrapper);
            boolean isData = !list1.getRecords().isEmpty()
                    && ObjectUtil.isNotEmpty(list1.getRecords().get(0).getCreatorTime());
            String now = isData ? DateUtil.dateFormat(list1.getRecords().get(0).getCreatorTime()) : DateUtil.getNow();
            JdbcUtil.creUpDe(new PrepSqlDTO(
                    XSSEscape.escapeEmpty(SqlFrameFastUtil.ADMINISTRATOR_DEL.replace("{userId}", userId)))
                    .withConn(dataSourceUtils, null));
            String sql = DbTypeUtil.checkOracle(dataSourceUtils) || DbTypeUtil.checkPostgre(dataSourceUtils)
                    ? SqlFrameFastUtil.INSERT_ADMINISTRATOR2
                    : SqlFrameFastUtil.INSERT_ADMINISTRATOR;
            String column_key = StringUtil.EMPTY, column_plceholder = StringUtil.EMPTY,
                    column_value = TenantDataSourceUtil.getTenantColumn();
            if (StringUtil.isNotEmpty(column_value)) {
                column_key = StrPool.COMMA + configValueUtil.getMultiTenantColumn();
                column_plceholder = ",?";
            }
            sql = sql.replace("%COLUMN_KEY%", column_key).replace("%COLUMN_PLACEHOLDER%", column_plceholder);
            PrepSqlDTO dto = new PrepSqlDTO(sql).withConn(dataSourceUtils, null);

            for (OrganizeAdministratorEntity entity : list) {
                List<Object> data = new LinkedList<>();
                data.add(RandomUtil.uuId());
                data.add(userId);
                data.add(entity.getOrganizeId());
                data.add(entity.getOrganizeType());
                data.add(entity.getThisLayerAdd());
                data.add(entity.getThisLayerEdit());
                data.add(entity.getThisLayerDelete());
                data.add(entity.getSubLayerAdd());
                data.add(entity.getSubLayerEdit());
                data.add(entity.getSubLayerDelete());
                data.add(entity.getThisLayerSelect());
                data.add(entity.getSubLayerSelect());
                data.add(entity.getManagerGroup());
                data.add(now);
                data.add(UserProvider.getLoginUserId());
                data.add(1);
                if (StringUtil.isNotEmpty(column_value)) {
                    data.add(column_value);
                }
                dto.addMultiData(data);
            }
            JdbcUtil.creUpDeBatchOneSql(dto);
            userService.delCurUser(MsgCode.PS010.get(), ImmutableList.of(userId), 2);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("分级报错:" + e.getMessage());
        }
    }

    @Override
    public boolean update(String organizeId, OrganizeAdministratorEntity entity) {
        entity.setId(entity.getId());
        entity.setLastModifyTime(DateUtil.getNowDate());
        entity.setLastModifyUserId(UserProvider.getLoginUserId());
        return this.updateById(entity);
    }

    @Override
    public boolean deleteByUserId(String userId) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        boolean remove = this.remove(queryWrapper);
        userService.delCurUser(MsgCode.PS010.get(), ImmutableList.of(userId), 2);
        userService.updateStand(ImmutableList.of(userId), 2);
        return remove;
    }

    @Override
    public List<OrganizeAdministratorEntity> getInfoByUserId(String userId) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getEnabledMark, 1);
        return this.list(queryWrapper);
    }

    @Override
    public OrganizeAdministratorEntity getInfo(String id) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void delete(OrganizeAdministratorEntity entity) {
        this.removeById(entity.getId());
    }

    @Override
    public String getManagerGroupByUserId(String userId) {
        if (StringUtil.isEmpty(userId)) {
            return null;
        }
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getUserId, userId);
        List<OrganizeAdministratorEntity> list = this.list(queryWrapper);
        return list.size() > 0 ? list.get(0).getManagerGroup() : null;
    }

    @Override
    public OrganizeAdministratorEntity getInfoByOrganizeId(String organizeId) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrganizeAdministratorEntity::getOrganizeId, organizeId);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<OrganizeAdministratorEntity> getListByOrganizeId(List<String> organizeIdList) {
        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(OrganizeAdministratorEntity::getOrganizeId, organizeIdList);
        return this.list(queryWrapper);
    }

    @Override
    public List<OrganizeAdministratorListVo> getList(Pagination pagination) {
        List<OrganizeAdministratorEntity> list = getOrganizeAdministratorEntity(UserProvider.getLoginUserId(),
                PermissionConst.ORGANIZE, false);
        Map<String, String> orgIdNameMaps = organizeService.getInfoList();
        List<String> organizeIdList;
        Map<String, OrganizeEntity> orgMaps = organizeService.getOrgMaps(null, true, null);
        // 存放所有的有资格管理的组织id
        if (UserProvider.getUser().getIsAdministrator()) {
            organizeIdList = new ArrayList<>(orgMaps.keySet());
        } else {
            Set<String> orgId = new HashSet<>(16);
            // 判断自己是哪些组织的管理员
            list.stream().forEach(t -> {
                if (t != null) {
                    // t.getThisLayerAdd() == 1 || t.getThisLayerEdit() == 1 ||
                    // t.getThisLayerDelete() == 1 ||
                    // (StringUtil.isNotEmpty(String.valueOf(t.getSubLayerSelect())) &&
                    // t.getThisLayerSelect() == 1)
                    if (t.getSubLayerSelect() != null && t.getThisLayerSelect() == 1) {
                        orgId.add(t.getOrganizeId());
                    }
                    // t.getSubLayerAdd() == 1 || t.getSubLayerEdit() == 1 || t.getSubLayerDelete()
                    // == 1 || (StringUtil.isNotEmpty(String.valueOf(t.getSubLayerSelect())) &&
                    // t.getSubLayerSelect() == 1)
                    if (t.getSubLayerSelect() != null && t.getSubLayerSelect() == 1) {
                        List<String> underOrganizations = organizeService.getUnderOrganizations(t.getOrganizeId(),
                                true);
                        orgId.addAll(underOrganizations);
                    }
                }
            });
            organizeIdList = new ArrayList<>(orgId);
        }
        if (organizeIdList.size() < 1) {
            organizeIdList.add("");
        }
        List<OrganizeAdministratorListVo> jsonToList = new ArrayList<>();
        List<List<String>> organizeList = Lists.partition(organizeIdList, 1000);
        List<String> userId = new ArrayList<>();
        for (List<String> objectId : organizeList) {
            userId.addAll(userRelationService.getListByOrgId(objectId).stream().map(UserRelationEntity::getUserId)
                    .collect(Collectors.toList()));
        }
        userId.remove(UserProvider.getUser().getUserId());
        if (userId.isEmpty()) {
            return jsonToList;
        }

        QueryWrapper<UserEntity> userWrapper = new QueryWrapper<>();
        userWrapper.lambda().select(UserEntity::getId, UserEntity::getAccount, UserEntity::getRealName,
                UserEntity::getGender, UserEntity::getMobilePhone);
        userWrapper.lambda().in(UserEntity::getId, userId);
        userWrapper.lambda().eq(UserEntity::getEnabledMark, 1);
        if (StringUtil.isNotEmpty(pagination.getKeyword())) {
            userWrapper.lambda().and(
                    t -> t.like(UserEntity::getRealName, pagination.getKeyword())
                            .or().like(UserEntity::getAccount, pagination.getKeyword())
                            .or().like(UserEntity::getMobilePhone, pagination.getKeyword()));
        }
        List<UserEntity> userList = userService.list(userWrapper);
        if (userList.isEmpty()) {
            return jsonToList;
        }

        QueryWrapper<OrganizeAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNotNull(OrganizeAdministratorEntity::getManagerGroup);
        queryWrapper.lambda().select(OrganizeAdministratorEntity::getUserId,
                OrganizeAdministratorEntity::getCreatorTime, OrganizeAdministratorEntity::getManagerGroup);
        queryWrapper.lambda().in(OrganizeAdministratorEntity::getUserId,
                userList.stream().map(UserEntity::getId).collect(Collectors.toList()));
        queryWrapper.lambda().orderByDesc(OrganizeAdministratorEntity::getCreatorTime);
        List<OrganizeAdministratorEntity> entityList = this.list(queryWrapper);

        List<String> ids = new ArrayList<>();
        for (OrganizeAdministratorEntity entity : entityList) {
            if (!ids.contains(entity.getUserId())) {
                ids.add(entity.getUserId());
            }
        }

        List listPage = PageUtil.getListPage((int) pagination.getCurrentPage(), (int) pagination.getPageSize(), ids);
        if (!listPage.isEmpty()) {
            List<OrganizeAdministratorEntity> organizeAdminList = entityList.stream()
                    .filter(t -> listPage.contains(t.getUserId())).distinct()
                    .sorted(Comparator.comparing(OrganizeAdministratorEntity::getCreatorTime).reversed())
                    .collect(Collectors.toList());
            for (OrganizeAdministratorEntity entity : organizeAdminList) {
                UserEntity info = userList.stream().filter(t -> t.getId().equals(entity.getUserId())).findFirst()
                        .orElse(null);
                if (info != null) {
                    OrganizeAdministratorListVo vo = JsonUtil.getJsonToBean(info, OrganizeAdministratorListVo.class);
                    // 所属组织
                    List<UserRelationEntity> orgRelationByUserId = userRelationService
                            .getAllOrgRelationByUserId(entity.getUserId());
                    StringBuilder orgName = new StringBuilder();
                    orgRelationByUserId.stream().forEach(or -> {
                        OrganizeEntity organizeEntity = orgMaps.get(or.getObjectId());
                        if (organizeEntity != null && StringUtil.isNotEmpty(organizeEntity.getOrganizeIdTree())) {
                            String fullNameByOrgIdTree = organizeService.getFullNameByOrgIdTree(orgIdNameMaps,
                                    organizeEntity.getOrganizeIdTree(), "/");
                            orgName.append("," + fullNameByOrgIdTree);
                        }
                    });
                    // 组织名称
                    String org = orgName.length() > 0 ? orgName.toString().replaceFirst(",", "") : "";
                    vo.setOrganizeId(org);
                    vo.setManagerGroup(entity.getManagerGroup());
                    vo.setCreatorTime(entity.getCreatorTime().getTime());
                    jsonToList.add(vo);
                }
            }
        }
        return pagination.setData(jsonToList, ids.size());
    }

    @Override
    public List<String> getOrganizeUserList(String type) {
        if (UserProvider.getUser().getIsAdministrator()) {
            return organizeService.getList(true).stream().map(OrganizeEntity::getId).collect(Collectors.toList());
        }
        List<OrganizeAdministratorEntity> list = getOrganizeAdministratorEntity(UserProvider.getLoginUserId());
        Set<String> orgId = new HashSet<>(16);
        // 判断自己是哪些组织的管理员
        list.stream().forEach(t -> {
            if (t != null) {
                if (t.getSubLayerSelect() != null && t.getThisLayerSelect() == 1) {
                    orgId.add(t.getOrganizeId());
                }
                if (t.getSubLayerSelect() != null && t.getSubLayerSelect() == 1) {
                    List<String> underOrganizations = organizeService.getUnderOrganizations(t.getOrganizeId(), true);
                    orgId.addAll(underOrganizations);
                }
            }
        });
        List<String> orgIds = new ArrayList<>(orgId);
        if (YunzhupaasConst.CURRENT_ORG_SUB.equals(type)) {
            return orgIds;
        }
        List<String> userList = userRelationService.getListByObjectIdAll(orgIds).stream()
                .map(UserRelationEntity::getUserId).collect(Collectors.toList());
        return userList;
    }

    @Override
    public List<OrganizeEntity> getListByAuthorize() {
        // 通过权限转树
        List<OrganizeAdministratorEntity> listss = getOrganizeAdministratorEntity(UserProvider.getLoginUserId());
        Set<String> orgIds = new HashSet<>(16);
        // 判断自己是哪些组织的管理员
        listss.stream().forEach(t -> {
            if (t != null) {
                if (t.getThisLayerSelect() != null && t.getThisLayerSelect() == 1) {
                    orgIds.add(t.getOrganizeId());
                }
                if (t.getSubLayerSelect() != null && t.getSubLayerSelect() == 1) {
                    List<String> underOrganizations = organizeService.getUnderOrganizations(t.getOrganizeId(), true);
                    orgIds.addAll(underOrganizations);
                }
            }
        });
        List<String> list1 = new ArrayList<>(orgIds);
        // 得到所有有权限的组织
        List<OrganizeEntity> organizeName = organizeService.getOrganizeName(list1);
        return organizeName;
    }

    @Override
    public OrganizeAdministratorModel getOrganizeAdministratorList() {
        // 通过权限转树
        List<OrganizeAdministratorEntity> list = getOrganizeAdministratorEntity(UserProvider.getUser().getUserId());
        List<String> addList = new ArrayList<>();
        List<String> editList = new ArrayList<>();
        List<String> deleteList = new ArrayList<>();
        List<String> selectList = new ArrayList<>();
        // 判断自己是哪些组织的管理员
        list.forEach(t -> {
            if (t != null) {
                // 查询
                if (t.getThisLayerSelect() != null && t.getThisLayerSelect() == 1) {
                    selectList.add(t.getOrganizeId());
                    // 修改
                    if (t.getThisLayerEdit() != null && t.getThisLayerEdit() == 1) {
                        editList.add(t.getOrganizeId());
                    }
                    // 删除
                    if (t.getThisLayerDelete() != null && t.getThisLayerDelete() == 1) {
                        deleteList.add(t.getOrganizeId());
                    }
                    // 新增
                    if (t.getThisLayerAdd() != null && t.getThisLayerAdd() == 1) {
                        addList.add(t.getOrganizeId());
                    }
                }
                // 查询
                if (t.getSubLayerSelect() != null && t.getSubLayerSelect() == 1) {
                    List<String> underOrganizations = organizeService.getUnderOrganizations(t.getOrganizeId(), false);
                    selectList.addAll(underOrganizations);
                    // 修改
                    if (t.getSubLayerEdit() != null && t.getSubLayerEdit() == 1) {
                        editList.addAll(underOrganizations);
                    }
                    // 删除
                    if (t.getSubLayerDelete() != null && t.getSubLayerDelete() == 1) {
                        deleteList.addAll(underOrganizations);
                    }
                    // 新增
                    if (t.getSubLayerAdd() != null && t.getSubLayerAdd() == 1) {
                        addList.addAll(underOrganizations);
                    }
                }
            }
        });
        OrganizeAdministratorModel model = new OrganizeAdministratorModel(addList, editList, deleteList, selectList);
        return model;
    }

}
