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

    <insert id="saveMerchantProfitBatch">
        insert into
        tb_pluss_merchant_profit(userId,type,price,orderNumber,merchantCode,merchantName,consumeFee,createDt,orderDt,status,payTypeCode,payTypeName,merchantLogo,level,merchantParentId,merchantParentName,merchantParentLogo,phone,userName)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{userId},#{type},#{price},#{orderNumber},#{merchantCode},#{merchantName},#{consumeFee},#{createDt},#{orderDt},#{status},#{payTypeCode},#{payTypeName},#{merchantLogo},#{level},#{merchantParentId},#{merchantParentName},#{merchantParentLogo},#{phone},#{userName})
        </foreach>
    </insert>

    <select id="queryMerchantProfit" parameterType="cn.pluss.platform.entity.MerchantProfit"
            resultType="cn.pluss.platform.entity.MerchantProfit">
        SELECT * from tb_pluss_merchant_profit
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="agentStaffId != null">
                and agentStaffId = #{agentStaffId}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="price!= null">
                and price = #{price}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="consumeFee!= null">
                and consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="payTypeCode!= null">
                and payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and merchantParentPhone = #{merchantParentPhone}
            </if>

        </where>
    </select>

    <select id="queryMerchantProfitList" parameterType="cn.pluss.platform.entity.MerchantProfit"
            resultType="cn.pluss.platform.entity.MerchantProfit">
        SELECT * from tb_pluss_merchant_profit
        <where>
            <if test="agentStaffId != null">
                and agentStaffId = #{agentStaffId}
            </if>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="price!= null">
                and price = #{price}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="consumeFee!= null">
                and consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="payTypeCode!= null">
                and payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="startTime!= null">
                and createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and merchantParentPhone = #{merchantParentPhone}
            </if>
        </where>
    </select>

    <update id="updateMerchantProfit" parameterType="cn.pluss.platform.entity.MerchantProfit">
        update tb_pluss_merchant_profit
        <set>
            <if test="userId!= null">
                userId = #{userId},
            </if>
            <if test="type!= null">
                type = #{type},
            </if>
            <if test="price!= null">
                price = #{price},
            </if>
            <if test="orderNumber!= null">
                orderNumber = #{orderNumber},
            </if>
            <if test="merchantCode!= null">
                merchantCode = #{merchantCode},
            </if>
            <if test="merchantName!= null">
                merchantName = #{merchantName},
            </if>
            <if test="consumeFee!= null">
                consumeFee = #{consumeFee},
            </if>
            <if test="createDt!= null">
                createDt = #{createDt},
            </if>
            <if test="orderDt!= null">
                orderDt = #{orderDt},
            </if>
            <if test="status!= null">
                status = #{status},
            </if>
            <if test="payTypeCode!= null">
                payTypeCode = #{payTypeCode},
            </if>
            <if test="payTypeName!= null">
                payTypeName = #{payTypeName},
            </if>
            <if test="merchantLogo!= null">
                merchantLogo = #{merchantLogo},
            </if>
            <if test="level!= null">
                level = #{level},
            </if>
            <if test="merchantParentId!= null">
                merchantParentId = #{merchantParentId},
            </if>
            <if test="merchantParentName!= null">
                merchantParentName = #{merchantParentName},
            </if>
            <if test="merchantParentLogo!= null">
                merchantParentLogo = #{merchantParentLogo},
            </if>
            <if test="phone!= null">
                phone = #{phone},
            </if>
            <if test="userName!= null">
                userName = #{userName},
            </if>
            <if test="merchantParentLevel!= null">
                merchantParentLevel = #{merchantParentLevel},
            </if>
            <if test="merchantParentPhone!= null">
                merchantParentPhone = #{merchantParentPhone},
            </if>
            <if test="profitRate!= null">
                profitRate = #{profitRate}
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="queryMerchantProfitPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.MerchantProfit">
        SELECT
        profit.*,
        ua.userType
        FROM
        tb_pluss_merchant_profit profit
        LEFT JOIN tb_pluss_merchant_order merchantOrder ON profit.orderNumber = merchantOrder.orderNumber
        LEFT JOIN tb_pluss_user_app ua ON ua.userId = profit.userId
        <where>
            and merchantOrder.status = 1
            <if test="id!= null">
                and profit.id = #{id}
            </if>
            <if test="userId!= null">
                and profit.userId = #{userId}
            </if>
            <if test="type!= null">
                and profit.type = #{type}
            </if>
            <if test="price!= null">
                and profit.price = #{price}
            </if>
            <if test="orderNumber!= null">
                and profit.orderNumber = #{orderNumber}
            </if>
            <if test="agentStaffId != null">
                and profit.agentStaffId = #{agentStaffId}
            </if>
            <if test="merchantCode!= null">
                and profit.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and profit.merchantName like CONCAT('%',#{merchantName},'%')
            </if>
            <if test="consumeFee!= null">
                and profit.consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and profit.createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and profit.orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and profit.status = #{status}
            </if>

            <if test="self  != null">
                and profit.agentStaffId IS NULL
            </if>

            <if test="agentStaffIds != null">
                and profit.agentStaffId IN
                <foreach collection="agentStaffIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>

            <if test="payTypeCode!= null">
                and profit.payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and profit.payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and profit.merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and profit.level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and profit.merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and profit.merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and profit.merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="startTime!= null">
                and profit.createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and profit.createDt &lt; #{endTime}
            </if>
            <if test="retype!= null">
                and profit.type !=4
            </if>
            <if test="phone!= null">
                and profit.phone = #{phone}
            </if>
            <if test="userName!= null">
                and profit.userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and profit.merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and profit.merchantParentPhone = #{merchantParentPhone}
            </if>

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

    <select id="queryMerchantProfitPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_merchant_profit
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="agentStaffId != null">
                and agentStaffId = #{agentStaffId}
            </if>
            <if test="price!= null">
                and price = #{price}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName like CONCAT('%',#{merchantName},'%')
            </if>
            <if test="consumeFee!= null">
                and consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>

            <if test="payTypeCode!= null">
                and payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="startTime!= null">
                and createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
            <if test="retype!= null">
                and type !=4
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and merchantParentPhone = #{merchantParentPhone}
            </if>
            <if test="recordDate != null">
                and recordDate = #{recordDate}
            </if>
            <if test="recordDateStart != null">
                and recordDate &gt;= #{recordDateStart}
            </if>
            <if test="recordDateEnd != null">
                and recordDate &lt;= #{recordDateEnd}
            </if>
            <if test="recordMonth != null">
                and recordMonth = #{recordMonth}
            </if>
            <if test="recordMonthStart != null">
                and recordMonth &gt;= #{recordMonthStart}
            </if>
            <if test="recordMonthEnd != null">
                and recordMonth &lt;= #{recordMonthEnd}
            </if>
        </where>
    </select>


    <select id="queryMerchantProfitSumConsumeFee" parameterType="java.util.Map" resultType="java.lang.Double">
        SELECT sum(consumeFee) from tb_pluss_merchant_profit
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="agentStaffId != null">
                and agentStaffId = #{agentStaffId}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="price!= null">
                and price = #{price}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="consumeFee!= null">
                and consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>

            <if test="payTypeCode!= null">
                and payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="stTime!= null">
                and createDt &gt;= #{stTime}
            </if>
            <if test="enTime!= null">
                and createDt &lt;= #{enTime}
            </if>
            <if test="startTime!= null">
                and createDt &gt;= #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
            <if test="merchantCodeList!= null">
                and merchantCode in
                <foreach collection="merchantCodeList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and merchantParentPhone = #{merchantParentPhone}
            </if>
            <if test="retype!= null">
                and type !=4
            </if>
        </where>
    </select>


    <select id="queryMerchantProfitSumPrice" parameterType="java.util.Map" resultType="java.lang.Double">
        SELECT sum(price) from tb_pluss_merchant_profit profit
        <where>
            <if test="id!= null">
                and profit.id = #{id}
            </if>
            <if test="agentStaffId != null">
                and profit.agentStaffId = #{agentStaffId}
            </if>
            <if test="userId!= null">
                and profit.userId = #{userId}
            </if>
            <if test="type!= null">
                and profit.type = #{type}
            </if>
            <if test="price!= null">
                and profit.price = #{price}
            </if>
            <if test="orderNumber!= null">
                and profit.orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and profit.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and profit.merchantName = #{merchantName}
            </if>
            <if test="agentStaffId != null">
                and profit.agentStaffId = #{agentStaffId}
            </if>
            <if test="consumeFee!= null">
                and profit.consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and profit.createDt = #{createDt}
            </if>
            <if test="stTime!= null">
                and profit.createDt &gt;= #{stTime}
            </if>
            <if test="enTime!= null">
                and profit.createDt &lt;= #{enTime}
            </if>
            <if test="orderDt!= null">
                and profit.orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and profit.status = #{status}
            </if>
            <if test="startTime!= null">
                and profit.createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and profit.createDt &lt; #{endTime}
            </if>
            <if test="payTypeCode!= null">
                and profit.payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and profit.payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and profit.merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and profit.level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and profit.merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and profit.merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and profit.merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="merchantCodeList!= null">
                and profit.merchantCode in
                <foreach collection="merchantCodeList" item="item" open="(" close=")" index="index" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="phone!= null">
                and profit.phone = #{phone}
            </if>
            <if test="userName!= null">
                and profit.userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and profit.merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and profit.merchantParentPhone = #{merchantParentPhone}
            </if>
            <if test="retype!= null">
                and profit.type !=4
            </if>
            <if test="recordDate != null">
                and profit.recordDate = #{recordDate}
            </if>
            <if test="recordDateStart != null">
                and profit.recordDate &gt;= #{recordDateStart}
            </if>
            <if test="recordDateEnd != null">
                and profit.recordDate &lt;= #{recordDateEnd}
            </if>
        </where>
    </select>


    <select id="queryMerchantProfitPageCountGroupByOrderNumber" parameterType="java.util.Map"
            resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_merchant_profit
        <where>
            <if test="agentStaffId != null">
                and agentStaffId = #{agentStaffId}
            </if>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="price!= null">
                and price = #{price}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName like CONCAT('%',#{merchantName},'%')
            </if>
            <if test="consumeFee!= null">
                and consumeFee = #{consumeFee}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderDt!= null">
                and orderDt = #{orderDt}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>

            <if test="payTypeCode!= null">
                and payTypeCode = #{payTypeCode}
            </if>
            <if test="payTypeName!= null">
                and payTypeName = #{payTypeName}
            </if>
            <if test="merchantLogo!= null">
                and merchantLogo = #{merchantLogo}
            </if>
            <if test="level!= null">
                and level = #{level}
            </if>
            <if test="merchantParentId!= null">
                and merchantParentId = #{merchantParentId}
            </if>
            <if test="merchantParentName!= null">
                and merchantParentName = #{merchantParentName}
            </if>
            <if test="merchantParentLogo!= null">
                and merchantParentLogo = #{merchantParentLogo}
            </if>
            <if test="startTime!= null">
                and createDt &gt; #{startTime}
            </if>
            <if test="endTime!= null">
                and createDt &lt; #{endTime}
            </if>
            <if test="retype!= null">
                and type !=4
            </if>
            <if test="phone!= null">
                and phone = #{phone}
            </if>
            <if test="userName!= null">
                and userName = #{userName}
            </if>
            <if test="merchantParentLevel!= null">
                and merchantParentLevel = #{merchantParentLevel}
            </if>
            <if test="merchantParentPhone!= null">
                and merchantParentPhone = #{merchantParentPhone}
            </if>
        </where>
        group by orderNumber
    </select>


    <select id="queryMerchantBillWithProfit" resultType="cn.pluss.platform.dto.MerchantFeeDTO">
        SELECT tmpo.merchantCode, SUM(tmpo.consumeFee) money, SUM(profit.price) profit FROM tb_pluss_merchant_profit profit
        LEFT JOIN tb_pluss_merchant_order tmpo ON tmpo.orderNumber = profit.orderNumber
        WHERE profit.userId = #{userId} AND tmpo.status = '1' AND tmpo.merchantCode IN
        <foreach collection="merchantCodeList" close=")" item="item" open="(" separator=",">
            #{item}
        </foreach>
        <if test="startTime != null">
            AND tmpo.createDt &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND tmpo.createDt &lt;= #{endTime}
        </if>
        GROUP BY tmpo.merchantCode
    </select>

    <update id="updateBatch" parameterType="java.util.List">
        update tb_pluss_merchant_profit
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="price = case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.price != null">
                        when id=#{i.id} then #{i.price}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" profitRate = case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.profitRate != null">
                        when id=#{i.id} then #{i.profitRate}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index">
            id=#{i.id}
        </foreach>
    </update>

    <select id="getProfitCount" parameterType="java.util.Map" resultType="java.util.Map">
    SELECT
    IFNULL(SUM(mp.consumeFee),0.00) AS transAmountCount,
    IFNULL(sum(mp.price),0.00) AS shareAmountCount,
    IFNULL(SUM(CASE WHEN mp.type = 4 THEN mp.price end),0.00) AS toUpShareAmtCount,
    IFNULL(SUM(CASE WHEN mp.type <![CDATA[ <> ]]> 4 THEN mp.price end),0.00) AS mercShareAmtCount
    FROM tb_pluss_merchant_profit mp
    LEFT JOIN tb_pluss_user_info ui ON mp.userId = ui.id
    LEFT JOIN tb_pluss_user_app ua ON mp.userId = ua.userId
    <where>
        <if test="userId != null and userId != ''">
            and mp.userId = #{userId}
        </if>
        <if test="profitName != null and profitName != ''">
            and ua.userName = #{profitName}
        </if>
        <if test="startDate != null and startDate != ''">
            and DATE_FORMAT(mp.createDt,'%Y-%m-%d') <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and DATE_FORMAT(mp.createDt,'%Y-%m-%d') <![CDATA[ <= ]]> #{endDate}
        </if>
    </where>
    </select>

    <select id="queryMerchantProfitRate" resultType="java.util.Map">
        SELECT profit.id, IFNULL(mrr.rate, mr.rate) rateCur
        FROM tb_pluss_merchant_profit profit LEFT JOIN (
           SELECT profit.id, profit.userId, MIN(mrr.createTime) createTime
           FROM
                (SELECT * FROM  tb_pluss_merchant_profit WHERE userId = #{userId} ) profit
                    LEFT JOIN
                (SELECT * FROM  tb_pluss_merchant_rate_record WHERE userId = #{userId} )mrr
                              ON mrr.userId = profit.userId
           WHERE profit.createDt &gt; mrr.createTime
           GROUP BY profit.id, profit.userId
        ) t ON profit.id = t.id
        LEFT JOIN tb_pluss_merchant_rate mr ON mr.userId = profit.userId
        LEFT JOIN tb_pluss_merchant_rate_record mrr ON t.createTime = mrr.createTime AND t.userId = mrr.userId
        <where>
            <if test="merchantProfitList != null and merchantProfitList.size() > 0">
                profit.id IN
                <foreach collection="merchantProfitList" close=")" item="item" open="(" separator=",">
                    #{item.id}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getProfitAmtCount" resultType="java.util.Map" parameterType="cn.pluss.platform.entity.MerchantProfit">
        SELECT
        SUM( price ) as shareAmountCount,
        SUM( IF ( type = 1, price, 0 ) ) as diretAmountCount,
        SUM( IF ( type != 1, price, 0 ) ) as teamAmountCount
        FROM
        tb_pluss_merchant_profit
        <where>
            <if test="userId != null">
                and userId = #{userId}
            </if>
            <if test="orderNumber != null and orderNumber != ''">
                and orderNumber = #{orderNumber}
            </if>
            <if test="recordDate != null">
                and recordDate = #{recordDate}
            </if>
        </where>
    </select>

    <select id="selectGroupMercProfit" resultType="cn.pluss.platform.entity.MerchantProfit">
        SELECT sum(price) price, userId FROM tb_pluss_merchant_profit WHERE  `status` =  '1' AND userId IN
            <foreach collection="userIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        GROUP BY userId
    </select>
</mapper>
