package cn.ysk.cashier.mybatis.mapper;

import cn.ysk.cashier.mybatis.entity.TbShopUserFlow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author GYJ
 */
public interface TbShopUserFlowMapper extends BaseMapper<TbShopUserFlow> {

    /**
     * 根据条件查询用户流水总金额
     *
     * @param shopId    店铺ID
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 用户流水总金额
     */
    @Select("<script>" +
            "  SELECT \n" +
            "    COALESCE(SUM(flow.total_amount), 0) AS chageTotal\n" +
            "FROM \n" +
            "    tb_shop_user AS a\n" +
            "    LEFT JOIN (\n" +
            "        SELECT shop_user_id, SUM(amount) AS total_amount \n" +
            "        FROM tb_shop_user_flow \n" +
            "        WHERE biz_code IN ('cashMemberIn','scanMemberIn', 'scanMemberAwardIn') \n" +
            "        AND create_time BETWEEN #{startTime} AND #{endTime} \n" +
            "        GROUP BY shop_user_id\n" +
            "    ) AS flow ON flow.shop_user_id = a.id\n" +
            "WHERE \n" +
            "    a.shop_id = #{shopId}" +
            "</script>")
    BigDecimal sumUserFlowAmountByConditions(@Param("shopId") Long shopId,
                                             @Param("startTime") String startTime,
                                             @Param("endTime") String endTime);


    @Select(value = "select * from tb_shop_user_flow where shop_user_id=#{userId} order by id desc limit #{page},#{size}")
    List<TbShopUserFlow> selectByUserId(@Param("userId") Integer userId,@Param("page") Integer page,@Param("size") Integer size);

    @Select(value = "select count(0) from tb_shop_user_flow where shop_user_id=#{userId} ")
    Integer selectCountByUserId(@Param("userId") Integer userId);


}
