package com.hai.dao;

import com.hai.entity.HighDiscountAgentCode;
import com.hai.model.DiscountUseConditionModel;
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 * FROM ( " +
            " select " +
            " a.id discountAgentCodeId, " +
            " c.discount_name discountName, " +
            " case c.discount_type when 1 then '满减' when 2 then '抵扣'  when 3 then '折扣' else '未知' end discountType, " +
            " c.discount_price discountPrice, " +
            " insert(b.phone, 4, 4, '****') userPhone, " +
            " b.create_time receiveTime, " +
            " b.use_end_time useEndTime, " +
            " b.use_time useTime, " +
            " case a.`status`  " +
            " when 1 then '待领取' " +
            " when 2 then '待使用' " +
            " when 3 then '已使用' " +
            " when 4 then '已过期' " +
            " when 5 then '已分配' " +
            " end discountUseStatus, " +
            " d.order_no orderNo, " +
            " d.total_price orderTotalPrice, " +
            " (d.total_price - d.pay_price) orderDiscountPrice, " +
            " d.pay_price orderPayPrice " +
            " from (select id,status,discount_agent_id from high_discount_agent_code  " +
            " where discount_agent_id in (select id from high_discount_agent_rel where agent_id = #{param.agentId}" +
            " <if test='param.discountId != null'> and discount_id = #{param.discountId} </if> )) a " +
            "  " +
            " LEFT JOIN (select ur.discount_agent_code_id,ur.id,ur.create_time,ur.use_time,ur.use_end_time,u.phone  " +
            " from high_discount_user_rel ur " +
            " LEFT JOIN high_user u on u.id = ur.user_id " +
            " ) b on b.discount_agent_code_id = a.id " +
            "  " +
            " LEFT JOIN (select mem_discount_id,order_no,total_price,pay_price  " +
            " from high_order where mem_discount_id is not null and order_status in (2,3) GROUP BY mem_discount_id) d on d.mem_discount_id = b.id " +
            "  " +
            " LEFT JOIN (select a.id, b.id discount_id,b.discount_name,b.discount_type,b.discount_price  " +
            " from high_discount_agent_rel a, high_discount b where a.discount_id = b.id GROUP BY a.id) c on c.id = a.discount_agent_id " +
            "  " +
            " where 1 = 1  <if test='param.userPhone != null'> <![CDATA[ AND b.phone = #{param.userPhone} ]]> </if> " +
            " ORDER BY a.id desc " +
            "  " +
            " ) discountData " +
            " where 1 = 1 " +
            " <if test='param.receiveTimeS != null'> <![CDATA[ AND receiveTime >= #{param.receiveTimeS} ]]> </if> " +
            " <if test='param.receiveTimeE != null'> <![CDATA[ AND receiveTime <= #{param.receiveTimeE} ]]> </if> " +
            " <if test='param.useTimeS != null'> <![CDATA[ AND useTime >= #{param.useTimeS} ]]> </if> " +
            " <if test='param.useTimeE != null'> <![CDATA[ AND useTime <= #{param.useTimeE} ]]> </if> " +
            " <if test='param.idS != null'> <![CDATA[ AND discountAgentCodeId >= #{param.idS} ]]> </if> " +
            " <if test='param.idE != null'> <![CDATA[ AND discountAgentCodeId <= #{param.idE} ]]> </if> " +
            " <if test='param.status != null'> <![CDATA[ AND discountUseStatus = #{param.status} ]]> </if> " +
            "</script>" })
    List<DiscountUseConditionModel> 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);
}
