dataease-dm/backend/src/main/java/io/dataease/service/sys/SysUserService.java

239 lines
8.8 KiB
Java
Raw Normal View History

2021-02-24 15:44:26 +08:00
package io.dataease.service.sys;
2021-05-13 16:00:15 +08:00
import io.dataease.auth.api.dto.CurrentUserDto;
2021-02-24 15:44:26 +08:00
import io.dataease.base.domain.SysUser;
import io.dataease.base.domain.SysUserExample;
import io.dataease.base.domain.SysUsersRolesExample;
import io.dataease.base.domain.SysUsersRolesKey;
import io.dataease.base.mapper.SysUserMapper;
import io.dataease.base.mapper.SysUsersRolesMapper;
import io.dataease.base.mapper.ext.ExtSysUserMapper;
2021-03-11 22:58:05 +08:00
import io.dataease.base.mapper.ext.query.GridExample;
import io.dataease.commons.constants.AuthConstants;
2021-05-13 16:00:15 +08:00
import io.dataease.commons.utils.AuthUtils;
2021-02-24 15:44:26 +08:00
import io.dataease.commons.utils.BeanUtils;
import io.dataease.commons.utils.CodingUtil;
2021-03-11 22:58:05 +08:00
import io.dataease.controller.sys.base.BaseGridRequest;
2021-02-24 15:44:26 +08:00
import io.dataease.controller.sys.request.SysUserCreateRequest;
2021-03-08 18:19:57 +08:00
import io.dataease.controller.sys.request.SysUserPwdRequest;
2021-03-03 17:38:41 +08:00
import io.dataease.controller.sys.request.SysUserStateRequest;
2021-02-24 15:44:26 +08:00
import io.dataease.controller.sys.response.SysUserGridResponse;
2021-02-24 22:04:05 +08:00
import io.dataease.controller.sys.response.SysUserRole;
import org.apache.commons.collections4.CollectionUtils;
2021-02-24 15:44:26 +08:00
import org.apache.commons.lang3.ObjectUtils;
2021-02-24 18:52:09 +08:00
import org.apache.commons.lang3.StringUtils;
2021-04-28 12:16:07 +08:00
import org.springframework.beans.factory.annotation.Value;
2021-03-08 18:19:57 +08:00
import org.springframework.cache.annotation.CacheEvict;
2021-02-24 15:44:26 +08:00
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
2021-02-24 15:44:26 +08:00
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class SysUserService {
2021-04-28 12:16:07 +08:00
//private final static String DEFAULT_PWD = "DataEase123..";
@Value("${dataease.init_password:DataEase123..}")
private String DEFAULT_PWD;
2021-02-24 15:44:26 +08:00
@Resource
private SysUserMapper sysUserMapper;
@Resource
private SysUsersRolesMapper sysUsersRolesMapper;
@Resource
private ExtSysUserMapper extSysUserMapper;
2021-03-11 22:58:05 +08:00
public List<SysUserGridResponse> query(BaseGridRequest request) {
2021-05-27 14:45:20 +08:00
List<SysUser> sysUsers = sysUserMapper.selectByExample(new SysUserExample());
List<SysUserGridResponse> lists = sysUsers.stream().map(ele -> {
SysUserGridResponse response = new SysUserGridResponse();
BeanUtils.copyBean(response, ele);
return response;
}).collect(Collectors.toList());
2021-03-11 22:58:05 +08:00
GridExample gridExample = request.convertExample();
2021-05-27 14:45:20 +08:00
List<SysUserGridResponse> query = extSysUserMapper.query(gridExample);
2021-02-24 22:04:05 +08:00
lists.forEach(item -> {
2021-05-27 14:45:20 +08:00
for (SysUserGridResponse response : query) {
if (item.getUserId().equals(response.getUserId())) {
item.setId(response.getId());
List<SysUserRole> roles = response.getRoles();
item.setRoles(roles);
List<Long> roleIds = roles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
item.setRoleIds(roleIds);
item.setDept(response.getDept());
}
}
// List<SysUserRole> roles = item.getRoles();
// List<Long> roleIds = roles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
// item.setRoleIds(roleIds);
2021-02-24 22:04:05 +08:00
});
return lists;
2021-02-24 15:44:26 +08:00
}
@Transactional
public int save(SysUserCreateRequest request) {
2021-02-24 15:44:26 +08:00
SysUser user = BeanUtils.copyBean(new SysUser(), request);
long now = System.currentTimeMillis();
user.setCreateTime(now);
user.setUpdateTime(now);
user.setIsAdmin(false);
if (ObjectUtils.isEmpty(user.getPassword()) || StringUtils.equals(user.getPassword(), DEFAULT_PWD)) {
2021-02-24 18:52:09 +08:00
user.setPassword(CodingUtil.md5(DEFAULT_PWD));
} else {
2021-02-24 18:52:09 +08:00
user.setPassword(CodingUtil.md5(user.getPassword()));
}
if (StringUtils.isEmpty(user.getLanguage())) {
2021-05-21 17:33:47 +08:00
user.setLanguage("zh_CN");
}
2021-02-24 15:44:26 +08:00
int insert = sysUserMapper.insert(user);
SysUser dbUser = findOne(user);
saveUserRoles(dbUser.getUserId(), request.getRoleIds());//插入用户角色关联
return insert;
}
/**
* 修改用户密码清楚缓存
*
* @param request
* @return
*/
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #request.userId")
2021-02-24 15:44:26 +08:00
@Transactional
public int update(SysUserCreateRequest request) {
if (StringUtils.isEmpty(request.getPassword())) {
request.setPassword(null);
}
2021-02-24 15:44:26 +08:00
SysUser user = BeanUtils.copyBean(new SysUser(), request);
long now = System.currentTimeMillis();
user.setUpdateTime(now);
deleteUserRoles(user.getUserId());//先删除用户角色关联
saveUserRoles(user.getUserId(), request.getRoleIds());//再插入角色关联
return sysUserMapper.updateByPrimaryKeySelective(user);
2021-05-13 16:00:15 +08:00
}
/**
* 用户修改个人信息
*
2021-05-13 16:00:15 +08:00
* @param request
* @return
*/
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #request.userId")
@Transactional
public int updatePersonInfo(SysUserCreateRequest request) {
2021-05-13 16:00:15 +08:00
SysUser user = BeanUtils.copyBean(new SysUser(), request);
long now = System.currentTimeMillis();
user.setUpdateTime(now);
return sysUserMapper.updateByPrimaryKeySelective(user);
2021-02-24 15:44:26 +08:00
}
2021-03-03 17:38:41 +08:00
2021-04-28 12:16:07 +08:00
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #request.userId")
public int updateStatus(SysUserStateRequest request) {
2021-03-03 17:38:41 +08:00
SysUser sysUser = new SysUser();
sysUser.setUserId(request.getUserId());
sysUser.setEnabled(request.getEnabled());
return sysUserMapper.updateByPrimaryKeySelective(sysUser);
}
2021-03-08 18:19:57 +08:00
/**
* 修改用户密码清楚缓存
*
2021-03-08 18:19:57 +08:00
* @param request
* @return
*/
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #request.userId")
2021-03-08 18:19:57 +08:00
public int updatePwd(SysUserPwdRequest request) {
2021-05-13 16:00:15 +08:00
CurrentUserDto user = AuthUtils.getUser();
2021-03-08 18:19:57 +08:00
if (ObjectUtils.isEmpty(user)) {
throw new RuntimeException("用户不存在");
}
if (!StringUtils.equals(CodingUtil.md5(request.getPassword()), user.getPassword())) {
2021-03-08 18:19:57 +08:00
throw new RuntimeException("密码错误");
}
SysUser sysUser = new SysUser();
2021-05-13 16:00:15 +08:00
sysUser.setUserId(user.getUserId());
2021-03-08 18:19:57 +08:00
sysUser.setPassword(CodingUtil.md5(request.getNewPassword()));
return sysUserMapper.updateByPrimaryKeySelective(sysUser);
}
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #request.userId")
public int adminUpdatePwd(SysUserPwdRequest request) {
2021-03-09 15:38:36 +08:00
SysUser sysUser = new SysUser();
sysUser.setUserId(request.getUserId());
sysUser.setPassword(CodingUtil.md5(request.getNewPassword()));
return sysUserMapper.updateByPrimaryKeySelective(sysUser);
}
2021-03-08 18:19:57 +08:00
2021-02-24 15:44:26 +08:00
/**
* 删除用户角色关联
*
2021-02-24 15:44:26 +08:00
* @param userId
* @return
*/
private int deleteUserRoles(Long userId) {
2021-02-24 15:44:26 +08:00
SysUsersRolesExample example = new SysUsersRolesExample();
example.createCriteria().andUserIdEqualTo(userId);
return sysUsersRolesMapper.deleteByExample(example);
}
/**
* 保存用户角色关联
*
2021-02-24 15:44:26 +08:00
* @param userId
* @param roleIds
*/
private void saveUserRoles(Long userId, List<Long> roleIds) {
2021-02-24 15:44:26 +08:00
roleIds.forEach(roleId -> {
SysUsersRolesKey sysUsersRolesKey = new SysUsersRolesKey();
sysUsersRolesKey.setUserId(userId);
sysUsersRolesKey.setRoleId(roleId);
sysUsersRolesMapper.insert(sysUsersRolesKey);
});
}
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #userId")
2021-02-24 15:44:26 +08:00
@Transactional
public int delete(Long userId) {
2021-02-24 15:44:26 +08:00
deleteUserRoles(userId);
return sysUserMapper.deleteByPrimaryKey(userId);
}
public SysUser findOne(SysUser user) {
2021-02-24 15:44:26 +08:00
if (ObjectUtils.isEmpty(user)) return null;
if (ObjectUtils.isNotEmpty(user.getUserId())) {
2021-02-24 15:44:26 +08:00
return sysUserMapper.selectByPrimaryKey(user.getUserId());
}
SysUserExample example = new SysUserExample();
SysUserExample.Criteria criteria = example.createCriteria();
if (ObjectUtils.isNotEmpty(user.getUsername())) {
2021-02-24 15:44:26 +08:00
criteria.andUsernameEqualTo(user.getUsername());
List<SysUser> sysUsers = sysUserMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(sysUsers)) return sysUsers.get(0);
2021-02-24 15:44:26 +08:00
}
return null;
}
2021-02-26 12:03:01 +08:00
public List<SysUser> users(List<Long> userIds) {
2021-02-26 12:03:01 +08:00
return userIds.stream().map(sysUserMapper::selectByPrimaryKey).collect(Collectors.toList());
}
2021-05-18 15:46:14 +08:00
@CacheEvict(value = AuthConstants.USER_CACHE_NAME, key = "'user' + #userId")
public void setLanguage(Long userId, String language) {
2021-05-18 15:46:14 +08:00
SysUser sysUser = new SysUser();
sysUser.setUserId(userId);
sysUser.setLanguage(language);
sysUserMapper.updateByPrimaryKeySelective(sysUser);
}
2021-02-24 15:44:26 +08:00
}