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

    <select id="queryMerchantBaseInfoPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.MerchantBaseInfo">
        SELECT * from tb_pluss_merchant_base_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName like CONCAT("%",#{merchantName},"%")
            </if>
            <if test="merchantType!= null">
                and merchantType = #{merchantType}
            </if>
            <if test="mcc!= null">
                and mcc = #{mcc}
            </if>
            <if test="alias!= null">
                and alias = #{alias}
            </if>
            <if test="contactMobile!= null">
                and contactMobile = #{contactMobile}
            </if>
            <if test="contactName!= null">
                and contactName = #{contactName}
            </if>
            <if test="province!= null">
                and province = #{province}
            </if>
            <if test="city!= null">
                and city = #{city}
            </if>
            <if test="district!= null">
                and district = #{district}
            </if>
            <if test="address!= null">
                and address = #{address}
            </if>
            <if test="email!= null">
                and email = #{email}
            </if>
            <if test="principalMobile!= null">
                and principalMobile = #{principalMobile}
            </if>
            <if test="principalCertType!= null">
                and principalCertType = #{principalCertType}
            </if>
            <if test="principalCertNo!= null">
                and principalCertNo = #{principalCertNo}
            </if>
            <if test="principalPerson!= null">
                and principalPerson = #{principalPerson}
            </if>
            <if test="bussAuthNum!= null">
                and bussAuthNum = #{bussAuthNum}
            </if>
            <if test="certOrgCode!= null">
                and certOrgCode = #{certOrgCode}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="updateDt!= null">
                and updateDt = #{updateDt}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="bussAuthAddress!= null">
                and bussAuthAddress = #{bussAuthAddress}
            </if>
            <if test="bussAuthStartTime!= null">
                and bussAuthStartTime = #{bussAuthStartTime}
            </if>
            <if test="bussAuthEndTime!= null">
                and bussAuthEndTime = #{bussAuthEndTime}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="aliAccount!= null">
                and aliAccount = #{aliAccount}
            </if>
            <if test="aliMcc!= null">
                and aliMcc = #{aliMcc}
            </if>
            <if test="addressNo!= null">
                and addressNo = #{addressNo}
            </if>
            <if test="productDesc!= null">
                and productDesc = #{productDesc}
            </if>
            <if test="mccName!= null">
                and mccName = #{mccName}
            </if>
            <if test="bindingCode!= null">
                and bindingCode = #{bindingCode}
            </if>
            <if test="bussAuthName!= null">
                and bussAuthName = #{bussAuthName}
            </if>
            <if test="buslicType!= null">
                and buslicType = #{buslicType}
            </if>
            <if test="isVoice!= null">
                and isVoice = #{isVoice}
            </if>

        </where>
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMerchantBaseInfoPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_merchant_base_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null and merchantCode!=''">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null and merchantName!=''">
                and merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="merchantType!= null">
                and merchantType = #{merchantType}
            </if>
            <if test="mcc!= null">
                and mcc = #{mcc}
            </if>
            <if test="authCode!= null">
                and authCode = #{authCode}
            </if>
            <if test="supportStage!= null">
                and supportStage = #{supportStage}
            </if>
            <if test="alias!= null">
                and alias = #{alias}
            </if>
            <if test="contactMobile!= null">
                and contactMobile = #{contactMobile}
            </if>
            <if test="contactName!= null">
                and contactName = #{contactName}
            </if>
            <if test="province!= null">
                and province = #{province}
            </if>
            <if test="city!= null">
                and city = #{city}
            </if>
            <if test="district!= null">
                and district = #{district}
            </if>
            <if test="address!= null">
                and address = #{address}
            </if>
            <if test="email!= null">
                and email = #{email}
            </if>
            <if test="legalPerson!= null">
                and legalPerson = #{legalPerson}
            </if>
            <if test="principalMobile!= null">
                and principalMobile = #{principalMobile}
            </if>
            <if test="principalCertType!= null">
                and principalCertType = #{principalCertType}
            </if>
            <if test="principalCertNo!= null">
                and principalCertNo = #{principalCertNo}
            </if>
            <if test="principalPerson!= null">
                and principalPerson = #{principalPerson}
            </if>
            <if test="bussAuthNum!= null">
                and bussAuthNum = #{bussAuthNum}
            </if>
            <if test="certOrgCode!= null">
                and certOrgCode = #{certOrgCode}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="updateDt!= null">
                and updateDt = #{updateDt}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="registerStatus!= null">
                and registerStatus = #{registerStatus}
            </if>
            <if test="bindingCode!= null">
                and bindingCode = #{bindingCode}
            </if>
            <if test="bussAuthAddress!= null">
                and bussAuthAddress = #{bussAuthAddress}
            </if>
            <if test="bussAuthStartTime!= null">
                and bussAuthStartTime = #{bussAuthStartTime}
            </if>
            <if test="bussAuthEndTime!= null">
                and bussAuthEndTime = #{bussAuthEndTime}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="aliAccount!= null">
                and aliAccount = #{aliAccount}
            </if>
            <if test="aliMcc!= null">
                and aliMcc = #{aliMcc}
            </if>
            <if test="addressNo!= null">
                and addressNo = #{addressNo}
            </if>
            <if test="productDesc!= null">
                and productDesc = #{productDesc}
            </if>
            <if test="mccName!= null">
                and mccName = #{mccName}
            </if>
            <if test="bussAuthName!= null">
                and bussAuthName = #{bussAuthName}
            </if>
            <if test="buslicType!= null">
                and buslicType = #{buslicType}
            </if>
            <if test="isVoice!= null">
                and isVoice = #{isVoice}
            </if>
        </where>
    </select>
    <select id="getByUserId" parameterType="java.lang.String" resultType="cn.pluss.platform.entity.MerchantBaseInfo">
    SELECT * from tb_pluss_merchant_base_info
        <where>
        userId=#{userId}
        </where>
    </select>

    <!-- 根据签约时间查询当前用户的推广商户 -->
    <select id="queryMerchantBaseInfoPageCountBySignDt" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*)
        from tb_pluss_merchant_base_info
        where signDt &gt;= #{startTime}
          and signDt &lt;= #{endTime}
          and promoterCode = #{promoterCode}
    </select>

    <select id="queryMerchantBaseNewPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.vo.MerchantBaseNewVO">
        SELECT
        info.alias,
        info.id,
        info.userId,
        info.merchantCode,
        info.userName,
        info.contactMobile AS phone,
        IFNULL( spreadCountTb.`count`, 0) yesterdaySpreadCount,
        IFNULL( orderCountTb.orderCount, 0) orderCount,
        IFNULL( orderCountTb.yesterdayOrderCount, 0) yesterdayOrderCount,
        IFNULL(trade.totalMoney,0.00) as earnings,
        IFNULL(trade.toDayEarnings,0.00) as toDayEarnings,
        IFNULL(trade.yestDayEarnings,0.00) as yestDayEarnings,
        IFNULL(member.totalMember,0) as menber,
        IFNULL(member.toDayMenber,0) as toDayMenber,
        IFNULL( member.yestDayMenber, 0 ) AS yestDayMenber,
        IFNULL(fans.yesterDayFansCount,0) as yesterDayFansCount,
        IFNULL(fans.toDayFansCount,0) as toDayFansCount,
        IFNULL(shape.fansMoney,0.00) as fansMoney,
        info.parentIdPhone,
        info.merchantName as merchantName,
        info.createDt
        FROM
        (SELECT
        mbi.id,
        mbi.alias,
        mbi.userId AS userId,
        mbi.merchantCode AS merchantCode,
        mbi.contactName as userName,
        mbi.contactMobile,
        mbi.merchantName,
        ui.phone AS parentIdPhone,
        mbi.createDt
        FROM
        tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_user_app ua
        ON mbi.userId=ua.userId
        LEFT JOIN tb_pluss_user_info ui
        ON ua.parentId = ui.id
        ) info
        LEFT JOIN
        (
        SELECT
        mo.merchantCode,SUM(mo.consumeFee) totalMoney,
        SUM(IFNULL((CASE WHEN to_days(mo.createDt) = TO_DAYS(now()) THEN mo.consumeFee end),0.00)) AS toDayEarnings,
        SUM(IFNULL((CASE WHEN to_days(mo.createDt) = TO_DAYS(DATE_SUB(now(),INTERVAL 1 DAY)) THEN mo.consumeFee
        end),0.00)) AS yestDayEarnings
        FROM tb_pluss_merchant_order mo WHERE mo.`status` = 1 GROUP BY mo.merchantCode) trade
        ON info.merchantCode = trade.merchantCode
        LEFT JOIN
        (SELECT
        mm.merchantCode,count(mm.id) AS totalMember,
        count(CASE WHEN to_days(mm.createDt) = to_days(now()) THEN mm.id end) AS toDayMenber,
        count(CASE WHEN (to_days(mm.createDt) + 1) = to_days(now()) THEN mm.id end) AS yestDayMenber
        FROM tb_pluss_merchant_menber mm GROUP BY mm.merchantCode
        ) member
        ON info.merchantCode = member.merchantCode
        LEFT JOIN
        (SELECT
        rf.enableMerchantCode,
        count(CASE WHEN to_days(NOW()) - to_days(rf.enableTime) <![CDATA[<=]]>  1 THEN rf.id end) as yesterDayFansCount,
        count(CASE WHEN to_days(rf.enableTime) = to_days(now()) THEN rf.id end) as toDayFansCount
        FROM tb_pluss_real_fans rf GROUP BY rf.enableMerchantCode ) fans
        ON info.merchantCode = fans.enableMerchantCode
        LEFT JOIN
        (SELECT pf.lastMerchantCode,ROUND(SUM(pf.shareMoney),2) AS fansMoney FROM tb_pluss_fans pf WHERE
        pf.lastMerchantCode is not null GROUP BY pf.lastMerchantCode ) as shape
        ON info.merchantCode = shape.lastMerchantCode
        LEFT JOIN (
        SELECT merchantCode, COUNT(1) orderCount, COUNT(IF(TO_DAYS(now()) = (TO_DAYS(createDt) + 1 ), 1, NULL))
        yesterdayOrderCount
        FROM tb_pluss_merchant_order GROUP BY merchantCode
        ) orderCountTb ON info.merchantCode = orderCountTb.merchantCode
        LEFT JOIN (
        SELECT parentId userId, COUNT(IF(TO_DAYS(now()) = (TO_DAYS(createDt) + 1 ), 1, NULL)) `count`
        FROM tb_pluss_user_app GROUP BY parentId
        ) spreadCountTb ON spreadCountTb.userId = info.userId
        <where>
            <if test="merchantCode!=null and merchantCode!=''">
                and info.merchantCode= #{merchantCode}
            </if>
            <if test="alias!=null and alias!=''">
                and info.alias like concat("%",#{alias},"%")
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and info.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="userName!=null and userName!=''">
                and info.userName LIKE CONCAT('%',#{userName},'%')
            </if>
            <if test="userId!=null">
                and info.userId = #{userId}
            </if>
            <if test="phone!=null and phone!=''">
                and info.contactMobile LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="parentIdPhone!=null and parentIdPhone!=''">
                and info.parentIdPhone LIKE CONCAT("%",#{parentIdPhone},"%")
            </if>
        </where>
        <choose>
            <when test="column != null and column != ''">
                order by ${column}
            </when>
            <otherwise>
                order by info.createDt
            </otherwise>
        </choose>
        <choose>
            <when test="asc">
                asc
            </when>
            <otherwise>
                desc
            </otherwise>
        </choose>
        limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMerchantNoBaseNewPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.vo.MerchantBaseNewVO">
        SELECT
        info.alias,
        info.id,
        info.userId,
        info.merchantCode,
        info.userName,
        info.contactMobile AS phone,
        IFNULL( spreadCountTb.`count`, 0) yesterdaySpreadCount,
        IFNULL( orderCountTb.orderCount, 0) orderCount,
        IFNULL( orderCountTb.yesterdayOrderCount, 0) yesterdayOrderCount,
        IFNULL( trade.totalMoney, 0.00 ) AS earnings,
        IFNULL( trade.toDayEarnings, 0.00 ) AS toDayEarnings,=
        IFNULL( trade.yestDayEarnings, 0.00 ) AS yestDayEarnings,
        IFNULL( member.totalMember, 0 ) AS menber,
        IFNULL( member.toDayMenber, 0 ) AS toDayMenber,
        IFNULL( member.yestDayMenber, 0 ) AS yestDayMenber,
        IFNULL( fans.yesterDayFansCount, 0 ) AS yesterDayFansCount,
        IFNULL( fans.toDayFansCount, 0 ) AS toDayFansCount,
        IFNULL( shape.fansMoney, 0.00 ) AS fansMoney,
        info.merchantName AS merchantName,
        info.createDt,
        IFNULL( trade.sumNumber, 0 ) AS sumNumber,
        info.sumDifference
        FROM
        (
        SELECT
        mbi.id,
        mbi.alias,
        mbi.userId AS userId,
        mbi.merchantCode AS merchantCode,
        mbi.contactName AS userName,
        mbi.contactMobile,
        mbi.merchantName,
        mbi.createDt,
        (
        SELECT
        SUM(
        IFNULL( mm.usableRechargeFee, 0 )+ IFNULL( mm.usableGiveFee, 0 ))
        FROM
        tb_pluss_merchant_menber mm
        WHERE
        mbi.merchantCode = mm.merchantCode
        GROUP BY
        mbi.userId
        ) AS sumDifference
        FROM
        tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_user_app ua ON mbi.userId = ua.userId
        LEFT JOIN tb_pluss_user_info ui ON ua.parentId = ui.id
        ) info
        LEFT JOIN (
        SELECT
        mo.merchantCode,
        SUM( mo.consumeFee ) totalMoney,
        count(*) AS sumNumber,
        SUM(
        IFNULL(( CASE WHEN to_days( mo.createDt ) = to_days( now()) THEN mo.consumeFee END ), 0.00 )) AS toDayEarnings,
        SUM(
        IFNULL(( CASE WHEN to_days( now()) - to_days( mo.createDt ) <![CDATA[<=]]> 1 THEN mo.consumeFee END ), 0.00 ))
        AS yestDayEarnings
        FROM
        tb_pluss_merchant_order mo
        WHERE
        mo.`status` = 1
        GROUP BY
        mo.merchantCode
        ) trade ON info.merchantCode = trade.merchantCode
        LEFT JOIN (
        SELECT
        mm.merchantCode,
        count( mm.id ) AS totalMember,
        count( CASE WHEN to_days( mm.createDt ) = to_days( now()) THEN mm.id END ) AS toDayMenber,
        count( CASE WHEN to_days( now())- to_days( mm.createDt ) <![CDATA[<=]]> 1 THEN mm.id END ) AS yestDayMenber
        FROM
        tb_pluss_merchant_menber mm
        GROUP BY
        mm.merchantCode
        ) member ON info.merchantCode = member.merchantCode
        LEFT JOIN (
        SELECT
        rf.enableMerchantCode,
        count( CASE WHEN to_days( NOW()) - to_days( rf.enableTime ) <![CDATA[<=]]> 1 THEN rf.id END ) AS
        yesterDayFansCount,
        count( CASE WHEN to_days( rf.enableTime ) = to_days( now()) THEN rf.id END ) AS toDayFansCount
        FROM
        tb_pluss_real_fans rf
        GROUP BY
        rf.enableMerchantCode
        ) fans ON info.merchantCode = fans.enableMerchantCode
        LEFT JOIN (
        SELECT
        pf.lastMerchantCode,
        ROUND( SUM( pf.shareMoney ), 2 ) AS fansMoney
        FROM
        tb_pluss_fans pf
        WHERE
        pf.lastMerchantCode IS NOT NULL
        GROUP BY
        pf.lastMerchantCode
        ) AS shape ON info.merchantCode = shape.lastMerchantCode
        LEFT JOIN (
        SELECT merchantCode, COUNT(1) orderCount, COUNT(IF(TO_DAYS(now()) = (TO_DAYS(createDt) + 1 ), 1, NULL))
        yesterdayOrderCount
        FROM tb_pluss_merchant_order GROUP BY merchantCode
        ) orderCountTb ON info.merchantCode = orderCountTb.merchantCode
        LEFT JOIN (
        SELECT parentId userId, COUNT(IF(TO_DAYS(now()) = (TO_DAYS(createDt) + 1 ), 1, NULL)) count
        FROM tb_pluss_user_app GROUP BY parentId
        ) spreadCountTb ON spreadCountTb.userId = info.userId
        <where>
            info.merchantCode in (SELECT merchantCode7
            FROM tb_pluss_merchant_base_info
            WHERE bindingCode =(SELECT bindingCode
            FROM tb_pluss_merchant_base_info
            where merchantCode = #{merchantCode}))
            <if test="alias!=null and alias!=''">
                and info.alias like concat("%",#{alias},"%")
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and info.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="userName!=null and userName!=''">
                and info.userName LIKE CONCAT('%',#{userName},'%')
            </if>
            <if test="userId!=null">
                and info.userId = #{userId}
            </if>
            <if test="phone!=null and phone!=''">
                and info.contactMobile LIKE CONCAT("%",#{phone},"%")
            </if>
            <if test="parentIdPhone!=null and parentIdPhone!=''">
                and info.parentIdPhone LIKE CONCAT("%",#{parentIdPhone},"%")
            </if>
        </where>
        <choose>
            <when test="column != null and column != ''">
                order by ${column}
            </when>
            <otherwise>
                order by info.createDt
            </otherwise>
        </choose>
        <choose>
            <when test="asc">
                asc
            </when>
            <otherwise>
                desc
            </otherwise>
        </choose>
        limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMerchantBaseNewPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        (SELECT
        mbi.id,
        mbi.alias,
        mbi.userId AS userId,
        mbi.merchantCode AS merchantCode,
        mbi.contactName as userName,
        mbi.contactMobile,
        mbi.merchantName,
        ui.phone AS parentIdPhone,
        mbi.createDt
        FROM
        tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_user_app ua
        ON mbi.userId=ua.userId
        LEFT JOIN tb_pluss_user_info ui
        ON ua.parentId = ui.id
        ) info
        LEFT JOIN
        (
        SELECT
        mo.merchantCode,SUM(mo.consumeFee) totalMoney,
        SUM(IFNULL((CASE WHEN to_days(mo.createDt) = to_days(now()) THEN mo.consumeFee end),0.00)) AS toDayEarnings
        FROM tb_pluss_merchant_order mo WHERE mo.`status` = 1 GROUP BY mo.merchantCode) trade
        ON info.merchantCode = trade.merchantCode
        LEFT JOIN
        (SELECT
        mm.merchantCode,count(mm.id) AS totalMember,
        count(CASE WHEN to_days(mm.createDt) = to_days(now()) THEN mm.id end) AS toDayMenber
        FROM tb_pluss_merchant_menber mm GROUP BY mm.merchantCode
        ) member
        ON info.merchantCode = member.merchantCode
        LEFT JOIN
        (SELECT
        rf.enableMerchantCode,
        count(CASE WHEN to_days(NOW()) - to_days(rf.enableTime) <![CDATA[<=]]>  1 THEN rf.id end) as yesterDayFansCount,
        count(CASE WHEN to_days(rf.enableTime) = to_days(now()) THEN rf.id end) as toDayFansCount
        FROM tb_pluss_real_fans rf GROUP BY rf.enableMerchantCode ) fans
        ON info.merchantCode = fans.enableMerchantCode
        LEFT JOIN
        (SELECT pf.lastMerchantCode,ROUND(SUM(pf.shareMoney),2) AS fansMoney FROM tb_pluss_fans pf WHERE
        pf.lastMerchantCode is not null GROUP BY pf.lastMerchantCode ) as shape
        ON info.merchantCode = shape.lastMerchantCode
        <where>
            <if test="merchantCode!=null and merchantCode!=''">
                and merchantCode=#{merchantCode}
            </if>
            <if test="alias!=null and alias!=''">
                and alias like concat("%",#{alias},"%")
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="userName!=null and userName!=''">
                and userName LIKE CONCAT('%',#{userName},'%')
            </if>
            <if test="userId!=null">
                and userId = #{userId}
            </if>
            <if test="phone!=null and phone!=''">
                and contactMobile LIKE CONCAT('%',#{phone},'%')
            </if>
            <if test="parentIdPhone!=null and parentIdPhone!=''">
                and contactMobile LIKE CONCAT('%',#{parentIdPhone},'%')
            </if>
        </where>
    </select>

    <select id="queryMerchantNoBaseNewPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        (
        SELECT
        mbi.id,
        mbi.alias,
        mbi.userId AS userId,
        mbi.merchantCode AS merchantCode,
        mbi.contactName AS userName,
        mbi.contactMobile,
        mbi.merchantName,
        mbi.createDt,
        (
        SELECT
        SUM(
        IFNULL( mm.usableRechargeFee, 0 )+ IFNULL( mm.usableGiveFee, 0 ))
        FROM
        tb_pluss_merchant_menber mm
        WHERE
        mbi.merchantCode = mm.merchantCode
        GROUP BY
        mbi.userId
        ) AS sumDifference
        FROM
        tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_user_app ua ON mbi.userId = ua.userId
        LEFT JOIN tb_pluss_user_info ui ON ua.parentId = ui.id
        ) info
        LEFT JOIN (
        SELECT
        mo.merchantCode,
        SUM( mo.consumeFee ) totalMoney,
        count(*) AS sumNumber,
        SUM(
        IFNULL(( CASE WHEN to_days( mo.createDt ) = to_days( now()) THEN mo.consumeFee END ), 0.00 )) AS toDayEarnings,
        SUM(
        IFNULL(( CASE WHEN to_days( now()) - to_days( mo.createDt ) <![CDATA[<=]]> 1 THEN mo.consumeFee END ), 0.00 ))
        AS yestDayEarnings
        FROM
        tb_pluss_merchant_order mo
        WHERE
        mo.`status` = 1
        GROUP BY
        mo.merchantCode
        ) trade ON info.merchantCode = trade.merchantCode
        LEFT JOIN (
        SELECT
        mm.merchantCode,
        count( mm.id ) AS totalMember,
        count( CASE WHEN to_days( mm.createDt ) = to_days( now()) THEN mm.id END ) AS toDayMenber,
        count( CASE WHEN to_days( now())- to_days( mm.createDt ) <![CDATA[<=]]> 1 THEN mm.id END ) AS yestDayMenber
        FROM
        tb_pluss_merchant_menber mm
        GROUP BY
        mm.merchantCode
        ) member ON info.merchantCode = member.merchantCode
        LEFT JOIN (
        SELECT
        rf.enableMerchantCode,
        count( CASE WHEN to_days( NOW()) - to_days( rf.enableTime ) <![CDATA[<=]]> 1 THEN rf.id END ) AS
        yesterDayFansCount,
        count( CASE WHEN to_days( rf.enableTime ) = to_days( now()) THEN rf.id END ) AS toDayFansCount
        FROM
        tb_pluss_real_fans rf
        GROUP BY
        rf.enableMerchantCode
        ) fans ON info.merchantCode = fans.enableMerchantCode
        LEFT JOIN (
        SELECT
        pf.lastMerchantCode,
        ROUND( SUM( pf.shareMoney ), 2 ) AS fansMoney
        FROM
        tb_pluss_fans pf
        WHERE
        pf.lastMerchantCode IS NOT NULL
        GROUP BY
        pf.lastMerchantCode
        ) AS shape ON info.merchantCode = shape.lastMerchantCode
        <where>
            info.merchantCode in (SELECT merchantCode
            FROM tb_pluss_merchant_base_info
            WHERE bindingCode =(SELECT bindingCode
            FROM tb_pluss_merchant_base_info
            where merchantCode = #{merchantCode}))
            <if test="alias!=null and alias!=''">
                and alias like concat("%",#{alias},"%")
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="userName!=null and userName!=''">
                and userName LIKE CONCAT('%',#{userName},'%')
            </if>
            <if test="userId!=null">
                and userId = #{userId}
            </if>
            <if test="phone!=null and phone!=''">
                and contactMobile LIKE CONCAT('%',#{phone},'%')
            </if>
            <if test="parentIdPhone!=null and parentIdPhone!=''">
                and contactMobile LIKE CONCAT('%',#{parentIdPhone},'%')
            </if>
        </where>
    </select>

    <select id="queryMerchantFansCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_fans
        <where>
            nickName is not null and nickName!=''
            <if test="merchantCode!=null and merchantCode!=''">
                and merchantCode=#{merchantCode}
            </if>
        </where>
    </select>

    <!-- //queryFanCountCount -->
    <select id="queryTodayFanCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_fans
        <where>
            nickName is not null and nickName!=''
            <if test="merchantCode!=null and merchantCode!=''">
                and merchantCode=#{merchantCode}
            </if>
        </where>
        and date_format(createDt,'%a %m %Y') = date_format(now(), '%a %m %Y')
    </select>


    <select id="queryMerchantListByUserId" resultType="cn.pluss.platform.entity.MerchantBaseInfo">
        SELECT *
        FROM tb_pluss_merchant_base_info baseInfo
        WHERE baseInfo.bindingCode = (
            SELECT bindingCode FROM tb_pluss_merchant_base_info baseInfo WHERE baseInfo.userId = #{userId}
        )
    </select>
    <select id="selectParticulars" resultType="cn.pluss.platform.vo.StoreInfoVO">
        select status, payEcdemicSwitch
        from tb_pluss_merchant_store
        where id = #{id}
    </select>

    <select id="pageJoinData" resultType="cn.pluss.platform.entity.MerchantBaseInfo"
            parameterType="cn.pluss.platform.entity.UserApp">
        SELECT CONCAT(LEFT(ui.loginName, 3),'****',RIGHT(ui.loginName, 4)) as 'loginName',
        mbi.alias,
        IFNULL(cs.createTime,mbi.createDt) AS createDt,
        mbi.merchantCode
        FROM tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_user_app ua ON mbi.userId = ua.userId
        LEFT JOIN tb_pluss_user_info ui ON mbi.userId = ui.id
        LEFT JOIN tb_pluss_merchant_channel_status cs ON mbi.merchantCode = cs.merchantCode
        WHERE ua.parentId = #{userApp.userId} AND cs.`status` = '3' AND LENGTH(ui.loginName) = 11
        <if test="userApp.startDate != null">
            and DATE_FORMAT(cs.createTime,'%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{userApp.startDate},'%Y-%m-%d')
        </if>
        <if test="userApp.endDate != null">
            and DATE_FORMAT(cs.createTime,'%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{userApp.endDate},'%Y-%m-%d')
        </if>
        GROUP BY mbi.merchantCode
        order by mbi.createDt desc
    </select>

    <select id="pageData" parameterType="cn.pluss.platform.dto.MercOrderDataDTO"
            resultType="cn.pluss.platform.dto.MercOrderDataDTO">
        SELECT
        mbi.userId,
        mbi.alias,
        mbi.merchantCode,
        a.totalAmt,
        a.tradeSucAmt,
        a.tradeNum,
        a.tradeSucNum,
        a.debitSucNum,
        a.creditSucNum,
        a.otherSucNum,
        a.d1Num,
        a.d0Num,
        cs.valid,
        cs.channel,
        ROUND(((a.tradeSucNum / a.tradeNum) *100),2) AS numScale, -- 支付成功占比
        ROUND(((a.creditSucNum / a.tradeSucNum) *100),2) AS creditScale, -- 信用卡占比
        ROUND(((a.debitSucNum / a.tradeSucNum) *100),2) AS debitScale, -- 借记卡占比

        ROUND(((a.d1Num / a.tradeSucNum) *100),2) AS d1Scale, -- d1占比
        ROUND(((a.d0Num / a.tradeSucNum) *100),2) AS d0Scale, -- D0占比
        IF( a.tradeSucAmt >= 1000,
        IF(
        a.creditSucNum / a.tradeSucNum >= 1 ,
        IF(a.d1Num / tradeSucNum >= 0.9,"D1套现",
        IF(a.d1Num / tradeSucNum >= 0.7 and a.d1Num / tradeSucNum <![CDATA[<]]> 0.9 , "疑似D1套现","D0套现")),
        IF( a.creditSucNum / a.tradeSucNum >= 0.8 AND a.creditSucNum / a.tradeSucNum <![CDATA[<]]> 1 , "疑似套现","-")
        )
        ,"-") AS risk
        FROM tb_pluss_merchant_base_info mbi LEFT JOIN
        (
        SELECT
        merchantCode,
        SUM(consumeFee) as totalAmt,
        SUM(IF(`status` = 1,consumeFee,0)) AS tradeSucAmt,
        count(1) AS tradeNum,
        SUM( IF ( `status` = 1, 1, 0 ) ) AS tradeSucNum,
        SUM( IF ( `status` = 1 AND drType = 1, 1, 0 ) ) AS debitSucNum,
        SUM( IF ( `status` = 1 AND drType = 2, 1, 0 ) ) AS creditSucNum,
        SUM( IF ( `status` = 1 AND (drType = 3 or drType is null), 1, 0 ) ) AS otherSucNum,
        SUM( IF ( `status` = 1 AND settlementType = 1,1,0)) AS d1Num,
        SUM( IF ( `status` = 1 AND settlementType = 0,1,0)) AS d0Num
        FROM
        tb_pluss_merchant_order
        WHERE
        CURDATE() = createDate
        GROUP BY
        merchantCode) a ON mbi.merchantCode = a.merchantCode
        LEFT JOIN tb_pluss_merchant_channel_status cs ON mbi.merchantCode = cs.merchantCode AND cs.channel != 4
        <where>
            <if test="dto.userId != null">
                and mbi.userId = #{dto.userId}
            </if>
            <if test="dto.alias != null and dto.alias != ''">
                and mbi.alias like CONCAT('%',#{dto.alias},'%')
            </if>
        </where>
        ORDER BY creditScale desc,numScale asc
    </select>


    <select id="pageTradeData" parameterType="cn.pluss.platform.dto.MercTradeDataDTO"
            resultType="cn.pluss.platform.dto.MercTradeDataDTO">
        SELECT mbi.userId,
        mbi.merchantCode,
        SUBSTRING(ui.phone, 1, 11) phone,
        mbi.alias,
        a.tradeSumAmt,
        a.tradeSumNum,
        a.prevTradeSumAmt,
        a.prevTradeSumNum,
        ms.payEcdemicSwitch,
        ms.id as storeId,
        ue.`value` as followStatus,
        <if test="dto.condition == -1">
            "-" as amtChain,
            "-" as numChain
        </if>
        <if test="dto.condition != -1">
            IF(a.prevTradeSumNum = 0, 100,ROUND((a.tradeSumAmt-a.prevTradeSumAmt)/a.prevTradeSumAmt*100,2)) AS amtChain,
            IF(a.prevTradeSumNum = 0, 100,ROUND((a.tradeSumNum-a.prevTradeSumNum)/a.prevTradeSumNum*100,2)) AS numChain
        </if>
        FROM tb_pluss_merchant_base_info mbi LEFT JOIN (
        SELECT
        merchantCode,
        <if test="dto.condition != -1">
            SUM(IF(createDate = #{dto.endTime},consumeFee,0)) AS tradeSumAmt,
            SUM(IF(createDate = #{dto.endTime},1,0)) AS tradeSumNum,
            SUM(IF(DATE_SUB(#{dto.endTime}, INTERVAL 1 DAY) = createDate,consumeFee,0)) as prevTradeSumAmt,
            SUM(IF(DATE_SUB(#{dto.endTime}, INTERVAL 1 DAY) = createDate,1,0)) as prevTradeSumNum
        </if>
        <if test="dto.condition == -1">
            SUM(consumeFee) AS tradeSumAmt,
            count(1) AS tradeSumNum,
            0 as prevTradeSumAmt,
            0 as prevTradeSumNum
        </if>
        FROM
        tb_pluss_merchant_order
        <where>
            `status` = 1
            <if test="dto.endTime != null and dto.endTime != '' and dto.condition != -1">
                AND (createDate = #{dto.endTime} OR DATE_SUB(#{dto.endTime}, INTERVAL 1 DAY) = createDate)
            </if>
        </where>
        GROUP BY
        merchantCode
        ) a ON mbi.merchantCode = a.merchantCode
        LEFT JOIN tb_pluss_user_info ui ON ui.id = mbi.userId
        LEFT JOIN tb_pluss_user_extra ue ON ue.userId = ui.id AND ue.key = 'FOLLOW_UP'
        left JOIN tb_pluss_merchant_store ms on mbi.merchantCode = ms.merchantCode
        <where>
            <if test="dto.userId != null">
                and mbi.userId = #{dto.userId}
            </if>
        </where>
        ORDER BY ${dto.field} ${dto.order}
    </select>

    <select id="listMercData" parameterType="cn.pluss.platform.dto.MercTradeDataDTO"
            resultType="cn.pluss.platform.dto.MercTradeDataDTO">
        SELECT
        mbi.userId,
        mbi.merchantCode,
        SUBSTRING(ui.phone, 1, 11) phone,
        mbi.alias,
        ms.payEcdemicSwitch,
        ms.id as storeId,
        ue.`value` as followStatus
        FROM tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_merchant_store ms on mbi.merchantCode = ms.merchantCode
        LEFT JOIN tb_pluss_user_info ui ON ui.id = mbi.userId
        LEFT JOIN tb_pluss_user_extra ue ON ue.userId = ui.id AND ue.key = 'FOLLOW_UP'
        <where>
            <if test="userId != null">
                and mbi.userId = #{userId}
            </if>
            <if test="merchantCodeList != null and merchantCodeList.size() > 0">
                and mbi.merchantCode IN
                <foreach collection="merchantCodeList" close=")" separator="," open="(" item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="pageTradeDataHistory" parameterType="cn.pluss.platform.dto.MercTradeDataDTO"
            resultType="cn.pluss.platform.dto.MercTradeDataDTO">
        SELECT
        mbi.userId,
        mbi.merchantCode,
        SUBSTRING(ui.phone, 1, 11) phone,
        mbi.alias,
        a.tradeSumAmt,
        a.tradeSumNum,
        a.prevTradeSumAmt,
        a.prevTradeSumNum,
        ms.payEcdemicSwitch,
        ms.id as storeId,
        ue.`value` as followStatus,
        <if test="dto.condition == -1">
            "-" as amtChain,
            "-" as numChain
        </if>
        <if test="dto.condition != -1">
            IF(a.prevTradeSumNum = 0, 100,ROUND((a.tradeSumAmt-a.prevTradeSumAmt)/a.prevTradeSumAmt*100,2)) AS amtChain,
            IF(a.prevTradeSumNum = 0, 100,ROUND((a.tradeSumNum-a.prevTradeSumNum)/a.prevTradeSumNum*100,2)) AS numChain
        </if>
        FROM (
        SELECT
        merchantCode,
        totalFee AS tradeSumAmt,
        `count` AS tradeSumNum,
        0 AS prevTradeSumAmt,
        0 AS prevTradeSumNum
        FROM
        tb_pluss_merchant_order_statistics
        WHERE
        startDate = '1970-01-01'
        ORDER BY totalFee DESC
        LIMIT 10
        ) a left JOIN tb_pluss_merchant_base_info mbi ON mbi.merchantCode = a.merchantCode
        LEFT JOIN
        tb_pluss_merchant_store ms on mbi.merchantCode = ms.merchantCode
        LEFT JOIN tb_pluss_user_info ui ON ui.id = mbi.userId
        LEFT JOIN tb_pluss_user_extra ue ON ue.userId = ui.id AND ue.key = 'FOLLOW_UP'
        <where>
            <if test="dto.userId != null">
                and mbi.userId = #{dto.userId}
            </if>
        </where>
    </select>

    <select id="pageActiveData" parameterType="cn.pluss.platform.dto.MercActiveDataDTO"
            resultType="cn.pluss.platform.dto.MercActiveDataDTO">


    </select>


    <select id="getAreaAndRoleList" resultType="java.lang.String">
        SELECT
        mi.merchantCode
        FROM
        tb_pluss_merchant_base_info mi
        LEFT JOIN tb_pluss_user_app ua ON mi.userId = ua.userId
        WHERE
        ua.roleCode = #{roleCode} AND
        (
        <foreach collection="areaList" separator="or" open="(" close=")" item="item">
            addressNo like CONCAT(#{item},'%')
        </foreach>
        )
    </select>

</mapper>
