package com.izouma.wenlvju.repo;

import com.izouma.wenlvju.domain.Organization;
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;
import java.util.Optional;

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

    Optional<Organization> findByUserId(Long userId);

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

    Organization findByUscc(String uscc);

    Optional<Organization> findByAllianceId(Long allianceId);

    Optional<Organization> findByAllianceIdAndIdIsNot(Long allianceId, Long id);

    List<Organization> findAllByNameEquals(String name);

}