package org.jeecg.modules.dnc.service.impl;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.jeecg.modules.activiti.entity.ActivitiDefinition;
|
import org.jeecg.modules.activiti.service.IActivitiDefinitionService;
|
import org.jeecg.modules.dnc.dto.DepartApproveUser;
|
import org.jeecg.modules.dnc.dto.DepartmentExt;
|
import org.jeecg.modules.dnc.dto.DepartmentUser;
|
import org.jeecg.modules.dnc.entity.DevicePermissionStream;
|
import org.jeecg.modules.dnc.entity.PermissionStream;
|
import org.jeecg.modules.dnc.exception.ExceptionCast;
|
import org.jeecg.modules.dnc.mapper.DepartmentMapper;
|
import org.jeecg.modules.dnc.request.DepartmentRequest;
|
import org.jeecg.modules.dnc.response.CommonCode;
|
import org.jeecg.modules.dnc.response.CommonJsonTree;
|
import org.jeecg.modules.dnc.response.DepartmentCode;
|
import org.jeecg.modules.dnc.response.QueryPageResponseResult;
|
import org.jeecg.modules.dnc.service.*;
|
import org.jeecg.modules.dnc.service.support.DepartmentTreeWrapper;
|
import org.jeecg.modules.dnc.ucenter.Department;
|
import org.jeecg.modules.dnc.utils.ValidateUtil;
|
import org.jeecg.modules.system.entity.SysUser;
|
import org.jeecg.modules.system.service.ISysUserService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.*;
|
|
@Service
|
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
|
@Autowired
|
private ISysUserService userService;
|
@Autowired
|
private IDepartApproveUserService departApproveUserService;
|
@Autowired
|
private IDepartmentUserService departmentUserService;
|
@Autowired
|
private IPermissionStreamService permissionStreamService;
|
@Autowired
|
private IDevicePermissionStreamService devicePermissionStreamService;
|
@Autowired
|
private IActivitiDefinitionService activitiDefinitionService;
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean addDepartment(Department department) {
|
if(department == null)
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
if(!ValidateUtil.validateString(department.getDepartCode()))
|
ExceptionCast.cast(DepartmentCode.DEPART_CODE_NONE);
|
if(!ValidateUtil.validateString(department.getDepartName()))
|
ExceptionCast.cast(DepartmentCode.DEPART_NAME_NONE);
|
Department en = this.getByDepartCode(department.getDepartCode(), null);
|
if(en != null)
|
ExceptionCast.cast(DepartmentCode.DEPART_IS_EXIST);
|
if(ValidateUtil.validateString(department.getParentId())) {
|
Department parent = super.getById(department.getParentId());
|
if(parent == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_PARENT_NOT_EXIST);
|
department.setRankLevel(parent.getRankLevel() + 1);
|
}else {
|
department.setParentId(null);
|
department.setRankLevel(1);
|
}
|
return super.save(department);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean editDepartment(String id, Department department) {
|
if(!ValidateUtil.validateString(id) || department == null)
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
Department en = super.getById(id);
|
if (en == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
|
department.setDepartId(id);
|
department.setDepartCode(null);
|
if(ValidateUtil.validateString(department.getParentId())) {
|
if (!department.getParentId().equals(en.getParentId())) {
|
Department parent = super.getById(department.getParentId());
|
if(parent == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_PARENT_NOT_EXIST);
|
department.setRankLevel(parent.getRankLevel() + 1);
|
}
|
}else {
|
department.setRankLevel(1);
|
}
|
return super.updateById(department);
|
}
|
|
@Override
|
public Department getByDepartCode(String departCode, String departId) {
|
if(!ValidateUtil.validateString(departCode))
|
return null;
|
LambdaQueryChainWrapper<Department> lambdaQueryChain = super.lambdaQuery();
|
lambdaQueryChain.eq(Department::getDepartCode, departCode);
|
if(ValidateUtil.validateString(departId)) {
|
lambdaQueryChain.ne(Department::getDepartId, departCode);
|
}
|
List<Department> departmentList = lambdaQueryChain.list();
|
if(!ValidateUtil.validateString(departCode))
|
return null;
|
if(departmentList == null || departmentList.isEmpty())
|
return null;
|
return departmentList.get(0);
|
}
|
|
@Override
|
public List<Department> getUserNonPermDepart(String userId) {
|
return super.getBaseMapper().getUserNonPermDepart(userId);
|
}
|
|
@Override
|
public List<Department> getUserPermDepart(String userId) {
|
return super.getBaseMapper().getUserPermDepart(userId);
|
}
|
|
@Override
|
public QueryPageResponseResult<Department> findPageList(int page, int size, DepartmentRequest requestParams) {
|
if(page < 1 || size < 1) {
|
ExceptionCast.cast(CommonCode.INVALID_PAGE);
|
}
|
IPage<Department> pageData = new Page<>(page, size);
|
LambdaQueryChainWrapper<Department> lambdaQuery = super.lambdaQuery();
|
if(requestParams != null) {
|
if(ValidateUtil.validateString(requestParams.getDepartName())) {
|
lambdaQuery.like(Department::getDepartName, requestParams.getDepartName());
|
}
|
if(ValidateUtil.validateString(requestParams.getDepartCode())) {
|
lambdaQuery.like(Department::getDepartCode, requestParams.getDepartCode());
|
}
|
if(ValidateUtil.validateString(requestParams.getId())) {
|
lambdaQuery.eq(Department::getParentId,requestParams.getId());
|
}
|
if(ValidateUtil.validateString(requestParams.getAscStr())) {
|
String[] ascArr = requestParams.getAscStr().split(",");
|
// ((Page<Department>) pageData).setAsc(ascArr);
|
}
|
if(ValidateUtil.validateString(requestParams.getDescStr())) {
|
String[] descStr = requestParams.getDescStr().split(",");
|
// ((Page<Department>) pageData).setDesc(descStr);
|
}
|
}
|
IPage<Department> userIPage = lambdaQuery.page(pageData);
|
return new QueryPageResponseResult<>(CommonCode.SUCCESS, userIPage);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean deleteDepartmentById(String id) {
|
if(!ValidateUtil.validateString(id)) {
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
}
|
Department en = super.getById(id);
|
if (en == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
|
List<Department> list = findSunLint(id);
|
if (list != null && list.size() > 0) {
|
ExceptionCast.cast(DepartmentCode.DEPART_SUN_EXIST);
|
}
|
List<DepartmentUser> departmentUserList = departmentUserService.findByDepartId(id);
|
if(departmentUserList != null && !departmentUserList.isEmpty())
|
ExceptionCast.cast(DepartmentCode.DEPART_USER_EXIST);
|
List<PermissionStream> permissionStreamList = permissionStreamService.findByDepartId(id);
|
if(permissionStreamList != null && !permissionStreamList.isEmpty())
|
ExceptionCast.cast(DepartmentCode.DEPART_PRODUCT_EXIST);
|
List<DevicePermissionStream> devicePermissionStreams = devicePermissionStreamService.findByDepartId(id);
|
if(devicePermissionStreams != null && !devicePermissionStreams.isEmpty())
|
ExceptionCast.cast(DepartmentCode.DEPART_DEVICE_EXIST);
|
ActivitiDefinition activitiDefinition = activitiDefinitionService.getByDepartId(id);
|
if(activitiDefinition != null)
|
ExceptionCast.cast(DepartmentCode.DEPART_ACT_EXIST);
|
return super.removeById(id);
|
}
|
|
@Override
|
public List<Department> findSunLint(String parentId) {
|
if(!ValidateUtil.validateString(parentId))
|
return null;
|
List<Department> list = super.lambdaQuery().eq(Department::getParentId, parentId).list();
|
if(list == null || list.isEmpty())
|
return null;
|
return list;
|
}
|
|
@Override
|
public List<CommonJsonTree> loadTree() {
|
List<DepartmentExt> multilevelExt = this.getBaseMapper().findExtAll();
|
return DepartmentTreeWrapper.loadTree(multilevelExt);
|
}
|
|
@Override
|
public Map<String, Department> getMapByUserId(String userId) {
|
if(!ValidateUtil.validateString(userId))
|
return null;
|
List<Department> userPermDepart = getUserPermDepart(userId);
|
if(userPermDepart == null || userPermDepart.isEmpty())
|
return null;
|
Map<String, Department> map = new HashMap<>();
|
userPermDepart.forEach(item -> {
|
map.put(item.getDepartId(), item);
|
});
|
return map;
|
}
|
|
@Override
|
public List<SysUser> getUserNonApproveDepart(String departId) {
|
return super.getBaseMapper().getUserNonApproveDepart(departId);
|
}
|
|
@Override
|
public List<SysUser> getUserApproveDepart(String departId) {
|
return super.getBaseMapper().getUserApproveDepart(departId);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean assignAddApproveUser(String departId, Integer relativeFlag, String[] userIds) {
|
if(!ValidateUtil.validateString(departId) ||! ValidateUtil.validateInteger(relativeFlag))
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
if(userIds == null || userIds.length < 1)
|
ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
|
Department en = super.getById(departId);
|
if(en == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
|
List<String> userIdList = new ArrayList<>(userIds.length);
|
Collections.addAll(userIdList, userIds);
|
Collection<SysUser> userCollection = userService.listByIds(userIdList);
|
if(userCollection == null || userCollection.size() != userIds.length)
|
ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
|
List<DepartApproveUser> permissionList = new ArrayList<>();
|
userCollection.forEach(item -> {
|
DepartApproveUser approveUser = departApproveUserService.getByDepartIdAndUserId(en.getDepartId(), item.getId());
|
if(approveUser == null) {
|
approveUser = new DepartApproveUser();
|
approveUser.setUserId(item.getId());
|
approveUser.setDepartId(en.getDepartId());
|
permissionList.add(approveUser);
|
}
|
});
|
if(!permissionList.isEmpty()) {
|
return departApproveUserService.saveBatch(permissionList);
|
}
|
return false;
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean assignRemoveApproveUser(String departId, Integer relativeFlag, String[] userIds) {
|
if(!ValidateUtil.validateString(departId) ||! ValidateUtil.validateInteger(relativeFlag))
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
if(userIds == null || userIds.length < 1)
|
ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
|
Department en = super.getById(departId);
|
if(en == null)
|
ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
|
List<String> userIdList = new ArrayList<>(userIds.length);
|
Collections.addAll(userIdList, userIds);
|
Collection<SysUser> userCollection = userService.listByIds(userIdList);
|
if(userCollection == null || userCollection.size() != userIds.length)
|
ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
|
List<DepartApproveUser> permissionList = new ArrayList<>();
|
userCollection.forEach(item -> {
|
DepartApproveUser approveUser = departApproveUserService.getByDepartIdAndUserId(en.getDepartId(), item.getId());
|
if(approveUser != null) {
|
permissionList.add(approveUser);
|
}
|
});
|
if(!permissionList.isEmpty()) {
|
return departApproveUserService.removeByCollection(permissionList);
|
}
|
return false;
|
}
|
}
|