<?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.ib.task.dao.mapper.TradeHistoryDao">

    <insert id="saveInBatch">
        INSERT INTO
        <choose>
            <when test="ym != null">
                trade_history_${platform}_${ym}
            </when>
            <otherwise>
                trade_history_${platform}
            </otherwise>
        </choose>
        (login, `order`, cmd, symbol, volume, open_time, open_price, close_time, close_price,
        profit, storage, commission, sl, tp, add_time, group_code, group_type,
        agent_id, sales_id, custom_id, symbol_type)
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.login}, #{item.order}, #{item.cmd}, #{item.symbol}, #{item.volume}, #{item.openTime}, #{item.openPrice}, #{item.closeTime}, #{item.closePrice},
            #{item.profit}, #{item.storage}, #{item.commission}, #{item.sl}, #{item.tp}, #{item.addTime}, #{item.groupCode}, #{item.groupType},
            #{item.agentId}, #{item.salesId}, #{item.customId}, #{item.symbolType})
        </foreach>
    </insert>

    <update id="createTradeTable">
        CREATE TABLE `trade_history_${platform}_${ym}` (
            `id`  bigint(20) NOT NULL AUTO_INCREMENT ,
            `login`  bigint(20) NULL DEFAULT NULL ,
            `order`  bigint(20) NULL DEFAULT NULL ,
            `cmd`  int(11) NULL DEFAULT NULL ,
            `symbol`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
            `volume`  double NULL DEFAULT NULL ,
            `open_time`  datetime NULL DEFAULT NULL ,
            `open_price`  double NULL DEFAULT NULL ,
            `close_time`  datetime NULL DEFAULT NULL ,
            `close_price`  double NULL DEFAULT NULL ,
            `profit`  double NULL DEFAULT NULL ,
            `storage`  double NULL DEFAULT NULL ,
            `commission`  double NULL DEFAULT NULL ,
            `sl`  double NULL DEFAULT NULL ,
            `tp`  double NULL DEFAULT NULL ,
            `group_code`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
            `group_type` bigint(20) NULL DEFAULT NULL ,
            `add_time`  datetime NULL DEFAULT NULL ,
            `add_user`  bigint(20) NULL DEFAULT NULL ,
            `add_ip`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
            `modify_time`  datetime NULL DEFAULT NULL ,
            `modify_user`  bigint(20) NULL DEFAULT NULL ,
            `modify_ip`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
            `note`  varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注' ,
            `agent_id` bigint(20) NULL DEFAULT NULL ,
            `sales_id` bigint(20) NULL DEFAULT NULL ,
            `custom_id` bigint(20) NULL DEFAULT NULL ,
            `symbol_type` int(11) NULL DEFAULT NULL ,
            PRIMARY KEY (`id`),
            INDEX `th_${platform}${ym}_close_time` (`close_time`) USING BTREE ,
            INDEX `th_${platform}${ym}_login` (`login`) USING BTREE ,
            INDEX `th_${platform}${ym}_open_time` (`open_time`) USING BTREE ,
            INDEX `th_${platform}${ym}_LC` (`login`, `close_time`) USING BTREE,
            INDEX `th_${platform}${ym}_lcvs` (login,close_time,volume,symbol) USING BTREE,
            INDEX `th_${platform}${ym}_agent_id` (agent_id) USING BTREE,
            INDEX `th_${platform}${ym}_sales_id` (sales_id) USING BTREE
            )
            ENGINE=InnoDB
            DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
            AUTO_INCREMENT=1
            ROW_FORMAT=DYNAMIC
    </update>

    <select id="findAllTradeBetweenDate" resultType="Long">
        select `order`
        <choose>
            <when test="ym != null">
                from trade_history_${platform}_${ym}
            </when>
            <otherwise>
                from trade_history_${platform}
            </otherwise>
        </choose>
        where close_time <![CDATA[>=]]>#{startDate}
        and close_time <![CDATA[<]]>#{endDate}
        order by close_time asc
    </select>

    <select id="countVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="Integer">
        select count(1)
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
        group by trade.login,agent.id
    </select>

    <select id="listVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="com.crm.rely.backend.model.pojo.view.VolumeReportView">
        select trade.login,agent.ib_no ib_no,sales.ib_no sales_no,
        sum((case trade.symbol_type when 1 then trade.volume else 0 end)) fx_volume,
        sum((case trade.symbol_type when 2 then trade.volume else 0 end)) cfd_volume,
        sum((case trade.symbol_type when 3 then trade.volume else 0 end)) index_volume,
        sum((case trade.symbol_type when 4 then trade.volume else 0 end)) metal_volume,
        trade.custom_id
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
        group by trade.login,agent.id
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="sumVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="com.crm.rely.backend.model.pojo.view.VolumeReportView">
        select trade.symbol_type,sum(ifnull(trade.volume,0)) volume
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
        group by trade.symbol_type
    </select>

    <select id="countSalesVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="Integer">
        select count(1)
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        inner join trade_commission tc on trade.`order` = tc.`order`
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
    </select>

    <select id="listSalesVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="com.crm.rely.backend.model.pojo.view.VolumeReportView">
        select trade.login,agent.ib_no ib_no,sales.ib_no sales_no,
        trade.symbol_type,trade.volume,trade.custom_id,sales.`name` `sales_name`,
        trade.`order` `order`,tc.sales_commission,trade.close_time
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        inner join trade_commission tc on trade.`order` = tc.`order`
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
        order by trade.close_time desc
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="sumSalesVolumeReport"
            parameterType="com.crm.rely.backend.model.entity.report.VolumeReportEntity"
            resultType="com.crm.rely.backend.model.pojo.view.VolumeReportView">
        select sum(trade.volume) volume,sum(tc.sales_commission) sales_commission
        from trade_history_${platform} trade
        inner join custom_info ci on trade.custom_id = ci.id
        inner join user_info agent on trade.agent_id = agent.id
        inner join user_info sales on trade.sales_id = sales.id
        inner join trade_commission tc on trade.`order` = tc.`order`
        <where>
            <if test="login != null">
                and trade.login = #{login}
            </if>
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and trade.close_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and trade.close_time <![CDATA[<]]>#{endDate}
            </if>
        </where>
    </select>
</mapper>