package com.td.WaDa.game.comsnbtran.repository;

import com.td.WaDa.common.repository.*;
import com.td.WaDa.game.comsnbtran.pojo.ComSnbTran;
import com.td.WaDa.game.comsnbtran.vo.ComSnbTranAbnormalVo;
import com.td.WaDa.game.comsnbtran.vo.ComSnbTranTranTypeSumVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ComSnbTranRepository extends CommonRepository<ComSnbTran, String> {

    Page<ComSnbTran> findAllByUserId(String userId, Pageable pageable);


    /**
     * 返回一个降序的pay_type 统计
     * @return
     */
    @Query(value = "SELECT new com.td.WaDa.game.comsnbtran.vo.ComSnbTranTranTypeSumVo(u.tranType,sum(u.tranSnb+u.tranSnbPart)) from ComSnbTran u GROUP BY u.tranType ORDER BY sum(u.tranSnb + u.tranSnbPart) desc", nativeQuery = false)
    List<ComSnbTranTranTypeSumVo> findSnbSumByTranType();


    @Query(value = "SELECT new com.td.WaDa.game.comsnbtran.vo.ComSnbTranAbnormalVo(u.userId,max(u.tranPrice),u.createTime) from ComSnbTran  as u WHERE u.tranPrice <0 GROUP BY u.tranPrice", nativeQuery = false)
    List<ComSnbTranAbnormalVo> findSnbAbnormalData();
    /**
     * 根据开始结束时间获取payType 统计数据
     * @param startTime
     * @param endTime
     * @return
     */
    @Query(value = "SELECT new com.td.WaDa.game.comsnbtran.vo.ComSnbTranTranTypeSumVo(u.tranType,sum(u.tranSnb+u.tranSnbPart)) FROM  ComSnbTran u  where u.createTime >= :startTime and u.createTime<= :endTime GROUP BY u.tranType ORDER BY sum(u.tranSnb+u.tranSnbPart) desc")
    List<ComSnbTranTranTypeSumVo> findCNTSumByPayTypeFromDate(Date startTime, Date endTime);
}
