<?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.activity.dao.mapper.ActivityRaceCalcMapper">

<!--    <select id="findAllByRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceCalcView">-->
<!--        select sign.custom_id,account.equity,ifnull(SUM(dw.PROFIT),0) dw-->
<!--        from activity_race_rule rule-->
<!--        inner join activity_race_sign sign on rule.id = sign.rule_id-->
<!--        inner join account_view_${platform} account on sign.login = account.login-->
<!--        left join dw_view_${platform} dw on account.login = dw.login-->
<!--        and dw.CLOSE_TIME <![CDATA[>=]]> rule.start_date and dw.CLOSE_TIME <![CDATA[<]]> rule.end_date-->
<!--        where rule.id = #{ruleId}-->
<!--    </select>-->

    <select id="findAllByRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceCalcView">
        select sign.custom_id,sum(ifnull(dw.profit,0)) dw,
        ifnull(count(trade.`order`),0) has_trade,ifnull(count(pos.`order`),0) has_open,
        act.equity
        from activity_race_sign sign
        inner join account_view_${platform} act on sign.login = act.login
        left join dw_view_${platform} dw on sign.login = dw.login and dw.close_time <![CDATA[>=]]> sign.sign_date and dw.close_time <![CDATA[<]]> sign.race_end_date
        left join trade_history_view_${platform} trade on sign.login = trade.login and trade.close_time <![CDATA[>=]]> sign.sign_date and trade.close_time <![CDATA[<]]> sign.race_end_date
        left join trade_position_view_${platform} pos on sign.login = pos.login and pos.open_time <![CDATA[>=]]> sign.sign_date and pos.open_time <![CDATA[<]]> sign.race_end_date
        where sign.rule_id = #{ruleId}
        group by sign.login
    </select>

    <select id="findAccountAndDwInfoByRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceCalcView">
        select sign.custom_id,sum(ifnull(dw.profit,0)) dw,
               act.equity
        from activity_race_sign sign
        inner join account_view_${platform} act on sign.login = act.login
        left join dw_view_${platform} dw on sign.login = dw.login and dw.close_time <![CDATA[>=]]> sign.sign_date and dw.close_time <![CDATA[<]]> sign.race_end_date
        where sign.rule_id = #{ruleId}
        group by sign.login
    </select>

    <select id="findCloseTradeByRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceCalcView">
        select sign.custom_id,ifnull(count(trade.`order`),0) has_trade
        from activity_race_sign sign
        inner join account_view_${platform} act on sign.login = act.login
        left join trade_history_view_${platform} trade on sign.login = trade.login and trade.close_time <![CDATA[>=]]> sign.sign_date and trade.close_time <![CDATA[<]]> sign.race_end_date
        where sign.rule_id = #{ruleId}
        group by sign.login
    </select>

    <select id="findOpenTradeByRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceCalcView">
        select sign.custom_id,ifnull(count(pos.`order`),0) has_open
        from activity_race_sign sign
        inner join account_view_${platform} act on sign.login = act.login
        left join trade_position_view_${platform} pos on sign.login = pos.login and pos.open_time <![CDATA[>=]]> sign.sign_date and pos.open_time <![CDATA[<]]> sign.race_end_date
        where sign.rule_id = #{ruleId}
        group by sign.login
    </select>
</mapper>