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

import com.td.WaDa.common.repository.CommonRepository;
import com.td.WaDa.game.complayergoods.pojo.ComPlayerGoods;
import com.td.WaDa.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ComPlayerGoodsRepository extends CommonRepository<ComPlayerGoods, String> {

    Optional<ComPlayerGoods> findByUserIdAndGoodsIndexAndGoodsType(String userId, Integer index, Integer type);


    List<ComPlayerGoods> findAllByUserId(String userId);

    Optional<ComPlayerGoods> findFirstByUserIdOrderByCreateTimeDesc(String userId);

    /**
     * 返回一个降序的goods_type统计
     *
     * @return
     */
    @Query(value = "SELECT new com.td.WaDa.game.complayergoods.vo.ComPlayerGoodsTypeSumVo(u.goodsType,sum(u.amount)) from ComPlayerGoods u GROUP BY u.goodsType ORDER BY sum(u.amount) desc", nativeQuery = false)
    List<ComPlayerGoodsTypeSumVo> findAmountSumByGoodsType();
}
