提交 b6c39d58 authored 作者: zhoushaopan's avatar zhoushaopan

feat(培训人次): 导入excel查询错误的数据

导入excel查询错误的数据
上级 1414dcc2
......@@ -92,6 +92,24 @@
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
......
......@@ -72,4 +72,10 @@ public class transferController {
public void pushNow() {
pushTask.task();
}
@GetMapping("/fromExcel")
public String fromExcel(String filePath) {
biz088DcBranchTrainStatisticsService.fromExcel(filePath);
return "success";
}
}
package com.tykj.transfer.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import java.util.Date;
/**
* @author:zhoushaopan
* @create:2024/7/15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("branchTrainStatExcelVO")
public class BranchTrainStatExcelVO {
@Excel(name = "主键")
private String id;
@Excel(name = "地区编码")
private String regionCode;
@Excel(name = "网点名称")
private String branchName;
@Excel(name = "培训人数")
private String trainNum;
@Excel(name = "数据更新时间",importFormat = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 业务库数据产生时间
*/
@Column(name = "Biz_time")
private Date bizTime;
/**
* 数据仓数据记录时间
*/
@Column(name = "load_time")
private Date loadTime;
/**
* op
*/
private String op;
/**
* tongTime
*/
private Date tongTime;
/**
* tongId
*/
private Long tongId;
}
......@@ -3,4 +3,10 @@ package com.tykj.transfer.slave_service;
public interface Biz088DcBranchTrainStatisticsService {
void transferBranchApply();
/**
* 从excel导入数据
* @param filePath
*/
void fromExcel(String filePath);
}
package com.tykj.transfer.slave_service.impl;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tykj.transfer.dao.ActivitiesDao;
import com.tykj.transfer.dao.ActivityApplyDao;
import com.tykj.transfer.dao.BranchesDao;
......@@ -8,21 +11,31 @@ import com.tykj.transfer.entity.Activity;
import com.tykj.transfer.entity.Branches;
import com.tykj.transfer.entity.vo.BranchesVo;
import com.tykj.transfer.entity.vo.PageVo;
import com.tykj.transfer.excel.BranchTrainStatExcelVO;
import com.tykj.transfer.local_dao.YlBranchTrainStatisticsDao;
import com.tykj.transfer.local_entity.YlBranchTrainStatistics;
import com.tykj.transfer.pojo.Biz088DcBranchTrainStatistics;
import com.tykj.transfer.slave_dao.Biz088DcBranchTrainStatisticsDao;
import com.tykj.transfer.slave_service.Biz088DcBranchTrainStatisticsService;
import com.tykj.transfer.util.EntityToVoUtil;
import com.tykj.transfer.util.NotNullUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
@Slf4j
......@@ -81,4 +94,93 @@ public class Biz088DcBranchTrainStatisticsImpl implements Biz088DcBranchTrainSta
ylBranchTrainStatisticsDao.saveAll(listVo);
log.info("网点培训人数转移完成,成功转移了{}条", rs.size());
}
@Override
@Transactional(rollbackOn = Exception.class)
public void fromExcel(String filePath) {
JSONObject jsonObject = new JSONObject();
//根据文件路径获取文件夹下面的所有的文件
File file = new File(filePath);
File[] files = file.listFiles();
//输出文件数量
log.info("文件数量:{}", files.length);
File newFile ;
//最后统一输出到文件中
try {
newFile = new File(System.getProperty("user.dir")+ File.separator+"error.txt");
if (!newFile.exists()){
newFile.createNewFile();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
//
Date date = new Date();
for (File file1 : files) {
boolean b = file1.getName().startsWith(".");
if (!b) {
ImportParams importParams = new ImportParams();
importParams.setTitleRows(0);
importParams.setHeadRows(1);
List<BranchTrainStatExcelVO> importedExcel = ExcelImportUtil.importExcel(file1, BranchTrainStatExcelVO.class, importParams);
//过滤出只需要名称包含手机号码的
List<BranchTrainStatExcelVO> voList = importedExcel.stream()
.filter(vo -> {
String branchName = vo.getBranchName();
String s = removePhoneNumbers(branchName);
if (!branchName.equals(s)) {
//处理名称
vo.setBranchName(s);
//处理时间
vo.setBizTime(date);
vo.setLoadTime(date);
vo.setTongTime(date);
vo.setTongId(0L);
return true;
} else {
return false;
}
}).collect(Collectors.toList());
if (NotNullUtil.collectionNotNull(voList)) {
List<YlBranchTrainStatistics> ylBranchTrainStatistics = EntityToVoUtil.listVoStream(voList, YlBranchTrainStatistics.class);
ylBranchTrainStatistics.forEach(ylBranchTrainStatistics1 -> {
jsonObject.put(ylBranchTrainStatistics1.getId(),file1.getName());
});
ylBranchTrainStatisticsDao.saveAll(ylBranchTrainStatistics);
log.info("文件名:{},成功转移了{}条", file1.getName(), voList.size());
} else {
log.info("文件名:{},没有需要转移的数据", file1.getName());
}
}
}
//将jsonobj输出到文件中
try {
FileUtils.writeStringToFile(newFile, jsonObject.toJSONString(), "UTF-8");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String input = "这是一段包含手机号码13512345678的文本,还有另一个号码13998765432。";
// 判断字符串中是否包含手机号,并替换成空字符串
String result = removePhoneNumbers(input);
System.out.println("原始字符串: " + input);
System.out.println("处理后字符串: " + result);
}
public static String removePhoneNumbers(String input) {
// 匹配手机号的正则表达式
String regex = "(?<!\\d)(?:(?:1[3456789]\\d{9})|(?:861[3456789]\\d{9}))(?!\\d)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// 替换手机号为""
String result = matcher.replaceAll("");
return result;
}
}
package com.tykj.transfer.util;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author: zsp
* @create: 2023-02-09 22:28
**/
public class EntityToVoUtil {
/**
*
* @param currentClass 当前对象
* @param targetClass 目标对象
* @param <E>
* @return
*/
public static <E> E convert(Object currentClass, Class<E> targetClass) {
// 判断currentClass 是否为空!
if (currentClass == null) {
return null;
}
// 判断targetClass 是否为空
if (targetClass == null) {
return null;
}
try {
// 创建新的对象实例
E newInstance = targetClass.newInstance();
// 把原对象数据拷贝到新的对象
BeanUtils.copyProperties(currentClass, newInstance);
// 返回新对象
return newInstance;
} catch (Exception e) {
return null;
}
}
/**
* Page<Entity> 分页对象转 Page<Vo> (Stream 方式)
* @param page
* @param v
* @param <T>
* @param <V>
* @return
*/
public static <T, V> Page<V> pageVoStream(Page<T> page, Class<V> v) {
List<V> voList = page.getContent().stream().map(item -> {
try {
return (V) EntityToVoUtil.convert(item, v.newInstance().getClass());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
return new PageImpl<>(voList, page.getPageable(), page.getTotalElements());
}
/**
* list<Entity> 集合对象转list<Vo> (Stream 方式)
* @param oldList
* @param v
* @param <T>
* @param <V>
* @return
*/
public static <T, V> List<V> listVoStream(List<T> oldList, Class<V> v) {
List<V> voList = oldList.stream().map(item -> {
try {
return (V) EntityToVoUtil.convert(item, v.newInstance().getClass());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
return voList;
}
}
package com.tykj.transfer.util;
import org.springframework.stereotype.Component;
import java.util.Collection;
/**
* @author: zsp
* @create: 2023-02-10 10:19
**/
@Component
public class NotNullUtil {
/**
* 字符串判断空
* @param str
* @return
*/
public static boolean stringNotNull(String str) {
return str != null && !"".equals(str);
}
/**
* 集合判断空
* @param collection
* @return
*/
public static boolean collectionNotNull(Collection<?> collection) {
return collection != null && collection.size()>0;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论