<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yonge.cooleshow.biz.dal.mapper.CouponInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CouponInfo">
        <id column="id_" property="id" />
        <result column="name_" property="name" />
        <result column="serial_num_" property="serialNum" />
        <result column="describe_" property="describe" />
        <result column="client_type_" property="clientType" />
        <result column="category_" property="category" />
        <result column="use_limit_" property="useLimit" />
        <result column="discount_price_" property="discountPrice" />
        <result column="coupon_type_" property="couponType" />
        <result column="valid_day_" property="validDay" />
        <result column="start_time_" property="startTime" />
        <result column="end_time_" property="endTime" />
        <result column="inventory_" property="inventory" />
        <result column="quantity_limit_" property="quantityLimit" />
        <result column="status_" property="status" />
        <result column="updated_by_" property="updatedBy" />
        <result column="update_time_" property="updateTime" />
        <result column="created_by_" property="createdBy" />
        <result column="created_time_" property="createdTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, name_, serial_num_, describe_, client_type_, category_, use_limit_, discount_price_, coupon_type_, valid_day_, start_time_, end_time_, inventory_, quantity_limit_, status_, updated_by_, update_time_, created_by_, created_time_
    </sql>

    <!--优惠券分页查询-->
    <select id="selectCouponPageInfo" resultType="com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper">
        SELECT t1.id_, t1.name_, t1.client_type_, t1.category_, t1.coupon_type_, t1.inventory_, t1.status_, t1.updated_by_, t1.update_time_, t1.created_time_, t1.valid_type_, t1.end_time_ FROM coupon_info t1
        <where>
            <if test="record.couponId != null">
                AND t1.id_ = #{record.couponId}
            </if>
            <if test="record.name != null">
                AND t1.name_ LIKE '%${record.name}%'
            </if>
            <if test="record.clientType != null">
                AND t1.client_type_ = #{record.clientType}
            </if>
            <if test="record.couponType != null">
                AND t1.coupon_type_ = #{record.couponType}
            </if>
            <if test="record.category != null">
                AND t1.category_ = #{record.category}
            </if>
            <if test="record.status != null">
                AND t1.status_ = #{record.status}
            </if>
            <if test="record.timestamp != null">
                AND (#{record.timestamp} &lt;= t1.end_time_ <if test="record.validType != null"> OR t1.valid_type_ = #{record.validType}</if>)
            </if>
        </where>
        ORDER BY t1.id_ DESC
    </select>

    <!--优惠券发放统计-->
    <select id="selectCouponIssueStatInfo" resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.coupon_id_ AS id, COUNT(DISTINCT t1.id_) AS total FROM coupon_issue t1
        <where>
            <if test="couponIds != null">
                AND t1.coupon_id_ IN (<foreach collection="couponIds" separator="," item="item">#{item}</foreach>)
            </if>
            <if test="record.useState != null">
                AND t1.use_state_ = #{record.useState}
            </if>
            <if test="record.timestamp != null">
                AND (t1.start_time_ &lt;= #{record.timestamp} AND #{record.timestamp} &lt;= t1.end_time_)
            </if>
            <if test="record.excludeUseState != null">
                AND t1.use_state_ NOT IN (<foreach collection="record.excludeUseState" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.coupon_id_
    </select>
    <!--优惠券分页查询-->

    <!--优惠券库存调整信息-->
    <resultMap id="CouponInventoryPageInfoResultMap" type="com.yonge.cooleshow.biz.dal.wrapper.coupon.CouponInventoryWrapper">
        <id column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="coupon_id_" property="couponId" />
        <result column="data_type_" property="dataType" />
        <result column="number_" property="number" />
        <result column="remark_" property="remark" />
        <result column="created_time_" property="createdTime" />

        <result column="username_" property="username" />
        <result column="real_name_" property="realName" />
    </resultMap>
    <select id="selectCouponInventoryPageInfo" resultMap="CouponInventoryPageInfoResultMap">
        SELECT t1.id_, t1.user_id_, t1.coupon_id_, t1.data_type_, t1.number_, t1.remark_, t1.created_time_, t2.username_, t2.real_name_
        FROM coupon_inventory t1 JOIN sys_user t2 ON t1.user_id_ = t2.id_
        <where>
            <if test="record.couponId != null">
                AND t1.coupon_id_ = #{record.couponId}
            </if>
            <if test="record.dataType != null">
                AND t1.data_type_ = #{record.dataType}
            </if>
            <if test="record.userId != null">
                AND t1.user_id_ = #{record.userId}
            </if>
            <if test="record.keyword != null">
                AND (t2.username_ LIKE '%${record.keyword}%' OR t2.real_name_ LIKE '%${record.keyword}%')
            </if>
            <if test="record.startTime != null and record.endTime != null">
                AND (#{record.startTime} &lt;= UNIX_TIMESTAMP(t1.created_time_) AND UNIX_TIMESTAMP(t1.created_time_) &lt;= #{record.endTime})
            </if>
        </where>
        <if test="record.groupByUser != null">GROUP BY t1.user_id_</if> ORDER BY t1.id_ DESC
    </select>
    <!--优惠券库存调整信息-->

    <!--活动优惠券统计-->
    <select id="selectActivityCouponStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.coupon_id_ AS id, COUNT(DISTINCT t1.id_) AS total FROM activity_reward t1
        <where>
            <if test="couponIds != null">
                AND t1.coupon_id_ IN (<foreach collection="couponIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.coupon_id_
    </select>

    <!--用户订单优惠券信息-->
    <resultMap id="UserOrderCouponInfoResultMap" type="com.yonge.cooleshow.biz.dal.wrapper.coupon.CouponOrderWrapper$CouponInfo">
        <id column="id_" property="id" />
        <result column="name_" property="name" />
        <result column="use_limit_" property="useLimit" />
        <result column="discount_price_" property="discountPrice" />
        <result column="issue_id" property="issueId" />
    </resultMap>
    <select id="selectUserOrderCouponInfo" resultMap="UserOrderCouponInfoResultMap">
        SELECT t1.id_, t1.name_, t1.use_limit_, t1.discount_price_, t2.id_ AS issue_id
        FROM coupon_info t1 JOIN coupon_issue t2 ON t1.id_ = t2.coupon_id_
        <where>
            <if test="userId != null">
                AND t2.user_id_ = #{userId}
            </if>
            <if test="record.issueIds != null">
                AND t2.id_ IN (<foreach collection="record.issueIds" separator="," item="item">#{item}</foreach>)
            </if>
            <if test="record.clientType != null">
                AND t2.client_type_ = #{record.clientType}
            </if>
            <if test="record.useState != null">
                AND t2.use_state_ = #{record.useState}
            </if>
            <if test="record.timestamp != null">
                AND (t2.start_time_ &lt;= #{record.timestamp} AND #{record.timestamp} &lt;= t2.end_time_)
            </if>
            <if test="record.orderNo != null">
                AND t2.order_no_ = #{record.orderNo}
            </if>
            <if test="record.couponTypes != null">
                AND t1.category_ IN (<foreach collection="record.couponTypes" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
    </select>
    <!--用户订单优惠券信息-->

    <update id="updateStock">
        update coupon_info set inventory_ = inventory_
        <choose>
            <when test="inOrOut.code == 'IN'">
                + #{size}
            </when>
            <when test="inOrOut.code == 'OUT'">
                - #{size}
            </when>
        </choose>
        <where>
            <if  test="inOrOut.code == 'OUT'">
                and inventory_ - #{size} &gt;= 0
            </if>
            and id_ = #{couponId}
        </where>
    </update>


</mapper>
