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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.turnstore.entity.TurnstoreIn;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface TurnstoreInDao extends BaseDao<TurnstoreIn, Integer> {
    @Query(value = "SELECT IFNULL(MAX(sort),0) AS maxSort FROM zhxx_turnstore_in WHERE date=?1", nativeQuery = true)
    Integer getMaxSortByDate(String date);

    @Query(value = "SELECT IFNULL(batch_no,0) AS maxBatchNo FROM zhxx_turnstore_in WHERE type='圆棒入库' AND batch_no IS NOT NULL ORDER BY id DESC LIMIT 1", nativeQuery = true)
    Long getMaxBatchNo();

    @Query(value = "SELECT * FROM zhxx_turnstore_in WHERE batch_no=?1", nativeQuery = true)
    List<TurnstoreIn> getItemsByBatchNo(Long batchNo);

    @Query(value = "SELECT COUNT(1) AS batchCount FROM zhxx_turnstore_in WHERE batch_no=?1", nativeQuery = true)
    Integer getCountByBatchNo(Long batchNo);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_data_wip_state SET `state`=?2, update_time=NOW() WHERE sn IN (?1) ", nativeQuery = true)
    void updateWipStateBySnIn(List<String> nos, String state);

}
