<?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="com.crm.manager.dao.mapper.TwCooperateDailyMapper">

    <update id="updateById" parameterType="com.crm.rely.backend.core.pojo.table.TwCooperateDailyTable">
        UPDATE tw_cooperate_daily
        <set>
            <if test="totalProfit != null">total_profit = #{totalProfit},</if>
            <if test="negativeProfit != null">negative_profit = #{negativeProfit},</if>
            <if test="depositFee != null">deposit_fee = #{depositFee},</if>
            <if test="withdrawalFee != null">withdrawal_fee = #{withdrawalFee},</if>
            <if test="rebate != null">rebate = #{rebate},</if>
            <if test="commission != null">commission = #{commission},</if>
            <if test="hidden != null">hidden = #{hidden},</if>
            <if test="sumRebate != null">sum_rebate = #{sumRebate},</if>
            <if test="ptc != null">ptc = #{ptc},</if>
            <if test="depositComment != null">deposit_comment = #{depositComment},</if>
            <if test="settlementPl != null">settlement_pl = #{settlementPl},</if>
            <if test="dba != null">dba = #{dba},</if>
            <if test="assBase != null">ass_base = #{assBase},</if>
            <if test="finalProfit != null">final_profit = #{finalProfit},</if>
            <if test="modifyTime != null">modify_time = #{modifyTime},</if>
            <if test="modifyUser != null">modify_user = #{modifyUser},</if>
            <if test="modifyIp != null">modify_ip = #{modifyIp},</if>
            <if test="note != null">note = #{note},</if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="listSearch" parameterType="com.crm.rely.backend.core.entity.cooperate.tw.TwCooperateListEntity" resultType="com.crm.rely.backend.core.pojo.view.TwCooperateDailyView">
        select daily.*
        from tw_cooperate_daily daily
        <include refid="whereSql"></include>
        order by daily.summary_date desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="countSearch" parameterType="com.crm.rely.backend.core.entity.cooperate.tw.TwCooperateListEntity" resultType="Integer">
        select count(daily.id)
        from tw_cooperate_daily daily
        <include refid="whereSql"></include>
    </select>

    <select id="sumSearch" parameterType="com.crm.rely.backend.core.entity.cooperate.tw.TwCooperateListEntity" resultType="com.crm.rely.backend.core.pojo.view.TwCooperateDailyView">
        select IFNULL(SUM(daily.total_profit), 0)       AS total_profit,
        IFNULL(SUM(daily.negative_profit), 0)    AS negative_profit,
        IFNULL(SUM(daily.deposit_fee), 0)        AS deposit_fee,
        IFNULL(SUM(daily.withdrawal_fee), 0)     AS withdrawal_fee,
        IFNULL(SUM(daily.rebate), 0)             AS rebate,
        IFNULL(SUM(daily.commission), 0)         AS commission,
        IFNULL(SUM(daily.hidden), 0)             AS hidden,
        IFNULL(SUM(daily.sum_rebate), 0)         AS sum_rebate,
        IFNULL(SUM(daily.ptc), 0)                AS ptc,
        IFNULL(SUM(daily.deposit_comment), 0)    AS deposit_comment,
        IFNULL(SUM(daily.settlement_pl), 0)      AS settlement_pl,
        IFNULL(SUM(daily.dba), 0)                AS dba,
        IFNULL(SUM(daily.ass_base), 0)           AS ass_base,
        IFNULL(SUM(daily.final_profit), 0)       AS final_profit
        from tw_cooperate_daily daily
        <include refid="whereSql"></include>
    </select>


    <sql id="whereSql">
        <where>
            <if test="summaryType != null and summaryType!=0">
                and daily.summary_type = #{summaryType}
            </if>
            <if test="ibNo != null and ibNo !=''">
                and daily.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="startDate != null">
                and daily.summary_date <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and daily.summary_date <![CDATA[<]]>#{endDate}
            </if>
        </where>
    </sql>
</mapper>