package com.loan.system.repository;

import com.loan.system.domain.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
 * @author EdwinXu
 * @date 2020/9/2 - 15:35
 * @Description
 */
public interface UserRepository extends JpaRepository<User,Long> {
    User findByMobile(String mobile);

    Page<User> findByIsDelete(Boolean isDelete, Pageable pageable);

    @Query("select u from User u where u.id in ?1 and u.isDelete = ?2")
    User findByUserIdAndIsDelete(Long userId, Boolean isDelete);

    boolean existsByMobileAndIsDelete(String mobile,Boolean isDelete);

    User findByMobileAndIsDelete(String phoneNumber,Boolean isDelete);

    User findByIdAndIsDelete(Long mobile, Boolean isDelete);

    @Query("select u from User u where u.isDelete = ?1 and( u.role like '%LEAD_SALES%' or u.role like '%ASSIST_SALES%')")
    List<User> findAllSalesByIsDelete(boolean b);

    @Query("select u from User u where u.role like ?1 and u.isDelete = ?2")
    List<User> getUsersByRoleAndIsDelete(String role, boolean isDelete);

    @Transactional
    @Modifying
    @Query("update User u set u.isDelete = ?2 where u.id = ?1")
    void updateIsDeleteById(Long id, boolean isDelete);

    @Transactional
    @Modifying
    @Query("update User u set u.role = ?1 where u.id = ?2")
    void updateRoleById(String role,Long id);

    @Query("select u from User u where (u.role like %?1% or u.role = ?1) and u.isDelete = ?2")
    List<User> findByRoleLikeAndIsDelete(String role, Boolean isDelete);


}
