Commit 2b60874cf2c230b76cebb7e2fab10091b215a640

Authored by 徐烜
1 parent 2911e54c

PSM-12

Too many changes to show.

To preserve performance only 5 of 7 files are displayed.

src/main/java/com/bsth/controller/BaseController2.java
... ... @@ -5,10 +5,13 @@ import com.bsth.common.ResponseCode;
5 5 import com.bsth.service.BaseService;
6 6 import com.bsth.service.schedule.utils.DataImportExportService;
7 7 import com.google.common.base.Splitter;
  8 +import jxl.Sheet;
  9 +import jxl.Workbook;
8 10 import org.springframework.beans.factory.annotation.Autowired;
9 11 import org.springframework.data.domain.Page;
10 12 import org.springframework.data.domain.PageRequest;
11 13 import org.springframework.data.domain.Sort;
  14 +import org.springframework.util.CollectionUtils;
12 15 import org.springframework.web.bind.annotation.*;
13 16 import org.springframework.web.multipart.MultipartFile;
14 17  
... ... @@ -164,11 +167,24 @@ public class BaseController2<T, ID extends Serializable> {
164 167 */
165 168 @RequestMapping(value = "/dataExport", method = RequestMethod.GET)
166 169 public void dataExport(HttpServletResponse response) throws Exception {
  170 + dataExport(response, null);
  171 + }
  172 +
  173 + @RequestMapping(value = "/dataExportExt", method = RequestMethod.GET)
  174 + public void dataExport(HttpServletResponse response, @RequestParam Map<String, Object> param) throws Exception {
167 175 // 1、使用ktr转换获取输出文件
168 176 File ktrfile = new File(this.getClass().getResource(getDataExportKtrClasspath()).toURI());
169   - File outputfile = dataImportExportService.fileDataOutput(
170   - getDataExportFilename(),
171   - ktrfile);
  177 + File outputfile = null;
  178 + if (!CollectionUtils.isEmpty(param)) {
  179 + outputfile = dataImportExportService.fileDataOutput(
  180 + getDataExportFilename(),
  181 + ktrfile,
  182 + param);
  183 + } else {
  184 + outputfile = dataImportExportService.fileDataOutput(
  185 + getDataExportFilename(),
  186 + ktrfile);
  187 + }
172 188  
173 189 System.out.println(outputfile.getName());
174 190 String filePath = outputfile.getAbsolutePath();
... ... @@ -225,4 +241,52 @@ public class BaseController2&lt;T, ID extends Serializable&gt; {
225 241 throw new RuntimeException("必须override,并指定ktr classpath");
226 242 }
227 243  
  244 +
  245 + public static class ExcelFileOutput {
  246 + private String fileName;
  247 + private List<Map<String, Object>> sheetnames = new ArrayList<>();
  248 +
  249 + public String getFileName() {
  250 + return fileName;
  251 + }
  252 +
  253 + public void setFileName(String fileName) {
  254 + this.fileName = fileName;
  255 + }
  256 +
  257 + public List<Map<String, Object>> getSheetnames() {
  258 + return sheetnames;
  259 + }
  260 +
  261 + public void setSheetnames(List<Map<String, Object>> sheetnames) {
  262 + this.sheetnames = sheetnames;
  263 + }
  264 + }
  265 +
  266 + /**
  267 + * 上传Excel文件,返回文件全路径名,工作区名称列表。
  268 + * @param file
  269 + * @return
  270 + * @throws Exception
  271 + */
  272 + @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
  273 + public ExcelFileOutput fileUpload(MultipartFile file) throws Exception {
  274 + // 返回对象
  275 + ExcelFileOutput rs = new ExcelFileOutput();
  276 +
  277 + // 上传文件
  278 + File file1 = dataImportExportService.uploadFile(file);
  279 + // 获取文件的sheet
  280 + Workbook book = Workbook.getWorkbook(file1);
  281 + for (Sheet sheet : book.getSheets()) {
  282 + String sheetname = sheet.getName();
  283 + Map<String, Object> s = new HashMap<>();
  284 + s.put("name", sheetname);
  285 + rs.getSheetnames().add(s);
  286 + }
  287 +
  288 + rs.setFileName(file1.getAbsolutePath());
  289 + return rs;
  290 + }
  291 +
228 292 }
... ...
src/main/java/com/bsth/controller/CarsController.java
1 1 package com.bsth.controller;
2 2  
  3 +import com.bsth.common.ResponseCode;
3 4 import com.bsth.entity.Cars;
  5 +import com.bsth.service.schedule.utils.DataImportExportService;
4 6 import com.bsth.service.schedule.utils.DataToolsProperties;
5 7 import org.springframework.beans.factory.annotation.Autowired;
6 8 import org.springframework.boot.context.properties.EnableConfigurationProperties;
7   -import org.springframework.web.bind.annotation.*;
  9 +import org.springframework.web.bind.annotation.RequestMapping;
  10 +import org.springframework.web.bind.annotation.RequestMethod;
  11 +import org.springframework.web.bind.annotation.RequestParam;
  12 +import org.springframework.web.bind.annotation.RestController;
8 13  
  14 +import java.io.File;
  15 +import java.util.HashMap;
9 16 import java.util.Map;
10 17  
11 18 /**
... ... @@ -14,24 +21,12 @@ import java.util.Map;
14 21 @RestController
15 22 @RequestMapping("cars")
16 23 @EnableConfigurationProperties(DataToolsProperties.class)
17   -public class CarsController extends BaseController<Cars, Integer> {
  24 +public class CarsController extends BaseController2<Cars, Integer> {
18 25  
19 26 @Autowired
20 27 private DataToolsProperties dataToolsProperties;
21   -
22   - /**
23   - * 覆写方法,因为form提交的方式参数不全,改用 json形式提交 @RequestBody
24   - * @Title: save
25   - * @Description: TODO(持久化对象)
26   - * @param @param t
27   - * @param @return 设定文件
28   - * @return Map<String,Object> {status: 1(成功),-1(失败)}
29   - * @throws
30   - */
31   - @RequestMapping(method = RequestMethod.POST)
32   - public Map<String, Object> save(@RequestBody Cars t){
33   - return baseService.save(t);
34   - }
  28 + @Autowired
  29 + private DataImportExportService dataImportExportService;
35 30  
36 31 /**
37 32 * 验证。
... ... @@ -44,6 +39,48 @@ public class CarsController extends BaseController&lt;Cars, Integer&gt; {
44 39 return baseService.validateEquale(map);
45 40 }
46 41  
  42 + // uploadFile post
  43 +
  44 + // 验证excel sheet
  45 + @RequestMapping(value = "/validate/sheet", method = RequestMethod.GET)
  46 + public Map<String, Object> validateSheet() throws Exception {
  47 + Map<String, Object> rtn = new HashMap<>();
  48 +
  49 + // TODO:
  50 +
  51 + rtn.put("status", ResponseCode.SUCCESS);
  52 + return rtn;
  53 + }
  54 +
  55 + @RequestMapping(value = "/importfile", method = RequestMethod.POST)
  56 + public Map<String, Object> importData(
  57 + @RequestParam Map<String, Object> form)
  58 + throws Exception {
  59 + Map<String, Object> rtn = new HashMap<>();
  60 +
  61 + // TODO:
  62 + String filename = (String) form.get("filename");
  63 +
  64 +
  65 + try {
  66 + // 获取ktr转换文件绝对路径
  67 + File ktrfile = new File(this.getClass().getResource(getDataImportKtrClasspath()).toURI());
  68 + System.out.println(ktrfile.getAbsolutePath());
  69 + // 导入数据
  70 + dataImportExportService.fileDataImport(new File(filename), ktrfile);
  71 +
  72 + rtn.put("status", ResponseCode.SUCCESS);
  73 + rtn.put("msg", "导入成功");
  74 + } catch (Exception exp) {
  75 + exp.printStackTrace();
  76 + rtn.put("status", ResponseCode.ERROR);
  77 + rtn.put("msg", exp.getLocalizedMessage());
  78 + }
  79 +
  80 + return rtn;
  81 + }
  82 +
  83 +
47 84 @Override
48 85 protected String getDataImportKtrClasspath() {
49 86 return dataToolsProperties.getCarsDatainputktr();
... ...
src/main/java/com/bsth/controller/schedule/TTInfoDetailController.java
1 1 package com.bsth.controller.schedule;
2 2  
3 3 import com.bsth.common.ResponseCode;
4   -import com.bsth.controller.BaseController;
  4 +import com.bsth.controller.BaseController2;
5 5 import com.bsth.entity.CarPark;
6 6 import com.bsth.entity.LineInformation;
7 7 import com.bsth.entity.StationRoute;
... ... @@ -38,7 +38,7 @@ import java.util.regex.Pattern;
38 38 */
39 39 @RestController
40 40 @RequestMapping("tidc")
41   -public class TTInfoDetailController extends BaseController<TTInfoDetail, Long> {
  41 +public class TTInfoDetailController extends BaseController2<TTInfoDetail, Long> {
42 42 @Autowired
43 43 private TTInfoDetailService ttInfoDetailService;
44 44 @Autowired
... ... @@ -56,53 +56,13 @@ public class TTInfoDetailController extends BaseController&lt;TTInfoDetail, Long&gt; {
56 56 @Autowired
57 57 private DataToolsProperties dataToolsProperties;
58 58  
59   -
60   - public static class ExcelFileOutput {
61   - private String fileName;
62   - private List<Map<String, Object>> sheetnames = new ArrayList<>();
63   -
64   - public String getFileName() {
65   - return fileName;
66   - }
67   -
68   - public void setFileName(String fileName) {
69   - this.fileName = fileName;
70   - }
71   -
72   - public List<Map<String, Object>> getSheetnames() {
73   - return sheetnames;
74   - }
75   -
76   - public void setSheetnames(List<Map<String, Object>> sheetnames) {
77   - this.sheetnames = sheetnames;
78   - }
79   - }
80   -
81 59 /**
82 60 * 1、上传Excel文件,返回文件全路径名,工作区名称列表。
83 61 * @param file
84 62 * @return
85 63 * @throws Exception
86 64 */
87   - @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
88   - public ExcelFileOutput fileUpload(MultipartFile file) throws Exception {
89   - // 返回对象
90   - ExcelFileOutput rs = new ExcelFileOutput();
91   -
92   - // 上传文件
93   - File file1 = dataImportExportService.uploadFile(file);
94   - // 获取文件的sheet
95   - Workbook book = Workbook.getWorkbook(file1);
96   - for (Sheet sheet : book.getSheets()) {
97   - String sheetname = sheet.getName();
98   - Map<String, Object> s = new HashMap<>();
99   - s.put("name", sheetname);
100   - rs.getSheetnames().add(s);
101   - }
102 65  
103   - rs.setFileName(file1.getAbsolutePath());
104   - return rs;
105   - }
106 66  
107 67 /**
108 68 * 2、验证sheet(以后放到规则引擎里去做)。
... ... @@ -441,21 +401,6 @@ public class TTInfoDetailController extends BaseController&lt;TTInfoDetail, Long&gt; {
441 401 return ttInfoDetailService.getEditInfo(xlid, ttid);
442 402 }
443 403  
444   - /**
445   - * 覆写方法,因为form提交的方式参数不全,改用 json形式提交 @RequestBody
446   - * @Title: save
447   - * @Description: TODO(持久化对象)
448   - * @param @param t
449   - * @param @return 设定文件
450   - * @return Map<String,Object> {status: 1(成功),-1(失败)}
451   - * @throws
452   - */
453   - @RequestMapping(method = RequestMethod.POST)
454   - public Map<String, Object> save(@RequestBody TTInfoDetail t){
455   -
456   - return baseService.save(t);
457   - }
458   -
459 404 @Override
460 405 public TTInfoDetail findById(@PathVariable("id") Long aLong) {
461 406 return ttInfoDetailRepository.findOneExtend(aLong);
... ...
src/main/java/com/bsth/service/schedule/utils/DataImportExportService.java
... ... @@ -24,6 +24,7 @@ public interface DataImportExportService {
24 24 * @throws Exception
25 25 */
26 26 void fileDataImport(MultipartFile datafile, File ktrFile) throws Exception;
  27 + void fileDataImport(File datafile, File ktrFile) throws Exception;
27 28  
28 29 /**
29 30 * 数据导出。
... ...
src/main/java/com/bsth/service/schedule/utils/DataImportExportServiceImpl.java
... ... @@ -111,6 +111,31 @@ public class DataImportExportServiceImpl implements DataImportExportService, Ini
111 111 }
112 112  
113 113 @Override
  114 + public void fileDataImport(File datafile, File ktrFile) throws Exception {
  115 +// // 1、上传数据文件
  116 +// File uploadFile = datafile;
  117 +
  118 + // 2、使用kettle运行封装数据导入逻辑的ktr转换文件
  119 + // 2.1、初始化kettle(组件初始化已经做了)
  120 + // 2.2、创建转换元数据,转换
  121 + TransMeta transMeta = new TransMeta(ktrFile.getAbsolutePath());
  122 + Trans trans = new Trans(transMeta);
  123 + // 2.3、设定命名参数,用于指定数据文件,注意每个ktr必须都有以下指定的命名参数
  124 + trans.setParameterValue("filepath", datafile.getAbsolutePath()); // 指定导入数据文件的位置
  125 + trans.setParameterValue("erroroutputdir", dataToolsProperties.getTransErrordir()); // ktr转换错误输出目录
  126 + // TODO:可以考虑设定日志输出
  127 + // 2.4、执行转换
  128 + trans.execute(null);
  129 + // 2.5、等待转换结束
  130 + trans.waitUntilFinished();
  131 +
  132 + // 3、判定ktr错误数,注意这种错误代表部分数据错误,不会终止转换执行,一般设计ktr的时候,会有错误输出文件,TODO:以后考虑使用日志实时输出
  133 + if (trans.getErrors() > 0) {
  134 + throw new Exception("转换数据部分错误,请查看相关错误输出文件!");
  135 + }
  136 + }
  137 +
  138 + @Override
114 139 public File fileDataOutput(String fileName, File ktrFile) throws Exception {
115 140 return fileDataOutput(fileName, ktrFile, null);
116 141 }
... ...