package com.bizzan.bitrade.dao;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.bizzan.bitrade.constant.WithdrawStatus;
import com.bizzan.bitrade.dao.base.BaseDao;
import com.bizzan.bitrade.entity.WithdrawRecord;

import java.util.List;

/**
 * @author 
 * @date 2020年01月29日
 */
public interface WithdrawRecordDao extends BaseDao<WithdrawRecord> {

    @Query(value = "select a.unit , sum(b.total_amount) amount,sum(b.fee) from withdraw_record b,coin a where a.name = b.coin_id and date_format(b.deal_time,'%Y-%m-%d') = :date and b.status = 3 group by a.unit",nativeQuery = true)
    List<Object[]> getWithdrawStatistics(@Param("date")String date);

    long countAllByStatus(WithdrawStatus status);

    WithdrawRecord findWithdrawRecordByTransactionNumber(String transactionNumber);
    
    @Query(value = "select count(*) from withdraw_record where status in (0,1) and member_id=:memberId",nativeQuery = true)
    int countProcessingByMemberId(@Param("memberId")Long memberId);
    
    @Query(value="select member_id, sum(total_amount<5000) as small_count, sum(if(total_amount<5000, total_amount, 0)) as small_amount from withdraw_record where create_time between :startTime and :endTime group by member_id having small_count >= 15",nativeQuery = true)
	Object[][] statControl(@Param("startTime")String startTime, @Param("endTime")String endTime);
    
    @Query(value="select member_id, count(distinct address) as address_count from withdraw_record where create_time between :startTime and :endTime group by member_id having address_count >= 5",nativeQuery = true)
	Object[][] statAddressControl(@Param("startTime")String startTime, @Param("endTime")String endTime);
    
    @Query(value="select member_id, (select count(*) from transfer_record where target_member_id=w.member_id and create_time<w.create_time and create_time between :startTime and :endTime) as transfer_count from withdraw_record w where create_time between :startTime and :endTime and (select count(*) from transfer_record where target_member_id=w.member_id and create_time<w.create_time and create_time between :startTime and :endTime)>=5",nativeQuery = true)
	Object[][] statTransferControl(@Param("startTime")String startTime, @Param("endTime")String endTime);
}
