SectionRouteRepository.java 9.05 KB
package com.bsth.repository;

import java.util.List;

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.SectionRoute;

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

@Repository
public interface SectionRouteRepository extends BaseRepository<SectionRoute, Integer> {
	
	// 查询最大ID
	@Query(value = "SELECT IFNULL(num,0) as maxId from (SELECT MAX(id) as num FROM bsth_c_sectionroute) k" , nativeQuery=true)
	public long sectionRouteMaxId();
	
	// 查询最大ID
	@Query(value = "SELECT IFNULL(num,0) as maxId from (SELECT MAX(sectionroute_code) as num FROM bsth_c_sectionroute where line_code = 601010 and directions = 1 and destroy = 0) k" , nativeQuery=true)
	public int sectionRouteCodeMaxId();
	
	/**
	 * @Description :TODO(查询路段信息)
	 * 
	 * @param map <lineId:线路ID; directions:方向>
	 * 
	 * @return List<Object[]>
	 */
	@Query(value ="SELECT  a.sectionrouteId," +
			 "a.sectionrouteLine," +
			" a.sectionrouteLineCode," +
			" a.sectionrouteSection," +
			" a.sectionrouteSectionCode," +
			" a.sectionrouteCode," +
			" a.sectionrouteDirections," +
			" b.id AS sectionId," +
			" b.section_code AS sectionCode," +
			" b.section_name AS sectionName," +
			" b.croses_road AS sectionRoad," +
			" b.end_node AS sectionEndNode," +
			" b.start_node AS sectionStartNode," +
			" b.middle_node AS sectionMiddleNode," +
	        " b.section_type AS sectionType," +
			" b.csection_vector AS sectionCsectionVector," +
			" ST_AsText(b.bsection_vector) AS sectionBsectionVector," +
			" ST_AsText(b.gsection_vector) AS sectionGsectionVector," +
			" b.road_coding AS sectionRoadCoding," +
			" b.section_distance AS sectionDistance," +
            " b.section_time AS sectionTime," +
			" b.db_type AS sectiondbType," +
			" b.speed_limit AS sectionSpeedLimit ,a.destroy,a.versions,a.descriptions,a.isRoadeSpeed FROM (" +
						"SELECT  r.id AS sectionrouteId," +
								"r.line AS sectionrouteLine," +
								"r.line_code AS sectionrouteLineCode," +
								"r.section AS sectionrouteSection," +
								"r.section_code AS sectionrouteSectionCode," +
								"r.sectionroute_code AS sectionrouteCode," +
								"r.directions AS sectionrouteDirections," +
								"r.destroy AS destroy," +
								"r.versions AS versions," +
								"r.descriptions AS descriptions, r.is_roade_speed AS isRoadeSpeed" +
								"  FROM bsth_c_sectionroute r where r.line = ?1 and r.directions = ?2 and r.destroy=0 ) a " +
								"	LEFT JOIN bsth_c_section b  ON a.sectionrouteSection = b.id order by a.sectionrouteCode asc", nativeQuery=true)
	List<Object[]> getSectionRoute(int lineId, int directions);
	
	
	/**
	 * @Description :TODO(查询路段信息)
	 * 
	 * @param map <id:路段路由ID>
	 * 
	 * @return List<Object[]>
	 */
	@Query(value ="SELECT a.sectionRouteId," +
			"a.sectionRouteLineCode," +
			"a.sectionRouteCode," +
			"a.sectionRouteDirections," +
			"a.sectionRouteLine," +
			"a.sectionRouteSection," +
			"a.sectionRouteDescriptions," +
			"a.sectionRouteCreateBy," +
			"a.sectionRouteCreateDate," +
			"a.sectionRouteUpdateBy," +
			"a.sectionRouteUpdateDate," +
			"a.sectionRouteVersions," +
			"a.sectionRouteDestroy," +
			"b.id AS sectionId," +
			"b.section_code AS sectionCode," +
			"b.section_name AS sectionName," +
			"b.road_coding AS sectionRoadCoding," +
			"b.end_node AS sectionEndCode," +
			"b.start_node AS sectionStartNode," +
			"b.middle_node AS sectionMiddleNode," +
			"b.section_type AS sectionType," +
			"ST_AsText(b.csection_vector) AS sectionCsectionVector," +
			"ST_AsText(b.bsection_vector) AS sectionBsectionVector," +
			"ST_AsText(b.gsection_vector) AS sectionGsectionVector," +
			"b.section_distance AS sectionDistance," +
			"b.section_time AS sectionTime," +
			"b.db_type AS sectionDbtype," +
			"b.speed_limit AS sectionSpeedLimit," +
			"b.descriptions AS sectionDescriptions," +
			"b.create_by AS sectionCreateBy," +
			"b.create_date AS sectionCreateDate," +
			"b.update_by AS sectionUpdateBy," +
			"b.update_date AS sectionUpdateDate," +
			"b.versions AS sectionVersion , a.isRoadeSpeed  FROM (" +
			" SELECT s.id AS sectionRouteId," +
			"s.line_code AS sectionRouteLineCode," +
			"s.sectionroute_code AS sectionRouteCode," +
			"s.directions AS sectionRouteDirections," +
			"s.line AS sectionRouteLine," +
			"s.section AS sectionRouteSection," +
			"s.descriptions AS sectionRouteDescriptions," +
			"s.create_by AS sectionRouteCreateBy," +
			"s.create_date AS sectionRouteCreateDate," +
			"s.update_by AS sectionRouteUpdateBy," +
			"s.update_date AS sectionRouteUpdateDate," +
			"s.versions AS sectionRouteVersions," +
			"s.destroy AS sectionRouteDestroy,  s.is_roade_speed AS isRoadeSpeed " +
			" FROM  bsth_c_sectionroute s where s.id =?1) a " +
			" LEFT JOIN bsth_c_section b on a.sectionRouteSection = b.id", nativeQuery=true)
	List<Object[]> findSectionRouteInfoFormId(int id);
	
	
	/**
	 * @Description :TODO(查询线路某方向下的上一个路段序号)
	 * 
	 * @param map <lineId:线路ID; direction:方向;sectionRouteCode:路段编码>
	 * 
	 * @return List<Map<String, Object>> 
	 */
	@Query(value = " select  MAX(r.sectionroute_code) as sectionrouteCode   from bsth_c_sectionroute r  WHERE r.line=?1 and r.directions =?2 and r.sectionroute_code< ?3 and r.destroy = 0", nativeQuery=true)
	List<Object[]> findUpSectionRouteCode(Integer lineId,Integer direction,Integer stationRouteCode);
	
	/**
	 * @Description :TODO(获取路段路由信息)
	 * 
	 * @param lineId:线路ID;directions:方向
	 * 
	 * @return List<Map<String, Object>> {[0]:directions(方向);[1]:bsection_vector(路段坐标);[2]:speed_limit(限速);[3]:section_name(路段名称)}
	 */
	@Query(value =  "SELECT " +
					"c.directions," +
					"ST_AsText(s.gsection_vector) as gsection_vector," +
					"s.speed_limit," +
					"s.section_name " +
					" FROM bsth_c_sectionroute c " +
					" LEFT JOIN bsth_c_section s on c.section = s.id where c.line = ?1 and c.directions = ?2 and c.destroy = 0", nativeQuery=true)
	List<Object[]> sectionRouteVector(Integer lineId,Integer directions);
	
	@Transactional
    @Modifying 
	@Query(value="UPDATE bsth_c_sectionroute set is_roade_speed = ?1  where line= ?2 and directions=?3 ",nativeQuery = true)
	void isRoadSpeedUpd(Integer isR,Integer line,Integer directions);
	
	@Modifying
	@Query(value="UPDATE bsth_c_sectionroute set sectionroute_code = (sectionroute_code+1) where line = ?1  and directions = ?2 and sectionroute_code >=?3 and destroy = 0", nativeQuery=true)
	public void sectionUpdSectionRouteCode(Integer line,Integer dir,Integer routeCod);

	@Modifying
	@Query(value="UPDATE bsth_c_sectionroute set sectionroute_code = (sectionroute_code+1) where line_code = ?1  and directions = ?2 and sectionroute_code >=?3 and destroy = 0", nativeQuery=true)
	public void sectionUpdSectionRouteCode(String lineCode,Integer dir,int routeCod);

	@Modifying
	@Query(value="UPDATE bsth_c_sectionroute set destroy = 1 where id = ?1", nativeQuery=true)
	public void sectionRouteIsDestroyUpdBatch(Integer id);
	
	/**
     * 更新路线前撤销线路原有路段
     * 
     * @param line
     * @param dir
     */
    @Modifying
    @Query(value="UPDATE bsth_c_sectionroute  set destroy = 1 where line = ?1  and directions = ?2", nativeQuery=true)
    public void sectionRouteUpdDestroy(Integer line,Integer dir);
	
    @Modifying
    @Query(value="update bsth_c_sectionroute set directions = case directions when 1 then 0 when 0 then 1 end where line_code = ?1   ", nativeQuery=true)
	public void sectionRouteDir(Integer line);
    
    // 更具线路批量撤销
    @Modifying
    @Query(value="UPDATE SectionRoute sr set sr.destroy = 1 where sr.line.id = ?1  and sr.lineCode = ?2")
	public void batchUpdate(Integer lineId, String lineCode);

    // 批量删除
    @Modifying
    @Query(value="delete from SectionRoute sr where sr.line.id = ?1  and sr.lineCode = ?2")
	public void batchDelete(Integer lineId, String lineCode);

    @Query("select r from SectionRoute r where r.line.id=?1 and r.directions=?2 and r.destroy=0 order by r.sectionrouteCode")
	public List<SectionRoute> findByLine(Integer lineId, Integer dir);

	/**
	 * 删除相应线路和版本的站点路由信息
	 * @param lineId
	 * @param version
	 */
	@Modifying
	@Query(value="delete from SectionRoute sr where sr.line.id = ?1  and sr.versions = ?2")
	void deleteByLineAndVersion(Integer lineId, Integer version);

	/**
	 * 历史路由表变更之后从历史表批量更新当前版本至当前表
	 * @param lineId
	 * @param version
	 */
	@Modifying
	@Query(value="insert into bsth_c_sectionroute select * from bsth_c_ls_sectionroute where line = ?1 and versions = ?2", nativeQuery = true)
	void updateFromHistory(Integer lineId, Integer version);
}