package com.hai.dao;

import com.hai.entity.HighDiscountAgentCode;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

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

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

    @Insert({
            "<script>",
            "insert into high_discount_agent_code (discount_agent_id, qr_code, ",
            "create_time, `status`, ",
            "ext_1, ext_2, ext_3)",
            "values " +
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{discountAgentId,jdbcType=BIGINT}, ",
            "#{qrCode,jdbcType=VARCHAR}, ",
            "#{createTime,jdbcType=TIMESTAMP},",
            "#{status,jdbcType=INTEGER}, ",
            "#{ext1,jdbcType=VARCHAR},",
            "#{ext2,jdbcType=VARCHAR}, ",
            "#{ext3,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    void insertListCode(@Param(value = "list") List<HighDiscountAgentCode> discountAgentCodeList);

    @Select({"<script>" +
            " SELECT" +
            " a.id," +
            " a.`status`," +
            " a.create_time createTime," +
            " c.discountId," +
            " c.discountName," +
            " b.receiveTime," +
            " b.useEndTime," +
            " b.useTime," +
            " b.userId," +
            " b.userName," +
            " insert(b.userPhone, 4, 4, '****') userPhone," +
            " b.storeName" +
            " FROM" +
            " high_discount_agent_code a" +
            " LEFT JOIN (" +
            " SELECT" +
            " du.discount_agent_code_id," +
            " du.create_time receiveTime," +
            " du.use_end_time useEndTime," +
            " du.use_time useTime," +
            " u.id userId," +
            " u.`name` userName," +
            " u.phone userPhone," +
            " c.store_name storeName" +
            " FROM" +
            " high_discount_user_rel du" +
            " LEFT JOIN high_user u ON u.id = du.user_id " +
            " LEFT JOIN (select a.mem_discount_id,b.store_name from (select b.id,a.mem_discount_id  from high_order a" +
            " left join high_child_order b on a.id = b.order_id " +
            " where a.mem_discount_id is not null " +
            " and a.order_status in (2,3,4,6,7)" +
            " and b.giveaway_type = 0) a,high_coupon_code b where a.id = b.child_order_id  GROUP BY a.mem_discount_id) c on c.mem_discount_id = du.id" +
            " GROUP BY" +
            " du.id " +
            " ) b ON b.discount_agent_code_id = a.id " +
            " LEFT JOIN (select " +
            " ca.id discountAgentCodeId," +
            " hd.id discountId," +
            " hd.discount_name discountName from high_discount_agent_code ca,high_discount_agent_rel cb,high_discount hd " +
            " where ca.discount_agent_id = cb.id and cb.discount_id = hd.id) c on c.discountAgentCodeId = a.id" +
            " WHERE" +
            " a.discount_agent_id IN (SELECT id FROM high_discount_agent_rel WHERE agent_id = #{param.agentId} and status = 1)" +
            " <if test='param.status != null'> and a.`status` = #{param.status} </if> " +
            " <if test='param.discountId != null'> and c.discountId = #{param.discountId} </if> " +
            " <if test='param.discountName != null'> and c.discountName like concat('%',#{param.discountName},'%') </if> " +
            " <if test='param.storeName != null'> and b.storeName like concat('%',#{param.storeName},'%') </if> " +
            " <if test='param.receiveTimeS != null'> <![CDATA[ and b.receiveTime >= #{param.receiveTimeS} ]]> </if> " +
            " <if test='param.receiveTimeE != null'> <![CDATA[ and b.receiveTime <= #{param.receiveTimeE} ]]> </if> " +
            " <if test='param.useTimeS != null'> <![CDATA[ and b.useTime >= #{param.useTimeS} ]]> </if> " +
            " <if test='param.useTimeE != null'> <![CDATA[ and b.useTime <= #{param.useTimeE} ]]> </if> " +
            " <if test='param.idS != null'> <![CDATA[ and a.id >= #{param.idS} ]]> </if> " +
            " <if test='param.idE != null'> <![CDATA[ and a.id <= #{param.idE} ]]> </if> " +
            "</script>"})
    List<Map<String,Object>> getDiscountUseConditionList(@Param("param") Map<String, Object> param);

    @Select(" select * from high_discount_agent_code where discount_agent_id = #{discountAgentId} and status = 1 limit #{num} " )
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.BIGINT, id=true),
            @Result(column="discount_agent_id", property="discountAgentId", jdbcType=JdbcType.BIGINT),
            @Result(column="qr_code", property="qrCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @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<HighDiscountAgentCode> getDiscountAgentCodByLimit(@Param("discountAgentId") Long discountAgentId ,@Param("num") Long num);
}
