package com.bizzan.bitrade.dao;

import com.bizzan.bitrade.entity.MemberDevice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.Optional;

public interface MemberDeviceDao extends JpaRepository<MemberDevice, Long> {

    Optional<MemberDevice> findByMemberIdAndDeviceFingerprint(Long memberId, String deviceFingerprint);

    @Modifying
    @Query("UPDATE MemberDevice d SET d.lastLoginTime = :now, d.lastLoginIp = :ip, " +
           "d.loginCount = d.loginCount + 1 " +
           "WHERE d.memberId = :memberId AND d.deviceFingerprint = :fingerprint")
    int updateLogin(@Param("memberId") Long memberId,
                    @Param("fingerprint") String fingerprint,
                    @Param("ip") String ip,
                    @Param("now") Date now);
}
