package com.chinaztt.mes.aps.service.impl; import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.aps.dto.ApsResourceGroupDTO; import com.chinaztt.mes.aps.entity.ApsResourceGroup; import com.chinaztt.mes.aps.entity.ApsResourceRelation; import com.chinaztt.mes.aps.mapper.ApsResourceGroupMapper; import com.chinaztt.mes.aps.service.ApsResourceRelationService; import com.chinaztt.mes.aps.service.ApsResourceGroupService; import com.chinaztt.ztt.admin.api.dto.DeptTree; import com.chinaztt.ztt.admin.api.vo.TreeUtil; import com.chinaztt.ztt.common.data.datascope.DataScope; import lombok.AllArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * @author sunxiaoling * @date 2020-08-27 10:32:31 */ @Service @AllArgsConstructor public class ApsResourceGroupServiceImpl extends ServiceImpl implements ApsResourceGroupService { private final ApsResourceRelationService apsResourceRelationService; /** * 添加资源分组部门 * @param resource 资源 * @return */ @Override @Transactional(rollbackFor = Exception.class) public Boolean saveResource(ApsResourceGroup resource) { ApsResourceGroup apsResource = new ApsResourceGroup(); BeanUtils.copyProperties(resource, apsResource); this.save(apsResource); apsResourceRelationService.insertDeptRelation(apsResource); return Boolean.TRUE; } /** * 删除资源分组 * @param id 部门 ID * @return 成功、失败 */ @Override @Transactional(rollbackFor = Exception.class) public Boolean removeDeptById(Integer id) { // 级联删除部门 List idList = apsResourceRelationService .list(Wrappers.query().lambda().eq(ApsResourceRelation::getAncestry,id.longValue())).stream() .map(ApsResourceRelation::getDescendants).collect(Collectors.toList()); if (CollUtil.isNotEmpty(idList)) { this.removeByIds(idList); } // 删除部门级联关系 apsResourceRelationService.deleteAllDeptRealtion(id); return Boolean.TRUE; } /** * 更新资源分组 * @param apsResource 资源分组信息 * @return 成功、失败 */ @Override @Transactional(rollbackFor = Exception.class) public Boolean updateDeptById(ApsResourceGroup apsResource) { // 更新部门状态 this.updateById(apsResource); // 更新部门关系 ApsResourceRelation relation = new ApsResourceRelation(); relation.setAncestry(apsResource.getParentId()); relation.setDescendants(apsResource.getId()); apsResourceRelationService.updateDeptRealtion(relation); return Boolean.TRUE; } @Override public ApsResourceGroupDTO getResourceById(Integer id) { return baseMapper.getResourceById(id); } /** * 查询全部资源分组树 * @return 树 */ @Override public List selectTree() { // 查询全部资源分组 List deptAllList = baseMapper.selectList(Wrappers.emptyWrapper()); // 查询数据权限内资源分组 List deptOwnIdList = baseMapper.selectList(Wrappers.emptyWrapper()).stream() .map(ApsResourceGroup::getId).collect(Collectors.toList()); // 权限内部门 List collect = deptAllList.stream().filter(dept -> dept.getId().intValue() != dept.getParentId()) .sorted(Comparator.comparing(ApsResourceGroup::getRscGroupNo)).map(dept -> { DeptTree node = new DeptTree(); node.setId(dept.getId().intValue()); node.setParentId(dept.getParentId().intValue()); node.setName(dept.getRscGroupName()); // 有权限不返回标识 if (deptOwnIdList.contains(dept.getId())) { node.setIsLock(Boolean.FALSE); } return node; }).collect(Collectors.toList()); return TreeUtil.build(collect, 0); } }