package com.bizzan.bitrade.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;

import com.bizzan.bitrade.entity.MemberDeposit;

import java.math.BigDecimal;
import java.util.List;

public interface MemberDepositDao extends JpaRepository<MemberDeposit,Long>,QueryDslPredicateExecutor<MemberDeposit>{
    MemberDeposit findByAddressAndTxid(String address,String txid);
    MemberDeposit findByTxid(String txid);
    
    @Query(value="select unit ,sum(amount) from member_deposit where date_format(create_time,'%Y-%m-%d') = :date group by unit",nativeQuery = true)
    List<Object[]> getDepositStatistics(@Param("date")String date);

    @Query("select max(a.id) as id from MemberDeposit a")
    Long getMaxId();
    
    @Query("select max(a.amount) as id from MemberDeposit a where a.memberId=:memberId")
    BigDecimal getMaxAmountByMemberId(@Param("memberId")Long memberId);
    
    @Query(value="select * from member_deposit a where a.member_id=:memberId order by a.create_time desc limit 1",nativeQuery = true)
    MemberDeposit findLatestByMemberId(@Param("memberId")Long memberId);
    
    @Query(value="select member_id, sum(amount<1000) as small_count, sum(if(amount<1000, amount, 0)) as small_amount from member_deposit where create_time between :startTime and :endTime group by member_id having small_count >= 10 and small_amount >= 10000",nativeQuery = true)
	Object[][] statControl(@Param("startTime")String startTime, @Param("endTime")String endTime);
    
    @Query(value="select d.member_id, TIMESTAMPDIFF(MINUTE, d.create_time, t.create_time), d.amount, t.target_member_id from member_deposit d inner join transfer_record t on d.id=t.deposit_id and d.amount=t.amount and TIMESTAMPDIFF(MINUTE, d.create_time, t.create_time) < 10 where d.create_time between :startTime and :endTime",nativeQuery = true)
	Object[][] statTransferControl(@Param("startTime")String startTime, @Param("endTime")String endTime);
}
