123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package com.fdkankan.fusion.service.impl;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.util.IdUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.common.enums.IdPreEnum;
- import com.fdkankan.fusion.entity.*;
- import com.fdkankan.fusion.exception.BusinessException;
- import com.fdkankan.fusion.mapper.ITmDepartmentMapper;
- import com.fdkankan.fusion.service.ITmDepartmentService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.fusion.service.ITmRoleService;
- import com.fdkankan.fusion.service.ITmUserRoleService;
- import com.fdkankan.fusion.service.ITmUserService;
- import org.apache.catalina.User;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- import java.util.logging.Level;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2023-07-27
- */
- @Service
- public class TmDepartmentServiceImpl extends ServiceImpl<ITmDepartmentMapper, TmDepartment> implements ITmDepartmentService {
- @Autowired
- ITmUserService tmUserService;
- @Autowired
- ITmUserRoleService tmUserRoleService;
- @Autowired
- ITmRoleService tmRoleService;
- @Override
- public void insertDept(TmDepartment dept) {
- if(!checkDeptNameUnique(dept.getName(),null)){
- throw new BusinessException(ResultCode.DEPT_NAME_EXITS);
- }
- List<TmDepartment> tmDepartments = new ArrayList<>();
- this.getParentList(dept.getParentId(),tmDepartments);
- List<TmDepartment> collect = tmDepartments.stream().filter(entity -> !entity.getId().equals("0")).collect(Collectors.toList());
- if(collect.size() >=4){
- throw new BusinessException(ResultCode.DEPT_ADD_ERROR);
- }
- dept.setId(IdPreEnum.DEPARTMENT_PRE.getPre() +IdUtil.getSnowflake(1).nextId() );
- //dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
- dept.setCreatorId((String) StpUtil.getLoginId());
- this.save(dept);
- }
- private void getParentList(String deptId,List<TmDepartment> tmDepartments) {
- if(StringUtils.isNotBlank(deptId)){
- TmDepartment tmDepartment = this.getById(deptId);
- if(tmDepartment != null){
- tmDepartments.add(tmDepartment);
- getParentList(tmDepartment.getParentId(),tmDepartments);
- }
- }
- }
- @Override
- public void updateDept(TmDepartment dept) {
- if(!checkDeptNameUnique(dept.getName(),dept.getId())){
- throw new BusinessException(ResultCode.DEPT_NAME_EXITS);
- }
- if(dept.getId().equals(dept.getParentId())){
- throw new BusinessException(ResultCode.DEPT_EDIT_ERROR);
- }
- List<TmDepartment> tmDepartments = new ArrayList<>();
- this.getParentList(dept.getParentId(),tmDepartments);
- List<TmDepartment> collect2 = tmDepartments.stream().filter(entity -> !entity.getId().equals("0")).collect(Collectors.toList());
- if(collect2.size() >=4){
- throw new BusinessException(ResultCode.DEPT_ADD_ERROR);
- }
- List<TmDepartment> sonByDeptId = this.getSonByDeptId(dept.getId());
- if(sonByDeptId.size() >0){
- List<String> collect = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
- if(collect.contains(dept.getParentId())){
- throw new BusinessException(ResultCode.DEPT_EDIT_ERROR);
- }
- }
- dept.setCreateTime(null);
- dept.setUpdateTime(null);
- this.updateById(dept);
- }
- @Override
- public void deleteDeptById(String deptId) {
- TmDepartment department = this.getById(deptId);
- if(department != null){
- List<TmDepartment> deptList = this.getDeptListByParentId(department);
- if(deptList .size() >0){
- throw new BusinessException(ResultCode.DEPT_DEL_ERROR1);
- }
- List<TmUser> userList = tmUserService.getByDeptId(deptId);
- if(userList.size() >0){
- throw new BusinessException(ResultCode.DEPT_DEL_ERROR2);
- }
- this.removeById(deptId);
- }
- }
- private boolean checkDeptNameUnique(String name, String id) {
- LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TmDepartment::getName,name);
- if(StringUtils.isNotBlank(id)){
- wrapper.ne(TmDepartment::getId,id);
- }
- List<TmDepartment> list = this.list(wrapper);
- if(list.size() >0){
- return false;
- }
- return true;
- }
- @Override
- public List<TmDepartment> getDeptList() {
- String userId =(String) StpUtil.getLoginId();
- TmUser tmUser = tmUserService.getById(userId);
- if(tmUser == null){
- return new ArrayList<>();
- }
- List<TmUserRole> tmUserRoles = tmUserRoleService.getByUserId(tmUser.getId());
- if(tmUserRoles.size() >0){
- TmUserRole tmUserRole = tmUserRoles.get(0);
- TmRole tmRole = tmRoleService.getById(tmUserRole.getRoleId());
- if(tmRole != null && tmRole.getRoleKey().equals("admin-ordinary")){
- TmDepartment tmDepartment = this.getById(tmUser.getDeptId());
- TmDepartment parentDept = this.getById(tmDepartment.getParentId());
- if(parentDept !=null){
- tmDepartment.setParentName(parentDept.getName());
- }
- return Arrays.asList(tmDepartment);
- }
- }
- return getDeptList(tmUser.getDeptId());
- }
- public List<TmDepartment> getDeptList(String deptId) {
- TmDepartment dept = this.getById(deptId);
- if(dept == null){
- return new ArrayList<>();
- }
- // if("0".equals(dept.getId())){
- // return this.getDeptListByParentId(dept);
- // }
- dept.setChildren(this.getDeptListByParentId(dept));
- TmDepartment parentDept = this.getById(dept.getParentId());
- if(parentDept !=null){
- dept.setParentName(parentDept.getName());
- }
- return Arrays.asList(dept);
- }
- public List<TmDepartment> getDeptListByParentId(TmDepartment department) {
- LambdaQueryWrapper<TmDepartment> queryWrapper = new LambdaQueryWrapper<>();
- if(department.getId() == null){
- queryWrapper.isNull(TmDepartment::getParentId);
- }else {
- queryWrapper.eq(TmDepartment::getParentId,department.getId());
- }
- queryWrapper.orderByDesc(TmDepartment::getCreateTime);
- List<TmDepartment> list = this.list(queryWrapper);
- if(list.size() <=0){
- return list;
- }
- for (TmDepartment deptVo : list) {
- deptVo.setChildren(getDeptListByParentId(deptVo));
- deptVo.setParentName(department.getName());
- }
- return list;
- }
- @Override
- public List<String> getByZdDeptIds() {
- String deptId = this.getDeptId();
- String zdDeptId = this.getZdDeptId(deptId);
- List<TmDepartment> deptList = this.getSonByDeptId(zdDeptId);
- List<String> deptIds = deptList.stream().map(TmDepartment::getId).collect(Collectors.toList());
- deptIds.add(zdDeptId);
- return deptIds;
- }
- private String getZdDeptId(String deptId) {
- TmDepartment tmDepartment = this.getById(deptId);
- if(tmDepartment == null){
- throw new BusinessException(ResultCode.NOT_DEPT.code,ResultCode.NOT_DEPT.msg +":"+deptId);
- }
- if(tmDepartment.getDeptType()==0 || tmDepartment.getDeptType()==1 ){
- return tmDepartment.getId();
- }
- return getZdDeptId(tmDepartment.getParentId());
- }
- @Override
- public List<String> getDeptIds() {
- List<String> Ids = new ArrayList<>();
- List<TmDepartment> deptList = this.getDeptList();
- getDeptIds(deptList,Ids);
- return Ids;
- }
- @Override
- public String getDeptId() {
- String userId =(String) StpUtil.getLoginId();
- TmUser tmUser = tmUserService.getById(userId);
- if(tmUser == null){
- return null;
- }
- return tmUser.getDeptId();
- }
- public void getDeptIds(List<TmDepartment> departments, List<String > Ids) {
- for (TmDepartment department : departments) {
- Ids.add(department.getId());
- if(department.getChildren() != null && department.getChildren().size() >0){
- this.getDeptIds(department.getChildren(),Ids);
- }
- }
- }
- @Override
- public List<TmDepartment> getSonByDeptId(String deptId) {
- List<TmDepartment> listAll = new ArrayList<>();
- converSon(Arrays.asList(deptId),listAll);
- return listAll;
- }
- private void converSon(List<String> deptIds, List<TmDepartment> listAll){
- LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(TmDepartment::getParentId,deptIds);
- List<TmDepartment> list = this.list(wrapper);
- if(list.size() <=0){
- return ;
- }
- listAll.addAll(list);
- List<String> ids = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
- converSon(ids,listAll);
- }
- @Override
- public HashMap<String, TmDepartment> getMapByDeptIds(Set<String> deptIdSet) {
- HashMap<String,TmDepartment> map = new HashMap<>();
- if(deptIdSet.size() >0){
- List<TmDepartment> tmDepartments = this.listByIds(deptIdSet);
- tmDepartments.forEach(entity -> map.put(entity.getId(),entity));
- }
- return map;
- }
- @Override
- public List<TmDepartment> getLikeName(String organizerDeptName) {
- LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
- wrapper.like(TmDepartment::getName,organizerDeptName);
- return this.list(wrapper);
- }
- @Override
- public List<String> getSonByDeptIdAndDeptIds(List<String> deptIds, String deptId) {
- List<String> deptIds2 = new ArrayList<>();
- if(StringUtils.isNotBlank(deptId)){
- List<TmDepartment> sonByDeptId = this.getSonByDeptId(deptId);
- deptIds2 = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
- deptIds2.add(deptId);
- }
- if(!deptIds2.isEmpty()){
- List<String> collect = deptIds.stream().filter(deptIds2::contains).collect(Collectors.toList());
- if(collect.size()<=0){
- collect.add("empty-dept");
- }
- return collect;
- }
- if(deptIds.size()<=0){
- deptIds.add("empty-dept");
- }
- return deptIds;
- }
- }
|