package com.sw.mes.agv.dao;

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

import java.util.List;

public interface MonitorEquipmentChargeDAO extends BaseDao<MonitorEquipmentCharge, Long> {

    @Query(value = "SELECT * FROM monitor_equipment_charge WHERE name = ?1 ORDER BY pre_sling_time ASC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findByName(String deviceNo);

    @Query(value = "SELECT * FROM monitor_equipment_charge WHERE sling_state != ?1 ORDER BY pre_sling_time ASC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findSlingStateNotEqual(String state);

    @Query(value = "SELECT * FROM monitor_equipment_charge WHERE sling_state = ?1 ORDER BY pre_sling_time  ", nativeQuery = true)
    List<MonitorEquipmentCharge> findBySlingState(String slingState);

    @Query(value = "SELECT name FROM monitor_equipment_charge WHERE lot_no = ?1  order BY id limit 1", nativeQuery = true)
    String getNameByLotNo(String lotNo);

    @Modifying
    @Transactional
    @Query(value = "UPDATE monitor_equipment_charge SET equipment_state=?2 WHERE name=?1", nativeQuery = true)
    void updateMonitor(String deviceNo, String state);

    @Query(value = "SELECT * FROM monitor_equipment_charge " +
            "WHERE name IN(?1) ORDER BY pre_sling_time DESC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findByEquipments(List<String> equipmentList);

    @Query(value = "SELECT * FROM monitor_equipment_charge " +
            "WHERE store_code IS NOT NULL AND name IN(?1) ORDER BY pre_sling_time ASC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findByEquipmentsAndStoreCodeIsNotNull(List<String> equipmentList);

    @Query(value = "SELECT * FROM monitor_equipment_charge " +
            "WHERE store_code IS NULL AND name IN(?1) ORDER BY pre_sling_time ASC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findByEquipmentsAndStoreCodeIsNull(List<String> equipmentList);

    @Query(value = "SELECT * FROM monitor_equipment_charge " +
            "WHERE name IN(?1) AND sling_state=?2 ORDER BY pre_sling_time ASC ", nativeQuery = true)
    List<MonitorEquipmentCharge> findByEquipmentsAndSlingState(List<String> equipmentList, String daidiaoliao);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM monitor_equipment_charge WHERE name=?1", nativeQuery = true)
    void deleteByName(String deviceName);
}