package com.yn.bftl.thirdparty.common.repository;

import com.yn.bftl.common.modules.company.entity.BrandAuthorization;
import com.yn.bftl.common.modules.company.entity.Company;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;

public interface BrandAuthorizationRepository extends JpaRepository<BrandAuthorization, Long> {
    
    @Query(nativeQuery = true, value = "SELECT * FROM `company_brand_authorization` WHERE brand = ?1 AND company = ?2 AND authorization_start_time < ?3 AND authorization_end_time > ?3 AND is_valid = 1 LIMIT 1")
    BrandAuthorization getBrandAuthorization(Integer brandId, Integer compangId, LocalDateTime startTime);

    @Query("select ba from BrandAuthorization ba left join Brand b on ba.brand.id = b.id " +
            " where ba.company = ?2 and ba.isValid = true and b.company = ?1 and b.isBrandProportion = true")
    BrandAuthorization findByPartyAAndPartyB(Company partyA, Company partyB);
}