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;
/**
*
* 服务实现类
*
*
* @author
* @since 2023-07-27
*/
@Service
public class TmDepartmentServiceImpl extends ServiceImpl 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 tmDepartments = new ArrayList<>();
this.getParentList(dept.getParentId(),tmDepartments);
List 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 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 tmDepartments = new ArrayList<>();
this.getParentList(dept.getParentId(),tmDepartments);
List collect2 = tmDepartments.stream().filter(entity -> !entity.getId().equals("0")).collect(Collectors.toList());
if(collect2.size() >=4){
throw new BusinessException(ResultCode.DEPT_ADD_ERROR);
}
List sonByDeptId = this.getSonByDeptId(dept.getId());
if(sonByDeptId.size() >0){
List 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 deptList = this.getDeptListByParentId(department);
if(deptList .size() >0){
throw new BusinessException(ResultCode.DEPT_DEL_ERROR1);
}
List 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 wrapper = new LambdaQueryWrapper<>();
wrapper.eq(TmDepartment::getName,name);
if(StringUtils.isNotBlank(id)){
wrapper.ne(TmDepartment::getId,id);
}
List list = this.list(wrapper);
if(list.size() >0){
return false;
}
return true;
}
@Override
public List getDeptList() {
String userId =(String) StpUtil.getLoginId();
TmUser tmUser = tmUserService.getById(userId);
if(tmUser == null){
return new ArrayList<>();
}
List 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 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 getDeptListByParentId(TmDepartment department) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
if(department.getId() == null){
queryWrapper.isNull(TmDepartment::getParentId);
}else {
queryWrapper.eq(TmDepartment::getParentId,department.getId());
}
queryWrapper.orderByDesc(TmDepartment::getCreateTime);
List 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 getByZdDeptIds() {
String deptId = this.getDeptId();
String zdDeptId = this.getZdDeptId(deptId);
List deptList = this.getSonByDeptId(zdDeptId);
List 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 getDeptIds() {
List Ids = new ArrayList<>();
List 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 departments, List 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 getSonByDeptId(String deptId) {
List listAll = new ArrayList<>();
converSon(Arrays.asList(deptId),listAll);
return listAll;
}
private void converSon(List deptIds, List listAll){
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(TmDepartment::getParentId,deptIds);
List list = this.list(wrapper);
if(list.size() <=0){
return ;
}
listAll.addAll(list);
List ids = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
converSon(ids,listAll);
}
@Override
public HashMap getMapByDeptIds(Set deptIdSet) {
HashMap map = new HashMap<>();
if(deptIdSet.size() >0){
List tmDepartments = this.listByIds(deptIdSet);
tmDepartments.forEach(entity -> map.put(entity.getId(),entity));
}
return map;
}
@Override
public List getLikeName(String organizerDeptName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(TmDepartment::getName,organizerDeptName);
return this.list(wrapper);
}
@Override
public List getSonByDeptIdAndDeptIds(List deptIds, String deptId) {
List deptIds2 = new ArrayList<>();
if(StringUtils.isNotBlank(deptId)){
List sonByDeptId = this.getSonByDeptId(deptId);
deptIds2 = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
deptIds2.add(deptId);
}
if(!deptIds2.isEmpty()){
List 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;
}
}