package net.northking.cctp.se.repository;

import net.northking.cctp.se.plan.bean.EnginePlanInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface EnginePlanInfoRepository extends JpaRepository<EnginePlanInfo,String> {

    @Query(value = "select * from engine_plan_info where device_id = :deviceId and type = :type order by priority desc ,created_time asc"
            ,nativeQuery = true)
    List<EnginePlanInfo> getOneInfoByDeviceId(@Param("deviceId") String deviceId,@Param("type") String type);

    @Transactional
    @Modifying
    @Query(value="delete from engine_plan_info where batch_id = :batchId",nativeQuery = true)
    void deletePlanInfoByBatchId(@Param("batchId") String batchId);

    @Transactional
    @Modifying
    @Query(value = "delete from engine_plan_info where batch_id = :batchId and type = :type",nativeQuery = true)
    void removeEnginePlanByType(@Param("batchId") String batchId,@Param("type") String type);

    @Query(value = "select distinct device_id from engine_plan_info where type = :type"
            ,nativeQuery = true)
    List<String> getAllDeviceId(@Param("type") String type);

    @Query(value = "select * from engine_plan_info where 1 = 1"
            ,nativeQuery = true)
    List<EnginePlanInfo> selectAllPlanInfo();
    
}
