<?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.muchinfo.mtp3interface.mapper.IReckonDaydividecountMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.muchinfo.mtp3pojos.entity.ReckonDaydividecount">
        <id column="TRADEDATE" property="tradedate" />
        <result column="AREAID" property="areaid" />
        <result column="AREATYPE" property="areatype" />
        <result column="ACCOUNTCURRENCYID" property="accountcurrencyid" />
        <result column="AREAACCOUNTID" property="areaaccountid" />
        <result column="TOTALCOUNT" property="totalcount" />
        <result column="TOTALFEE" property="totalfee" />
        <result column="DIVIDEAMOUNT" property="divideamount" />
        <result column="PAYSTATUS" property="paystatus" />
        <result column="PAYERID" property="payerid" />
        <result column="PAYTIME" property="paytime" />
        <result column="REMARK" property="remark" />
        <result column="PARENTUSERID" property="parentuserid" />
        <result column="PARENTACCOUNTID" property="parentaccountid" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        TRADEDATE, AREAID, AREATYPE, ACCOUNTCURRENCYID, AREAACCOUNTID, TOTALCOUNT, TOTALFEE, DIVIDEAMOUNT, PAYSTATUS, PAYERID, PAYTIME, REMARK, PARENTUSERID, PARENTACCOUNTID
    </sql>

    <select id="queryShareDayList" resultType="com.muchinfo.mtp3pojos.vo.settlement.SecondProfitSharResult">
        select a.tradedate,a.accountcurrencyid,
        sum(a.totalcount) as totalcount,
        sum(a.divideamount) as divideamount,
        sum(a.totalpay) as totalpay
        from (select
        t.tradedate,t.totalcount,t.totalfee,t.divideamount,t.accountcurrencyid,
        case when (t.paystatus = 3 or t.paystatus =5) then t.divideamount else 0 end totalpay
        from reckon_daydividecount t
        left join useraccount org on t.areaid = org.userid
        <where>
            t.areaid !=1
            <if test="tradedate != null and tradedate != '' ">
                and t.tradedate= #{tradedate}
            </if>
            <if test="areauserid != null and areauserid != '' ">
                and org.parentuserid=#{areauserid}
            </if>
            <choose>
                <when test="areatype != null">
                    and t.areatype = #{areatype,jdbcType=DECIMAL}
                </when>
                <otherwise>
                    and t.areatype != 12
                </otherwise>
            </choose>
        </where>
        ) a
        group by a.tradedate,a.accountcurrencyid
        order by a.tradedate desc
    </select>

    <select id="queryOrgShareDayList" resultType="com.muchinfo.mtp3pojos.vo.settlement.SecondProfitSharDetailResult"
            parameterType="com.muchinfo.mtp3pojos.vo.settlement.SecondProfitSharDetailParam">
        SELECT
        o.tradedate,
        o.areaid,
        o.areatype,
        o.accountcurrencyid,
        org.AccountName AS shareOrgName,
        o.totalcount,
        o.totalFee,
        o.divideamount,
        o.paystatus,
        o.payerid,
        o.paytime,
        o.remark,
        o.AREAID||'-'|| o.AREATYPE ||'-'||o.TRADEDATE as area
        FROM
        RECKON_DAYDIVIDECOUNT o
        LEFT JOIN USERACCOUNT org ON o.AREAID = org.USERID
        <where>
            o.areaid != 1
            <if test="vo.tradedate != null and vo.tradedate != '' ">
                and o.tradedate= #{vo.tradedate,jdbcType=CHAR}
            </if>
            <if test="vo.shareorgname != null and vo.shareorgname != '' ">
                and org.AccountName like '%'||#{vo.shareorgname,jdbcType=VARCHAR}||'%'
            </if>
            <if test="vo.paystatus != null and vo.paystatus != -1 ">
                and o.paystatus = #{vo.paystatus,jdbcType=DECIMAL}
            </if>
            <if test="vo.parentuserid != null">
                and org.parentuserid = #{vo.parentuserid,jdbcType=DECIMAL}
            </if>
            <if test="vo.accountcurrencyid != null">
                and o.accountcurrencyid = #{vo.accountcurrencyid,jdbcType=DECIMAL}
            </if>
            <choose>
                <when test="vo.areatype != null">
                    and o.areatype = #{vo.areatype,jdbcType=DECIMAL}
                </when>
                <otherwise>
                    and o.areatype != 12
                </otherwise>
            </choose>
        </where>
        ORDER BY TRADEDATE, PAYTIME DESC
    </select>

    <select id="queryPositionTransfer" resultType="com.muchinfo.mtp3pojos.vo.settlement.PositionTransferQueryResult">
        select t.*, g.goodscode || '/' || g.goodsname "goodsfullname",
        m.marketid || '/' || m.marketname "marketfullname",
        t.outloginid || '/' || ua1.accountname "outname",
        t.inloginid || '/' || ua2.accountname "inname",
        sm.logincode || '/' || sm.username "auditname"
        from Trade_PositionTransfer t
        left join goods g on t.goodsid = g.goodsid
        left join market m on t.marketid = m.marketid
        left join useraccount ua1 on ua1.userid = t.outuserid
        left join useraccount ua2 on ua2.userid = t.inuserid
        left join systemmanager sm on sm.autoid = t.auditorid
        <where>
            <if test="vo.transferapplystatus != null">
                and t.transferapplystatus=#{vo.transferapplystatus}
            </if>
            <if test="vo.goodsid != null">
                and t.goodsid=#{vo.goodsid}
            </if>
            <if test="vo.outaccountid != null">
                and t.outaccountid=#{vo.outaccountid}
            </if>
            <if test="vo.inaccountid != null">
                and t.inaccountid=#{vo.inaccountid}
            </if>
            <if test="vo.applyid != null">
                and t.applyid=#{vo.applyid}
            </if>
            <if test="vo.applydate != null and vo.applydate!=''">
                and (t.applytime between to_date(#{vo.applydate}||' 00:00:00','yyyy-MM-dd hh24:mi:ss')
                and to_date(#{vo.applydate}||' 23:59:59','yyyy-MM-dd hh24:mi:ss'))
            </if>
            <if test="vo.outuserid != null">
                and t.outuserid=#{vo.outuserid}
            </if>
        </where>
        order by t.applytime desc
    </select>
</mapper>
