提交 cebe9061 authored 作者: xc's avatar xc

[excle]excel导入

上级 e7fdeca8
package com.tykj.model_layer.controller;
import com.tykj.base.result.ResultUtil;
import com.tykj.model_layer.entity.ExcelConfig;
import com.tykj.model_layer.excel.ExcelData;
import com.tykj.model_layer.service.ExcelLogService;
import com.tykj.model_layer.service.impl.ModelHelper;
......@@ -27,7 +28,9 @@ public class ExcelController {
@ApiOperation("导入excel")
@PostMapping("/import")
public ResponseEntity importExcel(@RequestParam int id){
public ResponseEntity importExcel(){
//@RequestParam int id
int id = 14;
return excelData.getExcelData(id);
}
......@@ -55,5 +58,16 @@ public class ExcelController {
return excelData.compareRule();
}
@ApiOperation("新增excel路径")
@PostMapping("/path")
public ResponseEntity savePath(@RequestParam String path){
return excelData.savePath(path);
}
@ApiOperation("查询路径")
@PostMapping("/findPath")
public ResponseEntity findPath(){
return excelData.findPath();
}
}
package com.tykj.model_layer.dao;
import com.tykj.model_layer.entity.ExcelConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface ExcelConfigDao extends JpaRepository<ExcelConfig, Integer>, JpaSpecificationExecutor<ExcelConfig> {
}
package com.tykj.model_layer.entity;
import com.tykj.base.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelConfig extends BaseEntity {
private String path;
}
......@@ -5,10 +5,16 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Table;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table
public class ExcelLog extends BaseEntity {
private String result;
private String reason;
private String fileName;
}
......@@ -31,6 +31,6 @@ public class Rule extends BaseEntity {
private String level;
@ApiModelProperty("对比结果")
private String result;
@ApiModelProperty("是否需要对比多个sheet数据")
@ApiModelProperty("是否需要对比多个sheet数据, 0不用,1需要对比所有sheet的name是否相同,2 唯一值")
private Integer compare;
}
......@@ -14,4 +14,5 @@ public class ExcelLogVo extends JpaCustomPage {
@ApiModelProperty("成功")
private String result;
}
......@@ -9,6 +9,7 @@ import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
public class ExcelVo {
private Integer total;
private Integer successNum;
private Integer errorNum;
private List<ExcelErrorVo> list;
......
......@@ -2,17 +2,23 @@ package com.tykj.model_layer.excel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tykj.base.result.ApiException;
import com.tykj.base.result.ResultUtil;
import com.tykj.model_layer.dao.ExcelConfigDao;
import com.tykj.model_layer.entity.ExcelConfig;
import com.tykj.model_layer.entity.ExcelLog;
import com.tykj.model_layer.entity.Quote;
import com.tykj.model_layer.entity.Rule;
import com.tykj.model_layer.entity.vo.ExcelErrorVo;
import com.tykj.model_layer.entity.vo.ExcelVo;
import com.tykj.model_layer.entity.vo.QueryCondition;
import com.tykj.model_layer.service.ExcelLogService;
import com.tykj.model_layer.service.ModelService;
import com.tykj.model_layer.service.RuleService;
import com.tykj.model_layer.service.impl.ModelHelper;
import io.swagger.models.auth.In;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
......@@ -41,10 +47,12 @@ public class ExcelData {
private RuleService ruleService;
@Autowired
private ExcelLogService excelLogService;
private String path = "/home/xie/test/";
private String successPath = System.getProperty("user.dir") + "/successFile";
private String errorPath = System.getProperty("user.dir") + "/errorFile";
private String tmpPath = System.getProperty("user.dir") + "/tmp";
@Autowired
private ExcelConfigDao excelConfigDao;
private String path;
private String successPath = System.getProperty("user.dir") + "/successFile/";
private String errorPath = System.getProperty("user.dir") + "/errorFile/";
private String tmpPath = System.getProperty("user.dir") + "/tmp/";
public ExcelData(){
File success = new File(successPath);
......@@ -53,10 +61,10 @@ public class ExcelData {
if (!success.exists()){
success.mkdirs();
}
if (error.exists()){
if (!error.exists()){
error.mkdirs();
}
if (tmp.exists()){
if (!tmp.exists()){
tmp.mkdirs();
}
}
......@@ -68,22 +76,31 @@ public class ExcelData {
new TypeReference<Map<String, Map<String, Object>>>(){});
File file = new File(tmpPath);
if (!file.isDirectory()){
excelLogService.save(new ExcelLog("导入失败", "文件路径不是文件夹"));
excelLogService.save(new ExcelLog("导入失败", "文件路径不是文件夹", file.getPath()));
return ResultUtil.failed("文件路径不是文件夹!");
}
File[] dataFiles = file.listFiles();
if (dataFiles == null || dataFiles.length == 0){
excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件"));
excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件", ""));
return ResultUtil.failed("文件路径下没有文件!");
}
List<Rule> ruleList = ruleService.findAll().stream().filter(rule1 -> rule1.getCompare() == 2).collect(Collectors.toList());
Rule rule = new Rule();
if (ruleList.size() > 0){
rule = ruleList.get(0);
}
String successFileNameList = "";
String errorFileNameList = "";
String errorReason = "";
for (File dataFile : dataFiles){
List<Map<String, Object>> mapList = new ArrayList<>();
//取模板,如果有模板文件传过来就解析模板文件并保存,否则就读本地模板
Map<String, int[]> indexMap = getIndenxMap(null);
if (indexMap.isEmpty()){
excelLogService.save(new ExcelLog("导入失败", "没有模板文件"));
excelLogService.save(new ExcelLog("导入失败", "没有模板文件", ""));
return ResultUtil.failed("没有模板文件导入失败!");
}
//取数据
......@@ -95,11 +112,16 @@ public class ExcelData {
} else if(fileName.endsWith("xlsx")) {
dataWb = new XSSFWorkbook(dataFis);
}else {
excelLogService.save(new ExcelLog("导入失败", "路径下不是excel文件"));
return ResultUtil.failed("路径下不是excel文件!");
errorFileNameList = errorFileNameList + "[" + fileName + "]";
errorReason = "不是excel文件或保存失败";
dataFile.renameTo(new File(errorPath + fileName));
continue;
}
for (int i = 0; i < dataWb.getNumberOfSheets(); i++){
Sheet sheet1 = dataWb.getSheetAt(i);
if ("引用表(请勿改动)".equals(sheet1.getSheetName())){
continue;
}
Map<String, Object> objectMap = new HashMap<>();
String className = null;
for (String key : indexMap.keySet()){
......@@ -111,6 +133,20 @@ public class ExcelData {
if (dataMap.get(className) != null && dataMap.get(className).keySet().contains(classArray[1])){
objectMap.put(classArray[1], sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue());
}
if (Objects.equals(key, rule.getRuleKey())){
//查询数据库是否有相同网络名称,有就更新
List<QueryCondition> queryConditions = new ArrayList<>();
QueryCondition queryCondition = new QueryCondition();
queryCondition.setName(classArray[1]);
queryCondition.setType("=");
queryCondition.setValue(sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue());
queryConditions.add(queryCondition);
List<Map<String, Object>> list = modelService.complexQuery(className, null, queryConditions);
if (list != null && list.size() > 0){
Object object = list.get(0).get("id");
objectMap.put("id", object);
}
}
}
if (!objectMap.isEmpty()){
Map<String, Object> result = new HashMap<>();
......@@ -119,43 +155,69 @@ public class ExcelData {
mapList.add(result);
}
}
try {
modelService.putValueByEntityNameList(mapList);
dataFile.renameTo(new File(successPath + fileName));
}catch (Exception e){
dataFile.renameTo(new File(errorPath + fileName));
e.printStackTrace();
}
dataFile.renameTo(new File(successPath + fileName));
excelLogService.save(new ExcelLog("导入成功", ""));
int re = modelService.putValueByEntityNameList(mapList);
if (re == 0){
successFileNameList = successFileNameList + "[" + fileName + "]";
dataFile.renameTo(new File(successPath + fileName));
}else {
errorFileNameList = errorFileNameList + "[" + fileName + "]";
errorReason = "不是excel文件或保存失败";
dataFile.renameTo(new File(errorPath + fileName));
}
}
if (!"".equals(errorFileNameList)){
excelLogService.save(new ExcelLog("导入失败", errorReason, errorFileNameList));
String s = "没有文件";
if (!"".equals(successFileNameList)){
s = successFileNameList;
}
return ResultUtil.failed(errorFileNameList + "导入失败," + s + "导入成功");
}else {
excelLogService.save(new ExcelLog("导入成功", "", successFileNameList));
return ResultUtil.success("", "导入成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
excelLogService.save(new ExcelLog("导入失败", ""));
excelLogService.save(new ExcelLog("导入失败", "", ""));
return ResultUtil.failed("导入失败!");
}
/**
* 预览
* @return
*/
public ResponseEntity compareRule(){
if (path == null){
path = findPaths();
// return ResultUtil.failed("没有配置导入路径!");
}
List<ExcelErrorVo> excelVos = new ArrayList<>();
List<Rule> rules = ruleService.findAll();
int successNum = 0;
int errorNum = 0;
int total = 0;
boolean isSuccess = true;
try {
Map<String, int[]>indexMap = getIndenxMap(null);
if (indexMap.isEmpty()){
return ResultUtil.failed("没有excle模板!");
}
File file = new File(path);
File[] dataFiles = file.listFiles();
total = dataFiles.length;
//用于判断excel内容是否符合选项
String className = null;
for (String key : indexMap.keySet()){
className = key.split("[.]")[0];
break;
}
//取所有字段的引用map<字段名,引用列表>
Map<String, List<Quote>> he = modelHelper.getQuoteList(className);
for (File dataFile : dataFiles){
FileInputStream dataFis = new FileInputStream(dataFile);
Workbook wb = null;
String fileName = dataFile.getName();
......@@ -169,17 +231,55 @@ public class ExcelData {
List<String> result = new ArrayList<>();
Map<String, String> networkName = new HashMap<>();
for (int i = 0; i < wb.getNumberOfSheets(); i++){
Sheet sheet1 = wb.getSheetAt(i);
if ("引用表(请勿改动)".equals(sheet1.getSheetName())){
continue;
}
if (sheet1 == null){
continue;
}
String sameNet = null;
for (String key : indexMap.keySet()){
//key=className.property
int[] value = indexMap.get(key);
int dataRow = value[0];
int dataCell = value[1];
String data = sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue();
//判断是否是两个相同的sheet
if (content.get(sheet1.getSheetName()) == null || content.get(sheet1.getSheetName()).isEmpty()) {
content.put(sheet1.getSheetName(), data);
} else {
String con = content.get(sheet1.getSheetName());
content.put(sheet1.getSheetName(), con + data);
}
//判断引用
String propertyName = key.split("[.]")[1];
List<Quote> quotes = he.get(propertyName);
// System.out.println("引用");
// System.out.println(JSON.toJSONString(quotes));
if (quotes != null && quotes.size() > 0){
boolean isQuote = true;
for (Quote quote : quotes){
if(Objects.equals(data, quote.getValue())){
isQuote = false;
break;
}
}
if (isQuote){
int k = result.size() + 1;
System.out.println();
System.out.println(JSON.toJSONString(quotes));
result.add(k + "." + sheet1.getSheetName() + data + "不符合引用规则");
isSuccess = false;
}
}
//正则规则判断
List<Rule> ruleList = rules.stream().filter(rule1 -> rule1.getRuleKey().equals(key)).collect(Collectors.toList());
if (!ruleList.isEmpty()) {
Rule rule = ruleList.get(0);
//判断同一个文件里所有sheet中该字段是否相同
if (rule.getCompare() == 1) {
if (map.get(rule.getName())== null || map.get(rule.getName()).isEmpty()) {
List<String> list = new ArrayList<>();
......@@ -188,28 +288,24 @@ public class ExcelData {
} else {
map.get(rule.getName()).add(data);
}
}else if (rule.getCompare() == 2){
//判断统一文件中该字段是否唯一
if (networkName.keySet().contains(data)) {
sameNet = networkName.get(data);
}
networkName.put(data, sheet1.getSheetName());
}
Pattern p = Pattern.compile(rule.getRule());
Matcher m = p.matcher(data);
if (!m.matches()) {
//不符合
int k = result.size() + 1;
result.add(k + "." + sheet1.getSheetName() + rule.getName() + "不符合规则");
result.add(k + "." + sheet1.getSheetName() + rule.getName() + "不符合正则规则");
isSuccess = false;
}
if ("网络名称".equals(rule.getName())) {
if (networkName.keySet().contains(data)) {
sameNet = networkName.get(data);
}
networkName.put(data, sheet1.getSheetName());
}
if (content.get(sheet1.getSheetName()) == null || content.get(sheet1.getSheetName()).isEmpty()) {
content.put(sheet1.getSheetName(), data);
} else {
String con = content.get(sheet1.getSheetName());
content.put(sheet1.getSheetName(), con + data);
}
}
}
if (sameNet != null){
String now = content.get(sheet1.getSheetName());
......@@ -229,7 +325,7 @@ public class ExcelData {
if (1 < new HashSet<Object>(list).size()){
//不是全部相同
int k = result.size() + 1;
result.add(k + "." + key + "有不同数据");
result.add(k + "." + key + "在同一文件里数据不同");
isSuccess = false;
}
}
......@@ -239,17 +335,18 @@ public class ExcelData {
successNum = successNum + 1;
}else {
dataFile.renameTo(new File(errorPath + fileName));
// dataFile.renameTo(new File(errorPath + fileName));
errorNum = errorNum + 1;
excelVos.add(new ExcelErrorVo(fileName, result));
}
excelVos.add(new ExcelErrorVo(fileName, result));
}
} catch (IOException e) {
e.printStackTrace();
return ResultUtil.failed("预览失败!");
}
ExcelVo excelVo = new ExcelVo(successNum, errorNum, excelVos);
ExcelVo excelVo = new ExcelVo(total,successNum, errorNum, excelVos);
return ResultUtil.success(excelVo, "预览成功");
}
......@@ -276,6 +373,8 @@ public class ExcelData {
for (int index = 0; index <= lastRow; index++){
row = sheet.getRow(index);
for(int i = row.getFirstCellNum(); i < row.getLastCellNum(); ++i) {
row.getCell(i).setCellType(CellType.STRING);
if (row.getCell(i) != null && row.getCell(i).getStringCellValue().contains("$")) {
//{className.propertyName=[1,2]}
String type = row.getCell(i).getStringCellValue().substring(1);
......@@ -286,13 +385,11 @@ public class ExcelData {
}
}
}
System.out.println(JSON.toJSONString(indexMap));
Template template = new Template();
template.saveTemplate(JSON.toJSONString(indexMap));
}else {
Template template = new Template();
String tem = template.getTemplate();
System.out.println(tem);
if (tem.isEmpty()){
return indexMap;
}
......@@ -301,4 +398,30 @@ public class ExcelData {
return indexMap;
}
public ResponseEntity findPath(){
try {
return ResultUtil.success(findPaths(), "查询成功");
}catch (Exception e){
e.printStackTrace();
}
return ResultUtil.failed("查询失败");
}
public ResponseEntity savePath(String path){
try {
this.path = path;
excelConfigDao.saveAndFlush(new ExcelConfig(path));
return ResultUtil.success("","保存成功");
}catch (Exception e){
e.printStackTrace();
}
return ResultUtil.failed("新增失败");
}
private String findPaths(){
List<ExcelConfig> excelConfigs = excelConfigDao.findAll();
if (excelConfigs != null && excelConfigs.size() > 0){
return excelConfigs.get(0).getPath();
}
return "";
}
}
......@@ -2,6 +2,7 @@ package com.tykj.model_layer.service.impl;
import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.tykj.base.page.JpaCustomOrder;
import com.tykj.base.result.ResultUtil;
import com.tykj.model_layer.dao.ExcelLogDao;
import com.tykj.model_layer.entity.ExcelLog;
......@@ -9,9 +10,13 @@ import com.tykj.model_layer.entity.vo.ExcelLogVo;
import com.tykj.model_layer.service.ExcelLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ExcelLogServiceImpl implements ExcelLogService {
@Autowired
......@@ -25,6 +30,12 @@ public class ExcelLogServiceImpl implements ExcelLogService {
public ResponseEntity pageAll(ExcelLogVo ruleVo) {
try {
PredicateBuilder<ExcelLog> and = Specifications.and();
List<JpaCustomOrder> orders = new ArrayList<>();
JpaCustomOrder jpaCustomOrder = new JpaCustomOrder();
jpaCustomOrder.setCoulmn("createdTime");
jpaCustomOrder.setDirection(Sort.Direction.DESC);
orders.add(jpaCustomOrder);
ruleVo.setOrders(orders);
Page<ExcelLog> rules = excelLogDao.findAll(and.build(), ruleVo.getPageable());
return ResultUtil.success(rules, "查询成功!");
}catch (Exception e){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论