StationRouteRepository.java 8.92 KB
package com.bsth.repository;

import java.util.List;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bsth.entity.Line;
import com.bsth.entity.LineInformation;
import com.bsth.entity.StationRoute;

/**
 * 
 * @Interface: StationRouteRepository(站点路由Repository数据持久层接口)
 * 
 * @Extends : BaseRepository
 * 
 * @Description: TODO(站点路由Repository数据持久层接口)
 * 
 * @Author bsth@lq
 * 
 * @Date 2016年5月03日 上午9:21:17
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */

@Repository
public interface StationRouteRepository extends BaseRepository<StationRoute, Integer> {
	
	@Query(value = "SELECT  a.`stationRoute.id`," +
						    "a.`stationRoute.line`," +
						    "a.`stationRoute.station`," +
							"a.`stationRoute.stationName`," +
							"a.`stationRoute.stationRouteCode`," +
							"a.`stationRoute.lineCode`," +
							"a.`stationRoute.stationMark`," +
							"a.`stationRoute.outStationNmber`," +
							"a.`stationRoute.directions`," +
							"a.`stationRoute.distances`," +
							"a.`stationRoute.toTime`," +
							"a.`stationRoute.firstTime`," +
							"a.`stationRoute.endTime`," +
							"a.`stationRoute.descriptions`," +
							"a.`stationRoute.versions`," +
							"b.id AS 'station.id'," +
							"b.station_cod AS 'station.stationCod'," +
							"b.station_name AS 'station.stationName'," +
							"b.road_coding AS 'station.roadCoding'," +
							"b.db_type AS 'station.dbType'," +
							"b.b_jwpoints AS 'station.bJwpoints'," +
							"b.g_lonx AS 'station.gLonx'," +
							"b.g_lonx AS 'station.gLaty'," +
							"b.x AS 'station.x'," +
							"b.y AS 'station.y'," +
							"b.shapes_type AS 'station.shapesType'," +
							"b.radius AS 'station.radius'," +
							"AsText(b.g_polygon_grid) AS 'station.gPolygonGrid'," +
							"AsText(b.b_polygon_grid) AS 'station.bPolygonGrid'," +
							"b.destroy AS 'station.destroy'," +
							"b.versions AS 'station.versions'," +
							"b.descriptions AS 'station.descriptions' FROM (" +
								"SELECT r.id AS 'stationRoute.id'," +
								" r.line AS 'stationRoute.line'," +
								"r.station AS 'stationRoute.station'," +
								"r.station_name AS 'stationRoute.stationName'," +
								"r.station_route_code as 'stationRoute.stationRouteCode'," +
								"r.line_code AS 'stationRoute.lineCode'," +
								"r.station_mark AS 'stationRoute.stationMark'," +
								"r.out_station_nmber AS 'stationRoute.outStationNmber'," +
								"r.directions AS 'stationRoute.directions'," +
								"r.distances AS 'stationRoute.distances'," +
								"r.to_time AS 'stationRoute.toTime'," +
								"r.first_time AS 'stationRoute.firstTime'," +
								"r.end_time AS 'stationRoute.endTime'," +
								"r.descriptions AS 'stationRoute.descriptions'," +
								"r.versions AS 'stationRoute.versions' FROM bsth_c_stationroute r  WHERE  r.line = ?1 and r.directions = ?2 and r.destroy=0) a " +
							"LEFT JOIN bsth_c_station b " +
							"ON a.`stationRoute.station` = b.id ORDER BY a.`stationRoute.stationRouteCode` ASC", nativeQuery=true)
	List<Object[]> findPoints(int line,int directions);
	
	
	/**
	 * @Description :TODO(查询线路某方向下的站点序号与类型)
	 * 
	 * @param map <lineId:线路ID; direction:方向;stationRouteCode:站点编码>
	 * 
	 * @return List<Map<String, Object>> 
	 */
	@Query(value = "select t.station_route_code,t.station_mark from bsth_c_stationroute t where " +
			" t.station_route_code =(" +
				"select  MAX(station_route_code) as stationRouteCode   from bsth_c_stationroute r  WHERE  " +
					"r.line=?1 and r.directions =?2 and station_route_code< ?3  ) and t.line=?1 and t.directions = ?2", nativeQuery=true)
	List<Object[]> findUpStationRouteCode(Integer lineId,Integer direction,Integer stationRouteCode);
	
	
	/**
	 * @Description :TODO(站点中心点坐标查询)
	 * 
	 * @param map <lineId:线路ID; direction:方向>
	 * 
	 * @return List<Object[]>
	 */
	@Query(value = "SELECT s.b_jwpoints FROM (" +
						"SELECT b.station FROM bsth_c_stationroute b where b.line =?1 and b.directions = ?2 and b.destroy=0) r " +
				   "LEFT JOIN bsth_c_station s on r.station = s.id", nativeQuery=true)
	List<Object[]> getSelectStationRouteCenterPoints(Integer lineId,Integer direction);
	
	/**
	 * @Description :TODO(撤销站点)
	 * 
	 * @param map <lineId:线路ID; destroy:是否撤销(0:否;1:是)>
	 * 
	 */
	@Transactional
    @Modifying
	@Query(value="UPDATE bsth_c_stationroute SET " +
				"destroy = ?2 WHERE id = ?1", nativeQuery=true)
	public void stationRouteIsDestroyUpd(Integer stationRouteId, Integer destroy);
	
	
	/**
	 * @Description : TODO(根据线路ID生成行单)
	 * 
	 * @param  lineId:线路ID 
	 * 
	 * @return List<Object[]>:{[0]:g_lonx(GPS经度);[1]:g_laty(GPS纬度);[2]:b_jwpoints(百度经纬度坐标)
	 * 							
	 * 							[3]:station_mark(站点类型);[4]:station_route_code(站点序号);[5]:station_cod(站点编码);
	 * 
	 * 							[6]:distances(站点距离);[7]:station_name(站点名称);[8]:directions(方向)}
	 */
	@Query(value = "SELECT * FROM ("+
				   "SELECT b.g_lonx," +
				   "b.g_laty,b.b_jwpoints," +
				   "a.station_mark," +
				   "a.station_route_code," +
				   "b.station_cod," +
				   "a.distances,"+
				   "b.station_name," +
				   "a.directions FROM (SELECT " +
				   "s.station_mark," +
				   "s.station_route_code," +
				   "s.directions," +
				   "s.distances,"+
				   "s.station FROM bsth_c_stationroute s where  s.line = ?1) a " +
				   "LEFT JOIN bsth_c_station b " +
				   " on a.station = b.id ORDER BY a.directions ASC ) k ORDER BY k.directions,k.station_route_code ASC", nativeQuery=true)
	List<Object[]> usingSingle(Integer lineId);
	
	/**
	 * @Description : TODO(根据站点路由Id查询详情)
	 * 
	 * @param  id:站点路由ID 
	 * 
	 * @return List<Object[]>
	 */
	@Query(value = "SELECT a.stationRouteLine," +
			" a.stationRouteStation," +
			" a.stationRouteCode," +
			" a.stationRouteLIneCode," +
			" a.stationRouteStationMark," +
			" a.stationOutStationNmber," +
			" a.stationRoutedirections," +
			" a.stationRouteDistances," +
			" a.stationRouteToTime," +
			" a.staitonRouteFirstTime," +
			" a.stationRouteEndTime," +
			" a.stationRouteDescriptions," +
			" a.stationRouteDestroy," +
			" a.stationRouteVersions," +
			" a.stationRouteCreateBy," +
			" a.stationRouteCreateDate," +
			" a.stationRouteUpdateBy," +
			" a.stationRouteUpdateDate," +
			" b.id AS stationId," +
			" b.station_cod AS stationCode," +
			" b.station_name AS stationName," +
			" b.road_coding AS stationRoadCoding," +
			" b.db_type AS stationDbType," +
			" b.b_jwpoints AS stationJwpoints," +
			" b.g_lonx AS stationGlonx," +
			" b.g_laty AS stationGlaty," +
			" b.x AS stationX," +
			" b.y AS stationY," +
			" AsText(b.b_polygon_grid) as stationBPolyonGrid," +
			" AsText(b.g_polygon_grid) AS stationGPloyonGrid, " +
			" b.destroy AS stationDestroy," +
			" b.radius AS stationRadius," +
			" b.shapes_type AS stationShapesType," +
			" b.versions AS stationVersions," +
			" b.descriptions AS sttationDescriptions," +
			" b.create_by AS stationCreateBy," +
			" b.create_date AS stationCreateDate," +
			" b.update_by AS stationUpdateBy," +
			" b.update_date AS stationUpdateDate," +
			" a.stationRouteId"+
			" FROM ( SELECT s.id AS stationRouteId," +
			" s.line AS stationRouteLine," +
			" s.station as stationRouteStation," +
			" s.station_route_code as stationRouteCode," +
			" s.line_code AS stationRouteLIneCode," +
			" s.station_mark AS stationRouteStationMark," +
			" s.out_station_nmber AS stationOutStationNmber," +
			" s.directions AS stationRoutedirections," +
			" s.distances AS stationRouteDistances," +
			" s.to_time AS stationRouteToTime," +
			" s.first_time AS staitonRouteFirstTime," +
			" s.end_time AS stationRouteEndTime," +
			" s.descriptions AS stationRouteDescriptions," +
			" s.destroy AS stationRouteDestroy," +
			" s.versions AS stationRouteVersions," +
			" s.create_by AS stationRouteCreateBy," +
			" s.create_date AS stationRouteCreateDate," +
			" s.update_by AS stationRouteUpdateBy," +
			" s.update_date AS stationRouteUpdateDate FROM bsth_c_stationroute s WHERE s.id = ?1 ) a " +
			" LEFT JOIN  bsth_c_station b ON a.stationRouteStation = b.id", nativeQuery=true)
	List<Object[]> findStationRouteInfo(Integer id);
	
	List<StationRoute> findByLine(Line line);
	
	@EntityGraph(value = "stationRoute_station", type = EntityGraph.EntityGraphType.FETCH)
	@Query("select s from StationRoute s where s.destroy=0")
	List<StationRoute> findAll2();
}