InoutCarparkServiceImpl.java 8.61 KB
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.bsth.util.CustomBeanUtils;
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;

	@Autowired
	private LineVersionsRepository lineVersionsRepository;

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

	/**
	 * 添加进出场路段和路由
	 */
	@Override
	@Transactional
	public void add(LsInoutSectionRoute sectionRoute) {
		Section section = sectionRoute.getSection();
		Line line = lineRepository.findById(sectionRoute.getLine().getId()).get();
		Integer version = lineVersionsRepository.findCurrentVersion(line.getId());
		boolean isPresent = sectionRepository.findById(section.getId()).isPresent();
		sectionRoute.setLineCode(line.getLineCode());
		if (sectionRoute.getVersions() < version) {
			throw new IllegalArgumentException("历史版本不可变更");
		}

		// 路段坐标信息
		if (!isPresent) {
			SectionServiceImpl.centerLine(section);
		}

		lsInoutSectionRouteRepository.updateSectiouRouteCode(sectionRoute);
		if (!isPresent) {
			sectionRepository.save(section);
		}
		lsInoutSectionRouteRepository.save(sectionRoute);
	}

	/** 百度坐标转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;

	}

	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public void modify(LsInoutSectionRoute sectionRoute) {
		Section section = sectionRoute.getSection();
		LsInoutSectionRoute sectionRoute1 = lsInoutSectionRouteRepository.findById(sectionRoute.getId()).get();
		Section section1 = sectionRepository.findById(section.getId()).get();
		Integer version = lineVersionsRepository.findCurrentVersion(sectionRoute1.getLine().getId());
		if (sectionRoute1.getVersions() < version) {
			throw new IllegalArgumentException("历史版本不可变更");
		}

		SectionServiceImpl.centerLine(section);

		lsInoutSectionRouteRepository.updateSectiouRouteCode(sectionRoute);
		CustomBeanUtils.copyPropertiesIgnoredNull(sectionRoute, sectionRoute1);
		CustomBeanUtils.copyPropertiesIgnoredNull(section, section1);
		sectionRepository.save(section1);
		lsInoutSectionRouteRepository.save(sectionRoute1);
	}

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

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

	@Override
	@Transactional(rollbackFor = RuntimeException.class)
	public void pathPlaningByHistory(long schId, int versions) {
		LsInoutSectionRoute sectionRoute = new LsInoutSectionRoute();
		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");
		StringBuilder sb = new StringBuilder("LINESTRING(");
		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;
			}
			sb.append(lng).append(" ").append(lat).append(",");
		}
		sb.deleteCharAt(sb.length() - 1).append(")");

		Integer sectionId = sectionRepository.findLatestSectionId() + 1;
		Line line = new Line();Section section = new Section();
		sectionRoute.setLine(line);
		sectionRoute.setSection(section);
		line.setId(Integer.parseInt(scheduleRealInfo.getXlBm()));
		line.setLineCode(scheduleRealInfo.getXlBm());
		section.setId(sectionId);
		section.setSectionCode(sectionId.toString());
		section.setSectionName(scheduleRealInfo.getQdzName() + "-" + scheduleRealInfo.getZdzName());
		section.setBsectionVectorWkt(sb.toString());
		section.setSectionTime(0.);
		section.setSectionDistance(0.);
		section.setSpeedLimit(60.);
		sectionRoute.setSectionCode(sectionId.toString());
		sectionRoute.setSectionrouteCode(1);
		sectionRoute.setStart(scheduleRealInfo.getQdzName());
		sectionRoute.setEnd(scheduleRealInfo.getZdzName());
		sectionRoute.setDirections(3);
		sectionRoute.setDestroy(0);
		sectionRoute.setVersions(versions);

		lsInoutSectionRouteRepository.destroy(scheduleRealInfo.getXlBm(), versions, scheduleRealInfo.getQdzName(), scheduleRealInfo.getZdzName());
		add(sectionRoute);
	}
}