新增网格员导入功能

This commit is contained in:
wanggeng 2022-01-24 20:58:59 +08:00
parent a009f59551
commit 668a572fb2

View File

@ -0,0 +1,467 @@
package cn.com.tenlion.usercenter;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.date.DateUtil;
import ink.wgink.util.jdbc.JdbcUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName: BaseGridMemberTest
* @Description: 基础网格员测试
* @Author: wanggeng
* @Date: 2022/1/24 10:19 AM
* @Version: 1.0
*/
public class BaseGridMemberTest {
// 网格员角色ID
private String gridMemberRoleId = "90675eba-e63e-4819-b45d-2c99ab78a278";
private String SQL_GET_USER = "SELECT * FROM sys_user WHERE user_username = ?";
// 新增用户
private String SQL_INSERT_USER = "INSERT INTO sys_user(" +
"`user_id`, " +
"`user_username`, " +
"`user_password`, " +
"`user_name`, " +
"`user_phone`, " +
"`user_email`, " +
"`user_ukey`, " +
"`user_ukey_electronic_secret_key`, " +
"`user_type`, " +
"`user_state`, " +
"`user_expired_date`, " +
"`user_avatar`, " +
"`user_longitude`, " +
"`user_latitude`, " +
"`last_login_address`, " +
"`last_login_time`, " +
"`login_type`, " +
"`gmt_password_modified`, " +
"`remarks`, " +
"`creator`, " +
"`gmt_create`, " +
"`modifier`, " +
"`gmt_modified`, " +
"`is_delete`) VALUES (?, ?, ?, ?, ?, '', NULL, NULL, 2, 0, '', '', '0', '0', '', NULL, 1, NULL, NULL, '1', ?, '1', ?, 0);";
// 删除用户拓展属性
private String SQL_DELETE_USER_EXPAND = "DELETE FROM sys_user_expand WHERE user_id = ?";
// 用户拓展属性
private String SQL_INSERT_USER_EXPAND = "INSERT INTO sys_user_expand(" +
"`user_id`, " +
"`area_code`, " +
"`area_name`, " +
"`user_level`, " +
"`user_sex`, " +
"`user_sex_name`, " +
"`user_birth`, " +
"`user_politic`, " +
"`user_politic_name`, " +
"`user_education`, " +
"`user_education_name`, " +
"`creator`, " +
"`gmt_create`, " +
"`modifier`, " +
"`gmt_modified`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '1', ?, '1', ?);";
// 删除用户角色
private String SQL_DELETE_USER_ROLE = "DELETE FROM sys_role_user WHERE user_id = ?";
// 用户角色
private String SQL_INSERT_USER_ROLE = "INSERT INTO sys_role_user(`role_id`, `user_id`) VALUES(?, ?)";
// 字典
private String SQL_LIST_DATA_BY_DATA_PARENT_ID = "SELECT data_id, data_name FROM data_data WHERE data_parent_id = ?";
// 区域
private String SQL_GET_AREA_BY_AREA_CODE = "SELECT area_id, area_parent_id, area_name, area_code FROM data_area WHERE area_code = ?";
private String SQL_GET_AREA_BY_AREA_ID = "SELECT area_id, area_parent_id, area_name, area_code FROM data_area WHERE area_id = ?";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void importGridMember() throws SQLException {
long startTime = System.currentTimeMillis();
String currentDateTime = DateUtil.getTime();
Connection connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/db_smart_city_usercenter?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true", "localhost", 3306), "root", "root");
// PreparedStatement
PreparedStatement getUserPS = connection.prepareStatement(SQL_GET_USER);
PreparedStatement insertUserPS = connection.prepareStatement(SQL_INSERT_USER);
PreparedStatement deleteUserExpandPS = connection.prepareStatement(SQL_DELETE_USER_EXPAND);
PreparedStatement insertUserExpandPS = connection.prepareStatement(SQL_INSERT_USER_EXPAND);
PreparedStatement deleteUserRolePS = connection.prepareStatement(SQL_DELETE_USER_ROLE);
PreparedStatement insertUserRolePS = connection.prepareStatement(SQL_INSERT_USER_ROLE);
// 性别字典
PreparedStatement listSexDataPS = connection.prepareStatement(SQL_LIST_DATA_BY_DATA_PARENT_ID);
listSexDataPS.setString(1, "4ef46940-76ab-4758-b5f0-0cbc93ffc660");
List<Map<String, Object>> sexMaps = JdbcUtil.listResult(listSexDataPS.executeQuery());
// 学历字典
PreparedStatement listEducationDataPS = connection.prepareStatement(SQL_LIST_DATA_BY_DATA_PARENT_ID);
listEducationDataPS.setString(1, "d6b9f026-6ea9-456a-b48b-0c18d502523b");
List<Map<String, Object>> educationMaps = JdbcUtil.listResult(listEducationDataPS.executeQuery());
// 政治面貌字典
PreparedStatement listPoliticalDataPS = connection.prepareStatement(SQL_LIST_DATA_BY_DATA_PARENT_ID);
listPoliticalDataPS.setString(1, "b2c96de0-3df8-41db-99b1-bc4e24ac6c24");
List<Map<String, Object>> politicalMaps = JdbcUtil.listResult(listPoliticalDataPS.executeQuery());
// 区域
PreparedStatement getAreaByCodePS = connection.prepareStatement(SQL_GET_AREA_BY_AREA_CODE);
PreparedStatement getAreaByIdPS = connection.prepareStatement(SQL_GET_AREA_BY_AREA_ID);
connection.setAutoCommit(false);
EasyExcel.read(new File("/Users/wanggeng/Desktop/乌兰察布市域社会治理/基础数据/集宁区/网格员/网格员(全).xlsx"), ExcelGridMember.class, new ExcelGridMemberExcelListener() {
private int saveUserCount = 0;
private int saveUserExpandCount = 0;
private int saveUserRoleCount = 0;
@Override
public void doSave(List<ExcelGridMember> excelGridMembers) {
try {
for (ExcelGridMember excelGridMember : excelGridMembers) {
// 查询用户
getUserPS.setString(1, excelGridMember.getUserPhone());
Map<String, Object> userMap = JdbcUtil.getResult(getUserPS.executeQuery());
if (userMap != null) {
continue;
}
String userId = UUIDUtil.getUUID();
// 新增用户
saveUserCount += saveUser(insertUserPS, userId, excelGridMember, currentDateTime);
// 新增用户拓展
saveUserExpandCount += saveUserExpand(deleteUserExpandPS, insertUserExpandPS, userId, excelGridMember, currentDateTime, getAreaByCodePS, getAreaByIdPS, sexMaps, educationMaps, politicalMaps);
// 新增用户角色
saveUserRoleCount += saveUserRole(deleteUserRolePS, insertUserRolePS, userId, gridMemberRoleId);
}
System.out.println();
System.out.println("saveUserCount: " + saveUserCount);
System.out.println("saveUserExpandCount: " + saveUserExpandCount);
System.out.println("saveUserRoleCount: " + saveUserRoleCount);
connection.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}).headRowNumber(1).sheet(0).doRead();
long endTime = System.currentTimeMillis();
System.out.println("耗时:" + (endTime - startTime) + "ms");
}
/**
* 获取性别
*
* @param dataId
* @param sexMaps
* @return
*/
private String getSexById(String dataId, List<Map<String, Object>> sexMaps) {
if (StringUtils.isBlank(dataId)) {
return "";
}
for (Map<String, Object> map : sexMaps) {
if (StringUtils.equals(dataId, map.get("data_id").toString())) {
return map.get("data_name").toString();
}
}
return "";
}
/**
* 获取学历
*
* @param dataId
* @param educationMaps
* @return
*/
private String getEducationById(String dataId, List<Map<String, Object>> educationMaps) {
if (StringUtils.isBlank(dataId)) {
return "";
}
for (Map<String, Object> map : educationMaps) {
if (StringUtils.equals(dataId, map.get("data_id").toString())) {
return map.get("data_name").toString();
}
}
return "";
}
/**
* 获取政治面
*
* @param dataId
* @param politicalMaps
* @return
*/
private String getPoliticalById(String dataId, List<Map<String, Object>> politicalMaps) {
if (StringUtils.isBlank(dataId)) {
return "";
}
for (Map<String, Object> map : politicalMaps) {
if (StringUtils.equals(dataId, map.get("data_id").toString())) {
return map.get("data_name").toString();
}
}
return "";
}
/**
* 获取地区全称
*
* @param areaCode
* @param getAreaByCodePS
* @return
* @throws SQLException
*/
private String getFullArea(String areaCode, PreparedStatement getAreaByCodePS, PreparedStatement getAreaByParentIdPS) throws SQLException {
getAreaByCodePS.setString(1, areaCode);
Map<String, Object> areaMap = JdbcUtil.getResult(getAreaByCodePS.executeQuery());
String areaParentId = areaMap.get("area_parent_id").toString();
String areaName = areaMap.get("area_name").toString();
return getFullArea(areaName, areaParentId, getAreaByParentIdPS);
}
/**
* 获取地区全称
*
* @param fullName
* @param areaParentId
* @param getAreaByParentIdPS
* @return
* @throws SQLException
*/
private String getFullArea(String fullName, String areaParentId, PreparedStatement getAreaByParentIdPS) throws SQLException {
getAreaByParentIdPS.setString(1, areaParentId);
System.out.println(getAreaByParentIdPS);
Map<String, Object> areaMap = JdbcUtil.getResult(getAreaByParentIdPS.executeQuery());
if (areaMap == null) {
return fullName;
}
fullName = areaMap.get("area_name").toString() + " / " + fullName;
System.out.println(fullName);
return getFullArea(fullName, areaMap.get("area_parent_id").toString(), getAreaByParentIdPS);
}
/**
* 得到生日
*
* @param birth
* @return
*/
private String getBirth(String birth) {
if (StringUtils.isBlank(birth)) {
return "";
}
String[] birthArray = birth.split("\\.");
if (birthArray.length <= 1) {
return "";
}
return String.format("%04d-%02d", Integer.parseInt(birthArray[0]), Integer.parseInt(birthArray[1]));
}
/**
* 保存用户
*
* @param insertUserPS
* @param userId
* @param excelGridMember
* @param currentDateTime
* @return
* @throws SQLException
*/
private int saveUser(PreparedStatement insertUserPS, String userId, ExcelGridMember excelGridMember, String currentDateTime) throws SQLException {
// 新增用户
insertUserPS.setString(1, userId);
insertUserPS.setString(2, excelGridMember.getUserPhone());
insertUserPS.setString(3, new BCryptPasswordEncoder().encode("88888888"));
insertUserPS.setString(4, excelGridMember.getUserName().replaceAll("\\s", ""));
insertUserPS.setString(5, excelGridMember.getUserPhone());
insertUserPS.setString(6, currentDateTime);
insertUserPS.setString(7, currentDateTime);
System.out.println(insertUserPS);
return insertUserPS.executeUpdate();
}
/**
* 保存用户拓展
*
* @param deleteUserExpandPS
* @param insertUserExpandPS
* @param userId
* @param excelGridMember
* @param currentDateTime
* @return
* @throws SQLException
*/
private int saveUserExpand(PreparedStatement deleteUserExpandPS,
PreparedStatement insertUserExpandPS,
String userId,
ExcelGridMember excelGridMember,
String currentDateTime,
PreparedStatement getAreaByCodePS,
PreparedStatement getAreaByParentIdPS,
List<Map<String, Object>> sexMaps,
List<Map<String, Object>> educationMaps,
List<Map<String, Object>> politicalMaps) throws SQLException {
// 删除用户拓展属性
deleteUserExpandPS.setString(1, userId);
System.out.println(deleteUserExpandPS);
deleteUserExpandPS.executeUpdate();
// 新增用户拓展属性
insertUserExpandPS.setString(1, userId);
insertUserExpandPS.setString(2, excelGridMember.getAreaCode());
insertUserExpandPS.setString(3, getFullArea(excelGridMember.getAreaCode(), getAreaByCodePS, getAreaByParentIdPS));
insertUserExpandPS.setInt(4, 4);
insertUserExpandPS.setString(5, excelGridMember.getSexId());
insertUserExpandPS.setString(6, getSexById(excelGridMember.getSexId(), sexMaps));
insertUserExpandPS.setString(7, getBirth(excelGridMember.getUserBirth()));
insertUserExpandPS.setString(8, excelGridMember.getPoliticalId());
insertUserExpandPS.setString(9, getPoliticalById(excelGridMember.getPoliticalId(), politicalMaps));
insertUserExpandPS.setString(10, excelGridMember.getEducationId());
insertUserExpandPS.setString(11, getEducationById(excelGridMember.getEducationId(), educationMaps));
insertUserExpandPS.setString(12, currentDateTime);
insertUserExpandPS.setString(13, currentDateTime);
System.out.println(insertUserExpandPS);
return insertUserExpandPS.executeUpdate();
}
/**
* 新增用户角色
*
* @param deleteUserRolePS
* @param insertUserRolePS
* @param userId
* @param roleId
* @return
* @throws SQLException
*/
private int saveUserRole(PreparedStatement deleteUserRolePS, PreparedStatement insertUserRolePS, String userId, String roleId) throws SQLException {
// 删除用户角色
deleteUserRolePS.setString(1, userId);
System.out.println(deleteUserRolePS);
deleteUserRolePS.executeUpdate();
// 新增用户角色
insertUserRolePS.setString(1, roleId);
insertUserRolePS.setString(2, userId);
System.out.println(insertUserRolePS);
return insertUserRolePS.executeUpdate();
}
public abstract class ExcelGridMemberExcelListener extends AnalysisEventListener<ExcelGridMember> {
private List<ExcelGridMember> excelGridMembers = new ArrayList<>(0);
@Override
public void invoke(ExcelGridMember excelGridMember, AnalysisContext analysisContext) {
if (excelGridMembers.size() > 100) {
doSave(excelGridMembers);
excelGridMembers.clear();
}
excelGridMembers.add(excelGridMember);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
doSave(excelGridMembers);
excelGridMembers.clear();
}
public abstract void doSave(List<ExcelGridMember> excelGridMembers);
}
public static class ExcelGridMember {
@ExcelProperty(index = 2)
private String userName;
@ExcelProperty(index = 4)
private String userBirth;
@ExcelProperty(index = 7)
private String userPhone;
@ExcelProperty(index = 8)
private String areaCode;
@ExcelProperty(index = 9)
private String sexId;
@ExcelProperty(index = 10)
private String educationId;
@ExcelProperty(index = 11)
private String politicalId;
public String getUserName() {
return userName == null ? "" : userName.trim();
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserBirth() {
return userBirth == null ? "" : userBirth.trim();
}
public void setUserBirth(String userBirth) {
this.userBirth = userBirth;
}
public String getUserPhone() {
return userPhone == null ? "" : userPhone.trim();
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getAreaCode() {
return areaCode == null ? "" : areaCode.trim();
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getSexId() {
return sexId == null ? "" : sexId.trim();
}
public void setSexId(String sexId) {
this.sexId = sexId;
}
public String getEducationId() {
return educationId == null ? "" : educationId.trim();
}
public void setEducationId(String educationId) {
this.educationId = educationId;
}
public String getPoliticalId() {
return politicalId == null ? "" : politicalId.trim();
}
public void setPoliticalId(String politicalId) {
this.politicalId = politicalId;
}
}
}