package cn.quantgroup.xyqb.risk.repository;

import cn.quantgroup.xyqb.risk.entity.CountDevice;
import cn.quantgroup.xyqb.risk.entity.LoginInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Async;

public interface LoginInfoRepository extends JpaRepository<LoginInfo, Long> {
    /**
     * 根据设备ID看有多少手机号
     *
     * @param deviceId 设备维度策略
     * @return
     */
    @Query(value = "select count(distinct phone_no) from  login_info where device_id=?1 and phone_no != ?2 and  created_at> DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -90 DAY)", nativeQuery = true)
    Long countByDeviceId(String deviceId, String phone);


    /**
     * 账号维度策略
     *
     * @param phone
     * @return
     */
//    @Query(value = "select count(1) ,device from  login_info where phone_no=?1 and  last_login_at> DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -90 DAY  group by device", nativeQuery = true)
    CountDevice countByPhoneAndDevice(String phone, int device,String deviceId);


    @Async
    @Override
    <S extends LoginInfo> S save(S s);

}
