package com.izouma.jiashanxia.repo;

import com.izouma.jiashanxia.domain.Package;
import com.izouma.jiashanxia.dto.PackageDTO;
import com.izouma.jiashanxia.enums.PackageType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface PackageRepo extends JpaRepository<Package, Long>, JpaSpecificationExecutor<Package> {
    @Query("update Package t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

//    List<Package> findAllByTypeAndDelFalse(PackageType type);

    List<Package> findAllByAttractionsId(Long attractionsId);

    @Query("select t.id from Package t where t.attractionsId = ?1")
    List<Long> findAllIdByAttractionsId(Long attractionsId);

    @Query(nativeQuery = true, value = "select p.*,a.name attractionsName,c.name categoryName " +
            "from package p " +
            "left join attractions a on p.attractions_id = a.id " +
            "left join category c on c.id = p.category_id where p.id = ?1")
    PackageDTO findInformationById(Long id);

    @Query("update Package t set t.shareNum = t.shareNum + 1 where t.id = ?1")
    Package addShareNum(Long id);

    @Query(nativeQuery = true, value = "SELECT ifnull(max(sort + 1),1) FROM package")
    int nextSort();
}