| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- package com.ydtech.modules.admin.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.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.ydtech.core.page.HttpResult;
- import com.ydtech.core.page.PageRequest;
- import com.ydtech.core.page.PageResult;
- import com.ydtech.exception.SystemException;
- import com.ydtech.modules.admin.dao.SysAreaMapper;
- import com.ydtech.modules.admin.dao.SysDeptMapper;
- import com.ydtech.modules.admin.model.SysArea;
- import com.ydtech.modules.admin.model.SysDept;
- import com.ydtech.modules.admin.model.vo.SysDeptRemoveVo;
- import com.ydtech.modules.admin.model.vo.SysDeptVo;
- import com.ydtech.modules.admin.model.vo.TreeSysDeptVO;
- import com.ydtech.modules.admin.service.SysAreaService;
- import com.ydtech.modules.admin.service.SysDeptService;
- import com.ydtech.modules.protocol.utils.AssertionUtils;
- import com.ydtech.utils.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.ObjectUtils;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- @Service
- public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
- implements SysDeptService {
- @Autowired
- private SysDeptMapper sysDeptMapper;
- @Autowired
- private SysAreaService sysAreaService;
- @Autowired
- private SysAreaMapper sysAreaMapper;
- @Override
- public List<SysDept> findTree(String deptId,String managementSource) {
- if(null == deptId || "".equals(deptId) || "undefined".equals(deptId)){
- deptId = "9";
- }
- SysDept sysDept = getById(deptId);
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0);
- lqw.likeRight(SysDept::getId, deptId);
- lqw.orderByAsc(SysDept::getOrderNum);
- List<SysDept> sysDeptList = list(lqw);
- //判断机构来源条件 进行遍历
- if (null != managementSource && !"undefined".equals(managementSource) && !"".equals(managementSource)) {
- lqw.eq(SysDept::getManagementSource, managementSource);
- //条件
- List<SysDept> conditionList = list(lqw);
- Map<String, List<SysDept>> conditionMap = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- Map<String, List<SysDept>> map = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- //先查第6层
- conditionList.forEach(i ->
- i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()))
- );
- List<SysDept> result = new ArrayList<>();
- //再查前4层
- int level = 5;
- while (level > 0) {
- List<SysDept> resultTmp = recursion(sysDeptList, conditionMap);
- if (resultTmp.size() == 0) {
- break;
- } else {
- result.clear();
- result.addAll(resultTmp);
- }
- conditionMap = result.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- level--;
- }
- sysDeptList = result;
- } else {
- Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- sysDeptList.forEach(i -> {
- i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()));
- i.setDeptCount(i.getChildren().size());
- });
- sysDeptList = map.get(sysDept.getParentId());
- }
- return sysDeptList;
- }
- private List<SysDept> recursion(List<SysDept> sysDeptList, Map<String, List<SysDept>> conditionMap) {
- List<SysDept> result = new ArrayList<>();
- sysDeptList.forEach(i ->
- conditionMap.forEach((key, value) -> {
- if (i.getId().equals(key)) {
- if (i.getChildren() == null) {
- i.setChildren(new ArrayList<>());
- }
- i.getChildren().addAll(value);
- result.add(i);
- }
- })
- );
- result.sort(Comparator.comparing(SysDept::getOrderNum));
- return result;
- }
- @Override
- public List<SysDept> getUserList(String deptId) {
- SysDept sysDept = getById(deptId);
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0);
- lqw.likeRight(SysDept::getId, deptId);
- List<SysDept> sysDeptList = list(lqw);
- Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- return map.get(sysDept.getParentId());
- }
- @Override
- @Transactional
- public void addDept(SysDeptVo sysDeptVo) {
- SysDept sysDept = new SysDept();
- BeanUtils.copyProperties(sysDeptVo, sysDept);
- String comlevel = ObjectUtils.isEmpty(sysDept.getComlevel()) ? "" : sysDept.getComlevel();
- String parentId = sysDept.getParentId();
- String number;
- if (comlevel.equals("2")) {
- AssertionUtils.isStringEmpty(sysDept.getProvince(), "二级机构份必填");
- number = sysDept.getProvince().substring(0, 2);
- } else if (comlevel.equals("3")) {
- AssertionUtils.isStringEmpty(sysDept.getCity(), "三级机构市必填");
- number = sysDept.getCity().substring(0, 4);
- } else if (comlevel.equals("4")) {
- AssertionUtils.isStringEmpty(sysDept.getArea(), "四级机构区必填");
- number = sysDept.getArea().substring(0, 6);
- } else {
- if (comlevel.equals("5")) {
- sysDeptVo.setDeptType("M");
- }
- String maxId = baseMapper.getMaxId(sysDept.getParentId());
- if (!maxId.equals("0")) {
- String substring = maxId.substring(sysDept.getParentId().length() + 1);
- int i = Integer.parseInt("1" + substring) + 1;
- number = String.valueOf(i);
- } else {
- number = "101";
- }
- }
- String substring = number.substring(number.length() - 2);
- String serialNumber;
- if ("M,D".contains(sysDeptVo.getDeptType())) {
- serialNumber = parentId + sysDeptVo.getDeptType() + substring;
- } else {
- serialNumber = parentId + substring;
- }
- sysDept.setId(serialNumber);
- SysDept dept = getById(sysDept.getId());
- if (!ObjectUtils.isEmpty(dept)) {
- throw new SystemException("机构编号为:" + dept.getId() + " " + dept.getName() + "已被占用");
- }
- SysDept parentDept = baseMapper.selectById(sysDept.getParentId());
- if (parentDept.getComlevel().equals("5")){
- sysDept.setManagementSource(parentDept.getManagementSource());
- }
- // 查询当前部门序号是否存在
- LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
- qw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
- qw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
- qw.eq(SysDept::getOrderNum, sysDept.getOrderNum()); // 排序号相同
- boolean exists = baseMapper.exists(qw);
- if (exists) {
- // 如果存在,查询该父级部门下排序号大于当前部门的部门列表
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
- lqw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
- lqw.ge(SysDept::getOrderNum, sysDept.getOrderNum()); // 排序号大于等于当前部门
- List<SysDept> sysDeptList = list(lqw);
- // 更新这些部门的排序号
- sysDeptList.forEach(i -> {
- i.setOrderNum(i.getOrderNum() + 1);
- updateById(i);
- });
- }
- boolean b = save(sysDept);
- AssertionUtils.isSucceed(b, "删除失败");
- }
- @Override
- @Transactional
- public HttpResult updateDept(SysDept record) {
- // 查询修改的序号是否存在
- LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
- qw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
- qw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
- qw.eq(SysDept::getOrderNum, record.getOrderNum()); // 排序号相同
- boolean exists = baseMapper.exists(qw);
- //不存在则直接修改
- if (!exists) {
- baseMapper.updateById(record);
- return HttpResult.ok();
- }
- //查询机构
- SysDept sysDept = getById(record.getId());
- //原本的序号
- Integer orderNum = sysDept.getOrderNum();
- //本次修改的序号
- Integer newOrderNum = record.getOrderNum();
- // 向上换位
- if (newOrderNum < orderNum) {
- // 查询该父级部门下排序号大于当前部门的部门列表
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
- lqw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
- lqw.ge(SysDept::getOrderNum, record.getOrderNum()); // 排序号大于等于当前部门
- List<SysDept> sysDeptList = list(lqw);
- sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum));
- for (SysDept dept : sysDeptList) {
- dept.setOrderNum(dept.getOrderNum() + 1);
- updateById(dept);
- if (dept.getOrderNum().equals(orderNum)) {
- break;
- }
- }
- baseMapper.updateById(record);
- }
- //向下换位
- if (newOrderNum > orderNum) {
- // 查询该父级部门下排序号小于当前部门的部门列表
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
- lqw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
- lqw.le(SysDept::getOrderNum, record.getOrderNum()); // 排序号大于等于当前部门
- List<SysDept> sysDeptList = list(lqw);
- sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum).reversed());
- for (SysDept dept : sysDeptList) {
- dept.setOrderNum(dept.getOrderNum() - 1);
- updateById(dept);
- if (dept.getOrderNum().equals(orderNum)) {
- break;
- }
- }
- baseMapper.updateById(record);
- }
- if (newOrderNum.equals(orderNum)) {
- baseMapper.updateById(record);
- }
- return HttpResult.ok();
- }
- @Override
- public void deleteDept(SysDeptRemoveVo sysDeptRemoveVo) {
- SysDept sysDept = getById(sysDeptRemoveVo.getDeptId());
- List<SysDept> list = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list();
- if (list.isEmpty()) {
- boolean b = removeById(sysDeptRemoveVo.getDeptId());
- AssertionUtils.isSucceed(b, "删除失败");
- } else {
- throw new SystemException(sysDept.getName() + "下还有其他隶属机构");
- }
- }
- @Override
- public PageResult<SysDept> findSubDepts(PageRequest pageRequest) {
- String id = pageRequest.getColumnFilterValue(pageRequest, "id");
- String managementSource = pageRequest.getColumnFilterValue(pageRequest, "managementSource");
- Page<SysDept> p = PageRequest.buildPageRequest(pageRequest);
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(StringUtils.isNotEmpty(id), SysDept::getParentId, id);
- lqw.eq(StringUtils.isNotEmpty(managementSource), SysDept::getManagementSource, managementSource);
- IPage<SysDept> pResult = page(p, lqw);
- //传入查询条件
- return PageResult.buildPageResult(pResult);
- }
- public String findMaxDeptId(String deptId) {
- if (deptId == null) return "";
- String result = "00";
- QueryWrapper<SysDept> qw = new QueryWrapper<>();
- qw.likeRight(StringUtils.isNotEmpty(deptId), "id", deptId + "D%");
- qw.select("IFNULL(MAX(id),0) maxid");
- Map<String, Object> m = getMap(qw);
- String maxId = m.get("maxid").toString();
- if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
- result = maxId.substring(deptId.length() + 1);
- }
- return result;
- }
- public String findMaxHouseId(String deptId) {
- if (deptId == null) {
- return "";
- }
- String result = "00";
- QueryWrapper<SysDept> qw = new QueryWrapper<>();
- qw.like(StringUtils.isNotEmpty(deptId), "id", deptId + "M%");
- qw.select("IFNULL(MAX(id),0) maxid");
- Map<String, Object> m = getMap(qw);
- String maxId = m.get("maxid").toString();
- if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
- result = maxId.substring(deptId.length() + 1);
- }
- return result;
- }
- //
- @Override
- public List<TreeSysDeptVO> selectListWhere(String where, Object[] paramValues) {
- String sql = "select t1.id, t1.name, t1.parent_id, t1.dept_code, t1.province, t1.city, t1.area, t1.comlevel from sys_dept t1 " +
- "where del_flag='0' " + where;
- QueryWrapper<SysDept> qw = new QueryWrapper<>();
- qw.select(sql);
- List<SysDept> sysDeptList = list(qw);
- List<TreeSysDeptVO> recordList = new ArrayList<TreeSysDeptVO>();
- sysDeptList.forEach(a -> {
- TreeSysDeptVO model = new TreeSysDeptVO();
- model.setId(a.getId());
- model.setName(a.getName());
- model.setParentId(a.getParentId());
- model.setDeptCode(a.getDeptCode());
- model.setProvince(a.getProvince());
- model.setCity(a.getCity());
- model.setArea(a.getArea());
- model.setComlevel(a.getComlevel());
- recordList.add(model);
- });
- return recordList;
- }
- /**
- * 获取机构树(不包含团队 dept_type=C)
- */
- @Override
- public List<SysDept> findCompanyTree(String deptId) {
- SysDept sysDept = getById(deptId);
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0);
- lqw.likeRight(SysDept::getId, deptId);
- lqw.and((wrapper)->{wrapper.eq(SysDept::getDeptType,'C').or().eq(SysDept::getId,'9');});
- List<SysDept> sysDeptList = list(lqw);
- Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
- sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
- return map.get(sysDept.getParentId());
- }
- /**
- * 根据传入机构,查询归属所有团队
- */
- @Override
- public List<SysDept> findTeamList(String deptId) {
- SysDept sysDept = getById(deptId);
- LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
- lqw.eq(SysDept::getDelFlag, 0);
- lqw.likeRight(SysDept::getId, deptId);
- lqw.eq(SysDept::getDeptType,'D');
- lqw.apply(" exists (SELECT 1 FROM `sys_dept` b WHERE sys_dept.parent_id = b.`id` AND b.`comlevel`='5') ");
- List<SysDept> sysDeptList = list(lqw);
- return sysDeptList;
- }
- /**
- * @version
- * @author: hxl
- * @Date: 2024/4/28 10:24
- * @Description: 查询省、市、县 、门店的列表
- */
- @Override
- public List<SysDept> queryListByParentId(String level, String parentId) {
- return sysDeptMapper.queryListByParentId(level,parentId);
- }
- @Override
- public String queryBelongArea(String deptId){
- //长度大于8的话 进行切割
- StringBuilder result = new StringBuilder();
- if(deptId.length() >= 8)
- {
- String area = deptId.substring(2,8);
- SysArea sysArea = sysAreaMapper.selectOne(new LambdaQueryWrapper<SysArea>().eq(SysArea::getAreaCode, area));
- if(sysArea == null){
- return "";
- }
- List<SysArea> sysAreas = sysAreaService.queryAllParent(sysArea);
- result.append(sysAreas.get(2).getAreaCname());
- result.append(sysAreas.get(1).getAreaCname());
- result.append(sysAreas.get(0).getAreaCname());
- }
- return result.toString();
- }
- }
|