From 2a76be6f05cf13453b69609aa36d8548a5bae119 Mon Sep 17 00:00:00 2001
From: Administrator <admin>
Date: Mon, 29 Nov 2021 16:04:42 +0800
Subject: [PATCH] 保安员导入新增身份证号校验
---
src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java | 174 ++++++++++++++++++++++++----------
src/main/java/org/springblade/common/utils/IdCardNoUtil.java | 118 +++++++++++++++++++++++
2 files changed, 242 insertions(+), 50 deletions(-)
diff --git a/src/main/java/org/springblade/common/utils/IdCardNoUtil.java b/src/main/java/org/springblade/common/utils/IdCardNoUtil.java
new file mode 100644
index 0000000..be8a13e
--- /dev/null
+++ b/src/main/java/org/springblade/common/utils/IdCardNoUtil.java
@@ -0,0 +1,118 @@
+package org.springblade.common.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 身份证号码校验工具类
+ * @author zhongrj
+ * @since 2021-11-29
+ */
+public class IdCardNoUtil {
+
+ /**
+ * 身份证号码校验
+ * @param idCardNo
+ * @return
+ */
+ public static boolean checkIdCardNo(String idCardNo){
+ //先校验长度
+ if (idCardNo.length()!=18){
+ return false;
+ }
+
+ //将末尾可能存在的x转成X
+ idCardNo=idCardNo.toUpperCase();
+
+ String regex="";
+ //前6位地址码。后面仍需打表校验
+ regex+="^[1-6]\\d{5}";
+
+ //年份。后面仍需校验
+ regex+="(18|19|20)\\d{2}";
+ //月份。后面仍需校验
+ regex+="((0[1-9])|(1[0-2]))";
+ //日期。后面仍需校验
+ regex+="(([0-2][1-9])|10|20|30|31)";
+
+ //3位顺序码
+ regex+="\\d{3}";
+
+ //检验码。后面仍需验证
+ regex+="[0-9X]";
+
+ if(!idCardNo.matches(regex)){
+ return false;
+ }
+
+ //第1,2位(省)打表进一步校验
+ int[] d={11,12,13,14,15,
+ 21,22,23,31,32,33,34,35,36,37,
+ 41,42,43,
+ 44,45,46,
+ 50,51,52,53,53,
+ 61,62,63,64,65,
+ 83,81,82};
+ boolean flag=false;
+ int prov=Integer.parseInt(idCardNo.substring(0, 2));
+ for(int i=0;i<d.length;i++) {
+ if (d[i] == prov) {
+ flag = true;
+ break;
+ }
+ }
+ if(!flag) {
+ return false;
+ }
+ //生日校验:生日的时间不能比当前时间(指程序检测用户输入的身份证号码的时候)晚
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
+
+ Date birthDate= null;
+ try {
+ birthDate = sdf.parse(idCardNo.substring(6, 14));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ Date curDate=new Date();
+ if(birthDate.getTime()>curDate.getTime()) {
+ return false;
+ }
+ //生日校验:每个月的天数不一样(有的月份没有31),还要注意闰年的二月
+ int year=Integer.parseInt(idCardNo.substring(6, 10));
+ int leap=((year%4==0 && year%100!=0) || year%400==0)?1:0;
+ final int[] month={0,31,28+leap,31,30,31,30,31,31,30,31,30,31};
+ int mon=Integer.parseInt(idCardNo.substring(10, 12));
+ int day=Integer.parseInt(idCardNo.substring(12, 14));
+ if(day>month[mon]){
+ //System.out.println(day+" "+month[mon]+"\n");
+ //System.out.println("---");
+ return false;
+ }
+
+ //检验码
+ if(idCardNo.charAt(17)!=getLastChar(idCardNo)) {
+ return false;
+ }
+ return true;
+ }
+
+
+ /**
+ * 根据身份证号码的前17位计算校验码
+ * @param idNum
+ * @return
+ */
+ public static char getLastChar(String idNum){
+ final int[] w={0,7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
+ //这就是为什么一开始将末尾可能存在的x转成X的原因
+ final char[] ch={'1','0','X','9','8','7','6','5','4','3','2'};
+ int res=0;
+ for(int i=0;i<17;i++)
+ {
+ int t=idNum.charAt(i)-'0';
+ res+=(t*w[i+1]);
+ }
+ return ch[res%11];
+ }
+}
diff --git a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
index 926be67..ae31613 100644
--- a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
+++ b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
@@ -27,6 +27,7 @@
import org.springblade.common.constant.AgeUtil;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.constant.TenantConstant;
+import org.springblade.common.utils.IdCardNoUtil;
import org.springblade.common.utils.QRCodeUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
@@ -826,6 +827,26 @@
//如果deptIds 为空,则说明还没有改公司
throw new ServiceException("导入失败!公司名:["+user.getDeptId()+"]不存在!");
}
+
+ //身份证校验
+ if (null==user.getCardid() || user.getCardid().equals("")){
+ throw new ServiceException("导入失败!身份证号码不能为空!");
+ }
+ if (null!=user.getCardid() && !user.getCardid().equals("")){
+ //去除所有空格
+ String cardid = user.getCardid().replaceAll(" ", "");
+ //校验
+ boolean b = IdCardNoUtil.checkIdCardNo(cardid);
+ if(b){
+ user.setCardid(cardid);
+ }else {
+ agetStatus.set(false);
+ ageErrorList.add(user.getCardid());
+// throw new ServiceException("导入失败!身份证号码[ "+user.getCardid()+" ]不正确,请核对!");
+ //forEach 只能使用 return 跳出本次循环
+ return;
+ }
+ }
//判断当前用户是否已在本单位,如果是的更新数据
User user1 = new User();
user1.setAccount(user.getCardid());
@@ -881,26 +902,6 @@
//新增
this.save(user);
//内网同步
-// String s = "insert into blade_user(" +
-// "id,tenant_id,account,password,real_name,phone,sex,role_id,dept_id," +
-// "cardid,nation,registered,securitynumber,hold,status,is_deleted) " +
-// "values(" + "'" + user.getId() + "'" + "," +
-// "'" + user.getTenantId() + "'" + "," +
-// "'" + user.getAccount() + "'" + "," +
-// "'" + user.getPassword() + "'" + "," +
-// "'" + user.getRealName() + "'" + "," +
-// "'" + user.getPhone() + "'" + "," +
-// "'" + user.getSex() + "'" + "," +
-// "'" + user.getRoleId() + "'" +
-// "," + "'" + user.getDeptId() + "'" +
-// "," + "'" + user.getCardid() + "'" +
-// "," + "'" + user.getNation() + "'" +
-// "," + "'" + user.getRegistered() + "'" +
-// "," + "'" + user.getSecuritynumber() + "'" +
-// "," + "'" + user.getHold() + "'" +
-// "," + "'" + user.getStatus() + "'" +
-// "," + "'" + user.getIsDeleted() + "'"
-// + ")";
String s = "insert into blade_user(" +
"id,tenant_id,account,password,name,real_name,avatar,email,phone,sex," +
"role_id,dept_id,cardid,nativePlace,nation,fingerprint,education," +
@@ -944,9 +945,7 @@
//匹配组织机构是否一致,如果不一致
if(!user2.getDeptId().equals(user.getDeptId())){
Dept dept = deptService.getById(user2.getDeptId());
- System.out.println("dept = " + dept);
Dept dept1 = deptService.getById(user.getDeptId());
- System.out.println("dept1 = " + dept1);
//如果是南昌总公司分公司导入的
if (dept.getId().equals(1432626178757275649L) && dept1.getParentId().equals(1432626178757275649L)){
user2.setDeptId(dept1.getId().toString());
@@ -1012,26 +1011,26 @@
}
});
//如果所有数据导入有一个异常
-// if (!status.get() || !agetStatus.get()){
-// if (!status.get() && agetStatus.get()) {
-// String errorAccount = StringUtils.join(errorList, "\\\n");
-// throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
-// }
-// if (!agetStatus.get() && status.get()) {
-// String errorAccount = StringUtils.join(ageErrorList, "\\\n");
-// throw new ServiceException("用户:[" + errorAccount + "]导入失败!年龄不符!");
-// }
-// if (!status.get() && !agetStatus.get()) {
-// String errorAccount = StringUtils.join(errorList, "\\\n");
-// String errorAgeAccount = StringUtils.join(ageErrorList, "\\\n");
-// throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!"+
-// "用户:[" + errorAgeAccount + "]导入失败!年龄不符!");
-// }
-// }
- if (!status.get()) {
- String errorAccount = StringUtils.join(errorList, "\\\n");
- throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
+ if (!status.get() || !agetStatus.get()){
+ if (!status.get() && agetStatus.get()) {
+ String errorAccount = StringUtils.join(errorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
+ }
+ if (!agetStatus.get() && status.get()) {
+ String errorAccount = StringUtils.join(ageErrorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!身份证号码不正确,请核对!");
+ }
+ if (!status.get() && !agetStatus.get()) {
+ String errorAccount = StringUtils.join(errorList, "\\\n");
+ String errorAgeAccount = StringUtils.join(ageErrorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!"+
+ "用户:[" + errorAgeAccount + "]导入失败!身份证号码不正确,请核对!");
+ }
}
+// if (!status.get()) {
+// String errorAccount = StringUtils.join(errorList, "\\\n");
+// throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
+// }
}
@Override
@@ -1039,11 +1038,12 @@
//将不能导入的保安员账号存起来
List<String> errorList = new ArrayList<>();
//将需要新增的保安员信息存入集合
- List<User> insertList = new ArrayList<>();
+ List<String> ageErrorList = new ArrayList<>();
//将需要更新的保安员信息存入集合
List<User> updateList = new ArrayList<>();
//导入状态,默认为true ,如果有一个出现问题则为 false
AtomicBoolean status = new AtomicBoolean(true);
+ AtomicBoolean agetStatus = new AtomicBoolean(true);
data.forEach(userExcel -> {
User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class));
//设置部门id
@@ -1058,6 +1058,26 @@
}else {
//如果deptIds 为空,则说明还没有改公司
throw new ServiceException("导入失败!公司名:["+user.getDeptId()+"]不存在!");
+ }
+
+ //身份证校验
+ if (null==user.getCardid() || user.getCardid().equals("")){
+ throw new ServiceException("导入失败!身份证号码不能为空!");
+ }
+ if (null!=user.getCardid() && !user.getCardid().equals("")){
+ //去除所有空格
+ String cardid = user.getCardid().replaceAll(" ", "");
+ //校验
+ boolean b = IdCardNoUtil.checkIdCardNo(cardid);
+ if(b){
+ user.setCardid(cardid);
+ }else {
+ agetStatus.set(false);
+ ageErrorList.add(user.getCardid());
+// throw new ServiceException("导入失败!身份证号码[ "+user.getCardid()+" ]不正确,请核对!");
+ //forEach 只能使用 return 跳出本次循环
+ return;
+ }
}
//判断当前用户是否已在本单位,如果是的更新数据
User user1 = new User();
@@ -1173,12 +1193,50 @@
else {
//匹配组织机构是否一致,如果不一致
if(!user2.getDeptId().equals(user.getDeptId())){
+// Dept dept = deptService.getById(user2.getDeptId());
+// status.set(false);
+// //加入集合
+// errorList.add(user.getCardid());
+// //forEach 只能使用 return 跳出本次循环
+// return;
+
Dept dept = deptService.getById(user2.getDeptId());
- status.set(false);
- //加入集合
- errorList.add(user.getCardid());
- //forEach 只能使用 return 跳出本次循环
- return;
+ Dept dept1 = deptService.getById(user.getDeptId());
+ //如果是南昌总公司分公司导入的
+ if (dept.getId().equals(1432626178757275649L) && dept1.getParentId().equals(1432626178757275649L)){
+ user2.setDeptId(dept1.getId().toString());
+ //判断是否持证
+ if (null != userExcel.getHold() && userExcel.getHold() != "") {
+ if (userExcel.getHold().equals("是") && userExcel.getSecuritynumber()!=null && !userExcel.getSecuritynumber().equals("")) {
+ user2.setHold("1");
+ //更新保安证编号
+ user2.setSecuritynumber(user.getSecuritynumber());
+ }
+ if (userExcel.getHold().equals("否")) {
+ user2.setHold("2");
+ }
+ }
+ if (null!=userExcel.getRegistered()){
+ user2.setRegistered(userExcel.getRegistered());
+ }else {
+ user2.setRegistered("");
+ }
+ //更新用户数据
+ this.updateById(user2);
+ String s1 =
+ "update blade_user set hold = " + "'" + user2.getHold() + "'"
+ + ",securitynumber = " + "'" + user2.getSecuritynumber() + "'"
+ + ",dept_id = " + "'" + user2.getDeptId() + "'"
+ + ",registered = " + "'" + user2.getRegistered() + "'"
+ + " " + "where id = " + "'" + user2.getId() + "'";
+ FtpUtil.sqlFileUpload(s1);
+ }else {
+ status.set(false);
+ //加入集合
+ errorList.add(user.getCardid());
+ //forEach 只能使用 return 跳出本次循环
+ return;
+ }
}else {
//如果是一致,则更新用户数据
//判断是否持证
@@ -1209,9 +1267,25 @@
}
});
//如果所有数据导入有一个异常
- if (!status.get()){
- String errorAccount = StringUtils.join(errorList, "\\\n");
- throw new ServiceException("用户:["+errorAccount+"]导入失败!已在其他单位存在!");
+// if (!status.get()){
+// String errorAccount = StringUtils.join(errorList, "\\\n");
+// throw new ServiceException("用户:["+errorAccount+"]导入失败!已在其他单位存在!");
+// }
+ if (!status.get() || !agetStatus.get()){
+ if (!status.get() && agetStatus.get()) {
+ String errorAccount = StringUtils.join(errorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
+ }
+ if (!agetStatus.get() && status.get()) {
+ String errorAccount = StringUtils.join(ageErrorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!身份证号码不正确,请核对!");
+ }
+ if (!status.get() && !agetStatus.get()) {
+ String errorAccount = StringUtils.join(errorList, "\\\n");
+ String errorAgeAccount = StringUtils.join(ageErrorList, "\\\n");
+ throw new ServiceException("用户:[" + errorAccount + "]导入失败!已在其他单位存在!"+
+ "用户:[" + errorAgeAccount + "]导入失败!身份证号码不正确,请核对!");
+ }
}
}
--
Gitblit v1.9.3