package com.sw.mes.fb.turnstore.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.fb.turnstore.entity.FbTurnstoreTrayItem;
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 FbTurnstoreTrayItemDao extends BaseDao<FbTurnstoreTrayItem, Integer> {


    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE tray_id in ?1 GROUP BY tray_id", nativeQuery = true)
    List<FbTurnstoreTrayItem> getItemsByAllTrayId(List<Integer> trayIdList);


    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 AND (state =0 or state =1)", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNo(String crystalNo);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNo1(String crystalNo);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 and state <> 3", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNo2(String crystalNo);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE tray_id=?1", nativeQuery = true)
    List<FbTurnstoreTrayItem> getItemsByTrayId(Integer trayId);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE tray_id=?1 order by order_num", nativeQuery = true)
    List<FbTurnstoreTrayItem> getItemsByTrayIdByOrder(Integer trayId);


    @Modifying
    @Query(value = "DELETE FROM  fb_turnstore_tray_item WHERE tray_id=?1", nativeQuery = true)
    void deleteByTrayId(Integer id);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 AND state=?2", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNoAndState(String crystalNo, int state);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE tray_no=?1 AND section_no=?2 ", nativeQuery = true)
    List<FbTurnstoreTrayItem> findByTrayNoAndSectionNo(String trayNo, String sectionNo);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 AND (state= 9 or has_gone='退料')", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNoValidate(String sectionNo);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1", nativeQuery = true)
    List<FbTurnstoreTrayItem> findBySectionNoValidateAll(String sectionNo);

    @Query(value = "SELECT * FROM fb_turnstore_tray_item WHERE tray_no = ?1", nativeQuery = true)
    List<FbTurnstoreTrayItem> getData(String trayNos);

    @Query(value = "SELECT * FROM fb_turnstore_tray_item WHERE tray_id = ?1", nativeQuery = true)
    List<FbTurnstoreTrayItem> findItemsByTrayId(Integer id);

    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 order by id desc limit 1", nativeQuery = true)
    FbTurnstoreTrayItem findBySectionNo3(String crystalNo);
    @Query(value = "SELECT * FROM  fb_turnstore_tray_item WHERE section_no=?1 and return_inspection_status = ?2 order by id desc limit 1", nativeQuery = true)
    FbTurnstoreTrayItem findBySectionNoAndReturnInspectionStatus(String sectionNo, int returnInspectionStatus);
}
