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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.turnstore.entity.TurnstoreOutItem;
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 TurnstoreOutItemDao extends BaseDao<TurnstoreOutItem, Integer> {
    @Modifying
    @Query(value = "DELETE FROM zhxx_turnstore_out_item WHERE out_id=?1",nativeQuery = true)
    void deleteByOutId(Integer id);

    @Query(value = "SELECT * FROM zhxx_turnstore_out_item WHERE out_id=?1",nativeQuery = true)
    List<TurnstoreOutItem> getItemsByOutId(Integer outId);

    @Query(value = "SELECT * FROM zhxx_turnstore_out_item WHERE order_no=?1",nativeQuery = true)
    List<TurnstoreOutItem> getItemsByOrderNo(String orderNo);

    @Modifying
    @Query(value = "DELETE FROM zhxx_turnstore_out_item WHERE section_no=?1 AND state ='待出库'",nativeQuery = true)
    void deleteBySectionNo(String sectionNo);

    @Query(value = "SELECT * FROM zhxx_turnstore_out_item WHERE section_no=?1 AND type=?2 AND state='已出库'",nativeQuery = true)
    List<TurnstoreOutItem> getItemsBySectionNoAndType(String sectionNo, String type);

    @Query(value = "SELECT * FROM zhxx_turnstore_out_item WHERE section_no=?1 AND type=?2 order by time desc limit 1",nativeQuery = true)
    TurnstoreOutItem getItems2SectionNoAndType(String sectionNo, String type);

    @Query(value = "SELECT * FROM zhxx_turnstore_out_item WHERE section_no=?1 AND state='已出库' order by time desc limit 1",nativeQuery = true)
    TurnstoreOutItem getItems2SectionNo(String sectionNo);

    List<TurnstoreOutItem> findByTypeAndStateAndSectionNoOrderByIdDesc(String type, String state, String sectionNo);

    List<TurnstoreOutItem> findBySectionNoOrderByIdDesc(String sectionNo);

}
