package org.holer.server.db;

import java.util.List;
import org.holer.server.constant.HolerType;
import org.holer.server.model.HolerPort;
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.transaction.annotation.Transactional;

public interface DBPortService extends JpaRepository<HolerPort, Long> {
  List<HolerPort> findByAccessKey(String paramString);

  //查询端口是否被使用
  List<HolerPort> findByPortNum(Integer paramInteger);
  //查询域名是否被使用
  List<HolerPort> findByDomain(String paramString);

  void deleteByAccessKey(String paramString);

  Long countAllByType(HolerType paramHolerType);
  
  Long countAllByEnabled(Boolean paramBoolean);
  
  @Query(value = "select count(*) as count from holer_port where unix_timestamp(expire_at) <= unix_timestamp(now())", nativeQuery = true)
  Long countExpiredPort();
  
  @Query(value = "select count(*) as count from holer_port where unix_timestamp(expire_at) > unix_timestamp(now()) and enabled=true", nativeQuery = true)
  Long countActivePort();


  //根据id更新秘钥信息
  @Transactional
  @Modifying
  @Query(value = "update holer_port set access_key= :accessKey WHERE port_id= :id",nativeQuery = true)
  void updateAccessKey(@Param("accessKey") String accessKey, @Param("id") Long id);
}
