TTInfoDetailServiceImpl.java 16.4 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
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.*;
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.repository.sys.SysUserRepository;
import com.bsth.service.CarParkService;
import com.bsth.service.LineInformationService;
import com.bsth.service.LineService;
import com.bsth.service.StationRouteService;
import com.bsth.service.schedule.GuideboardInfoService;
import com.bsth.service.schedule.TTInfoDetailService;
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.ExcelData;
import com.bsth.service.schedule.timetable.ExcelFormatType;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsFileType;
import com.bsth.service.schedule.utils.DataToolsService;
import com.bsth.service.schedule.utils.PoiUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 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 StationRouteService stationRouteService;
    @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_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 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);
    }

    @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, ExcelFormatType excelFormatType) throws ScheduleException {
        ExcelData excelData = new ExcelData(
                filename, sheetname, lineid,
                lineService, guideboardInfoService,
                lineversion, stationRouteService,
                excelFormatType);
        excelData.validateExcelSheet();
    }

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