TTInfoDetailServiceImpl.java 30 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
package com.bsth.service.schedule.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.entity.CarPark;
import com.bsth.entity.Line;
import com.bsth.entity.LineInformation;
import com.bsth.entity.StationRoute;
import com.bsth.entity.schedule.GuideboardInfo;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.repository.CarParkRepository;
import com.bsth.repository.LineRepository;
import com.bsth.repository.StationRepository;
import com.bsth.repository.schedule.GuideboardInfoRepository;
import com.bsth.repository.schedule.TTInfoDetailRepository;
import com.bsth.repository.schedule.TTInfoRepository;
import com.bsth.service.CarParkService;
import com.bsth.service.LineInformationService;
import com.bsth.service.StationRouteService;
import com.bsth.service.schedule.GuideboardInfoService;
import com.bsth.service.schedule.TTInfoDetailService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsProperties;
import com.bsth.service.schedule.utils.DataToolsService;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xu on 17/1/3.
 */
@Service
@EnableConfigurationProperties(DataToolsProperties.class)
public class TTInfoDetailServiceImpl extends BServiceImpl<TTInfoDetail, Long> implements TTInfoDetailService {
    /** 日志记录器 */
    private static final Logger LOGGER = LoggerFactory.getLogger(TTInfoDetailServiceImpl.class);

    @Autowired
    private GuideboardInfoService guideboardInfoService;
    @Autowired
    private StationRouteService stationRouteService;
    @Autowired
    private LineInformationService lineInformationService;
    @Autowired
    private CarParkService carParkService;
    @Autowired
    private TTInfoDetailRepository ttInfoDetailRepository;
    @Autowired
    private DataToolsProperties dataToolsProperties;
    @Autowired
    private LineRepository lineRepository;
    @Autowired
    private TTInfoRepository infoRepository;
    @Autowired
    private StationRepository staRepository;
    @Autowired
    private CarParkRepository carParkRepository;
    @Autowired
    private GuideboardInfoRepository guideboardInfoRepository;
    @Autowired
    @Qualifier(value = "dataToolsServiceImpl")
    private DataToolsService dataToolsService;

    @Override
    public TTInfoDetail findById(Long aLong) {
        return ttInfoDetailRepository.findOneExtend(aLong);
    }

    @Override
    public File uploadFile(String filename, byte[] filedata) throws ScheduleException {

        return dataToolsService.uploadFile(filename, filedata);
    }

    @Override
    public void importData(
            File file,
            Map<String, Object> params) throws ScheduleException {

        try {
            LOGGER.info("//---------------- 导入时刻表明细 start... ----------------//");

            String filename = file.getAbsolutePath(); // xls文件名
            String sheetname = String.valueOf(params.get("sheetname")); // sheet名字
            Long ttid = Long.valueOf(String.valueOf(params.get("ttid"))); // 时刻表id
            Long xlid = Long.valueOf(String.valueOf(params.get("xlid"))); // 线路id
            Integer lineid = Integer.valueOf(String.valueOf(params.get("lineinfo"))); // 线路标准id
            String xlname = String.valueOf(params.get("xlname")); // 线路名字
            String ttname = String.valueOf(params.get("ttname")); // 时刻表名字

            LOGGER.info("参数1, xls文件名={},sheet名字={}", filename, sheetname);
            LOGGER.info("参数2, 线路id={},线路名字={}", xlid, xlname);
            LOGGER.info("参数3, 时刻表id={},时刻表名字={}", ttid, ttname);

            LOGGER.info("转换xls文件格式成文本格式...");
            // 1、修改已经上传的excel文件,在每个起点站后标示数字,表示第几个班次
            // 2、由于格式问题,需要把内容都转换成字符串
            List<String> colList = new ArrayList<>();
            Workbook workbook = Workbook.getWorkbook(new File(filename));
            Sheet sheet = workbook.getSheet(sheetname);
            Cell[] cells = sheet.getRow(0);
            for (int i = 0; i < cells.length; i++) {
                if (i == 0) {
                    colList.add(cells[i].getContents().trim());
                } else {
                    colList.add(cells[i].getContents() + i);
                }
            }

            File fileCal = new File(filename + "_stringType.xls");
            WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal, workbook);
            WritableSheet sheet1 = writableWorkbook.getSheet(sheetname);
            for (int i = 0; i < sheet1.getColumns(); i++) { // 第一行数据
                sheet1.addCell(new Label(i, 0, colList.get(i)));
            }
            for (int i = 1; i < sheet1.getRows(); i++) { // 第二行开始
                Cell[] cells1 = sheet.getRow(i);
                for (int j = 0; j < cells1.length; j++) {
                    sheet1.addCell(new Label(j, i, cells1[j].getContents()));
                }
            }
            writableWorkbook.write();
            writableWorkbook.close();

            // 2、删除原有数据
            // 操作在ktr内部执行

            // 3、导入时刻表
            // 获取停车场名字
            LOGGER.info("获取停车场名字...");
            LineInformation lineInformation = lineInformationService.findById(lineid);
            Map<String, Object> p1 = new HashMap<>();
            p1.put("parkCode_eq", lineInformation.getCarPark());
            List<CarPark> carParkList = (List<CarPark>) carParkService.list(p1);
            String tccname = carParkList.get(0).getParkName();
            LOGGER.info("停车场名字={}", tccname);


            // 计算表头参数
            Workbook book = Workbook.getWorkbook(fileCal);
            Sheet sheet_exp = book.getSheet(sheetname);
            List<String> columnames = new ArrayList<>();
            for (int i = 0; i < sheet_exp.getColumns(); i++) { // 获取第一行,数据,作为列名
                columnames.add(sheet_exp.getCell(i, 0).getContents());
            }
            LOGGER.info("表头={}", StringUtils.join(columnames.toArray(), ","));

            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            File ktrFile = new File(this.getClass().getResource(
                    dataToolsProperties.getTtinfodetailMetadatainputktr()).toURI());
            File ktrFile2 = new File(this.getClass().getResource(
                    dataToolsProperties.getTtinfodetailDatainputktr()).toURI());

            // 通用参数,转换文件路径,excel输入文件路径,错误输出文件路径
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filepath", fileCal.getAbsolutePath());
            ktrParms.put("erroroutputdir", dataToolsProperties.getTransErrordir());

            // 附加参数
            ktrParms.put("injectktrfile", ktrFile2.getAbsolutePath()); // 注入元数据的ktr文件
            ktrParms.put("sheetname", sheetname); // sheet工作区的名字
            ktrParms.put("xlname", xlname); // 线路名称
            ktrParms.put("ttinfoname", ttname); // 时刻表名称
            ktrParms.put("ttid", ttid.intValue()); // 时刻表id
            ktrParms.put("tccname", tccname); // 停车场名字
            ktrParms.put("excelfieldnames", StringUtils.join(columnames.toArray(), ",")); // 时刻表excel输入字段名,以逗号连接
            columnames.remove(0);
            ktrParms.put("normalizefieldnames", StringUtils.join(columnames.toArray(), ",")); // 数据范式化字段名,以逗号连接

            super.importData(fileCal, ktrParms);

            LOGGER.info("//---------------- 导入时刻表明细 success... ----------------//");
        } catch (Exception exp) {
            LOGGER.info("//---------------- 导入时刻表明细 failed... ----------------//");

            StringWriter sw = new StringWriter();
            exp.printStackTrace(new PrintWriter(sw));
            LOGGER.info(sw.toString());

            throw new ScheduleException(exp.getMessage());
        }
    }

    @Override
    public File exportData(Map<String, Object> params) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 导出时刻表明细 start... ----------------//");

            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            File ktrFile = new File(this.getClass().getResource(
                    dataToolsProperties.getTtinfodetailMetaoutput()).toURI());
            File ktrFile2 = new File(this.getClass().getResource(
                    dataToolsProperties.getTtinfodetailOutput()).toURI());

            // 通用参数,转换文件路径,excel输出文件名
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filename", String.format("时刻表_(id=%s)_download-", String.valueOf(params.get("ttinfoid"))));

            // 附加参数
            ktrParms.put("injectktrfile", ktrFile2.getAbsolutePath()); // 注入元数据的ktr文件
            ktrParms.put("ttinfoid", String.valueOf(params.get("ttinfoid")));

            File file = super.exportData(ktrParms);

            LOGGER.info("//---------------- 导出时刻表明细 success... ----------------//");

            return file;
        } catch (Exception exp) {
            LOGGER.info("//---------------- 导出时刻表明细 failed... ----------------//");

            StringWriter sw = new StringWriter();
            exp.printStackTrace(new PrintWriter(sw));
            LOGGER.info(sw.toString());

            throw new ScheduleException(exp.getMessage());
        }
    }

    @Override
    public EditInfo getEditInfo(Integer xlid, Long ttid) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 时刻表编辑用数据输出 start... ----------------//");

            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            File ktrFile = new File(this.getClass().getResource(
                    dataToolsProperties.getTtinfodetailForeditktr()).toURI());

            // 通用参数,转换文件路径,excel输出文件名,错误输出文件路径
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filename", "todo");

            // 附加参数
            String outputFilePath = String.format("ttinfodetail_(id=%s)_foredit-%s",
                    String.valueOf(ttid), new DateTime().toString("yyyyMMddHHmmss"));

            ktrParms.put("tempfilepath", dataToolsProperties.getTransTempdir() + File.separator + outputFilePath); // 数据输出文件路径
            ktrParms.put("xlid", String.valueOf(xlid));
            ktrParms.put("ttid", String.valueOf(ttid));

            super.exportData(ktrParms);

            EditInfo editInfo = new EditInfo(); // 输出数据

            // 1.6、获取最大的发车数,用于输出数据的数量
            Long maxfcno = ttInfoDetailRepository.findMaxFcno(xlid, ttid);
            LOGGER.info("最大发车顺序号={}", maxfcno);
            if (maxfcno != null) {
                // 2、读取ktr生成的excel数据,组织编辑用数据返回
                // 2-1、读取Excel文件
                Workbook book = Workbook.getWorkbook(new File(dataToolsProperties.getTransTempdir() +
                        File.separator + outputFilePath + ".xls"));
                Sheet sheet = book.getSheet(0);

                // 2-2、处理数据
                int all_bc = 0; // 总班次
                double all_lc_ks = 0; // 总空驶里程
                double all_lc_yy = 0; // 总营运里程

                String[] headarrays = new String[maxfcno.intValue() + 3];
                headarrays[0] = "路牌";
                headarrays[maxfcno.intValue() + 1] = "空驶班次/空驶里程";
                headarrays[maxfcno.intValue() + 2] = "运营班次/运营里程";

                for (int r = 1; r < sheet.getRows(); r++) {
                    List<FcInfo> fcInfos = new ArrayList<>();
                    // 每行第一列都是路牌
                    fcInfos.add(new FcInfo(null, null, sheet.getCell(0, r).getContents(), null, null, null, null, null)); // 用fcsj放置路牌显示

                    int bc_ks = 0; // 空驶班次
                    int bc_yy = 0; // 营运班次
                    double lc_ks = 0; // 空驶里程
                    double lc_yy = 0; // 营运里程

                    for (int c = 1; c <= maxfcno; c++) {
                        String content_str = sheet.getCell(c, r).getContents();
                        try {
                            String[] content = StringUtils.isEmpty(content_str) ? null : content_str.split(","); // 总的内容

                            String ttdid_str = content == null ? "" : content[0]; // 时刻表明细id
                            String fcsj = content == null ? "" : content[1]; // 发车时间
                            String jhlc = content == null ? "" : content[2]; // 计划里程
                            String fzdname = content == null ? "" : content[3]; // 发车站点名称
                            String bctype = content == null ? "" : content[4]; // 班次类型
                            String xldir = content == null ? "" : content[5]; // 线路上下行
                            String isfb = content == null ? "" : content[6]; // 是否分班

                            String qdz = content == null ? "" : content[7]; // 起点站
                            String zdz = content == null ? "" : content[8]; // 终点站
                            String tcc = content == null ? "" : content[9]; // 停车场

                            FcInfo fcInfo = new FcInfo(ttdid_str, bctype, fcsj, xldir, isfb, qdz, zdz, tcc);

                            if (StringUtils.isNotEmpty(fzdname))
                                headarrays[c] = fzdname;
                            fcInfos.add(fcInfo);


                            // 计算班次里程
                            if (StringUtils.isNotEmpty(jhlc)) {
                                if ("in".equals(bctype) || "out".equals(bctype)) {
                                    bc_ks += 1;
                                    lc_ks += Double.valueOf(jhlc);

                                    all_bc += 1;
                                    all_lc_ks += Double.valueOf(jhlc);

                                } else {
                                    bc_yy += 1;
                                    lc_yy += Double.valueOf(jhlc);

                                    all_bc += 1;
                                    all_lc_yy += Double.valueOf(jhlc);
                                }
                            }

                        } catch (Exception exp) {
                            LOGGER.info("第{}行,第{}列数据有问题,数据={},异常message={}", r, c, content_str, exp.getCause());
                            break;
                        }

                    }

                    // 添加一列 空驶班次/空驶里程,fcsj放置数据
                    fcInfos.add(new FcInfo(null, null, String.format("%d/%.2f", bc_ks, lc_ks), null, null, null, null, null));

                    // 添加一列 营运班次/营运里程,fcsj放置数据
                    fcInfos.add(new FcInfo(null, null, String.format("%d/%.2f", bc_yy, lc_yy), null, null, null, null, null));

                    editInfo.getContents().add(fcInfos);
                }
                editInfo.getHeader().addAll(Arrays.asList(headarrays));

                editInfo.setYy_desc(String.format("班次=%d,空驶里程=%.2f,营运里程=%.2f", all_bc, all_lc_ks, all_lc_yy));
            }

            LOGGER.info("//---------------- 时刻表编辑用数据输出 success... ----------------//");

            return editInfo;
        } catch (Exception exp) {
            LOGGER.info("//---------------- 时刻表编辑用数据输出 failed... ----------------//");

            StringWriter sw = new StringWriter();
            exp.printStackTrace(new PrintWriter(sw));
            LOGGER.info(sw.toString());

            throw new ScheduleException(exp.getMessage());
        }
    }



    @Override
    public void validateExcelSheet(String filename, String sheetname, Integer lineid, String linename) throws ScheduleException {
        try {
            Workbook book = Workbook.getWorkbook(new File(filename));
            Sheet sheet = book.getSheet(sheetname);
            if (sheet.getRows() == 0 || sheet.getColumns() == 0) { // 工作区是否为空
                throw new Exception(String.format("%s 工作区没有数据!", sheetname));
            } else {
                if (sheet.getRows() <= 1 || sheet.getColumns() <= 1) {
                    throw new Exception(String.format("工作区至少包含2行2列的数据"));
                } else {
                    Cell[] cells = sheet.getRow(0); // 获取第一行数据列
                    for (int i = 0; i < cells.length; i++) {
                        String cell_con = cells[i].getContents();

                        if (StringUtils.isEmpty(cell_con)) {
                            throw new Exception(String.format("第1行,第%d列数据不能为空", i + 1));
                        } else {
                            // 正则表达式去除数字
                            cell_con = cell_con.replaceAll("[\\d+]", "");

                            if (i == 0) { // 第一列必须是路牌2个字
                                if (!"路牌".equals(cell_con.trim())) {
                                    throw new Exception("第1行,第1列数据必须是路牌2个字");
                                }
                            } else { // 排除出场,进场,其余内容到站点路由里查询,以各个方向的起点站为查询依据
                                if ((!"出场".equals(cell_con.trim())) &&
                                        (!"进场".equals(cell_con.trim()))) {
                                    Map<String, Object> p1 = new HashMap<>();
                                    p1.put("line.id_eq", lineid);
                                    p1.put("stationName_eq", cell_con.trim());
                                    p1.put("stationMark_eq", "B");


                                    // TODO:这里要修改(起点站有启用撤销的标志的)

                                    List<StationRoute> stationRouteList = (List<StationRoute>) stationRouteService.list(p1);
                                    if (CollectionUtils.isEmpty(stationRouteList)) {
                                        throw new Exception(String.format("第1行,第%d列数据%s在%s站点路由中不是起点站", i + 1, cell_con.trim(), linename));
                                    } else if (stationRouteList.size() > 1) {
                                        throw new Exception(String.format("第1行,第%d列数据%s在%s站点路由中上下行都是起点站", i + 1, cell_con.trim(), linename));
                                    }
                                }

                            }
                        }
                    }

                    // 验证路牌内容
                    Map<String, Integer> gbindexmap = new HashMap<>(); // 记录每个路牌在第几行
                    for (int i = 1; i < sheet.getRows(); i++) { // 从第2行开始验证数据
                        Cell bcell = sheet.getRow(i)[0]; // 获取第1列
                        String bcell_con = bcell.getContents();
                        if (StringUtils.isEmpty(bcell_con)) {
                            throw new Exception(String.format("第%d行,第1列路牌无数据", i + 1));
                        } else if (gbindexmap.get(bcell_con.trim()) != null) {
                            throw new Exception(String.format("第%d行,第1列的路牌数据与第%d行,第1列数据重复",
                                    i + 1,
                                    gbindexmap.get(bcell_con.trim())));
                        } else {
                            Map<String, Object> p2 = new HashMap<>();
                            p2.put("xl.id_eq", lineid);
                            p2.put("lpName_eq", bcell_con.trim());
                            List<GuideboardInfo> guideboardInfoList = (List<GuideboardInfo>) guideboardInfoService.list(p2);
                            if (CollectionUtils.isEmpty(guideboardInfoList)) {
                                throw new Exception(String.format("第%d行,第1列的路牌在%s中不存在", i + 1, linename));
                            } else if (guideboardInfoList.size() > 1) {
                                throw new Exception(String.format("第%d行,第1列的路牌在%s中重复", i + 1, linename));
                            } else {
                                gbindexmap.put(bcell_con.trim(), i + 1);
                            }
                        }
                    }

                    // 班次时间验证,正则表达式,格式hh:mm或者hhmm
                    String rex1 = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$"; // hh:mm格式
                    String rex2 = "^([01]?[0-9]|2[0-3]),[0-5][0-9]$"; // hh,mm格式
                    String rex3 = "^([01]?[0-9]|2[0-3])[0-5][0-9]$"; // hhmm格式
                    Pattern p1 = Pattern.compile(rex1);
                    Pattern p2 = Pattern.compile(rex2);
                    Pattern p3 = Pattern.compile(rex3);

                    for (int i = 1; i < sheet.getRows(); i++) { // 从第2行开始验证数据
                        Cell[] bcells = sheet.getRow(i);
                        for (int j = 1; j < bcells.length; j++) { // 从第2列开始
                            String bcell_con = bcells[j].getContents();
                            if (StringUtils.isNotEmpty(bcell_con)) {
                                Matcher m1 = p1.matcher(bcell_con.trim());
                                Matcher m2 = p2.matcher(bcell_con.trim());
                                Matcher m3 = p3.matcher(bcell_con.trim());
                                if ((!m1.matches()) && (!m2.matches()) && (!m3.matches())) {
                                    throw new Exception(String.format("第%d行,第%d列的发车时间格式不正确,格式应为hh:mm或hh,mm或hhmm", i + 1, j + 1));
                                }
                            }
                        }
                    }
                }

            }
        } catch (Exception exp) {
            throw new ScheduleException(exp.getMessage());
        }

    }

    @Override
    public void validateAssoLineInfo(Integer lineinfoid) throws ScheduleException {
        LineInformation lineInformation = lineInformationService.findById(lineinfoid);
        if (lineInformation.getUpInMileage() == null) {
            throw new ScheduleException("上行进场里程为空");
        } else if (lineInformation.getUpInTimer() == null) {
            throw new ScheduleException("上行进场时间为空");
        } else if (lineInformation.getUpOutMileage() == null) {
            throw new ScheduleException("上行出场里程为空");
        } else if (lineInformation.getUpOutTimer() == null) {
            throw new ScheduleException("上行出场时间为空");
        } else if (lineInformation.getUpMileage() == null) {
            throw new ScheduleException("上行班次里程为空");
        } else if (lineInformation.getUpTravelTime() == null) {
            throw new ScheduleException("上行班次时间为空");
        } else if (lineInformation.getDownInMileage() == null) {
            throw new ScheduleException("下行进场里程为空");
        } else if (lineInformation.getDownInTimer() == null) {
            throw new ScheduleException("下行进场时间为空");
        } else if (lineInformation.getDownOutMileage() == null) {
            throw new ScheduleException("下行出场里程为空");
        } else if (lineInformation.getDownOutTimer() == null) {
            throw new ScheduleException("下行出场时间为空");
        } else if (lineInformation.getDownMileage() == null) {
            throw new ScheduleException("下行班次里程为空");
        } else if (lineInformation.getDownTravelTime() == null) {
            throw new ScheduleException("下行班次时间为空");
        } else if (StringUtils.isEmpty(lineInformation.getCarPark())) {
            throw new ScheduleException("停车场必须选择");
        }

        // 单独验证停车场信息
        String tcccode = lineInformation.getCarPark();
        Map<String, Object> p1 = new HashMap<>();
        p1.put("parkCode_eq", tcccode);
        List<CarPark> carParkList = (List<CarPark>) carParkService.list(p1);
        if (CollectionUtils.isEmpty(carParkList)) {
            throw new ScheduleException(String.format("线路标准里的停车场code=%s,在停车场信息中未找到", tcccode));
        } else if (carParkList.size() > 1) {
            throw new ScheduleException(String.format("线路标准里的停车场code=%s,在停车场信息中有重复数据", tcccode));
        } else {
            CarPark carPark = carParkList.get(0);
            if (StringUtils.isEmpty(carPark.getParkName())) {
                throw new ScheduleException(String.format("线路标准里的停车场code=%s,在停车场信息中没有停车场名字", tcccode));
            }
        }

    }

    @Override
    public List<TTInfoDetail> findBcdetails(Integer xlId, Long ttinfoId, Long lpId) {
        return ttInfoDetailRepository.findBcdetails(xlId, ttinfoId, lpId);
    }
    
    @Override
   	public Map<String, Object> skbDetailMxSave(Map<String, Object> map) {
   		Map<String, Object> rs_m = new HashMap<String,Object>();
   		try {
   			String d = map.get("d") == null ? null : map.get("d").toString();
   	    	if(d!=null)
   	    		ttInfoDetailRepository.save(jsonArrayToListEntity(d));
   		} catch (Exception e) {
   			e.printStackTrace();
   			rs_m.put("status", ResponseCode.ERROR);
   		}
   		rs_m.put("status", ResponseCode.SUCCESS);
   		return rs_m;
   	}
   	
   	public List<TTInfoDetail> jsonArrayToListEntity(String jsonStr) throws Exception {
   		List<TTInfoDetail> listTd = new ArrayList<TTInfoDetail>();
   		JSONArray jsonArray = JSONArray.parseArray(jsonStr);
   		for(int i =0; i<jsonArray.size();i++) {
   			JSONObject jsonObj = jsonArray.getJSONObject(i);
   			if(jsonObj.getString("bcType").equals("bd") || jsonObj.getString("bcType").equals("lc") || jsonObj.getString("bcType").equals("cf"))
   				continue;
   			listTd.add(objToEntity(jsonObj));
   		}
   		return listTd;
   	}
   	
   	public TTInfoDetail objToEntity(JSONObject obj) throws Exception {
   		TTInfoDetail td = new TTInfoDetail();
   		Line xl = lineRepository.findOne(Integer.parseInt(obj.getString("xl"))); 
   		td.setXl(xl);
   		td.setTtinfo(obj.getString("ttinfo") == null ? null : infoRepository.findOne(Long.parseLong(obj.getString("ttinfo"))));
   		td.setLp(getLp(xl,obj.getString("lp"),Integer.parseInt(obj.getString("lp")),obj.getString("lpType")));
   		td.setFcno(Integer.parseInt(obj.getString("fcno")));
   		td.setXlDir(dirToCod(obj.get("xlDir").toString()));
   		td.setQdz( obj.getString("qdz") ==null? null : staRepository.findOne(Integer.parseInt(obj.getString("qdz"))));
   		td.setZdz(obj.getString("zdz") ==null ? null :staRepository.findOne(Integer.parseInt(obj.getString("zdz"))));
   		td.setTcc(carParkRepository.findOne(Integer.parseInt(obj.getString("tcc"))));
   		td.setFcsj(obj.getString("fcsj"));
   		td.setBcs(Integer.parseInt(obj.getString("bcs")));
   		td.setJhlc(Double.parseDouble(obj.getString("jhlc")));
   		td.setBcsj(Integer.parseInt(obj.getString("bcsj")));
   		td.setBcType(obj.getString("bcType"));
   		td.setIsFB(false);
   		td.setIsSwitchXl(false);
   		td.setSwitchXl(null);
   		td.setSwitchXlDesc(null);
   		td.setRemark(null);
   		td.setCreateBy(null);
   		td.setUpdateBy(null);
   		return td;
   	}
   	
   	public GuideboardInfo getLp(Line xl,String name, int code, String lpType) throws Exception {
   		GuideboardInfo entity = new GuideboardInfo();
   		List<GuideboardInfo> lgi = guideboardInfoRepository.validateLp(xl, name, code,lpType);
   		if(lgi.size()>0) {
   			entity = lgi.get(0);
   		}else{
   			entity.setXl(xl);
   			entity.setLpNo(code);
   			entity.setLpName(name);
   			entity.setLpType(lpType);
   			entity.setIsCancel(false);
   			entity.setCreateBy(null);
   			entity.setUpdateBy(null);
   			guideboardInfoRepository.save(entity);
   		} 
   		return entity;
   	}
   	
   	public String dirToCod(String str) throws Exception {
   		String c = "";
   		if(str.equals("relationshipGraph-up"))
   			c = "0";
   		else if(str.equals("relationshipGraph-down"))
   			c = "1";
   		return c;
   	}
}