package com.huaxu.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.huaxu.common.ToolUtil; import com.huaxu.dao.OrgMapper; import com.huaxu.dao.UserMapper; import com.huaxu.dto.UserListDto; import com.huaxu.entity.Org; import com.huaxu.entity.UserEntity; import com.huaxu.entity.UserRoleEntity; import com.huaxu.entity.UserTagEntity; import com.huaxu.model.LoginUser; import com.huaxu.model.ProgramItem; import com.huaxu.util.ByteArrayUtils; import com.huaxu.util.RedisUtil; import com.huaxu.util.UserUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import javax.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; /** * * 用户Service接口 * @author: WYY * @date 2020-10-22 17:35 */ @Service public class UserService extends ServiceImpl { @Resource private UserMapper userMapper; @Resource private UserTagService userTagService; @Resource private UserRoleService userRoleService; @Resource private OrgMapper orgMapper; @Autowired private RedisUtil redisUtil; /** * 自定义分页查询,含关联实体对像 */ public IPage findPage(IPage page, UserEntity userEntity) { LoginUser currentUser = UserUtil.getCurrentUser(); userEntity.setTenantId(currentUser.getTenantId()); userEntity.setProgramItems(currentUser.getProgramItemList()); userEntity.setUserType(currentUser.getType()); //1是公司,2是公司及以下,3部门,4部门及以下,5自定义 userEntity.setPermissonType(currentUser.getPermissonType()); Page userPage = userMapper.findPage(page, userEntity); return userPage; } /** * 查列表 */ public List findList(UserEntity userEntity) { LoginUser currentUser = UserUtil.getCurrentUser(); userEntity.setTenantId(currentUser.getTenantId()); return userMapper.findList(userEntity); } /** * 批量删除 */ @Transactional(rollbackFor = Exception.class) public boolean delUserByIds(Long[] ids) { for (Long id : ids) { UserEntity userEntity = findUserById(id); if (userEntity != null) { //將禁用的角色放入redis,作为登出判断 if (userEntity.getEnableState().equals("0")) { String roleKey = "disableUser:" + id; redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期 } if (userEntity.getEnableState().equals("1")) { String roleKey = "disableUser:" + id; redisUtil.del(roleKey.getBytes()); } } } return this.removeByIds(Arrays.asList(ids)); } public boolean checkMobileUnique(String phone) { LoginUser currentUser = UserUtil.getCurrentUser(); int count = this.count(new QueryWrapper().eq("phone", phone).eq("tenant_id",currentUser.getTenantId())); if (count > 0) { return true; } return false; } public boolean checkMobileUnique(UserEntity user) { LoginUser currentUser = UserUtil.getCurrentUser(); Long userId = ToolUtil.isEmpty(user.getId()) ? -1L : user.getId(); UserEntity info = this.getOne(new QueryWrapper().eq("phone", user.getPhone()).eq("tenant_id",currentUser.getTenantId())); if (ToolUtil.isNotEmpty(info) && !info.getId().equals(userId)) { return true; } return false; } /** * 单个删除 */ public boolean delUserById(Long id) { UserEntity userEntity = findUserById(id); if (userEntity != null) { //將禁用的角色放入redis,作为登出判断 if (userEntity.getEnableState().equals("0")) { String roleKey = "disableUser:" + id; redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期 } if (userEntity.getEnableState().equals("1")) { String roleKey = "disableUser:" + id; redisUtil.del(roleKey.getBytes()); } } return this.removeById(id); } /** * 保存 */ public boolean addUser(UserEntity user) { if (this.save(user)) { Long pkId = user.getId(); //添加用户标签 if (user.getUserTags() != null) { for (Long item : user.getUserTags()) { UserTagEntity userTagEntity = new UserTagEntity(); userTagEntity.setCreateBy(user.getCreateBy()); userTagEntity.setUpdateBy(user.getUpdateBy()); userTagEntity.setDateCreate(user.getDateCreate()); userTagEntity.setDateUpdate(user.getDateUpdate()); userTagEntity.setStatus(1); userTagEntity.setTagId(Long.valueOf(item)); userTagEntity.setUserId(pkId); userTagService.addUserTag(userTagEntity); } } //添加角色 if (user.getRoleId() != null) { UserRoleEntity userRoleEntity = new UserRoleEntity(); userRoleEntity.setCreateBy(user.getCreateBy()); userRoleEntity.setUpdateBy(user.getUpdateBy()); userRoleEntity.setDateCreate(user.getDateCreate()); userRoleEntity.setDateUpdate(user.getDateUpdate()); userRoleEntity.setStatus(1); userRoleEntity.setRoleId(Long.valueOf(user.getRoleId())); userRoleEntity.setUserId(pkId); userRoleService.addUserRole(userRoleEntity); } return true; } return false; } /** * 修改根居ID */ public boolean updateUserById(UserEntity user) { if (this.updateById(user)) { UserEntity userEntity = findUserById(user.getId()); if (userEntity != null) { //將禁用的角色放入redis,作为登出判断 if (userEntity.getEnableState().equals("0")) { String roleKey = "disableUser:" + user.getId(); redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期 } if (userEntity.getEnableState().equals("1")) { String roleKey = "disableUser:" + user.getId(); redisUtil.del(roleKey.getBytes()); } } //更新关联附件信息 Long pkId = user.getId(); //添加用户标签 if (user.getUserTags() != null && user.getUserTags().size() > 0) { //先删除之前的标签 List userTagEntitys = new ArrayList<>(); UserTagEntity userTagDelete = new UserTagEntity(); userTagDelete.setUserId(pkId); userTagEntitys = userTagService.findList(userTagDelete); Long[] ids = new Long[userTagEntitys.size()]; for (int i = 0; i < userTagEntitys.size(); i++) { ids[i] = userTagEntitys.get(i).getId(); } userTagService.delUserTagByIds(ids); //新增标签 for (Long item : user.getUserTags()) { UserTagEntity userTagEntity = new UserTagEntity(); userTagEntity.setCreateBy(user.getCreateBy()); userTagEntity.setUpdateBy(user.getUpdateBy()); userTagEntity.setDateCreate(user.getDateCreate()); userTagEntity.setDateUpdate(user.getDateUpdate()); userTagEntity.setStatus(1); userTagEntity.setTagId(Long.valueOf(item)); userTagEntity.setUserId(pkId); userTagService.addUserTag(userTagEntity); } } //修改角色 if (user.getRoleId() != null) { List userRoleEntities = new ArrayList<>(); UserRoleEntity userRoleEntity = new UserRoleEntity(); userRoleEntity.setUserId(pkId); userRoleEntities = userRoleService.findList(userRoleEntity); if (userRoleEntities.size() > 0) { Long userRoleId = userRoleEntities.get(0).getId(); userRoleService.delUserRoleById(userRoleId); } //新增角色 UserRoleEntity userRole = new UserRoleEntity(); userRole.setCreateBy(user.getCreateBy()); userRole.setUpdateBy(user.getUpdateBy()); userRole.setDateCreate(user.getDateCreate()); userRole.setDateUpdate(user.getDateUpdate()); userRole.setStatus(1); userRole.setRoleId(Long.valueOf(user.getRoleId())); userRole.setUserId(pkId); userRoleService.addUserRole(userRole); } return true; } return false; } /** * 根居ID获取对象 */ public UserEntity findUserById(Long id) { List userTagEntitys = new ArrayList<>(); UserTagEntity userTags = new UserTagEntity(); userTags.setUserId(id); userTagEntitys = userTagService.findList(userTags); List ids = new ArrayList<>(); for (int i = 0; i < userTagEntitys.size(); i++) { ids.add(userTagEntitys.get(i).getTagId()); } UserEntity userEntity = userMapper.findUserById(id); userEntity.setUserTags(ids); return userEntity; } public List getUserIdsByOrgId(List orgIds) { ListuserIds=new ArrayList<>(); if(orgIds.size()>0){ userIds=userMapper.getUserIdsByOrgId(orgIds); } return userIds; } public List findUserIdsByOrgId(List orgIds) { List userEntities = new ArrayList<>(); if(orgIds.size()>0){ userEntities=userMapper.findUserIdsByOrgId(orgIds); } return userEntities; } public List findUserList() { List userListDtos = new ArrayList<>(); LoginUser currentUser = UserUtil.getCurrentUser(); UserEntity userEntity=new UserEntity(); userEntity.setTenantId(currentUser.getTenantId()); userEntity.setProgramItems(currentUser.getProgramItemList()); userEntity.setUserType(currentUser.getType()); //1是公司,2是公司及以下,3部门,4部门及以下,5自定义 userEntity.setPermissonType(currentUser.getPermissonType()); ListuserEntities=userMapper.findUserList(userEntity); ListcompanyUser=new ArrayList<>(); ListdepartmentUser=new ArrayList<>(); userEntities.forEach(user->{ if(user.getDeptOrgId()!=null){ departmentUser.add(user); }else{ companyUser.add(user); } }); Map> companyGroup = companyUser.stream() .collect(Collectors.groupingBy(d -> d.getCompanyOrgId())); Map> departMentGroup = departmentUser.stream() .collect(Collectors.groupingBy(d -> d.getDeptOrgId())); Iterator> companyIterator = companyGroup.values().iterator(); Iterator> departmentIterator = departMentGroup.values().iterator(); while(companyIterator.hasNext()){ List users = companyIterator.next(); UserListDto userListDto=new UserListDto(); UserEntity user = users.get(0); userListDto.setLabel(user.getCompanyOrgName()); userListDto.setId(user.getCompanyOrgId()); userListDto.setValue(user.getCompanyOrgName()); userListDto.setChildren(setChildrens(users)); userListDtos.add(userListDto); } while(departmentIterator.hasNext()){ List users = departmentIterator.next(); UserListDto userListDto=new UserListDto(); UserEntity user = users.get(0); userListDto.setLabel(user.getDeptOrgName()); userListDto.setId(user.getDeptOrgId()); userListDto.setValue(user.getDeptOrgName()); userListDto.setChildren(setChildrens(users)); userListDtos.add(userListDto); } return userListDtos; } private List setChildrens(List users){ List childrens=new ArrayList<>(); users.forEach(u->{ UserListDto children=new UserListDto(); children.setLabel(u.getUsername()); children.setId(u.getId()); children.setValue(u.getUsername()); childrens.add(children); }); return childrens; } public List findUserIdsByUserIds(List userIds) { List userEntities = new ArrayList<>(); if(userIds.size()>0){ userEntities=userMapper.findUserIdsByUserIds(userIds); } return userEntities; } public List findUserIdsForOrg() { LoginUser currentUser = UserUtil.getCurrentUser(); Integer compayId = currentUser.getCompanyId(); Integer deptId = currentUser.getDepartmentId(); List list = userMapper.findUserIdsForOrg(compayId,deptId); return list; } public List findUserIdsByPermissonOrg(String tenantId,Integer companyOrgId,Integer deptOrgId){ List userIds = new ArrayList<>(); //1是公司,2是公司及以下,3部门,4部门及以下,5自定义 if(companyOrgId == null){ return userIds; } //查询租户下所有公司部门 List orgs = orgMapper.findOrgByTenant(tenantId); Map orgMap = orgs.stream().collect(Collectors.toMap(Org::getId, a -> a, (k1, k2) -> k1)); List companyOrgIds = new ArrayList<>(); List deptOrgIds = new ArrayList<>(); Integer deptOrgParentId =deptOrgId; if(deptOrgId != null){ //权限类型为4部门及以下 while(true){ Org org = orgMap.get(deptOrgParentId); if(org == null ){ break; } if(org.getOrgType().equals("company") ){ break; } deptOrgIds.add(deptOrgParentId); deptOrgParentId = org.getParentOrgId(); } } //权限类型为2是公司及以下 Integer companyOrgParentId = companyOrgId ; while(true){ Org org = orgMap.get(companyOrgParentId); if(org == null ){ break; } companyOrgIds.add(companyOrgParentId); companyOrgParentId = org.getParentOrgId(); if(org.getParentOrgId() == 0){ break; } } //查询客户id if(companyOrgId != null && companyOrgIds.size() > 0){ userIds = userMapper.findUserIdsByPermissonOrg(tenantId,companyOrgId,deptOrgId,companyOrgIds,deptOrgIds); } return userIds; } }