package com.ecep.contract.cloud.rk;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ecep.contract.ds.MyRepository;
import com.ecep.contract.model.CloudRk;
import com.ecep.contract.model.Company;

@Repository
public interface CloudRkRepository extends MyRepository<CloudRk, Integer> {

    Stream<CloudRk> findByCloudLatestAfter(Instant timestamp);

    Stream<CloudRk> findByCloudEntUpdateAfter(Instant timestamp);

    /**
     * 按公司查找 Cloud RK
     *
     * @param company 公司对象
     * @return Cloud RK
     */
    Optional<CloudRk> findByCompany(Company company);

    /**
     * 按公司查找 Cloud RK
     *
     * @param companyId 公司对象编号
     * @return Cloud RK
     */
    Optional<CloudRk> findByCompanyId(int companyId);

    List<CloudRk> findAllByCompanyId(int companyId);

    long countByLatestUpdateBefore(Instant instant);

    long countByAutoUpdateIsTrueAndLatestUpdateBefore(Instant instant);

    List<CloudRk> findTop100ByLatestUpdateBeforeOrderByLatestUpdateDesc(Instant instant);

    List<CloudRk> findTop100ByAutoUpdateIsTrueAndLatestUpdateBeforeOrderByLatestUpdateDesc(Instant instant);

    @Modifying
    @Transactional
    int deleteAllByCompany(Company company);
}
