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

模板去重逻辑

parent ce8d5880
package com.platform.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD) // 表示该注解只能用于成员变量
@Retention(RetentionPolicy.RUNTIME) // 表示该注解在运行时可用
public @interface NumericValue {
double value(); // 唯一的数值参数
}
\ No newline at end of file
...@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId; ...@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.platform.annotation.NumericValue;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
...@@ -33,64 +34,81 @@ public class ExcelDataEntity { ...@@ -33,64 +34,81 @@ public class ExcelDataEntity {
@ApiModelProperty("目录id") @ApiModelProperty("目录id")
private String directoryId; private String directoryId;
@NumericValue(0)
@ApiModelProperty("交易类型") @ApiModelProperty("交易类型")
private String type; private String type;
@NumericValue(1)
@ApiModelProperty("交易主体的出入") @ApiModelProperty("交易主体的出入")
private String direction; private String direction;
@NumericValue(2)
@ApiModelProperty("交易时间") @ApiModelProperty("交易时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date time; private Date time;
@NumericValue(3)
@ApiModelProperty("交易金额") @ApiModelProperty("交易金额")
private double amount; private double amount;
@NumericValue(4)
@ApiModelProperty("交易流水号") @ApiModelProperty("交易流水号")
private String serial; private String serial;
@NumericValue(5)
@ApiModelProperty("收款方银行卡所属银行卡号") @ApiModelProperty("收款方银行卡所属银行卡号")
@TableField("payee_bank_card") @TableField("payee_bank_card")
private String payeeBankCard; private String payeeBankCard;
@NumericValue(6)
@ApiModelProperty("收款方支付账号") @ApiModelProperty("收款方支付账号")
@TableField("payee_account") @TableField("payee_account")
private String payeeAccount; private String payeeAccount;
@NumericValue(7)
@ApiModelProperty("收款方商户号") @ApiModelProperty("收款方商户号")
@TableField("payee_merchant") @TableField("payee_merchant")
private String payeeMerchant; private String payeeMerchant;
@NumericValue(8)
@ApiModelProperty("收款方商户名称") @ApiModelProperty("收款方商户名称")
@TableField("payee_name") @TableField("payee_name")
private String payeeName; private String payeeName;
@NumericValue(9)
@ApiModelProperty("付款方银行卡所属银行名称") @ApiModelProperty("付款方银行卡所属银行名称")
@TableField("payment_bank_name") @TableField("payment_bank_name")
private String paymentBankName; private String paymentBankName;
@NumericValue(10)
@ApiModelProperty("付款方银行卡所属银行卡号") @ApiModelProperty("付款方银行卡所属银行卡号")
@TableField("payment_bank_card") @TableField("payment_bank_card")
private String paymentBankCard; private String paymentBankCard;
@NumericValue(11)
@ApiModelProperty("付款方支付账号") @ApiModelProperty("付款方支付账号")
@TableField("payment_account") @TableField("payment_account")
private String paymentAccount; private String paymentAccount;
@NumericValue(12)
@ApiModelProperty("交易支付设备ip") @ApiModelProperty("交易支付设备ip")
private String ip; private String ip;
@NumericValue(13)
@ApiModelProperty("mac地址") @ApiModelProperty("mac地址")
private String mac; private String mac;
@NumericValue(14)
@ApiModelProperty("银行外部渠道交易流水号") @ApiModelProperty("银行外部渠道交易流水号")
@TableField("external_channel") @TableField("external_channel")
private String externalChannel; private String externalChannel;
@NumericValue(15)
@ApiModelProperty("支付账号") @ApiModelProperty("支付账号")
@TableField("pay_account") @TableField("pay_account")
private String payAccount; private String payAccount;
@NumericValue(16)
@ApiModelProperty("备注") @ApiModelProperty("备注")
private String remark; private String remark;
......
package com.platform.service.impl; package com.platform.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import com.platform.model.entity.*; import com.platform.model.entity.ExcelDataEntity;
import com.platform.model.entity.UserAccountEntity;
import com.platform.model.entity.UserEntity;
import com.platform.model.resp.Result; import com.platform.model.resp.Result;
import com.platform.model.vo.AnalysisVO; import com.platform.model.vo.AnalysisVO;
import com.platform.model.vo.FileUploadVO; import com.platform.model.vo.FileUploadVO;
import com.platform.model.vo.TableHeaderVO; import com.platform.model.vo.TableHeaderVO;
import com.platform.service.*; import com.platform.service.*;
import com.platform.utils.AnnotationUtils;
import com.platform.utils.DateUtils; import com.platform.utils.DateUtils;
import com.platform.utils.IdCardUtils; import com.platform.utils.IdCardUtils;
import com.platform.utils.StringUtils; import com.platform.utils.StringUtils;
...@@ -25,6 +27,9 @@ import javax.annotation.Resource; ...@@ -25,6 +27,9 @@ import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException; import java.text.ParseException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -71,51 +76,50 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -71,51 +76,50 @@ public class AnalysisServiceImpl implements AnalysisService {
// if (ObjectUtils.isEmpty(ExcelTemplate)) { // if (ObjectUtils.isEmpty(ExcelTemplate)) {
// return Result.fail("模板不存在"); // return Result.fail("模板不存在");
// } // }
boolean flag = false;
//默认模板 //默认模板
List<TableHeaderVO> tableHeaderList = Lists.newArrayList(); List<TableHeaderVO> tableHeaderList = Lists.newArrayList();
tableHeaderList.add(TableHeaderVO.builder().name("交易类型").index(2).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易类型").index(2).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("交易主体的出入账标识").index(4).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易主体的出入账标识").index(4).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("交易时间").index(5).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易时间").index(5).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("交易金额").index(7).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易金额").index(7).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("交易流水号").index(8).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易流水号").index(8).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("收款方银行卡所属银行卡号").index(12).build()); tableHeaderList.add(TableHeaderVO.builder().name("收款方银行卡所属银行卡号").index(12).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("收款方的支付帐号").index(13).build()); tableHeaderList.add(TableHeaderVO.builder().name("收款方的支付帐号").index(13).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("收款方的商户号").index(15).build()); tableHeaderList.add(TableHeaderVO.builder().name("收款方的商户号").index(15).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("收款方的商户名称").index(16).build()); tableHeaderList.add(TableHeaderVO.builder().name("收款方的商户名称").index(16).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("付款方银行卡所属银行名称").index(18).build()); tableHeaderList.add(TableHeaderVO.builder().name("付款方银行卡所属银行名称").index(18).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("付款方银行卡所属银行卡号").index(19).build()); tableHeaderList.add(TableHeaderVO.builder().name("付款方银行卡所属银行卡号").index(19).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("付款方的支付帐号").index(20).build()); tableHeaderList.add(TableHeaderVO.builder().name("付款方的支付帐号").index(20).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("交易支付设备ip").index(22).build()); tableHeaderList.add(TableHeaderVO.builder().name("交易支付设备ip").index(22).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("mac地址").index(23).build()); tableHeaderList.add(TableHeaderVO.builder().name("mac地址").index(23).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("银行外部渠道交易流水号").index(27).build()); tableHeaderList.add(TableHeaderVO.builder().name("银行外部渠道交易流水号").index(27).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("支付帐号").index(29).build()); tableHeaderList.add(TableHeaderVO.builder().name("支付帐号").index(29).filter(false).build());
tableHeaderList.add(TableHeaderVO.builder().name("备注").index(30).build()); tableHeaderList.add(TableHeaderVO.builder().name("备注").index(30).filter(false).build());
if (files.size() == 0) { if (files.isEmpty()) {
return Result.fail("上传的文件为空"); return Result.fail("上传的文件为空");
} }
List<FileUploadVO> fileUploadList = Lists.newArrayList(); List<FileUploadVO> fileUploadList = Lists.newArrayList();
//错误数量 //错误数量
List<AnalysisVO> detail = Lists.newArrayList(); List<AnalysisVO> detail = Lists.newArrayList();
int errorCount = 0; int errorCount = 0;
int totalCount = 0; int totalCount = 0;
Map<String, String> groupedMap = Maps.newHashMap(); Map<String, String> groupedMap = Maps.newHashMap();
List<UserAccountEntity> userAccountList = Lists.newArrayList(); List<UserAccountEntity> userAccountList = Lists.newArrayList();
List<ExcelDataEntity> excelData = Lists.newArrayList();
for (MultipartFile file : files) { for (MultipartFile file : files) {
// 检查是否是Excel文件 // 检查是否是Excel文件
if (isExcelFile(file.getOriginalFilename())) { if (isExcelFile(file.getOriginalFilename())) {
FileUploadVO fileUploadVO = fileUploadService.uploadFile(file); FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
File tempFile = null; File tempFile;
try { try {
tempFile = convertMultipartFileToFile(file); tempFile = convertMultipartFileToFile(file);
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException("文件转换错误"); throw new BusinessException("文件转换错误");
} }
try { try {
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, userAccountList, groupedMap); processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException("文件解析错误"); throw new BusinessException("文件解析错误");
} }
...@@ -125,44 +129,55 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -125,44 +129,55 @@ public class AnalysisServiceImpl implements AnalysisService {
} else { } else {
errorCount++; errorCount++;
totalCount++; totalCount++;
detail.add(AnalysisVO.builder() detail.add(AnalysisVO.builder().folderName(folderName).fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
.folderName(folderName) }
.fileName(file.getOriginalFilename()) }
.errorReason("文件格式错误") //把tableHeaderList中filter为true索引组成一个数字类型的集合
.build()); List<Integer> filterIndex = tableHeaderList.stream().filter(TableHeaderVO::getFilter).map(TableHeaderVO::getIndex).collect(Collectors.toList());
Map<String, ExcelDataEntity> excelDataMap = Maps.newHashMap();
//保存excel数据,判断是否使用默认模板
if (flag) {
List<ExcelDataEntity> afterDeduplication = Lists.newArrayList();
for (ExcelDataEntity excelDatum : excelData) {
StringBuilder key = new StringBuilder();
for (Integer index : filterIndex) {
Field field = AnnotationUtils.findFieldByNumericValue(excelDatum, index);
if (field != null) {
// 获取getter方法
Method getter = AnnotationUtils.findGetterMethod(field, excelDatum.getClass());
if (getter != null) {
// 调用getter方法获取值
try {
key.append((String) getter.invoke(excelDatum));
} catch (IllegalAccessException | InvocationTargetException ignored) {
}
}
}
}
excelDataMap.put(key.toString(), excelDatum);
} }
//excelDataMap中的value传给afterDeduplication
excelDataMap.forEach(
(key, excelDatum) -> afterDeduplication.add(excelDatum)
);
excelDataService.saveBatch(afterDeduplication);
} else {
excelDataService.saveBatch(excelData);
} }
//保存账户 //保存账户
String userName = trimChineseNameFromFilename(folderName); String userName = trimChineseNameFromFilename(folderName);
groupedMap.forEach((paymentAccount, paymentBankName) -> groupedMap.forEach((paymentAccount, paymentBankName) -> userAccountList.add(UserAccountEntity.builder().userName(userName).account(paymentBankName).caseId(caseId).code(paymentAccount).build()));
userAccountList.add(UserAccountEntity.builder()
.userName(userName)
.account(paymentBankName)
.caseId(caseId)
.code(paymentAccount)
.build()));
userAccountService.saveUserBatch(userAccountList); userAccountService.saveUserBatch(userAccountList);
if (StringUtils.isNotEmpty(folderName)) { if (StringUtils.isNotEmpty(folderName)) {
Map<String, String> idCardInfo = IdCardUtils.parseIdCardInfo(folderName); Map<String, String> idCardInfo = IdCardUtils.parseIdCardInfo(folderName);
if (!ObjectUtils.isEmpty(idCardInfo)) { if (!ObjectUtils.isEmpty(idCardInfo)) {
userService.save(UserEntity.builder().userName(userName) userService.save(UserEntity.builder().userName(userName).age(Integer.parseInt(idCardInfo.get("age"))).idCard(idCardInfo.get("idCard")).sex(idCardInfo.get("sex")).idType("身份证").caseId(caseId).build());
.age(Integer.parseInt(idCardInfo.get("age")))
.idCard(idCardInfo.get("idCard"))
.sex(idCardInfo.get("sex"))
.idType("身份证")
.caseId(caseId)
.build());
} }
} }
if (errorCount == 0) { if (errorCount == 0) {
return Result.data(fileUploadList); return Result.data(fileUploadList);
} else { } else {
return Result.data(AnalysisVO.builder() return Result.data(AnalysisVO.builder().folderCount(1).totalCount(totalCount).errorCount(errorCount).detail(detail).build());
.folderCount(1)
.totalCount(totalCount)
.errorCount(errorCount)
.detail(detail)
.build());
} }
} }
...@@ -205,7 +220,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -205,7 +220,6 @@ public class AnalysisServiceImpl implements AnalysisService {
// 正则表达式匹配连续的中文字符 // 正则表达式匹配连续的中文字符
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+"); Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+");
Matcher matcher = pattern.matcher(filename); Matcher matcher = pattern.matcher(filename);
// 只获取第一组匹配的中文 // 只获取第一组匹配的中文
if (matcher.find()) { if (matcher.find()) {
return matcher.group(); return matcher.group();
...@@ -268,36 +282,29 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -268,36 +282,29 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; return false;
} }
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, List<UserAccountEntity> userAccountList, Map<String, String> groupedMap) throws IOException { private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, Map<String, String> groupedMap, List<ExcelDataEntity> excelData) throws IOException {
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度 //获取sheet长度
int sheetLength = sheet.getLastRowNum(); int sheetLength = sheet.getLastRowNum();
List<ExcelDataEntity> excelData = Lists.newArrayList();
for (int i = 1; i < sheetLength; i++) { for (int i = 1; i < sheetLength; i++) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
//处理excel数据 //处理excel数据
ExcelDataEntity excelDataEntity = handleExcelData(tableHeaderList, row, folderName, fileId, caseId); ExcelDataEntity excelDataEntity = handleExcelData(tableHeaderList, row, folderName, fileId, caseId);
excelData.add(excelDataEntity); excelData.add(excelDataEntity);
} }
if (StringUtils.isNotEmpty(folderName)) { if (StringUtils.isNotEmpty(folderName)) {
if (!excelData.isEmpty()) { if (!excelData.isEmpty()) {
//excelData转化成以paymentAccount为键paymentBankName为值的map //excelData转化成以paymentAccount为键paymentBankName为值的map
for (ExcelDataEntity excelDatum : excelData) { for (ExcelDataEntity excelDatum : excelData) {
if("出账".equals(excelDatum.getDirection())){ if ("出账".equals(excelDatum.getDirection())) {
groupedMap.put(excelDatum.getPaymentAccount(), excelDatum.getPaymentBankName()); groupedMap.put(excelDatum.getPaymentAccount(), excelDatum.getPaymentBankName());
}else if("入账".equals(excelDatum.getDirection())){ } else if ("入账".equals(excelDatum.getDirection())) {
groupedMap.put(excelDatum.getPayeeAccount(), excelDatum.getPayeeBankCard()); groupedMap.put(excelDatum.getPayeeAccount(), excelDatum.getPayeeBankCard());
} }
} }
} }
} }
//保存excel数据
excelDataService.saveBatch(excelData);
} }
...@@ -319,7 +326,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -319,7 +326,6 @@ public class AnalysisServiceImpl implements AnalysisService {
if (cell == null) { if (cell == null) {
return ""; return "";
} }
switch (cell.getCellType()) { switch (cell.getCellType()) {
case STRING: case STRING:
return cell.getStringCellValue(); return cell.getStringCellValue();
......
package com.platform.utils;
import com.platform.annotation.NumericValue;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class AnnotationUtils {
/**
* 获取带有指定NumericValue值的字段
* @param target 目标对象
* @param value 要查找的注解值
* @return 匹配的字段,如果没有找到返回null
*/
public static Field findFieldByNumericValue(Object target, int value) {
for (Field field : target.getClass().getDeclaredFields()) {
NumericValue annotation = field.getAnnotation(NumericValue.class);
if (annotation != null && annotation.value() == value) {
return field;
}
}
return null;
}
/**
* 获取字段的getter方法
* @param field 目标字段
* @param clazz 目标类
* @return getter方法,如果没有找到返回null
*/
public static Method findGetterMethod(Field field, Class<?> clazz) {
String fieldName = field.getName();
String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
return clazz.getMethod(getterName);
} catch (NoSuchMethodException e) {
// 尝试boolean类型的is前缀
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
getterName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
return clazz.getMethod(getterName);
} catch (NoSuchMethodException ex) {
return null;
}
}
return null;
}
}
}
\ No newline at end of file
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