ZQN
2024-06-24 b4b2b63345961884c8a9dd95ca1f32c0ea1123e5
project-system/src/main/java/com/project/system/service/impl/SysUserDeptServiceImpl.java
@@ -1,12 +1,25 @@
package com.project.system.service.impl;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.core.domain.entity.SysDept;
import com.project.common.exception.base.BaseException;
import com.project.common.utils.StringUtils;
import com.project.system.domain.SysUserDept;
import com.project.system.domain.bo.editBo.UserDeptBo;
import com.project.system.domain.vo.SysUserDeptTreeVo;
import com.project.system.domain.vo.UserDeptVo;
import com.project.system.mapper.SysUserDeptMapper;
import com.project.system.service.ISysDeptService;
import com.project.system.service.ISysUserDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 用户与部门关联Service业务层处理
@@ -17,5 +30,75 @@
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept> implements ISysUserDeptService {
    private final ISysDeptService deptService;
    /**
     * 用户多机构回显
     * @param userId    用户id
     * @return  内容
     */
    @Override
    public UserDeptVo batchUserDeptView(Long userId)
    {
        List<SysUserDept> list = this.list(lq().eq(SysUserDept::getUserId, userId));
        List<Long> deptIds = list.stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
        UserDeptVo vo = new UserDeptVo();
        vo.setUserId(userId);
        vo.setDeptIds(deptIds);
        return vo;
    }
    /**
     * 用户多机构保存
     * @param bo    参数
     * @return  结果
     */
    @Override
    @Transactional
    public Boolean batchUserDeptSave(UserDeptBo bo)
    {
        Long userId = bo.getUserId();
        if (userId==null || StringUtils.isEmpty(bo.getDeptIds())){
            throw new BaseException("参数有误!");
        }
        this.remove(lq().eq(SysUserDept::getUserId, userId));
        List<SysUserDept> saveList = new ArrayList<>();
        for (Long deptId : bo.getDeptIds()) {
            saveList.add(new SysUserDept().setUserId(userId).setDeptId(deptId));
        }
        return this.saveBatch(saveList);
    }
    /**
     * 用户多机构树
     * @param deptId    参数
     * @return  结果
     */
    @Override
    public SysUserDeptTreeVo batchUserDeptTree(Long deptId)
    {
        Long checkDeptIdByLoginDeptId = deptService.getCheckDeptIdByLoginDeptId(deptId);
        return getDeptTree(checkDeptIdByLoginDeptId);
    }
    /**
     * 获取审批及其下级部门
     * @param deptId    审批部门id
     * @return  机构树
     */
    private SysUserDeptTreeVo getDeptTree(Long deptId)
    {
        SysUserDeptTreeVo vo = new SysUserDeptTreeVo();
        SysDept sysDept = deptService.selectDeptById(deptId);
        vo.setDeptId(sysDept.getDeptId());
        vo.setDeptName(sysDept.getDeptName());
        List<SysDept> list = deptService.list(deptService.lq().eq(SysDept::getParentId, deptId));
        List<SysUserDeptTreeVo> child = Convert.toList(SysUserDeptTreeVo.class, list);
        vo.setChild(child);
        return vo;
    }
}