InoutCarparkServiceImpl.java 18.5 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
package com.bsth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.data.BasicData;
import com.bsth.entity.*;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.repository.*;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.service.InoutCarparkService;
import com.bsth.service.gps.GpsService;
import com.bsth.util.CoordinateConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Hill
 */
@Service
public class InoutCarparkServiceImpl extends BaseServiceImpl<LsInoutSectionRoute, Long> implements InoutCarparkService {

	private final static Logger logger = LoggerFactory.getLogger(InoutCarparkServiceImpl.class);

	@Autowired
	private LsStationRouteRepository lsStationRouteRepository;

	@Autowired
	private CarParkRepository carParkRepository;

	@Autowired
	private LsInoutSectionRouteRepository lsInoutSectionRouteRepository;

	@Autowired
	private SectionRepository sectionRepository;

	@Autowired
	LineRepository lineRepository;

	@Autowired
	private ScheduleRealInfoRepository scheduleRealInfoRepository;

	@Autowired
	private GpsService gpsService;

	@Override
	public Map<String, Object> getStartEndByLine(int lineId, int version) {
		Map<String, Object> result = new HashMap<>(), data = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("data", data);

		List<LsStationRoute> upRoutes = lsStationRouteRepository.findByLineVersion(lineId, version, 0), downRoutes = lsStationRouteRepository.findByLineVersion(lineId, version, 1);
		CarPark carPark = carParkRepository.findByLineId(lineId);
		if (carPark == null) {
			result.put("code", 500);
			result.put("msg", "线路标准信息中无相应的停车场信息");

			return  result;
		}
		if (upRoutes.size() < 2) {
			result.put("code", 500);
			result.put("msg", "线路上行站级少于2");

			return result;
		}
		LsStationRoute upFirst = upRoutes.get(0), upLast = upRoutes.get(upRoutes.size() - 1);
		LsStationRoute downFirst = downRoutes.get(0), downLast = downRoutes.get(downRoutes.size() - 1);
		// 环线或双环线只需提取一个起点站
		if (upFirst.getStationCode().equals(upLast.getStationCode())) {
			data.put("start", new LsStationRoute[]{ upFirst });
			data.put("end", new LsStationRoute[]{ upFirst });
		} else {
			if (downRoutes.size() < 2) {
				result.put("code", 500);
				result.put("msg", "非环线线路下行站级少于2");

				return result;
			}
			data.put("start", new LsStationRoute[]{ upFirst, downFirst });
			data.put("end", new LsStationRoute[]{ upLast, downLast });
		}
		data.put("carpark", carPark);

		return result;
	}

	@Override
	public Map<String, Object> getRouteByStartEnd(int lineId, int version, String start, String end) {
		Map<String, Object> result = new HashMap<>(), data = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("data", data);
		data.put("routes", lsInoutSectionRouteRepository.getRouteByStartEnd(lineId, version, start, end));

		return result;
	}

	/**
	 * 新增路段信息
	 *
	 * @param map:<bsectionVector:折线百度坐标集合;dbType:圆坐标类型;descriptions:描述与说明;destroy:是否撤销;directions:方向;lineId:线路ID
	 *
	 * 			   lineCode :线路编码;roadCoding:道路编码;sectionCode:路段编码;sectionDistance:路段长度;sectionName:路段名称;sectionTime:路段时长;
	 *
	 * 			   sectionrouteCode:路段序号;speedLimit:路段限速>
	 *
	 * @return map<SUCCESS:成功;ERROR:异常>
	 */
	@Override
	@Transactional
	public Map<String, Object> sectionSave(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 线路ID.
			Integer lineId = map.get("lineId").equals("") ? null : Integer.valueOf(map.get("lineId").toString());
			// 线路编码.
			String lineCode = map.get("lineCode").equals("") ? "" : map.get("lineCode").toString();
			// 路段名称.
			String sectionName = map.get("sectionName").equals("") ? "" : map.get("sectionName").toString();
			// 路段编码.
			String sectionCode = map.get("sectionCode").equals("") ? "" : map.get("sectionCode").toString();
			// 道路编码.
			String roadCoding = map.get("roadCoding").equals("") ? "" : map.get("roadCoding").toString();
			// 原始坐标类型.
			String dbType = map.get("dbType").equals("") ? "" :map.get("dbType").toString();
			// 路段几何图形坐标.
			String sectionJSON = map.get("bsectionVector").equals("") ? "" : map.get("bsectionVector").toString();
			// 路段序号
			String sectionrouteCode = map.get("sectionrouteCode").equals("") ? "" : map.get("sectionrouteCode").toString();
			// 路段时长.
			Double sectionTime = map.get("sectionTime").equals("") ? 0.0d : Double.valueOf(map.get("sectionTime").toString());
			// 路段距离.
			Double sectionDistance = map.get("sectionDistance").equals("") ? 0.0d : Double.valueOf(map.get("sectionDistance").toString());
			// 路段限速.
			Double speedLimit = map.get("speedLimit").equals("") ? 0.0d : Double.valueOf(map.get("speedLimit").toString());
			// 版本.
			Integer versions = map.get("versions").equals("") ? 1 : Integer.valueOf(map.get("versions").toString());
			// 是否撤销.
			Integer destroy = map.get("destroy").equals("") ? 0 : Integer.valueOf(map.get("destroy").toString());
			// 路段方向.
			Integer directions = map.get("directions").equals("") ? 0 : Integer.valueOf(map.get("directions").toString());
			// 描述与说明.
			String descriptions = map.get("descriptions").equals("")? "" : map.get("descriptions").toString();
			// 起始
			String start = map.get("start").equals("")? "" : map.get("start").toString();
			// 结束
			String end = map.get("end").equals("")? "" : map.get("end").toString();
			// 原始线状图形坐标集合
			String sectionsBpoints = "";
			// WGS线状图形坐标集合
			String sectionsWJPpoints = "";
			if(!sectionJSON.equals("")) {
				// 转换成JSON数组
				JSONArray sectionsArray = JSONArray.parseArray(sectionJSON);
				// 遍历
				for(int s = 0 ;s<sectionsArray.size();s++) {
					String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();
					String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();
					String WGSLngStr = "";
					String WGSLatStr = "";
					CoordinateConverter.Location resultPoint = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
					WGSLngStr = String.valueOf(resultPoint.getLng());
					WGSLatStr = String.valueOf(resultPoint.getLat());
					if(s==0) {
						sectionsBpoints = pointsLngStr + " " + pointsLatStr;
						sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
					}else {
						sectionsBpoints = sectionsBpoints + "," +  pointsLngStr + " " + pointsLatStr;
						sectionsWJPpoints = sectionsWJPpoints + ","  +   WGSLngStr + " " + WGSLatStr;
					}
				}
			}
			// WGS坐标点集合
			String gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
			// 原坐标点集合
			String bsectionVector = "LINESTRING(" + sectionsBpoints + ")";
			String crosesRoad="";
			String endNode ="";
			String startNode="";
			String middleNode="";
			String sectionType="";
			String csectionVector=null;
			Integer id = Integer.valueOf(sectionCode);
			sectionRepository.systemSave(sectionCode, sectionName, crosesRoad, endNode, startNode, middleNode, gsectionVector, bsectionVector, sectionType, csectionVector, roadCoding, sectionDistance, sectionTime, dbType, speedLimit, descriptions, versions, id);
			Section section = sectionRepository.findById(id).get();

			Line line = lineRepository.findById(lineId).get();


			if (map.get("status") == null || Integer.parseInt(map.get("status").toString()) != 0) {

				LsInoutSectionRoute sectionRoute = new LsInoutSectionRoute();
				Integer routeCode = null;
				if(!sectionrouteCode.equals("")){
					String sectionrouteCodeArray[] =  sectionrouteCode.split("_");
					routeCode = Integer.valueOf(sectionrouteCodeArray[0])+1;
				}else {
					routeCode = 1;
				}
				lsInoutSectionRouteRepository.sectionUpdSectionRouteCode(lineCode, versions, start, end, routeCode);
				sectionRoute.setSectionrouteCode(routeCode);
				sectionRoute.setLineCode(lineCode);
				sectionRoute.setSection(section);
				sectionRoute.setSectionCode(sectionCode);
				sectionRoute.setDirections(directions);
				sectionRoute.setDescriptions(descriptions);
				sectionRoute.setDestroy(destroy);
				sectionRoute.setVersions(versions);
				sectionRoute.setLine(line);
				sectionRoute.setStart(start);
				sectionRoute.setEnd(end);
				lsInoutSectionRouteRepository.save(sectionRoute);
			}


			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		return resultMap;
	}

	/** 百度坐标转WGS坐标 */
	public CoordinateConverter.Location FromBDPointToWGSPoint(String bLonx, String bLatx) {

		double lng = Double.parseDouble(bLonx);

		double lat = Double.parseDouble(bLatx);

		CoordinateConverter.Location bdLoc = CoordinateConverter.LocationMake(lng, lat);

		CoordinateConverter.Location location = CoordinateConverter.bd_decrypt(bdLoc);

		CoordinateConverter.Location WGSPoint = CoordinateConverter.transformFromGCJToWGS(location);

		return WGSPoint;

	}

	/**
	 * @Description :TODO(编辑线路走向)
	 *
	 * @param map <sectionId:路段ID; sectionJSON:路段信息>
	 *
	 * @return Map<String, Object> <SUCCESS ; ERROR>
	 */
	@Override
	@Transactional
	public Map<String, Object> sectionUpdate(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String bsectionVector = map.get("bsectionVector").equals("") ? "" :map.get("bsectionVector").toString();
			// 原始线状图形坐标集合
			String sectionsBpoints = "";
			// WGS线状图形坐标集合
			String sectionsWJPpoints = "";
			if(!bsectionVector.equals("")) {
				// 转换成JSON数组
				JSONArray sectionsArray = JSONArray.parseArray(bsectionVector);
				// 遍历
				for(int s = 0 ;s<sectionsArray.size();s++) {
					String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();
					String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();
					/** to WGS坐标 */
					CoordinateConverter.Location resultPoint = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
					String WGSLngStr = String.valueOf(resultPoint.getLng());
					String WGSLatStr = String.valueOf(resultPoint.getLat());
					if(s==0) {
						sectionsBpoints = pointsLngStr + " " + pointsLatStr;
						sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
					}else {
						sectionsBpoints = sectionsBpoints + "," +  pointsLngStr + " " + pointsLatStr;
						sectionsWJPpoints = sectionsWJPpoints + ","  +   WGSLngStr + " " + WGSLatStr;
					}
				}
			}
			// 原坐标类型
			String dbType = map.get("dbType").equals("") ? "" : map.get("dbType").toString();
			// 说明
			String descriptions = "";
			// 是否撤销
			Integer destroy = map.get("destroy").equals("") ? null : Integer.parseInt(map.get("destroy").toString());
			// 方向
			Integer directions = map.get("directions").equals("") ? null : Integer.parseInt(map.get("directions").toString());
			// 线路ID
			Integer sectionRouteLine = map.get("sectionRouteLine").equals("") ? null : Integer.parseInt(map.get("sectionRouteLine").toString());
			// 道路编码
			String roadCoding = map.get("roadCoding").equals("") ? "" : map.get("roadCoding").toString();
			// 路段编码
			String sectionCode = map.get("sectionCode").equals("") ? "" : map.get("sectionCode").toString();
			// 路段长度
			Double sectionDistance = map.get("sectionDistance").equals("") ? null : Double.valueOf(map.get("sectionDistance").toString());
			// 路段ID
			Integer sectionId = map.get("sectionId").equals("") ? 0 : Integer.parseInt(map.get("sectionId").toString());
			// 路段名称
			String sectionName = map.get("sectionName").equals("") ? "" : map.get("sectionName").toString();
			// 路段路由Id
			Long sectionRouteId = map.get("sectionRouteId").equals("") ? null : Long.valueOf(map.get("sectionRouteId").toString());
			// 线路编码
			String lineCode =map.get("lineCode").equals("") ? "" : map.get("lineCode").toString();
			// 路段时长
			Double  sectionTime = map.get("sectionTime").equals("") ? null : Double.valueOf(map.get("sectionTime").toString());
			// 路段路由
			Integer sectionrouteCode = "".equals(map.get("sectionrouteCode")) ? null : Integer.valueOf(map.get("sectionrouteCode").toString());
			// 限速
			Double speedLimit = map.get("speedLimit").equals("") ? null : Double.valueOf(map.get("speedLimit").toString());
			// 版本
			Integer version = map.get("versions").equals("") ? null : Integer.valueOf(map.get("versions").toString());
			// 起始
			String start = map.get("start").equals("")? "" : map.get("start").toString();
			// 结束
			String end = map.get("end").equals("")? "" : map.get("end").toString();
			// WGS坐标点集合
			String gsectionVector = null;
			if(!sectionsWJPpoints.equals("")) {
				gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
			}
			// 原坐标点集合
			String bsectionVectorS = null;
			if(!sectionsBpoints.equals("")) {
				bsectionVectorS = "LINESTRING(" + sectionsBpoints + ")";
			}
			Integer createBy = map.get("createBy").equals("") ? null : Integer.valueOf(map.get("createBy").toString());
			String createDate = map.get("createDate").equals("") ? null : map.get("createDate").toString();
			Integer updateBy = map.get("updateBy").equals("") ?null : Integer.valueOf(map.get("updateBy").toString());
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			// 修改日期
			String updateDate = formatter.format(date);
			String crosesRoad="";
			String endNode="";
			String startNode="";
			String middleNode="";
			String sectionType="";
			// 更新
			sectionRepository.sectionUpdate(sectionId, gsectionVector, bsectionVectorS, sectionCode, sectionName, crosesRoad, endNode, startNode, middleNode, sectionType, roadCoding, sectionDistance, sectionTime, dbType, speedLimit, descriptions, version, createBy, createDate, updateBy, updateDate);

			Line line = lineRepository.findById(sectionRouteLine).get();
			Section section = sectionRepository.findById(sectionId).get();

			if(map.get("status") == null || Integer.parseInt(map.get("status").toString()) != 0) {

				LsInoutSectionRoute resultS = lsInoutSectionRouteRepository.findById(sectionRouteId).get();
				int old_code = resultS.getSectionrouteCode();
				// 是否修改路段序号标记
				boolean type = false;
				if(sectionrouteCode!=null) {
					if(++sectionrouteCode == old_code) {
						type = true;
					}
					// 默认是最前面路段
				}else {
					sectionrouteCode = 1;
				}
				if(!type)
					lsInoutSectionRouteRepository.sectionUpdSectionRouteCode(lineCode, version, start, end,sectionrouteCode);
				LsInoutSectionRoute route = new LsInoutSectionRoute();
				route.setId(sectionRouteId);
				route.setSectionrouteCode(sectionrouteCode);
				route.setLineCode(lineCode);
				route.setSectionCode(sectionCode);
				route.setDirections(directions);
				route.setVersions(version);
				route.setDestroy(destroy);
				route.setCreateBy(createBy);
				route.setUpdateBy(updateBy);
				route.setSection(section);
				route.setLine(line);
				route.setStart(start);
				route.setEnd(end);
				lsInoutSectionRouteRepository.save(route);
			}
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> destroy(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			long id = Long.parseLong(map.get("id").toString());

			if(map.get("status") == null || Integer.parseInt(map.get("status").toString()) != 0) {
				lsInoutSectionRouteRepository.deleteById(id);
			}
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("destroy erro.", e);
		}
		return resultMap;
	}

	@Override
	@Transactional
	public void pathPlaningByHistory(long schId, int versions) throws JsonProcessingException {
		ScheduleRealInfo scheduleRealInfo = scheduleRealInfoRepository.findById(schId).get();
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm");
		DateTime start = formatter.parseDateTime(scheduleRealInfo.getRealExecDate() + scheduleRealInfo.getFcsjActual());
		DateTime end = formatter.parseDateTime(scheduleRealInfo.getRealExecDate() + scheduleRealInfo.getZdsjActual());
		if (start.isAfter(end)) {
			end = end.plusDays(1);
		}
		Map<String, Object> map = gpsService.history(new String[]{ scheduleRealInfo.getClZbh() }, start.getMillis() / 1000 - 20, end.getMillis() / 1000 + 20);
		List<Map<String, Object>> list = (List<Map<String, Object>>)map.get("list");
		List<Map<String, Object>> bsectionVector = new ArrayList<>();
		for (Map<String, Object> m : list) {
			Map<String, Object> m1 = new HashMap<>();
			Float lng = (Float)m.get("lon"), lat = (Float)m.get("lat");
			if (lng == 0 || lat == 0) {
				continue;
			}
			m1.put("lng", m.get("bd_lon"));
			m1.put("lat", m.get("bd_lat"));
			bsectionVector.add(m1);
		}
		map.clear();
		map.put("lineId", scheduleRealInfo.getXlBm());
		map.put("lineCode", scheduleRealInfo.getXlBm());
		map.put("sectionName", scheduleRealInfo.getQdzName() + "-" + scheduleRealInfo.getZdzName());
		map.put("sectionCode", sectionRepository.findLatestSectionId() + 1);
		map.put("roadCoding", "");
		map.put("dbType", "b");
		map.put("sectionrouteCode", "1");
		map.put("sectionTime", "");
		map.put("sectionDistance", "");
		map.put("speedLimit", "60");
		map.put("versions", versions);
		map.put("destroy", 0);
		map.put("directions", 3);
		map.put("descriptions", "");
		map.put("start", scheduleRealInfo.getQdzName());
		map.put("end", scheduleRealInfo.getZdzName());
		map.put("bsectionVector", new ObjectMapper().writeValueAsString(bsectionVector));

		lsInoutSectionRouteRepository.destroy(scheduleRealInfo.getXlBm(), versions, scheduleRealInfo.getQdzName(), scheduleRealInfo.getZdzName());
		sectionSave(map);
	}
}