Commit 2754c5e0 authored by zhuxiang's avatar zhuxiang
Browse files

Merge remote-tracking branch 'origin/dev' into dev

parents bc10e8bc bfcfe506
......@@ -3,7 +3,12 @@ package com.platform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.platform.model.entity.UserAccountEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccountEntity> {
void saveUserBatch(@Param("userAccountList") List<UserAccountEntity> userAccountList);
}
......@@ -3,10 +3,16 @@ package com.platform.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@TableName("user_account")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserAccountEntity {
@TableId(value = "id", type = IdType.AUTO)
......
......@@ -3,10 +3,16 @@ package com.platform.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@TableName("user")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserEntity {
@TableId(value = "id",type = IdType.AUTO)
......
......@@ -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;
import com.platform.model.entity.UserEntity;
import com.platform.model.resp.Result;
import com.platform.model.vo.FileUploadVO;
import com.platform.model.vo.TableHeaderVO;
import com.platform.service.AnalysisService;
import com.platform.service.ExcelDataService;
import com.platform.service.ExcelTemplateService;
import com.platform.service.FileUploadService;
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;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
......@@ -24,13 +26,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.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -48,14 +46,18 @@ public class AnalysisServiceImpl implements AnalysisService {
private ExcelTemplateService excelTemplateService;
@Resource
private ExcelDataService excelDataService;
@Resource
private FileUploadService fileUploadService;
@Resource
private UserService userService;
@Resource
private UserAccountService userAccountService;
private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"};
@Override
public Result<?> canvas(MultipartFile[] files, Integer templateId,String caseId, String folderName) {
@Transactional
public Result<?> canvas(MultipartFile[] files, Integer templateId, String caseId, String folderName) {
//查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
String json = ExcelTemplate.getJson();
......@@ -73,18 +75,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()))));
......@@ -109,7 +111,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)));
}
......@@ -128,9 +130,10 @@ public class AnalysisServiceImpl implements AnalysisService {
if (matcher.find()) {
return matcher.group();
} else {
return "";
return "";
}
}
// 辅助方法:简化异常处理
private void trySetValue(Runnable setter) {
try {
......@@ -185,7 +188,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长度
......@@ -196,9 +199,27 @@ 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)) {
String userName = trimChineseNameFromFilename(folderName);
Map<String, String> idCardInfo = IdCardUtils.parseIdCardInfo(folderName);
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.saveUserBatch(paymentBankNames.stream().map(paymentBankName -> UserAccountEntity.builder().userName(userName).account(paymentBankName).build()).collect(Collectors.toList()));
}
}
//保存excel数据
excelDataService.saveBatch(excelData);
}
......
......@@ -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) {
// 完全重新设计的正则表达式,更灵活地匹配身份证号
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()) {
// 获取匹配的身份证号(可能是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) {
// 处理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> idCardInfo = Maps.newHashMap();
idCardInfo.put("idCard", idCard);
idCardInfo.put("age", String.valueOf(age));
idCardInfo.put("sex", gender);
return idCardInfo;
}
/**
* 将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
<?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