<?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="cn.pluss.platform.mapper.UserAppMapper">

    <resultMap id="userInfoVO" type="cn.pluss.platform.vo.UserInfoVO" autoMapping="true">
        <result property="id" column="id" />
        <result property="userType" column="userType" />
        <result property="merchantCode" column="merchantCode" />
        <result property="alias" column="alias" />
        <result property="merchantName" column="merchantName" />
        <result property="contactMobile" column="contactMobile" />
        <result property="parentId" column="parentId" />
        <result property="userName" column="userName" />
        <result property="inviteNum" column="inviteNum" />
        <result property="parentPhone" column="parentPhone" />
        <result property="parentLoginName" column="parentLoginName" />
        <result property="parentUserType" column="parentUserType" />
        <result property="parentUserId" column="parentUserId" />
        <result property="parentUserName" column="parentUserName" />
        <result property="loginName" column="loginName" />
        <result property="level" column="level" />
        <result property="phone" column="phone" />
        <result property="status" column="status" />
        <result property="createDt" column="createDt" />
        <result property="userId" column="userId" />
        <result property="merchantType" column="merchantType" />
        <result property="ppThirdStatus" column="ppThirdStatus" />
        <result property="ppStatus" column="ppStatus" />
        <result property="isBlack" column="isBlack" />
    </resultMap>

    <select id="selectUserApp" resultType="cn.pluss.platform.entity.UserApp">
        <include refid="commonSelect"></include>
        ${ew.customSqlSegment}
    </select>

    <sql id="commonSelect">
        SELECT DISTINCT ua.*, ui.phone, IFNULL(sp.permissionBill, 1) permissionBill
        FROM tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
                 LEFT JOIN tb_pluss_merchant_staff_conn msc ON ua.userId = msc.userId
                 LEFT JOIN tb_pluss_staff_permission sp ON ua.userId = sp.userId AND msc.merchantCode = sp.merchantCode
    </sql>

    <select id="queryUserAppPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_user_app
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="logo!= null">
                and logo = #{logo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="userType!= null">
                and userType = #{userType}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="stTime!= null">
                and createDt &gt;= #{stTime}
            </if>
            <if test="enTime!= null">
                and createDt &lt;= #{enTime}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="certnum!= null">
                and certnum = #{certnum}
            </if>
            <if test="certBacKPhoto!= null">
                and certBacKPhoto = #{certBacKPhoto}
            </if>
            <if test="handCertPhoto!= null">
                and handCertPhoto = #{handCertPhoto}
            </if>
            <if test="bankName!= null">
                and bankName = #{bankName}
            </if>
            <if test="bankAddress!= null">
                and bankAddress = #{bankAddress}
            </if>
            <if test="bankBranch!= null">
                and bankBranch = #{bankBranch}
            </if>
            <if test="bankNo!= null">
                and bankNo = #{bankNo}
            </if>
            <if test="bankPhoto!= null">
                and bankPhoto = #{bankPhoto}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="fansProfit!= null">
                and fansProfit = #{fansProfit}
            </if>
            <if test="extendProfit!= null">
                and extendProfit = #{extendProfit}
            </if>
            <if test="cashProfit!= null">
                and cashProfit = #{cashProfit}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="staffType!= null">
                and staffType = #{staffType}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="inviteNum!= null">
                and inviteNum = #{inviteNum}
            </if>
            <if test="bankStatus!= null">
                and bankStatus = #{bankStatus}
            </if>
            <if test="merchantStatus!= null">
                and merchantStatus = #{merchantStatus}
            </if>
            <if test="certReason!= null">
                and certReason = #{certReason}
            </if>
            <if test="bankReason!= null">
                and bankReason = #{bankReason}
            </if>
            <if test="merchantReason!= null">
                and merchantReason = #{merchantReason}
            </if>
            <if test="certPeriod!= null">
                and certPeriod = #{certPeriod}
            </if>
            <if test="certStartTime!= null">
                and certStartTime = #{certStartTime}
            </if>
            <if test="bankAddressCode!= null">
                and bankAddressCode = #{bankAddressCode}
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="merchantAddress!= null">
                and merchantAddress = #{merchantAddress}
            </if>
            <if test="merchantDetailAddress!= null">
                and merchantDetailAddress = #{merchantDetailAddress}
            </if>
            <if test="contactName!= null">
                and contactName = #{contactName}
            </if>
            <if test="contactPhone!= null">
                and contactPhone = #{contactPhone}
            </if>
            <if test="productDesc!= null">
                and productDesc = #{productDesc}
            </if>
            <if test="bussAuthNum!= null">
                and bussAuthNum = #{bussAuthNum}
            </if>
            <if test="bussAuthPeriod!= null">
                and bussAuthPeriod = #{bussAuthPeriod}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="appAuthToken!= null">
                and appAuthToken = #{appAuthToken}
            </if>
            <if test="tradeMoney!= null">
                and tradeMoney = #{tradeMoney}
            </if>
            <if test="authCount!= null">
                and authCount = #{authCount}
            </if>
            <if test="subMchId!= null">
                and subMchId = #{subMchId}
            </if>
            <if test="aisleSwitch!= null">
                and aisleSwitch = #{aisleSwitch}
            </if>
            <if test="unionpay!= null">
                and unionpay = #{unionpay}
            </if>
            <if test="mccCode!= null">
                and mccCode = #{mccCode}
            </if>
            <if test="contactEmail!= null">
                and contactEmail = #{contactEmail}
            </if>
            <if test="mccName!= null">
                and mccName = #{mccName}
            </if>
            <if test="leshuaLicenseName!= null">
                and leshuaLicenseName = #{leshuaLicenseName}
            </if>
            <if test="aliAccount!= null">
                and aliAccount = #{aliAccount}
            </if>
            <if test="aliName!= null">
                and aliName = #{aliName}
            </if>
            <if test="aliSignUrl!= null">
                and aliSignUrl = #{aliSignUrl}
            </if>
            <if test="wechatSignUrl!= null">
                and wechatSignUrl = #{wechatSignUrl}
            </if>
            <if test="token!= null">
                and token = #{token}
            </if>
            <if test="endTime!= null">
                and createDt <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt <![CDATA[<=]]> #{endTime}
            </if>
            <if test="isVoice!= null">
                and isVoice = #{isVoice}
            </if>
            <if test="parentIdList!= null">
                and parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryUserPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_user_app ua
            LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        <where>
            <if test="phone != null">
                and ui.phone = #{phone}
            </if>
            <if test="id!= null">
                and ua.id = #{id}
            </if>
            <if test="userId!= null">
                and ua.userId = #{userId}
            </if>
            <if test="logo!= null">
                and ua.logo = #{logo}
            </if>
            <if test="level!= null">
                and ua.level = #{level}
            </if>
            <if test="userType!= null">
                and ua.userType = #{userType}
            </if>
            <if test="createDt!= null">
                and ua.createDt = #{createDt}
            </if>
            <if test="stTime!= null">
                and ua.createDt &gt;= #{stTime}
            </if>
            <if test="enTime!= null">
                and ua.createDt &lt;= #{enTime}
            </if>
            <if test="status!= null">
                and ua.status = #{status}
            </if>
            <if test="certnum!= null">
                and ua.certnum = #{certnum}
            </if>
            <if test="certBacKPhoto!= null">
                and ua.certBacKPhoto = #{certBacKPhoto}
            </if>
            <if test="handCertPhoto!= null">
                and ua.handCertPhoto = #{handCertPhoto}
            </if>
            <if test="bankName!= null">
                and ua.bankName = #{bankName}
            </if>
            <if test="bankAddress!= null">
                and ua.bankAddress = #{bankAddress}
            </if>
            <if test="bankBranch!= null">
                and ua.bankBranch = #{bankBranch}
            </if>
            <if test="bankNo!= null">
                and ua.bankNo = #{bankNo}
            </if>
            <if test="bankPhoto!= null">
                and ua.bankPhoto = #{bankPhoto}
            </if>
            <if test="merchantCode!= null">
                and ua.merchantCode = #{merchantCode}
            </if>
            <if test="fansProfit!= null">
                and ua.fansProfit = #{fansProfit}
            </if>
            <if test="extendProfit!= null">
                and ua.extendProfit = #{extendProfit}
            </if>
            <if test="cashProfit!= null">
                and ua.cashProfit = #{cashProfit}
            </if>
            <if test="userName!= null">
                and ua.userName = #{userName}
            </if>
            <if test="staffType!= null">
                and ua.staffType = #{staffType}
            </if>
            <if test="parentId!= null">
                and ua.parentId = #{parentId}
            </if>
            <if test="inviteNum!= null">
                and ua.inviteNum = #{inviteNum}
            </if>
            <if test="bankStatus!= null">
                and ua.bankStatus = #{bankStatus}
            </if>
            <if test="merchantStatus!= null">
                and ua.merchantStatus = #{merchantStatus}
            </if>
            <if test="certReason!= null">
                and ua.certReason = #{certReason}
            </if>
            <if test="bankReason!= null">
                and ua.bankReason = #{bankReason}
            </if>
            <if test="merchantReason!= null">
                and ua.merchantReason = #{merchantReason}
            </if>
            <if test="certPeriod!= null">
                and ua.certPeriod = #{certPeriod}
            </if>
            <if test="certStartTime!= null">
                and ua.certStartTime = #{certStartTime}
            </if>
            <if test="bankAddressCode!= null">
                and ua.bankAddressCode = #{bankAddressCode}
            </if>
            <if test="merchantName!= null">
                and ua.merchantName = #{merchantName}
            </if>
            <if test="merchantAddress!= null">
                and ua.merchantAddress = #{merchantAddress}
            </if>
            <if test="merchantDetailAddress!= null">
                and ua.merchantDetailAddress = #{merchantDetailAddress}
            </if>
            <if test="contactName!= null">
                and ua.contactName = #{contactName}
            </if>
            <if test="contactPhone!= null">
                and ua.contactPhone = #{contactPhone}
            </if>
            <if test="productDesc!= null">
                and ua.productDesc = #{productDesc}
            </if>
            <if test="bussAuthNum!= null">
                and ua.bussAuthNum = #{bussAuthNum}
            </if>
            <if test="bussAuthPeriod!= null">
                and ua.bussAuthPeriod = #{bussAuthPeriod}
            </if>
            <if test="storeId!= null">
                and ua.storeId = #{storeId}
            </if>
            <if test="appAuthToken!= null">
                and ua.appAuthToken = #{appAuthToken}
            </if>
            <if test="tradeMoney!= null">
                and ua.tradeMoney = #{tradeMoney}
            </if>
            <if test="authCount!= null">
                and ua.authCount = #{authCount}
            </if>
            <if test="subMchId!= null">
                and ua.subMchId = #{subMchId}
            </if>
            <if test="aisleSwitch!= null">
                and ua.aisleSwitch = #{aisleSwitch}
            </if>
            <if test="unionpay!= null">
                and ua.unionpay = #{unionpay}
            </if>
            <if test="mccCode!= null">
                and ua.mccCode = #{mccCode}
            </if>
            <if test="contactEmail!= null">
                and ua.contactEmail = #{contactEmail}
            </if>
            <if test="mccName!= null">
                and ua.mccName = #{mccName}
            </if>
            <if test="leshuaLicenseName!= null">
                and ua.leshuaLicenseName = #{leshuaLicenseName}
            </if>
            <if test="aliAccount!= null">
                and ua.aliAccount = #{aliAccount}
            </if>
            <if test="aliName!= null">
                and ua.aliName = #{aliName}
            </if>
            <if test="aliSignUrl!= null">
                and ua.aliSignUrl = #{aliSignUrl}
            </if>
            <if test="wechatSignUrl!= null">
                and ua.wechatSignUrl = #{wechatSignUrl}
            </if>
            <if test="token!= null">
                and ua.token = #{token}
            </if>
            <if test="endTime!= null">
                and ua.createDt <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime!= null">
                and ua.createDt <![CDATA[<=]]> #{endTime}
            </if>
            <if test="isVoice!= null">
                and ua.isVoice = #{isVoice}
            </if>
            <if test="parentIdList!= null">
                and ua.parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryUserInfoVOPage" parameterType="java.util.Map" resultMap="userInfoVO">
        SELECT DISTINCT SQL_CALC_FOUND_ROWS
        ua.id,
        ui.status AS uiStatus,
        ua.roleCode AS roleCode,
        ua.userType AS userType,
        ua.merchantCode AS merchantCode,
        mbi.alias,
        mbi.merchantName,
        mbi.contactMobile AS contactMobile,
        ua.parentId AS parentId,
        ua.userName AS userName,
        ua.inviteNum AS inviteNum,
        pui.phone AS parentPhone,
        pui.loginName AS parentLoginName,
        pua.userType AS parentUserType,
        pua.userId AS parentUserId,
        pua.userName as parentUserName,
        ui.loginName AS loginName,
        ua.`level` AS level,
        ul.levelName as levelName,
        ui.phone AS phone,
        ua.`status` AS status,
        ua.createDt AS createDt,
        ua.userId AS userId,
        ua.spreadFlag,
        mbi.merchantType AS merchantType,
        pp.thirdStatus AS ppThirdStatus,
        pp.status AS ppStatus,
        IF(rb.id IS NULL, false, true) isBlack
        FROM
        <choose>
            <when test="sameIp != null and sameIp != ''">
                tb_pluss_user_app ua1
                LEFT JOIN tb_pluss_operation_record opr ON opr.userId = ua1.userId
                LEFT JOIN tb_pluss_operation_record opr2 ON opr.ip = opr2.ip
                LEFT JOIN tb_pluss_user_app ua ON ua.userId = opr2.userId
            </when>
            <otherwise>
                tb_pluss_user_app ua
            </otherwise>
        </choose>
        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_user_info pui ON pui.id = ua.parentId
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_risk_blacklist rb ON rb.userId = ua.userId
        LEFT JOIN tb_pluss_user_level ul ON ul.levelCode = ua.levelCode
        LEFT JOIN tb_pluss_sub_merchant sm ON sm.merchantId = mcs.merchantId
        LEFT JOIN (
            SELECT mp.* FROM
            (
                SELECT userId, MAX(createTime) createTime
                FROM tb_pluss_merchant_plugin GROUP BY userId
            ) t LEFT JOIN
        tb_pluss_merchant_plugin mp
        ON t.userId = mp.userId AND mp.createTime = t.createTime
        ) pp
        ON ua.userId = pp.userId
        <where>
            <choose>
                <when test="userType!=null and userType!=''">
                    <choose>
                        <when test="userType == 'agent_agency'">
                            and ua.userType = 'agent' and pua.userId is not null
                        </when>
                        <otherwise>
                            <choose>
                                <when test="userType == 'agent'">
                                    and ua.userType = 'agent' and pua.userId is null
                                </when>
                                <otherwise>
                                    and ua.userType=#{userType}
                                </otherwise>
                            </choose>
                        </otherwise>
                    </choose>
                </when>
                <otherwise>
                    and ua.userType <![CDATA[<>]]> 'staff'
                </otherwise>
            </choose>
            <if test="userId!=null and userId!=''">
                <choose>
                    <when test="sameIp != null and sameIp != ''">
                        and ua1.userId = #{userId}
                    </when>
                    <otherwise>
                        and ua.userId = #{userId}
                    </otherwise>
                </choose>
            </if>
            <if test="merchantType != null and merchantType != ''">
                and mbi.merchantType = #{merchantType}
            </if>
            <if test="inviteNum!=null and inviteNum!=''">
                and ua.inviteNum LIKE CONCAT("%",#{inviteNum},"%")
            </if>
            <if test="merchantCode!=null and merchantCode!=''">
                and ua.merchantCode = #{merchantCode}
            </if>
            <if test="alias!=null and alias!=''">
                and mbi.alias LIKE CONCAT("%",#{alias},"%")
            </if>
            <if test="userName!=null and userName!=''">
                and ua.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="merchantId != null and merchantId  != ''">
                and mcs.merchantId LIKE CONCAT("%",#{merchantId},"%")
            </if>
            <if test="merchantId != null and merchantId  != ''">
                and mcs.merchantId LIKE CONCAT("%",#{merchantId},"%")
            </if>
            <if test="phone!=null and phone!=''">
                and ui.phone LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="parentPhone!=null and parentPhone!=''">
                and pui.phone LIKE CONCAT("%",#{parentPhone},"%")
            </if>
            <if test="contactMobile!=null and contactMobile!=''">
                and mbi.contactMobile LIKE CONCAT("%",#{contactMobile},"%")
            </if>
            <if test="status!=null">
                and ua.status=#{status}
            </if>
            <if test="wxMercNo != null">
                and sm.subMchType = 'WX'
                and sm.subMchId LIKE CONCAT('%', #{wxMercNo}, '%')
            </if>
            <if test="zfbMercNo != null">
                and sm.subMchType = 'ZFB'
                and sm.subMchId LIKE CONCAT('%', #{wxMercNo}, '%')
            </if>
            <choose>
                <when test="mcsStatus == '2'.toString()">
                    and (mcs.status = '2' or (mcs.status = '0' and mcs.thirdStatus = '-100'))
                </when>
                <otherwise>
                    <if test="mcsStatus!=null and mcsStatus!=''">
                        and mcs.status = #{mcsStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="thirdStatus == '100'.toString()">
                    and mcs.thirdStatus <![CDATA[<>]]> '-100'
                </when>
                <otherwise>
                    <if test="thirdStatus != null and thirdStatus != ''">
                        and mcs.thirdStatus = #{thirdStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="ppStatus == '2'.toString()">
                    and (pp.status = '2' or (pp.status = '0' and pp.thirdStatus = '-100'))
                </when>
                <otherwise>
                    <if test="ppStatus!=null and ppStatus!=''">
                        and pp.status = #{ppStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="ppThirdStatus == '100'.toString()">
                    and pp.thirdStatus <![CDATA[<>]]> '-100'
                </when>
                <otherwise>
                    <if test="ppThirdStatus != null and ppThirdStatus != ''">
                        and pp.thirdStatus = #{ppThirdStatus}
                    </if>
                </otherwise>
            </choose>
            <if test="parentUserType != null and parentUserType != ''">
                <choose>
                    <when test="parentUserType == 'agent_agency'">
                        and pua.userId IN (SELECT ua.userId
                        FROM tb_pluss_user_app ua
                        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
                        WHERE ua.userType = 'agent' and pua.userId is not null)
                    </when>
                    <otherwise>
                        <choose>
                            <when test="parentUserType == 'agent'">
                                and pua.userId IN (SELECT ua.userId
                                FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
                                WHERE ua.userType = 'agent' and pua.userId is null)
                            </when>
                            <otherwise>
                                and pua.userType=#{parentUserType}
                            </otherwise>
                        </choose>
                    </otherwise>
                </choose>
            </if>
            <if test="merchantCode!=null">
                and mbi.merchantCode=#{merchantCode}
            </if>
        </where>
        order by ua.id desc limit #{pageSize} offset #{offset};
    </select>

    <select id="queryUserInfoVONoPage" parameterType="java.util.Map" resultType="cn.pluss.platform.vo.UserInfoVO">
        SELECT
        ua.id,
        mbi.alias,
        mbi.merchantName,
        mbi.contactMobile AS contactMobile,
        ua.userName AS userName,
        ua.inviteNum AS inviteNum,
        pui.phone AS parentPhone,
        pui.loginName AS parentLoginName,
        pua.userType AS parentUserType,
        ui.loginName AS loginName,
        ua.`level` AS LEVEL,
        ui.phone AS phone,
        ua.`status` AS STATUS,
        ua.createDt AS createDt,
        ua.userId AS userId
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
        LEFT JOIN tb_pluss_user_info pui ON pui.id = ua.parentId
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.merchantCode = ua.merchantCode
        <where>
            <if test="userId != null and userId !='' ">
                and ua.userId = #{userId}
            </if>
            <if test="inviteNum!=null and inviteNum!=''">
                and ua.inviteNum LIKE CONCAT("%",#{inviteNum},"%")
            </if>
            <if test="alias!=null and alias!=''">
                and mbi.alias LIKE CONCAT("%",#{alias},"%")
            </if>
            <if test="userName!=null and userName!=''">
                and ua.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="phone!=null and phone!=''">
                and ui.phone LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="parentPhone!=null and parentPhone!=''">
                and pui.phone LIKE CONCAT("%",#{parentPhone},"%")
            </if>
            <if test="contactMobile!=null and contactMobile!=''">
                and mbi.contactMobile LIKE CONCAT("%",#{contactMobile},"%")
            </if>
            <if test="userType!=null and userType!=''">
                and ua.userType=#{userType}
            </if>
            <if test="status!=null">
                and ua.status=#{status}
            </if>
            <if test="parentUserType!=null">
                and pua.userType=#{parentUserType}
            </if>
            <if test="merchantCode!=null">
                and ua.merchantCode in (SELECT merchantCode
                FROM tb_pluss_merchant_base_info
                WHERE bindingCode =(SELECT bindingCode
                FROM tb_pluss_merchant_base_info
                where merchantCode = #{merchantCode}))
            </if>
        </where>
        order by ua.id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryUserInfoVOPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(1)
        from
        <choose>
            <when test="sameIp != null and sameIp != ''">
                tb_pluss_user_app ua1
                LEFT JOIN tb_pluss_operation_record opr ON opr.userId = ua1.userId
                LEFT JOIN tb_pluss_operation_record opr2 ON opr.ip = opr2.ip
                LEFT JOIN tb_pluss_user_app ua ON ua.userId = opr2.userId
            </when>
            <otherwise>
                tb_pluss_user_app ua
            </otherwise>
        </choose>
        LEFT JOIN tb_pluss_user_info ui
        ON ua.userId=ui.id
        LEFT JOIN tb_pluss_user_info pui ON pui.id = ua.parentId
        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_merchant_base_info mbi
        on mbi.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_merchant_plugin pp ON pp.userId = ua.userId
        <where>
            <choose>
                <when test="userType!=null and userType!=''">
                    <choose>
                        <when test="userType == 'agent_agency'">
                            and ua.userType = 'agent' and pua.userId is not null
                        </when>
                        <otherwise>
                            <choose>
                                <when test="userType == 'agent'">
                                    and ua.userType = 'agent' and pua.userId is null
                                </when>
                                <otherwise>
                                    and ua.userType=#{userType}
                                </otherwise>
                            </choose>
                        </otherwise>
                    </choose>
                </when>
                <otherwise>
                    and ua.userType <![CDATA[<>]]> 'staff'
                </otherwise>
            </choose>
            <if test="merchantCode != null ">
                and ua.merchantCode = #{merchantCode}
            </if>
            <if test="merchantType != null and merchantType != ''">
                and mbi.merchantType = #{merchantType}
            </if>
            <if test="userId!=null and userId!=''">
                <choose>
                    <when test="sameIp != null and sameIp != ''">
                        and ua1.userId = #{userId}
                    </when>
                    <otherwise>
                        and ua.userId = #{userId}
                    </otherwise>
                </choose>
            </if>
            <choose>
                <when test="mcsStatus == '2'.toString()">
                    and (mcs.status = '2' or (mcs.status = '0' and mcs.thirdStatus = '-100'))
                </when>
                <otherwise>
                    <if test="mcsStatus!=null and mcsStatus!=''">
                        and mcs.status = #{mcsStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="thirdStatus == '100'.toString()">
                    and mcs.thirdStatus <![CDATA[<>]]> '-100'
                </when>
                <otherwise>
                    <if test="thirdStatus != null and thirdStatus != ''">
                        and mcs.thirdStatus = #{thirdStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="ppStatus == '2'.toString()">
                    and (pp.status = '2' or (pp.status = '0' and pp.thirdStatus = '-100'))
                </when>
                <otherwise>
                    <if test="ppStatus!=null and ppStatus!=''">
                        and pp.status = #{ppStatus}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="ppThirdStatus == '100'.toString()">
                    and pp.thirdStatus <![CDATA[<>]]> '-100'
                </when>
                <otherwise>
                    <if test="ppThirdStatus != null and ppThirdStatus != ''">
                        and pp.thirdStatus = #{ppThirdStatus}
                    </if>
                </otherwise>
            </choose>
            <if test="parentPhone!=null and parentPhone!=''">
                and pui.phone LIKE CONCAT("%",#{parentPhone},"%")
            </if>
            <if test="inviteNum!=null and inviteNum!=''">
                and ua.inviteNum LIKE CONCAT("%",#{inviteNum},"%")
            </if>
            <if test="alias!=null and alias!=''">
                and mbi.alias LIKE CONCAT("%",#{alias},"%")
            </if>
            <if test="userName!=null and userName!=''">
                and ua.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="phone!=null and phone!=''">
                and ui.phone LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="contactMobile!=null and contactMobile!=''">
                and mbi.contactMobile LIKE CONCAT("%",#{contactMobile},"%")
            </if>
            <if test="status!=null">
                and ua.status=#{status}
            </if>
            <if test="parentUserType != null and parentUserType != ''">
                <choose>
                    <when test="parentUserType == 'agent_agency'">
                        and pua.userId IN (SELECT ua.userId
                        FROM tb_pluss_user_app ua
                        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
                        WHERE ua.userType = 'agent' and pua.userId is not null)
                    </when>
                    <otherwise>
                        <choose>
                            <when test="parentUserType == 'agent'">
                                and pua.userId IN (SELECT ua.userId
                                FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
                                WHERE ua.userType = 'agent' and pua.userId is null)
                            </when>
                            <otherwise>
                                and pua.userType=#{parentUserType}
                            </otherwise>
                        </choose>
                    </otherwise>
                </choose>
            </if>
        </where>
    </select>

    <select id="queryUserInfoVONoPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(*)
        from tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui
        ON ua.userId=ui.id
        LEFT JOIN tb_pluss_user_info pui ON pui.id = ua.parentId
        LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
        LEFT JOIN tb_pluss_merchant_bank_card mb
        ON mb.userId=ua.userId
        LEFT JOIN tb_pluss_merchant_base_info mbi
        on mbi.merchantCode = ua.merchantCode
        <where>
            <if test="merchantCode != null ">
                and ua.merchantCode in (SELECT merchantCode
                FROM tb_pluss_merchant_base_info
                WHERE bindingCode =(SELECT bindingCode
                FROM tb_pluss_merchant_base_info
                where merchantCode = #{merchantCode}))
            </if>
            <if test="userId!=null and userId!=''">
                and ua.userId = #{userId}
            </if>
            <if test="parentPhone!=null and parentPhone!=''">
                and pui.phone LIKE CONCAT("%",#{parentPhone},"%")
            </if>
            <if test="inviteNum!=null and inviteNum!=''">
                and ua.inviteNum LIKE CONCAT("%",#{inviteNum},"%")
            </if>
            <if test="alias!=null and alias!=''">
                and mbi.alias LIKE CONCAT("%",#{alias},"%")
            </if>

            <if test="userName!=null and userName!=''">
                and ua.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="phone!=null and phone!=''">
                and ui.phone LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="contactMobile!=null and contactMobile!=''">
                and mbi.contactMobile LIKE CONCAT("%",#{contactMobile},"%")
            </if>
            <if test="status!=null">
                and ua.status=#{status}
            </if>
            <if test="userType!=null and userType!=''">
                and ua.userType=#{userType}
            </if>
            <if test="parentUserType!=null">
                and pua.userType=#{parentUserType}
            </if>
        </where>
    </select>

    <!-- 未实名认证信息 -->
    <select id="queryUserAppNoRealNamePage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.vo.UserExpandVO">
        select ua.id, ua.userId, ua.userName, ui.phone, ua.logo, ua.createDt
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where (ua.status = '0' or ua.status = '1' or ua.status = '2' or ua.status is null)
          and ua.parentId = #{parentId}
    </select>

    <!-- 未实名认证信息统计条数 -->
    <select id="queryUserAppNoRealNamePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(ua.id)
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where (ua.status = '0' or ua.status = '1' or ua.status = '2' or ua.status is null)
          and ua.parentId = #{parentId}
    </select>


    <!-- 已实名认证信息 -->
    <select id="queryUserAppRealNamePage" parameterType="java.util.Map" resultType="cn.pluss.platform.vo.UserExpandVO">
        select ua.id, ua.userId, ua.userName, ui.phone, ua.logo, ua.createDt
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where ua.status = '3'
          and ua.parentId = #{parentId}
    </select>

    <!-- 已实名认证信息统计条数 -->
    <select id="queryUserAppRealNamePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(ua.id)
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where ua.status = '3'
          and ua.parentId = #{parentId}
    </select>

    <!--是商户的信息  -->
    <select id="queryUserAppIsMerchantPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.vo.UserExpandVO">
        select ua.id, ua.userId, ua.userName, ui.phone, ua.logo, ua.createDt
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where ua.status = '3'
          and ua.parentId = #{parentId}
          and (ua.wechatStatus = '3' or ua.aliStatus = '3')
    </select>

    <!-- 是商户信息统计数量 -->
    <select id="queryUserAppIsMerchantPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(ua.id)
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where ua.status = '3'
          and ua.parentId = #{parentId}
          and (ua.wechatStatus = '3' or ua.aliStatus = '3')
    </select>


    <select id="queryUserAppListByPhone" parameterType="cn.pluss.platform.entity.UserApp"
            resultType="cn.pluss.platform.entity.UserApp">
        SELECT u.loginName,a.logo,a.userName,a.createDt,a.userId,a.status from tb_pluss_user_app a left
        join tb_pluss_user_info u on a.userId=u.id
        <where>
            instr(u.loginName, '@') = 0
            <if test="level!= null">
                and a.level = #{level}
            </if>
            <if test="status!= null">
                and a.status = #{status}
            </if>
            <if test="parentId!= null">
                and a.parentId = #{parentId}
            </if>
            <if test="agentStaffId!= null">
                and a.agentStaffId = #{agentStaffId}
            </if>
            <if test="parentIdList!= null">
                and a.parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="loginName!= null">
                and u.loginName is not null and u.loginName > '13000000000' and length(u.loginName)=11 AND u.loginName
                NOT LIKE '%@%'
            </if>
        </where>
    </select>


    <select id="queryUserAppPageByPhone" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.UserApp">
        SELECT u.loginName,a.logo,a.userName,a.createDt,a.userId,a.status,a.level,a.parentId from
        tb_pluss_user_app a left join tb_pluss_user_info u on a.userId=u.id
        <where>
            <if test="level!= null">
                and a.level = #{level}
            </if>
            <if test="status!= null">
                and a.status = #{status}
            </if>
            <if test="parentId!= null">
                and a.parentId = #{parentId}
            </if>
            <if test="agentStaffId!= null">
                and a.agentStaffId = #{agentStaffId}
            </if>
            <if test="parentIdList!= null">
                and a.parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="loginName!= null">
                and u.loginName is not null and u.loginName > '13000000000' and length(u.loginName)=11 AND u.loginName
                NOT LIKE '%@%'
            </if>
            <if test="merchantName!= null">
                and userId in (select userId from tb_pluss_merchant_base_info where alias like
                CONCAT('%',#{merchantName},'%'))
            </if>
            <if test="startTime!= null">
                and createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
        </where>
        order by a.createDt desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryUserAppPageCountByPhone" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_user_app a left join tb_pluss_user_info u on a.userId=u.id
        <where>
            <if test="level!= null">
                and a.level = #{level}
            </if>
            <if test="status!= null">
                and a.status = #{status}
            </if>
            <if test="parentId!= null">
                and a.parentId = #{parentId}
            </if>
            <if test="parentIdList!= null">
                and a.parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="agentStaffId!= null">
                and a.agentStaffId = #{agentStaffId}
            </if>
            <if test="loginName!= null">
                and u.loginName is not null and u.loginName > '13000000000' and length(u.loginName)=11 AND u.loginName
                NOT LIKE '%@%'
            </if>
            <if test="startTime!= null">
                and createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
            <if test="merchantName!= null">
                and userId in (select userId from tb_pluss_merchant_base_info where alias like
                CONCAT('%',#{merchantName},'%'))
            </if>
        </where>
    </select>

    <select id="queryUserAppByInviteNum" parameterType="java.lang.String" resultType="cn.pluss.platform.entity.UserApp">
        SELECT ua.id,
               ua.userName  AS userName,
               ua.inviteNum AS inviteNum,
               ua.loginName AS loginName,
               ua.`level`   AS LEVEL,
               ui.phone     AS phone,
               ua.`status`  AS STATUS,
               ua.createDt  AS createDt,
               ua.userId    AS userId,
               ua.userType  AS userType,
               ua.parentId  AS parentId,
               ua.`sort`  AS sort,
               ua.`roleCode` AS roleCode,
               ua.`levelCode` AS levelCode,
               ua.`status` AS status,
               ua.`bankStatus` AS bankStatus
        FROM tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        where inviteNum = #{inviteNum}
    </select>

    <select id="getExPageUserApp" resultType="cn.pluss.platform.entity.UserApp">
        SELECT u.loginName,a.logo,a.userName,a.createDt,a.userId,channel.status,a.level,a.parentId,
        a.agentStaffId from
        tb_pluss_user_app a left join tb_pluss_user_info u on a.userId=u.id
        LEFT JOIN tb_pluss_merchant_channel_status channel ON channel.merchantCode = a.merchantCode
        <where>
            and a.userType = 'promoter'
            and instr(u.loginName, '@') = 0
            <if test="expandDto.level!= null">
                and a.level = #{expandDto.level}
            </if>
            <if test="parentIdList!= null">
                and a.parentId in
                <foreach collection="parentIdList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="expandDto.merchantName!= null">
                and userId in (select userId from tb_pluss_merchant_base_info where alias like
                CONCAT('%',#{expandDto.merchantName},'%'))
            </if>
            <if test="expandDto.agentStaffId!= null">
                and agentStaffId= #{expandDto.agentStaffId}
            </if>
            <if test="expandDto.startTime!= null">
                and createDt &gt; #{expandDto.startTime}
            </if>
            <if test="expandDto.endTime!= null">
                and createDt &lt; #{expandDto.endTime}
            </if>
            <if test="expandDto.type != null and expandDto.type != ''">
                and
                <choose>
                    <when test='expandDto.type == "1"'>
                        a.status != 3
                    </when>
                    <when test='expandDto.type == "2"'>
                        a.status = 3
                    </when>
                    <when test='expandDto.type == "3"'>
                        a.status = 3 and channel.status = 3
                    </when>
                </choose>
            </if>
        </where>
        order by a.createDt DESC
    </select>

    <select id="selectCustomList" resultType="cn.pluss.platform.entity.UserApp">
        SELECT DISTINCT *
        FROM (
             SELECT tpui.loginName,
                    tpua.userId,
                    userName,
                    tpua.logo,
                    tpua.createDt,
                    tpua.merchantCode,
                    tpmbi.merchantName,
                    tpmbi.alias,
                    tpmcs.`status`,
                    tpua.parentId,
                    tpua.agentStaffId
             FROM tb_pluss_user_app tpua
                      LEFT JOIN tb_pluss_user_info tpui ON tpua.userId = tpui.id
                      LEFT JOIN tb_pluss_merchant_base_info tpmbi ON tpua.userId = tpmbi.userId
                      LEFT JOIN tb_pluss_merchant_channel_status tpmcs ON tpmcs.merchantCode = tpua.merchantCode
             ) t ${ew.customSqlSegment}
    </select>

    <select id="queryUserAppJoinById" parameterType="java.lang.Long" resultType="cn.pluss.platform.entity.UserApp">
        SELECT pua.id,
               pua.userName,
               ui.loginName,
               pua.status,
               pua.merchantCode,
               pua.userId,
               id.id AS 'idCard.id', id.certNo AS 'idCard.certNo', id.imgPositive AS 'idCard.imgPositive', id.imgNegative AS 'idCard.imgNegative', id.certStartTime AS 'idCard.certStartTime', id.certEndTime AS 'idCard.certEndTime', pbc.bankCardNo AS 'bankCard.bankCardNo', pbc.branchName AS 'bankCard.branchName', pbc.imgUrl AS 'bankCard.imgUrl', pbc.phone AS 'bankCard.phone', pbc.bankName AS 'bankCard.bankName', pbc.branchProvince AS 'bankCard.branchProvince', pbc.branchCity AS 'bankCard.branchCity', pbc.branchArea AS 'bankCard.branchArea', pmai.id AS 'merchantBaseInfo.id', pmai.merchantName AS 'merchantBaseInfo.merchantName', pmai.merchantType AS 'merchantBaseInfo.merchantType', pmai.alias AS 'merchantBaseInfo.alias', pmai.address AS 'merchantBaseInfo.address', pmai.contactName AS 'merchantBaseInfo.contactName', pmai.email AS 'merchantBaseInfo.email', pmai.contactMobile AS 'merchantBaseInfo.contactMobile', pmai.aliAccount AS 'merchantBaseInfo.aliAccount', pmai.mcc AS 'merchantBaseInfo.mcc', pmai.mccName AS 'merchantBaseInfo.mccName', pmai.province AS 'merchantBaseInfo.province', pmai.city AS 'merchantBaseInfo.city', pmai.district AS 'merchantBaseInfo.district', pmai.principalPerson AS 'merchantBaseInfo.principalPerson', pmai.bussAuthName AS 'merchantBaseInfo.bussAuthName', pmai.bussAuthNum AS 'merchantBaseInfo.bussAuthNum', pmai.bussAuthAddress AS 'merchantBaseInfo.bussAuthAddress', pmai.bussAuthStartTime AS 'merchantBaseInfo.bussAuthStartTime', pmai.bussAuthEndTime AS 'merchantBaseInfo.bussAuthEndTime', pp.thirdStatus AS ppThirdStatus,
               pp.`status` AS ppStatus
        FROM tb_pluss_user_app pua
                 left join tb_pluss_merchant_base_info pmai on pua.merchantCode = pmai.merchantCode
                 left join tb_pluss_bank_card pbc on pbc.userId = pua.userId
                 left join tb_pluss_id_card id on id.userId = pua.userId
                 LEFT JOIN tb_pluss_user_info ui ON pua.userId = ui.id
                 LEFT JOIN (SELECT *
                            FROM tb_pluss_merchant_plugin
                            WHERE userId = #{userId}
                            ORDER BY createTime DESC LIMIT 1) pp
                           ON pua.userId = pp.userId
        where pua.userId = #{userId}
          AND id.userType = "01"
          AND pbc.accountType = '03'
    </select>

    <select id="queryUserAppJoinByUserId" parameterType="java.lang.Long" resultType="cn.pluss.platform.entity.UserApp">
        SELECT pua.*,
               pmai.id AS 'merchantBaseInfo.id', pmai.userId AS 'merchantBaseInfo.userId', pmai.merchantName AS 'merchantBaseInfo.merchantName', pmai.merchantType AS 'merchantBaseInfo.merchantType', pmai.alias AS 'merchantBaseInfo.alias', pmai.province AS 'merchantBaseInfo.province', pmai.city AS 'merchantBaseInfo.city', pmai.district AS 'merchantBaseInfo.district', pmai.address AS 'merchantBaseInfo.address', pmai.contactName AS 'merchantBaseInfo.contactName', pmai.email AS 'merchantBaseInfo.email', pmai.contactMobile AS 'merchantBaseInfo.contactMobile', pmai.aliAccount AS 'merchantBaseInfo.aliAccount', pmbc.id AS 'merchantBankCard.id', pmbc.merchantCode AS 'merchantBankCard.merchantCode', pmbc.storeId AS 'merchantBankCard.storeId', pmbc.bankCardNo AS 'merchantBankCard.bankCardNo', pmbc.bankAddressNo AS 'merchantBankCard.bankAddressNo', pmbc.bankCertName AS 'merchantBankCard.bankCertName', pmbc.bankName AS 'merchantBankCard.bankName', pmbc.branchName AS 'merchantBankCard.branchName', pmbc.accountType AS 'merchantBankCard.accountType', pmbc.contactLine AS 'merchantBankCard.contactLine', pmbc.certNo AS 'merchantBankCard.certNo', pmbc.phone AS 'merchantBankCard.phone', pmbc.certFullPhoto AS 'merchantBankCard.certFullPhoto', pmbc.certBacKPhoto AS 'merchantBankCard.certBacKPhoto', pmbc.bankCardFrontPic AS 'merchantBankCard.bankCardFrontPic'
        FROM tb_pluss_user_app pua
                 left join tb_pluss_merchant_base_info pmai on pua.merchantCode = pmai.merchantCode
                 left join tb_pluss_merchant_bank_card pmbc on pua.merchantCode = pmbc.merchantCode
        where pua.userId = #{userId}
    </select>

    <select id="selectUserAppWithChannelStatusCount" resultType="java.lang.Integer">
        SELECT count(DISTINCT userId)
        FROM (
                 SELECT t1.userId,
                        t1.userType,
                        t1.logo,
                        t1.level,
                        t1.createDt,
                        t1.status,
                        t1.parentId,
                        t1.bankStatus,
                        t1.agentStaffId,
                        t2.status merchantAuditStatus
                 FROM tb_pluss_user_app t1
                     LEFT JOIN tb_pluss_user_info ui ON t1.userId = ui.id
                          LEFT JOIN tb_pluss_merchant_channel_status t2 ON t1.merchantCode = t2.merchantCode
                    WHERE ui.loginName NOT LIKE '%@%'
             ) t ${ew.customSqlSegment}
    </select>

    <select id="selectListWithChannelStatus" resultType="cn.pluss.platform.entity.UserApp">
        SELECT *
        FROM (
                 SELECT t1.*, t2.status merchantAuditStatus
                 FROM tb_pluss_user_app t1
                          LEFT JOIN tb_pluss_merchant_channel_status t2 ON t1.merchantCode = t2.merchantCode
             ) t
            ${ew.customSqlSegment}
    </select>

    <select id="getUserAppInfoByParentId" resultType="cn.pluss.platform.entity.UserApp">
        SELECT ua.status,
               CONCAT(LEFT(ui.loginName, 3),'****', RIGHT(ui.loginName, 4)) as 'loginName', ua.createDt
        FROM tb_pluss_user_app ua
                 left join tb_pluss_user_info ui on ua.userId = ui.id
        where ua.parentId = #{userId}
          and ua.userType = 'promoter'
          AND instr(ui.loginName, '@') = 0
        order By ua.createDt desc
    </select>

    <select id="pageData" parameterType="cn.pluss.platform.entity.UserApp"
            resultType="cn.pluss.platform.entity.UserApp">
        SELECT ua.status,
        CONCAT(LEFT(ui.loginName, 3),'****',RIGHT(ui.loginName, 4)) as 'loginName',
        ua.createDt , cs.`status` AS merchantAuditStatus
        FROM tb_pluss_user_app ua
        left join tb_pluss_user_info ui on ua.userId = ui.id
        left join tb_pluss_merchant_channel_status cs on ua.merchantCode = cs.merchantCode
        where ua.parentId = #{userApp.userId} and ua.userType = 'promoter' AND instr(ui.loginName,'@') = 0
        <if test="userApp.startDate != null">
            <![CDATA[ and DATE_FORMAT(ua.createDt,'%Y-%m-%d')  >= DATE_FORMAT(#{userApp.startDate},'%Y-%m-%d')]]>
        </if>
        <if test="userApp.endDate != null">
            <![CDATA[ and DATE_FORMAT(ua.createDt,'%Y-%m-%d')  <= DATE_FORMAT(#{userApp.endDate},'%Y-%m-%d')]]>
        </if>
        GROUP BY ua.merchantCode
        order By ua.createDt desc
    </select>


    <select id="getSubUserCount" resultType="java.lang.Integer">
        SELECT count( 1 ) FROM tb_pluss_user_app ua LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        WHERE
        ua.parentId = #{userId}
        AND userType = 'promoter'
        AND instr( ui.loginName, '@' ) = 0
        <if test="startDate != null and startDate != ''">
            <![CDATA[ and DATE_FORMAT(ua.createDt,'%Y-%m-%d')  >= #{startDate}]]>
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[ and DATE_FORMAT(ua.createDt,'%Y-%m-%d')  <= #{endDate}]]>
        </if>
    </select>

    <select id="queryActPage" parameterType="cn.pluss.platform.vo.UserInfoVO"
            resultType="cn.pluss.platform.vo.UserInfoVO">
        SELECT
        t4.userId,
        t4.nextRegisterCount,
        t4.nextCertCount,
        t4.nextIncomingCount,
        ua.id,
        ua.userName,
        ua.`level`,
        mbi.alias,
        ui.loginName,
        pui.phone AS parentPhone,
        mbi.contactMobile,
        ua.createDt,
        ua.`status`
        FROM
        (
        SELECT
        t3.userId,
        SUM( t3.nextCount ) AS nextRegisterCount,
        SUM( IF ( t3.cStatus = '2', t3.nextCount, 0 )) AS nextCertCount,
        SUM( IF ( t3.cStatus = '3', t3.nextCount, 0 )) AS nextIncomingCount
        FROM
        (
        SELECT
        ua.userId,
        IFNULL( t2.count, 0 ) AS nextCount,
        t2.`status` AS cStatus
        FROM
        tb_pluss_user_app ua
        LEFT JOIN (
        SELECT
        t.parentId,
        t.`status`,
        count( 1 ) count
        FROM
        (
        SELECT
        ua.parentId,
        IF
        ( ua.bankStatus = 3, IF ( mcs.`status` = 3, 3, 2 ), 1 ) `status`
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON ua.merchantCode = mcs.merchantCode
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        WHERE
        INSTR( ui.loginName, '@' ) = 0
        AND DATE_FORMAT(ua.createDt,'%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{userInfoVO.startTime},'%Y-%m-%d')
        AND DATE_FORMAT(ua.createDt,'%Y-%m-%d')  <![CDATA[ <= ]]> DATE_FORMAT(#{userInfoVO.endTime},'%Y-%m-%d')
        AND ua.userType = 'promoter'
        ) t

        GROUP BY
        t.parentId,
        t.`status`
        ) t2 ON ua.userId = t2.parentId
        ) t3
        GROUP BY
        t3.userId
        ) t4
        LEFT JOIN tb_pluss_user_app ua ON t4.userId = ua.userId
        LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_user_info ui ON ui.id = t4.userId
        LEFT JOIN tb_pluss_user_info pui ON ua.parentId = pui.id
        <if test="userInfoVO.userId != null">
            and t3.userId = #{userInfoVO.userId}
        </if>
        <if test="userInfoVO.userName != null and userInfoVO.userName != ''">
            and t3.userName like CONCAT('%',#{userInfoVO.userName},'%')
        </if>
        <if test="userInfoVO.loginName != null and userInfoVO.loginName !=''">
            and ui.loginName = #{userInfoVO.loginName}
        </if>
        <if test="userInfoVO.alias != null and userInfoVO.alias != ''">
            and mbi.alias like CONCAT('%',#{userInfoVO.alias},'%')
        </if>
        <if test="userInfoVO.contactMobile != null and userInfoVO.contactMobile != ''">
            and mbi.contactMobile = #{userInfoVO.contactMobile}
        </if>
        <if test="userInfoVO.parentPhone != null and userInfoVO.parentPhone != ''">
            and pui.phone = #{userInfoVO.parentPhone}
        </if>
        ORDER BY
        nextRegisterCount DESC
    </select>

    <select id="getNotificationMenuId" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT id
        FROM `tb_pluss_menu_info`
        where link = #{link}
    </select>

    <select id="getParentUserId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(pua.userId)
        from tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
        where ua.userId = #{parentUserId}
    </select>

    <select id="getUserId" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT ua.userId
        FROM tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON ua.merchantCode = mcs.merchantCode
        WHERE mcs.merchantCode = #{merchantCode}
          AND ua.userType = 'promoter'
          AND mcs.merchantId IS NULL
    </select>

    <!-- 根据userId获取用户的所有上级 -->
    <select id="getUserAppParentsByUserId" resultType="java.util.Map">
        SELECT ua.userId    as nowUserId,
               ua.LEVEL     AS nowLevel,
               ua.userType  as nowType,
               ua.pIdArr    AS nowPidArr,

               ua1.userId   as prevOneUserId,
               ua1.LEVEL    AS prevOneLevel,
               ua1.userType as prevOneType,
               ua1.pIdArr   AS prevOnePidArr,

               ua2.userId   as prevTwoUserId,
               ua2.LEVEL    AS prevTwoLevel,
               ua2.userType as prevTwoType,
               ua2.pIdArr   AS prevTwoPidArr,

               ua3.userId   AS prevThreeUserId,
               ua3.LEVEL    AS prevThreeLevel,
               ua3.userType as prevThreeType,
               ua3.pIdArr   AS prevThreePidArr,

               ua4.userId   AS prevFourUserId,
               ua4.LEVEL    AS prevFourLevel,
               ua4.userType as prevFourType,
               ua4.pIdArr   AS prevFourPidArr
        FROM tb_pluss_user_app ua
                 LEFT JOIN tb_pluss_user_app ua1 ON ua.parentId = ua1.userId
                 LEFT JOIN tb_pluss_user_app ua2 ON ua1.parentId = ua2.userId
                 LEFT JOIN tb_pluss_user_app ua3 ON ua2.parentId = ua3.userId
                 LEFT JOIN tb_pluss_user_app ua4 ON ua3.parentId = ua4.userId
        WHERE ua.userId = #{userId};
    </select>

    <select id="getPlatformData" resultType="java.util.Map">
        SELECT
            SUM(IF(type = 'toDayPromoter',num,0)) AS addToDayPromoterNum,
            SUM(IF(type = 'toDayMember',num,0))AS addToDayMemberNum,
            SUM(IF(type = 'toDayStore',num,0)) AS addToDayStoreNum,
            SUM(IF(type = 'mercNum',num,0)) AS mercCountNum,
            SUM(IF(type = 'fansNum',num,0)) AS fansCountNum,
            SUM(IF(type = 'promNum',num,0)) AS promCountNum,
            SUM(IF(type = 'memberNum',num,0)) AS memberCountNum

        FROM (

            SELECT count(1) AS num, 'toDayPromoter' as type, 'add' as addType FROM tb_pluss_user_app WHERE date(createDt) = curdate()

            union

            SELECT count(1) AS num, 'toDayMember' AS type,'add' as addType  FROM tb_pluss_merchant_menber WHERE date(createDt) = curdate()

            union

            SELECT count(1) AS num, 'toDayStore' AS type,'add' as addType FROM tb_pluss_merchant_store WHERE date(createDt) = curdate()

            union

            SELECT count(1) AS num, 'mercNum' AS type,'add' as addType FROM tb_pluss_merchant_base_info

            union

            SELECT count(1) AS num, 'fansNum' AS type,'add' as addType FROM tb_pluss_real_fans

            union

            SELECT count(1) AS num, 'promNum' AS type,'add' as addType FROM tb_pluss_user_app

            union

            SELECT count(1) AS num, 'memberNum' AS type,'add' as addType FROM tb_pluss_merchant_menber

        )as
        a GROUP BY addType
    </select>

    <select id="selectMerchantSummaryPage" resultType="cn.pluss.platform.vo.MerchSummaryVO">
        SELECT
            ua.createDt createTime,
            ua.userId,
            mbi.alias,
            mbi.merchantCode,
            IFNULL(mos.count, 0) count,
            IFNULL(mos.totalFee, 0.00) totalFee,
            IFNULL(mos.retFee, 0.00) retFee,
            IFNULL(mos.retCount, 0) retCount,
            IFNULL(mcsD1.`status`, "-1") `d1Status`,
            IFNULL(mcsD1.`thirdStatus`, "-1") `d1thirdStatus`,
            mcsD1.`remark` `d1Remark`,
            IFNULL(mcsD0.`status`, "-1") `d0Status`,
            IFNULL(mcsD0.`thirdStatus`, "-1") `d0thirdStatus`,
            mcsD0.`remark` `d0Remark`,
            ms.`status` `d1StoreStatus`,
            ms.`status` `d0StoreStatus`,
            IFNULL(mcsD1.`authorizationStatus`, '0') `d1WxCertStatus`,
            IFNULL(mcsD0.`authorizationStatus`, '0') `d0WxCertStatus`,
            IF(ua.parentId = #{condition.userId}, "1", "0") directFlag
        FROM
            (
                <if test="condition.userId != null and condition.userId != ''">
                    <if test="condition.type != null and condition.type != ''">
                        <choose>
                            <when test='"1" == condition.type'>
                                SELECT ua.userId, ua.createDt, ua.merchantCode, ua.parentId, ua.agentStaffId FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app ua1 ON ua1.userId = ua.parentId WHERE ua1.userId = #{condition.userId}
                            </when>
                            <when test='"2" == condition.type'>
                                SELECT ua.userId, ua.createDt, ua.merchantCode, ua.parentId, ua.agentStaffId FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app ua1 ON ua1.userId = ua.parentId
                                LEFT JOIN tb_pluss_user_app ua2 ON ua2.userId = ua1.parentId WHERE ua2.userId = #{condition.userId}
                            </when>
                            <otherwise>
                                SELECT ua.userId, ua.createDt, ua.merchantCode, ua.parentId, ua.agentStaffId FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app ua1 ON ua1.userId = ua.parentId WHERE ua1.userId = #{condition.userId}
                                UNION
                                SELECT ua.userId, ua.createDt, ua.merchantCode, ua.parentId, ua.agentStaffId FROM tb_pluss_user_app ua
                                LEFT JOIN tb_pluss_user_app ua1 ON ua1.userId = ua.parentId
                                LEFT JOIN tb_pluss_user_app ua2 ON ua2.userId = ua1.parentId WHERE ua2.userId = #{condition.userId}
                            </otherwise>
                        </choose>
                    </if>
                </if>
            ) ua
            LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = ua.userId
            LEFT JOIN tb_pluss_merchant_store ms ON ms.merchantCode = ua.merchantCode
            LEFT JOIN
                tb_pluss_merchant_channel_status mcsD1
                ON mcsD1.merchantCode = mbi.merchantCode AND (mcsD1.channel IN (1,3) OR mcsD1.channel IS NULL)
            LEFT JOIN
                tb_pluss_merchant_channel_status mcsD0
                ON mcsD0.merchantCode = mbi.merchantCode AND (mcsD0.channel IN (4) OR mcsD0.channel IS NULL)
            LEFT JOIN (
            SELECT
                merchantCode,
                SUM( count ) count,
                SUM( totalFee ) totalFee,
                SUM( retFee ) retFee,
                SUM( retCount ) retCount
            FROM
                tb_pluss_merchant_order_statistics mos
                WHERE mos.`startDate` = '1970-01-01'
            GROUP BY
                merchantCode
            ) mos ON mos.merchantCode = mbi.merchantCode
        WHERE (mcsD1.`status` IS NOT NULL OR mcsD0.`status` IS NOT NULL)
        <if test="condition.merchantName != null and condition.merchantName != ''">
            AND mbi.alias LIKE CONCAT('%', #{condition.merchantName}, '%')
        </if>
        <if test='condition.agentStaffIds != null and condition.agentStaffIds.size() != 0'>
            AND ua.agentStaffId IN
            <foreach collection="condition.agentStaffIds" close=")" open="(" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="condition.startTime != null">
            AND ua.createDt &gt;= #{condition.startTime}
        </if>
        <if test="condition.endTime != null">
            AND ua.createDt &lt;= #{condition.endTime}
        </if>
    </select>

    <select id="selectBillCountAndTotalFee" resultType="cn.pluss.platform.vo.MerchSummaryVO">
        SELECT
            IFNULL(SUM(mos.count), 0) `count`,
            IFNULL(SUM(mos.totalFee), 0.00) totalFee
        FROM (
            <if test="condition.userId != null and condition.userId != ''">
                <if test="condition.type != null and condition.type != ''">
                    <choose>
                        <when test='"1" == condition.type'>
                            SELECT ua.* FROM tb_pluss_user_app ua
                            WHERE ua.parentId = #{condition.userId}
                        </when>
                        <when test='"2" == condition.type'>
                            SELECT ua1.* FROM tb_pluss_user_app ua
                            LEFT JOIN tb_pluss_user_app ua1 ON ua1.parentId = ua.userId
                            WHERE ua.parentId = #{condition.userId}
                            UNION
                            SELECT ua2.* FROM tb_pluss_user_app ua
                            LEFT JOIN tb_pluss_user_app ua1 ON ua1.parentId = ua.userId
                            LEFT JOIN tb_pluss_user_app ua2 ON ua2.parentId = ua1.userId
                            WHERE ua.parentId = #{condition.userId}
                        </when>
                        <otherwise>
                            SELECT ua.* FROM tb_pluss_user_app ua
                            WHERE ua.parentId = #{condition.userId}
                            UNION
                            SELECT ua1.* FROM tb_pluss_user_app ua
                            LEFT JOIN tb_pluss_user_app ua1 ON ua1.parentId = ua.userId
                            WHERE ua.parentId = #{condition.userId}
                            UNION
                            SELECT ua2.* FROM tb_pluss_user_app ua
                            LEFT JOIN tb_pluss_user_app ua1 ON ua1.parentId = ua.userId
                            LEFT JOIN tb_pluss_user_app ua2 ON ua2.parentId = ua1.userId
                            WHERE ua.parentId = #{condition.userId}
                        </otherwise>
                    </choose>
                </if>
            </if>
        ) ua
        LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = ua.userId
        LEFT JOIN tb_pluss_merchant_store ms ON ms.merchantCode = ua.merchantCode
        LEFT JOIN tb_pluss_merchant_channel_status mcsD1 ON mcsD1.merchantCode = mbi.merchantCode AND mcsD1.channel IN (1,3)
        LEFT JOIN tb_pluss_merchant_channel_status mcsD0 ON mcsD0.merchantCode = mbi.merchantCode AND mcsD0.channel IN (4)
        LEFT JOIN (
        SELECT
            merchantCode,
            SUM( count ) count,
            SUM( totalFee ) totalFee,
            SUM( retFee ) retFee,
            SUM( retCount ) retCount
        FROM
            tb_pluss_merchant_order_statistics
        WHERE startDate = '1970-01-01'
        GROUP BY
            merchantCode
        ) mos ON mos.merchantCode = mbi.merchantCode
        WHERE (mcsD1.`status` IN (1, 2, 3) OR mcsD0.`status` IN (1, 2, 3))
        <if test="condition.merchantName != null and condition.merchantName != ''">
            AND mbi.alias LIKE CONCAT('%', #{condition.merchantName}, '%')
        </if>
        <if test="condition.userId != null and condition.userId != ''">
            <if test="condition.type != null and condition.type != ''">

            </if>
        </if>
        <if test='condition.agentStaffIds != null and condition.agentStaffIds.size() != 0'>
            AND ua.agentStaffId IN
            <foreach collection="condition.agentStaffIds" close=")" open="(" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="condition.startTime != null">
            AND ua.createDt &gt;= #{condition.startTime}
        </if>
        <if test="condition.endTime != null">
            AND ua.createDt &lt;= #{condition.endTime}
        </if>
    </select>

    <select id="getMaxSort" resultType="java.lang.Integer">
        SELECT
            MAX(sort)
        FROM
         tb_pluss_user_app
    </select>

    <select id="getUserNumInfo" resultType="cn.pluss.platform.vo.UserNumVO">
        SELECT
            IFNULL(SUM(t.totalNum), 0) as totalNum,
            IFNULL(SUM(IF(t.createDate = #{date},1,0)),0) AS addNum
        FROM (
             SELECT
                 count(1) as totalNum,
                 <if test="dateType == null or dateType == 1">
                     DATE_FORMAT(createDt, '%Y-%m-%d' ) AS createDate
                 </if>
                 <if test="dateType == 2">
                    DATE_FORMAT(createDt, '%Y-%m' ) AS createDate
                 </if>
                <if test="dateType == 3">
                    DATE_FORMAT(createDt, '%Y' ) AS createDate
                </if>
             FROM tb_pluss_user_app WHERE parentId = #{userId} and userType not in ('staff','agent_staff')
             GROUP BY
            <if test="dateType == null or dateType == 1">
                DATE_FORMAT(createDt, '%Y-%m-%d')
            </if>
            <if test="dateType == 2">
                DATE_FORMAT(createDt, '%Y-%m')
            </if>
            <if test="dateType == 3">
                DATE_FORMAT(createDt, '%Y')
            </if>
         ) t
    </select>

    <select id="getUserInfoByPhone" resultType="cn.pluss.platform.entity.UserApp">
        SELECT ua.*,ui.phone FROM tb_pluss_user_app ua
        left join tb_pluss_user_info ui on ua.userId = ui.id
        where ui.phone = #{phone} and userType not in ('agent_staff','staff') AND LENGTH(ui.phone) = 11
    </select>
    <select id="selectUserAppWithChannelStatusCountV2" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_promotion up ON ua.userId = up.parent_user_id
        WHERE
        up.parent_user_id = #{userId}
        AND up.type_code = 'MC'
        AND up.create_time <![CDATA[ >= ]]> #{startTime}
        AND up.create_time <![CDATA[ <= ]]> #{endTime}
    </select>
</mapper>
