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

    <select id="queryCashPage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.Cash">
        SELECT
               tpc.id, tpc.userId,tpc.userName,tpc.merchantCode,tpc.merchantName,
               IFNULL(tpc.accountNo, ca.accountNo) accountNo, IFNULL(tpc.accountName, ca.accountName) accountName,
               tpc.cashAmt,tpc.status,tpc.reason,tpc.createDt,tpc.updateDt,tpc.cashNumber,tpc.type,
                tpui.alias as alias ,tpc.cashStatus from tb_pluss_cash tpc
        LEFT JOIN tb_pluss_merchant_base_info tpui on tpui.userId = tpc.userId
        LEFT JOIN tb_pluss_cash_account ca ON ca.userId = tpc.userId
        <where>
            <if test="id!= null">
                and tpc.id = #{id}
            </if>
            <if test="userId!= null">
                and tpc.userId = #{userId}
            </if>
            <if test="userName!= null">
                and tpc.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="merchantCode!= null">
                and tpc.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and tpc.merchantName LIKE CONCAT("%", #{merchantName},"%")
            </if>
            <if test="cashAmt!= null">
                and tpc.cashAmt = #{cashAmt}
            </if>
            <if test="status!= null">
                and tpc.status = #{status}
            </if>
            <if test="reason!= null">
                and tpc.reason = #{reason}
            </if>
            <if test="createDt!= null">
                and tpc.createDt = #{createDt}
            </if>
            <if test="updateDt!= null">
                and tpc.updateDt = #{updateDt}
            </if>
            <if test="logo!= null">
                and tpc.logo = #{logo}
            </if>
            <if test="cashNumber!= null">
                and tpc.cashNumber = #{cashNumber}
            </if>
            <if test="backNo!= null">
                and tpc.backNo = #{backNo}
            </if>
            <if test="backDt!= null">
                and tpc.backDt = #{backDt}
            </if>
            <if test="updator!= null">
                and tpc.updator = #{updator}
            </if>
            <if test="type!= null">
                and tpc.type = #{type}
            </if>
            <if test="beginTime!= null">
                and tpc.createDt &gt; #{beginTime}
            </if>
            <if test="endTime!= null">
                and tpc.createDt &lt; #{endTime}
            </if>
            <if test="phone!= null">
                and phone LIKE CONCAT("%", #{phone},"%")
            </if>

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

    <select id="queryCashPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_cash tpc
            left join tb_pluss_merchant_base_info tpui on tpui.userId = tpc.userId
        <where>
            <if test="id!= null">
                and tpc.id = #{id}
            </if>
            <if test="userId!= null">
                and tpc.userId = #{userId}
            </if>
            <if test="userName!= null">
                and tpc.userName LIKE CONCAT("%",#{userName},"%")
            </if>
            <if test="merchantCode!= null">
                and tpc.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and tpc.merchantName LIKE CONCAT("%", #{merchantName},"%")
            </if>
            <if test="cashAmt!= null">
                and tpc.cashAmt = #{cashAmt}
            </if>
            <if test="status!= null">
                and tpc.status = #{status}
            </if>
            <if test="reason!= null">
                and tpc.reason = #{reason}
            </if>
            <if test="createDt!= null">
                and tpc.createDt = #{createDt}
            </if>
            <if test="updateDt!= null">
                and tpc.updateDt = #{updateDt}
            </if>
            <if test="logo!= null">
                and tpc.logo = #{logo}
            </if>
            <if test="cashNumber!= null">
                and tpc.cashNumber = #{cashNumber}
            </if>
            <if test="backNo!= null">
                and tpc.backNo = #{backNo}
            </if>
            <if test="backDt!= null">
                and tpc.backDt = #{backDt}
            </if>
            <if test="updator!= null">
                and tpc.updator = #{updator}
            </if>
            <if test="type!= null">
                and tpc.type = #{type}
            </if>
            <if test="beginTime!= null">
                and tpc.createDt &gt; #{beginTime}
            </if>
            <if test="endTime!= null">
                and tpc.createDt &lt; #{endTime}
            </if>
            <if test="phone!= null">
                and phone LIKE CONCAT("%", #{phone},"%")
            </if>
        </where>
    </select>


    <!-- 已经成功提现的金额/申请提现中的金额  粉丝分润-->
    <select id="sumFansSuccessCash" parameterType="java.util.Map" resultType="java.lang.Double">
        select IFNULL(sum(cashAmt), 0) as cashAmt
        from tb_pluss_cash
        where merchantCode = #{merchantCode}
          and type = '0'
          and (status = '1' OR status = '0')
    </select>

    <!-- 已经成功提现 粉丝分润-->
    <select id="sumFansAreadlyCash" parameterType="java.util.Map" resultType="java.lang.Double">
        select sum(cashAmt) as cashAmt
        from tb_pluss_cash
        where merchantCode = #{merchantCode}
          and type = '0'
          and status = '1'
    </select>

    <!-- 已经成功提现的金额/申请提现中的金额 推广分润-->
    <select id="sumMerChantSuccessCash" resultType="java.lang.Double">
        select sum(cashAmt) as cashAmt
        from tb_pluss_cash
        where userId = #{userId}
          and (status = '1' OR status = '0')
    </select>

    <!-- 已经成功提现 推广分润 -->
    <select id="sumMerChantAreadlyCash" resultType="java.lang.Double">
        select sum(cashAmt) as cashAmt
        from tb_pluss_cash
        where userId = #{userId}
          and type = '1'
          and status = '1'
    </select>

    <select id="queryCashPage2" resultType="cn.pluss.platform.entity.Cash">
        SELECT tpc.*, cr.remark, tpui.aliAccount,tpui.alias as alias, ui.phone phone, t2.cashTotalAmt from tb_pluss_cash tpc
        left join tb_pluss_merchant_base_info tpui on tpui.userId = tpc.userId
        LEFT JOIN tb_pluss_user_info ui ON ui.id = tpc.userId
        LEFT JOIN (
            SELECT sum(cashAmt) cashTotalAmt,  userId FROM tb_pluss_cash
            WHERE `status` = '1'
            GROUP BY userId
        ) t2 ON tpc.userId = t2.userId
        LEFT JOIN tb_pluss_common_remark cr ON cr.userId = tpc.userId AND cr.code = 'CASH'
        ${ew.customSqlSegment}
    </select>

    <select id="getCashCountData" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
            IFNULL(SUM(if(`status` = 0,cashAmt,0)),0) AS inCashAmtCount,
            IFNULL(SUM(if(`status` = 1,cashAmt,0)),0) AS useCashAmtCount
        FROM
            tb_pluss_cash
        <where>
            <if test="startTime != null">
                and createDt <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                and createDt <![CDATA[<=]]> #{endTime}
            </if>
            <if test="userId != null and userId != ''">
                and userId = #{userId}
            </if>
        </where>

    </select>

    <select id="selectCashAmtTotal" resultType="java.math.BigDecimal">
        SELECT SUM(`cashAmt`) FROM tb_pluss_cash WHERE `userId` = #{userId}
        <if test="statusArr != null and statusArr.length > 0">
            AND `status` IN
            <foreach collection="statusArr" separator=", " close=")" item="item" open="(">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="subordinate" parameterType="java.lang.Long" resultType="cn.pluss.platform.entity.Cash">
        SELECT
            v.*
        FROM
            (
                SELECT
                    @ids AS _ids,
                    ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
            @l := @l + 1 AS LEVEL
        FROM
            tb_pluss_user_promotion,
            ( SELECT @ids := #{userId}, @l := 0 ) b
        WHERE
            @ids IS NOT NULL
            ) ID,
            tb_pluss_user_promotion p
            LEFT JOIN tb_pluss_cash v ON p.user_id = v.userId
        WHERE
            FIND_IN_SET( p.user_id, ID._ids )
          AND v.userId != #{userId}
          AND v.id IS NOT NULL
          <if test="status != null and status !=''">
              AND v.status = #{status}
          </if>
          ORDER BY v.id DESC
          limit #{page},#{size}
    </select>
</mapper>
