提交 43a4b9de authored 作者: xc's avatar xc

[excel导入]修改百分比BUG

上级 1be24adf
...@@ -60,8 +60,8 @@ public class ExcelController { ...@@ -60,8 +60,8 @@ public class ExcelController {
@ApiOperation("新增excel路径") @ApiOperation("新增excel路径")
@PostMapping("/path") @PostMapping("/path")
public ResponseEntity savePath(@RequestParam String path){ public ResponseEntity savePath(@RequestParam String path, @RequestParam String rootPath){
return excelData.savePath(path); return excelData.savePath(path, rootPath);
} }
@ApiOperation("查询路径") @ApiOperation("查询路径")
...@@ -76,4 +76,10 @@ public class ExcelController { ...@@ -76,4 +76,10 @@ public class ExcelController {
return excelData.findPrecen(); return excelData.findPrecen();
} }
@ApiOperation("查询预览结果")
@PostMapping("/preview")
public ResponseEntity findPreview(){
return excelData.findPreview();
}
} }
...@@ -15,4 +15,5 @@ import javax.persistence.Table; ...@@ -15,4 +15,5 @@ import javax.persistence.Table;
@NoArgsConstructor @NoArgsConstructor
public class ExcelConfig extends BaseEntity { public class ExcelConfig extends BaseEntity {
private String path; private String path;
private String rootPath;
} }
...@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; ...@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List; import java.util.List;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
...@@ -13,6 +14,7 @@ public class ExcelVo { ...@@ -13,6 +14,7 @@ public class ExcelVo {
private Integer successNum; private Integer successNum;
private Integer errorNum; private Integer errorNum;
private Integer importNum; private Integer importNum;
private Date date;
private List<ExcelErrorVo> list; private List<ExcelErrorVo> list;
} }
...@@ -32,6 +32,7 @@ import java.io.File; ...@@ -32,6 +32,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -50,27 +51,27 @@ public class ExcelData { ...@@ -50,27 +51,27 @@ public class ExcelData {
@Autowired @Autowired
private ExcelConfigDao excelConfigDao; private ExcelConfigDao excelConfigDao;
private String path; private String path;
private String rootPath;
private float percent; private float percent;
private String successPath = System.getProperty("user.dir") + "/successFile/"; private String tmpPath;
private String errorPath = System.getProperty("user.dir") + "/errorFile/"; private ExcelVo previewResult;
private String tmpPath = System.getProperty("user.dir") + "/tmp/";
public ExcelData(){ public ExcelData(){
}
public ResponseEntity getExcelData(int id){
String successPath = rootPath + "/research/successFile/";
String errorPath = rootPath + "/research/errorFile/";
File success = new File(successPath); File success = new File(successPath);
File error = new File(errorPath); File error = new File(errorPath);
File tmp = new File(tmpPath);
if (!success.exists()){ if (!success.exists()){
success.mkdirs(); success.mkdirs();
} }
if (!error.exists()){ if (!error.exists()){
error.mkdirs(); error.mkdirs();
} }
if (!tmp.exists()){
tmp.mkdirs();
}
}
public ResponseEntity getExcelData(int id){
this.percent =0;//百分比数字 this.percent =0;//百分比数字
String jsonData = modelHelper.getJsonExpample(id); String jsonData = modelHelper.getJsonExpample(id);
try { try {
...@@ -79,7 +80,7 @@ public class ExcelData { ...@@ -79,7 +80,7 @@ public class ExcelData {
new TypeReference<Map<String, Map<String, Object>>>(){}); new TypeReference<Map<String, Map<String, Object>>>(){});
File file = new File(tmpPath); File file = new File(tmpPath);
if (!file.isDirectory()){ if (!file.isDirectory()){
excelLogService.save(new ExcelLog("导入失败", "文件路径不是文件夹", file.getPath())); excelLogService.save(new ExcelLog("导入失败", "文件路径不是文件夹", tmpPath));
return ResultUtil.failed("文件路径不是文件夹!"); return ResultUtil.failed("文件路径不是文件夹!");
} }
File[] dataFiles = file.listFiles(); File[] dataFiles = file.listFiles();
...@@ -210,7 +211,9 @@ public class ExcelData { ...@@ -210,7 +211,9 @@ public class ExcelData {
} }
ExcelVo excelVo = new ExcelVo(dataFiles.length, successNum, errorNum, 0,null); File[] successFile = new File(tmpPath).listFiles();
int importNum = successFile == null ? 0 : successFile.length;
ExcelVo excelVo = new ExcelVo(dataFiles.length, successNum, errorNum, importNum, new Date(),null);
if (!"".equals(errorFileNameList)){ if (!"".equals(errorFileNameList)){
excelLogService.save(new ExcelLog("导入失败", errorReason, errorFileNameList)); excelLogService.save(new ExcelLog("导入失败", errorReason, errorFileNameList));
String s = "没有文件"; String s = "没有文件";
...@@ -235,12 +238,24 @@ public class ExcelData { ...@@ -235,12 +238,24 @@ public class ExcelData {
* @return * @return
*/ */
public ResponseEntity compareRule(){ public ResponseEntity compareRule(){
if (path == null){ this.percent = 0;
path = findPaths(); this.previewResult = null;
if ("".equals(path)){ if (path == null || rootPath == null){
path = findPaths().getPath();
rootPath = findPaths().getRootPath();
if (path ==null){
return ResultUtil.failed("没有配置导入路径!"); return ResultUtil.failed("没有配置导入路径!");
} }
if (rootPath == null){
rootPath = System.getProperty("user.dir") + "/";
}
} }
tmpPath = rootPath + "/research/tmp/";
File tmp = new File(tmpPath);
if (!tmp.exists()){
tmp.mkdirs();
}
List<ExcelErrorVo> excelVos = new ArrayList<>(); List<ExcelErrorVo> excelVos = new ArrayList<>();
List<Rule> rules = ruleService.findAll(); List<Rule> rules = ruleService.findAll();
int successNum = 0;//符合规则文件数量 int successNum = 0;//符合规则文件数量
...@@ -252,13 +267,13 @@ public class ExcelData { ...@@ -252,13 +267,13 @@ public class ExcelData {
String tem = template.getTemplate("template.txt"); String tem = template.getTemplate("template.txt");
Map<String, int[]> indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){}); Map<String, int[]> indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){});
if (indexMap.isEmpty()){ if (indexMap.isEmpty()){
return ResultUtil.failed("没有excle模板!"); return ResultUtil.failed("没有excle模板,请导入模板文件!");
} }
File file = new File(path); File file = new File(path);
File[] dataFiles = file.listFiles(); File[] dataFiles = file.listFiles();
if (dataFiles == null){ if (dataFiles == null || dataFiles.length == 0){
return ResultUtil.failed("文件夹为空!"); return ResultUtil.failed("文件夹为空,请检查" + path + "下是否有excel文件!");
} }
total = dataFiles.length; total = dataFiles.length;
...@@ -421,6 +436,9 @@ public class ExcelData { ...@@ -421,6 +436,9 @@ public class ExcelData {
errorNum = errorNum + 1; errorNum = errorNum + 1;
excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result)); excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result));
} }
System.out.println("++++++++++++++++=");
System.out.println(successNum + errorNum);
}catch (Exception e){ }catch (Exception e){
// e.printStackTrace(); // e.printStackTrace();
errorNum = errorNum + 1; errorNum = errorNum + 1;
...@@ -428,7 +446,8 @@ public class ExcelData { ...@@ -428,7 +446,8 @@ public class ExcelData {
result.add(k + "." + "不是模板文件或读文件失败"); result.add(k + "." + "不是模板文件或读文件失败");
excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result)); excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result));
} }
this.percent = (float) (successNum + errorNum)/dataFiles.length;
System.out.println(percent + " lll " + dataFiles.length);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -436,7 +455,8 @@ public class ExcelData { ...@@ -436,7 +455,8 @@ public class ExcelData {
} }
File[] successFile = new File(tmpPath).listFiles(); File[] successFile = new File(tmpPath).listFiles();
int importNum = successFile == null ? 0 : successFile.length; int importNum = successFile == null ? 0 : successFile.length;
ExcelVo excelVo = new ExcelVo(total,successNum, errorNum, importNum, excelVos); ExcelVo excelVo = new ExcelVo(total,successNum, errorNum, importNum, new Date(), excelVos);
this.previewResult = excelVo;
return ResultUtil.success(excelVo, "预览成功"); return ResultUtil.success(excelVo, "预览成功");
} }
...@@ -514,15 +534,17 @@ public class ExcelData { ...@@ -514,15 +534,17 @@ public class ExcelData {
} }
return ResultUtil.failed("查询失败"); return ResultUtil.failed("查询失败");
} }
public ResponseEntity savePath(String path){ public ResponseEntity savePath(String path, String rootPath){
try { try {
this.path = path; this.path = path;
this.rootPath = rootPath;
List<ExcelConfig> excelConfigs = excelConfigDao.findAll(); List<ExcelConfig> excelConfigs = excelConfigDao.findAll();
ExcelConfig excelConfig = new ExcelConfig(); ExcelConfig excelConfig = new ExcelConfig();
if (excelConfigs != null && excelConfigs.size() > 0){ if (excelConfigs != null && excelConfigs.size() > 0){
excelConfig = excelConfigs.get(0); excelConfig = excelConfigs.get(0);
} }
excelConfig.setPath(path); excelConfig.setPath(path);
excelConfig.setRootPath(rootPath);
excelConfigDao.saveAndFlush(excelConfig); excelConfigDao.saveAndFlush(excelConfig);
return ResultUtil.success("","保存成功"); return ResultUtil.success("","保存成功");
}catch (Exception e){ }catch (Exception e){
...@@ -530,16 +552,28 @@ public class ExcelData { ...@@ -530,16 +552,28 @@ public class ExcelData {
} }
return ResultUtil.failed("新增失败"); return ResultUtil.failed("新增失败");
} }
private String findPaths(){ private ExcelConfig findPaths(){
List<ExcelConfig> excelConfigs = excelConfigDao.findAll(); List<ExcelConfig> excelConfigs = excelConfigDao.findAll();
if (excelConfigs != null && excelConfigs.size() > 0){ if (excelConfigs != null && excelConfigs.size() > 0){
return excelConfigs.get(0).getPath(); return excelConfigs.get(0);
} }
return ""; return new ExcelConfig();
} }
public ResponseEntity findPrecen(){ public ResponseEntity findPrecen(){
return ResultUtil.success(this.percent,"查询成功");
DecimalFormat df=new DecimalFormat("0");//设置保留位数
String s = df.format(this.percent * 100);
System.out.println("=============================================");
System.out.println(s);
if ("100".equals(s)){
this.percent = 0;
}
return ResultUtil.success(s,"查询成功");
}
public ResponseEntity findPreview(){
return ResultUtil.success(previewResult,"查询成功");
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论