package com.sw.mes.yb.production.dao;

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

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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.ProcessWorkingHours;

/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface ProcessWorkingHoursDao extends BaseDao<ProcessWorkingHours, Long>{

	List<ProcessWorkingHours> findByEquipmentCodeAndLotNo(String deviceCode, String lotNo);

	ProcessWorkingHours findTopByEquipmentCodeAndLotNoAndStartTimeAndEndTimeOrderByIdDesc(String deviceCode, String lotNo, Date startTime, Date endTime);

	@Query(value = "select * from production_process_working_hours where equipment_code = ?1 and lot_no is null", nativeQuery = true)
	List<ProcessWorkingHours> findByEquipmentCodeAndLotNoIsNull(String deviceCode);

	@Query(value = "select * from production_process_working_hours where equipment_code = ?1 order by id limit 1", nativeQuery = true)
	ProcessWorkingHours getByEquipmentCodeLast(String equipmentCode);

	@Query(value = "select * from production_process_working_hours where series = ?1 and step_name = ?2 and end_time >= ?3 and end_time < ?4", nativeQuery = true)
	List<ProcessWorkingHours> getBySeriesAndStepNameAndStartTime(String series, String workHourName, String startTime, String endTime);

	@Query(value = "select * from production_process_working_hours where end_time >= ?1 and end_time < ?2", nativeQuery = true)
	List<ProcessWorkingHours> getByStartTime(String startTime, String endTime);

	ProcessWorkingHours findTopByLotNoAndStepNameOrderByIdDesc(String lotNo, String step);
	@Query(value = "select * from production_process_working_hours where step_name = ?1 and end_time >=DATE_SUB(NOW(), INTERVAL 24 HOUR)", nativeQuery = true)
    List<ProcessWorkingHours> findByStepName(String stepName);
}
