package cn.ysk.cashier.repository;

import javax.persistence.Tuple;
import cn.ysk.cashier.pojo.TbShopPayType;
import cn.ysk.cashier.pojo.TbToken;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

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

/**
 * @author lyf
 */
public interface TbTokenRepository extends JpaRepository<TbToken, Integer>, JpaSpecificationExecutor<TbToken> {
    @Query("select count(0) from TbToken token where token.accountId=:shopId")
    Tuple countByAccountId(@Param("shopId") Integer shopId);

    @Query("select count(0) from TbToken token where token.accountId=:shopId " +
            "and token.createTime BETWEEN :startTime and :endTime")
    Tuple countByAccountId(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Query(value = "select count(0),DATE_FORMAT(create_time,'%Y-%m-%d')from tb_token where account_id=:shopId " +
            "and create_time BETWEEN :startTime and :endTime group by DATE_FORMAT( create_time, '%Y-%m-%d') ",nativeQuery = true)
    List<Object[]> countByMonth(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

}
