TTInfoDetailController.java 20.9 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
package com.bsth.controller.schedule;

import com.bsth.common.ResponseCode;
import com.bsth.controller.BaseController;
import com.bsth.entity.CarPark;
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.schedule.TTInfoDetailRepository;
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.utils.DataImportExportService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * Created by xu on 16/7/2.
 */
@RestController
@RequestMapping("tidc")
public class TTInfoDetailController extends BaseController<TTInfoDetail, Long> {
    @Autowired
    private TTInfoDetailService ttInfoDetailService;
    @Autowired
    private CarParkService carParkService;
    @Autowired
    private LineInformationService lineInformationService;
    @Autowired
    private TTInfoDetailRepository ttInfoDetailRepository;
    @Autowired
    private DataImportExportService dataImportExportService;
    @Autowired
    private StationRouteService stationRouteService;
    @Autowired
    private GuideboardInfoService guideboardInfoService;


    public static class ExcelFileOutput {
        private String fileName;
        private List<Map<String, Object>> sheetnames = new ArrayList<>();

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public List<Map<String, Object>> getSheetnames() {
            return sheetnames;
        }

        public void setSheetnames(List<Map<String, Object>> sheetnames) {
            this.sheetnames = sheetnames;
        }
    }

    /**
     * 1、上传Excel文件,返回文件全路径名,工作区名称列表。
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ExcelFileOutput fileUpload(MultipartFile file) throws Exception {
        // 返回对象
        ExcelFileOutput rs = new ExcelFileOutput();

        // 上传文件
        File file1 = dataImportExportService.uploadFile(file);
        // 获取文件的sheet
        Workbook book = Workbook.getWorkbook(file1);
        for (Sheet sheet : book.getSheets()) {
            String sheetname = sheet.getName();
            Map<String, Object> s = new HashMap<>();
            s.put("name", sheetname);
            rs.getSheetnames().add(s);
        }

        rs.setFileName(file1.getAbsolutePath());
        return rs;
    }

    /**
     * 2、验证sheet(以后放到规则引擎里去做)。
     * @param filename excel文件全路径名
     * @param sheetname sheet名字
     * @param lineid 线路id
     * @param linename 线路名称
     * @return
     */
    @RequestMapping(value = "/validate/sheet", method = RequestMethod.POST)
    public Map<String, Object> validateSheet(String filename, String sheetname, Integer lineid, String linename) throws Exception {
        Map<String, Object> rtn = new HashMap<>();
        Workbook book = Workbook.getWorkbook(new File(filename));
        Sheet sheet = book.getSheet(sheetname);
        if (sheet.getRows() == 0 || sheet.getColumns() == 0) { // 工作区是否为空
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", String.format("%s 工作区没有数据!", sheetname));
        } else {
            if (sheet.getRows() <= 1 || sheet.getColumns() <= 1) {
                rtn.put("status", ResponseCode.ERROR);
                rtn.put("msg", String.format("工作区至少包含2行2列的数据"));
                return rtn;
            } 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)) {
                        rtn.put("status", ResponseCode.ERROR);
                        rtn.put("msg", String.format("第1行,第%d列数据不能为空", i + 1));
                        return rtn;
                    } else {
                        if (i == 0) { // 第一列必须是路牌2个字
                            if (!"路牌".equals(cell_con.trim())) {
                                rtn.put("status", ResponseCode.ERROR);
                                rtn.put("msg", "第1行,第1列数据必须是路牌2个字");
                                return rtn;
                            }
                        } 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");

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

                        }
                    }
                }

                // 验证路牌内容
                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)) {
                        rtn.put("status", ResponseCode.ERROR);
                        rtn.put("msg", String.format("第%d行,第1列路牌无数据", i + 1));
                        return rtn;
                    } else if (gbindexmap.get(bcell_con.trim()) != null) {
                        rtn.put("status", ResponseCode.ERROR);
                        rtn.put("msg", String.format("第%d行,第1列的路牌数据与第%d行,第1列数据重复",
                                i + 1,
                                gbindexmap.get(bcell_con.trim())));
                        return rtn;
                    } 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)) {
                            rtn.put("status", ResponseCode.ERROR);
                            rtn.put("msg", String.format("第%d行,第1列的路牌在%s中不存在", i + 1, linename));
                            return rtn;
                        } else if (guideboardInfoList.size() > 1) {
                            rtn.put("status", ResponseCode.ERROR);
                            rtn.put("msg", String.format("第%d行,第1列的路牌在%s中重复", i + 1, linename));
                            return rtn;
                        } else {
                            gbindexmap.put(bcell_con.trim(), i + 1);
                        }
                    }
                }

                // 班次时间验证,正则表达式,格式hh:mm或者hhmm
                String el = "^(([0-1]\\d)|(2[0-4])):[0-5]\\d$"; // hh:mm格式
                String el2 = "^(([0-1]\\d)|(2[0-4]))[0-5]\\d$"; // hhmm格式
                Pattern p = Pattern.compile(el);
                Pattern p2 = Pattern.compile(el2);

                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 m = p.matcher(bcell_con.trim());
                            Matcher m2 = p2.matcher(bcell_con.trim());
                            if ((!m.matches()) && (!m2.matches())) {
                                rtn.put("status", ResponseCode.ERROR);
                                rtn.put("msg", String.format("第%d行,第%d列的发车时间格式不正确,格式应为hh:mm或hhmm", i + 1, j + 1));
                                return rtn;
                            }
                        }
                    }
                }
            }

        }

        rtn.put("status", ResponseCode.SUCCESS);
        return rtn;
    }

    /**
     * 3、验证关联的线路标准信息(以后放到规则引擎里去做)。
     * @param lineinfoid
     * @return
     */
    @RequestMapping(value = "/validate/lineinfo", method = RequestMethod.GET)
    public Map<String, Object> validateAssoLineInfo(Integer lineinfoid) {
        Map<String, Object> rtn = new HashMap<>();
        LineInformation lineInformation = lineInformationService.findById(lineinfoid);
        if (lineInformation.getUpInMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行进场里程为空");
            return rtn;
        } else if (lineInformation.getUpInTimer() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行进场时间为空");
            return rtn;
        } else if (lineInformation.getUpOutMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行出场里程为空");
            return rtn;
        } else if (lineInformation.getUpOutTimer() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行出场时间为空");
            return rtn;
        } else if (lineInformation.getUpMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行班次里程为空");
            return rtn;
        } else if (lineInformation.getUpTravelTime() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "上行班次时间为空");
            return rtn;
        } else if (lineInformation.getDownInMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行进场里程为空");
            return rtn;
        } else if (lineInformation.getDownInTimer() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行进场时间为空");
            return rtn;
        } else if (lineInformation.getDownOutMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行出场里程为空");
            return rtn;
        } else if (lineInformation.getDownOutTimer() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行出场时间为空");
            return rtn;
        } else if (lineInformation.getDownMileage() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行班次里程为空");
            return rtn;
        } else if (lineInformation.getDownTravelTime() == null) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "下行班次时间为空");
            return rtn;
        } else if (StringUtils.isEmpty(lineInformation.getCarPark())) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", "停车场必须选择");
            return rtn;
        }

        // 单独验证停车场信息
        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)) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", String.format("线路标准里的停车场code=%s,在停车场信息中未找到", tcccode));
            return rtn;
        } else if (carParkList.size() > 1) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", String.format("线路标准里的停车场code=%s,在停车场信息中有重复数据", tcccode));
            return rtn;
        } else {
            CarPark carPark = carParkList.get(0);
            if (StringUtils.isEmpty(carPark.getParkName())) {
                rtn.put("status", ResponseCode.ERROR);
                rtn.put("msg", String.format("线路标准里的停车场code=%s,在停车场信息中没有停车场名字", tcccode));
                return rtn;
            }
        }

        rtn.put("status", ResponseCode.SUCCESS);
        return rtn;
    }

    /**
     * 4、导入时刻表明细数据。
     * @param form
     * @return
     */
    @RequestMapping(value = "/importfile", method = RequestMethod.POST)
    public Map<String, Object> importTTinfo(@RequestParam Map<String, Object> form) throws Exception {
        Map<String, Object> rtn = new HashMap<>();

        // 1、修改已经上传的excel文件,在每个起点站后标示数字,表示第几个班次
        // 2、由于格式问题,需要把内容都转换成字符串
        String filename = (String) form.get("filename");
        List<String> colList = new ArrayList<>();
        Workbook workbook = Workbook.getWorkbook(new File(filename));
        Sheet sheet = workbook.getSheet((String) form.get("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);
            }
        }

        WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File(filename + "_temp.xls"), workbook);
        WritableSheet sheet1 = writableWorkbook.getSheet((String) form.get("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、删除原有数据
        ttInfoDetailService.deleteByTtinfo(Long.valueOf(form.get("ttid").toString()));

        // 3、导入时刻表
        // 获取停车场名字
        LineInformation lineInformation = lineInformationService.findById(Integer.valueOf(form.get("lineinfo").toString()));
        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();

        ttInfoDetailService.fileDataImport(
                new File(filename + "_temp.xls"),
                (String) form.get("xlname"),
                (String) form.get("ttname"),
                tccname
        );

        return rtn;
    }

    //------------- 旧版本 --------------//
    @RequestMapping(value = "/dataImportExtend", method = RequestMethod.POST)
    public Map<String, Object> uploadDataAndImport(
            MultipartFile file, String xlmc, String ttinfoname) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 查找lineinformation对象,没有报错
            Map<String, Object> param = new HashMap<>();
            param.put("line.name_eq", xlmc);
            Iterator<LineInformation> lineInformationIterator = lineInformationService.list(param).iterator();
            if (!lineInformationIterator.hasNext()) {
                // 没有lineinformation,报错
                resultMap.put("status", ResponseCode.ERROR);
                resultMap.put("msg", "没有lineinfomation,线路名称=" + xlmc);
            } else {
                String tcccode = lineInformationIterator.next().getCarPark();
                if (StringUtils.isEmpty(tcccode)) {
                    // 没有停车场code,报错
                    resultMap.put("status", ResponseCode.ERROR);
                    resultMap.put("msg", "线路lineinfomation没有停车场code信息,线路名称=" + xlmc);
                } else {
                    // 使用停车场code查找停车场
                    param.clear();;
                    param.put("parkCode_eq", tcccode);
                    Iterator<CarPark> carParkIterator = carParkService.list(param).iterator();
                    if (!carParkIterator.hasNext()) {
                        // 指定的停车场code没有找到停车场信息,报错
                        resultMap.put("status", ResponseCode.ERROR);
                        resultMap.put("msg", "没有找到停车场信息,停车场code=" + tcccode);
                    } else {
                        String tccname = carParkIterator.next().getParkName();
                        if (StringUtils.isEmpty(tccname)) {
                            // 没有停车场名字,报错
                            resultMap.put("status", ResponseCode.ERROR);
                            resultMap.put("msg", "停车场信息没有停车场名字,停车场code=" + tcccode);
                        } else {
                            ttInfoDetailService.fileDataImport(file, xlmc, ttinfoname, tccname);
                            resultMap.put("status", ResponseCode.SUCCESS);
                            resultMap.put("msg", "导入成功");
                        }
                    }
                }
            }
        } catch (Exception exp) {
            exp.printStackTrace();
            throw exp;
        }

        return resultMap;
    }

    @RequestMapping(value = "/edit/{xlid}/{ttid}", method = RequestMethod.GET)
    public Object getEditInfo(
            @PathVariable("xlid") Integer xlid,
            @PathVariable("ttid") Long ttid) throws Exception {
        // TODO:返回类型需要修正
        return ttInfoDetailService.getEditInfo(xlid, ttid);
    }

    /**
     * 覆写方法,因为form提交的方式参数不全,改用 json形式提交 @RequestBody
     * @Title: save
     * @Description: TODO(持久化对象)
     * @param @param t
     * @param @return    设定文件
     * @return Map<String,Object>  {status: 1(成功),-1(失败)}
     * @throws
     */
    @RequestMapping(method = RequestMethod.POST)
    public Map<String, Object> save(@RequestBody TTInfoDetail t){

        return baseService.save(t);
    }

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

    @RequestMapping(value = "/bcdetail", method = RequestMethod.GET)
    public List<TTInfoDetail> findBcdetails(Integer xlId, Long ttinfoId, Long lpId) {
        return ttInfoDetailRepository.findBcdetails(xlId, ttinfoId, lpId);
    }
}