2021-06-15 18:23:48 +08:00
package com.cm.systemcity ;
2021-06-23 23:55:15 +08:00
import com.alibaba.excel.EasyExcel ;
import com.alibaba.excel.annotation.ExcelProperty ;
import com.alibaba.excel.context.AnalysisContext ;
import com.alibaba.excel.event.AnalysisEventListener ;
import com.cm.common.utils.UUIDUtil ;
import com.cm.common.utils.jdbc.JdbcUtil ;
import org.apache.commons.lang3.StringUtils ;
2021-06-16 11:30:26 +08:00
import org.junit.Test ;
2021-06-15 18:23:48 +08:00
import org.springframework.boot.test.context.SpringBootTest ;
2021-06-23 23:55:15 +08:00
import org.springframework.security.core.parameters.P ;
2021-06-15 18:23:48 +08:00
2021-06-23 23:55:15 +08:00
import java.io.File ;
import java.sql.* ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
//@SpringBootTest
public class SystemCityApplicationTests {
2021-06-15 18:23:48 +08:00
@Test
2021-06-23 23:55:15 +08:00
public void importCommunityBoss ( ) throws SQLException , ClassNotFoundException {
Class . forName ( " com.mysql.jdbc.Driver " ) ;
2021-07-04 12:53:58 +08:00
Connection connection = DriverManager . getConnection ( " jdbc:mysql://127.0.0.1:3306/db_cloud_servicecity?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false " , " root " , " 123456 " ) ;
2021-06-23 23:55:15 +08:00
// 所有用户
String sql = " SELECT * FROM sys_user WHERE is_delete = '0' " ;
PreparedStatement preparedStatement = connection . prepareStatement ( sql ) ;
ResultSet resultSet = preparedStatement . executeQuery ( ) ;
List < Map < String , Object > > userMaps = JdbcUtil . listResult ( resultSet ) ;
// 所有地区
sql = " SELECT * FROM city_dict WHERE is_delete = '0' " ;
preparedStatement = connection . prepareStatement ( sql ) ;
resultSet = preparedStatement . executeQuery ( ) ;
List < Map < String , Object > > areaMaps = JdbcUtil . listResult ( resultSet ) ;
// 所有社区
sql = " SELECT * FROM city_community WHERE is_delete = '0' " ;
preparedStatement = connection . prepareStatement ( sql ) ;
resultSet = preparedStatement . executeQuery ( ) ;
List < Map < String , Object > > communityMaps = JdbcUtil . listResult ( resultSet ) ;
// 读取Excel
List < CommunityBossExcel > noUsernameList = new ArrayList < > ( ) ;
List < CommunityBossExcel > noLeaderUsernameList = new ArrayList < > ( ) ;
List < CommunityBossExcel > noAreaList = new ArrayList < > ( ) ;
List < CommunityBossExcel > noCommunityList = new ArrayList < > ( ) ;
2021-07-04 12:53:58 +08:00
String wsqExcel = " D: \\ 腾狮科技 \\ 城市综合治理操作视频 \\ 1.xlsx " ;
String xtlExcel = " D: \\ 腾狮科技 \\ 城市综合治理操作视频 \\ 2.xlsx " ;
EasyExcel . read ( new File ( xtlExcel ) , CommunityBossExcel . class , new CommunityBossExcelListener ( ) {
2021-06-23 23:55:15 +08:00
@Override
public void listExcel ( List < CommunityBossExcel > communityBossExcels ) throws Exception {
for ( CommunityBossExcel communityBossExcel : communityBossExcels ) {
if ( StringUtils . isBlank ( communityBossExcel . getUsername ( ) ) ) {
continue ;
}
boolean isUsernameExist = false ;
boolean isLeaderUsernameExist = false ;
boolean isAreaExist = false ;
boolean isCommunityExist = false ;
for ( Map < String , Object > userMap : userMaps ) {
if ( StringUtils . equals ( communityBossExcel . getUsername ( ) , userMap . get ( " user_username " ) . toString ( ) ) ) {
isUsernameExist = true ;
communityBossExcel . setCommunityBossUserId ( userMap . get ( " user_id " ) . toString ( ) ) ;
}
if ( StringUtils . equals ( communityBossExcel . getLeaderUsername ( ) , userMap . get ( " user_username " ) . toString ( ) ) ) {
isLeaderUsernameExist = true ;
communityBossExcel . setCommunityBossParentUserId ( userMap . get ( " user_id " ) . toString ( ) ) ;
}
}
if ( ! isUsernameExist ) {
noUsernameList . add ( communityBossExcel ) ;
continue ;
}
if ( ! isLeaderUsernameExist ) {
noLeaderUsernameList . add ( communityBossExcel ) ;
}
for ( Map < String , Object > areaMap : areaMaps ) {
if ( StringUtils . equals ( communityBossExcel . getArea ( ) , areaMap . get ( " dict_name " ) . toString ( ) ) ) {
isAreaExist = true ;
communityBossExcel . setAreaId ( areaMap . get ( " dict_id " ) . toString ( ) ) ;
}
}
if ( ! isAreaExist ) {
noAreaList . add ( communityBossExcel ) ;
continue ;
}
String communities = communityBossExcel . getCommunities ( ) ;
String [ ] communityArray = communities . split ( " , " ) ;
for ( String community : communityArray ) {
for ( Map < String , Object > communityMap : communityMaps ) {
if ( StringUtils . equals ( community , communityMap . get ( " community_name " ) . toString ( ) ) ) {
isCommunityExist = true ;
String communityIds = communityBossExcel . getCommunityIds ( ) ;
if ( StringUtils . isNotBlank ( communityIds ) ) {
communityIds + = " , " ;
}
communityIds + = communityMap . get ( " community_id " ) . toString ( ) ;
communityBossExcel . setCommunityIds ( communityIds ) ;
}
}
}
if ( ! isCommunityExist ) {
noCommunityList . add ( communityBossExcel ) ;
continue ;
}
communityBossExcel . setCommunityBossId ( UUIDUtil . getUUID ( ) ) ;
}
// 添加2级管理员
// 万水泉镇,吴振宏
CommunityBossExcel wuZhenHong = new CommunityBossExcel ( ) ;
2021-06-25 00:43:12 +08:00
wuZhenHong . setUsername ( " 15148216918 " ) ;
2021-06-23 23:55:15 +08:00
wuZhenHong . setCommunityBossId ( UUIDUtil . getUUID ( ) ) ;
wuZhenHong . setCommunityBossParentId ( " 0 " ) ;
wuZhenHong . setCommunityBossUserId ( " e7cd222c-b9f6-436c-a914-88fe1319f771 " ) ;
wuZhenHong . setCommunityBossParentUserId ( " 0 " ) ;
wuZhenHong . setAreaId ( " 8d62a0cc-2c15-4355-906c-6720f8f3617e " ) ;
wuZhenHong . setArea ( " 万水泉镇 " ) ;
wuZhenHong . setLevel ( 2 ) ;
// 民馨路街道,石磊
CommunityBossExcel shiLei = new CommunityBossExcel ( ) ;
2021-06-25 00:43:12 +08:00
shiLei . setUsername ( " 13904729156 " ) ;
2021-06-23 23:55:15 +08:00
shiLei . setCommunityBossId ( UUIDUtil . getUUID ( ) ) ;
shiLei . setCommunityBossParentId ( " 0 " ) ;
shiLei . setCommunityBossUserId ( " f33bfdd0-2114-4c06-95df-03bdc0402f69 " ) ;
shiLei . setCommunityBossParentUserId ( " 0 " ) ;
shiLei . setAreaId ( " 6e922367-f21f-4daf-b38c-c55a67d9a35b " ) ;
shiLei . setArea ( " 民馨路街道 " ) ;
shiLei . setLevel ( 2 ) ;
// 稀土路街道,白世平
CommunityBossExcel baiShiPing = new CommunityBossExcel ( ) ;
baiShiPing . setCommunityBossId ( UUIDUtil . getUUID ( ) ) ;
2021-06-25 00:43:12 +08:00
baiShiPing . setUsername ( " 15561096300 " ) ;
2021-06-23 23:55:15 +08:00
baiShiPing . setCommunityBossParentId ( " 0 " ) ;
baiShiPing . setCommunityBossUserId ( " 81ea01a3-6d87-427b-85a2-aca60e58e757 " ) ;
baiShiPing . setCommunityBossParentUserId ( " 0 " ) ;
baiShiPing . setAreaId ( " 7ed23f06-566d-43b7-acd9-4f0b5db08974 " ) ;
baiShiPing . setArea ( " 稀土路街道 " ) ;
baiShiPing . setLevel ( 2 ) ;
communityBossExcels . add ( wuZhenHong ) ;
communityBossExcels . add ( shiLei ) ;
communityBossExcels . add ( baiShiPing ) ;
// 完善parentId
for ( CommunityBossExcel communityBossExcel : communityBossExcels ) {
for ( CommunityBossExcel parentCommunityBossExcel : communityBossExcels ) {
if ( StringUtils . equals ( communityBossExcel . getLeaderUsername ( ) , parentCommunityBossExcel . getUsername ( ) ) ) {
communityBossExcel . setCommunityBossParentId ( parentCommunityBossExcel . getCommunityBossId ( ) ) ;
break ;
}
}
}
2021-06-25 00:43:12 +08:00
String queryCommunityBossSql = " SELECT count(0) total FROM city_community_boss WHERE community_boss_user_id = ? AND community_boss_parent_user_id = ? AND community_boss_level = ? " ;
PreparedStatement queryCommunityBossPreparedStatement = connection . prepareStatement ( queryCommunityBossSql ) ;
2021-06-23 23:55:15 +08:00
// 新增网格长
String insertCommunityBossSql = " INSERT INTO city_community_boss( " +
" community_boss_id, " +
" community_boss_user_id, " +
" community_boss_parent_id, " +
" community_boss_parent_user_id, " +
" community_boss_level, " +
" area_id, " +
" area_name, " +
" grid_summary, " +
" creator, " +
" gmt_create, " +
" modifier, " +
" gmt_modified, " +
" is_delete) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " ;
PreparedStatement insertCommunityBossPreparedStatement = connection . prepareStatement ( insertCommunityBossSql ) ;
String insertDay = " 2021-06-23 22:22:22 " ;
// 网格长社区的关联关系
String insertCommunityBossCommunitySql = " INSERT INTO city_community_boss_community( " +
" community_boss_id, " +
" community_id) VALUES(?, ?) " ;
PreparedStatement insertCommunityBossCommunityPreparedStatement = connection . prepareStatement ( insertCommunityBossCommunitySql ) ;
for ( CommunityBossExcel communityBossExcel : communityBossExcels ) {
2021-06-25 00:43:12 +08:00
queryCommunityBossPreparedStatement . setString ( 1 , communityBossExcel . getCommunityBossUserId ( ) ) ;
queryCommunityBossPreparedStatement . setString ( 2 , communityBossExcel . getCommunityBossParentUserId ( ) ) ;
queryCommunityBossPreparedStatement . setInt ( 3 , communityBossExcel . getLevel ( ) - 1 ) ;
ResultSet countResultSet = queryCommunityBossPreparedStatement . executeQuery ( ) ;
Map < String , Object > count = JdbcUtil . getResult ( countResultSet ) ;
System . out . println ( " total: " + count . get ( " total " ) ) ;
if ( Integer . parseInt ( count . get ( " total " ) . toString ( ) ) > 0 ) {
continue ;
}
2021-06-23 23:55:15 +08:00
insertCommunityBossPreparedStatement . setString ( 1 , communityBossExcel . getCommunityBossId ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 2 , communityBossExcel . getCommunityBossUserId ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 3 , communityBossExcel . getCommunityBossParentId ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 4 , communityBossExcel . getCommunityBossParentUserId ( ) ) ;
insertCommunityBossPreparedStatement . setInt ( 5 , communityBossExcel . getLevel ( ) - 1 ) ;
insertCommunityBossPreparedStatement . setString ( 6 , communityBossExcel . getAreaId ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 7 , communityBossExcel . getArea ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 8 , communityBossExcel . getDistinct ( ) ) ;
insertCommunityBossPreparedStatement . setString ( 9 , " 1 " ) ;
insertCommunityBossPreparedStatement . setString ( 10 , insertDay ) ;
insertCommunityBossPreparedStatement . setString ( 11 , " 1 " ) ;
insertCommunityBossPreparedStatement . setString ( 12 , insertDay ) ;
insertCommunityBossPreparedStatement . setInt ( 13 , 0 ) ;
int insertCommunityBossCount = insertCommunityBossPreparedStatement . executeUpdate ( ) ;
System . out . println ( " insert communityBoss: " + insertCommunityBossCount ) ;
if ( ! communityBossExcel . getCommunityIds ( ) . isEmpty ( ) ) {
String [ ] communityIdArray = communityBossExcel . getCommunityIds ( ) . split ( " , " ) ;
for ( String communityId : communityIdArray ) {
insertCommunityBossCommunityPreparedStatement . setString ( 1 , communityBossExcel . getCommunityBossId ( ) ) ;
insertCommunityBossCommunityPreparedStatement . setString ( 2 , communityId ) ;
int insertCommunityBossCommunityCount = insertCommunityBossCommunityPreparedStatement . executeUpdate ( ) ;
System . out . println ( " insert communityBossCommunity: " + insertCommunityBossCommunityCount ) ;
}
}
}
}
2021-07-04 12:53:58 +08:00
} ) . headRowNumber ( 1 ) . sheet ( 0 ) . doRead ( ) ;
2021-06-25 09:42:16 +08:00
System . out . println ( " 无用户名网格长 " ) ;
2021-06-23 23:55:15 +08:00
noUsernameList . forEach ( System . out : : println ) ;
2021-06-25 09:42:16 +08:00
System . out . println ( " 无领导用户名网格长 " ) ;
2021-06-23 23:55:15 +08:00
noLeaderUsernameList . forEach ( System . out : : println ) ;
2021-06-25 09:42:16 +08:00
System . out . println ( " 无地区网格长 " ) ;
2021-06-23 23:55:15 +08:00
noAreaList . forEach ( System . out : : println ) ;
2021-06-25 09:42:16 +08:00
System . out . println ( " 无社区网格长 " ) ;
2021-06-23 23:55:15 +08:00
noCommunityList . forEach ( System . out : : println ) ;
}
public abstract class CommunityBossExcelListener extends AnalysisEventListener < CommunityBossExcel > {
private List < CommunityBossExcel > communityBossExcels = new ArrayList < > ( ) ;
@Override
public void invoke ( CommunityBossExcel communityBossExcel , AnalysisContext analysisContext ) {
communityBossExcels . add ( communityBossExcel ) ;
}
@Override
public void doAfterAllAnalysed ( AnalysisContext analysisContext ) {
try {
listExcel ( communityBossExcels ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
communityBossExcels . clear ( ) ;
}
public abstract void listExcel ( List < CommunityBossExcel > communityBossExcels ) throws Exception ;
}
public static class CommunityBossExcel {
@ExcelProperty ( index = 0 )
private String username ;
@ExcelProperty ( index = 1 )
private String leaderUsername ;
@ExcelProperty ( index = 2 )
private String userName ;
@ExcelProperty ( index = 3 )
private Integer level ;
@ExcelProperty ( index = 4 )
private String area ;
@ExcelProperty ( index = 5 )
private String communities ;
@ExcelProperty ( index = 6 )
private String distinct ;
private String communityBossUserId ;
private String communityBossParentUserId ;
private String areaId ;
private String communityIds ;
private String communityBossId ;
private String communityBossParentId ;
public String getUsername ( ) {
return username = = null ? " " : username . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setUsername ( String username ) {
this . username = username ;
}
public String getLeaderUsername ( ) {
return leaderUsername = = null ? " " : leaderUsername . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setLeaderUsername ( String leaderUsername ) {
this . leaderUsername = leaderUsername ;
}
public String getUserName ( ) {
return userName = = null ? " " : userName . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setUserName ( String userName ) {
this . userName = userName ;
}
public Integer getLevel ( ) {
return level ;
}
public void setLevel ( Integer level ) {
this . level = level ;
}
public String getArea ( ) {
return area = = null ? " " : area . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setArea ( String area ) {
this . area = area ;
}
public String getCommunities ( ) {
return communities = = null ? " " : communities . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setCommunities ( String communities ) {
this . communities = communities ;
}
public String getDistinct ( ) {
return distinct = = null ? " " : distinct . trim ( ) . replaceAll ( " \\ s " , " " ) ;
}
public void setDistinct ( String distinct ) {
this . distinct = distinct ;
}
public String getCommunityBossId ( ) {
return communityBossId = = null ? " " : communityBossId . trim ( ) ;
}
public void setCommunityBossId ( String communityBossId ) {
this . communityBossId = communityBossId ;
}
public String getCommunityBossParentId ( ) {
return communityBossParentId = = null ? " 0 " : communityBossParentId . trim ( ) ;
}
public void setCommunityBossParentId ( String communityBossParentId ) {
this . communityBossParentId = communityBossParentId ;
}
public String getCommunityBossUserId ( ) {
return communityBossUserId = = null ? " " : communityBossUserId . trim ( ) ;
}
public void setCommunityBossUserId ( String communityBossUserId ) {
this . communityBossUserId = communityBossUserId ;
}
public String getCommunityBossParentUserId ( ) {
return communityBossParentUserId = = null ? " " : communityBossParentUserId . trim ( ) ;
}
public void setCommunityBossParentUserId ( String communityBossParentUserId ) {
this . communityBossParentUserId = communityBossParentUserId ;
}
public String getAreaId ( ) {
return areaId = = null ? " " : areaId . trim ( ) ;
}
public void setAreaId ( String areaId ) {
this . areaId = areaId ;
}
public String getCommunityIds ( ) {
return communityIds = = null ? " " : communityIds . trim ( ) ;
}
public void setCommunityIds ( String communityIds ) {
this . communityIds = communityIds ;
}
@Override
public String toString ( ) {
final StringBuilder sb = new StringBuilder ( " { " ) ;
sb . append ( " \" username \" : \" " )
. append ( username ) . append ( '\"' ) ;
sb . append ( " , \" leaderUsername \" : \" " )
. append ( leaderUsername ) . append ( '\"' ) ;
sb . append ( " , \" userName \" : \" " )
. append ( userName ) . append ( '\"' ) ;
sb . append ( " , \" level \" : " )
. append ( level ) ;
sb . append ( " , \" area \" : \" " )
. append ( area ) . append ( '\"' ) ;
sb . append ( " , \" communities \" : \" " )
. append ( communities ) . append ( '\"' ) ;
sb . append ( " , \" distinct \" : \" " )
. append ( distinct ) . append ( '\"' ) ;
sb . append ( " , \" communityBossUserId \" : \" " )
. append ( communityBossUserId ) . append ( '\"' ) ;
sb . append ( " , \" communityBossParentUserId \" : \" " )
. append ( communityBossParentUserId ) . append ( '\"' ) ;
sb . append ( " , \" areaId \" : \" " )
. append ( areaId ) . append ( '\"' ) ;
sb . append ( " , \" communityIds \" : \" " )
. append ( communityIds ) . append ( '\"' ) ;
sb . append ( " , \" communityBossId \" : \" " )
. append ( communityBossId ) . append ( '\"' ) ;
sb . append ( " , \" communityBossParentId \" : \" " )
. append ( communityBossParentId ) . append ( '\"' ) ;
sb . append ( '}' ) ;
return sb . toString ( ) ;
}
2021-06-15 18:23:48 +08:00
}
}