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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.turnstore.entity.ProductionBindingTray;
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 ProductionBindingTrayDao extends BaseDao<ProductionBindingTray, Long> {

    List<ProductionBindingTray> findByTrayNo(String trayNo);

    @Query(value = "SELECT * FROM `production_binding_tray` WHERE tray_no =?1 and (crystal_no is not null and crystal_no != '')",nativeQuery = true)
    List<ProductionBindingTray> findBindingTrayByTrayNo(String trayNo);

    @Query(value = "SELECT * FROM `production_binding_tray` WHERE tray_no =?1 and (crystal_no is not null and crystal_no != '')", nativeQuery = true)
    List<ProductionBindingTray> findByTrayNoAndCrystalNo(String trayNo);


    @Query(value = "SELECT * FROM `production_binding_tray` WHERE tray_no =?1 and (crystal_no is not null and crystal_no != '') order by binding_time desc limit 1", nativeQuery = true)
    List<ProductionBindingTray> findLastOneByTrayNoAndCrystalNo(String trayNo);


    @Query(value = "SELECT crystal_no FROM `production_binding_tray` WHERE tray_no = ?1 and (crystal_no is not null or crystal_no != '' )", nativeQuery = true)
    List<String> findCrystalNoByTrayNo(String trayNo);

    @Query(value = "SELECT * FROM `production_binding_tray` WHERE tray_no = ?1 and (crystal_no is not null or crystal_no != '' )", nativeQuery = true)
    List<ProductionBindingTray> findCrystalNoByTrayNoV2(String trayNo);

    ProductionBindingTray getByTrayNoAndLocationNo(String pailNum, String locationNo);

    List<ProductionBindingTray> findByCrystalNo(String crystalNo);

    ProductionBindingTray findByTrayNoAndCrystalNo(String trayNo, String crystalNo);

    @Modifying
    @Query(value = "update `production_binding_tray` set crystal_no = ?2 where crystal_no = ?1", nativeQuery = true)
    void updateByCrystalNo(String crystalNoOld, String crystalNoNew);
}
