package com.bizzan.bitrade.dao;

import com.bizzan.bitrade.dao.base.BaseDao;
import com.bizzan.bitrade.entity.MemberWeightUpper;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public interface MemberWeightUpperDao extends BaseDao<MemberWeightUpper> {

    @Query(value = "select * from member_weight_upper where member_id in (:ids) order by member_id desc",nativeQuery = true)
    List<MemberWeightUpper> findAllByUpperIds(@Param("ids") List<Long> ids);
    
    @Query(value = "select * from member_weight_upper where upper =:upper order by member_id desc",nativeQuery = true)
    List<MemberWeightUpper> findAllByUpper(@Param("upper") String upper);
    
    @Query(value = "select * from member_weight_upper where all_uppers like CONCAT('%',:upper,'%') order by member_id desc",nativeQuery = true)
    List<MemberWeightUpper> findAllByAllUppers(@Param("upper") String upper);
    
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query(value = "UPDATE member_weight_upper SET rate=0,wc_rate=0 WHERE member_id in (:ids)",nativeQuery = true)
    void cancelSuperPartner(@Param("ids") List<Long> ids);
    
    @Query("select count(memberId) as total from MemberWeightUpper a where a.createTime between :startDate and :endDate and a.memberId in (:ids)")
    int countRegisteredMembersByMemberIdsAndTime(@Param("startDate") Date startDate, @Param("endDate") Date endDate,@Param("ids") List<Long> ids);
    
}
