<?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.TenantActivationCodeMapper">
    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ AS id
        , t.tenant_id_ AS tenantId
        , t.tenant_album_id_ AS tenantAlbumId
        , t.tenant_album_purchase_id_ AS tenantAlbumPurchaseId
        , t.activation_code_ AS activationCode
        , t.send_status_ AS sendStatus
        , t.activation_status_ AS activationStatus
        , t.activation_phone_ AS activationPhone
        , t.activation_time_ AS activationTime
        , t.activation_user_id_ AS activationUserId
        , t.update_time_ AS updateTime
        , t.create_time_ AS createTime
        , t.send_time_ AS sendTime
    </sql>

    <select id="selectPage"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantActivationCodeWrapper$TenantActivationCode">
        SELECT
        <include refid="baseColumns"/>, ta.purchase_cycle_ as purchaseCycle, su.username_ as activationUserName,ta.purchase_type_ as purchaseType,
        su.avatar_ as activationUserAvatar
        FROM tenant_activation_code t
        LEFT JOIN tenant_album_purchase ta on t.tenant_album_purchase_id_ = ta.id_
        LEFT JOIN sys_user su on t.activation_phone_ = su.phone_
        LEFT JOIN student st on su.id_ = st.user_id_
        <if test="param.orderNo != null and param.orderNo != ''">
            left join tenant_album_purchase t4 on t4.id_ = t.tenant_album_purchase_id_
        </if>
        <where>
            <if test="param.keyword != null and param.keyword.trim() != ''">
                AND (
                t.activation_code_ like concat ('%',#{param.keyword},'%') or
                su.username_ like concat ('%',#{param.keyword},'%')
                )
            </if>
            <if test="param.tenantId != null">
                AND t.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.tenantAlbumPurchaseId != null">
                AND t.tenant_album_purchase_id_ = #{param.tenantAlbumPurchaseId}
            </if>
            <if test="param.sendStatus != null">
                AND t.send_status_ = #{param.sendStatus}
            </if>
            <if test="param.activationStatus != null">
                AND t.activation_status_ = #{param.activationStatus}
            </if>
            <if test="param.activationPhone != null">
                AND t.activation_phone_ = #{param.activationPhone}
            </if>
            <if test="param.activationStartTime != null">
                AND t.activation_time_ >= #{param.activationStartTime}
            </if>
            <if test="param.activationEndTime != null">
                AND #{param.activationEndTime} >= t.activation_time_
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and ${param.orderNo} = t4.order_no_
            </if>
            <if test="param.subjectId != null and param.subjectId.trim() != ''">
                and #{param.subjectId} = st.subject_id_
            </if>
            <if test="param.tenantGroupId != null">
                and st.tenant_group_id_ = #{param.tenantGroupId}
            </if>
            <if test="param.activationCode != null and param.activationCode.trim() != ''">
                AND t.activation_code_ like concat('%',#{param.activationCode},'%')
            </if>
        </where>
        order by t.activation_status_ asc, t.id_ desc
    </select>
</mapper>