StationServiceImpl.java 4.12 KB
package com.bsth.service.impl;

import com.bsth.entity.Station;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.*;
import com.bsth.service.StationService;
import com.bsth.util.CoordinateConverter;
import com.bsth.util.CoordinateConverter.Location;
import com.bsth.util.CustomBeanUtils;
import com.bsth.util.Geo.GeoUtils;
import com.bsth.util.Geo.Point;
import com.bsth.util.GeoConverter;
import org.geolatte.geom.codec.Wkt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName: StationServiceImpl(站点service业务层实现类)
 * 
 * @Extends : BaseService
 * 
 * @Description: TODO(站点service业务层)
 * 
 * @Author bsth@lq
 * 
 * @Date 2016年05月03日 上午9:21:17
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */

@Service
public class StationServiceImpl extends BaseServiceImpl<Station, Integer> implements StationService {

	@Autowired
	private StationRepository stationRepository;

	@Autowired
	private LsStationRouteRepository lsStationRouteRepository;

	@Autowired
	private LineRepository lineRepository;

	@Autowired
	private SectionRepository sectionRepository;

	@Autowired
	private LsSectionRouteRepository lsSectionRouteRepository;

	@Autowired
	LineVersionsRepository lineVersionsRepository;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public long findLatestStationId() {
		return stationRepository.findLatestStationId();
	}

	/**
	 * 新增站点
	 * @param station
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void add(Station station) {
		centerPoint(station);
		stationRepository.save(station);
	}

	/**
	 * @Description :TODO(更新站点保存)
	 * 
	 * @param station
	 * 
	 * @return Map<String, Object> <SUCCESS ; ERROR>
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void modify(Station station) {
		centerPoint(station);
		Station station1 = stationRepository.findById(station.getId()).get();
		CustomBeanUtils.copyPropertiesIgnoredNull(station, station1);
		stationRepository.save(station1);
	}

	/**
	 * @Description :TODO(根据坐标点匹配数据库中的站点)
	 * 将匹配站点的名称赋值到新的站点
	 */
	@Override
	public void matchStation(List<Station> stations) {
		Map<String, Object> param = new HashMap<>();
		param.put("destroy_eq", 0);
		List<Station> stations1 = stationRepository.findAll(new CustomerSpecs<>(param));
		for (Station s : stations) {
			Location location = CoordinateConverter.LocationMake(s.getCenterPointWkt());
			Point point = new Point(location.getLng(), location.getLat());
			double matchDistance = 60;
			for (Station s1 : stations1) {
				Location location1 = CoordinateConverter.LocationMake(s1.getCenterPoint().toString());
				Point point1 = new Point(location1.getLng(), location1.getLat());
				double distance = GeoUtils.getDistance(point, point1);
				if (distance <= matchDistance) {
					s.setId(s1.getId());
					s.setStationCode(s1.getStationCode());
					s.setStationName(s1.getStationName());
					break;
				}
			}
		}
	}

	@Override
	public List<Station> findStationByName(String stationName) {
		String query = "SELECT id,station_code,station_name,ST_AsText(center_point) center_point_wkt,pass_lines,ew_direction,sn_direction FROM bsth_c_station WHERE station_name LIKE CONCAT(?,'%')";
		return jdbcTemplate.query(query, new Object[]{ stationName }, BeanPropertyRowMapper.newInstance(Station.class));
	}

	/**
	 * 存在wkt 则转换wkt为geo信息
	 * @param station
	 */
	private void centerPoint(Station station) {
		// 中心点坐标信息
		String wkt = station.getCenterPointWkt();
		if (!StringUtils.isEmpty(wkt)) {
			org.geolatte.geom.Point baidu = (org.geolatte.geom.Point) Wkt.fromWkt(wkt);
			org.geolatte.geom.Point wgs = GeoConverter.pointBd2wgs(wkt);
			station.setCenterPoint(baidu);
			station.setCenterPointWgs(wgs);
		}
	}
}