Commit 32f74a41 authored by minminyan's avatar minminyan

http支持批量查询userId的手机号,返回id+phone的map

当请求数据量过大时一次只拿1000条,休息1ms,确保服务不挂
parent 46092b41
......@@ -21,6 +21,7 @@ import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Created by Miraculous on 2016/12/19.
......@@ -69,6 +70,18 @@ public class InnerController {
return JsonResult.buildSuccessResult("", userRet);
}
@RequestMapping("/user/getPhoneByUserIds")
public JsonResult findByIds(@RequestParam(value = "userIds") String userIdsString) {
LOGGER.info("批量查询用户的手机号列表, userIdsString:" + userIdsString);
if (StringUtils.isEmpty(userIdsString)) {
return JsonResult.buildErrorStateResult(null, null);
}
List<Long> userIds = JSONObject.parseObject(userIdsString, new TypeReference<List<Long>>() {
});
Map<Long, String> userIdAndPhoneMap = userService.findPhoneByIdsInDb(userIds);
return JsonResult.buildSuccessResult("", userIdAndPhoneMap);
}
@RequestMapping("/user/save")
public JsonResult saveUser(
String phoneNo, Long registeredFrom, Long createdAt, Long updatedAt,
......
......@@ -3,14 +3,18 @@ package cn.quantgroup.xyqb.repository;
import cn.quantgroup.xyqb.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by Miraculous on 15/7/4.
*/
public interface IUserRepository extends JpaRepository<User, Long> {
User findByPhoneNo(String phoneNo);
User findByPhoneNo(String phoneNo);
User findByUuid(String uuid);
User findByUuid(String uuid);
List<User> findByIdIn(List<Long> ids);
User findById(Long id);
User findById(Long id);
}
package cn.quantgroup.xyqb.service.user;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.entity.WechatUserInfo;
import java.util.List;
import java.util.Map;
/**
* Created by Miraculous on 15/7/5.
*/
public interface IUserService {
User findByPhoneWithCache(String phone);
User findByPhoneWithCache(String phone);
User findByUuidWithCache(String uuid);
User findByUuidWithCache(String uuid);
boolean register(String phoneNo, String password, Long registerFrom, String userIp, Long channelId);
boolean register(String phoneNo, String password, Long registerFrom, String userIp, Long channelId);
User registerAndReturn(String phoneNo, String password, Long registerFrom);
User registerAndReturn(String phoneNo, String password, Long registerFrom);
boolean exist(String phoneNo);
boolean exist(String phoneNo);
boolean resetPassword(String phoneNo, String password);
boolean resetPassword(String phoneNo, String password);
User findByPhoneInDb(String phone);
User findByPhoneInDb(String phone);
User findByUuidInDb(String uuid);
User findByUuidInDb(String uuid);
Map<Long, String> findPhoneByIdsInDb(List<Long> userIds);
User saveUser(User user);
User saveUser(User user);
User findById(Long userId);
User findById(Long userId);
}
......@@ -7,137 +7,169 @@ import cn.quantgroup.xyqb.service.sms.ISmsService;
import cn.quantgroup.xyqb.service.user.ILkbUserService;
import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.PasswordUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by Miraculous on 15/7/5.
*/
@Service
@Slf4j
public class UserServiceImpl implements IUserService {
@Autowired
RedisTemplate<String, String> stringRedisTemplate;
@Autowired
RedisTemplate<String, String> stringRedisTemplate;
@Autowired
private ILkbUserService lkbUserService;
@Autowired
private ILkbUserService lkbUserService;
@Autowired
private IUserRepository userRepository;
@Autowired
private IUserRepository userRepository;
@Autowired
private ISmsService smsService;
@Autowired
private ISmsService smsService;
@Override
public User findByPhoneInDb(String phone) {
return userRepository.findByPhoneNo(phone);
}
@Override
public User findByUuidInDb(String uuid) {
return userRepository.findByUuid(uuid);
}
@Override
public User saveUser(User user) {
return userRepository.save(user);
}
@Override
public User findById(Long userId) {
return userRepository.findById(userId);
}
@Override
public User registerAndReturn(String phoneNo, String password, Long registerFrom) {
String uuid = lkbUserService.registerApp(phoneNo, password);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
User user = new User();
user.setEnable(true);
user.setPhoneNo(phoneNo);
user.setUpdatedAt(currentTime);
user.setCreatedAt(currentTime);
user.setUuid(uuid);
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
user.setRegisteredFrom(registerFrom);
return userRepository.save(user);
}
@Override
@Cacheable(value = "usercache", key = "'xyqbuser' + #phone", unless = "#result == null", cacheManager = "cacheManager")
public User findByPhoneWithCache(String phone) {
return userRepository.findByPhoneNo(phone);
}
@Override
public User findByPhoneInDb(String phone) {
return userRepository.findByPhoneNo(phone);
}
@Override
@Cacheable(value = "usercache", key = "'xyqbuser' + #uuid", unless = "#result == null", cacheManager = "cacheManager")
public User findByUuidWithCache(String uuid) {
return userRepository.findByUuid(uuid);
@Override
public Map<Long, String> findPhoneByIdsInDb(List<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Maps.newHashMap();
}
@Override
@Transactional(value = Transactional.TxType.REQUIRED)
public boolean register(String phoneNo, String password, Long registerFrom, String userIp, Long channelId) {
String uuid = lkbUserService.registerApp(phoneNo, password);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
User user = new User();
user.setEnable(true);
user.setPhoneNo(phoneNo);
user.setUpdatedAt(currentTime);
user.setCreatedAt(currentTime);
user.setUuid(uuid);
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
//解决线上白条registerFrom为1的问题
if(channelId == 222L) {
user.setRegisteredFrom(channelId);
} else {
user.setRegisteredFrom(registerFrom);
Map<Long, String> userIdAndPhoneMap = Maps.newHashMap();
int pageSize = 1000;
int idSize = userIds.size();
for (int i = 0; i < idSize; i += pageSize) {
List<Long> subList = userIds.subList(i, Math.min(idSize, i + pageSize));
List<User> users = userRepository.findByIdIn(subList);
users.stream().forEach(user -> {
userIdAndPhoneMap.put(user.getId(), user.getPhoneNo());
});
if (i + pageSize < idSize) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
log.error("线程sleep失败", e);
}
//user.setRegisteredFrom(registerFrom);
user = userRepository.save(user);
smsService.sendAfterRegister(phoneNo);
return user != null;
}
}
@Override
public boolean exist(String phoneNo) {
return userRepository.findByPhoneNo(phoneNo) != null;
return userIdAndPhoneMap;
}
@Override
public User findByUuidInDb(String uuid) {
return userRepository.findByUuid(uuid);
}
@Override
public User saveUser(User user) {
return userRepository.save(user);
}
@Override
public User findById(Long userId) {
return userRepository.findById(userId);
}
@Override
public User registerAndReturn(String phoneNo, String password, Long registerFrom) {
String uuid = lkbUserService.registerApp(phoneNo, password);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
User user = new User();
user.setEnable(true);
user.setPhoneNo(phoneNo);
user.setUpdatedAt(currentTime);
user.setCreatedAt(currentTime);
user.setUuid(uuid);
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
user.setRegisteredFrom(registerFrom);
return userRepository.save(user);
}
@Override
@Cacheable(value = "usercache", key = "'xyqbuser' + #phone", unless = "#result == null", cacheManager = "cacheManager")
public User findByPhoneWithCache(String phone) {
return userRepository.findByPhoneNo(phone);
}
@Override
@Cacheable(value = "usercache", key = "'xyqbuser' + #uuid", unless = "#result == null", cacheManager = "cacheManager")
public User findByUuidWithCache(String uuid) {
return userRepository.findByUuid(uuid);
}
@Override
@Transactional(value = Transactional.TxType.REQUIRED)
public boolean register(String phoneNo, String password, Long registerFrom, String userIp, Long channelId) {
String uuid = lkbUserService.registerApp(phoneNo, password);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
User user = new User();
user.setEnable(true);
user.setPhoneNo(phoneNo);
user.setUpdatedAt(currentTime);
user.setCreatedAt(currentTime);
user.setUuid(uuid);
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
//解决线上白条registerFrom为1的问题
if (channelId == 222L) {
user.setRegisteredFrom(channelId);
} else {
user.setRegisteredFrom(registerFrom);
}
/**
* 修改用户密码
* @date 2017-02-15 修改用户修改密码时,更新updatedAt时间
* @param phoneNo
* @param password
* @return
*/
@Override
@CacheEvict(value = "usercache", key = "'xyqbuser' + #phone", cacheManager = "cacheManager")
public boolean resetPassword(String phoneNo, String password) {
User user = userRepository.findByPhoneNo(phoneNo);
if (user == null) {
throw new RuntimeException("用户[" + phoneNo + "]不存在");
}
user.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
user = userRepository.save(user);
stringRedisTemplate.expire("usercache:xyqbuser" + phoneNo, 1L, TimeUnit.MILLISECONDS);
return StringUtils.equals(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT), user.getPassword());
//user.setRegisteredFrom(registerFrom);
user = userRepository.save(user);
smsService.sendAfterRegister(phoneNo);
return user != null;
}
@Override
public boolean exist(String phoneNo) {
return userRepository.findByPhoneNo(phoneNo) != null;
}
/**
* 修改用户密码
*
* @param phoneNo
* @param password
* @return
* @date 2017-02-15 修改用户修改密码时,更新updatedAt时间
*/
@Override
@CacheEvict(value = "usercache", key = "'xyqbuser' + #phone", cacheManager = "cacheManager")
public boolean resetPassword(String phoneNo, String password) {
User user = userRepository.findByPhoneNo(phoneNo);
if (user == null) {
throw new RuntimeException("用户[" + phoneNo + "]不存在");
}
user.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
user = userRepository.save(user);
stringRedisTemplate.expire("usercache:xyqbuser" + phoneNo, 1L, TimeUnit.MILLISECONDS);
return StringUtils.equals(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT), user.getPassword());
}
}
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