CarDeviceRepository.java 1.54 KB
package com.bsth.repository;

import com.bsth.entity.CarDevice;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * Created by xu on 16/6/15.
 */
@Repository
public interface CarDeviceRepository extends BaseRepository<CarDevice, Long> {
	@Query(value="select s from CarDevice s where s.newDeviceNo=?1 and s.qyrq<=?2 ")
	List<CarDevice> findCarDevice(String device,Date date );
	
	@Query(value="select s from CarDevice s where s.oldDeviceNo=?1 and s.qyrq>=?2 ")
	List<CarDevice> findCarOldDevice(String device,Date date );
	
	@Query(value="select s from CarDevice s where s.clZbh=?1 and s.qyrq>=?2 ")
	List<CarDevice> findCarCode(String code,Date date );
	
	/**
	 * 查询车编号关联的历史设备号
	 * 
	 * @return 车辆自编号、牌照号、设备编号、设备编号的启用时间/停用时间、on启用/off停用
	 */
	@Query(value="select * from( "
				+"select * from(select c.inside_code, c.car_plate, d.new_device_no device_no, d.qyrq, 'on' state "
				+"from bsth_c_cars c left join bsth_c_car_device d on c.inside_code = d.cl_zbh "
				+"where qyrq is not null)a "
				+"UNION "
				+"select * from(select c.inside_code, c.car_plate, d.old_device_no device_no, d.qyrq, 'off' state "
				+"from bsth_c_cars c left join bsth_c_car_device d on c.inside_code = d.cl_zbh "
				+"where qyrq is not null)b "
				+")c order by device_no, qyrq, state ",nativeQuery=true)
	List<Object[]> selectCarHistoryDeviceNo();
	
}