Commit f679fe44 by huangjinxin

fix:用户列表获取,参数校验

1 parent 89e937fd
......@@ -117,7 +117,11 @@ public enum StatusCodeEnum {
GIS_AREA_SMALL("005", "对象操作后面积过小", false),
GIS_AREA_TOPOLOGY("006", "拓扑处理出错", false)
GIS_AREA_TOPOLOGY("006", "拓扑处理出错", false),
USER_PHONE_ERROR("019", "手机号格式有误", false),
USER_EMAIL_ERROR("020", "邮箱格式有误", false)
;
/**
......
......@@ -21,5 +21,13 @@ public interface UserOrgDao extends JpaRepository<UserOrgEntity, String>, JpaSpe
*/
List<UserOrgEntity> findByUserId(String userId);
/**
* 根据用户id查询
*
* @param orgId
* @return
*/
List<UserOrgEntity> findByOrgId(String orgId);
void deleteByUserId(String userId);
}
......@@ -34,6 +34,14 @@ public interface UserRoleDao extends JpaRepository<UserRoleEntity, String>,
List<UserRoleEntity> findByUserId(String userId);
/**
* 根据用户ID查询
*
* @param userId 用户ID
* @return
*/
List<UserRoleEntity> findByUserIdIn(List<String> userId);
/**
* 根据角色ID和用户ID删除
*
* @param roleId 角色ID
......
......@@ -554,7 +554,7 @@ public class UserService {
if (StringUtils.isNotBlank(userInfo.getPhone())) {
// 校验手机格式是否正确
if (!Validator.isMobile(userInfo.getPhone())) {
return Result.failed(StatusCodeEnum.COMMON_PARAM_ERROR, null);
return Result.failed(StatusCodeEnum.USER_PHONE_ERROR, null);
// throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkPhone(userInfo.getPhone(), userInfo.getId())) {
......@@ -566,7 +566,7 @@ public class UserService {
if (StringUtils.isNotBlank(userInfo.getEmail())) {
// 校验邮箱格式是否正确
if (!Validator.isEmail(userInfo.getEmail())) {
return Result.failed(StatusCodeEnum.COMMON_PARAM_ERROR, null);
return Result.failed(StatusCodeEnum.USER_EMAIL_ERROR, null);
// throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkEmail(userInfo.getEmail(), userInfo.getId())) {
......@@ -721,20 +721,38 @@ public class UserService {
entityManager.clear();
PageResult<UserInfoSearch> pageResult = new PageResult<>();
String count = "select count(*) ";
String select = "SELECT su.*,sr.name roleName ,sr.id roleId ";
String select = "SELECT su.* ";
String limit = " ORDER BY su.CREATED_TIME DESC limit " + (search.getPage() - 1) * search.getSize() + "," + search.getSize();
StringBuffer querySql = new StringBuffer("from sys_user su left join sys_user_role sur on su.id =sur.user_id left JOIN sys_role sr on sr.id =sur.role_id left join sys_user_org suo on su.id=suo.user_id where 1=1 ");
// StringBuffer querySql = new StringBuffer("from sys_user su left join sys_user_role sur on su.id =sur.user_id left JOIN sys_role sr on sr.id =sur.role_id left join sys_user_org suo on su.id=suo.user_id where 1=1 ");
StringBuffer querySql = new StringBuffer("from sys_user su where 1=1 ");
RoleEntity roleEntity = null;
List<UserRoleEntity> byRoleId = null;
if (StringUtils.isNotBlank(search.getRoleId())) {
querySql.append("and sr.id ='" + search.getRoleId() + "'");
roleEntity = roleDao.findById(search.getRoleId()).orElse(null);
if (ObjectUtil.isNull(roleEntity)) {
return pageResult;
}
byRoleId = userRoleDao.findByRoleId(search.getRoleId());
if (CollectionUtils.isEmpty(byRoleId)) {
return pageResult;
}
List<String> collect = byRoleId.stream().map(UserRoleEntity::getUserId).collect(Collectors.toList());
querySql.append(" and su.id in ('" + String.join("','", collect) + "') ");
}
if (null != search.getSource()) {
querySql.append("and su.source =" + search.getSource() + " ");
querySql.append(" and su.source =" + search.getSource() + " ");
}
if (null != search.getOrgId()) {
querySql.append("and suo.org_id ='" + search.getOrgId() + "'");
// querySql.append("and suo.org_id ='" + search.getOrgId() + "'");
List<UserOrgEntity> byOrgId = userOrgDao.findByOrgId(search.getOrgId());
if (CollectionUtils.isEmpty(byOrgId)) {
return pageResult;
}
List<String> collect = byOrgId.stream().map(UserOrgEntity::getUserId).collect(Collectors.toList());
querySql.append(" and su.id in ('" + String.join("','", collect) + "')");
}
if (null != search.getQueryString()) {
querySql.append("and (su.engineer_code LIKE '%" + search.getQueryString() + "%' or su.nickname like '%" + search.getQueryString() + "%' or su.phone like '%" + search.getQueryString() + "%' or su.email like '%" + search.getQueryString() + "%' )");
querySql.append(" and (su.engineer_code LIKE '%" + search.getQueryString() + "%' or su.nickname like '%" + search.getQueryString() + "%' or su.phone like '%" + search.getQueryString() + "%' or su.email like '%" + search.getQueryString() + "%' )");
}
log.info("countSql:" + (count + querySql.toString()));
BigInteger countNum = (BigInteger) entityManager.createNativeQuery(count + querySql.toString()).getSingleResult();
......@@ -744,13 +762,35 @@ public class UserService {
log.info("selectSql:" + (select + querySql.toString() + limit));
Query query = entityManager.createNativeQuery(select + querySql.toString() + limit);
List<Map<String, Object>> resultList1 = query.getResultList();
// List<Map<String, Object>> resultList1 = query.getResultList();
// log.info("resultList1:" + JSONObject.toJSONString(resultList1));
//将结果转换为Map,返回后的map中的key是数据库表字段,如果用实体接收,使用@JsonAlias设置一下在反序列化时进行映射
query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List<Map<String, Object>> resultList = query.getResultList();
// log.info("resultList:" + JSONObject.toJSONString(resultList));
List<UserInfoSearch> list = resultList.stream().map(a -> mapConvertToObject(a)).collect(Collectors.toList());
RoleEntity finalRoleEntity = roleEntity;
List<UserInfoSearch> list = resultList.stream().map(a -> mapConvertToObject(a, finalRoleEntity)).collect(Collectors.toList());
//组装角色信息
if (CollectionUtils.isNotEmpty(list) && CollectionUtils.isEmpty(byRoleId) && StringUtils.isBlank(search.getRoleId())) {
List<String> collect = list.stream().map(UserInfoSearch::getId).collect(Collectors.toList());
List<UserRoleEntity> byUserIdIn = userRoleDao.findByUserIdIn(collect);
if (CollectionUtils.isNotEmpty(byUserIdIn)) {
List<RoleEntity> allById = roleDao.findAllById(byUserIdIn.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList()));
if (CollectionUtils.isNotEmpty(allById)) {
Map<String, String> roleMap = allById.stream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getName));
Map<String, String> userRoleMap = byUserIdIn.stream().collect(Collectors.toMap(UserRoleEntity::getUserId, UserRoleEntity::getRoleId));
for (UserInfoSearch infoSearch : list) {
if (userRoleMap.containsKey(infoSearch.getId())) {
if (roleMap.containsKey(userRoleMap.get(infoSearch.getId()))) {
infoSearch.setRoleId(userRoleMap.get(infoSearch.getId()));
infoSearch.setRoleName(roleMap.get(userRoleMap.get(infoSearch.getId())));
}
}
}
}
}
}
Integer pageInt = countNum.intValue() / search.getSize();
pageResult.setDataList(list);
pageResult.setCurrPage(search.getPage());
......@@ -766,7 +806,7 @@ public class UserService {
* @param map map
* @return r
*/
public static UserInfoSearch mapConvertToObject(Map<String, Object> map) {
public static UserInfoSearch mapConvertToObject(Map<String, Object> map, RoleEntity roleEntity) {
UserInfoSearch search = new UserInfoSearch();
search.setId(map.containsKey("id") && ObjectUtil.isNotEmpty(map.get("id")) ? map.get("id").toString() : null);
search.setAccount(map.containsKey("account") && ObjectUtil.isNotEmpty(map.get("account")) ? map.get("account").toString() : null);
......@@ -777,8 +817,6 @@ public class UserService {
search.setEmail(map.containsKey("email") && ObjectUtil.isNotEmpty(map.get("email")) ? map.get("email").toString() : null);
search.setPhone(map.containsKey("phone") && ObjectUtil.isNotEmpty(map.get("phone")) ? map.get("phone").toString() : null);
search.setEngineerCode(map.containsKey("engineer_code") && ObjectUtil.isNotEmpty(map.get("engineer_code")) ? map.get("engineer_code").toString() : null);
search.setRoleName(map.containsKey("roleName") && ObjectUtil.isNotEmpty(map.get("roleName")) ? map.get("roleName").toString() : null);
search.setRoleId(map.containsKey("roleId") && ObjectUtil.isNotEmpty(map.get("roleId")) ? map.get("roleId").toString() : null);
search.setBan(map.containsKey("ban") && ObjectUtil.isNotEmpty(map.get("ban")) ? Integer.parseInt(map.get("ban").toString()) : null);
search.setCreatedBy(map.containsKey("CREATED_BY") && ObjectUtil.isNotEmpty(map.get("CREATED_BY")) ? map.get("CREATED_BY").toString() : null);
search.setUpdatedBy(map.containsKey("UPDATED_BY") && ObjectUtil.isNotEmpty(map.get("UPDATED_BY")) ? map.get("UPDATED_BY").toString() : null);
......@@ -786,6 +824,10 @@ public class UserService {
search.setCreatedTime(map.containsKey("CREATED_TIME") && ObjectUtil.isNotEmpty(map.get("CREATED_TIME")) ? formatter.format(map.get("CREATED_TIME")) : null);
search.setUpdatedTime(map.containsKey("UPDATED_TIME") && ObjectUtil.isNotEmpty(map.get("UPDATED_TIME")) ? formatter.format(map.get("UPDATED_TIME")) : null);
search.setLastLoginTime(map.containsKey("LAST_LOGIN_TIME") && ObjectUtil.isNotEmpty(map.get("LAST_LOGIN_TIME")) ? formatter.format(map.get("LAST_LOGIN_TIME")) : null);
if (ObjectUtil.isNotNull(roleEntity)) {
search.setRoleName(roleEntity.getName());
search.setRoleId(roleEntity.getId());
}
return search;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!