package com.izouma.jmrh.repo;

import com.izouma.jmrh.domain.User;
import com.izouma.jmrh.security.Authority;
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;

public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    @Query
    User findByUsername(String username);

    List<User> findAllByAuthoritiesContains(Authority authority);

    User findByOpenId(String openId);

    User findByPhone(String phone);

    @Query(value = "update user set password = :#{#user.password},username = :#{#user.phone} where phone = :#{#user.phone}", nativeQuery = true)
    @Modifying
    @Transactional
    Integer updatePassword(User user);

    @Query(value = "update user set org_info_id=:#{#user.orgInfo.id} where id = :#{#user.id}", nativeQuery = true)
    @Modifying
    @Transactional
    Integer updateUser(User user);

    @Query(value = "update user set phone=?1 where id = ?2", nativeQuery = true)
    @Modifying
    @Transactional
    Integer updateUserPhone(String phone, Long id);

    User findByEmail(String mail);
}
