/*
|
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the dreamlu.net developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: Chill 庄骞 (smallchill@163.com)
|
*/
|
package org.sxkj.system.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.logging.log4j.util.Strings;
|
import org.springblade.core.log.exception.ServiceException;
|
import org.springblade.core.secure.utils.AuthUtil;
|
import org.springblade.core.tool.constant.BladeConstant;
|
import org.springblade.core.tool.node.ForestNodeMerger;
|
import org.springblade.core.tool.utils.BeanUtil;
|
import org.springblade.core.tool.utils.CollectionUtil;
|
import org.springblade.core.tool.utils.Func;
|
import org.springblade.core.tool.utils.StringPool;
|
import org.springframework.stereotype.Service;
|
import org.sxkj.common.constant.WordOrderConstant;
|
import org.sxkj.common.func.Streams;
|
import org.sxkj.common.node.TreeStringNode;
|
import org.sxkj.common.utils.NodeTreeUtil;
|
import org.sxkj.common.utils.OrderNumUtils;
|
import org.sxkj.system.cache.SysCache;
|
import org.sxkj.system.entity.Dept;
|
import org.sxkj.system.excel.DeptExcel;
|
import org.sxkj.system.excel.DeptImportExcel;
|
import org.sxkj.system.mapper.DeptMapper;
|
import org.sxkj.system.param.DeptExportParam;
|
import org.sxkj.system.param.DeptPageParam;
|
import org.sxkj.system.service.IDeptService;
|
import org.sxkj.system.vo.DeptVO;
|
import org.sxkj.system.wrapper.DeptWrapper;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 服务实现类
|
*
|
* @author Chill
|
*/
|
@Service
|
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
|
|
private static final String TENANT_ID = "tenantId";
|
private static final String PARENT_ID = "parentId";
|
|
@Override
|
public List<DeptVO> lazyList(String tenantId, Long parentId, Map<String, Object> param) {
|
// 设置租户ID
|
if (AuthUtil.isAdministrator()) {
|
tenantId = StringPool.EMPTY;
|
}
|
String paramTenantId = Func.toStr(param.get(TENANT_ID));
|
if (Func.isNotEmpty(paramTenantId) && !AuthUtil.isAdministrator() && !AuthUtil.isAdmin()) {
|
tenantId = paramTenantId;
|
}
|
// 判断点击搜索但是没有查询条件的情况
|
if (Func.isEmpty(param.get(PARENT_ID)) && param.size() == 1) {
|
parentId = 0L;
|
}
|
// 判断数据权限控制,非超管角色只可看到本级及以下数据
|
if (null != parentId && Func.toLong(parentId) == 0L && !AuthUtil.isAdministrator() && !AuthUtil.isAdmin()) {
|
parentId = Func.firstLong(AuthUtil.getDeptId());
|
Dept dept = SysCache.getDept(parentId);
|
if (dept.getParentId() != 0) {
|
parentId = dept.getParentId();
|
}
|
}
|
// 判断点击搜索带有查询条件的情况
|
if (Func.isEmpty(param.get(PARENT_ID)) && param.size() > 1 && Func.toLong(parentId) == 0L) {
|
parentId = null;
|
}
|
return baseMapper.lazyList(tenantId, parentId, param);
|
}
|
|
|
@Override
|
public List<DeptVO> tree(String tenantId) {
|
return ForestNodeMerger.merge(baseMapper.tree(tenantId));
|
}
|
|
@Override
|
public List<DeptVO> lazyTree(String tenantId, Long parentId, Integer level) {
|
if (AuthUtil.isAdministrator()) {
|
tenantId = StringPool.EMPTY;
|
}
|
// 判断数据权限控制,非超管角色只可看到本级及以下数据
|
if (level == 0 && !AuthUtil.isAdministrator()) {
|
parentId = Func.firstLong(AuthUtil.getDeptId());
|
}
|
return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId, level));
|
}
|
|
@Override
|
public String getDeptIds(String tenantId, String deptNames) {
|
List<Dept> deptList = baseMapper.selectList(Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId).in(Dept::getDeptName, Func.toStrList(deptNames)));
|
if (deptList != null && deptList.size() > 0) {
|
return deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
|
}
|
return null;
|
}
|
|
@Override
|
public String getDeptIdsByFuzzy(String tenantId, String deptNames) {
|
LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId);
|
queryWrapper.and(wrapper -> {
|
List<String> names = Func.toStrList(deptNames);
|
names.forEach(name -> wrapper.like(Dept::getDeptName, name).or());
|
});
|
List<Dept> deptList = baseMapper.selectList(queryWrapper);
|
if (deptList != null && deptList.size() > 0) {
|
return deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
|
}
|
return null;
|
}
|
|
@Override
|
public List<String> getDeptNames(String deptIds) {
|
return baseMapper.getDeptNames(Func.toLongArray(deptIds));
|
}
|
|
@Override
|
public List<Dept> getDeptChild(Long deptId) {
|
return baseMapper.selectList(Wrappers.<Dept>query().lambda().like(Dept::getAncestors, deptId));
|
}
|
|
@Override
|
public boolean removeDept(String ids) {
|
Long cnt = baseMapper.selectCount(Wrappers.<Dept>query().lambda().in(Dept::getParentId, Func.toLongList(ids)));
|
if (cnt > 0L) {
|
throw new ServiceException("请先删除子节点!");
|
}
|
return removeByIds(Func.toLongList(ids));
|
}
|
|
@Override
|
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() > 0) {
|
Dept parent = getById(dept.getParentId());
|
if (Func.toLong(dept.getParentId()) == Func.toLong(dept.getId())) {
|
throw new ServiceException("父节点不可选择自身!");
|
}
|
dept.setTenantId(parent.getTenantId());
|
String ancestors = parent.getAncestors() + StringPool.COMMA + dept.getParentId();
|
dept.setAncestors(ancestors);
|
}
|
dept.setIsDeleted(BladeConstant.DB_NOT_DELETED);
|
|
return saveOrUpdate(dept);
|
}
|
|
@Override
|
public List<DeptVO> search(String deptName, Long parentId) {
|
String tenantId = AuthUtil.getTenantId();
|
LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>query().lambda();
|
if (Func.isNotEmpty(tenantId)) {
|
queryWrapper.eq(Dept::getTenantId, tenantId);
|
}
|
if (Func.isNotEmpty(deptName)) {
|
queryWrapper.like(Dept::getDeptName, deptName);
|
}
|
if (Func.isNotEmpty(parentId) && parentId > 0L) {
|
queryWrapper.eq(Dept::getParentId, parentId);
|
}
|
List<Dept> deptList = baseMapper.selectList(queryWrapper);
|
return DeptWrapper.build().listNodeVO(deptList);
|
}
|
|
/**
|
* 获取部门树形结构
|
*
|
* @param dept
|
* @return
|
*/
|
@Override
|
public List<TreeStringNode> getTree(DeptVO dept) {
|
String deptId = AuthUtil.getDeptId();
|
if (null != dept.getId()) {
|
deptId = dept.getId().toString();
|
}
|
if (AuthUtil.isAdministrator()) {
|
deptId = null;
|
}
|
return NodeTreeUtil.getStringNodeTree(baseMapper.getTreeList(deptId));
|
}
|
|
/**
|
* 自定义详情查询-根据系统配置id
|
*
|
* @param dept
|
* @return
|
*/
|
@Override
|
public DeptVO getDetailBySysConfigId(DeptVO dept) {
|
// 根据系统配置id查询机构信息并返回
|
return baseMapper.getDetailBySysConfigId(dept);
|
}
|
|
/**
|
* 下拉数据源
|
*
|
* @param deptId
|
* @return
|
*/
|
@Override
|
public List<DeptVO> selectDeptList(String deptId) {
|
if (Strings.isBlank(deptId)) {
|
deptId = AuthUtil.getDeptId();
|
}
|
// 下拉机构列表查询并返回
|
return baseMapper.selectDeptList(deptId);
|
}
|
|
/**
|
* 查询父级机构信息(一级机构)
|
*
|
* @param deptId
|
* @return
|
*/
|
@Override
|
public DeptVO getParentDeptInfo(String deptId) {
|
deptId = AuthUtil.getDeptId();
|
return baseMapper.getParentDeptInfo(deptId);
|
}
|
|
/**
|
* 查询机构树(除本机机构以外的机构)
|
*
|
* @param deptId
|
* @param deviceSn
|
* @return
|
*/
|
@Override
|
public List<TreeStringNode> getDeptTreeByNotItself(String deptId, String deviceSn) {
|
return NodeTreeUtil.getStringNodeTree(baseMapper.getDeptTreeByNotItself(deptId, deviceSn));
|
}
|
|
/**
|
* 懒加载获取部门树形结构(子集)
|
*
|
* @param param
|
* @param parentId
|
* @return
|
*/
|
@Override
|
public List<DeptVO> getChildLazyTree(Map<String, Object> param, Long parentId) {
|
return ForestNodeMerger.merge(baseMapper.getChildLazyTree(parentId, param));
|
}
|
|
/**
|
* 部门名称关系map
|
*
|
* @param deptIds
|
* @return
|
*/
|
@Override
|
public Map<Long, String> getDeptNamesMap(List<Long> deptIds) {
|
if (CollectionUtil.isEmpty(deptIds)) {
|
return Collections.emptyMap();
|
}
|
List<Dept> depts = baseMapper.selectBatchIds(deptIds);
|
return Streams.toMap(depts, Dept::getId, Dept::getDeptName);
|
}
|
|
@Override
|
public List<DeptVO> deptsByAreaCode(String areaCode) {
|
List<Dept> depts = baseMapper.selectList(Wrappers.<Dept>lambdaQuery()
|
.select(Dept::getDeptName, Dept::getId)
|
.like(StringUtils.isNoneBlank(areaCode), Dept::getAreaCode, areaCode));
|
|
List<DeptVO> list = Streams.toList(depts, dept -> {
|
DeptVO deptVO = new DeptVO();
|
BeanUtil.copyProperties(dept, deptVO);
|
return deptVO;
|
});
|
return list;
|
}
|
|
/**
|
* 分页查询
|
*
|
* @param page
|
* @param deptPageParam
|
* @return
|
*/
|
@Override
|
public IPage<DeptVO> selectDeptPage(IPage<DeptVO> page, DeptPageParam deptPageParam) {
|
return baseMapper.selectDeptPage(page, deptPageParam);
|
}
|
|
/**
|
* 导入部门
|
*
|
* @param data
|
* @param isCovered
|
*/
|
@Override
|
public void importDept(List<DeptImportExcel> data, Boolean isCovered) {
|
List<Dept> list = new ArrayList<>();
|
data.forEach(deptExcel -> {
|
// 校验必填字段
|
if (deptExcel.getDeptName() == null || deptExcel.getDeptName().trim().isEmpty()) {
|
throw new RuntimeException("机构名称不能为空");
|
}
|
if (deptExcel.getAreaCode() == null || deptExcel.getAreaCode().trim().isEmpty()) {
|
throw new RuntimeException("所属区划编码不能为空");
|
}
|
if (deptExcel.getRemark() == null || deptExcel.getRemark().trim().isEmpty()) {
|
throw new RuntimeException("机构描述不能为空");
|
}
|
if (deptExcel.getStatus() == null || deptExcel.getStatus().trim().isEmpty()) {
|
throw new RuntimeException("机构状态不能为空");
|
}
|
// 转换数据
|
Dept dept = BeanUtil.copy(deptExcel, Dept.class);
|
dept.setStatus(1);
|
dept.setIsDeleted(0);
|
dept.setCreateTime(new Date());
|
dept.setUpdateTime(new Date());
|
dept.setDeptNature(2);
|
dept.setDeptCategory(2);
|
list.add(dept);
|
});
|
if (isCovered) {
|
this.saveOrUpdateBatch(list);
|
} else {
|
list.forEach(item -> {
|
String times = OrderNumUtils.initOrderNum2(WordOrderConstant.ORG_CODE);
|
String deptCode = WordOrderConstant.ORG_PREFIX + times;
|
item.setDeptCode(deptCode);
|
});
|
this.saveBatch(list);
|
}
|
}
|
|
@Override
|
public List<DeptExcel> exportDept(DeptExportParam dept) {
|
return baseMapper.exportDept(dept);
|
}
|
|
@Override
|
public DeptVO getDetailWithAreaName(Dept dept) {
|
return baseMapper.getDetailWithAreaName(dept);
|
}
|
}
|