package com.td.boss.game.comcntorder.repository;

import com.td.boss.common.repository.*;
import com.td.boss.game.comcntorder.pojo.ComCntOrder;
import com.td.boss.game.comcntorder.vo.ComCntOrderPayTypeSumVo;
import com.td.boss.game.comcntorder.vo.ComCntOrderVo;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ComCntOrderRepository extends CommonRepository<ComCntOrder, String> {

    Optional<ComCntOrder> findByTxHash(String tx);

    List<ComCntOrder> findByUserIdAndPayType(String userId,Integer payType);

    //SELECT pay_type,sum(pay_amount) from com_cnt_order WHERE pay_type = 4

    /**
     * 返回一个降序的pay_type 统计
     *
     * @return
     */
    @Query(value = "SELECT new com.td.boss.game.comcntorder.vo.ComCntOrderPayTypeSumVo(u.payType,sum(u.payAmount)) from ComCntOrder u GROUP BY u.payType ORDER BY sum(u.payAmount) desc", nativeQuery = false)
    List<ComCntOrderPayTypeSumVo> findCNTSumByPayType();


    /**
     * 根据开始结束时间获取payType 统计数据
     * @param startTime
     * @param endTime
     * @return
     */
    @Query(value = "SELECT new com.td.boss.game.comcntorder.vo.ComCntOrderPayTypeSumVo(u.payType,sum(u.payAmount)) FROM  ComCntOrder u  where u.createTime >= :startTime and u.createTime<= :endTime GROUP BY u.payType ORDER BY sum(u.payAmount) desc")
    List<ComCntOrderPayTypeSumVo> findCNTSumByPayTypeFromDate(Date startTime, Date endTime);

}
