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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.report.entity.BrokenBractBase;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

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

    @Modifying
    @Query(value = "update rpt_broken_bract_base set repeated_info = ?3 where device_no not null and device_no = ?1 and date_time BETWEEN DATE_SUB(?2,INTERVAL 30 minute) AND DATE_ADD(?2,INTERVAL 30 minute)", nativeQuery = true)
    int updateRepeatedInfo(String deviceNo, String dateTime , String repeatedInfo);

    @Query(value = "SELECT * FROM rpt_broken_bract_base\n" +
            "            WHERE line_break_type IS NOT NULL AND line_break_type != ''\n" +
            "              AND ((CURRENT_TIME() <= '08:30:00' AND date_time BETWEEN\n" +
            "                   CURDATE() - INTERVAL 1 DAY + INTERVAL '20:30' HOUR_MINUTE\n" +
            "                   AND CURDATE() + INTERVAL '8:30' HOUR_MINUTE)\n" +
            "                OR\n" +
            "                (CURRENT_TIME() <= '20:30:00' AND  date_time BETWEEN\n" +
            "                   CURDATE() + INTERVAL '8:30' HOUR_MINUTE\n" +
            "                   AND CURDATE() + INTERVAL '20:30' HOUR_MINUTE)\n" +
            "                 OR " +
            "                 (CURRENT_TIME() >'20:30:00'AND CURRENT_TIME() <'23:59:59' AND date_time BETWEEN\n" +
            "                    CURDATE() + INTERVAL '20:30' HOUR_MINUTE\n" +
            "    AND CURDATE() + INTERVAL 1 DAY + INTERVAL '8:30' HOUR_MINUTE))", nativeQuery = true)
    List<BrokenBractBase> findByLineBreakType();

    @Query(value = "SELECT count(lot_no) FROM rpt_broken_bract_base WHERE lot_no = ?1 AND add_time = ?2",nativeQuery = true)
    Integer findByLotNoAndAddTime(String lotNo, Integer addTime);

}
