package com.izouma.nineth.repo;

import com.izouma.nineth.domain.CollectionPrivilege;
import org.springframework.cache.annotation.CacheEvict;
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.annotation.Nonnull;
import javax.transaction.Transactional;

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

    CollectionPrivilege findByCollectionId(Long collectionId);

    @Transactional
    @Modifying
    @Query(value = "update collection_privilege c set c.collection_id = ?2, c.head_bg = ?3, c.max_collection = ?4, " +
            "c.showroom_bg = ?5, c.vip = ?6 where c.id = ?1", nativeQuery = true)
    @CacheEvict(value = {"collectionInfo"}, allEntries = true)
    void update(@Nonnull Long id, Long collectionId, String headBg, Integer maxCollection, String showroomBg, boolean vip);

    @Transactional
    @Modifying
    @CacheEvict(value = {"collectionInfo"}, allEntries = true)
    CollectionPrivilege save(CollectionPrivilege record);

    CollectionPrivilege findByCompanyCollectionId(Long collectionId);
}