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

[excel导入]修改百分比BUG

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