<?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.CouponIssueMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CouponIssue">
        <id column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="coupon_id_" property="couponId" />
        <result column="client_type_" property="clientType" />
        <result column="use_state_" property="useState" />
        <result column="use_time_" property="useTime" />
        <result column="order_no_" property="orderNo" />
        <result column="start_time_" property="startTime" />
        <result column="end_time_" property="endTime" />
        <result column="issue_way_" property="issueWay" />
        <result column="issuer_" property="issuer" />
        <result column="issue_time_" property="issueTime" />
        <result column="remark_" property="remark" />
        <result column="status_" property="status" />
        <result column="created_time_" property="createdTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, user_id_, coupon_id_, client_type_, use_state_, use_time_, order_no_, start_time_, end_time_, issue_way_, issuer_, issue_time_, remark_, status_, created_time_
    </sql>

    <select id="queryCouponIssueInfo" resultType="com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper">
        select
            ci.user_id_ as userId,
            ci.id_ as couponIssueId,
            ci.client_type_ as clientType,
            ci.issue_way_ as issueWay,
            ci.order_no_ as orderNo,
            ci.start_time_ as startTime,
            ci.end_time_ as endTime,
            ci.issue_time_ as issueTime,
            ci.use_time_ as useTime,
            ci.use_state_ as useState,
            ci.issuer_ as issuer,
            ci.remark_ as remark,
            ci.use_type_ as useType,
            cou.name_ as couponName,
            cou.id_ as couponId,
            cou.coupon_type_ as couponType,
            cou.category_ as couponCategory,
            cou.use_limit_ as useLimit,
            cou.discount_price_ as discountPrice
        from coupon_issue ci
        <if test="query.userId != null or (query.keyword != null  and query.keyword != '')">
            left join sys_user su on ci.user_id_ = su.id_
        </if>
        left join coupon_info cou on cou.id_ = ci.coupon_id_
        <where>
            <if test="query.userId != null">
                and #{query.userId} = ci.user_id_
            </if>
            <if test="query.couponId != null">
                and #{query.couponId} = ci.coupon_id_
            </if>
            <if test="query.clientType != null">
                and #{query.clientType} = ci.client_type_
            </if>
            <if test="query.couponCategory != null">
                and #{query.couponCategory} = cou.category_
            </if>
            <if test="query.couponCategoryList != null and query.couponCategoryList.size() != 0">
                and cou.category_ in
                <foreach collection="query.couponCategoryList" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="query.couponType != null">
                and #{query.couponType} = cou.coupon_type_
            </if>
            <if test="query.couponKeyword != null and query.couponKeyword != ''">
                and (
                    cou.id_ like '%${query.couponKeyword}%'
                    or cou.name_ like '%${query.couponKeyword}%'
                )
            </if>
            <if test="query.useState != null">
                <choose>
                    <when test="query.useState.code == 'EXPIRED'">
                        and ci.end_time_ &lt; #{query.expiredTime} and ci.use_state_  = 'USABLE'
                    </when>
                    <when test="query.useState.code == 'USABLE'">
                        and ci.end_time_ &gt;= #{query.expiredTime} and ci.use_state_ = 'USABLE'
                        <if test="query.orderUse != null and query.orderUse == 1">
                            and ci.start_time_ &lt;= #{query.expiredTime}
                        </if>
                    </when>
                    <otherwise>
                        and ci.use_state_ = #{query.useState}
                    </otherwise>
                </choose>
            </if>
            <if test="query.issueWay != null">
                and #{query.issueWay} = ci.issue_way_
            </if>
            <if test="query.issueStartTime != null">
                and  #{query.issueStartTime} &lt;= ci.issue_time_
            </if>
            <if test="query.issueEndTime != null">
                and #{query.issueEndTime} &gt;= ci.issue_time_
            </if>
            <if test="query.usedStartTime != null">
                and #{query.usedStartTime} &lt;= ci.use_time_
            </if>
            <if test="query.usedEndTime != null">
                and #{query.usedEndTime} &gt;= ci.use_time_
            </if>
            <if test="query.keyword !=null and query.keyword != ''">
                and (
                    su.real_name_ like '%${query.keyword}%'
                    or su.username_ like '%${query.keyword}%'
                    or su.phone_ like '%${query.keyword}%'
                    or su.id_ like '%${query.keyword}%'
                )
            </if>
        </where>

        order by ci.id_ desc


    </select>

    <!--用户优惠券状态统计-->
    <select id="selectCouponStateStatInfo" resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT <if test="record.groupByState != null"> t1.use_state_ AS gid,</if> COUNT(DISTINCT t1.id_) AS total FROM coupon_issue t1 <if test="record.couponType != null"> JOIN coupon_info t2 ON (t1.coupon_id_ = t2.id_)</if>
        <where>
            <if test="userId != null">
                AND t1.user_id_ = #{userId}
            </if>
            <if test="record.clientType != null">
                AND t1.client_type_ = #{record.clientType}
            </if>
            <if test="record.couponType != null">
                AND t2.coupon_type_ = #{record.couponType}
            </if>
            <if test="record.timestamp != null">
                <choose>
                    <when test="record.userState.code == 'USABLE'">
                        AND #{record.timestamp} &lt;= t1.end_time_ AND t1.use_state_ = 'USABLE'
                    </when>
                    <when test="record.userState.code == 'EXPIRED'">
                        AND #{record.timestamp} &gt;= t1.end_time_ AND t1.use_state_ = 'USABLE'
                    </when>
                    <when test="record.userState.code == 'USED'">
                        AND t1.order_no_ != ''
                    </when>
                    <otherwise>
                        AND t1.use_state_ = #{record.userState}
                    </otherwise>
                </choose>
            </if>
        </where>
        <if test="record.groupByState != null">GROUP BY t1.use_state_</if>
    </select>
    <!--用户优惠券状态统计-->

    <!--用户发送优惠券统计-->
    <select id="selectCouponIssueStatInfo" resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.user_id_ AS id, COUNT(DISTINCT t1.id_) AS total FROM coupon_issue t1
        <where>
            <if test="couponId != null">
                AND t1.coupon_id_ = #{couponId}
            </if>
            <if test="record.userIds != null">
                AND t1.user_id_ IN (<foreach collection="record.userIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.user_id_
    </select>
    <!--用户发送优惠券统计-->
</mapper>
