package com.hai.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * mapper扩展类
 */
public interface HighCouponMapperExt {
    /**
     * 查询月销量
     * @param couponId
     * @return
     */
    @Select({"SELECT count(1) FROM `high_child_order` " +
            " where goods_type = 1 and goods_id = #{couponId} and child_order_status in (2,3,6,7) and DATE_FORMAT(pay_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')"})
    int getMonthlySales(@Param("couponId") Long couponId);


    @Select("<script>" +
            " select a.sales_price,sum(notUseCount) notUseCount,sum(a.useCount) useCount from " +
            "(select a.sales_price, (select count(1) FROM high_coupon_code_other WHERE cou_type_code = a.coupon_key and `status` = 20  <if test='param.salesTimeS != null'>  and create_time BETWEEN #{param.salesTimeS} and #{param.salesTimeE} </if> ) notUseCount, " +
            "(select count(1) FROM high_coupon_code_other WHERE cou_type_code = a.coupon_key and `status` = 40 <if test='param.salesTimeS != null'>  and create_time BETWEEN #{param.salesTimeS} and #{param.salesTimeE} </if> ) useCount " +
            " FROM high_coupon a" +
            " WHERE a.coupon_source = #{param.couponSource} GROUP BY coupon_key ORDER BY a.sales_price) a where 1 = 1 " +
            " <if test='param.salesPrice != null'> and a.sales_price = #{param.salesPrice} </if>" +
            " GROUP BY a.sales_price" +
            " </script>")
    List<Map<String, Object>> getUseCouponStatistics(@Param("param") Map<String, Object> param);

    @Select("select sales_price from high_coupon where coupon_source = #{couponSource} GROUP BY sales_price ORDER BY sales_price")
    List<BigDecimal> getCouponSalesPrice(@Param("couponSource") Integer couponSource);

    @Select(" select count(1) from high_child_order " +
            " where mem_id = #{userId} " +
            " and goods_type = 1 " +
            " and child_order_status in (2,3)" +
            " and pay_time BETWEEN CONCAT(CURDATE(),' 00:00:00') AND CONCAT(CURDATE(),' 23:59:59') " +
            " and goods_id in (select id from high_coupon where coupon_source = #{couponSource})")
    int userBuySourceLimitNumber(@Param("userId") Long userId, @Param("couponSource") Integer couponSource);
}
