package com.fzzy.igds.sys;
|
|
import com.fzzy.igds.dzhwk.constant.Constant;
|
import com.fzzy.igds.dzhwk.constant.RedisConst;
|
import com.fzzy.igds.util.ContextUtil;
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
import com.ruoyi.system.service.ISysDeptService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* @Description
|
* @Author CZT
|
* @Date 2024/11/22 18:03
|
*/
|
@Slf4j
|
@Service("sys.sysDeptService")
|
public class SysDeptService {
|
|
@Resource
|
private SysUserMapper userMapper;
|
@Resource
|
private ISysDeptService iSysDeptService;
|
@Resource
|
private RedisCache redisCache;
|
|
public void initUserDeptMap(String companyId) {
|
if(StringUtils.isEmpty(companyId)){
|
companyId = ContextUtil.getCompanyId();
|
}
|
|
SysUser sysUser = new SysUser();
|
sysUser.setCompanyId(companyId);
|
List<SysUser> list = userMapper.selectUserList(sysUser);
|
if (null == list || list.isEmpty()){
|
return;
|
}
|
for (SysUser userDept : list) {
|
ContextUtil.updateSubDept(userDept.getLoginName(), userDept.getDeptId().toString());
|
}
|
}
|
|
/**
|
* 刷新部门架构信息到缓存
|
* @param companyId
|
*/
|
public List<SysDept> flushDeptCache(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
SysDept sysDept = new SysDept();
|
sysDept.setCompanyId(companyId);
|
List<SysDept> listSysDept = iSysDeptService.selectDeptList(sysDept);
|
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
|
|
redisCache.setCacheObject(key, listSysDept);
|
return listSysDept;
|
}
|
|
/**
|
* 根据组织编码获取所有部门信息
|
* @param companyId
|
* @return
|
*/
|
public List<SysDept> getCacheDept(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
|
|
List<SysDept> listSysDept = redisCache.getCacheObject(key);
|
if(null == listSysDept){
|
listSysDept = flushDeptCache(companyId);
|
}
|
return listSysDept;
|
}
|
|
/**
|
* 缓存获取组织下所有库区列表
|
* @param companyId
|
*/
|
public List<SysDept> getAllDeptByCompanyId(String companyId) {
|
List<SysDept> list = getCacheDept(companyId);
|
return list.stream()
|
.filter(item -> item.getType().equals(Constant.DEPT_TYPE_20))
|
.collect(Collectors.toList());
|
}
|
|
/**
|
* 根据id获取部门信息
|
* @param companyId
|
* @param deptId
|
* @return
|
*/
|
public SysDept getCacheDept(String companyId, String deptId) {
|
if (StringUtils.isEmpty(deptId)) {
|
return null;
|
}
|
List<SysDept> list = getCacheDept(companyId);
|
|
if (null == list || list.isEmpty()){
|
return null;
|
}
|
|
for (SysDept dept : list) {
|
if (deptId.equals(dept.getDeptId() + ""))
|
return dept;
|
}
|
return null;
|
}
|
|
/**
|
* 根据类型获取部门信息
|
* type: 10-公司,20-库区,30-部门
|
* @param type
|
* @return
|
*/
|
public List<SysDept> getDeptByType(String type) {
|
if (StringUtils.isEmpty(type)) {
|
return null;
|
}
|
List<SysDept> list = getCacheDept(null);
|
if (null == list || list.isEmpty()) {
|
return null;
|
}
|
//根据类型查询
|
return list.stream()
|
.filter(item -> type.equals(item.getType()))
|
.collect(Collectors.toList());
|
}
|
}
|