package com.hai.dao;

import com.hai.entity.HighCouponAgentCode;
import com.hai.model.AgentSalesModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;


/**
 * mapper扩展类
 */
public interface HighCouponAgentRelMapperExt {


    @Select(" select case when sum(1) is not null then sum(1) else 0 end count," +
            " case when sum(c.sales_price) is not null then sum(c.sales_price) else 0 end salesCountPrice " +
            " from high_coupon_agent_code a,high_coupon_code b,high_coupon_agent_rel c" +
            " where c.id = a.coupon_agent_id and c.agent_id = #{agentId}" +
            " and a.status in (2,3) and a.coupon_code_id = b.id and a.sales_time >= #{consumeTimeS} and a.sales_time <= #{consumeTimeE}")
    AgentSalesModel getAgentSales(@Param("agentId") Long agentId, @Param("consumeTimeS")String consumeTimeS, @Param("consumeTimeE")String consumeTimeE);

    @Select(" select a.* from high_coupon_agent_code a" +
            " LEFT JOIN high_coupon_agent_rel c on c.id = a.coupon_agent_id " +
            " where c.id = a.coupon_agent_id and c.agent_id = #{agentId} and a.status in (2,3) " +
            " and a.sales_time >= #{consumeTimeS} and a.sales_time <= #{consumeTimeE} ORDER BY a.sales_time desc")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="coupon_agent_id", property="couponAgentId", jdbcType=JdbcType.BIGINT),
            @Result(column="coupon_id", property="couponId", jdbcType=JdbcType.BIGINT),
            @Result(column="agent_id", property="agentId", jdbcType=JdbcType.BIGINT),
            @Result(column="coupon_agent_record_id", property="couponAgentRecordId", jdbcType=JdbcType.BIGINT),
            @Result(column="convert_user_phone", property="convertUserPhone", jdbcType=JdbcType.VARCHAR),
            @Result(column="convert_code", property="convertCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="coupon_code_id", property="couponCodeId", jdbcType=JdbcType.BIGINT),
            @Result(column="coupon_code", property="couponCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="qr_code", property="qrCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="assign_coupon_agent_id", property="assignCouponAgentId", jdbcType=JdbcType.BIGINT),
            @Result(column="assign_agent_id", property="assignAgentId", jdbcType=JdbcType.BIGINT),
            @Result(column="assign_agent_name", property="assignAgentName", jdbcType=JdbcType.VARCHAR),
            @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="sales_time", property="salesTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="operator_id", property="operatorId", jdbcType=JdbcType.BIGINT),
            @Result(column="operator_name", property="operatorName", jdbcType=JdbcType.VARCHAR),
            @Result(column="ext_1", property="ext1", jdbcType=JdbcType.VARCHAR),
            @Result(column="ext_2", property="ext2", jdbcType=JdbcType.VARCHAR),
            @Result(column="ext_3", property="ext3", jdbcType=JdbcType.VARCHAR)
    })
    List<HighCouponAgentCode> getAgentSalesCodeList(@Param("agentId") Long agentId, @Param("consumeTimeS")String consumeTimeS, @Param("consumeTimeE")String consumeTimeE);

    @Select(" select count(1) from high_coupon_agent_code a where a.agent_id = #{agentId} and a.`status` in (${status}) and a.type = #{type} ")
    Integer getAgentCodeCount(@Param("agentId") Long agentId,@Param("status") String status,@Param("type") Integer type);

    @Select(" select " +
            " case WHEN sum(b.sales_price) is null then 0 else sum(b.sales_price) end" +
            " from high_coupon_agent_code a " +
            " LEFT JOIN high_coupon_agent_rel b on b.id = a.coupon_agent_id " +
            " where a.agent_id = #{agentId} and a.`status` in (${status}) and a.type = #{type} ")
    Integer getAgentCodePrice(@Param("agentId") Long agentId,@Param("status") String status,@Param("type") Integer type);

    @Select(" select " +
            " a.id," +
            " a.noUsedNum, " +
            " (a.noUsedNum * a.price) noUsedPrice," +
            " b.usedNum," +
            " (b.usedNum * b.price) usedPrice" +
            " from (" +
            " select " +
            " b.id," +
            " count(1) noUsedNum," +
            " b.sales_price price" +
            " from high_coupon_agent_code a " +
            " LEFT JOIN high_coupon_agent_rel b on b.id = a.coupon_agent_id" +
            " where b.id = #{couponAgentRelId} and a.`status` = 2) a," +
            " (select " +
            " b.id," +
            " count(1) usedNum," +
            " b.sales_price price" +
            " from high_coupon_agent_code a" +
            " LEFT JOIN high_coupon_agent_rel b on b.id = a.coupon_agent_id" +
            " where b.id = #{couponAgentRelId} and a.`status` = 3) b" +
            " where a.id = b.id ")
    Map<String, Object> getUseNum(@Param("couponAgentRelId") Long couponAgentRelId);
}
