TTInfoDetailServiceImpl.java 30.6 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
package com.bsth.service.schedule.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.controller.schedule.dto.LineRegionDto;
import com.bsth.controller.schedule.dto.LineStationRouteDto;
import com.bsth.entity.*;
import com.bsth.entity.schedule.GuideboardInfo;
import com.bsth.entity.schedule.TTInfo;
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.repository.sys.SysUserRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.CarParkService;
import com.bsth.service.LineInformationService;
import com.bsth.service.LineService;
import com.bsth.service.LsStationRouteService;
import com.bsth.service.schedule.GuideboardInfoService;
import com.bsth.service.schedule.TTInfoDetailService;
import com.bsth.service.schedule.datatools.Excel2007PoiOperator;
import com.bsth.service.schedule.datatools.ExcelPoiOperator;
import com.bsth.service.schedule.datatools.TTInfoDetailForEdit;
import com.bsth.service.schedule.datatools.TTinfoDetailDynamicData;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.timetable.TimetableExcelData;
import com.bsth.service.schedule.timetable.TimetableExcelFormatType;
import com.bsth.service.schedule.timetable.strategy.impl.TimetableExcelDataValidateStrategyImpl;
import com.bsth.service.schedule.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.awt.Color;
import java.io.File;
import java.util.*;
import java.util.List;

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

    @Autowired
    private GuideboardInfoService guideboardInfoService;
    @Autowired
    private LsStationRouteService lsStationRouteService;
    @Autowired
    private LineInformationService lineInformationService;
    @Autowired
    private CarParkService carParkService;
    @Autowired
    private TTInfoDetailRepository ttInfoDetailRepository;
    @Autowired
    private LineRepository lineRepository;
    @Autowired
    private TTInfoRepository infoRepository;
    @Autowired
    private StationRepository staRepository;
    @Autowired
    private CarParkRepository carParkRepository;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private GuideboardInfoRepository guideboardInfoRepository;
    @Autowired
    private LineService lineService;

    @Autowired
    @Qualifier(value = "ttInfoDetail_dataTool")
    private DataToolsService dataToolsService;

    @Autowired
    @Qualifier(value = "ttInfoDetail_dataTool")
    private TTInfoDetailForEdit ttInfoDetailForEdit;

    @Autowired
    @Qualifier(value = "ttInfoDetail_dataTool")
    private TTinfoDetailDynamicData tTinfoDetailDynamicData;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查找站点和停车场集合
     * @param lineid 线路id
     * @param xldir 线路上下行
     * @return
     */
    public List<Map<String, Object>> findZdAndTcc(int lineid, int xldir, int lineversion) {
        String sql = "select * from " +
                "(" +
                "select station_code as zcode, station_name as zname, concat(station_name, '(站点)') as aname from bsth_c_ls_stationroute " +
                "where destroy = 0 and line = ? and directions = ? and versions = ? " +
                "union all " +
                "select park_code as zcode, park_name as zname, concat(park_name, '(停车场)') as aname from bsth_c_car_park " +
                "where destroy = 0 " +
                ") a ";

        return jdbcTemplate.queryForList(sql, lineid, xldir, lineversion);
    }

    @Override
    public List<LineRegionDto> findLineRegion(Integer lineId, Integer lineVersion, Integer lineDir) {
        List<LineRegionDto> lineRegionDtoList = new ArrayList<>();
        List<LineRegion> lineRegionList = this.ttInfoDetailRepository.findLineRegion(lineId, lineVersion, lineDir);
        for (LineRegion lineRegion : lineRegionList) {
            List<LineStationRouteDto> lineStationRouteDtoList = new ArrayList<>();
            for (LsStationRoute lsStationRoute : lineRegion.getStationRoutes()) {
                LineStationRouteDto lineStationRouteDto = LineStationRouteDto.getBuilder()
                        .setStationName(lsStationRoute.getStationName())
                        .setStationCode(lsStationRoute.getStationCode())
                        .build();
                lineStationRouteDtoList.add(lineStationRouteDto);
            }

            LineRegionDto lineRegionDto = LineRegionDto.getBuilder()
                    .setRegionId(lineRegion.getId())
                    .setRegionName(lineRegion.getRegionAlias())
                    .setLineId(lineRegion.getLine())
                    .setLineDir(lineRegion.getDirection())
                    .setLineStationRouteDtoList(lineStationRouteDtoList)
                    .build();
            lineRegionDtoList.add(lineRegionDto);
        }

        return lineRegionDtoList;
    }

    @Override
    public Long findMaxFcno(Integer xlid, Long ttinfoid) {
        return ttInfoDetailRepository.findMaxFcno(xlid, ttinfoid);
    }

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

    @Override
    public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
        return dataToolsService.uploadFile(filename, filedata);
    }

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

    @Override
    public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
        return dataToolsService.exportData(params);
    }

    @Override
    public DataToolsFile exportDynamicTTinfo(TTinfoDetailDynamicData.DTInfos dtInfos, DataToolsFileType type) throws ScheduleException {
        return tTinfoDetailDynamicData.exportDynamicTTinfo(dtInfos, type);
    }

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Transactional
    @Override
    public DataToolsFile exportPvInfo(Long ttInfoId) throws ScheduleException {
        // 1、获取时刻表,时刻表明细数据
        TTInfo ttInfo = this.infoRepository.findOneExtend(ttInfoId);
        List<TTInfoDetail> ttInfoDetailList = this.ttInfoDetailRepository.findByTtinfoId(ttInfoId);
        LOGGER.info("----------------- 开始导出时刻表[{}]预览视图信息---------------", ttInfo.getName());

        // 2、构造导出信息
        try {
            // 使用excel2007格式(xlsx)
            ExcelPoiOperator excelPoiOperator = new Excel2007PoiOperator();
            Workbook wb = excelPoiOperator.createWorkBook(); // 创建workbook
            Sheet sheet = excelPoiOperator.createWorkBookSheet(wb, "预览信息"); // 创建sheet

            // 创建表头(从第2行开始,第1行需要合并单元格显示站点名字)
            int[] headColNums = new int[] {0, 1, 2, 3, 4, 7, 8, 9, 10, 11};
            String[] headColLabels = new String[] {"序号", "路牌", "发车时间", "到达时间", "备注", "序号", "路牌", "发车时间", "到达时间", "备注"};
            int[] headColWidth = new int[] {10, 10, 20, 20, 35, 10, 10, 20, 20, 35};

            Row headRow = excelPoiOperator.createSheetRow(sheet, 1);
            headRow.setHeight((short) (35 * 20)); // 单位:1/20个点
            for (int i = 0; i < headColNums.length; i ++) {
                excelPoiOperator.createCell(
                        wb, headRow, (short) headColNums[i],
                        headColLabels[i], XSSFCell.CELL_TYPE_STRING,
                        HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                        BorderStyle.THIN, new java.awt.Color(0x000000),
                        (short) 14, new java.awt.Color(0xffffff), "宋体",
                        new Color(0x857F7F), FillPatternType.SOLID_FOREGROUND);
                sheet.setColumnWidth(headColNums[i], headColWidth[i] * 256); // 单位:1/256个字符宽度
            }

            // 获取上行线路并按照时间排序
            List<TTInfoDetail> upBcList = sortedByBcsjAsc(ttInfoDetailList, true);
            List<TTInfoDetail> downBcList = sortedByBcsjAsc(ttInfoDetailList, false);
            int rowDiff = upBcList.size() - downBcList.size();
            int rowSize;
            if (rowDiff >= 0) { // 上行班次多
                rowSize = downBcList.size();
            } else { // 下行班次多
                rowSize = upBcList.size();
            }
            for (int i = 0; i < rowSize; i++) {
                Row bcRow = excelPoiOperator.createSheetRow(sheet, i + 2);
                // 上行班次
                excelPoiOperator.createIntegerCell(wb, bcRow, (short) 0, i + 1);
                String lpName = upBcList.get(i).getLp().getLpName();
                if (StringUtils.isNumeric(lpName)) {
                    excelPoiOperator.createIntegerCell(wb, bcRow, (short) 1, Integer.valueOf(lpName));
                } else {
                    excelPoiOperator.createStringCell(wb, bcRow, (short) 1, lpName);
                }
                excelPoiOperator.createStringCell(wb, bcRow, (short) 2, upBcList.get(i).getFcsj());
                Date ddsj_up = MyDateUtils.hhssTimePlusMinunits(upBcList.get(i).getFcsj(), upBcList.get(i).getBcsj(), 0);
                excelPoiOperator.createStringCell(wb, bcRow, (short) 3, DateFormatUtils.format(ddsj_up, "HH:mm"));
                excelPoiOperator.createStringCell(wb, bcRow, (short) 4, StringUtils.trimToEmpty(upBcList.get(i).getRemark()));

                // 下行班次
                excelPoiOperator.createIntegerCell(wb, bcRow, (short) 7, i + 1);
                lpName = downBcList.get(i).getLp().getLpName();
                if (StringUtils.isNumeric(lpName)) {
                    excelPoiOperator.createIntegerCell(wb, bcRow, (short) 8, Integer.valueOf(lpName));
                } else {
                    excelPoiOperator.createStringCell(wb, bcRow, (short) 8, lpName);
                }
                excelPoiOperator.createStringCell(wb, bcRow, (short) 9, downBcList.get(i).getFcsj());
                Date ddsj_down = MyDateUtils.hhssTimePlusMinunits(downBcList.get(i).getFcsj(), downBcList.get(i).getBcsj(), 0);
                excelPoiOperator.createStringCell(wb, bcRow, (short) 10, DateFormatUtils.format(ddsj_down, "HH:mm"));
                excelPoiOperator.createStringCell(wb, bcRow, (short) 11, StringUtils.trimToEmpty(downBcList.get(i).getRemark()));
            }

            List<TTInfoDetail> rowDiffBcList; // 剩下的班次
            int rowDiffColIndexStart;
            if (rowDiff >= 0) { // 上行班次多
                rowDiffBcList = upBcList;
                rowDiffColIndexStart = 0;
            } else { // 下行班次多
                rowDiffBcList = downBcList;
                rowDiffColIndexStart = 7;
            }
            for (int i = rowSize; i < rowSize + Math.abs(rowDiff); i++) {
                Row bcRow = excelPoiOperator.createSheetRow(sheet, i + 2);
                excelPoiOperator.createIntegerCell(wb, bcRow, (short) (rowDiffColIndexStart + 0), i + 1);
                String lpName = rowDiffBcList.get(i).getLp().getLpName();
                if (StringUtils.isNumeric(lpName)) {
                    excelPoiOperator.createIntegerCell(wb, bcRow, (short) (rowDiffColIndexStart + 1), Integer.valueOf(lpName));
                } else {
                    excelPoiOperator.createStringCell(wb, bcRow, (short) (rowDiffColIndexStart + 1), lpName);
                }
                excelPoiOperator.createStringCell(wb, bcRow, (short) (rowDiffColIndexStart + 2), rowDiffBcList.get(i).getFcsj());
                Date ddsj = MyDateUtils.hhssTimePlusMinunits(rowDiffBcList.get(i).getFcsj(), rowDiffBcList.get(i).getBcsj(), 0);
                excelPoiOperator.createStringCell(wb, bcRow, (short) (rowDiffColIndexStart + 3), DateFormatUtils.format(ddsj, "HH:mm"));
                excelPoiOperator.createStringCell(wb, bcRow, (short) (rowDiffColIndexStart + 4), StringUtils.trimToEmpty(rowDiffBcList.get(i).getRemark()));

            }

            // 创建第一行,合并列
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 7, 11));
            Row firstRow = excelPoiOperator.createSheetRow(sheet, 0);
            firstRow.setHeight((short) (50 * 20)); // 单位:1/20个点
            if (upBcList.size() > 0 && downBcList.size() > 0) {
                String upBcLabel = "";
                for (TTInfoDetail ttInfoDetail : upBcList) {
                    if ("normal".equals(ttInfoDetail.getBcType())) {
                        upBcLabel = "上行站点:" + ttInfoDetail.getQdzName() + " >> " + ttInfoDetail.getZdzName();
                        break;
                    }
                }
                String downBcLabel = "";
                for (TTInfoDetail ttInfoDetail : downBcList) {
                    if ("normal".equals(ttInfoDetail.getBcType())) {
                        downBcLabel = "下行站点:" + ttInfoDetail.getQdzName() + " >> " + ttInfoDetail.getZdzName();
                        break;
                    }
                }

                excelPoiOperator.createCell(
                        wb, firstRow, (short) 0,
                        upBcLabel, XSSFCell.CELL_TYPE_STRING,
                        HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                        BorderStyle.NONE, new java.awt.Color(0x000000),
                        (short) 20, new java.awt.Color(0xffffff), "宋体",
                        new Color(0x857F7F), FillPatternType.SOLID_FOREGROUND);

                excelPoiOperator.createCell(
                        wb, firstRow, (short) 7,
                        downBcLabel, XSSFCell.CELL_TYPE_STRING,
                        HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
                        BorderStyle.NONE, new java.awt.Color(0x000000),
                        (short) 20, new java.awt.Color(0xffffff), "宋体",
                        new Color(0x857F7F), FillPatternType.SOLID_FOREGROUND);
            }

            // 锁定第1第2行
            sheet.createFreezePane(0, 2);

            // wb内存写入文件
            String filepath = dataToolsProperties.getFileoutputDir() +
                    File.separator +
                    ttInfo.getName() + "预览信息-" +
                    new DateTime().toString("yyyyMMddHHmmss") + ".xlsx";
            File file = new File(filepath);
            excelPoiOperator.writeExcel(file, wb);

            DataToolsFile dataToolsFile = new DataToolsFile();
            dataToolsFile.setFileType(DataToolsFileType.XLSX);
            dataToolsFile.setFile(file);

            return dataToolsFile;

        } catch (Exception exp) {
            LOGGER.error("----------------- 导出时刻表[{}]预览视图信息失败---------------", ttInfo.getName());
            throw new ScheduleException(exp);
        }
    }
    private List<TTInfoDetail> sortedByBcsjAsc(List<TTInfoDetail> bcList, boolean isUp) {
        List<TTInfoDetail> sortedList = new ArrayList<>();
        for (TTInfoDetail ttInfoDetail : bcList) {
            if (isUp) {
                if ("0".equals(ttInfoDetail.getXlDir())) {
                    sortedList.add(ttInfoDetail);
                }
            } else {
                if ("1".equals(ttInfoDetail.getXlDir())) {
                    sortedList.add(ttInfoDetail);
                }
            }
        }
        Collections.sort(sortedList, new Comparator<TTInfoDetail>() {
            @Override
            public int compare(TTInfoDetail o1, TTInfoDetail o2) {
                Date d1 = MyDateUtils.hhssTimePlusMinunits(o1.getFcsj(), 0, 0);
                Date d2 = MyDateUtils.hhssTimePlusMinunits(o2.getFcsj(), 0, 0);

                return d1.compareTo(d2);
            }
        });

        return sortedList;
    }

    @Transactional
    @Override
    public void addLp(Long ttInfoId, Long lpId) {
        List<TTInfoDetail> bcList = this.ttInfoDetailRepository.findBcdetails(ttInfoId, lpId);
        if (!CollectionUtils.isEmpty(bcList)) {
            throw new RuntimeException("路牌已经存在!");
        }
        List<TTInfoDetail> allBcList = this.ttInfoDetailRepository.findByTtinfoId(ttInfoId);
        if (CollectionUtils.isEmpty(allBcList)) {
            throw new RuntimeException("时刻表没有班次(添加路牌至少需要一个班次)!");
        }

        Integer maxBcs = this.ttInfoDetailRepository.findMaxBcs(ttInfoId).intValue();
        GuideboardInfo lpInfo = this.guideboardInfoRepository.findById(lpId).get();
        // 将当前时刻表的第一个班次作为新增路牌的第一个班次,然后设置发车时间为00:00
        TTInfoDetail firstBc = allBcList.get(0);
        TTInfoDetail newBc = new TTInfoDetail();
        BeanUtils.copyProperties(firstBc, newBc);
        newBc.setId(null);
        newBc.setLp(lpInfo);
        newBc.setFcno(1);
        newBc.setBcs(maxBcs + 1);
        newBc.setFcsj("00:00");
        newBc.setCreateDate(new Date());
        newBc.setUpdateDate(new Date());
        newBc.setCreateBy(SecurityUtils.getCurrentUser());
        newBc.setUpdateBy(SecurityUtils.getCurrentUser());

        this.ttInfoDetailRepository.save(newBc);
    }

    @Transactional
    @Override
    public void removeBcByLp(Long ttInfoId, Long lpId) {
        this.ttInfoDetailRepository.deleteByTtinfoIdAndLpidWithModify(ttInfoId, lpId);
    }

    @Transactional
    @Override
    public void switchBcByLp(Long ttInfoId, Long lpAId, Long lpBId) {
        List<TTInfoDetail> lpABcList = this.ttInfoDetailRepository.findBcdetails(ttInfoId, lpAId);
        List<TTInfoDetail> lpBBcList = this.ttInfoDetailRepository.findBcdetails(ttInfoId, lpBId);

        if (!CollectionUtils.isEmpty(lpABcList) && !CollectionUtils.isEmpty(lpBBcList)) {
            GuideboardInfo lpA = lpABcList.get(0).getLp();
            GuideboardInfo lpB = lpBBcList.get(0).getLp();

            for (TTInfoDetail lpABc : lpABcList) {
                lpABc.setLp(lpB);
            }
            for (TTInfoDetail lpBBc : lpBBcList) {
                lpBBc.setLp(lpA);
            }
        }
    }

    @Override
    public TTInfoDetailForEdit.EditInfo getEditInfo(Integer xlid, Long ttid, Long maxfcno) throws ScheduleException {
        return ttInfoDetailForEdit.getEditInfo(xlid, ttid, maxfcno);
    }

    public static void main(String[] args) {
        String test = "中1中111";
        System.out.println(test.replaceAll("(\\d+)$", ""));
    }

    @Override
    public void validateExcelSheet(
            String filename,
            String sheetname,
            Integer lineid,
            String linename,
            Integer lineversion,
            TimetableExcelFormatType timetableExcelFormatType
    ) throws ScheduleException {
        TimetableExcelData timetableExcelData = TimetableExcelData.withValidateBuilder()
                .setExcelFilePath(filename)
                .setExcelSheetName(sheetname)
                .setLineId(lineid)
                .setLineRouteVersion(lineversion)
                .setLineService(this.lineService)
                .setLsStationRouteService(this.lsStationRouteService)
                .setGuideboardInfoService(this.guideboardInfoService)
                .setTimetableExcelFormatType(timetableExcelFormatType)
                .setTimetableExcelDataValidateStrategy(new TimetableExcelDataValidateStrategyImpl())
                .build();
        timetableExcelData.doValidate();
    }

    @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);
    }

    /**
     * @description (TODO) 时刻表明细模型数据保存.
     *
     * @param  map
     *
     * @return : 返回保存操作后的状态.
     *
     * @exception 处理所有抛出来的异常.
     * */
    @Transactional
   	public Map<String, Object> skbDetailMxSave(Map<String, Object> map) {
   		Map<String, Object> rs_m = new HashMap<String,Object>();
   		try {
   			// 1、获取数据.
   			String d = map.get("d") == null ? null : map.get("d").toString();
   			//boolean b = map.get("istidc") ==null ? false : Boolean.parseBoolean(map.get("istidc").toString());
   			Long ttinfoid = map.get("skb") ==null ? null : Long.parseLong(map.get("skb").toString());
   			Integer xlid = map.get("xl") ==null ? null : Integer.parseInt(map.get("xl").toString());
   			if(xlid !=null && ttinfoid !=null)
   	    		ttInfoDetailRepository.deltidc(xlid,ttinfoid);
   	    	if(d!=null)
   	    		ttInfoDetailRepository.saveAll(jsonArrayToListEntity(d));// 2、保存.
   		} catch (Exception e) {
   			e.printStackTrace();
   			rs_m.put("status", ResponseCode.ERROR);
   		}
   		rs_m.put("status", ResponseCode.SUCCESS);
   		return rs_m;
   	}

    /**
     * @description : (TODO) json班次数据转list班次.
     *
     * @param  jsonStr 班次json字符串]
     *
     * @return 返回一个list分装的班次数据.
     *
     * @status OK.
     * */
   	public List<TTInfoDetail> jsonArrayToListEntity(String jsonStr) throws Exception {
   		// 1、创建list分装的时刻表明细实体对象数据.
   		List<TTInfoDetail> listTd = new ArrayList<TTInfoDetail>();
   		// 2、json字符串转json数组.
   		JSONArray jsonArray = JSONArray.parseArray(jsonStr);
   		// 3、json数组转list分装的时刻表明细实体对象数据.
   		for(int i =0; i<jsonArray.size();i++) {
   			JSONObject jsonObj = jsonArray.getJSONObject(i);
   			listTd.add(objToEntity(jsonObj));
   		}
   		return listTd;
   	}

   	/**
   	 * @description : (TODO) 班次map对象转实体对象.
   	 *
   	 * @param obj 班次map对象.
   	 *
   	 * @return 返回一个班次实体对象.
   	 *
   	 * @exception 异常暂先抛出去.
   	 * */
   	public TTInfoDetail objToEntity(JSONObject obj) throws Exception {
   		// 1、创建班次实体对象.
   		TTInfoDetail td = new TTInfoDetail();
   		// 2、获取线路id键值,并查询出该线路的实体对象.
   		Line xl = lineRepository.findById(Integer.parseInt(obj.getString("xl"))).get();
   		// 3、设置线路.
   		td.setXl(xl);
   		// 4、设置时刻表.
   		td.setTtinfo(obj.getString("ttinfo") == null ? null : infoRepository.findById(Long.parseLong(obj.getString("ttinfo"))).get());
   		// 5、设置路牌.
   		td.setLp(obj.getString("lp") == null ? getLp(xl,obj.getString("lpName"),Integer.parseInt(obj.getString("lpNo")),obj.getString("lpType")) :
   			guideboardInfoRepository.findById(Long.parseLong(obj.getString("lp"))).get());
   		// 6、设置发车序号.
   		td.setFcno(obj.getString("fcno") == null ? null : Integer.parseInt(obj.getString("fcno")));
   		// 7、设置线路方向
   		td.setXlDir(dirToCod(obj.get("xlDir").toString()));
   		// 8、设置起始站.
   		td.setQdz(obj.getString("qdz") ==null? null : staRepository.findById(Integer.parseInt(obj.getString("qdz"))).get());
   		// 9、设置终点站.
   		td.setZdz(obj.getString("zdz") ==null ? null :staRepository.findById(Integer.parseInt(obj.getString("zdz"))).get());
   		// 10、设置停车场.
   		td.setTcc(obj.getString("tcc") == null ? null : carParkRepository.findById(Integer.parseInt(obj.getString("tcc"))).get());
   		// 11、设置发车时间.
   		td.setFcsj(obj.getString("fcsj")==null ? null : obj.getString("fcsj"));
   		// 12、设置班次序号.
   		td.setBcs(obj.getString("bcs") == null ? null : Integer.parseInt(obj.getString("bcs")));
   		// 13、设置计划里程.
   		td.setJhlc(obj.getString("jhlc")==null ? null : Double.parseDouble(obj.getString("jhlc")));
   		// 14、设置班次历时.
   		td.setBcsj(obj.getString("bcsj") == null ? null : Integer.parseInt(obj.getString("bcsj")));
   		// 15、设置班次类型.
   		td.setBcType(obj.getString("bcType")==null ? null : obj.getString("bcType"));
   		// 16、设置是否分班.
   		td.setIsFB(obj.getString("isfb") ==null ? null : intToBit(Integer.parseInt(obj.getString("isfb"))));
   		// 17、设置是否切换线路.
   		td.setIsSwitchXl(obj.getString("isSwitchXl") ==null ? null : intToBit(Integer.parseInt(obj.getString("isSwitchXl"))));
   		// 18、设置切换线路.
   		td.setSwitchXl(obj.getString("witchXl") ==null? null : lineRepository.findById(Integer.parseInt(obj.getString("witchXl"))).get());
   		td.setSwitchXlDesc(obj.getString("switchXlDesc")==null ? null : obj.getString("switchXlDesc"));
   		// 19、设置说明描述
   		td.setRemark(obj.getString("remark")== null ? null : obj.getString("remark"));
   		// 20、设置创建人.
   		td.setCreateBy(obj.getString("createBy") == null ? null : sysUserRepository.findById(Integer.parseInt(obj.getString("remark"))).get());
   		// 21、设置修改人.
   		td.setUpdateBy(obj.getString("updateBy") == null ? null : sysUserRepository.findById(Integer.parseInt(obj.getString("updateBy"))).get());
   		// 22、返回实体对象.
   		return td;
   	}

   	/**
   	 * @description : (TODO) int转boolean类型.
   	 *
   	 * @param value--int类型的数值]
   	 *
   	 * @return : 返回一个布尔类型值.
   	 * */
   	public Boolean intToBit(int value) {
   		Boolean tag = null;
   		if(value==0)
   			tag = false;
   		else if(value ==1)
   			tag = true;
   		return tag;
   	}

   	/**
   	 * @description (TODO) 获取路牌.
   	 *
   	 * @param xl --线路,name--路牌名称,code--路牌编码,lpType--路牌类型]
   	 *
   	 * @return 返回路牌.
   	 * */
   	public GuideboardInfo getLp(Line xl,String name, int code, String lpType) throws Exception {
   		// 1、创建路牌实体对象.
   		GuideboardInfo entity = new GuideboardInfo();
   		// 2、验证是否存在该路牌.如果数据库存在,则使用已有的.否则保存一块新的路牌.
   		List<GuideboardInfo> lgi = guideboardInfoRepository.validateLp(xl, name, code,lpType);
   		if(lgi.size()>0) {
   			entity = lgi.get(0);
   		}else{
   			// 1、设置线路.
   			entity.setXl(xl);
   			// 2、设置路牌编码.
   			entity.setLpNo(code);
   			// 3、设置路牌名称.
   			entity.setLpName(name);
   			// 4、设置路牌类型.
   			entity.setLpType(lpType);
   			// 5、标记是否已删除.
   			entity.setIsCancel(false);
   			// 6、设置创建人.
   			entity.setCreateBy(null);
   			// 7、设置修改人.
   			entity.setUpdateBy(null);
   			// 8、保存路牌.
   			guideboardInfoRepository.save(entity);
   		}
   		// 9、返回路牌.
   		return entity;
   	}

   	/**
   	 * @description : (TODO) 线路方向转代码.
   	 *
   	 * @param str--方向字符串]
   	 *
   	 * @return 返回方向代码.
   	 *
   	 * @exception 异常暂先抛出.
   	 * */
   	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;
   	}
}