Commit 3b5dae67ec843f7a2182dda76b4d65505ae00d6c
1 parent
fb0ff6f6
计划调度功能增加
1、修正原有导入格式(老系统),添加每个营运班次的行驶时间格式
Showing
5 changed files
with
1030 additions
and
164 deletions
Too many changes to show.
To preserve performance only 5 of 6 files are displayed.
src/main/java/com/bsth/controller/schedule/core/TTInfoDetailController.java
| ... | ... | @@ -6,9 +6,9 @@ import com.bsth.entity.schedule.TTInfoDetail; |
| 6 | 6 | import com.bsth.service.schedule.TTInfoDetailService; |
| 7 | 7 | import com.bsth.service.schedule.datatools.TTInfoDetailForEdit; |
| 8 | 8 | import com.bsth.service.schedule.datatools.TTinfoDetailDynamicData; |
| 9 | -import com.bsth.service.schedule.timetable.ExcelFormatType; | |
| 10 | 9 | import com.bsth.service.schedule.utils.DataToolsFile; |
| 11 | 10 | import com.bsth.service.schedule.utils.DataToolsFileType; |
| 11 | +import com.bsth.service.schedule.utils.TimetableExcelData; | |
| 12 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | 13 | import org.springframework.web.bind.annotation.*; |
| 14 | 14 | |
| ... | ... | @@ -50,7 +50,7 @@ public class TTInfoDetailController extends BController<TTInfoDetail, Long> { |
| 50 | 50 | throw new Exception("线路版本未知"); |
| 51 | 51 | } |
| 52 | 52 | ttInfoDetailService.validateExcelSheet(filename, sheetname, lineid, linename, lineversion, |
| 53 | - ExcelFormatType.getEnum(excelFormatType)); | |
| 53 | + TimetableExcelData.ImportFormatType.fromDesc(excelFormatType)); | |
| 54 | 54 | rtn.put("status", ResponseCode.SUCCESS); |
| 55 | 55 | } catch (Exception exp) { |
| 56 | 56 | rtn.put("status", ResponseCode.ERROR); |
| ... | ... | @@ -115,12 +115,12 @@ public class TTInfoDetailController extends BController<TTInfoDetail, Long> { |
| 115 | 115 | } |
| 116 | 116 | return rtn; |
| 117 | 117 | } |
| 118 | - | |
| 118 | + | |
| 119 | 119 | /** |
| 120 | 120 | * 时刻表明细批量插入 |
| 121 | - * | |
| 121 | + * | |
| 122 | 122 | * @param entities |
| 123 | - * | |
| 123 | + * | |
| 124 | 124 | * @return |
| 125 | 125 | */ |
| 126 | 126 | @RequestMapping(value = "/skbDetailMxSave" ,method = RequestMethod.POST) | ... | ... |
src/main/java/com/bsth/service/schedule/TTInfoDetailService.java
| ... | ... | @@ -4,9 +4,9 @@ import com.bsth.entity.schedule.TTInfoDetail; |
| 4 | 4 | import com.bsth.service.schedule.datatools.TTInfoDetailForEdit; |
| 5 | 5 | import com.bsth.service.schedule.datatools.TTinfoDetailDynamicData; |
| 6 | 6 | import com.bsth.service.schedule.exception.ScheduleException; |
| 7 | -import com.bsth.service.schedule.timetable.ExcelFormatType; | |
| 8 | 7 | import com.bsth.service.schedule.utils.DataToolsFile; |
| 9 | 8 | import com.bsth.service.schedule.utils.DataToolsFileType; |
| 9 | +import com.bsth.service.schedule.utils.TimetableExcelData; | |
| 10 | 10 | |
| 11 | 11 | import java.util.List; |
| 12 | 12 | import java.util.Map; |
| ... | ... | @@ -47,7 +47,7 @@ public interface TTInfoDetailService extends BService<TTInfoDetail, Long> { |
| 47 | 47 | * @param sheetname sheet名字 |
| 48 | 48 | * @param lineid 线路id |
| 49 | 49 | * @param lineversion 线路版本 |
| 50 | - * @param excelFormatType 版本 | |
| 50 | + * @param importFormatType excel导入业务格式 | |
| 51 | 51 | */ |
| 52 | 52 | void validateExcelSheet( |
| 53 | 53 | String filename, |
| ... | ... | @@ -55,7 +55,7 @@ public interface TTInfoDetailService extends BService<TTInfoDetail, Long> { |
| 55 | 55 | Integer lineid, |
| 56 | 56 | String linename, |
| 57 | 57 | Integer lineversion, |
| 58 | - ExcelFormatType excelFormatType) throws ScheduleException; | |
| 58 | + TimetableExcelData.ImportFormatType importFormatType) throws Exception; | |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * 验证关联的线路标准信息(以后放到规则引擎里去做)。 | ... | ... |
src/main/java/com/bsth/service/schedule/datatools/TTInfoDetailDataToolsImpl.java
| ... | ... | @@ -85,126 +85,142 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail |
| 85 | 85 | try { |
| 86 | 86 | LOGGER.info("//---------------- 导入时刻表明细 start... ----------------//"); |
| 87 | 87 | |
| 88 | - if (params.get("lineversion") == null) { | |
| 89 | - throw new Exception("线路版本未知"); | |
| 90 | - } | |
| 91 | - | |
| 92 | - String filename = file.getAbsolutePath(); // xls xlsx 文件名 | |
| 93 | - String sheetname = String.valueOf(params.get("sheetname")); // sheet名字 | |
| 94 | - Long ttid = Long.valueOf(String.valueOf(params.get("ttid"))); // 时刻表id | |
| 95 | - Long xlid = Long.valueOf(String.valueOf(params.get("xlid"))); // 线路id | |
| 96 | - Integer lineid = Integer.valueOf(String.valueOf(params.get("lineinfo"))); // 线路标准id | |
| 97 | - String xlname = String.valueOf(params.get("xlname")); // 线路名字 | |
| 98 | - String ttname = String.valueOf(params.get("ttname")); // 时刻表名字 | |
| 99 | - Integer lineversion = Integer.valueOf(params.get("lineversion").toString()); // 线路路由版本 | |
| 100 | - ExcelFormatType excelFormatType = ExcelFormatType.getEnum(String.valueOf(params.get("excelFormatType"))); // 格式 | |
| 101 | - | |
| 102 | - LOGGER.info("参数1, xls文件名={},sheet名字={}", filename, sheetname); | |
| 103 | - LOGGER.info("参数2, 线路id={},线路名字={}", xlid, xlname); | |
| 104 | - LOGGER.info("参数3, 时刻表id={},时刻表名字={}", ttid, ttname); | |
| 105 | - | |
| 106 | - LOGGER.info("转换xls文件格式成文本格式..."); | |
| 107 | - // 1、修改已经上传的excel文件,在每个起点站后标示数字,表示第几个班次 | |
| 108 | - // 2、由于格式问题,需要把内容都转换成字符串 | |
| 109 | - List<String> colList = new ArrayList<>(); | |
| 110 | - | |
| 111 | - // poi api,并读取第一行数据,组合成站点列表,逗号分隔 | |
| 112 | - org.apache.poi.ss.usermodel.Workbook poi_workbook; | |
| 113 | - org.apache.poi.ss.usermodel.Sheet poi_sheet; | |
| 114 | - if (DataToolsFileType.XLS.isThisType(file)) { | |
| 115 | - poi_workbook = DataToolsFileType.XLS.getWorkBook(file); | |
| 116 | - } else if (DataToolsFileType.XLSX.isThisType(file)) { | |
| 117 | - poi_workbook = DataToolsFileType.XLSX.getWorkBook(file); | |
| 118 | - } else { | |
| 119 | - throw new Exception("不是xls xlsx文件!"); | |
| 120 | - } | |
| 121 | - poi_sheet = poi_workbook.getSheet(sheetname); | |
| 122 | - int rownums = poi_sheet.getLastRowNum() + 1; | |
| 123 | - int colnums = poi_sheet.getRow(0).getLastCellNum(); | |
| 124 | - Row firstrow = poi_sheet.getRow(0); | |
| 125 | - for (int i = 0; i < colnums; i++) { | |
| 126 | - org.apache.poi.ss.usermodel.Cell cell = firstrow.getCell(i); | |
| 127 | - if (i == 0) { | |
| 128 | - colList.add(PoiUtils.getStringValueFromCell(cell).trim()); | |
| 129 | - } else { | |
| 130 | - // 站点名字后加->数字形式标示成不同的字段 | |
| 131 | - colList.add(PoiUtils.getStringValueFromCell(cell).trim().replaceAll("(->\\d+)", "") + "->" + i); | |
| 132 | - } | |
| 133 | - } | |
| 134 | - | |
| 135 | - // jxl api | |
| 136 | - File fileCal = new File(filename + "_stringType.xls"); | |
| 137 | - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal); | |
| 138 | - WritableSheet sheet1 = writableWorkbook.createSheet(sheetname, 0); | |
| 139 | - for (int i = 0; i < colnums; i++) { // 第一行数据 | |
| 140 | - sheet1.addCell(new Label(i, 0, colList.get(i))); | |
| 141 | - } | |
| 142 | - for (int i = 1; i < rownums; i++) { // 第二行开始 | |
| 143 | - for (int j = 0; j < colnums; j++) { | |
| 144 | - // poi读 | |
| 145 | - String cellContent = PoiUtils.getStringValueFromCell( | |
| 146 | - poi_sheet.getRow(i).getCell(j) | |
| 147 | - ).replaceAll("\\s*", ""); | |
| 148 | - // jxl写 | |
| 149 | - sheet1.addCell(new Label(j, i, cellContent)); | |
| 150 | - } | |
| 151 | - } | |
| 152 | - writableWorkbook.write(); | |
| 153 | - writableWorkbook.close(); | |
| 154 | - | |
| 155 | - // 2、删除原有数据 | |
| 156 | - // 操作在ktr内部执行 | |
| 157 | - | |
| 158 | - // 3、导入时刻表 | |
| 159 | - | |
| 160 | - // 计算表头参数 | |
| 161 | - Workbook book = Workbook.getWorkbook(fileCal); | |
| 162 | - Sheet sheet_exp = book.getSheet(sheetname); | |
| 163 | - List<String> columnames = new ArrayList<>(); | |
| 164 | - for (int i = 0; i < sheet_exp.getColumns(); i++) { // 获取第一行,数据,作为列名 | |
| 165 | - columnames.add(sheet_exp.getCell(i, 0).getContents()); | |
| 166 | - } | |
| 167 | - LOGGER.info("表头={}", StringUtils.join(columnames.toArray(), ",")); | |
| 168 | - | |
| 169 | - // 创建ktr转换所需参数 | |
| 170 | - Map<String, Object> ktrParms = new HashMap<>(); | |
| 171 | - // 元数据ktr | |
| 172 | - File mktrFile = new File(this.getClass().getResource( | |
| 173 | - dataToolsProperties.getTtinfodetailMetadatainputktr()).toURI()); | |
| 174 | - // 实际数据ktr | |
| 175 | - File ktrFile2_version = new File(this.getClass().getResource( | |
| 176 | - dataToolsProperties.getTtinfodetailDatainputktr2version()).toURI()); | |
| 177 | - // 实际数据ktr2 | |
| 178 | - File ktrFile2_version2 = new File(this.getClass().getResource( | |
| 179 | - dataToolsProperties.getTtinfodetailDatainputktr2version2()).toURI()); | |
| 180 | - | |
| 181 | - | |
| 182 | - // 通用参数,转换文件路径,excel输入文件路径,错误输出文件路径 | |
| 183 | - ktrParms.put("transpath", mktrFile.getAbsolutePath()); | |
| 184 | - ktrParms.put("filepath", fileCal.getAbsolutePath()); | |
| 185 | - ktrParms.put("erroroutputdir", dataToolsProperties.getTransErrordir()); | |
| 186 | - | |
| 187 | - // 附加参数 | |
| 188 | - if (excelFormatType == ExcelFormatType.Normal) { | |
| 189 | - ktrParms.put("injectktrfile", ktrFile2_version.getAbsolutePath()); // 注入元数据的ktr文件 | |
| 190 | - } else if (excelFormatType == ExcelFormatType.Dynamic) { | |
| 191 | - ktrParms.put("injectktrfile", ktrFile2_version2.getAbsolutePath()); // 注入元数据的ktr文件 | |
| 192 | - } else { | |
| 193 | - throw new ScheduleException("未知ExcelFormatType"); | |
| 194 | - } | |
| 195 | - | |
| 196 | - ktrParms.put("sheetname", sheetname); // sheet工作区的名字 | |
| 197 | - ktrParms.put("lineinfoid", lineid); // 线路标准id | |
| 198 | - ktrParms.put("xlname", xlname); // 线路名称 | |
| 199 | - ktrParms.put("xlid", xlid); // 线路id | |
| 200 | - ktrParms.put("ttinfoname", ttname); // 时刻表名称 | |
| 201 | - ktrParms.put("ttid", ttid.intValue()); // 时刻表id | |
| 202 | - ktrParms.put("lineversion", lineversion); // 站点路由版本 | |
| 203 | - ktrParms.put("excelfieldnames", StringUtils.join(columnames.toArray(), ",")); // 时刻表excel输入字段名,以逗号连接 | |
| 204 | - columnames.remove(0); | |
| 205 | - ktrParms.put("normalizefieldnames", StringUtils.join(columnames.toArray(), ",")); // 数据范式化字段名,以逗号连接 | |
| 206 | - | |
| 207 | - dataToolsService.importData(fileCal, ktrParms); | |
| 88 | + TimetableExcelData timetableExcelData = TimetableExcelData.importBuilder() | |
| 89 | + .setImportFormatType(TimetableExcelData.ImportFormatType.fromDesc(String.valueOf(params.get("excelFormatType")))) | |
| 90 | + .setExcelFilePath(file.getAbsolutePath()) | |
| 91 | + .setExcelSheetName(String.valueOf(params.get("sheetname"))) | |
| 92 | + .setLineId(Integer.valueOf(String.valueOf(params.get("xlid")))) | |
| 93 | + .setLineName(String.valueOf(params.get("xlname"))) | |
| 94 | + .setLineRouteVersion(Integer.valueOf(String.valueOf(params.get("lineversion")))) | |
| 95 | + .setLineInfoId(Integer.valueOf(String.valueOf(params.get("lineinfo")))) | |
| 96 | + .setTtInfoId(Long.valueOf(String.valueOf(params.get("ttid")))) | |
| 97 | + .setTtInfoName(String.valueOf(params.get("ttname"))) | |
| 98 | + .setDataToolsProperties(this.dataToolsProperties) | |
| 99 | + .setDataToolsService(this.dataToolsService) | |
| 100 | + .build(); | |
| 101 | + | |
| 102 | + timetableExcelData.doImport(); | |
| 103 | + | |
| 104 | +// if (params.get("lineversion") == null) { | |
| 105 | +// throw new Exception("线路版本未知"); | |
| 106 | +// } | |
| 107 | +// | |
| 108 | +// String filename = file.getAbsolutePath(); // xls xlsx 文件名 | |
| 109 | +// String sheetname = String.valueOf(params.get("sheetname")); // sheet名字 | |
| 110 | +// Long ttid = Long.valueOf(String.valueOf(params.get("ttid"))); // 时刻表id | |
| 111 | +// Long xlid = Long.valueOf(String.valueOf(params.get("xlid"))); // 线路id | |
| 112 | +// Integer lineid = Integer.valueOf(String.valueOf(params.get("lineinfo"))); // 线路标准id | |
| 113 | +// String xlname = String.valueOf(params.get("xlname")); // 线路名字 | |
| 114 | +// String ttname = String.valueOf(params.get("ttname")); // 时刻表名字 | |
| 115 | +// Integer lineversion = Integer.valueOf(params.get("lineversion").toString()); // 线路路由版本 | |
| 116 | +// ExcelFormatType excelFormatType = ExcelFormatType.getEnum(String.valueOf(params.get("excelFormatType"))); // 格式 | |
| 117 | +// | |
| 118 | +// LOGGER.info("参数1, xls文件名={},sheet名字={}", filename, sheetname); | |
| 119 | +// LOGGER.info("参数2, 线路id={},线路名字={}", xlid, xlname); | |
| 120 | +// LOGGER.info("参数3, 时刻表id={},时刻表名字={}", ttid, ttname); | |
| 121 | +// | |
| 122 | +// LOGGER.info("转换xls文件格式成文本格式..."); | |
| 123 | +// // 1、修改已经上传的excel文件,在每个起点站后标示数字,表示第几个班次 | |
| 124 | +// // 2、由于格式问题,需要把内容都转换成字符串 | |
| 125 | +// List<String> colList = new ArrayList<>(); | |
| 126 | +// | |
| 127 | +// // poi api,并读取第一行数据,组合成站点列表,逗号分隔 | |
| 128 | +// org.apache.poi.ss.usermodel.Workbook poi_workbook; | |
| 129 | +// org.apache.poi.ss.usermodel.Sheet poi_sheet; | |
| 130 | +// if (DataToolsFileType.XLS.isThisType(file)) { | |
| 131 | +// poi_workbook = DataToolsFileType.XLS.getWorkBook(file); | |
| 132 | +// } else if (DataToolsFileType.XLSX.isThisType(file)) { | |
| 133 | +// poi_workbook = DataToolsFileType.XLSX.getWorkBook(file); | |
| 134 | +// } else { | |
| 135 | +// throw new Exception("不是xls xlsx文件!"); | |
| 136 | +// } | |
| 137 | +// poi_sheet = poi_workbook.getSheet(sheetname); | |
| 138 | +// int rownums = poi_sheet.getLastRowNum() + 1; | |
| 139 | +// int colnums = poi_sheet.getRow(0).getLastCellNum(); | |
| 140 | +// Row firstrow = poi_sheet.getRow(0); | |
| 141 | +// for (int i = 0; i < colnums; i++) { | |
| 142 | +// org.apache.poi.ss.usermodel.Cell cell = firstrow.getCell(i); | |
| 143 | +// if (i == 0) { | |
| 144 | +// colList.add(PoiUtils.getStringValueFromCell(cell).trim()); | |
| 145 | +// } else { | |
| 146 | +// // 站点名字后加->数字形式标示成不同的字段 | |
| 147 | +// colList.add(PoiUtils.getStringValueFromCell(cell).trim().replaceAll("(->\\d+)", "") + "->" + i); | |
| 148 | +// } | |
| 149 | +// } | |
| 150 | +// | |
| 151 | +// // jxl api | |
| 152 | +// File fileCal = new File(filename + "_stringType.xls"); | |
| 153 | +// WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal); | |
| 154 | +// WritableSheet sheet1 = writableWorkbook.createSheet(sheetname, 0); | |
| 155 | +// for (int i = 0; i < colnums; i++) { // 第一行数据 | |
| 156 | +// sheet1.addCell(new Label(i, 0, colList.get(i))); | |
| 157 | +// } | |
| 158 | +// for (int i = 1; i < rownums; i++) { // 第二行开始 | |
| 159 | +// for (int j = 0; j < colnums; j++) { | |
| 160 | +// // poi读 | |
| 161 | +// String cellContent = PoiUtils.getStringValueFromCell( | |
| 162 | +// poi_sheet.getRow(i).getCell(j) | |
| 163 | +// ).replaceAll("\\s*", ""); | |
| 164 | +// // jxl写 | |
| 165 | +// sheet1.addCell(new Label(j, i, cellContent)); | |
| 166 | +// } | |
| 167 | +// } | |
| 168 | +// writableWorkbook.write(); | |
| 169 | +// writableWorkbook.close(); | |
| 170 | +// | |
| 171 | +// // 2、删除原有数据 | |
| 172 | +// // 操作在ktr内部执行 | |
| 173 | +// | |
| 174 | +// // 3、导入时刻表 | |
| 175 | +// | |
| 176 | +// // 计算表头参数 | |
| 177 | +// Workbook book = Workbook.getWorkbook(fileCal); | |
| 178 | +// Sheet sheet_exp = book.getSheet(sheetname); | |
| 179 | +// List<String> columnames = new ArrayList<>(); | |
| 180 | +// for (int i = 0; i < sheet_exp.getColumns(); i++) { // 获取第一行,数据,作为列名 | |
| 181 | +// columnames.add(sheet_exp.getCell(i, 0).getContents()); | |
| 182 | +// } | |
| 183 | +// LOGGER.info("表头={}", StringUtils.join(columnames.toArray(), ",")); | |
| 184 | +// | |
| 185 | +// // 创建ktr转换所需参数 | |
| 186 | +// Map<String, Object> ktrParms = new HashMap<>(); | |
| 187 | +// // 元数据ktr | |
| 188 | +// File mktrFile = new File(this.getClass().getResource( | |
| 189 | +// dataToolsProperties.getTtinfodetailMetadatainputktr()).toURI()); | |
| 190 | +// // 实际数据ktr | |
| 191 | +// File ktrFile2_version = new File(this.getClass().getResource( | |
| 192 | +// dataToolsProperties.getTtinfodetailDatainputktr2version()).toURI()); | |
| 193 | +// // 实际数据ktr2 | |
| 194 | +// File ktrFile2_version2 = new File(this.getClass().getResource( | |
| 195 | +// dataToolsProperties.getTtinfodetailDatainputktr2version2()).toURI()); | |
| 196 | +// | |
| 197 | +// | |
| 198 | +// // 通用参数,转换文件路径,excel输入文件路径,错误输出文件路径 | |
| 199 | +// ktrParms.put("transpath", mktrFile.getAbsolutePath()); | |
| 200 | +// ktrParms.put("filepath", fileCal.getAbsolutePath()); | |
| 201 | +// ktrParms.put("erroroutputdir", dataToolsProperties.getTransErrordir()); | |
| 202 | +// | |
| 203 | +// // 附加参数 | |
| 204 | +// if (excelFormatType == ExcelFormatType.Normal) { | |
| 205 | +// ktrParms.put("injectktrfile", ktrFile2_version.getAbsolutePath()); // 注入元数据的ktr文件 | |
| 206 | +// } else if (excelFormatType == ExcelFormatType.Dynamic) { | |
| 207 | +// ktrParms.put("injectktrfile", ktrFile2_version2.getAbsolutePath()); // 注入元数据的ktr文件 | |
| 208 | +// } else { | |
| 209 | +// throw new ScheduleException("未知ExcelFormatType"); | |
| 210 | +// } | |
| 211 | +// | |
| 212 | +// ktrParms.put("sheetname", sheetname); // sheet工作区的名字 | |
| 213 | +// ktrParms.put("lineinfoid", lineid); // 线路标准id | |
| 214 | +// ktrParms.put("xlname", xlname); // 线路名称 | |
| 215 | +// ktrParms.put("xlid", xlid); // 线路id | |
| 216 | +// ktrParms.put("ttinfoname", ttname); // 时刻表名称 | |
| 217 | +// ktrParms.put("ttid", ttid.intValue()); // 时刻表id | |
| 218 | +// ktrParms.put("lineversion", lineversion); // 站点路由版本 | |
| 219 | +// ktrParms.put("excelfieldnames", StringUtils.join(columnames.toArray(), ",")); // 时刻表excel输入字段名,以逗号连接 | |
| 220 | +// columnames.remove(0); | |
| 221 | +// ktrParms.put("normalizefieldnames", StringUtils.join(columnames.toArray(), ",")); // 数据范式化字段名,以逗号连接 | |
| 222 | +// | |
| 223 | +// dataToolsService.importData(fileCal, ktrParms); | |
| 208 | 224 | |
| 209 | 225 | LOGGER.info("//---------------- 导入时刻表明细 success... ----------------//"); |
| 210 | 226 | } catch (Exception exp) { | ... | ... |
src/main/java/com/bsth/service/schedule/impl/TTInfoDetailServiceImpl.java
| ... | ... | @@ -24,10 +24,7 @@ import com.bsth.service.schedule.datatools.TTinfoDetailDynamicData; |
| 24 | 24 | import com.bsth.service.schedule.exception.ScheduleException; |
| 25 | 25 | import com.bsth.service.schedule.timetable.ExcelData; |
| 26 | 26 | import com.bsth.service.schedule.timetable.ExcelFormatType; |
| 27 | -import com.bsth.service.schedule.utils.DataToolsFile; | |
| 28 | -import com.bsth.service.schedule.utils.DataToolsFileType; | |
| 29 | -import com.bsth.service.schedule.utils.DataToolsService; | |
| 30 | -import com.bsth.service.schedule.utils.PoiUtils; | |
| 27 | +import com.bsth.service.schedule.utils.*; | |
| 31 | 28 | import org.apache.commons.lang3.StringUtils; |
| 32 | 29 | import org.apache.poi.ss.usermodel.Cell; |
| 33 | 30 | import org.apache.poi.ss.usermodel.Row; |
| ... | ... | @@ -160,13 +157,20 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 160 | 157 | } |
| 161 | 158 | |
| 162 | 159 | @Override |
| 163 | - public void validateExcelSheet(String filename, String sheetname, Integer lineid, String linename, Integer lineversion, ExcelFormatType excelFormatType) throws ScheduleException { | |
| 164 | - ExcelData excelData = new ExcelData( | |
| 165 | - filename, sheetname, lineid, | |
| 166 | - lineService, guideboardInfoService, | |
| 167 | - lineversion, stationRouteService, | |
| 168 | - excelFormatType); | |
| 169 | - excelData.validateExcelSheet(); | |
| 160 | + public void validateExcelSheet( | |
| 161 | + String filename, String sheetname, Integer lineid, String linename, | |
| 162 | + Integer lineversion, TimetableExcelData.ImportFormatType importFormatType) throws Exception { | |
| 163 | + TimetableExcelData timetableExcelData = TimetableExcelData.validateBuilder() | |
| 164 | + .setImportFormatType(importFormatType) | |
| 165 | + .setExcelFilePath(filename) | |
| 166 | + .setExcelSheetName(sheetname) | |
| 167 | + .setLineId(lineid) | |
| 168 | + .setLineService(this.lineService) | |
| 169 | + .setLineRouteVersion(lineversion) | |
| 170 | + .setStationRouteService(this.stationRouteService) | |
| 171 | + .setGuideboardInfoService(this.guideboardInfoService) | |
| 172 | + .build(); | |
| 173 | + timetableExcelData.doValidate(); | |
| 170 | 174 | } |
| 171 | 175 | |
| 172 | 176 | @Override |
| ... | ... | @@ -225,11 +229,11 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 225 | 229 | |
| 226 | 230 | /** |
| 227 | 231 | * @description (TODO) 时刻表明细模型数据保存. |
| 228 | - * | |
| 232 | + * | |
| 229 | 233 | * @param map |
| 230 | - * | |
| 234 | + * | |
| 231 | 235 | * @return : 返回保存操作后的状态. |
| 232 | - * | |
| 236 | + * | |
| 233 | 237 | * @exception 处理所有抛出来的异常. |
| 234 | 238 | * */ |
| 235 | 239 | @Transactional |
| ... | ... | @@ -241,7 +245,7 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 241 | 245 | //boolean b = map.get("istidc") ==null ? false : Boolean.parseBoolean(map.get("istidc").toString()); |
| 242 | 246 | Long ttinfoid = map.get("skb") ==null ? null : Long.parseLong(map.get("skb").toString()); |
| 243 | 247 | Integer xlid = map.get("xl") ==null ? null : Integer.parseInt(map.get("xl").toString()); |
| 244 | - if(xlid !=null && ttinfoid !=null) | |
| 248 | + if(xlid !=null && ttinfoid !=null) | |
| 245 | 249 | ttInfoDetailRepository.deltidc(xlid,ttinfoid); |
| 246 | 250 | if(d!=null) |
| 247 | 251 | ttInfoDetailRepository.save(jsonArrayToListEntity(d));// 2、保存. |
| ... | ... | @@ -252,14 +256,14 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 252 | 256 | rs_m.put("status", ResponseCode.SUCCESS); |
| 253 | 257 | return rs_m; |
| 254 | 258 | } |
| 255 | - | |
| 259 | + | |
| 256 | 260 | /** |
| 257 | 261 | * @description : (TODO) json班次数据转list班次. |
| 258 | - * | |
| 262 | + * | |
| 259 | 263 | * @param jsonStr 班次json字符串] |
| 260 | - * | |
| 264 | + * | |
| 261 | 265 | * @return 返回一个list分装的班次数据. |
| 262 | - * | |
| 266 | + * | |
| 263 | 267 | * @status OK. |
| 264 | 268 | * */ |
| 265 | 269 | public List<TTInfoDetail> jsonArrayToListEntity(String jsonStr) throws Exception { |
| ... | ... | @@ -274,21 +278,21 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 274 | 278 | } |
| 275 | 279 | return listTd; |
| 276 | 280 | } |
| 277 | - | |
| 281 | + | |
| 278 | 282 | /** |
| 279 | 283 | * @description : (TODO) 班次map对象转实体对象. |
| 280 | - * | |
| 284 | + * | |
| 281 | 285 | * @param obj 班次map对象. |
| 282 | - * | |
| 286 | + * | |
| 283 | 287 | * @return 返回一个班次实体对象. |
| 284 | - * | |
| 288 | + * | |
| 285 | 289 | * @exception 异常暂先抛出去. |
| 286 | 290 | * */ |
| 287 | 291 | public TTInfoDetail objToEntity(JSONObject obj) throws Exception { |
| 288 | 292 | // 1、创建班次实体对象. |
| 289 | 293 | TTInfoDetail td = new TTInfoDetail(); |
| 290 | 294 | // 2、获取线路id键值,并查询出该线路的实体对象. |
| 291 | - Line xl = lineRepository.findOne(Integer.parseInt(obj.getString("xl"))); | |
| 295 | + Line xl = lineRepository.findOne(Integer.parseInt(obj.getString("xl"))); | |
| 292 | 296 | // 3、设置线路. |
| 293 | 297 | td.setXl(xl); |
| 294 | 298 | // 4、设置时刻表. |
| ... | ... | @@ -332,12 +336,12 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 332 | 336 | // 22、返回实体对象. |
| 333 | 337 | return td; |
| 334 | 338 | } |
| 335 | - | |
| 339 | + | |
| 336 | 340 | /** |
| 337 | 341 | * @description : (TODO) int转boolean类型. |
| 338 | - * | |
| 342 | + * | |
| 339 | 343 | * @param value--int类型的数值] |
| 340 | - * | |
| 344 | + * | |
| 341 | 345 | * @return : 返回一个布尔类型值. |
| 342 | 346 | * */ |
| 343 | 347 | public Boolean intToBit(int value) { |
| ... | ... | @@ -347,13 +351,13 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 347 | 351 | else if(value ==1) |
| 348 | 352 | tag = true; |
| 349 | 353 | return tag; |
| 350 | - } | |
| 351 | - | |
| 354 | + } | |
| 355 | + | |
| 352 | 356 | /** |
| 353 | 357 | * @description (TODO) 获取路牌. |
| 354 | - * | |
| 358 | + * | |
| 355 | 359 | * @param xl --线路,name--路牌名称,code--路牌编码,lpType--路牌类型] |
| 356 | - * | |
| 360 | + * | |
| 357 | 361 | * @return 返回路牌. |
| 358 | 362 | * */ |
| 359 | 363 | public GuideboardInfo getLp(Line xl,String name, int code, String lpType) throws Exception { |
| ... | ... | @@ -380,18 +384,18 @@ public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> im |
| 380 | 384 | entity.setUpdateBy(null); |
| 381 | 385 | // 8、保存路牌. |
| 382 | 386 | guideboardInfoRepository.save(entity); |
| 383 | - } | |
| 387 | + } | |
| 384 | 388 | // 9、返回路牌. |
| 385 | 389 | return entity; |
| 386 | 390 | } |
| 387 | - | |
| 391 | + | |
| 388 | 392 | /** |
| 389 | 393 | * @description : (TODO) 线路方向转代码. |
| 390 | - * | |
| 394 | + * | |
| 391 | 395 | * @param str--方向字符串] |
| 392 | - * | |
| 396 | + * | |
| 393 | 397 | * @return 返回方向代码. |
| 394 | - * | |
| 398 | + * | |
| 395 | 399 | * @exception 异常暂先抛出. |
| 396 | 400 | * */ |
| 397 | 401 | public String dirToCod(String str) throws Exception { | ... | ... |
src/main/java/com/bsth/service/schedule/utils/TimetableExcelData.java
0 → 100644
| 1 | +package com.bsth.service.schedule.utils; | |
| 2 | + | |
| 3 | +import com.bsth.entity.Line; | |
| 4 | +import com.bsth.entity.LsStationRoute; | |
| 5 | +import com.bsth.entity.schedule.GuideboardInfo; | |
| 6 | +import com.bsth.service.LineService; | |
| 7 | +import com.bsth.service.StationRouteService; | |
| 8 | +import com.bsth.service.schedule.GuideboardInfoService; | |
| 9 | +import com.bsth.service.schedule.exception.ScheduleException; | |
| 10 | +import com.fasterxml.jackson.annotation.JsonCreator; | |
| 11 | +import com.fasterxml.jackson.annotation.JsonValue; | |
| 12 | +import jxl.write.Label; | |
| 13 | +import jxl.write.WritableSheet; | |
| 14 | +import jxl.write.WritableWorkbook; | |
| 15 | +import org.apache.commons.lang3.StringUtils; | |
| 16 | +import org.apache.poi.ss.usermodel.Sheet; | |
| 17 | +import org.apache.poi.ss.usermodel.Workbook; | |
| 18 | +import org.slf4j.Logger; | |
| 19 | +import org.slf4j.LoggerFactory; | |
| 20 | +import org.springframework.util.Assert; | |
| 21 | +import org.springframework.util.CollectionUtils; | |
| 22 | + | |
| 23 | +import java.io.File; | |
| 24 | +import java.util.ArrayList; | |
| 25 | +import java.util.HashMap; | |
| 26 | +import java.util.List; | |
| 27 | +import java.util.Map; | |
| 28 | +import java.util.regex.Matcher; | |
| 29 | +import java.util.regex.Pattern; | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * 时刻表Excel数据类。 | |
| 33 | + */ | |
| 34 | +public class TimetableExcelData { | |
| 35 | + /** 日志记录器 */ | |
| 36 | + private final static Logger LOG = LoggerFactory.getLogger(TimetableExcelData.class); | |
| 37 | + | |
| 38 | + //-------------------- 公用属性,方法(如下:)-----------------// | |
| 39 | + /** 待导入excel workbook对象 */ | |
| 40 | + private Workbook excelWorkBook; | |
| 41 | + /** 待导入excel workbook 工作区对象 */ | |
| 42 | + private Sheet excelWorkBookSheet; | |
| 43 | + /** excel导入格式类型 */ | |
| 44 | + private ImportFormatType importFormatType; | |
| 45 | + | |
| 46 | + // 内部标识excel单元格类 | |
| 47 | + private static class TimetableExcelInternalCell { | |
| 48 | + /** 是否可用(默认不可用) */ | |
| 49 | + private Boolean isEnable = false; | |
| 50 | + /** 单元格内容 */ | |
| 51 | + private String cellContent; | |
| 52 | + | |
| 53 | + //------------- 班次类型(发车时间单元格 isFcsjCell = true)-----------// | |
| 54 | + /** 是否是班次 */ | |
| 55 | + private Boolean isBc = false; | |
| 56 | + /** 是否是出场班次 */ | |
| 57 | + private Boolean isOutBc = false; | |
| 58 | + /** 是否是进场班次 */ | |
| 59 | + private Boolean isInBc = false; | |
| 60 | + | |
| 61 | + //------------- 单元格类型 -------------// | |
| 62 | + /** 是否表头Cell */ | |
| 63 | + private Boolean isHeadCell = false; | |
| 64 | + /** 是否路牌名字Cell */ | |
| 65 | + private Boolean isLpNameCell = false; | |
| 66 | + /** 是否发车时间Cell */ | |
| 67 | + private Boolean isFcsjCell = false; | |
| 68 | + /** 是否行驶时间Cell */ | |
| 69 | + private Boolean isXssjCell = false; | |
| 70 | + } | |
| 71 | + /** 内部Excel单元格二维数组 */ | |
| 72 | + private TimetableExcelInternalCell[][] internalExcelCells; | |
| 73 | + | |
| 74 | + // 初始化内部标识excel单元格 | |
| 75 | + private void initInternalExcelCells() { | |
| 76 | + int rowCount = this.excelWorkBookSheet.getLastRowNum() + 1; // 基于0 base的,长度加1 | |
| 77 | + int colCount = this.excelWorkBookSheet.getRow(0).getLastCellNum(); // 不需要加1,就是长度 | |
| 78 | + | |
| 79 | + this.internalExcelCells = new TimetableExcelInternalCell[rowCount][colCount]; | |
| 80 | + for (int rowNum = 0; rowNum < rowCount; rowNum ++) { | |
| 81 | + for (int colNum = 0; colNum < colCount; colNum ++) { | |
| 82 | + TimetableExcelInternalCell cell = new TimetableExcelInternalCell(); | |
| 83 | + String cell_con = StringUtils.trimToEmpty(PoiUtils.getStringValueFromCell( | |
| 84 | + this.excelWorkBookSheet.getRow(rowNum).getCell(colNum)) | |
| 85 | + .replaceAll("\\s*", "")); // trimToEmpty | |
| 86 | + | |
| 87 | + // 如果站名中有类似->{数字},使用正则表达式过滤掉 | |
| 88 | + cell_con = cell_con.replaceAll("(->\\d+)", ""); | |
| 89 | + | |
| 90 | + cell.cellContent = cell_con; | |
| 91 | + cell.isEnable = true; | |
| 92 | + this.internalExcelCells[rowNum][colNum] = cell; | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + } | |
| 97 | + // 使用第一行单元格的属性设其他行单元格的属性 | |
| 98 | + private void setInternalExcelCellPropertyWithFirstRowCell() { | |
| 99 | + for (int rowNum = 1; rowNum < this.internalExcelCells.length; rowNum ++) { | |
| 100 | + for (int colNum = 0; colNum < this.internalExcelCells[rowNum].length; colNum ++) { | |
| 101 | + TimetableExcelInternalCell cell = this.internalExcelCells[rowNum][colNum]; | |
| 102 | + TimetableExcelInternalCell firstRowCell = this.internalExcelCells[0][colNum]; | |
| 103 | + if (firstRowCell.isEnable) { | |
| 104 | + cell.isBc = firstRowCell.isBc; | |
| 105 | + cell.isInBc = firstRowCell.isInBc; | |
| 106 | + cell.isOutBc = firstRowCell.isOutBc; | |
| 107 | + | |
| 108 | + cell.isLpNameCell = firstRowCell.isLpNameCell; | |
| 109 | + cell.isFcsjCell = firstRowCell.isFcsjCell; | |
| 110 | + cell.isXssjCell = firstRowCell.isXssjCell; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + //-------------------- 公用属性,方法(如上:)-----------------// | |
| 117 | + | |
| 118 | + private TimetableExcelData() {} | |
| 119 | + | |
| 120 | + | |
| 121 | + //-------------------- 验证相关属性,方法(如下:)------------------// | |
| 122 | + /** 线路信息 */ | |
| 123 | + private Line line; | |
| 124 | + /** 线路站点路由列表(包含上下行的起终点站) */ | |
| 125 | + private List<LsStationRoute> lsStationRouteList; | |
| 126 | + /** 路牌列表 */ | |
| 127 | + private List<GuideboardInfo> guideboardInfoList; | |
| 128 | + | |
| 129 | + private void validateRouteName(int colNum, String routeName) throws ScheduleException { | |
| 130 | + if (StringUtils.isEmpty(routeName)) { | |
| 131 | + throw new ScheduleException(String.format("第1行,第%d列数据不能为空", colNum + 1)); | |
| 132 | + } | |
| 133 | + | |
| 134 | + List<LsStationRoute> lsStationRoutes = new ArrayList<>(); | |
| 135 | + for (LsStationRoute lsStationRoute : this.lsStationRouteList) { | |
| 136 | + if ("B".equals(lsStationRoute.getStationMark()) && routeName.equals(lsStationRoute.getStationName())) { | |
| 137 | + lsStationRoutes.add(lsStationRoute); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + if (CollectionUtils.isEmpty(lsStationRoutes)) { | |
| 142 | + throw new ScheduleException(String.format("第1行,第%d列数据%s在%s站点路由中不是起点站", | |
| 143 | + colNum + 1, routeName, this.line.getName())); | |
| 144 | + } else if (lsStationRoutes.size() > 1) { | |
| 145 | + throw new ScheduleException(String.format("第1行,第%d列数据%s在%s站点路由中上下行都是起点站", | |
| 146 | + colNum + 1, routeName, this.line.getName())); | |
| 147 | + } else if (StringUtils.isEmpty(lsStationRoutes.get(0).getStationCode())) { | |
| 148 | + throw new ScheduleException(String.format("第1行,第%d列数据%s在%s站点路由中没有站点编码", | |
| 149 | + colNum + 1, routeName, this.line.getName())); | |
| 150 | + } | |
| 151 | + | |
| 152 | + } | |
| 153 | + | |
| 154 | + private void validateGuideboardName(int rowNum, String guideboardName) throws ScheduleException { | |
| 155 | + if (StringUtils.isEmpty(guideboardName)) { | |
| 156 | + throw new ScheduleException(String.format("第%d行,第1列路牌无数据", rowNum + 1)); | |
| 157 | + } | |
| 158 | + | |
| 159 | + List<GuideboardInfo> guideboardInfos = new ArrayList<>(); | |
| 160 | + for (GuideboardInfo guideboardInfo : this.guideboardInfoList) { | |
| 161 | + if (guideboardName.equals(guideboardInfo.getLpName())) { | |
| 162 | + guideboardInfos.add(guideboardInfo); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + if (CollectionUtils.isEmpty(guideboardInfos)) { | |
| 167 | + throw new ScheduleException(String.format("第%d行,第1列的路牌在%s中不存在", | |
| 168 | + rowNum + 1, this.line.getName())); | |
| 169 | + } else if (guideboardInfos.size() > 1) { | |
| 170 | + throw new ScheduleException(String.format("第%d行,第1列的路牌在%s中重复", | |
| 171 | + rowNum + 1, this.line.getName())); | |
| 172 | + } | |
| 173 | + } | |
| 174 | + private void validateFcsjContent(int rowNum, int colNum, String fcsjContent) throws ScheduleException { | |
| 175 | + if (!this.importFormatType.validateFcsjContent(fcsjContent)) { | |
| 176 | + if (this.importFormatType == ImportFormatType.Dynamic) { | |
| 177 | + throw new ScheduleException(String.format( | |
| 178 | + "第%d行,第%d列的发车时间格式不正确,格式应为(报|出)hh:mm或hh:mm(X|※)", | |
| 179 | + rowNum + 1, colNum + 1)); | |
| 180 | + } else { | |
| 181 | + throw new ScheduleException(String.format("第%d行,第%d列的发车时间格式不正确,格式应为hh:mm或hh,mm或hhmm", | |
| 182 | + rowNum + 1, colNum + 1)); | |
| 183 | + } | |
| 184 | + } | |
| 185 | + } | |
| 186 | + private void validateXssjContent(int rowNum, int colNum, String xssjContent) throws ScheduleException { | |
| 187 | + if (!this.importFormatType.validateXssjContent(xssjContent)) { | |
| 188 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间格式不正确,格式应为正整数", | |
| 189 | + rowNum + 1, colNum + 1)); | |
| 190 | + } | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * 验证Excel。 | |
| 195 | + */ | |
| 196 | + public void doValidate() throws Exception { | |
| 197 | + int rowCount = this.excelWorkBookSheet.getLastRowNum() + 1; // 基于0 base的,长度加1 | |
| 198 | + int colCount = this.excelWorkBookSheet.getRow(0).getLastCellNum(); // 不需要加1,就是长度 | |
| 199 | + if (rowCount == 0 || colCount == 0) { // 工作区是否为空 | |
| 200 | + throw new ScheduleException(String.format("%s 工作区没有数据!", this.excelWorkBookSheet.getSheetName())); | |
| 201 | + } | |
| 202 | + if (rowCount <= 1 || colCount <= 1) { | |
| 203 | + throw new ScheduleException("工作区至少包含2行2列的数据"); | |
| 204 | + } | |
| 205 | + | |
| 206 | + // 1、创建内部Excel单元格数组 | |
| 207 | + this.initInternalExcelCells(); | |
| 208 | + | |
| 209 | + // 2、验证单元格信息 | |
| 210 | + // 2-1、验证第1行数据(表头行数据) | |
| 211 | + for (int colNum = 0; colNum < colCount; colNum ++) { | |
| 212 | + TimetableExcelInternalCell cell = this.internalExcelCells[0][colNum]; | |
| 213 | + cell.isHeadCell = true; | |
| 214 | + | |
| 215 | + // 2-1-1、第一行,第一列必须路牌2个字 | |
| 216 | + if (colNum == 0) { | |
| 217 | + if (!"路牌".equals(cell.cellContent)) { | |
| 218 | + throw new ScheduleException("第1行,第1列数据必须是路牌2个字"); | |
| 219 | + } | |
| 220 | + cell.isLpNameCell = true; | |
| 221 | + continue; | |
| 222 | + } | |
| 223 | + | |
| 224 | + // 2-1-2、如果导入格式是Dynamic,忽略指定列 | |
| 225 | + if (this.importFormatType == ImportFormatType.Dynamic) { | |
| 226 | + if ("报到".equals(cell.cellContent) || | |
| 227 | + "到场".equals(cell.cellContent) || | |
| 228 | + "离场".equals(cell.cellContent) || | |
| 229 | + "总公里".equals(cell.cellContent) || | |
| 230 | + "营业公里".equals(cell.cellContent) || | |
| 231 | + "空驶公里".equals(cell.cellContent) || | |
| 232 | + "总工时".equals(cell.cellContent) || | |
| 233 | + "营业工时".equals(cell.cellContent) || | |
| 234 | + "营运班次".equals(cell.cellContent)) { | |
| 235 | + // 这些列都忽略,不参与计算 | |
| 236 | + cell.isEnable = false; | |
| 237 | + continue; | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 241 | + // 2-1-3、出场,进场班次,不验证路由 | |
| 242 | + if ("出场".equals(cell.cellContent)) { | |
| 243 | + cell.isBc = true; | |
| 244 | + cell.isOutBc = true; | |
| 245 | + continue; | |
| 246 | + } | |
| 247 | + if ("进场".equals(cell.cellContent)) { | |
| 248 | + cell.isBc = true; | |
| 249 | + cell.isInBc = true; | |
| 250 | + continue; | |
| 251 | + } | |
| 252 | + | |
| 253 | + // 2-1-4、如果导入类型是NormalWithXssj,空的列表表示行驶时间列,不做站点路由验证 | |
| 254 | + if (this.importFormatType == ImportFormatType.NormalWithXssj) { | |
| 255 | + if ("".equals(cell.cellContent)) { // 空的cell默认是行驶时间列头 | |
| 256 | + cell.isXssjCell = true; | |
| 257 | + continue; | |
| 258 | + } | |
| 259 | + } | |
| 260 | + | |
| 261 | + // 2-1-5、其他情况作为站点路由验证 | |
| 262 | + cell.isBc = true; | |
| 263 | + cell.isFcsjCell = true; | |
| 264 | + validateRouteName(colNum, cell.cellContent); // 验证路由名字 | |
| 265 | + | |
| 266 | + } | |
| 267 | + | |
| 268 | + // 2-2、根据第1行的单元格验证后的设置的属性,设置其余行单元格的属性 | |
| 269 | + this.setInternalExcelCellPropertyWithFirstRowCell(); | |
| 270 | + | |
| 271 | + // 2-3、验证具体路牌单元格(第1列) | |
| 272 | + Map<String, Integer> gbindexmap = new HashMap<>(); // 记录每个路牌在第几行 | |
| 273 | + for (int rowNum = 1; rowNum < rowCount; rowNum ++) { | |
| 274 | + TimetableExcelInternalCell cell = this.internalExcelCells[rowNum][0]; | |
| 275 | + if (StringUtils.isEmpty(cell.cellContent)) { | |
| 276 | + throw new ScheduleException(String.format("第%d行,第1列路牌无数据", rowNum + 1)); | |
| 277 | + } else if (gbindexmap.get(cell.cellContent) != null) { | |
| 278 | + throw new ScheduleException(String.format("第%d行,第1列的路牌数据与第%d行,第1列数据重复", | |
| 279 | + rowNum + 1, | |
| 280 | + gbindexmap.get(cell.cellContent))); | |
| 281 | + } else { | |
| 282 | + validateGuideboardName(rowNum, cell.cellContent); // 验证路牌名字 | |
| 283 | + gbindexmap.put(cell.cellContent, rowNum + 1); | |
| 284 | + } | |
| 285 | + } | |
| 286 | + | |
| 287 | + // 2-4、验证其余单元格(第2行,第2列开始) | |
| 288 | + for (int rowNum = 1; rowNum < rowCount; rowNum ++) { | |
| 289 | + for (int colNum = 1; colNum < colCount; colNum ++) { | |
| 290 | + TimetableExcelInternalCell cell = this.internalExcelCells[rowNum][colNum]; | |
| 291 | + if (!cell.isEnable) { | |
| 292 | + continue; | |
| 293 | + } | |
| 294 | + | |
| 295 | + if (cell.isBc) { | |
| 296 | + // 对应路牌对应单元格没有发车班次 | |
| 297 | + if (StringUtils.isNotEmpty(cell.cellContent)) { | |
| 298 | + validateFcsjContent(rowNum, colNum, cell.cellContent); | |
| 299 | + } | |
| 300 | + } else if (cell.isXssjCell) { | |
| 301 | + TimetableExcelInternalCell preCell = this.internalExcelCells[rowNum][colNum - 1]; | |
| 302 | + if (StringUtils.isEmpty(cell.cellContent)) { | |
| 303 | + if (StringUtils.isEmpty(preCell.cellContent)) { | |
| 304 | + // 当前行驶时间单元格为空,前一个单元格也为空,跳过 | |
| 305 | + continue; | |
| 306 | + } else { | |
| 307 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间为空(前一个单元格不为空)!", rowNum + 1, colNum + 1)); | |
| 308 | + } | |
| 309 | + } else { | |
| 310 | + if (StringUtils.isEmpty(preCell.cellContent)) { | |
| 311 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间(前一个单元格为空)!", rowNum + 1, colNum + 1)); | |
| 312 | + } else { | |
| 313 | + if (!preCell.isBc) { | |
| 314 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间同行前一个单元格必须是班次发车时间!", rowNum + 1, colNum + 1)); | |
| 315 | + } else if (preCell.isInBc) { | |
| 316 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间同行前一个单元格不能是进场班次!", rowNum + 1, colNum + 1)); | |
| 317 | + } else if (preCell.isOutBc) { | |
| 318 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间同行前一个单元格不能是出场班次!", rowNum + 1, colNum + 1)); | |
| 319 | + } else if (StringUtils.isEmpty(preCell.cellContent)) { | |
| 320 | + throw new ScheduleException(String.format("第%d行,第%d列的行驶时间同行前一个单元格的班次发车时间不能为空!", rowNum + 1, colNum + 1)); | |
| 321 | + } else { | |
| 322 | + validateXssjContent(rowNum, colNum, cell.cellContent); | |
| 323 | + } | |
| 324 | + } | |
| 325 | + } | |
| 326 | + } else { | |
| 327 | + throw new ScheduleException(String.format("第%d行,第%d列的单元格内容既不是发车时间,也不是行驶时间!", rowNum + 1, colNum + 1)); | |
| 328 | + } | |
| 329 | + } | |
| 330 | + } | |
| 331 | + | |
| 332 | + } | |
| 333 | + public static ValidateBuilder validateBuilder() { | |
| 334 | + return new ValidateBuilder(); | |
| 335 | + } | |
| 336 | + public TimetableExcelData(ValidateBuilder validateBuilder) { | |
| 337 | + this.excelWorkBook = validateBuilder.excelWorkBook; | |
| 338 | + this.excelWorkBookSheet = validateBuilder.excelWorkBookSheet; | |
| 339 | + this.line = validateBuilder.line; | |
| 340 | + this.lsStationRouteList = validateBuilder.lsStationRouteList; | |
| 341 | + this.guideboardInfoList = validateBuilder.guideboardInfoList; | |
| 342 | + this.importFormatType = validateBuilder.importFormatType; | |
| 343 | + } | |
| 344 | + //-------------------- 验证相关属性,方法(如上:)------------------// | |
| 345 | + | |
| 346 | + //-------------------- 导入相关属性,方法(如下:)------------------// | |
| 347 | + /** 导入的Excel文件路径 */ | |
| 348 | + private String excelFilePath; | |
| 349 | + /** 数据工具服务 */ | |
| 350 | + private DataToolsService dataToolsService; | |
| 351 | + /** 配置数据导入导出用到的配置信息 */ | |
| 352 | + private DataToolsProperties dataToolsProperties; | |
| 353 | + /** ktr转换所需参数对象 */ | |
| 354 | + private Map<String, Object> ktrParams = new HashMap<>(); | |
| 355 | + /** | |
| 356 | + * 导入Excel。 | |
| 357 | + */ | |
| 358 | + public void doImport() throws Exception { | |
| 359 | + // 注意:这里不做验证,业务上导入前必须验证的(调用验证用的builder) | |
| 360 | + | |
| 361 | + // 1、创建内部单元格数据 | |
| 362 | + // 1-1、创建内部Excel单元格数组 | |
| 363 | + this.initInternalExcelCells(); | |
| 364 | + // 1-2、计算第一行,并设置单元格属性 | |
| 365 | + for (int colNum = 0; colNum < this.internalExcelCells[0].length; colNum ++) { | |
| 366 | + TimetableExcelInternalCell cell = this.internalExcelCells[0][colNum]; | |
| 367 | + cell.isHeadCell = true; | |
| 368 | + if (colNum == 0) { | |
| 369 | + cell.isLpNameCell = true; | |
| 370 | + continue; | |
| 371 | + } | |
| 372 | + if (this.importFormatType == ImportFormatType.Dynamic) { | |
| 373 | + if ("报到".equals(cell.cellContent) || | |
| 374 | + "到场".equals(cell.cellContent) || | |
| 375 | + "离场".equals(cell.cellContent) || | |
| 376 | + "总公里".equals(cell.cellContent) || | |
| 377 | + "营业公里".equals(cell.cellContent) || | |
| 378 | + "空驶公里".equals(cell.cellContent) || | |
| 379 | + "总工时".equals(cell.cellContent) || | |
| 380 | + "营业工时".equals(cell.cellContent) || | |
| 381 | + "营运班次".equals(cell.cellContent)) { | |
| 382 | + // 这些列都忽略,不参与计算 | |
| 383 | + cell.isEnable = false; | |
| 384 | + continue; | |
| 385 | + } | |
| 386 | + } | |
| 387 | + // 2-1-3、出场,进场班次,不验证路由 | |
| 388 | + if ("出场".equals(cell.cellContent)) { | |
| 389 | + cell.isBc = true; | |
| 390 | + cell.isOutBc = true; | |
| 391 | + continue; | |
| 392 | + } | |
| 393 | + if ("进场".equals(cell.cellContent)) { | |
| 394 | + cell.isBc = true; | |
| 395 | + cell.isInBc = true; | |
| 396 | + continue; | |
| 397 | + } | |
| 398 | + | |
| 399 | + // 2-1-4、如果导入类型是NormalWithXssj,空的列表表示行驶时间列,不做站点路由验证 | |
| 400 | + if (this.importFormatType == ImportFormatType.NormalWithXssj) { | |
| 401 | + if ("".equals(cell.cellContent)) { // 空的cell默认是行驶时间列头 | |
| 402 | + cell.isXssjCell = true; | |
| 403 | + continue; | |
| 404 | + } | |
| 405 | + } | |
| 406 | + // 2-1-5、其他情况作为站点路由验证 | |
| 407 | + cell.isBc = true; | |
| 408 | + cell.isFcsjCell = true; | |
| 409 | + | |
| 410 | + } | |
| 411 | + // 1-3、根据第1行的单元格验证后的设置的属性,设置其余行单元格的属性 | |
| 412 | + this.setInternalExcelCellPropertyWithFirstRowCell(); | |
| 413 | + | |
| 414 | + // 2、重新构造一个新的单元格数组,去除enable=false的,合并发车时间和行驶时间 | |
| 415 | + List<List<TimetableExcelInternalCell>> newEnabledExcelCells = new ArrayList<>(); | |
| 416 | + for (int rowNum = 0; rowNum < this.internalExcelCells.length; rowNum ++) { | |
| 417 | + List<TimetableExcelInternalCell> newEnabledExcelRowCells = new ArrayList<>(); | |
| 418 | + newEnabledExcelCells.add(newEnabledExcelRowCells); | |
| 419 | + | |
| 420 | + for (int colNum = 0; colNum < this.internalExcelCells[rowNum].length; colNum ++) { | |
| 421 | + TimetableExcelInternalCell cell = this.internalExcelCells[rowNum][colNum]; | |
| 422 | + if (!cell.isEnable) { | |
| 423 | + continue; | |
| 424 | + } | |
| 425 | + if (cell.isXssjCell) { // 如果匹配到行驶时间单元格,需要合并到前一个单元格中 | |
| 426 | + if (StringUtils.isNotEmpty(cell.cellContent)) { | |
| 427 | + // 前一个单元格就是当前已经保存的行单元格列表的最后一个 | |
| 428 | + TimetableExcelInternalCell preNewEnabledCell = | |
| 429 | + newEnabledExcelRowCells.get(newEnabledExcelRowCells.size() - 1); | |
| 430 | + // 合并成 12:01->30 这种格式 | |
| 431 | + preNewEnabledCell.cellContent = preNewEnabledCell.cellContent + "->" + cell.cellContent; | |
| 432 | + } | |
| 433 | + } else { | |
| 434 | + newEnabledExcelRowCells.add(cell); | |
| 435 | + } | |
| 436 | + } | |
| 437 | + } | |
| 438 | + | |
| 439 | + // 3、计算其他ktr参数 | |
| 440 | + // 3-1、计算字段名相关参数 | |
| 441 | + List<String> columnNames = new ArrayList<>(); | |
| 442 | + for (int colNum = 0; colNum < newEnabledExcelCells.get(0).size(); colNum ++) { | |
| 443 | + TimetableExcelInternalCell cell = newEnabledExcelCells.get(0).get(colNum); | |
| 444 | + if (colNum == 0) { | |
| 445 | + columnNames.add(cell.cellContent); | |
| 446 | + } else { | |
| 447 | + columnNames.add(cell.cellContent + "->" + colNum); | |
| 448 | + } | |
| 449 | + } | |
| 450 | + LOG.info("表头参数={}", StringUtils.join(columnNames.toArray(), ",")); | |
| 451 | + this.ktrParams.put("excelfieldnames", StringUtils.join(columnNames.toArray(), ",")); // 时刻表excel输入字段名,以逗号连接 | |
| 452 | + columnNames.remove(0); | |
| 453 | + this.ktrParams.put("normalizefieldnames", StringUtils.join(columnNames.toArray(), ",")); // 数据范式化字段名,以逗号连接 | |
| 454 | + | |
| 455 | + // 3-2、计算excel内容文件参数 | |
| 456 | + File fileCal = new File(this.excelFilePath + "_stringType.xls"); | |
| 457 | + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal); | |
| 458 | + WritableSheet sheet1 = writableWorkbook.createSheet(this.excelWorkBookSheet.getSheetName(), 0); | |
| 459 | + for (int rowNum = 0; rowNum < newEnabledExcelCells.size(); rowNum ++) { | |
| 460 | + for (int colNum = 0; colNum < newEnabledExcelCells.get(rowNum).size(); colNum ++) { | |
| 461 | + TimetableExcelInternalCell cell = newEnabledExcelCells.get(rowNum).get(colNum); | |
| 462 | + sheet1.addCell(new Label(colNum, rowNum, cell.cellContent)); | |
| 463 | + } | |
| 464 | + } | |
| 465 | + writableWorkbook.write(); | |
| 466 | + writableWorkbook.close(); | |
| 467 | + this.ktrParams.put("filepath", fileCal.getAbsolutePath()); | |
| 468 | + | |
| 469 | + // 3-3、计算ktr路径相关参数 | |
| 470 | + // 元数据ktr | |
| 471 | + File mktrFile = new File(this.getClass().getResource( | |
| 472 | + dataToolsProperties.getTtinfodetailMetadatainputktr()).toURI()); | |
| 473 | + this.ktrParams.put("transpath", mktrFile.getAbsolutePath()); | |
| 474 | + // 实际数据ktr | |
| 475 | + if (this.importFormatType == ImportFormatType.NormalWithXssj) { | |
| 476 | + File normal_ktrFile = new File(this.getClass().getResource( | |
| 477 | + this.dataToolsProperties.getTtinfodetailDatainputktr2version()).toURI()); | |
| 478 | + this.ktrParams.put("injectktrfile", normal_ktrFile.getAbsolutePath()); | |
| 479 | + } | |
| 480 | + if (this.importFormatType == ImportFormatType.Dynamic) { | |
| 481 | + File dynamic_ktrFile = new File(this.getClass().getResource( | |
| 482 | + this.dataToolsProperties.getTtinfodetailDatainputktr2version2()).toURI()); | |
| 483 | + this.ktrParams.put("injectktrfile", dynamic_ktrFile.getAbsolutePath()); | |
| 484 | + } | |
| 485 | + | |
| 486 | + // 3-4、执行ktr | |
| 487 | + this.dataToolsService.importData(fileCal, this.ktrParams); | |
| 488 | + | |
| 489 | + } | |
| 490 | + public static ImportBuilder importBuilder() { | |
| 491 | + return new ImportBuilder(); | |
| 492 | + } | |
| 493 | + public TimetableExcelData(ImportBuilder importBuilder) { | |
| 494 | + this.excelWorkBook = importBuilder.excelWorkBook; | |
| 495 | + this.excelWorkBookSheet = importBuilder.excelWorkBookSheet; | |
| 496 | + this.importFormatType = importBuilder.importFormatType; | |
| 497 | + | |
| 498 | + this.excelFilePath = importBuilder.excelFilePath; | |
| 499 | + this.dataToolsService = importBuilder.dataToolsService; | |
| 500 | + this.dataToolsProperties = importBuilder.dataToolsProperties; | |
| 501 | + this.ktrParams = importBuilder.ktrParams; | |
| 502 | + | |
| 503 | + LOG.info("参数组1, xls文件名={},sheet名字={}", | |
| 504 | + this.excelFilePath, this.ktrParams.get("sheetname")); | |
| 505 | + LOG.info("参数组2, 线路id={},线路名字={},线路版本={}", | |
| 506 | + this.ktrParams.get("xlid"), this.ktrParams.get("xlname"), this.ktrParams.get("lineversion")); | |
| 507 | + LOG.info("参数组3, 时刻表id={},时刻表名字={}", | |
| 508 | + this.ktrParams.get("ttid"), this.ktrParams.get("ttinfoname")); | |
| 509 | + | |
| 510 | + } | |
| 511 | + //-------------------- 导入相关属性,方法(如上:)------------------// | |
| 512 | + | |
| 513 | + /** | |
| 514 | + * excel导入格式。 | |
| 515 | + */ | |
| 516 | + public enum ImportFormatType { | |
| 517 | + NormalWithXssj("normal") { // 老系统格式(可以在发车时间后带一个行驶时间) | |
| 518 | + @Override | |
| 519 | + boolean validateFcsjContent(String fcsjContent) { | |
| 520 | + Matcher m1 = p1.matcher(fcsjContent.trim()); | |
| 521 | + Matcher m2 = p2.matcher(fcsjContent.trim()); | |
| 522 | + Matcher m3 = p3.matcher(fcsjContent.trim()); | |
| 523 | + return m1.matches() || m2.matches() || m3.matches(); | |
| 524 | + } | |
| 525 | + @Override | |
| 526 | + boolean validateXssjContent(String xssjContent) { | |
| 527 | + Matcher m1 = xssj_p.matcher(xssjContent.trim()); | |
| 528 | + return m1.matches(); | |
| 529 | + } | |
| 530 | + }, | |
| 531 | + Dynamic("dynamic") { // 自动生成时刻表的导出格式 | |
| 532 | + @Override | |
| 533 | + boolean validateFcsjContent(String fcsjContent) { | |
| 534 | + Matcher m1 = p1_1.matcher(fcsjContent.trim()); | |
| 535 | + Matcher m2 = p2_1.matcher(fcsjContent.trim()); | |
| 536 | + return m1.matches() || m2.matches(); | |
| 537 | + } | |
| 538 | + @Override | |
| 539 | + boolean validateXssjContent(String xssjContent) { | |
| 540 | + throw new IllegalStateException("Dynamic导入格式下没有行驶时间单元格!"); | |
| 541 | + } | |
| 542 | + }; | |
| 543 | + | |
| 544 | + // NormalWithXssj格式下的发车时间格式 | |
| 545 | + private static Pattern p1 = Pattern.compile("^([01]?[0-9]|2[0-3]):[0-5][0-9]$"); // hh:mm格式 | |
| 546 | + private static Pattern p2 = Pattern.compile("^([01]?[0-9]|2[0-3]),[0-5][0-9]$"); // hh,mm格式 | |
| 547 | + private static Pattern p3 = Pattern.compile("^([01]?[0-9]|2[0-3])[0-5][0-9]$"); // hhmm格式 | |
| 548 | + | |
| 549 | + // Dynamic格式下的发车时间格式 | |
| 550 | + private static Pattern p1_1 = Pattern.compile("^(\u62a5|\u51fa)?([01]?[0-9]|2[0-3]):[0-5][0-9]$"); // (报|出)hh:mm格式 | |
| 551 | + private static Pattern p2_1 = Pattern.compile("^([01]?[0-9]|2[0-3]):[0-5][0-9](X|\u203b)?$"); // hh:mm(X|※)格式 | |
| 552 | + | |
| 553 | + // NormalWithXssj格式下的行驶时时间格式 | |
| 554 | + private static Pattern xssj_p = Pattern.compile("^[1-9]\\d*$"); // 正整数 | |
| 555 | + | |
| 556 | + private static Map<String, ImportFormatType> descMapEnum = new HashMap<>(); | |
| 557 | + static { | |
| 558 | + descMapEnum.put("normal", NormalWithXssj); | |
| 559 | + descMapEnum.put("dynamic", Dynamic); | |
| 560 | + } | |
| 561 | + | |
| 562 | + private String desc; | |
| 563 | + | |
| 564 | + @JsonValue | |
| 565 | + public String getDesc() { | |
| 566 | + return desc; | |
| 567 | + } | |
| 568 | + public void setDesc(String desc) { | |
| 569 | + this.desc = desc; | |
| 570 | + } | |
| 571 | + | |
| 572 | + @JsonCreator | |
| 573 | + ImportFormatType(String desc) { | |
| 574 | + this.desc = desc; | |
| 575 | + } | |
| 576 | + | |
| 577 | + public static ImportFormatType fromDesc(String desc) { | |
| 578 | + Assert.hasText(desc, "excel导入格式为空!"); | |
| 579 | + Assert.notNull(descMapEnum.get(desc), "未知的excel导入格式[" + desc + "]!"); | |
| 580 | + return descMapEnum.get(desc); | |
| 581 | + } | |
| 582 | + | |
| 583 | + /** | |
| 584 | + * 不同的excel导入格式验证发车时间内容。 | |
| 585 | + * @param fcsjContent 发车时间 | |
| 586 | + * @return true/通过,false/格式有问题 | |
| 587 | + */ | |
| 588 | + abstract boolean validateFcsjContent(String fcsjContent); | |
| 589 | + | |
| 590 | + /** | |
| 591 | + * 不同的excel导入格式验证行驶时间内容。 | |
| 592 | + * @param xssjContent 行驶时间 | |
| 593 | + * @return true/通过,false/格式有问题 | |
| 594 | + */ | |
| 595 | + abstract boolean validateXssjContent(String xssjContent); | |
| 596 | + } | |
| 597 | + // 验证数据用builder | |
| 598 | + public static class ValidateBuilder { | |
| 599 | + private ValidateBuilder() {} | |
| 600 | + | |
| 601 | + /** 导入的Excel文件路径 */ | |
| 602 | + private String excelFilePath; | |
| 603 | + /** 导入的Excel sheet名字 */ | |
| 604 | + private String excelSheetName; | |
| 605 | + /** 线路Id */ | |
| 606 | + private Integer lineId; | |
| 607 | + /** 线路路由版本 */ | |
| 608 | + private Integer lineRouteVersion; | |
| 609 | + /** 线路信息service */ | |
| 610 | + private LineService lineService; | |
| 611 | + /** 站点路由信息service */ | |
| 612 | + private StationRouteService stationRouteService; | |
| 613 | + /** 路牌信息service */ | |
| 614 | + private GuideboardInfoService guideboardInfoService; | |
| 615 | + /** excel导入格式类型 */ | |
| 616 | + private ImportFormatType importFormatType; | |
| 617 | + | |
| 618 | + public ValidateBuilder setExcelFilePath(String excelFilePath) { | |
| 619 | + this.excelFilePath = excelFilePath; | |
| 620 | + return this; | |
| 621 | + } | |
| 622 | + | |
| 623 | + public ValidateBuilder setExcelSheetName(String excelSheetName) { | |
| 624 | + this.excelSheetName = excelSheetName; | |
| 625 | + return this; | |
| 626 | + } | |
| 627 | + | |
| 628 | + public ValidateBuilder setLineId(Integer lineId) { | |
| 629 | + this.lineId = lineId; | |
| 630 | + return this; | |
| 631 | + } | |
| 632 | + | |
| 633 | + public ValidateBuilder setLineRouteVersion(Integer lineRouteVersion) { | |
| 634 | + this.lineRouteVersion = lineRouteVersion; | |
| 635 | + return this; | |
| 636 | + } | |
| 637 | + public ValidateBuilder setLineService(LineService lineService) { | |
| 638 | + this.lineService = lineService; | |
| 639 | + return this; | |
| 640 | + } | |
| 641 | + public ValidateBuilder setGuideboardInfoService(GuideboardInfoService guideboardInfoService) { | |
| 642 | + this.guideboardInfoService = guideboardInfoService; | |
| 643 | + return this; | |
| 644 | + } | |
| 645 | + public ValidateBuilder setStationRouteService(StationRouteService stationRouteService) { | |
| 646 | + this.stationRouteService = stationRouteService; | |
| 647 | + return this; | |
| 648 | + } | |
| 649 | + public ValidateBuilder setImportFormatType(ImportFormatType importFormatType) { | |
| 650 | + this.importFormatType = importFormatType; | |
| 651 | + return this; | |
| 652 | + } | |
| 653 | + | |
| 654 | + /** 待导入excel workbook对象 */ | |
| 655 | + private Workbook excelWorkBook; | |
| 656 | + /** 待导入excel workbook工作区对象 */ | |
| 657 | + private Sheet excelWorkBookSheet; | |
| 658 | + /** 线路信息 */ | |
| 659 | + private Line line; | |
| 660 | + /** 线路站点路由列表(包含上下行的起终点站) */ | |
| 661 | + private List<LsStationRoute> lsStationRouteList; | |
| 662 | + /** 路牌列表 */ | |
| 663 | + private List<GuideboardInfo> guideboardInfoList; | |
| 664 | + | |
| 665 | + public TimetableExcelData build() throws ScheduleException { | |
| 666 | + // 0、检测excel导入业务格式 | |
| 667 | + if (this.importFormatType == null) { | |
| 668 | + throw new ScheduleException("导入的Excel类型为空!"); | |
| 669 | + } | |
| 670 | + | |
| 671 | + // 1、检测excel文件,创建excel workbook对象 | |
| 672 | + // 1-1、检测文件是否存在 | |
| 673 | + Assert.hasText(this.excelFilePath, "导入的Excel文件路径为空!"); | |
| 674 | + File file = new File(this.excelFilePath); | |
| 675 | + if (!file.exists()) { | |
| 676 | + throw new ScheduleException("导入的Excel文件[" + this.excelFilePath + "]不存在!"); | |
| 677 | + } | |
| 678 | + // 1-2、检测文件类型 | |
| 679 | + DataToolsFile dataToolsFile = new DataToolsFile(); | |
| 680 | + dataToolsFile.setFile(file); | |
| 681 | + if (DataToolsFileType.XLS.isThisType(file)) { | |
| 682 | + dataToolsFile.setFileType(DataToolsFileType.XLS); | |
| 683 | + } else if (DataToolsFileType.XLSX.isThisType(file)) { | |
| 684 | + dataToolsFile.setFileType(DataToolsFileType.XLSX); | |
| 685 | + } else { | |
| 686 | + throw new ScheduleException("导入的Excel文件[" + this.excelFilePath + "]内部不是xls,xlsx文件!"); | |
| 687 | + } | |
| 688 | + // 1-3、创建workbook | |
| 689 | + this.excelWorkBook = dataToolsFile.getFileType().getWorkBook(dataToolsFile.getFile()); | |
| 690 | + // 1-4、检测sheet名字 | |
| 691 | + Assert.hasText(this.excelSheetName, "导入的Excel sheet名字为空!"); | |
| 692 | + this.excelWorkBookSheet = this.excelWorkBook.getSheet(this.excelSheetName); | |
| 693 | + if (this.excelWorkBookSheet == null) { | |
| 694 | + throw new ScheduleException("导入的Excel文件sheet[" + this.excelSheetName + "]不存在!"); | |
| 695 | + } | |
| 696 | + | |
| 697 | + // 2、获取线路信息,站点路由信息 | |
| 698 | + // 2-1、线路信息 | |
| 699 | + Assert.notNull(this.lineService, "线路信息service为空!"); | |
| 700 | + Assert.notNull(this.lineId, "线路Id为空!"); | |
| 701 | + this.line = this.lineService.findById(this.lineId); | |
| 702 | + if (this.line == null) { | |
| 703 | + throw new ScheduleException("线路[id=" + this.lineId + "]不存在!"); | |
| 704 | + } | |
| 705 | + // 2-2、站点路由信息 | |
| 706 | + Assert.notNull(this.stationRouteService, "站点路由信息service为空!"); | |
| 707 | + Assert.notNull(this.lineRouteVersion, "线路站点路由版本为空!"); | |
| 708 | + Map<String, Object> p1 = new HashMap<>(); | |
| 709 | + p1.put("line.id_eq", this.lineId); | |
| 710 | + p1.put("stationMark_in", "B,E"); // 起点站 | |
| 711 | + p1.put("destroy_eq", 0); // 未撤销 | |
| 712 | + p1.put("versions_eq", this.lineRouteVersion); // 带线路版本 | |
| 713 | + this.lsStationRouteList = (List<LsStationRoute>) this.stationRouteService.list_ls(p1); | |
| 714 | + if (CollectionUtils.isEmpty(this.lsStationRouteList)) { | |
| 715 | + throw new ScheduleException("线路[" + this.line.getName() + "],站点路由[版本=" + this.lineRouteVersion + "]信息为空"); | |
| 716 | + } | |
| 717 | + | |
| 718 | + // 3、获取路牌信息 | |
| 719 | + Assert.notNull(this.guideboardInfoService, "路牌信息service为空!"); | |
| 720 | + p1.clear(); | |
| 721 | + p1.put("xl.id_eq", this.lineId); | |
| 722 | + p1.put("isCancel_eq", false); | |
| 723 | + this.guideboardInfoList = guideboardInfoService.list(p1); | |
| 724 | + if (CollectionUtils.isEmpty(this.guideboardInfoList)) { | |
| 725 | + throw new ScheduleException("线路[" + this.line.getName() + "]路牌信息为空!"); | |
| 726 | + } | |
| 727 | + | |
| 728 | + return new TimetableExcelData(this); | |
| 729 | + } | |
| 730 | + } | |
| 731 | + // 导入数据用 | |
| 732 | + public static class ImportBuilder { | |
| 733 | + private ImportBuilder() {} | |
| 734 | + | |
| 735 | + /** 导入的Excel文件路径 */ | |
| 736 | + private String excelFilePath; | |
| 737 | + /** 导入的Excel sheet名字 */ | |
| 738 | + private String excelSheetName; | |
| 739 | + /** 时刻表Id */ | |
| 740 | + private Long ttInfoId; | |
| 741 | + /** 线路Id */ | |
| 742 | + private Integer lineId; | |
| 743 | + /** 线路标准信息Id */ | |
| 744 | + private Integer lineInfoId; | |
| 745 | + /** 线路名字 */ | |
| 746 | + private String lineName; | |
| 747 | + /** 时刻表名字 */ | |
| 748 | + private String ttInfoName; | |
| 749 | + /** 线路路由版本 */ | |
| 750 | + private Integer lineRouteVersion; | |
| 751 | + /** excel导入格式类型 */ | |
| 752 | + private ImportFormatType importFormatType; | |
| 753 | + /** 数据工具服务 */ | |
| 754 | + private DataToolsService dataToolsService; | |
| 755 | + /** 配置数据导入导出用到的配置信息 */ | |
| 756 | + private DataToolsProperties dataToolsProperties; | |
| 757 | + | |
| 758 | + public ImportBuilder setExcelFilePath(String excelFilePath) { | |
| 759 | + this.excelFilePath = excelFilePath; | |
| 760 | + return this; | |
| 761 | + } | |
| 762 | + | |
| 763 | + public ImportBuilder setExcelSheetName(String excelSheetName) { | |
| 764 | + this.excelSheetName = excelSheetName; | |
| 765 | + return this; | |
| 766 | + } | |
| 767 | + | |
| 768 | + public ImportBuilder setTtInfoId(Long ttInfoId) { | |
| 769 | + this.ttInfoId = ttInfoId; | |
| 770 | + return this; | |
| 771 | + } | |
| 772 | + | |
| 773 | + public ImportBuilder setLineId(Integer lineId) { | |
| 774 | + this.lineId = lineId; | |
| 775 | + return this; | |
| 776 | + } | |
| 777 | + | |
| 778 | + public ImportBuilder setLineInfoId(Integer lineInfoId) { | |
| 779 | + this.lineInfoId = lineInfoId; | |
| 780 | + return this; | |
| 781 | + } | |
| 782 | + | |
| 783 | + public ImportBuilder setLineName(String lineName) { | |
| 784 | + this.lineName = lineName; | |
| 785 | + return this; | |
| 786 | + } | |
| 787 | + | |
| 788 | + public ImportBuilder setTtInfoName(String ttInfoName) { | |
| 789 | + this.ttInfoName = ttInfoName; | |
| 790 | + return this; | |
| 791 | + } | |
| 792 | + | |
| 793 | + public ImportBuilder setLineRouteVersion(Integer lineRouteVersion) { | |
| 794 | + this.lineRouteVersion = lineRouteVersion; | |
| 795 | + return this; | |
| 796 | + } | |
| 797 | + | |
| 798 | + public ImportBuilder setImportFormatType(ImportFormatType importFormatType) { | |
| 799 | + this.importFormatType = importFormatType; | |
| 800 | + return this; | |
| 801 | + } | |
| 802 | + | |
| 803 | + public ImportBuilder setDataToolsService(DataToolsService dataToolsService) { | |
| 804 | + this.dataToolsService = dataToolsService; | |
| 805 | + return this; | |
| 806 | + } | |
| 807 | + | |
| 808 | + public ImportBuilder setDataToolsProperties(DataToolsProperties dataToolsProperties) { | |
| 809 | + this.dataToolsProperties = dataToolsProperties; | |
| 810 | + return this; | |
| 811 | + } | |
| 812 | + | |
| 813 | + /** 待导入excel workbook对象 */ | |
| 814 | + private Workbook excelWorkBook; | |
| 815 | + /** 待导入excel workbook工作区对象 */ | |
| 816 | + private Sheet excelWorkBookSheet; | |
| 817 | + /** ktr转换所需参数对象 */ | |
| 818 | + private Map<String, Object> ktrParams = new HashMap<>(); | |
| 819 | + | |
| 820 | + public TimetableExcelData build() { | |
| 821 | + // 注意:这里不做验证,业务上导入前必须验证的(调用验证用的builder) | |
| 822 | + File file = new File(this.excelFilePath); | |
| 823 | + DataToolsFile dataToolsFile = new DataToolsFile(); | |
| 824 | + dataToolsFile.setFile(file); | |
| 825 | + if (DataToolsFileType.XLS.isThisType(file)) { | |
| 826 | + dataToolsFile.setFileType(DataToolsFileType.XLS); | |
| 827 | + } else if (DataToolsFileType.XLSX.isThisType(file)) { | |
| 828 | + dataToolsFile.setFileType(DataToolsFileType.XLSX); | |
| 829 | + } | |
| 830 | + this.excelWorkBook = dataToolsFile.getFileType().getWorkBook(dataToolsFile.getFile()); | |
| 831 | + this.excelWorkBookSheet = this.excelWorkBook.getSheet(this.excelSheetName); | |
| 832 | + | |
| 833 | + // 设置ktr转换参数 | |
| 834 | + this.ktrParams.put("erroroutputdir", this.dataToolsProperties.getTransErrordir()); // 错误文件输出路径 | |
| 835 | + this.ktrParams.put("sheetname", this.excelSheetName); // sheet工作区的名字 | |
| 836 | + this.ktrParams.put("lineinfoid", this.lineInfoId); // 线路标准id | |
| 837 | + this.ktrParams.put("xlname", this.lineName); // 线路名称 | |
| 838 | + this.ktrParams.put("xlid", this.lineId); // 线路id | |
| 839 | + this.ktrParams.put("ttinfoname", this.ttInfoName); // 时刻表名称 | |
| 840 | + this.ktrParams.put("ttid", this.ttInfoId.intValue()); // 时刻表id | |
| 841 | + this.ktrParams.put("lineversion", this.lineRouteVersion); // 站点路由版本 | |
| 842 | + | |
| 843 | + return new TimetableExcelData(this); | |
| 844 | + } | |
| 845 | + } | |
| 846 | +} | ... | ... |