Commit c0a457e0 authored by xiaoguang.xu's avatar xiaoguang.xu

feat: 增加手机号hashMapping

parent b59276a3
......@@ -12,6 +12,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
......@@ -78,4 +79,12 @@ public class JpaConfig {
return transactionManager;
}
@Bean
@DependsOn(value = "dataSource")
public JdbcTemplate jdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
package cn.quantgroup.xyqb.controller.external;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.entity.UserHashMapping;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.repository.IUserRepository;
import cn.quantgroup.xyqb.util.encrypt.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
@RestController
@RequestMapping("/ex/sync_hash")
public class SyncHashController {
private Boolean isContinue = false;
public static Boolean fight = false;
@Autowired
private IUserRepository userRepository;
@Autowired
private JdbcTemplate jdbcTemplate;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
/**
* 发起同步
*
* @param startUserId
* @param step
* @return
*/
@GetMapping("/start")
public JsonResult sync(final Long startUserId, final Long step) {
isContinue = true;
executorService
.execute(() -> {
Long startPosition = startUserId;
while (isContinue) {
List<User> users = userRepository.findByIdLimit(startPosition, step);
if (users.isEmpty()) {
log.info("没有数据了. 结束了");
return;
}
List<UserHashMapping> userHashMappings = new ArrayList<>();
users.forEach(user -> {
UserHashMapping userHashMapping = new UserHashMapping();
userHashMapping.setUserId(user.getId());
userHashMapping.setPhoneNoMd5(MD5Util.build(user.getPhoneNo()));
userHashMappings.add(userHashMapping);
});
String sql = "INSERT INTO `user_hash_mapping` ( `user_id`, `phone_no_md5`, `phone_no_md5_short`) VALUES (?,?,?);";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
UserHashMapping userHashMapping = userHashMappings.get(i);
ps.setLong(1, userHashMapping.getUserId());
ps.setString(2, userHashMapping.getPhoneNoMd5());
ps.setLong(3, userHashMapping.getPhoneNoMd5Short());
}
@Override
public int getBatchSize() {
return userHashMappings.size();
}
});
startPosition = users.get(users.size() - 1).getId() + 1;
log.info("来一波");
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
});
return JsonResult.buildSuccessResult(null);
}
/**
* 暂停同步
*/
@GetMapping("/stop")
public JsonResult stop() {
isContinue = false;
return JsonResult.buildSuccessResult(null);
}
@GetMapping("/fight")
public JsonResult fight() {
fight = true;
return JsonResult.buildSuccessResult(null);
}
}
package cn.quantgroup.xyqb.entity;
import cn.quantgroup.xyqb.util.HashUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
......@@ -9,9 +11,14 @@ import java.sql.Timestamp;
@Data
@Entity
@Table(name = "user_hash_mapping")
@NoArgsConstructor
public class UserHashMapping implements Serializable {
private static final long serialVersionUID = -1L;
public UserHashMapping(Long userId) {
this.userId = userId;
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -49,4 +56,15 @@ public class UserHashMapping implements Serializable {
public void preUpdate() {
updatedAt = new Timestamp(System.currentTimeMillis());
}
public void setPhoneNoMd5(String phoneNoMd5) {
this.phoneNoMd5 = phoneNoMd5;
this.phoneNoMd5Short = HashUtil.crc32(phoneNoMd5);
}
public void setIdNoMd5(String idNoMd5) {
this.idNoMd5 = idNoMd5;
this.idNoMd5Short = HashUtil.crc32(idNoMd5);
}
}
\ No newline at end of file
package cn.quantgroup.xyqb.event;
import cn.quantgroup.xyqb.controller.external.SyncHashController;
import cn.quantgroup.xyqb.entity.UserHashMapping;
import cn.quantgroup.xyqb.model.UserRegisterParam;
import cn.quantgroup.xyqb.repository.IUserHashMappingRepository;
import cn.quantgroup.xyqb.util.encrypt.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class PhoneHashEventListener implements ApplicationListener<RegisterEvent> {
@Autowired
private IUserHashMappingRepository userHashMappingRepository;
@Override
public void onApplicationEvent(RegisterEvent event) {
UserRegisterParam userRegisterParam = event.getUserRegisterParam();
String phoneNo = userRegisterParam.getPhoneNo();
Long userId = userRegisterParam.getUser().getId();
if(!SyncHashController.fight){
return;
}
UserHashMapping userHashMapping = userHashMappingRepository.findByUserId(userId);
if (userHashMapping != null && userHashMapping.getIdNoMd5() != null) {
//已存在的用户, 并且已存在身份证号
return;
}
if (userHashMapping == null) {
//创建一个新的
userHashMapping = new UserHashMapping(userId);
}
//无论如何都更新手机号
String phoneNoMd5 = MD5Util.build(phoneNo);
userHashMapping.setPhoneNoMd5(phoneNoMd5);
String idNo = userRegisterParam.getIdNo();
//如果有身份证号. 更新一下
if (StringUtils.isNotEmpty(idNo)) {
userHashMapping.setIdNoMd5(MD5Util.build(idNo));
}
userHashMappingRepository.saveAndFlush(userHashMapping);
}
}
......@@ -7,4 +7,6 @@ public interface IUserHashMappingRepository extends JpaRepository<UserHashMappin
UserHashMapping findByPhoneNoMd5ShortAndPhoneNoMd5(Long phoneNoMd5Short, String phoneNoMd5);
UserHashMapping findByUserId(Long userId);
}
package cn.quantgroup.xyqb.repository;
import java.util.List;
import cn.quantgroup.xyqb.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import cn.quantgroup.xyqb.entity.User;
import java.util.List;
/**
......@@ -48,4 +47,7 @@ public interface IUserRepository extends JpaRepository<User, Long>, JpaSpecifica
@Query(value = "update user set enable=?1 where phone_no=?2", nativeQuery = true)
int forbiddenUser(Boolean enable, String phoneNo);
@Query(value = "select * from user where id >= ?1 limit ?2", nativeQuery = true)
List<User> findByIdLimit(Long id, Long limit);
}
......@@ -9,6 +9,7 @@ import cn.quantgroup.xyqb.repository.IUserHashMappingRepository;
import cn.quantgroup.xyqb.repository.IUserRepository;
import cn.quantgroup.xyqb.service.user.IUserDetailService;
import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.HashUtil;
import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.ValidationUtil;
import com.google.common.collect.Lists;
......@@ -27,7 +28,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.zip.CRC32;
/**
* Created by Miraculous on 15/7/5.
......@@ -187,9 +187,7 @@ public class UserServiceImpl implements IUserService {
@Override
public User findByPhoneMd5(String md5Value) {
md5Value = md5Value.toLowerCase();
CRC32 crc32 = new CRC32();
crc32.update(md5Value.getBytes());
long value = crc32.getValue();
long value = HashUtil.crc32(md5Value);
UserHashMapping userHashMapping = userHashMappingRepository.findByPhoneNoMd5ShortAndPhoneNoMd5(value, md5Value);
if (userHashMapping == null) {
return null;
......
package cn.quantgroup.xyqb.util;
import java.util.zip.CRC32;
/**
* hash工具类.
*/
public final class HashUtil {
public static Long crc32(String content) {
CRC32 crc32 = new CRC32();
crc32.update(content.getBytes());
return crc32.getValue();
}
}
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