工具类,案件压缩包导入优化

This commit is contained in:
18792927508
2023-12-12 16:22:41 +08:00
parent 5730bb12a7
commit a78e3e23d6
28 changed files with 1876 additions and 1096 deletions
@@ -117,4 +117,11 @@ public interface SysDeptMapper
public int deleteDeptById(Long deptId);
List<Long> selectUserDeptListByRoleId(@Param("roleId")Long roleId);
/**
* 批量新增
* @param sysDepts
* @return
*/
int batchSave(@Param("list")List<SysDept> sysDepts);
}
@@ -165,5 +165,10 @@ public interface SysUserMapper
List<SysUser> selectRoleUserByDeptId(@Param("deptId")Long deptId,@Param("roleId") Long roleId );
/**
* 批量新增用户
* @param addUsers
* @return
*/
int batchSave(@Param("list")List<SysUser> addUsers);
}
@@ -44,4 +44,6 @@ public interface CaseApplicationLogMapper {
void batchDeleteLog(@Param("ids") List<Long> ids);
CaseApplication selectBeforeCase(@Param("caseId") Long caseId, @Param("version")Integer version);
Integer batchSave(@Param("list")List<CaseApplication> caseApplications);
}
@@ -125,4 +125,10 @@ public interface CaseApplicationMapper {
*/
Integer selectBatchNumberLike();
/**
* 批量新增案件
* @param caseApplications
* @return
*/
int batchSave(@Param("list")List<CaseApplication> caseApplications);
}
@@ -27,4 +27,5 @@ public interface CaseAttachLogMapper {
CaseAttach queryAnnexById(Integer annexId);
Integer batchSave(@Param("list")List<CaseAttach> caseAttaches);
}
@@ -15,7 +15,7 @@ public interface ColumnValueLogMapper {
/**
* 批量新增
*/
void batchSave(@Param("list") List<ColumnValue> list);
int batchSave(@Param("list") List<ColumnValue> list);
void batchUpdate(@Param("list") List<ColumnValue> list);
/**
@@ -17,7 +17,7 @@ public interface ColumnValueMapper {
/**
* 批量新增
*/
void batchSave(@Param("list") List<ColumnValue> list);
int batchSave(@Param("list") List<ColumnValue> list);
/**
* 根据案件id查询字段及值
@@ -32,4 +32,11 @@ public interface IAdjudicationService {
* @return
*/
AjaxResult emailByCaseId(Long id);
/**
* 批量生成裁决书
* @param ids
* @return
*/
AjaxResult batchDocument(List<Long> ids);
}
@@ -19,7 +19,7 @@ public interface ICaseApplicationService {
List<CaseApplication> selectCaseApplicationListByRole(CaseApplication caseApplication);
int insertcaseApplication(CaseApplication caseApplication, List<ColumnValue> columnValueList);
int insertcaseApplication(CaseApplication caseApplication);
int selectCaseApplicationCount(CaseApplication caseApplication);
@@ -11,6 +11,8 @@ import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.thread.MultipleThreadListParam;
import com.ruoyi.common.utils.thread.MultipleThreadWorkUtil;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.wisdomarbitrate.domain.vo.BookSendVO;
import com.ruoyi.wisdomarbitrate.domain.vo.ColumnValue;
@@ -49,6 +51,7 @@ import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -1252,6 +1255,33 @@ public class AdjudicationServiceImpl implements IAdjudicationService {
}
return AjaxResult.success(bookSendVO);
}
private void setExecList(List<MultipleThreadListParam> execList, List<ColumnValue> columnValueList){
if(CollectionUtil.isNotEmpty(columnValueList)){
Function<List<ColumnValue>,Integer> function= columnValueMapper::batchSave;
execList.add(new MultipleThreadListParam(function,columnValueList));
}
}
@Transactional
@Override
public AjaxResult batchDocument(List<Long> ids) {
// todo 多线程生成裁决书
// List<MultipleThreadListParam> execList=new ArrayList<>();
// if(CollectionUtil.isNotEmpty(columnValueList)) {
// setExecList(execList, columnValueList);
// }
//
// if(CollectionUtil.isNotEmpty(execList)){
// MultipleThreadWorkUtil.execListFun(execList.toArray(new MultipleThreadListParam[execList.size()]));
// }
for (Long id : ids) {
CaseApplication caseApplication = new CaseApplication();
caseApplication.setId(id);
createDocument(caseApplication);
}
return AjaxResult.success();
}
public String getNewEquipmentNo() {
Object awardNum = redisCache.getCacheObject("awardNum");
@@ -0,0 +1,328 @@
package com.ruoyi.wisdomarbitrate.service.impl;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SpringUtil;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @author wangqiong
* @description excel导入校验
* @date 2023-12-11 11:45
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CaseImportValid {
private CaseApplication caseApplication;
private Map<String, Long> deptMap;
// 手机号正则
private static final Pattern TELEPHONE_REGX = Pattern.compile("^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$");
// 邮箱正则
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
private static SysUserMapper sysUserMapper= SpringUtil.getBean(SysUserMapper.class);
/**
* 导入校验
*
* @param caseApplication
* @param
*/
public void importValid(CaseApplication caseApplication, Map<String, Long> deptMap) {
StringBuilder failureMsg = new StringBuilder();
caseApplication.setErrorMsg(failureMsg);
// 校验基本字段
validBaseColumn(caseApplication, failureMsg);
// 校验申请人信息
validApplicationColumn(caseApplication, failureMsg);
// 校验申请人代理信息
validApplicationAgentColumn(caseApplication, failureMsg, deptMap);
// 校验被申请人信息
validDebtorApplicationColumn(caseApplication, failureMsg);
// 校验被申请人代理信息
validDebtorApplicationAgentColumn(caseApplication, failureMsg);
}
/**
* 校验被申请人代理信息
*
* @param caseApplication
* @param failureMsg
*/
private void validDebtorApplicationAgentColumn(CaseApplication caseApplication, StringBuilder failureMsg) {
if (StrUtil.isEmpty(caseApplication.getDebtorNameAgent())) {
failureMsg.append("【被申请人主体信息-代理人姓名】字段不能为空;");
} else if (caseApplication.getDebtorNameAgent().length() > 50) {
failureMsg.append("【被申请人主体信息-代理人姓名】字段超出指定长度,最大长度为50;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorIdentityNumAgent())) {
failureMsg.append("【被申请人主体信息-代理人身份证号】字段不能为空;");
} else if (!IdcardUtil.isValidCard((caseApplication.getDebtorIdentityNumAgent()))) {
failureMsg.append("【被申请人主体信息-代理人身份证号】不合法;");
}
String debtorContactTelphoneAgent = caseApplication.getDebtorContactTelphoneAgent();
if (StrUtil.isEmpty(debtorContactTelphoneAgent)) {
failureMsg.append("【被申请人主体信息-代理人联系电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(debtorContactTelphoneAgent).matches()) {
failureMsg.append("【被申请人主体信息-代理人联系电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorContactAddressAgent())) {
failureMsg.append("【被申请人主体信息-代理人联系地址】字段不能为空;");
} else if (caseApplication.getDebtorContactAddressAgent().length() > 50) {
failureMsg.append("【被申请人主体信息-代理人联系地址】字段超出指定长度,最大长度为50;");
}
}
/**
* 校验被申请人信息
*
* @param caseApplication
* @param failureMsg
*/
private void validDebtorApplicationColumn(CaseApplication caseApplication, StringBuilder failureMsg) {
if (StrUtil.isEmpty(caseApplication.getDebtorName())) {
failureMsg.append("【被申请人主体信息-申请人姓名】字段不能为空;");
} else if (caseApplication.getDebtorName().length() > 50) {
failureMsg.append("【被申请人主体信息-申请人姓名】字段超出指定长度,最大长度为50;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorIdentityNum())) {
failureMsg.append("【被申请人主体信息-身份证号】字段不能为空;");
} else if (!IdcardUtil.isValidCard(caseApplication.getDebtorIdentityNum())) {
failureMsg.append("【被申请人主体信息-身份证号】不合法;");
}
String debtorContactTelphone = caseApplication.getDebtorContactTelphone();
if (StrUtil.isEmpty(debtorContactTelphone)) {
failureMsg.append("【被申请人主体信息-联系电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(debtorContactTelphone).matches()) {
failureMsg.append("【被申请人主体信息-联系电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorContactAddress())) {
failureMsg.append("【被申请人主体信息-联系地址】字段不能为空;");
} else if (caseApplication.getDebtorContactAddress().length() > 50) {
failureMsg.append("【被申请人主体信息-联系地址】字段超出指定长度,最大长度为50;");
}
String debtorWorkTelphone = caseApplication.getDebtorWorkTelphone();
if (StrUtil.isEmpty(debtorWorkTelphone)) {
failureMsg.append("【被申请人主体信息-单位电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(debtorWorkTelphone).matches()) {
failureMsg.append("【被申请人主体信息-单位电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorWorkAddress())) {
failureMsg.append("【被申请人主体信息-单位地址】字段不能为空;");
} else if (caseApplication.getDebtorWorkAddress().length() > 50) {
failureMsg.append("【被申请人主体信息-单位地址】字段超出指定长度,最大长度为50;");
}
if (StrUtil.isEmpty(caseApplication.getResponSex())) {
failureMsg.append("【被申请人主体信息-性别】字段不能为空;");
} else if (caseApplication.getResponSex().length() > 1) {
failureMsg.append("【被申请人主体信息-性别】字段超出指定长度,最大长度为1;");
}
if (caseApplication.getResponBirth() == null) {
failureMsg.append("【被申请人主体信息-出生年月日】字段不合法;");
} else if (caseApplication.getResponBirth().after(new Date())) {
failureMsg.append("【被申请人主体信息-出生年月日】字段不合法,不能超过当前日期;");
}
if (StrUtil.isEmpty(caseApplication.getDebtorEmail())) {
failureMsg.append("【被申请人主体信息-邮箱】字段不能为空;");
} else if (!EMAIL_PATTERN.matcher(caseApplication.getDebtorEmail()).matches()) {
failureMsg.append("【被申请人主体信息-邮箱】字段不合法;");
}
}
/**
* 校验申请人代理信息
*
* @param caseApplication
* @param failureMsg
*/
private void validApplicationAgentColumn(CaseApplication caseApplication, StringBuilder failureMsg, Map<String, Long> deptMap) {
if (StrUtil.isEmpty(caseApplication.getNameAgent())) {
failureMsg.append("【申请人主体信息-代理人姓名】字段不能为空;");
} else if (caseApplication.getNameAgent().length() > 50) {
failureMsg.append("【申请人主体信息-代理人姓名】字段超出指定长度,最大长度为50;");
}
if (StrUtil.isEmpty(caseApplication.getIdentityNumAgent())) {
failureMsg.append("【申请人主体信息-代理人身份证号】字段不能为空;");
} else if (!IdcardUtil.isValidCard(caseApplication.getIdentityNumAgent())) {
failureMsg.append("【申请人主体信息-代理人身份证号】不合法;");
}
validAgentInfo(caseApplication, failureMsg, deptMap);
String contactTelphoneAgent = caseApplication.getContactTelphoneAgent();
if (StrUtil.isEmpty(contactTelphoneAgent)) {
failureMsg.append("【申请人主体信息-代理人联系电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(contactTelphoneAgent).matches()) {
failureMsg.append("【申请人主体信息-代理人联系电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getContactAddressAgent())) {
failureMsg.append("【申请人主体信息-代理人联系地址】字段不能为空;");
} else if (caseApplication.getContactAddressAgent().length() > 50) {
failureMsg.append("【申请人主体信息-代理人联系地址】字段超出指定长度,最大长度为50;");
}
}
/**
* 校验代理人与组织机构关系
*
* @param caseApplication
* @param failureMsg
* @return
*/
private void validAgentInfo(CaseApplication caseApplication, StringBuilder failureMsg, Map<String, Long> deptMap) {
// 申请机构与代理人都不为空,校验代理人与组织机构关系(代理人必须在该部门下)
if (StrUtil.isNotEmpty(caseApplication.getName()) && StrUtil.isNotEmpty(caseApplication.getNameAgent())) {
String applicationOrganId = "";
// 申请机构已经存在
if (deptMap.containsKey(caseApplication.getName())) {
applicationOrganId = String.valueOf(deptMap.get(caseApplication.getName()));
}
// 根据代理人身份证去用户表查询
SysUser agentUser = sysUserMapper.selectUserByIdCard(caseApplication.getIdentityNumAgent());
// 代理人的部门和申请机构不匹配
if (null != agentUser && null != agentUser.getDeptId() && !String.valueOf(agentUser.getDeptId()).equals(applicationOrganId)) {
// return "该申请代理人已在"+agentUser.getDeptName()+"申请机构下存在,请检查填写信息是否正确";
if (null != agentUser.getDept() && StrUtil.isNotEmpty(agentUser.getDept().getDeptName())) {
failureMsg.append("该申请代理人已在【").append(agentUser.getDept().getDeptName()).append("】申请机构下存在,请检查填写信息是否正确");
} else {
failureMsg.append("该申请代理人已存在,与申请机构不匹配,请检查填写信息是否正确");
}
}
}
}
/**
* 校验申请人主题信息
*
* @param caseApplication
* @param failureMsg
*/
private void validApplicationColumn(CaseApplication caseApplication, StringBuilder failureMsg) {
if (StrUtil.isEmpty(caseApplication.getName())) {
failureMsg.append("【申请人主体信息-申请人(机构)】字段不能为空;");
} else if (caseApplication.getName().length() > 20) {
failureMsg.append("【申请人主体信息-申请人(机构)】字段超出指定长度,最大长度为20;");
}
if (StrUtil.isNotEmpty(caseApplication.getIdentityNum()) && caseApplication.getIdentityNum().length() > 50) {
failureMsg.append("【申请人主体信息-代码】字段超出指定长度,最大长度为50;");
}
String contactTelphone = caseApplication.getContactTelphone();
if (StrUtil.isEmpty(contactTelphone)) {
failureMsg.append("【申请人主体信息-联系电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(contactTelphone).matches()) {
failureMsg.append("【申请人主体信息-联系电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getContactAddress())) {
failureMsg.append("【申请人主体信息-联系地址】字段不能为空;");
} else if (caseApplication.getName().length() > 50) {
failureMsg.append("【申请人主体信息-联系地址】字段超出指定长度,最大长度为50;");
}
String workTelphone = caseApplication.getWorkTelphone();
if (StrUtil.isEmpty(workTelphone)) {
failureMsg.append("【申请人主体信息-单位电话】字段不能为空;");
} else if (!TELEPHONE_REGX.matcher(workTelphone).matches()) {
failureMsg.append("【申请人主体信息-单位电话】字段不合法;");
}
if (StrUtil.isEmpty(caseApplication.getWorkAddress())) {
failureMsg.append("【申请人主体信息-单位地址】字段不能为空;");
} else if (caseApplication.getWorkAddress().length() > 50) {
failureMsg.append("【申请人主体信息-单位地址】字段超出指定长度,最大长度为50;");
}
if (StrUtil.isEmpty(caseApplication.getEmail())) {
failureMsg.append("【申请人主体信息-邮箱】字段不能为空;");
} else if (!EMAIL_PATTERN.matcher(caseApplication.getEmail()).matches()) {
failureMsg.append("【申请人主体信息-邮箱】字段不合法;");
}
}
/**
* 校验基本字段
*
* @param caseApplication
* @param failureMsg
*/
private void validBaseColumn(CaseApplication caseApplication, StringBuilder failureMsg) {
if (StrUtil.isEmpty(caseApplication.getCaseName())) {
failureMsg.append("【案件名称】字段不能为空;");
} else if (caseApplication.getCaseName().length() > 50) {
failureMsg.append("【案件名称】字段超出指定长度,最大长度为50;");
}
BigDecimal caseSubjectAmount = caseApplication.getCaseSubjectAmount();
if (null == caseSubjectAmount) {
failureMsg.append("【案件标的】字段不合法;");
} else {
if (caseSubjectAmount.compareTo(new BigDecimal("0")) < 0 || caseSubjectAmount.compareTo(new BigDecimal("99999999.99")) > 0) {
failureMsg.append("【案件标的】字段超出范围,范围为[0,100000000);");
}
if (caseSubjectAmount.scale() > 2) {
failureMsg.append("【案件标的】字段超出指定精度(10^-2);");
}
}
if (caseApplication.getLoanStartDate() == null) {
failureMsg.append("【借款开始日期】字段不合法;");
}
if (caseApplication.getLoanEndDate() == null) {
failureMsg.append("【借款结束日期】字段不合法;");
}
if (caseApplication.getLoanStartDate() != null && caseApplication.getLoanEndDate() != null && caseApplication.getLoanStartDate().after(caseApplication.getLoanEndDate())) {
failureMsg.append("【借款结束日期】不能早于【借款开始日期】;");
}
if (StrUtil.isEmpty(caseApplication.getContractNumber())) {
failureMsg.append("【合同编号】字段不能为空;");
} else if (caseApplication.getContractNumber().length() > 50) {
failureMsg.append("【合同编号】字段超出指定长度,最大长度为50;");
}
BigDecimal claimPrinciOwed = caseApplication.getClaimPrinciOwed();
if (null == claimPrinciOwed) {
failureMsg.append("【申请人主张欠本金】字段不合法;");
} else {
if (claimPrinciOwed.compareTo(new BigDecimal("0")) < 0 || claimPrinciOwed.compareTo(new BigDecimal("99999999.99")) > 0) {
failureMsg.append("【申请人主张欠本金】字段超出范围,范围为[0,100000000);");
}
if (claimPrinciOwed.scale() > 2) {
failureMsg.append("【申请人主张欠本金】字段超出指定精度(10^-2);");
}
}
BigDecimal claimInterestOwed = caseApplication.getClaimInterestOwed();
if (null == claimInterestOwed) {
failureMsg.append("【申请人主张欠利息】字段不合法;");
} else {
if (claimInterestOwed.compareTo(new BigDecimal("0")) < 0 || claimInterestOwed.compareTo(new BigDecimal("99999999.99")) > 0) {
failureMsg.append("【申请人主张欠利息】字段超出范围,范围为[0,100000000);");
}
if (claimInterestOwed.scale() > 2) {
failureMsg.append("【申请人主张欠利息】字段超出指定精度(10^-2);");
}
}
BigDecimal claimLiquidDamag = caseApplication.getClaimLiquidDamag();
if (null == claimLiquidDamag) {
failureMsg.append("【申请人主张违约金】字段不合法;");
} else {
if (claimLiquidDamag.compareTo(new BigDecimal("0")) < 0 || claimLiquidDamag.compareTo(new BigDecimal("99999999.99")) > 0) {
failureMsg.append("【申请人主张违约金】字段超出范围,范围为[0,100000000);");
}
if (claimLiquidDamag.scale() > 2) {
failureMsg.append("【申请人主张违约金】字段超出指定精度(10^-2);");
}
}
if (StrUtil.isEmpty(caseApplication.getArbitratClaims())) {
failureMsg.append("【申请人仲裁请求及事实和理由】字段不能为空;");
} else if (caseApplication.getArbitratClaims().length() > 10000) {
failureMsg.append("【申请人仲裁请求及事实和理由】字段超出指定长度,最大长度为10000;");
}
if (StrUtil.isNotEmpty(caseApplication.getArbitratClaims()) && caseApplication.getArbitratClaims().length() > 10000) {
failureMsg.append("【申请人请求仲裁庭裁决】字段超出指定长度,最大长度为10000;");
}
}
}
@@ -0,0 +1,811 @@
package com.ruoyi.wisdomarbitrate.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.CaseApplicationConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UpdateSubmitStatus;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.thread.MultipleThreadListParam;
import com.ruoyi.common.utils.thread.MultipleThreadWorkUtil;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.*;
import com.ruoyi.wisdomarbitrate.domain.CaseAffiliate;
import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
import com.ruoyi.wisdomarbitrate.domain.CaseAttach;
import com.ruoyi.wisdomarbitrate.domain.FatchRule;
import com.ruoyi.wisdomarbitrate.domain.vo.ColumnValue;
import com.ruoyi.wisdomarbitrate.mapper.*;
import com.ruoyi.wisdomarbitrate.utils.OCRUtils;
import com.ruoyi.wisdomarbitrate.utils.UnZipFileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;
/**
* @author wangqiong
* @description 案件压缩包导入
* @date 2023-12-11 11:45
*/
@Service
public class CaseZipImportImpl {
@Autowired
private CaseApplicationServiceImpl caseApplicationService;
@Autowired
private FatchRuleMapper fatchRuleMapper;
@Autowired
private SysDictDataMapper dictDataMapper;
@Autowired
private CaseApplicationMapper caseApplicationMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private SysRoleMapper roleMapper;
@Autowired
private SysUserMapper userMapper;
@Autowired
private SysUserRoleMapper userRoleMapper;
@Autowired
private CaseApplicationLogMapper caseApplicationLogMapper;
@Autowired
private CaseAffiliateLogMapper caseAffiliateLogMapper;
@Autowired
private CaseAttachLogMapper caseAttachLogMapper;
@Autowired
private SmsRecordMapper smsRecordMapper;
@Autowired
private CaseAttachMapper caseAttachMapper;
@Autowired
private ColumnValueMapper columnValueMapper;
@Autowired
private ColumnValueLogMapper columnValueLogMapper;
@Autowired
private CaseAffiliateMapper caseAffiliateMapper;
// 申请人角色id
private long roleId;
private Integer maxCaseNum;
private Integer maxBatchNumber;
public AjaxResult zipImport(MultipartFile file, Long templateId) {
UUID uuid = UUID.randomUUID();
// todo
String targetPath = "/home/ruoyi/uploadPath/upload/unzipFile/" + uuid + "/";
// String targetPath = "D:/home/ruoyi/uploadPath/upload/unzipFile/"+uuid+ "/";
File zipFile = null;
InputStream ins = null;
try {
ins = file.getInputStream();
//上传的压缩包保存的路径
// todo
String savePath = "/home/ruoyi/uploadPath/upload/zipFile/";
// String savePath = "D:/home/ruoyi/uploadPath/upload/zipFile/";
String saveName = uuid + "_" + file.getOriginalFilename();
zipFile = new File(savePath + saveName);
inputChangeToFile(ins, zipFile);
} catch (IOException e) {
e.printStackTrace();
}
//解压缩上传的压缩包
boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath);
if (!unzipSuccess) {
// 解压失败
return AjaxResult.error("解压失败");
}
// 查询抓取规则
// todo 批次需要再上传压缩包时用户填写
List<FatchRule> fatchRuleList = fatchRuleMapper.listByTemplateId(templateId);
if (CollectionUtil.isEmpty(fatchRuleList)) {
return error("未设置抓取规则");
}
File directory = new File(targetPath);
// fileMap<caseId, List<File>>
Map<Long, List<File>> fileMap = findAndConvertPDF(directory);
if (fileMap == null || fileMap.size() <= 0) {
// 解压失败
return AjaxResult.error("未获取到文件");
}
Map<String, String> fatchMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(fatchRuleList)) {
Map<String, List<FatchRule>> fatchRuleMap = fatchRuleList.stream().collect(Collectors.groupingBy(FatchRule::getFileName));
// 根据抓取规则循环抓取
fileMap.forEach((key, fileList) -> {
if (CollectionUtil.isNotEmpty(fileList)) {
for (File caseFile : fileList) {
if (fatchRuleMap.containsKey(caseFile.getName())) {
// 抓取内容
List<FatchRule> fatchRules = fatchRuleMap.get(caseFile.getName());
getFatchContentList(caseFile, fatchMap, fatchRules, key);
}
}
}
});
}
if (fatchMap.size() <= 0) {
return error("从压缩包中未抓取到内容,请检查抓取字段配置");
}
// 新增的案件
List<CaseApplication> caseApplications = new ArrayList<>();
// 从抓取规则表取字段和字典表取基本字段,字典表的字段名塞到基本表,is_default=1自定义字段塞到columnValue
// 抓取规则,0-内置字段,1-自定义字段
Map<Integer, List<FatchRule>> fatchRuleMap = fatchRuleList.stream().collect(Collectors.groupingBy(FatchRule::getIsDefault));
// 在系统表中查询案件内置字段
SysDictData sysDictData = new SysDictData();
sysDictData.setDictType("case_built_type");
List<SysDictData> dictDataList = dictDataMapper.selectDictDataList(sysDictData);
// 查询所有的组织机构,组装成map
List<SysDept> deptList = sysDeptMapper.selectDeptList(new SysDept());
// 所有部门
Map<String, Long> deptMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(deptList)) {
deptMap = deptList.stream().collect(Collectors.toMap(SysDept::getDeptName, SysDept::getDeptId, (oldV, newV) -> newV));
}
// 角色用户
List<SysUserRole> userRoleList = new ArrayList<>();
// 查询申请人角色id
roleId = roleMapper.selectRoleIdByName("申请人");
// 案件基本信息
caseApplications = new ArrayList<>();
// 自定义字段,组装columnValue表
List<ColumnValue> columnValueList = new ArrayList<>();
// 案件人员
List<CaseAffiliate> caseAffiliates = new ArrayList<>();
// 组装机构
List<SysDept> sysDepts = new ArrayList<>();
// 案件附件
List<CaseAttach> caseAttachs = new ArrayList<>();
/**
* 用户表已存在的用户
*/
List<SysUser> existUsers = userMapper.selectUserList(new SysUser());
Map<String, SysUser> userMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(existUsers)) {
userMap = existUsers.stream().collect(Collectors.toMap(SysUser::getPhonenumber, Function.identity(), (n1, n2) -> n2));
}
//查询出当天的案件编号的最大值
String currentDay = DateUtils.dateTime();
String caseNum = "zc" + currentDay;
maxCaseNum = caseApplicationMapper.selectCaseNumLike(caseNum, caseNum.length());
// 需要新增的用户
List<SysUser> addUsers = new ArrayList<>();
for (Long caseId : fileMap.keySet()) {
if (CollectionUtil.isEmpty(fileMap.get(caseId))) {
continue;
}
CaseApplication caseApplication = new CaseApplication();
caseApplications.add(caseApplication);
caseApplication.setId(caseId);
caseApplication.setTemplateId(templateId);
caseApplication.setCaseStatus(CaseApplicationConstants.CASE_APPLICATION);
caseApplication.setCaseLogId(IdWorkerUtil.getId());
caseApplication.setUpdateSubmitStatus(UpdateSubmitStatus.UNCOMMITTED.getCode());
caseApplication.setCaseAppliId(caseApplication.getId());
//默认案件标的 todo 案件标的是什么,默认写死
caseApplication.setCaseSubjectAmount(new BigDecimal(100000));
//todo 暂时设置计费比率为0.01
BigDecimal feeRate = new BigDecimal(0.01);
BigDecimal feePayable = caseApplication.getCaseSubjectAmount().multiply(feeRate).setScale(2, BigDecimal.ROUND_HALF_UP);
caseApplication.setFeePayable(feePayable);
// 设置批号
if (StrUtil.isEmpty(caseApplication.getBatchNumber())) {
maxBatchNumber = caseApplicationMapper.selectBatchNumberLike();
if (maxBatchNumber == null) {
maxBatchNumber=1;
caseApplication.setBatchNumber(maxBatchNumber.toString());
} else {
maxBatchNumber=maxBatchNumber+1;
caseApplication.setBatchNumber( maxBatchNumber.toString());
}
}
// 设置编号
String maxCaseNumStr=generateCaseNum();
caseApplication.setCaseNum(maxCaseNumStr);
caseApplication.setCreateBy(getUsername());
caseApplication.setVersion(1);
// 组装案件内置字段主表内容
if (fatchRuleMap.size() > 0 && fatchRuleMap.containsKey(1)) {
List<FatchRule> columnRules = fatchRuleMap.get(1);
columnRules.forEach(columnRule -> {
ColumnValue columnValue = new ColumnValue();
columnValue.setColumn(columnRule.getColumn());
columnValue.setName(columnRule.getColumnName());
columnValue.setName(columnRule.getColumnName());
columnValue.setValue(fatchMap.get(columnRule.getColumnName() + Constants.PDFSTR + caseId));
columnValue.setIsDefault(1);
columnValue.setCaseId(caseId);
columnValue.setCaseAppliLogId(caseApplication.getCaseLogId());
columnValueList.add(columnValue);
});
}
caseApplication.setColumnValues(columnValueList);
// 组装内置字段
buildDefaultColumn(caseApplication, dictDataList, fatchMap, caseAffiliates, deptMap, sysDepts, userMap, addUsers, userRoleList);
for (File caseFile : fileMap.get(caseId)) {
String fileUrl = caseFile.getAbsolutePath();
if (StrUtil.isEmpty(fileUrl)) {
continue;
}
// 上传
String filePath = RuoYiConfig.getUploadPath();
CaseAttach caseAttach = new CaseAttach();
caseAttach.setCaseAppliId(caseApplication.getId());
caseAttach.setCaseAppliLogId(caseApplication.getCaseLogId());
caseAttach.setAnnexPath(filePath);
if (StrUtil.isNotEmpty(fileUrl)) {
String fileName = fileUrl.replace(filePath, "/profile/upload");
caseAttach.setAnnexName(fileName);
}
// 申请人提供的证据材料
caseAttach.setAnnexType(2);
caseAttachs.add(caseAttach);
if (fileUrl.contains("仲裁申请书")) {
CaseAttach applyFile = new CaseAttach();
BeanUtil.copyProperties(caseAttach, applyFile);
applyFile.setAnnexType(1);
caseAttachs.add(applyFile);
}
}
// 案件压缩包导入
caseApplication.setImportFlag(2);
}
// 多线程执行
List<MultipleThreadListParam> execList=new ArrayList<>();
if (CollectionUtil.isNotEmpty(addUsers)) {
Function<List<SysUser>,Integer> function=userMapper::batchSave;
execList.add(new MultipleThreadListParam(function,addUsers));
}
if (CollectionUtil.isNotEmpty(userRoleList)) {
Function<List<SysUserRole>,Integer> function=userRoleMapper::batchUserRole;
execList.add(new MultipleThreadListParam(function,userRoleList));
}
if (CollectionUtil.isNotEmpty(sysDepts)) {
Function<List<SysDept>,Integer> function=sysDeptMapper::batchSave;
execList.add(new MultipleThreadListParam(function,sysDepts));
}
if (CollectionUtil.isNotEmpty(caseApplications)) {
Function<List<CaseApplication>,Integer> function=caseApplicationMapper::batchSave;
execList.add(new MultipleThreadListParam(function,caseApplications));
Function<List<CaseApplication>,Integer> functionLog=caseApplicationLogMapper::batchSave;
execList.add(new MultipleThreadListParam(functionLog,caseApplications));
}
if (CollectionUtil.isNotEmpty(caseAffiliates)) {
Function<List<CaseAffiliate>,Integer> function=caseAffiliateMapper::batchCaseAffiliate;
execList.add(new MultipleThreadListParam(function,caseAffiliates));
Function<List<CaseAffiliate>,Integer> functionLog=caseAffiliateLogMapper::batchCaseAffiliate;
execList.add(new MultipleThreadListParam(functionLog,caseAffiliates));
}
if (CollectionUtil.isNotEmpty(caseAttachs)) {
Function<List<CaseAttach>,Integer> function=caseAttachMapper::batchSave;
execList.add(new MultipleThreadListParam(function,caseAttachs));
Function<List<CaseAttach>,Integer> functionLog=caseAttachLogMapper::batchSave;
execList.add(new MultipleThreadListParam(functionLog,caseAttachs));
}
if (CollectionUtil.isNotEmpty(columnValueList)) {
Function<List<ColumnValue>,Integer> function=columnValueMapper::batchSave;
execList.add(new MultipleThreadListParam(function,columnValueList));
Function<List<ColumnValue>,Integer> functionLog=columnValueLogMapper::batchSave;
execList.add(new MultipleThreadListParam(functionLog,columnValueList));
}
if(CollectionUtil.isNotEmpty(execList)){
MultipleThreadWorkUtil.execListFun(execList.toArray(new MultipleThreadListParam[execList.size()]));
}
return success("导入成功");
}
/**
* 获取自动编码
*
* @return
*/
public String generateCaseNum() {
// 自动编码格式 zc+yyyyMMdd+001
String currentDay = DateUtils.dateTime();
String caseNum = "zc" + currentDay;
if (null == maxCaseNum) {
maxCaseNum=1;
caseNum = caseNum + "001";
} else {
maxCaseNum=maxCaseNum+1;
caseNum = caseNum + String.format("%03d", maxCaseNum);
}
return caseNum;
}
public void inputChangeToFile(InputStream instream, File file) {
try {
OutputStream outStr = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = instream.read(buffer, 0, 1024)) != -1) {
outStr.write(buffer, 0, bytesRead);
}
outStr.flush();
outStr.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查找文件
*
* @param directory
* @param
* @return
*/
public static Map<Long, List<File>> findAndConvertPDF(File directory) {
// caseMap<caseId,Map<fileName,filePath>>
Map<Long, List<File>> caseMap = new HashMap<>();
if (directory.isFile()) {
String path = "";
// 如果传入的参数是一个文件
path = directory.getAbsolutePath();
List<File> fileList = new ArrayList<>();
fileList.add(directory);
caseMap.put(IdWorkerUtil.getId(), fileList);
} else if (directory.isDirectory()) {
searchAndConvertPDF(directory, caseMap, 1, new HashMap<>());
} else {
return null;
}
return caseMap;
}
public static boolean isPDF(File file) {
String extension = FileUtils.getFileExtension(file);
return extension.equalsIgnoreCase("pdf");
}
/**
* 递归查找文件夹
*
* @param directory
* @param caseMap<caseId,List<file>>
* @param i 第几层文件夹
* @param fileMap<filePath,caseId>>
*/
public static void searchAndConvertPDF(File directory, Map<Long, List<File>> caseMap, int i, Map<String, Long> fileMap) {
File[] files = directory.listFiles();
// 约定压缩包第二层一个文件夹为一个案件
if (files != null) {
if (i == 2) {
for (File file : files) {
fileMap.put(file.getAbsolutePath(), IdWorkerUtil.getId());
}
}
i++;
for (File file : files) {
if (file.getName().contains("zip") || file.getName().contains("rar")) {
continue;
}
if (file.isFile()) {
for (Map.Entry<String, Long> entry : fileMap.entrySet()) {
// 为同一个案件
if (!file.getAbsolutePath().contains(entry.getKey())) {
continue;
}
List<File> fileList;
if (caseMap.containsKey(entry.getValue())) {
fileList = caseMap.get(entry.getValue());
} else {
fileList = new ArrayList<>();
}
fileList.add(file);
caseMap.put(entry.getValue(), fileList);
}
} else if (file.isDirectory()) {
// 如果是目录,递归查找
searchAndConvertPDF(file, caseMap, i, fileMap);
}
}
}
}
private static int getFileNumPage(String pdfUrl) {
File pdfFile = new File(pdfUrl);
int pageCount = 0;
try (PDDocument document = PDDocument.load(pdfFile)) {
pageCount = document.getNumberOfPages();
} catch (IOException e) {
e.printStackTrace();
}
return pageCount;
}
/**
* 获取模板和正文中替换符的内容
*
* @param a
* @param b
* @return
*/
public static List<String> getReplaceList(String a, String b) {
String aTmpe = filterString(a);
String bTmpe = filterString(b);
String regex = "(\\{[^}}]*})";
String[] ptTemplate = aTmpe.replaceAll(regex, "@=").split("@=");
String replace = "";
for (int i = 0; i < ptTemplate.length; i++) {
if (ptTemplate[i] == null || ptTemplate[i].equals(" ")) continue;
if (replace.equals("")) {
replace = bTmpe.replace(ptTemplate[i], "@=");
} else {
replace = replace.replace(ptTemplate[i], "@=");
}
}
List<String> aList = new ArrayList<>();
String[] split = replace.split("@=");
for (int i = 0; i < split.length; i++) {
if (split[i] == "" || split[i].equals("")) continue;
aList.add(split[i]);
}
return aList;
}
// 去掉内容中的换行符
public static String filterString(String str) {
if (str == null || str.equals("")) {
return null;
}
String regEx = "[\\r\\n]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll(" ").trim();
}
// 检索时,转换特殊字符
public static String escapeQueryChars(String s) {
if (StringUtils.isBlank(s)) {
return s;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
|| c == ':' || c == '^' || c == '[' || c == ']' || c == '\"'
|| c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
|| c == '|' || c == '&' || c == ';' || c == '/' || c == '.'
|| c == '$' || Character.isWhitespace(c)) {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}
/**
* 组装内置字段
*
* @param caseApplication 案件信息
* @param dictDataList 内置字段
* @param fatchMap 抓取字段内容
*/
private void buildDefaultColumn(CaseApplication caseApplication, List<SysDictData> dictDataList, Map<String, String> fatchMap,
List<CaseAffiliate> caseAffiliates, Map<String, Long> deptMap, List<SysDept> sysDepts,
Map<String, SysUser> userMap, List<SysUser> addUsers, List<SysUserRole> userRoleList) {
// 组装内置字段
if (CollectionUtil.isEmpty(dictDataList)) {
return;
}
// 被申请人
CaseAffiliate debtorAffiliate = new CaseAffiliate();
debtorAffiliate.setCaseAppliId(caseApplication.getId());
debtorAffiliate.setCaseAppliLogId(caseApplication.getCaseLogId());
// 申请人
CaseAffiliate affiliate = new CaseAffiliate();
affiliate.setCaseAppliLogId(caseApplication.getCaseLogId());
affiliate.setCaseAppliId(caseApplication.getId());
for (SysDictData dictData : dictDataList) {
if (StrUtil.isNotEmpty(dictData.getDictLabel())) {
if (dictData.getDictLabel().contains("被申请人")) {
// 组装被申请人内置自段
buildDebtorColumn(dictData, fatchMap, debtorAffiliate,caseApplication.getId());
} else if (dictData.getDictLabel().contains("申请人") || dictData.getDictLabel().contains("统一社会信用代码")
|| dictData.getDictLabel().contains("法定代表人") || dictData.getDictLabel().contains("委托代理人")) {
// 组装申请人内置自段
buildAffilcateColumn(dictData, fatchMap, affiliate, deptMap, sysDepts, userMap, addUsers, userRoleList,caseApplication.getId());
} else if (dictData.getDictLabel().contains("合同编号")) {
// 合同编号
String contractNumber = fatchMap.get("合同编号"+ Constants.PDFSTR + caseApplication.getId());
if (StrUtil.isNotEmpty(contractNumber)) {
// 提取字母和数字
String regx = "[^a-zA-Z0-9]";
String replaceAll = contractNumber.replaceAll(regx, "");
caseApplication.setContractNumber(replaceAll.toUpperCase());
}
} else {
ObjectFieldUtils.setValue(caseApplication, dictData.getDictValue(), fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseApplication.getId()));
}
} else {
ObjectFieldUtils.setValue(caseApplication, dictData.getDictValue(), fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseApplication.getId()));
}
}
if (ObjectUtil.isNotEmpty(affiliate)) {
caseAffiliates.add(affiliate);
}
if (ObjectUtil.isNotEmpty(debtorAffiliate)) {
caseAffiliates.add(debtorAffiliate);
}
}
/**
* 组装申请人内置字段
*
* @param dictData 内置字段
* @param fatchMap 抓取内容
* @param affiliate 案件人员
*/
private void buildAffilcateColumn(SysDictData dictData, Map<String, String> fatchMap, CaseAffiliate affiliate,
Map<String, Long> deptMap, List<SysDept> sysDepts,
Map<String, SysUser> userMap, List<SysUser> addUsers, List<SysUserRole> userRoleList,Long caseId) {
affiliate.setIdentityType(1);
// 申请人
switch (dictData.getDictLabel()) {
case "申请人姓名":
affiliate.setName((fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)));
if (StrUtil.isNotEmpty(affiliate.getName())) {
// 组装申请机构
// 将组织机构id设为申请人名称
if (deptMap.containsKey(affiliate.getName())) {
affiliate.setApplicationOrganId(String.valueOf(deptMap.get(affiliate.getName())));
affiliate.setApplicationOrganName(affiliate.getName());
} else {
// 如果不存在则新增
SysDept dept = new SysDept();
dept.setParentId(0L);
dept.setDeptName(affiliate.getName());
dept.setAncestors("0");
dept.setOrderNum(1);
dept.setStatus("0");
dept.setDelFlag("0");
dept.setCreateBy(getUsername());
dept.setUpdateBy(getUsername());
dept.setDeptId(Long.valueOf(IdWorkerUtil.getId()));
sysDepts.add(dept);
deptMap.put(dept.getDeptName(), dept.getDeptId());
affiliate.setApplicationOrganId(String.valueOf(dept.getDeptId()));
affiliate.setApplicationOrganName(affiliate.getName());
}
}
break;
case "统一社会信用代码":
affiliate.setIdentityNum((fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)));
break;
case "法定代表人":
affiliate.setCompLegalPerson(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "法定代表人职位":
affiliate.setCompLegalperPost((fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)));
break;
case "申请人住所":
affiliate.setResidenAffili((fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)));
break;
case "申请人联系地址":
affiliate.setContactAddress(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "委托代理人姓名":
affiliate.setNameAgent(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "委托代理人联系电话":
affiliate.setContactTelphoneAgent(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
if (StrUtil.isNotEmpty(affiliate.getContactTelphoneAgent())) {
// 用户已存在
if (userMap.containsKey(affiliate.getContactTelphoneAgent())) {
SysUser agentUser = userMap.get(affiliate.getContactTelphoneAgent());
if (null != agentUser.getDeptId() && String.valueOf(agentUser.getDeptId()).equals(affiliate.getApplicationOrganId())) {
// 同步用户表和案件关联人表的手机号和名称
affiliate.setContactTelphoneAgent(agentUser.getPhonenumber());
affiliate.setNameAgent(agentUser.getNickName());
affiliate.setApplicantAgentUserId(String.valueOf(agentUser.getUserId()));
if (StrUtil.isNotEmpty(agentUser.getIdCard())) {
affiliate.setIdentityNumAgent(agentUser.getIdCard());
} else {
affiliate.setIdentityNumAgent(affiliate.getIdentityNumAgent());
}
List<Long> longList = new ArrayList<>();
// 新增角色为申请人
if (CollectionUtil.isNotEmpty(agentUser.getRoles())) {
longList = agentUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList());
if (!longList.contains(roleId)) {
insertAgentUserRole(agentUser, roleId, userRoleList);
}
} else {
insertAgentUserRole(agentUser, roleId, userRoleList);
}
}
} else {
// 用户不存在,新增
SysUser agentUser = new SysUser();
agentUser.setUserId(Long.valueOf(IdWorkerUtil.getId()));
agentUser.setIdCard(affiliate.getIdentityNumAgent());
agentUser.setNickName(affiliate.getNameAgent());
agentUser.setUserName(affiliate.getContactTelphoneAgent());
agentUser.setPhonenumber(affiliate.getContactTelphoneAgent());
agentUser.setPassword(SecurityUtils.encryptPassword("abc123456"));
agentUser.setDeptId(Long.valueOf(affiliate.getApplicationOrganId()));
addUsers.add(agentUser);
userMap.put(agentUser.getPhonenumber(), agentUser);
insertAgentUserRole(agentUser, roleId, userRoleList);
}
}
break;
case "委托代理人电子邮件":
affiliate.setAgentEmail(StrUtil.isNotEmpty(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)) ? fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId).replace("\n", "").replaceAll("\\s", "") : null);
break;
default:
break;
}
}
/**
* 新增角色为申请人
*
* @param agentUser
* @param roleId
*/
private void insertAgentUserRole(SysUser agentUser, Long roleId, List<SysUserRole> userRoleList) {
SysUserRole sysUserRole = new SysUserRole();
sysUserRole.setUserId(agentUser.getUserId());
sysUserRole.setRoleId(roleId);
userRoleList.add(sysUserRole);
}
/**
* 组装被申请人内置字段
*
* @param dictData 内置字段
* @param fatchMap 抓取内容
* @param debtorAffiliate 被申请人
*/
private void buildDebtorColumn(SysDictData dictData, Map<String, String> fatchMap, CaseAffiliate debtorAffiliate,Long caseId) {
debtorAffiliate.setIdentityType(2);
// 被申请人
switch (dictData.getDictLabel()) {
case "被申请人姓名":
debtorAffiliate.setName(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "被申请人身份证号":
String identityNum = fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId);
debtorAffiliate.setIdentityNum(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
// 出生年月日,从身份证抓取
if (StrUtil.isNotEmpty(identityNum)) {
identityNum = identityNum.replace("\n", "");
Map<String, String> identityNumMap = IdCardUtils.getBirAgeSex(identityNum);
String birthday = identityNumMap.get("birthday");
if (StrUtil.isNotEmpty(birthday)) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = null;
try {
birthdayDate = simpleDateFormat.parse(birthday);
} catch (Exception e) {
e.printStackTrace();
}
debtorAffiliate.setResponBirth(birthdayDate);
}
//从身份证抓取性别
debtorAffiliate.setResponSex(identityNumMap.get("sexCode"));
}
break;
case "被申请人住所":
debtorAffiliate.setResidenAffili(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "被申请人联系电话":
debtorAffiliate.setContactTelphone(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId));
break;
case "被申请人电子邮件":
debtorAffiliate.setEmail(StrUtil.isNotEmpty(fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId)) ? fatchMap.get(dictData.getDictLabel()+ Constants.PDFSTR + caseId).replace("\n", "").replaceAll("\\s", "") : null);
break;
default:
break;
}
}
/**
* 获取抓取内容
*
* @param fatchRules 抓取规则
*/
private void getFatchContentList(File caseFile, Map<String, String> fatchMap, List<FatchRule> fatchRules, Long caseId) {
String fileURL = caseFile.getAbsolutePath();
if (fileURL.endsWith("txt")) {
String readerFile = ReadFileUtils.readerTxtFile(fileURL);
OCRUtils.fatchRuleGetContent(readerFile, fatchRules, fatchMap, caseId);
} else if (fileURL.endsWith("doc") || fileURL.endsWith("docx")) {
// doc,docx,text识别内容
String readerFile = null;
try {
readerFile = ReadFileUtils.readWord(fileURL);
} catch (Exception e) {
e.printStackTrace();
}
OCRUtils.fatchRuleGetContent(readerFile, fatchRules, fatchMap, caseId);
} else if (fileURL.endsWith("pdf")) {
//获取文件的页数
int fileNumPage = getFileNumPage(fileURL);
//文件转成base64
String base64 = OCRUtils.pdfConvertBase64(fileURL);
if (base64 == null) {
throw new ServiceException("pdf转base64失败");
// return false;
}
StringBuilder ocrText = new StringBuilder(); // 创建一个StringBuilder对象
for (int i = 1; i <= fileNumPage; i++) {
//对接腾讯云接口.识别里面的数据
String text = OCRUtils.pdfIdentifyText(base64, i, fatchRules);
ocrText.append(text); // 拼接当前的字符串
// 根据抓取规则截取内容
OCRUtils.fatchRuleGetContent(ocrText.toString(), fatchRules, fatchMap, caseId);
}
}
}
}
@@ -118,10 +118,10 @@ public class OCRUtils {
for (FatchRule fatchRule : fatchRules) {
// 从后往前抓取
if (fatchRule.getFatchOrder() != null && fatchRule.getFatchOrder() == 1) {
reverseSubstringText(ocrText, fatchRule, fatchMap);
reverseSubstringText(ocrText, fatchRule, fatchMap,null);
} else {
// 从前往后抓取
substringText(ocrText, fatchRule, fatchMap);
substringText(ocrText, fatchRule, fatchMap,null);
}
}
@@ -129,43 +129,32 @@ public class OCRUtils {
}
public static void sub1(String text, FatchRule fatchRule, Map<String, String> fatchMap) {
if (StrUtil.isEmpty(fatchRule.getStartContent()) && StrUtil.isEmpty(fatchRule.getEndContent())) {
fatchMap.put(fatchRule.getColumnName(), trimStr(text));
} else if (StrUtil.isNotEmpty(fatchRule.getStartContent())) {
int startContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getStartContent(), fatchRule.getStartContentRepeatOrder());
if (startContIndex != -1) {
// 开始不为空结束为空
if (StrUtil.isEmpty(fatchRule.getEndContent())) {
if ((startContIndex + fatchRule.getStartContent().length()) <= text.length()) {
String substring = text.substring(startContIndex + fatchRule.getStartContent().length());
// 去除\n
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
}
/**
* 根据抓取规则获取内容
*
* @param ocrText ocr识别的text
* @param fatchRules 抓取规则
* @return
*/
public static void fatchRuleGetContent(String ocrText, List<FatchRule> fatchRules, Map<String, String> fatchMap, Long caseId) {
if (StrUtil.isEmpty(ocrText) || CollectionUtil.isEmpty(fatchRules)) {
return;
}
for (FatchRule fatchRule : fatchRules) {
// 从后往前抓取
if (fatchRule.getFatchOrder() != null && fatchRule.getFatchOrder() == 1) {
reverseSubstringText(ocrText, fatchRule, fatchMap, caseId);
} else {
// 从前往后抓取
substringText(ocrText, fatchRule, fatchMap, caseId);
} else {
// 开始不为空结束不为空
int endContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getEndContent(), fatchRule.getEndContentRepeatOrder());
if (endContIndex != -1 && endContIndex <= text.length() && (startContIndex + fatchRule.getStartContent().length()) <= endContIndex) {
String substring = text.substring(startContIndex + fatchRule.getStartContent().length(), endContIndex);
// 去除\n
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
}
}
//
}
} else if (StrUtil.isEmpty(fatchRule.getStartContent()) && StrUtil.isNotEmpty(fatchRule.getEndContent())) {
// 开始为空结束不为空
int endContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getEndContent(), fatchRule.getEndContentRepeatOrder());
if (endContIndex != -1) {
String substring = text.substring(0, endContIndex);
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
}
}
}
/**
* 正向截取字段
*
@@ -173,19 +162,19 @@ public class OCRUtils {
* @param fatchRule
* @param fatchMap
*/
private static void substringText(String text, FatchRule fatchRule, Map<String, String> fatchMap) {
private static void substringText(String text, FatchRule fatchRule, Map<String, String> fatchMap, Long caseId) {
String startContent = fatchRule.getStartContent();
String endContent = fatchRule.getEndContent();
// 开始为空结束为空
if (StrUtil.isEmpty(startContent) && StrUtil.isEmpty(endContent)) {
fatchMap.put(fatchRule.getColumnName(), trimStr(text));
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(text));
} else if (StrUtil.isNotEmpty(startContent) && StrUtil.isEmpty(endContent)) {
// 开始不为空结束为空
int startContIndex = StrUtil.ordinalIndexOf(text, startContent, fatchRule.getStartContentRepeatOrder());
if (startContIndex != -1 && text.length() >= (startContIndex + startContent.length())) {
String substring = text.substring(startContIndex + startContent.length());
// 去除\n
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(substring));
}
} else if (StrUtil.isEmpty(startContent) && StrUtil.isNotEmpty(endContent)) {
@@ -193,7 +182,7 @@ public class OCRUtils {
int endContIndex = StrUtil.ordinalIndexOf(text, endContent, fatchRule.getEndContentRepeatOrder());
if (endContIndex != -1) {
String substring = text.substring(0, endContIndex);
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(substring));
}
} else if (StrUtil.isNotEmpty(startContent) && StrUtil.isNotEmpty(endContent)) {
// 开始结束不为空
@@ -202,12 +191,72 @@ public class OCRUtils {
if (startIndexOf != -1 && endIndexOf != -1 && endIndexOf >= (startIndexOf + startContent.length()) && text.length() >= endIndexOf) {
String substring = text.substring(startIndexOf + startContent.length(), endIndexOf);
fatchMap.put(fatchRule.getColumnName(), trimStr(substring));
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(substring));
}
}
}
/**
* 从后往前截取字符串
*
* @param text
* @param fatchRule
* @param fatchMap
*/
public static void reverseSubstringText(String text, FatchRule fatchRule, Map<String, String> fatchMap, Long caseId) {
// 反正字符串
String reverseText = StrUtil.reverse(text);
// 结束截取字段
String reverseEndContent = "";
// 开始截取字段
String reverseStartContent = "";
if (StrUtil.isNotEmpty(fatchRule.getEndContent())) {
reverseEndContent = StrUtil.reverse(fatchRule.getEndContent());
}
if (StrUtil.isNotEmpty(fatchRule.getStartContent())) {
reverseStartContent = StrUtil.reverse(fatchRule.getStartContent());
}
// 开始和结束截取都为空
if (StrUtil.isEmpty(reverseStartContent) && StrUtil.isEmpty(reverseEndContent)) {
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(text));
} else if (StrUtil.isEmpty(reverseStartContent) && StrUtil.isNotEmpty(reverseEndContent)) {
// 开始为空,结束不为空
// 根据截取的序号查找出位置
int indexOf = StrUtil.ordinalIndexOf(reverseText, reverseEndContent, fatchRule.getEndContentRepeatOrder());
if (indexOf != -1) {
String substring = reverseText.substring(0, indexOf);
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(StrUtil.reverse(substring)));
}
}
} else if (StrUtil.isNotEmpty(reverseStartContent) && StrUtil.isEmpty(reverseEndContent)) {
// 开始不为空,结束为空
int indexOf = StrUtil.ordinalIndexOf(reverseText, reverseStartContent, fatchRule.getStartContentRepeatOrder());
if (indexOf != -1 && (indexOf + reverseStartContent.length() <= text.length())) {
String substring = reverseText.substring(indexOf + reverseStartContent.length());
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(StrUtil.reverse(substring)));
}
}
} else if (StrUtil.isNotEmpty(reverseStartContent) && StrUtil.isNotEmpty(reverseEndContent)) {
// 开始结束都不为空
int endIndexOf = StrUtil.ordinalIndexOf(reverseText, reverseEndContent, fatchRule.getEndContentRepeatOrder());
int startIndexOf = StrUtil.ordinalIndexOf(reverseText, reverseStartContent, fatchRule.getStartContentRepeatOrder());
if (startIndexOf != -1 && endIndexOf != -1 && endIndexOf >= (startIndexOf + reverseStartContent.length()) && text.length() >= endIndexOf) {
String substring = reverseText.substring(startIndexOf + reverseStartContent.length(), endIndexOf);
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName()+Constants.PDFSTR+caseId, trimStr(StrUtil.reverse(substring)));
}
}
}
}
/**
* 去除末尾空格
*
@@ -223,63 +272,4 @@ public class OCRUtils {
}
/**
* 从后往前截取字符串
*
* @param text
* @param fatchRule
* @param fatchMap
*/
public static void reverseSubstringText(String text, FatchRule fatchRule, Map<String, String> fatchMap) {
// 反正字符串
String reverseText = StrUtil.reverse(text);
// 结束截取字段
String reverseEndContent = "";
// 开始截取字段
String reverseStartContent = "";
if (StrUtil.isNotEmpty(fatchRule.getEndContent())) {
reverseEndContent = StrUtil.reverse(fatchRule.getEndContent());
}
if (StrUtil.isNotEmpty(fatchRule.getStartContent())) {
reverseStartContent = StrUtil.reverse(fatchRule.getStartContent());
}
// 开始和结束截取都为空
if (StrUtil.isEmpty(reverseStartContent) && StrUtil.isEmpty(reverseEndContent)) {
fatchMap.put(fatchRule.getColumnName(), trimStr(text));
} else if (StrUtil.isEmpty(reverseStartContent) && StrUtil.isNotEmpty(reverseEndContent)) {
// 开始为空,结束不为空
// 根据截取的序号查找出位置
int indexOf = StrUtil.ordinalIndexOf(reverseText, reverseEndContent, fatchRule.getEndContentRepeatOrder());
if (indexOf != -1) {
String substring = reverseText.substring(0, indexOf);
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName(), trimStr(StrUtil.reverse(substring)));
}
}
} else if (StrUtil.isNotEmpty(reverseStartContent) && StrUtil.isEmpty(reverseEndContent)) {
// 开始不为空,结束为空
int indexOf = StrUtil.ordinalIndexOf(reverseText, reverseStartContent, fatchRule.getStartContentRepeatOrder());
if (indexOf != -1 && (indexOf + reverseStartContent.length() <= text.length())) {
String substring = reverseText.substring(indexOf + reverseStartContent.length());
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName(), trimStr(StrUtil.reverse(substring)));
}
}
} else if (StrUtil.isNotEmpty(reverseStartContent) && StrUtil.isNotEmpty(reverseEndContent)) {
// 开始结束都不为空
int endIndexOf = StrUtil.ordinalIndexOf(reverseText, reverseEndContent, fatchRule.getEndContentRepeatOrder());
int startIndexOf = StrUtil.ordinalIndexOf(reverseText, reverseStartContent, fatchRule.getStartContentRepeatOrder());
if (startIndexOf != -1 && endIndexOf != -1 && endIndexOf >= (startIndexOf + reverseStartContent.length()) && text.length() >= endIndexOf) {
String substring = reverseText.substring(startIndexOf + reverseStartContent.length(), endIndexOf);
if (StrUtil.isNotEmpty(substring)) {
fatchMap.put(fatchRule.getColumnName(), trimStr(StrUtil.reverse(substring)));
}
}
}
}
}