<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.pluss.platform.mapper.SubMerchantApplyOrderMapper">

    <select id="selectPageMoreDetail" resultType="java.util.Map">
        SELECT tpmcs.id, tpmcs.merchantCode, tpmcs.channel channelId,
        tpsmao.subMchType,tpsmao.applyNo,tpsmao.status,tpsmao.infoQrcode,tpsmao.resultStatus
        , tpsmao.remark, tpmcs.createTime auditCreateTime, tpmcs.updateTime auditUpdateTime, tpsmao.createTime,
        tpsmao.updateTime, tpui.phone, tpic.certName bankCertName, tpmcs.merchantId
        FROM tb_pluss_merchant_channel_status tpmcs
        LEFT JOIN tb_pluss_sub_merchant_apply_order tpsmao ON tpsmao.merchantCode = tpmcs.merchantCode AND tpmcs.channel
        = tpsmao.channelId
        LEFT JOIN tb_pluss_user_app tpua ON tpmcs.merchantCode = tpua.merchantCode
        LEFT JOIN tb_pluss_user_info tpui ON tpua.userId = tpui.id
        LEFT JOIN tb_pluss_id_card tpic ON tpic.userId = tpua.userId
        <where>
            tpua.userType = 'promoter' AND
            tpic.userType = '01' AND
            tpmcs.merchantId IS NOT NULL AND
            tpmcs.status IN (3, 4)
            <if test="condition.merchantCode != null and condition.merchantCode != ''">
                and tpmcs.merchantCode LIKE CONCAT('%', #{condition.merchantCode}, '%')
            </if>
            <if test="condition.phone != null and condition.phone != ''">
                and tpui.phone LIKE CONCAT('%', #{condition.phone}, '%')
            </if>
            <if test="condition.merchantId != null and condition.merchantId != ''">
                and tpmcs.merchantId LIKE CONCAT('%', #{condition.merchantId}, '%')
            </if>
            <if test="condition.userName != null and condition.userName != ''">
                and tpic.certName LIKE CONCAT('%', #{condition.userName}, '%')
            </if>
            <if test="condition.status != null and condition.status != ''">
                and tpsmao.status LIKE CONCAT('%', #{condition.status}, '%')
            </if>
            <if test="condition.resultStatus != null and condition.resultStatus != ''">
                <choose>
                    <when test="condition.resultStatus == '-1'">
                        AND (
                        (tpsmao.id IS NULL OR tpsmao.resultStatus = '4' OR (tpsmao.resultStatus IS NULL AND tpsmao.applyNo IS NOT NULL))
                        )
                    </when>
                    <when test="condition.resultStatus == 3">
                        AND (
                        tpsmao.resultStatus NOT IN ('waitting', 'APPLYMENT_STATE_WAITTING_FOR_AUDIT', '1',
                        'confirmcontact', '待确认联系信息', 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT')
                        )
                    </when>
                    <when test="condition.resultStatus == 0">
                        AND tpsmao.resultStatus IN ('waitting', 'APPLYMENT_STATE_WAITTING_FOR_AUDIT', '4')
                    </when>
                    <when test="condition.resultStatus == 1">
                        AND tpsmao.resultStatus IN ('1', 'confirmcontact', '待确认联系信息',
                        'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT')
                    </when>
                    <otherwise>
                        AND tpsmao.resultStatus = #{condition.resultStatus}
                    </otherwise>
                </choose>
            </if>
        </where>
        <if test="condition.column != null and condition.column != ''">
            ORDER BY ${condition.column}
            <if test='condition.asc == "false"'>
                DESC
            </if>
        </if>
    </select>

    <!-- 添加applyNo的校验是为了判断之前是否存在已经成功发起微信认证的记录 -->
    <select id="selectListNoCert" resultType="cn.pluss.platform.entity.SubMerchantApplyOrder">
        SELECT tpsmao.id,
               tpmcs.merchantCode,
               tpmcs.merchantId,
               tpmcs.applicationId applyNo,
               tpmcs.channel channelId
        FROM tb_pluss_merchant_channel_status tpmcs
                 LEFT JOIN (
            SELECT smao.*
            FROM (SELECT merchantCode, channelId, MAX(createTime) lastTime, MAX(applyNo) applyNo
                  FROM tb_pluss_sub_merchant_apply_order
                  GROUP BY merchantCode, channelId) t
                     LEFT JOIN tb_pluss_sub_merchant_apply_order smao ON smao.createTime = t.lastTime AND smao.merchantCode = t.merchantCode AND smao.channelId = t.channelId
        ) tpsmao ON tpsmao.merchantCode = tpmcs.merchantCode
            AND tpmcs.channel = tpsmao.channelId
                 LEFT JOIN tb_pluss_user_app tpua ON tpmcs.merchantCode = tpua.merchantCode
                 LEFT JOIN tb_pluss_user_info tpui ON tpua.userId = tpui.id
                 LEFT JOIN tb_pluss_id_card tpic ON tpic.userId = tpua.userId
        WHERE tpua.userType = 'promoter'
          AND tpic.userType = '01'
          AND tpmcs.merchantId IS NOT NULL
          AND tpmcs.channel IN (1, 4)
          AND tpmcs.`status` IN (3, 4)
          AND (tpsmao.id IS NULL OR
               (tpsmao.updateTime &lt; DATE_SUB(NOW(), INTERVAL 6 MINUTE ) AND tpsmao.resultStatus = '4' AND tpsmao.applyNo IS NULL ))
        ORDER BY tpmcs.createTime
                DESC LIMIT 0, 20;
    </select>
</mapper>