Commit 68967cf0 authored by 杨锐's avatar 杨锐

api - 按照姓名、份证号或手机号查询用户实名信息 - 精确查询,供客服用,不限制入参正确性,优化两次sql查询耗时问题

parent af38e4be
......@@ -6,13 +6,13 @@ import cn.quantgroup.xyqb.entity.UserDetail;
import cn.quantgroup.xyqb.event.UserDetailUpdateEvent;
import cn.quantgroup.xyqb.model.Gender;
import cn.quantgroup.xyqb.model.IdCardInfo;
import cn.quantgroup.xyqb.model.IdType;
import cn.quantgroup.xyqb.repository.IUserDetailRepository;
import cn.quantgroup.xyqb.repository.IUserRepository;
import cn.quantgroup.xyqb.service.auth.IIdCardService;
import cn.quantgroup.xyqb.service.user.IUserDetailService;
import cn.quantgroup.xyqb.service.user.vo.UserDetailVO;
import cn.quantgroup.xyqb.util.ValidationUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -22,13 +22,15 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by 11 on 2016/12/29.
......@@ -44,6 +46,8 @@ public class UserDetailServiceImpl implements IUserDetailService {
private IIdCardService idCardService;
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@Resource
private JdbcTemplate jdbcTemplate;
@Override
public UserDetail findByUserId(Long userId) {
......@@ -53,7 +57,7 @@ public class UserDetailServiceImpl implements IUserDetailService {
@Override
public UserDetail saveUserDetail(UserDetail userDetail) throws DataIntegrityViolationException {
String idNo = userDetail.getIdNo();
if (StringUtils.isNotEmpty(idNo)){
if (StringUtils.isNotEmpty(idNo)) {
userDetail.setIdNo(idNo.toUpperCase());
}
UserDetail userDetail1 = userDetailRepository.save(userDetail);
......@@ -79,19 +83,61 @@ public class UserDetailServiceImpl implements IUserDetailService {
@Override
public List<UserDetailVO> searchUserDetailList(String name, String phoneNo, String idNo) {
List<UserDetail> details = userDetailRepository.findAll(getSpecification(name, phoneNo, idNo));
Map<Long, User> userMap = Maps.newHashMap();
if (!CollectionUtils.isEmpty(details)) {
List<Long> userIds = details.stream().map(UserDetail::getUserId).collect(Collectors.toList());
List<User> users = userRepository.findAll((root, query, cb) -> {
query.where(root.get("id").in(userIds));
return query.getRestriction();
});
userMap = users.stream().collect(Collectors.toMap(User::getId, o -> o));
/* 优化两次sql查询耗时问题,示例name=王斌*/
// TODO: 2019/12/24 by rui 暂未找到jpa data同时满足join,动态传参方式。若日后发现,应优化该段代码。
/* 高层模块已校验至少必须满足一个条件不为空 */
StringBuilder sqlBuilder = new StringBuilder("select ud.*, u.enable " +
" from user_detail ud" +
" left join user u on (u.id = ud.user_id)" +
" where");
if (org.apache.commons.lang3.StringUtils.isNotBlank(name)) {
sqlBuilder.append(" name = ").append("'").append(name).append("'").append(" and");
}
if (org.apache.commons.lang3.StringUtils.isNotBlank(phoneNo)) {
sqlBuilder.append(" ud.phone_no = ").append("'").append(phoneNo).append("'").append(" and");
}
if (org.apache.commons.lang3.StringUtils.isNotBlank(idNo)) {
sqlBuilder.append(" id_no = ").append("'").append(idNo).append("'").append(" and");
}
String sql = sqlBuilder.toString();
if (sql.endsWith("and")) {
sql = sql.substring(0, sql.length() - 3);
}
return jdbcTemplate.query(sql, new RowMapper<UserDetailVO>() {
@Override
public UserDetailVO mapRow(ResultSet resultSet, int i) throws SQLException {
UserDetailVO userDetailVO = new UserDetailVO();
userDetailVO.setId(resultSet.getLong("id"));
userDetailVO.setUserId(resultSet.getLong("user_id"));
userDetailVO.setPhoneNo(resultSet.getString("phone_no"));
userDetailVO.setName(resultSet.getString("name"));
userDetailVO.setIdNo(resultSet.getString("id_no"));
userDetailVO.setIdType(IdType.values()[resultSet.getInt("id_type")]);
userDetailVO.setIsAuthenticated(resultSet.getInt("is_authenticated") == 1);
userDetailVO.setGender(Gender.values()[resultSet.getInt("gender")]);
userDetailVO.setEmail(resultSet.getString("email"));
userDetailVO.setQq(resultSet.getString("qq"));
userDetailVO.setEnable(resultSet.getObject("enable") == null ? null : resultSet.getBoolean("enable"));
userDetailVO.setCreatedAt(resultSet.getTimestamp("created_at").getTime());
userDetailVO.setUpdatedAt(resultSet.getTimestamp("updated_at").getTime());
return userDetailVO;
}
Map<Long, User> finalUserMap = userMap;
List<UserDetailVO> userDetailVOS = details.stream().map(o -> fromUserDetailAndUserMap(o, finalUserMap)).collect(Collectors.toList());
return userDetailVOS;
});
// TODO: 2019/12/24 by rui branch feature_optimize_20191223 上线验证后,即删除该注释块
// List<UserDetail> details = userDetailRepository.findAll(getSpecification(name, phoneNo, idNo));
// Map<Long, User> userMap = Maps.newHashMap();
// if (!CollectionUtils.isEmpty(details)) {
// List<Long> userIds = details.stream().map(UserDetail::getUserId).collect(Collectors.toList());
// List<User> users = userRepository.findAll((root, query, cb) -> {
// query.where(root.get("id").in(userIds));
// return query.getRestriction();
// });
// userMap = users.stream().collect(Collectors.toMap(User::getId, o -> o));
// }
// Map<Long, User> finalUserMap = userMap;
// List<UserDetailVO> userDetailVOS = details.stream().map(o -> fromUserDetailAndUserMap(o, finalUserMap)).collect(Collectors.toList());
// return userDetailVOS;
}
@Override
......@@ -207,14 +253,14 @@ public class UserDetailServiceImpl implements IUserDetailService {
}
if (StringUtils.isNotBlank(idNo)) {
IdCardInfo idCardInfo = idCardService.getIdCardInfo(idNo);
if(Objects.nonNull(idCardInfo) && idCardInfo.isValid()){
if (Objects.nonNull(idCardInfo) && idCardInfo.isValid()) {
newIdNo = idCardInfo.getIdNo();
newGender = idCardInfo.getGender();
}else{
} else {
log.error("用户的身份证错误,phoneNo:{},idNo:{}", phoneNo, idNo);
}
}
if(!Objects.equals(newName, userDetail.getName()) || !Objects.equals(newIdNo, userDetail.getIdNo())){
if (!Objects.equals(newName, userDetail.getName()) || !Objects.equals(newIdNo, userDetail.getIdNo())) {
return userDetailRepository.updateNameAndIdNoByPhoneNo(newName, newIdNo, Optional.ofNullable(newGender).orElse(Gender.UNKNOWN).ordinal(), phoneNo);
}
}
......@@ -231,7 +277,7 @@ public class UserDetailServiceImpl implements IUserDetailService {
}
@Override
public List<UserDetail> fuzzyQueryByPhoneNoAndIdNo(String phoneNo, String idNo){
public List<UserDetail> fuzzyQueryByPhoneNoAndIdNo(String phoneNo, String idNo) {
return userDetailRepository.fuzzyQueryByPhoneNoAndIdNo(phoneNo.concat("%"), idNo.concat("%"));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment