package com.hai.dao;

import com.hai.entity.HighCouponCode;
import com.hai.model.GzSinopecModel;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

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

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

    @Insert({
            "<script>",
            "insert into high_coupon_code (" +
            "coupon_id, " +
            "merchant_id, ",
            "is_assign_agent," +
            "agent_id, ",
            "store_id, " +
            "child_order_id," +
            "code_key, " +
            "sales_code," +
            "sales_end_time," +
            "use_end_time, ",
            "create_time," +
            "receive_time, ",
            "consume_time, " +
            "`status`, ",
            "operator_id, " +
            "operator_name, ",
            "ext_1, ext_2, ext_3)",
            "values ",
            "<foreach collection='list' item='item' index='index' separator=','>",
                "(#{item.couponId,jdbcType=BIGINT}, " +
                "#{item.merchantId,jdbcType=BIGINT}," +
                "#{item.isAssignAgent,jdbcType=BIT}," +
                "#{item.agentId,jdbcType=BIGINT}," +
                "#{item.storeId,jdbcType=BIGINT}," +
                "#{item.childOrderId,jdbcType=BIGINT}, " +
                "#{item.codeKey,jdbcType=VARCHAR}, " +
                "#{item.salesCode,jdbcType=VARCHAR}, ",
                "#{item.salesEndTime,jdbcType=TIMESTAMP}," +
                "#{item.useEndTime,jdbcType=TIMESTAMP}, ",
                "#{item.createTime,jdbcType=TIMESTAMP}, " +
                "#{item.receiveTime,jdbcType=TIMESTAMP}, ",
                "#{item.consumeTime,jdbcType=TIMESTAMP}," +
                "#{item.status,jdbcType=INTEGER}, ",
                "#{item.operatorId,jdbcType=BIGINT}, " +
                "#{item.operatorName,jdbcType=VARCHAR}, ",
                "#{item.ext1,jdbcType=VARCHAR}," +
                "#{item.ext2,jdbcType=VARCHAR}, " +
                "#{item.ext3,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    void insertList(@Param(value = "list") List<HighCouponCode> list);

    @Select("select * from high_coupon_code where child_order_id = (SELECT b.id child_order_id FROM high_order a,high_child_order b where a.mem_discount_id = #{discountUserRelId} and a.id = b.order_id and b.goods_type = 1 and b.child_orde_status in (2,3,4) GROUP BY a.mem_discount_id)")
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.BIGINT, id=true),
            @Result(column="coupon_id", property="couponId", jdbcType=JdbcType.BIGINT),
            @Result(column="merchant_id", property="merchantId", jdbcType=JdbcType.BIGINT),
            @Result(column="is_assign_agent", property="isAssignAgent", jdbcType=JdbcType.BIT),
            @Result(column="agent_id", property="agentId", jdbcType=JdbcType.BIGINT),
            @Result(column="store_id", property="storeId", jdbcType=JdbcType.BIGINT),
            @Result(column="store_name", property="storeName", jdbcType=JdbcType.VARCHAR),
            @Result(column="child_order_id", property="childOrderId", jdbcType=JdbcType.BIGINT),
            @Result(column="code_key", property="codeKey", jdbcType=JdbcType.VARCHAR),
            @Result(column="sales_code", property="salesCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="sales_end_time", property="salesEndTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="use_end_time", property="useEndTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="receive_time", property="receiveTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="consume_time", property="consumeTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @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)
    })
    HighCouponCode selectCouponCodeByUserDiscount(@Param("discountUserRelId") Long discountUserRelId);

    @Select({"<script>" +
            " select " +
            " a.coupon_name couponName," +
            " a.sales_price salesPrice," +
            " b.sales_time  salesTime," +
            " insert(b.convert_user_phone, 4, 4, '****') convertUserPhone," +
            " b.`status` status" +
            " from (select id,coupon_name,sales_price from high_coupon_agent_rel where coupon_id in (select id from high_coupon where coupon_source = 4)) a" +
            " LEFT JOIN high_coupon_agent_code b on b.coupon_agent_id = a.id" +
            " where 1 = 1" +
            " <if test='param.couponName != null'> and a.coupon_name like concat('%',#{param.couponName},'%') </if> " +
            " <if test='param.status == null'> and b.`status` in (2,3) </if> " +
            " <if test='param.status != null'> and b.`status` = #{param.status} </if> " +
            " <if test='param.assignTimeS != null'> <![CDATA[ and b.sales_time >= #{param.assignTimeS} ]]> </if> " +
            " <if test='param.assignTimeE != null'> <![CDATA[ and b.sales_time <= #{param.assignTimeE} ]]> </if> " +
            "</script>"})
    List<GzSinopecModel> selectGzSinopecAssignList(@Param("param") Map<String, Object> param);

}
