Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
relation
Commits
08e978b4
Commit
08e978b4
authored
Jul 31, 2025
by
wujipeng
Browse files
增加身份证解析
parent
e3688774
Changes
2
Show whitespace changes
Inline
Side-by-side
src/main/java/com/platform/service/impl/AnalysisServiceImpl.java
View file @
08e978b4
...
...
@@ -10,6 +10,7 @@ import com.platform.model.vo.FileUploadVO;
import
com.platform.model.vo.TableHeaderVO
;
import
com.platform.service.*
;
import
com.platform.utils.DateUtils
;
import
com.platform.utils.IdCardUtils
;
import
com.platform.utils.StringUtils
;
import
org.apache.commons.compress.utils.Lists
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
...
...
@@ -28,6 +29,7 @@ import java.text.ParseException;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.CompletionException
;
import
java.util.regex.Matcher
;
...
...
@@ -204,7 +206,12 @@ public class AnalysisServiceImpl implements AnalysisService {
if
(
StringUtils
.
isNotEmpty
(
folderName
)){
String
userName
=
trimChineseNameFromFilename
(
folderName
);
userService
.
save
(
UserEntity
.
builder
().
userName
(
userName
).
build
());
Map
<
String
,
String
>
idCardInfo
=
IdCardUtils
.
parseIdCardInfo
(
folderName
);
userService
.
save
(
UserEntity
.
builder
().
userName
(
userName
)
.
age
(
Integer
.
parseInt
(
idCardInfo
.
get
(
"age"
)))
.
idCard
(
idCardInfo
.
get
(
"idCard"
))
.
sex
(
idCardInfo
.
get
(
"sex"
))
.
build
());
if
(!
excelData
.
isEmpty
()){
List
<
String
>
paymentBankNames
=
excelData
.
stream
().
map
(
ExcelDataEntity:
:
getPaymentBankName
).
distinct
().
collect
(
Collectors
.
toList
());
userAccountService
.
saveBatch
(
paymentBankNames
.
stream
().
map
(
paymentBankName
->
UserAccountEntity
.
builder
().
userName
(
userName
).
account
(
paymentBankName
).
build
()).
collect
(
Collectors
.
toList
()));
...
...
src/main/java/com/platform/utils/IdCardUtils.java
0 → 100644
View file @
08e978b4
package
com.platform.utils
;
import
com.google.common.collect.Maps
;
import
java.time.LocalDate
;
import
java.time.Period
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
IdCardUtils
{
/**
* 从字符串中提取身份证号并解析年龄和性别
* @param input 包含身份证号的字符串
* @return 包含年龄和性别的字符串信息,如果没有找到身份证号返回null
*/
public
static
Map
<
String
,
String
>
parseIdCardInfo
(
String
input
)
{
// 匹配18位或15位身份证号的正则表达式
String
idCardPattern
=
"\\b[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]\\b|"
+
"\\b[1-9]\\d{5}\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}\\b"
;
Pattern
pattern
=
Pattern
.
compile
(
idCardPattern
);
Matcher
matcher
=
pattern
.
matcher
(
input
);
if
(
matcher
.
find
())
{
String
idCard
=
matcher
.
group
();
return
analyzeIdCard
(
idCard
);
}
return
null
;
}
/**
* 分析身份证号,计算年龄和性别
* @param idCard 身份证号码
* @return 包含年龄和性别的字符串
*/
private
static
Map
<
String
,
String
>
analyzeIdCard
(
String
idCard
)
{
// 处理15位身份证号,转换为18位
if
(
idCard
.
length
()
==
15
)
{
idCard
=
convert15To18
(
idCard
);
}
// 提取出生日期
String
birthDateStr
=
idCard
.
substring
(
6
,
14
);
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
);
LocalDate
birthDate
=
LocalDate
.
parse
(
birthDateStr
,
formatter
);
// 计算年龄
LocalDate
currentDate
=
LocalDate
.
now
();
Period
period
=
Period
.
between
(
birthDate
,
currentDate
);
int
age
=
period
.
getYears
();
// 判断性别(第17位,奇数为男,偶数为女)
int
genderDigit
=
Character
.
getNumericValue
(
idCard
.
charAt
(
16
));
String
gender
=
(
genderDigit
%
2
==
1
)
?
"男"
:
"女"
;
Map
<
String
,
String
>
map
=
Maps
.
newHashMap
();
map
.
put
(
"idCard"
,
idCard
);
map
.
put
(
"age"
,
String
.
valueOf
(
age
));
map
.
put
(
"gender"
,
gender
);
return
map
;
}
/**
* 将15位身份证号转换为18位
* @param idCard15 15位身份证号
* @return 18位身份证号
*/
private
static
String
convert15To18
(
String
idCard15
)
{
// 在6位后添加19,变成17位
String
idCard17
=
idCard15
.
substring
(
0
,
6
)
+
"19"
+
idCard15
.
substring
(
6
);
// 计算校验位
int
[]
weight
=
{
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
};
char
[]
validate
=
{
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
};
int
sum
=
0
;
for
(
int
i
=
0
;
i
<
17
;
i
++)
{
sum
+=
Character
.
getNumericValue
(
idCard17
.
charAt
(
i
))
*
weight
[
i
];
}
int
mod
=
sum
%
11
;
return
idCard17
+
validate
[
mod
];
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment