package com.ycl.service.user.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.api.BasePageDTO;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.user.*;
import com.ycl.mapper.user.AdminMenuRelationMapper;
import com.ycl.mapper.user.UmsAdminRoleRelationMapper;
import com.ycl.mapper.user.UmsDepartManageMapper;
import com.ycl.mapper.user.UmsRoleMenuRelationMapper;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.user.UmsAdminService;
import com.ycl.service.user.UmsDepartManageService;
import com.ycl.vo.depart.DepartVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author lyq
* @since 2022-09-09
*/
@Service
public class UmsDepartManageServiceImpl extends ServiceImpl implements UmsDepartManageService {
@Resource
private UmsDepartManageMapper umsDepartManageMapper;
@Resource
private UmsDepartService sccgDepartService;
@Resource
private UmsAdminService umsAdminService;
@Resource
UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;
@Resource
AdminMenuRelationMapper adminMenuRelationMapper;
@Resource
UmsRoleMenuRelationMapper roleMenuRelationMapper;
@Override
public List queryByDepartId(Long departId) {
List umsAdminDeparts = umsDepartManageMapper.selectList(new LambdaQueryWrapper()
.eq(UmsDepartManage::getDepartId, departId));
if (CollUtil.isNotEmpty(umsAdminDeparts)) {
return umsAdminDeparts;
}
return null;
}
@Override
public List queryByUserId(long userId) {
List umsAdminDeparts = umsDepartManageMapper.selectList(new LambdaQueryWrapper().eq(UmsDepartManage::getUserId, userId));
if (CollUtil.isNotEmpty(umsAdminDeparts)) {
return umsAdminDeparts;
}
return null;
}
@Override
public void deletedByDepartId(long departId) {
umsDepartManageMapper.deletedByDepartId(departId);
}
@Override
public BasePageDTO belongDepart(long userId, int current, int pageSize) {
BasePageDTO basePageDTO = new BasePageDTO();
Long count = umsDepartManageMapper.selectCount(new LambdaQueryWrapper().eq(UmsDepartManage::getUserId, userId));
basePageDTO.setTotal(count);
if (count > 0) {
current = (current - 1) * pageSize;
List departList = umsDepartManageMapper.selectPageByUserId(userId, current, pageSize);
List adminDepartInfoVOS = new ArrayList<>();
DepartVO.AdminDepartInfoVO adminDepartInfoVO = null;
for (UmsDepartManage umsAdminDepart : departList) {
adminDepartInfoVO = new DepartVO.AdminDepartInfoVO();
UmsDepart sccgDepart = sccgDepartService.loadDepartById(umsAdminDepart.getDepartId());
UmsAdmin umsAdmin = umsAdminService.getById(userId);
adminDepartInfoVO.setDepartId(umsAdminDepart.getDepartId());
adminDepartInfoVO.setDepartName(sccgDepart.getDepartName());
adminDepartInfoVO.setDepartDes(sccgDepart.getDepartDes());
adminDepartInfoVO.setDepartType(sccgDepart.getDepartType());
adminDepartInfoVO.setUserId(userId);
adminDepartInfoVO.setUserName(umsAdmin.getUsername());
adminDepartInfoVOS.add(adminDepartInfoVO);
}
basePageDTO.setRecords(adminDepartInfoVOS);
}
return basePageDTO;
}
@Override
public void userUpdateRoles(Long id, List roles) {
//解除用户角色绑定
umsAdminRoleRelationMapper.delete(new LambdaQueryWrapper().eq(UmsAdminRoleRelation::getAdminId, id));
//解除用户绑定角色绑定菜单
adminMenuRelationMapper.delete(new LambdaQueryWrapper().eq(AdminMenuRelation::getAdminId, id));
//绑定角色于用户
if (!roles.isEmpty()) {
for (Long role : roles) {
UmsAdminRoleRelation urr = new UmsAdminRoleRelation();
urr.setAdminId(id);
urr.setRoleId(role);
umsAdminRoleRelationMapper.insert(urr);
}
}
//绑定菜单于用户
ArrayList umsRoleMenuRelations = new ArrayList<>();
for (Long role : roles) {
umsRoleMenuRelations.addAll(roleMenuRelationMapper
.selectList(new LambdaQueryWrapper()
.eq(UmsRoleMenuRelation::getRoleId, role)));
}
umsRoleMenuRelations
.stream()
.map(item -> item.getMenuId())
.collect(Collectors.toList())
.stream()
.distinct()
.collect(Collectors.toList())
.stream()
.forEach(item -> {
AdminMenuRelation adminMenuRelation = new AdminMenuRelation();
adminMenuRelation.setAdminId(id);
adminMenuRelation.setMenuId(item);
adminMenuRelationMapper.insert(adminMenuRelation);
});
}
@Override
@Transactional(rollbackFor = Exception.class)
public void userUpdateDepart(Long userId,Long departId) {
umsDepartManageMapper.delete(new LambdaQueryWrapper().eq(UmsDepartManage::getUserId,userId));
UmsDepartManage umsDepartManage = new UmsDepartManage();
umsDepartManage.setUserId(userId);
umsDepartManage.setCreateTime(new Date());
umsDepartManage.setDepartId(departId);
umsDepartManageMapper.insert(umsDepartManage);
}
}