package com.ywt.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface SnsUserInfoRepository extends JpaRepository<SnsUserInfo, Integer> {

    /**
     * 根据openId获取同一用户的其他端的微信记录
     *
     * @param openId   openId
     * @param terminal {@link com.ywt.mg.domain.models.enums.TerminalEnum}
     * @return {@link SnsUserInfo}
     */
    @Query(value = "select u.* from sns_user_info u where u.user_id = (select s.user_id from sns_user_info s where s.open_id = ? and s.status = 1 and s.deleted = 0 limit 1) and u.terminal = ? and u.status = 1 and u.deleted = 0 limit 1;", nativeQuery = true)
    public SnsUserInfo findOneByOpenId(String openId, int terminal);

    SnsUserInfo findFirstByUserIdAndTerminalAndDeleted(int userId, int terminal, int deleted);

    @Query(value = "select * from sns_user_info where user_id = ? and terminal = ? and deleted = 0 limit 1;", nativeQuery = true)
    SnsUserInfo getSnsUserInfoByUserId(int userId, int terminal);
}