package com.yeejoin.amos.patrol.business.dao.repository;

import com.yeejoin.amos.patrol.dao.entity.Plan;
import com.yeejoin.amos.patrol.dao.entity.PlanTask;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository("planDao")
public interface IPlanDao extends BaseDao<Plan, Long> {
    @Modifying
    @Transactional
    @Query(value = "delete from p_plan where id in (?1)", nativeQuery = true)
    void delPlanBatch(List<Long> ids);

    @Modifying
    @Transactional
    @Query(value = "select * from p_plan WHERE next_gen_date = ?1 and status = ?2", nativeQuery = true)
    List<Plan> queryScheduledPlan(String strDate, String status);

    @Modifying
    @Transactional
    @Query(value = "select * from p_plan WHERE route_id = ?1 and status = ?2", nativeQuery = true)
    List<Plan> queryPlanByRouteId(long routeId, String status);

    @Modifying
    @Transactional
    @Query(value = "UPDATE p_plan SET is_delete = 1,status = 1 WHERE id IN (?1)", nativeQuery = true)
    void updatePlanDel(List<Long> ids);

    Plan findByOriginalId(String originalId);

    @Query(value = "select * from p_plan where original_id in (?1) and is_delete = 0", nativeQuery = true)
    List<Plan> findByOriginalIds(List<String> originalIds);


    @Transactional
    @Query(value = "select * from p_plan where id = (?1) and is_delete = 0", nativeQuery = true)
    Optional<Plan> findById(Long id);

}