package com.qianwen.smartman.modules.system.service.impl;
|
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.secure.utils.AuthUtil;
|
import com.qianwen.core.tool.constant.BladeConstant;
|
import com.qianwen.core.tool.node.ForestNodeMerger;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.smartman.common.cache.SysCache;
|
import com.qianwen.smartman.common.utils.Lambda;
|
import com.qianwen.smartman.modules.cps.entity.Employee;
|
import com.qianwen.smartman.modules.cps.service.IEmployeeService;
|
import com.qianwen.smartman.modules.system.entity.Dept;
|
import com.qianwen.smartman.modules.system.entity.DeptScope;
|
import com.qianwen.smartman.modules.system.mapper.DeptMapper;
|
import com.qianwen.smartman.modules.system.service.IDataScopeManagerService;
|
import com.qianwen.smartman.modules.system.service.IDeptScopeService;
|
import com.qianwen.smartman.modules.system.service.IUserDeptService;
|
import com.qianwen.smartman.modules.system.vo.DataScopeGrantVO;
|
import com.qianwen.smartman.modules.system.vo.DeptVO;
|
import com.qianwen.smartman.modules.system.wrapper.DeptWrapper;
|
|
@Service
|
public class DataScopeManagerServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDataScopeManagerService {
|
private static final String TENANT_ID = "tenantId";
|
private static final String PARENT_ID = "parentId";
|
private final IDeptScopeService deptScopeService;
|
private final IUserDeptService userDeptService;
|
private final IEmployeeService employeeService;
|
|
|
public DataScopeManagerServiceImpl(final IDeptScopeService deptScopeService, final IUserDeptService userDeptService, final IEmployeeService employeeService) {
|
this.deptScopeService = deptScopeService;
|
this.userDeptService = userDeptService;
|
this.employeeService = employeeService;
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<DeptVO> lazyList(String tenantId, Long parentId, Map<String, Object> param) {
|
if (AuthUtil.isAdministrator()) {
|
tenantId = "";
|
}
|
String paramTenantId = Func.toStr(param.get(TENANT_ID));
|
if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
|
tenantId = paramTenantId;
|
}
|
if (Func.isEmpty(param.get(PARENT_ID)) && param.size() == 1) {
|
parentId = BladeConstant.TOP_PARENT_ID;
|
}
|
if (Func.toLong(parentId) == BladeConstant.TOP_PARENT_ID.longValue() && !AuthUtil.isAdministrator()) {
|
Long deptId = Func.firstLong(AuthUtil.getDeptId());
|
Dept dept = SysCache.getDept(deptId);
|
if (dept.getParentId().longValue() != 0) {
|
parentId = dept.getParentId();
|
}
|
}
|
if (Func.isEmpty(param.get(PARENT_ID)) && param.size() > 1 && Func.toLong(parentId) == 0) {
|
parentId = null;
|
}
|
return ((DeptMapper) this.baseMapper).lazyList(tenantId, parentId, param);
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<DeptVO> tree(String tenantId) {
|
return ForestNodeMerger.merge(((DeptMapper) this.baseMapper).tree(tenantId));
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<DeptVO> lazyTree(String tenantId, Long parentId) {
|
if (AuthUtil.isAdministrator()) {
|
tenantId = "";
|
}
|
return ForestNodeMerger.merge(((DeptMapper) this.baseMapper).lazyTree(tenantId, parentId));
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public String getDeptIds(String tenantId, String deptNames) {
|
List<Dept> deptList = this.baseMapper.selectList(Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId).in(Dept::getDeptName, Func.toStrList(deptNames)));
|
/*
|
List<Dept> deptList = ((DeptMapper) this.baseMapper).selectList((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
|
return v0.getTenantId();
|
}, tenantId)).in((v0) -> {
|
return v0.getDeptName();
|
}, Func.toStrList(deptNames)));*/
|
if (deptList != null && deptList.size() > 0) {
|
return deptList.stream().map(dept -> {
|
return Func.toStr(dept.getId());
|
}).distinct().collect(Collectors.joining(","));
|
}
|
return null;
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public String getDeptIdsByFuzzy(String tenantId, String deptNames) {
|
LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId);
|
/*
|
Wrapper wrapper = (LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
|
return v0.getTenantId();
|
}, tenantId);*/
|
wrapper.and(wrapper2 -> {
|
List<String> names = Func.toStrList(deptNames);
|
names.forEach(name -> {
|
LambdaQueryWrapper lambdaQueryWrapper = wrapper2.like((v0) -> {
|
return v0.getDeptName();
|
}, name).or();
|
});
|
});
|
List<Dept> deptList = ((DeptMapper) this.baseMapper).selectList(wrapper);
|
if (deptList != null && deptList.size() > 0) {
|
return (String) deptList.stream().map(dept -> {
|
return Func.toStr(dept.getId());
|
}).distinct().collect(Collectors.joining(","));
|
}
|
return null;
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<String> getDeptNames(String deptIds) {
|
return this.baseMapper.getDeptNames(Func.toLongArray(deptIds));
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<Dept> getDeptChild(Long deptId) {
|
return this.baseMapper.selectList(Wrappers.<Dept>query().lambda().like(Dept::getAncestors, deptId));
|
/*
|
return ((DeptMapper) this.baseMapper).selectList((Wrapper) Wrappers.query().lambda().like((v0) -> {
|
return v0.getAncestors();
|
}, deptId));*/
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public boolean removeDept(String ids) {
|
Long cnt = this.baseMapper.selectCount(Wrappers.<Dept>query().lambda().in(Dept::getParentId, Func.toLongList(ids)));
|
/*
|
Long cnt = ((DeptMapper) this.baseMapper).selectCount((Wrapper) Wrappers.query().lambda().in((v0) -> {
|
return v0.getParentId();
|
}, Func.toLongList(ids)));*/
|
if (cnt.longValue() > 0) {
|
throw new ServiceException("请先删除子节点!");
|
}
|
checkUsingDept(Func.toLongList(ids));
|
return removeByIds(Func.toLongList(ids));
|
}
|
|
private void checkUsingDept(List<Long> deptId) {
|
if (this.userDeptService.count(Lambda.in((v0) -> {
|
return v0.getDeptId();
|
}, deptId)) != 0) {
|
throw new ServiceException("无法删除,账号绑定了该权限");
|
}
|
|
//long match = deptId.stream().anyMatch(dept -> this.employeeService.count(Lambda.<Employee>create().findIn(Employee::getDeptId, String.valueOf(dept)))) != 0L;
|
if (deptId.stream().anyMatch(dept -> (this.employeeService.count(Lambda.<Employee>create().findIn(Employee::getDeptId, String.valueOf(dept))) != 0L))) {
|
throw new ServiceException("无法删除, 员工绑定了该权限");
|
}
|
/*
|
if (match) {
|
throw new ServiceException("无法删除, 员工绑定了该权限");
|
}*/
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public boolean submit(Dept dept) {
|
if (Func.isEmpty(dept.getParentId())) {
|
dept.setTenantId(AuthUtil.getTenantId());
|
dept.setParentId(BladeConstant.TOP_PARENT_ID);
|
dept.setAncestors(String.valueOf(BladeConstant.TOP_PARENT_ID));
|
}
|
if (dept.getParentId().longValue() > 0) {
|
Dept parent = (Dept) getById(dept.getParentId());
|
if (Func.toLong(dept.getParentId()) == Func.toLong(dept.getId())) {
|
throw new ServiceException("父节点不可选择自身!");
|
}
|
dept.setTenantId(parent.getTenantId());
|
String ancestors = parent.getAncestors() + "," + dept.getParentId();
|
dept.setAncestors(ancestors);
|
}
|
checkExistDeptNameWithSameLevel(dept.getTenantId(), dept.getParentId(), dept.getId(), dept.getDeptName());
|
dept.setIsDeleted(0);
|
return saveOrUpdate(dept);
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<DeptVO> search(String deptName, Long parentId) {
|
LambdaQueryWrapper<Dept> lambda = Wrappers.<Dept>query().lambda();
|
/*
|
Wrapper lambda = Wrappers.query().lambda();*/
|
if (Func.isNotEmpty(deptName)) {
|
lambda.like((v0) -> {
|
return v0.getDeptName();
|
}, deptName);
|
}
|
if (Func.isNotEmpty(parentId) && parentId.longValue() > BladeConstant.TOP_PARENT_ID.longValue()) {
|
lambda.eq((v0) -> {
|
return v0.getParentId();
|
}, parentId);
|
}
|
List<Dept> deptList = this.baseMapper.selectList(lambda);
|
return DeptWrapper.build().listNodeVO(deptList);
|
}
|
|
private void checkExistDeptNameWithSameLevel(String tenantId, Long parentId, Long id, String name) {
|
List<Dept> list = list(Wrappers.<Dept>lambdaQuery().eq(Dept::getTenantId, tenantId).eq(Dept::getParentId, parentId)
|
.eq(Dept::getDeptName, name).ne(Func.isNotEmpty(id), Dept::getId, id));
|
/*
|
List<Dept> list = list(((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getTenantId();
|
}, tenantId)).eq((v0) -> {
|
return v0.getParentId();
|
}, parentId)).eq((v0) -> {
|
return v0.getDeptName();
|
}, name)).ne(Func.isNotEmpty(id), (v0) -> {
|
return v0.getId();
|
}, id));*/
|
if (Func.isNotEmpty(list)) {
|
throw new ServiceException("同一父机构下不能存在同名机构名称");
|
}
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
@Transactional
|
public boolean grant(final DataScopeGrantVO grantVO) {
|
this.deptScopeService.remove(Lambda.eq((v0) -> {
|
return v0.getDeptId();
|
}, grantVO.getDeptId()));
|
List<DeptScope> deptScopeList = grantVO.getDataScopeIds().stream().map(scopeId -> {
|
DeptScope scope = new DeptScope();
|
scope.setScopeId(scopeId);
|
scope.setDeptId(grantVO.getDeptId());
|
scope.setScopeCategory(1);
|
return scope;
|
}).collect(Collectors.toList());
|
this.deptScopeService.saveBatch(deptScopeList);
|
return true;
|
}
|
|
@Override // org.springblade.modules.system.service.IDataScopeManagerService
|
public List<String> grantSelect(final String deptId) {
|
return this.deptScopeService.listObjs(Lambda.eq(DeptScope::getDeptId, deptId).select(DeptScope::getScopeId), String::valueOf);
|
/*
|
return this.deptScopeService.listObjs(Lambda.eq((v0) -> {
|
return v0.getDeptId();
|
}, deptId).select((v0) -> {
|
return v0.getScopeId();
|
}), String::valueOf);*/
|
}
|
}
|