Commit 2f4e3b71 authored by wujipeng's avatar wujipeng
Browse files

解析用户增加判断

parent 08e978b4
......@@ -4,6 +4,10 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.platform.model.entity.UserAccountEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccountEntity> {
void saveUserBatch(List<UserAccountEntity> userAccountList);
}
......@@ -9,4 +9,6 @@ public interface UserAccountService extends IService<UserAccountEntity> {
List<UserAccountEntity> queryListByUserName(String userName);
void deleteByUserName(String userName);
void saveUserBatch(List<UserAccountEntity> userAccountList);
}
......@@ -9,4 +9,6 @@ public interface UserService extends IService<UserEntity> {
UserVO getDetail(String userName);
boolean updateUser(UserDTO userDTO);
}
package com.platform.service.impl;
import com.alibaba.fastjson.JSON;
import com.platform.exception.BusinessException;
import com.platform.model.entity.ExcelDataEntity;
import com.platform.model.entity.ExcelTemplateEntity;
import com.platform.model.entity.UserAccountEntity;
......@@ -24,14 +25,9 @@ import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -59,7 +55,7 @@ public class AnalysisServiceImpl implements AnalysisService {
private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"};
@Override
public Result<?> canvas(MultipartFile[] files, Integer templateId,String caseId, String folderName) {
public Result<?> canvas(MultipartFile[] files, Integer templateId, String caseId, String folderName) {
//查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
String json = ExcelTemplate.getJson();
......@@ -77,18 +73,18 @@ public class AnalysisServiceImpl implements AnalysisService {
if (isExcelFile(file.getOriginalFilename())) {
FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
File tempFile = convertMultipartFileToFile(file);
processSingleExcelFile(tempFile, tableHeaderList,folderName, fileUploadVO.getId(),caseId);
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId);
//文件上传
fileUploadList.add(fileUploadVO);
}
}
return Result.data(fileUploadList);
} catch (Exception e) {
return Result.fail("文件处理失败: " + e.getMessage());
throw new BusinessException(e.getMessage());
}
}
private ExcelDataEntity handleExcelData(List<TableHeaderVO> tableHeaderList, Row row, String folderName,String fileId,String caseId) {
private ExcelDataEntity handleExcelData(List<TableHeaderVO> tableHeaderList, Row row, String folderName, String fileId, String caseId) {
ExcelDataEntity excelDataEntity = new ExcelDataEntity();
trySetValue(() -> excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex()))));
......@@ -113,7 +109,7 @@ public class AnalysisServiceImpl implements AnalysisService {
trySetValue(() -> excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex()))));
trySetValue(() -> excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex()))));
trySetValue(() -> excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex()))));
if(StringUtils.isNotEmpty(folderName)){
if (StringUtils.isNotEmpty(folderName)) {
//切割文件名前面是中文的人名,使用正则表达式
trySetValue(() -> excelDataEntity.setPayUserName(trimChineseNameFromFilename(folderName)));
}
......@@ -132,9 +128,10 @@ public class AnalysisServiceImpl implements AnalysisService {
if (matcher.find()) {
return matcher.group();
} else {
return "";
return "";
}
}
// 辅助方法:简化异常处理
private void trySetValue(Runnable setter) {
try {
......@@ -189,7 +186,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return false;
}
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName,String fileId,String caseId) throws IOException {
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, String caseId) throws IOException {
//读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度
......@@ -200,21 +197,24 @@ public class AnalysisServiceImpl implements AnalysisService {
for (int i = 1; i < sheetLength; i++) {
Row row = sheet.getRow(i);
//处理excel数据
ExcelDataEntity excelDataEntity = handleExcelData(tableHeaderList, row, folderName,fileId,caseId);
ExcelDataEntity excelDataEntity = handleExcelData(tableHeaderList, row, folderName, fileId, caseId);
excelData.add(excelDataEntity);
}
if(StringUtils.isNotEmpty(folderName)){
if (StringUtils.isNotEmpty(folderName)) {
String userName = trimChineseNameFromFilename(folderName);
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()){
if (!ObjectUtils.isEmpty(idCardInfo)) {
userService.save(UserEntity.builder().userName(userName)
.age(Integer.parseInt(idCardInfo.get("age")))
.idCard(idCardInfo.get("idCard"))
.sex(idCardInfo.get("sex"))
.idType("身份证")
.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()));
userAccountService.saveUserBatch(paymentBankNames.stream().map(paymentBankName -> UserAccountEntity.builder().userName(userName).account(paymentBankName).build()).collect(Collectors.toList()));
}
}
......
......@@ -22,4 +22,9 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserA
baseMapper.delete(new LambdaQueryWrapper<UserAccountEntity>()
.eq(UserAccountEntity::getUserName, userName));
}
@Override
public void saveUserBatch(List<UserAccountEntity> userAccountList) {
baseMapper.saveUserBatch(userAccountList);
}
}
......@@ -48,4 +48,5 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
}
return Boolean.TRUE;
}
}
package com.platform.utils;
import com.google.common.collect.Maps;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
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";
public static Map<String, String> parseIdCardInfo(String input) {
// 完全重新设计的正则表达式,更灵活地匹配身份证号
String idCardPattern = "(?<![0-9Xx])([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])(?![0-9Xx])|" +
"(?<![0-9])([1-9]\\d{5}\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3})(?![0-9])";
Pattern pattern = Pattern.compile(idCardPattern);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String idCard = matcher.group();
// 获取匹配的身份证号(可能是18位或15位的匹配组)
String idCard = matcher.group(1) != null ? matcher.group(1) : matcher.group(4);
return analyzeIdCard(idCard);
}
return null;
}
/**
* 分析身份证号,计算年龄和性别
* @param idCard 身份证号码
* @return 包含年龄和性别的字符串
*/
private static Map<String,String> analyzeIdCard(String idCard) {
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;
Map<String, String> idCardInfo = Maps.newHashMap();
idCardInfo.put("idCard", idCard);
idCardInfo.put("age", String.valueOf(age));
idCardInfo.put("sex", gender);
return idCardInfo;
}
/**
* 将15位身份证号转换为18位
* @param idCard15 15位身份证号
......@@ -72,18 +74,17 @@ public class IdCardUtils {
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.platform.mapper.UserAccountMapper">
<insert id="saveUserBatch">
insert IGNORE into user_account (user_name, account) values
<foreach item="item" collection="userAccountList" separator=",">
(#{item.userName}, #{item.account})
</foreach>
</insert>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.platform.mapper.UserMapper">
</mapper>
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