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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.muchinfo.mtp3pojos.entity.Performanceplan">
        <id column="PERFORMANCEPLANID" property="performanceplanid" />
        <result column="PERFORMANCETYPE" property="performancetype" />
        <result column="RELATEDORDERID" property="relatedorderid" />
        <result column="AMOUNT" property="amount" />
        <result column="PAYMENTTYPE" property="paymenttype" />
        <result column="PERFORMANCESTATUS" property="performancestatus" />
        <result column="CURSTEPID" property="curstepid" />
        <result column="BUYACCOUNTID" property="buyaccountid" />
        <result column="WRTRANSFERUSERID" property="wrtransferuserid" />
        <result column="BUYPAIDAMOUNT" property="buypaidamount" />
        <result column="SELLACCOUNTID" property="sellaccountid" />
        <result column="SELLRECEIVEDAMOUNT" property="sellreceivedamount" />
        <result column="BUYTODAYAMOUNT" property="buytodayamount" />
        <result column="SELLTODAYAMOUNT" property="selltodayamount" />
        <result column="CREATETIME" property="createtime" />
        <result column="CREATORID" property="creatorid" />
        <result column="REMARK" property="remark" />
        <result column="BUYERFREEZEAMOUNT" property="buyerfreezeamount" />
        <result column="BUYERFREEZEAMOUNTREMAIN" property="buyerfreezeamountremain" />
        <result column="SELLERFREEZEAMOUNT" property="sellerfreezeamount" />
        <result column="SELLERFREEZEAMOUNTREMAIN" property="sellerfreezeamountremain" />
        <result column="OVERSHORTAMOUNT" property="overshortamount" />
        <result column="HASOVERSHORT" property="hasovershort" />
        <result column="SHIPREMARK" property="shipremark" />
        <result column="MIDDLEUSERID" property="middleuserid" />
        <result column="MIDDLEACCOUNTID" property="middleaccountid" />
        <result column="HASWR" property="haswr" />
        <result column="BUYERINFO" property="buyerinfo" />
        <result column="SELLERINFO" property="sellerinfo" />
        <result column="OVERSHORTQTY" property="overshortqty" />
        <result column="MARKETID" property="marketid" />
        <result column="EXPRESSFEECONFIRMED" property="expressfeeconfirmed" />
        <result column="EXPRESSFEE" property="expressfee" />
        <result column="CONTRACTID" property="contractid" />
        <result column="CURSTEPDEADLINE" property="curstepdeadline" />
        <result column="QTY" property="qty" />
        <result column="ACTIVATEMODE" property="activatemode" />
        <result column="ACTIVATEMONTH" property="activatemonth" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        PERFORMANCEPLANID, PERFORMANCETYPE, RELATEDORDERID, AMOUNT, PAYMENTTYPE, PERFORMANCESTATUS, CURSTEPID, BUYACCOUNTID, WRTRANSFERUSERID, BUYPAIDAMOUNT, SELLACCOUNTID, SELLRECEIVEDAMOUNT, BUYTODAYAMOUNT, SELLTODAYAMOUNT, CREATETIME, CREATORID, REMARK, BUYERFREEZEAMOUNT, BUYERFREEZEAMOUNTREMAIN, SELLERFREEZEAMOUNT, SELLERFREEZEAMOUNTREMAIN, OVERSHORTAMOUNT, HASOVERSHORT, SHIPREMARK, MIDDLEUSERID, MIDDLEACCOUNTID, HASWR, BUYERINFO, SELLERINFO, OVERSHORTQTY, MARKETID, EXPRESSFEECONFIRMED, EXPRESSFEE, CONTRACTID, CURSTEPDEADLINE, QTY, ACTIVATEMODE, ACTIVATEMONTH
    </sql>

    <select id="query" parameterType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceParam"
            resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceResult">
        with ps as(
        select p.*,st.steptypename from performancestep p
        left join performancesteptype st on p.steptypeid=st.steptypeid
        <where>
            <if test="vo.performancePlanId != null and vo.performancePlanId != '' ">
                AND p.planid = #{vo.performancePlanId,jdbcType=DECIMAL}
            </if>
        </where>
        )
        select p.performanceplanid,
			'- -' "histradedate",
			p.performancetype,
			p.relatedorderid,
			p.amount,
			p.paymenttype,
			p.performancestatus,
			p.curstepid,
			p.buyaccountid,
			p.wrtransferuserid,
			p.buypaidamount,
			p.sellaccountid,
			p.sellreceivedamount,
			p.buytodayamount,
			p.selltodayamount,
			p.createtime,
			p.creatorid,
			p.remark,
			p.overshortamount,
			p.hasovershort,
			p.shipremark,
			p.middleuserid,
			p.middleaccountid,
			p.haswr,
			p.buyerinfo,
			p.sellerinfo,
			p.overshortqty,
			p.buyerfreezeamount,
			p.buyerfreezeamountremain,
			p.sellerfreezeamount,
			p.sellerfreezeamountremain,
			p.marketid,
			p.expressfeeconfirmed,
			p.expressfee,
			p.contractid,
			p.curstepdeadline,
			p.qty,
			p.activatemode,
			p.activatemonth,
			ps.stepstatus,
			ps.steptypename,
			ps.executeside,
			ps.steptypeid,
			ps.delaydays,
			ps.remaindays,
			(p.amount + p.overshortamount) as rekonamount,
			(select t.stepstatus from performancestep t where t.planid=p.performanceplanid and t.steptypeid=11) as contractstatus,
			(select cpd.autoid from PerformanceDelay cpd where cpd.planid=p.performanceplanid and cpd.delaystatus=1 and rownum &lt; 2) as delayapplyid,
			(select cpd.applicant from PerformanceDelay cpd where cpd.planid=p.performanceplanid and cpd.delaystatus=1 and rownum &lt; 2) as applicant,
			0 as ishis
		from performanceplan p left join ps on p.curstepid = ps.performancestepid
		left join taaccount buyta on p.buyaccountid = buyta.accountid
		left join taaccount sellta on p.sellaccountid = sellta.accountid
        <where>
            <if test="vo.performancePlanId != null and vo.performancePlanId != '' ">
                AND p.performancePlanId =
                #{vo.performancePlanId,jdbcType=DECIMAL}
            </if>
            <if test="vo.buyId != null and vo.buyId != '' ">
                AND p.buyAccountId in (select accountid from taaccount
                where userid = #{vo.buyId,jdbcType=DECIMAL})
            </if>
            <if test="vo.sellId != null and vo.sellId != '' ">
                AND p.sellAccountId in (select accountid from taaccount
                where userid = #{vo.sellId,jdbcType=DECIMAL})
            </if>
            <if test="vo.buyAccountId != null and vo.buyAccountId != '' ">
                AND p.buyAccountId like '%'||#{vo.buyAccountId}||'%'
            </if>
            <if test="vo.sellAccountId != null and vo.sellAccountId != '' ">
                AND p.sellAccountId like '%'||#{vo.sellAccountId}||'%'
            </if>
            <if test="vo.relatedorderid != null and vo.relatedorderid != '' ">
                AND p.relatedOrderId like
                '%'||#{vo.relatedorderid}||'%'
            </if>
            <if test="vo.searchrange != null ">
                <if test="vo.searchrange == -1 ">
                    <![CDATA[and ps.remaindays < 0]]>
                </if>
            </if>
            <if test="vo.performancetype != null and vo.performancetype != '' ">
                AND p.performancetype = #{vo.performancetype,jdbcType=DECIMAL}
            </if>
            <if test="vo.performanceStatus != null and vo.performanceStatus != '' ">
                AND p.performancestatus = #{vo.performanceStatus,jdbcType=DECIMAL}
            </if>
            <if test="vo.parentuserid != null and vo.parentuserid!=0">
                and (buyta.userid in (select userid from useraccount uo
                start with uo.userid = #{vo.parentuserid}
                connect by prior uo.userid =uo.parentuserid and uo.userid !=#{vo.parentuserid})
                or sellta.userid in (select userid from useraccount uo
                start with uo.userid = #{vo.parentuserid}
                connect by prior uo.userid =uo.parentuserid and uo.userid !=#{vo.parentuserid})
                )
            </if>
        </where>
        order by p.performancestatus asc,p.createtime desc
    </select>

    <!-- 历史数据 -->
    <select id="queryHis"
            parameterType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceParam"
            resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceResult">
        with ps as(
        select p.*,st.steptypename from his_performancestep p
        left join performancesteptype st on p.steptypeid=st.steptypeid
        <where>
            and p.isvaliddata = 1
            <if test="vo.performancePlanId != null and vo.performancePlanId != '' ">
                AND p.planid = #{performancePlanId,jdbcType=DECIMAL}
            </if>
            <if test="vo.starttime != null and vo.starttime != '' ">
                AND p.histradedate &gt;= #{vo.starttime}
            </if>
            <if test="vo.endtime != null and vo.endtime != '' ">
                AND p.histradedate &lt;= #{vo.endtime}
            </if>
        </where>
        )
        select p.performanceplanid,
        p.histradedate,
        p.performancetype,
        p.relatedorderid,
        p.amount,
        p.paymenttype,
        p.performancestatus,
        p.curstepid,
        p.buyaccountid,
        p.wrtransferuserid,
        p.buypaidamount,
        p.sellaccountid,
        p.sellreceivedamount,
        p.buytodayamount,
        p.selltodayamount,
        p.createtime,
        p.creatorid,
        p.remark,
        p.overshortamount,
        p.hasovershort,
        p.shipremark,
        p.middleuserid,
        p.middleaccountid,
        p.haswr,
        p.buyerinfo,
        p.sellerinfo,
        p.overshortqty,
        p.buyerfreezeamount,
        p.buyerfreezeamountremain,
        p.sellerfreezeamount,
        p.sellerfreezeamountremain,
        p.marketid,
        p.expressfeeconfirmed,
        p.expressfee,
        ps.stepstatus,
        ps.steptypename,
        ps.executeside,
        ps.steptypeid,
        ps.delaydays,
        ps.remaindays,
        (p.amount + p.overshortamount) as rekonamount,
        (select t.stepstatus from his_performancestep t where t.planid=p.performanceplanid and t.steptypeid=11 and t.histradedate=p.histradedate and t.isvaliddata = 1) as contractstatus,
        1 as ishis
        from his_performanceplan p left join ps on p.curstepid = ps.performancestepid and p.histradedate = ps.histradedate
        left join taaccount buyta on p.buyaccountid = buyta.accountid
        left join taaccount sellta on p.sellaccountid = sellta.accountid
        where p.performancestatus in(3,6,7,8) and p.isvaliddata = 1
        <if test="vo.performancePlanId != null and vo.performancePlanId != '' ">
            AND p.performancePlanId =
            #{vo.performancePlanId,jdbcType=DECIMAL}
        </if>
        <if test="vo.buyId != null and vo.buyId != '' ">
            AND p.buyAccountId in (select accountid from taaccount
            where userid = #{vo.buyId,jdbcType=DECIMAL})
        </if>
        <if test="vo.sellId != null and vo.sellId != '' ">
            AND p.sellAccountId in (select accountid from taaccount
            where userid = #{vo.sellId,jdbcType=DECIMAL})
        </if>
        <if test="vo.buyAccountId != null and vo.buyAccountId != '' ">
            AND p.buyAccountId like '%'||#{vo.buyAccountId}||'%'
        </if>
        <if test="vo.sellAccountId != null and vo.sellAccountId != '' ">
            AND p.sellAccountId like '%'||#{vo.sellAccountId}||'%'
        </if>
        <if test="vo.relatedorderid != null and vo.relatedorderid != '' ">
            AND p.relatedOrderId like
            '%'||#{vo.relatedorderid}||'%'
        </if>
        <if test="vo.starttime != null and vo.starttime != '' ">
            AND p.histradedate &gt;= #{vo.starttime}
        </if>
        <if test="vo.endtime != null and vo.endtime != '' ">
            AND p.histradedate &lt;= #{vo.endtime}
        </if>
        <if test="vo.performancetype != null and vo.performancetype != '' ">
            AND p.performancetype = #{vo.performancetype,jdbcType=DECIMAL}
        </if>
        <if test="vo.performanceStatus != null and vo.performanceStatus != '' ">
            AND p.performancestatus = #{vo.performanceStatus,jdbcType=DECIMAL}
        </if>
        <if test="vo.searchrange != null ">
            <if test="vo.searchrange == -1 ">
                <![CDATA[and ps.remaindays < 0]]>
            </if>
        </if>
        <if test="vo.parentuserid != null and vo.parentuserid!=0">
            and (buyta.userid in (select userid from useraccount uo
            start with uo.userid = #{vo.parentuserid}
            connect by prior uo.userid =uo.parentuserid and uo.userid !=#{vo.parentuserid})
            or sellta.userid in (select userid from useraccount uo
            start with uo.userid = #{vo.parentuserid}
            connect by prior uo.userid =uo.parentuserid and uo.userid !=#{vo.parentuserid})
            )
        </if>
        order by p.performancestatus asc,p.createtime desc
    </select>

    <select id="queryPerformancePlanDetail" resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformancePlanResult">
        with ps as(
            select p.*,st.steptypename from performancestep p
                                                left join performancesteptype st on p.steptypeid=st.steptypeid
            where p.planid = #{performanceplanid,jdbcType=DECIMAL}
        )
        select p.*, ps.stepstatus, ps.steptypename, ua.accountname "wrtransferaccountname",
               (p.amount + NVL(p.overshortamount, 0) + NVL(p.expressfee, 0) - NVL(p.buypaidamount, 0)) "buyAmountReceived",
               (p.amount + NVL(p.overshortamount, 0) + NVL(p.expressfee, 0) - NVL(p.sellreceivedamount, 0)) "sellAmountReceived",
               ps.executeside, ps.steptypeid
        from performanceplan p left join ps on p.curstepid = ps.performancestepid
                               left join taaccount ta on p.wrtransferuserid = ta.accountid
                               left join useraccount ua on ta.userid = ua.userid
        where p.performanceplanid = #{planid}
    </select>

    <select id="getHisPerformancePlanDetail"
            resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformancePlanResult">
        with ps as(
        select p.*,st.steptypename from his_performancestep p
        left join performancesteptype st on p.steptypeid=st.steptypeid
        where p.planid = #{performanceplanid,jdbcType=DECIMAL} and p.isvaliddata = 1
        <if test="histradedate != null and histradedate != ''">
            and p.histradedate = #{histradedate}
        </if>
        )
        select p.*, ps.stepstatus, ps.steptypename, ua.accountname "wrtransferaccountname",
        (p.amount + NVL(p.overshortamount, 0) + NVL(p.expressfee, 0) - NVL(p.buypaidamount, 0)) "buyAmountReceived",
        (p.amount + NVL(p.overshortamount, 0) + NVL(p.expressfee, 0) - NVL(p.sellreceivedamount, 0))
        "sellAmountReceived",
        ps.executeside, ps.steptypeid
        from his_performanceplan p left join ps on p.curstepid = ps.performancestepid and p.histradedate =
        ps.histradedate
        left join taaccount ta on p.wrtransferuserid = ta.accountid
        left join useraccount ua on ta.userid = ua.userid
        where p.performanceplanid = #{planid} and p.isvaliddata = 1
        <if test="histradedate != null and histradedate != ''">
            and p.histradedate = #{histradedate}
        </if>
    </select>

    <select id="queryPerformanceDelayList" resultType="com.muchinfo.mtp3pojos.vo.performance.PerformanceDelayResult">
        select p.*, t.steptypename, f.relatedorderid,f.performancetype,ps.executeside
        from PerformanceDelay p
        left join performancestep ps on p.stepid = ps.performancestepid
        left join PerformanceStepType t on ps.steptypeid = t.steptypeid
        left join PerformancePlan f on ps.planid = f.performanceplanid
        <where>
            <if test="planid != null">
                and p.planid = #{planid}
                and p.applytime=(select max(tp.applytime) from performancedelay tp where tp.planid=#{planid})
            </if>

        </where>
        order by p.applytime desc
    </select>

    <!-- 竞拍成交单表查询 -->
    <select id="queryReportInfo" resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceReportResult">
        select t.tradeid "tradeId",
        t.buyorsell "buyOrSell",
        dic1.enumdicname "buyOrSellText",
        t.orderid "orderId",
        t.tradedate "tradeDate",
        t.accountid "accountId",
        t.goodsid "goodsId",
        t2.goodsname "goodsName",
        t2.goodscode "goodsCode",
        t2.unit "unit",
        t.marketid "marketId",
        m.marketname "marketName",
        t.deliverygoodsid "deliveryGoodsId",
        dg.deliverygoodscode "deliveryGoodsCode",
        dg.deliverygoodsname "deliveryGoodsName",
        t.memberuserid "memberUserId",
        mem.accountname "memberName",
        t.matchaccountid "matchAccountId",
        t.tradetime "tradeTime",
        t.tradeprice "tradePrice",
        t.tradeqty "tradeQty",
        t.tradeamount "tradeAmount",
        t.charge "charge",
        t.status "status",
        (case when t.status=1 then '违约待处理' when t.status=2 then '违约处理中' when t.status=3 then '完成' when
        t.status=4 then '违约完成'
        when t.status=5 then '履约中' when t.status=6 then '履约完成' when t.status=7 then '流拍' else '--' end)
        "statusText",
        t.handlestatus "handleStatus",
        (case when t.handlestatus=1 then '待处理' when t.handlestatus=2 then '已处理' when t.handlestatus=3 then '处理失败'
        else '--' end) "handleStatusText",
        t.relatedwrid "relatedWrid",
        t.feealgorithm "feeAlgorithm",
        dic2.enumdicname "feeAlgorithmText",
        t.exchagechargevalue "exchageChargeValue",
        t.ismain "isMain",
        (case when t.ismain=1 then '是' else '否' end) "isMainText",
        t.isreckoned "isReckoned",
        (case when t.isreckoned=0 then '未结算' else '已结算' end) "isReckonedText",
        t.performanceplanid "performancePlanId",
        (select to_char(wm_concat(dm.pricemovedesc))
        from deliverygoodspricemove dm
        where dm.deliverygoodsid = t2.deliverygoodsid
        and dm.autoid in (t2.brandid, t2.qualityid, t2.specid,
        t2.warehouseid, t2.deliverymonthid)) "wrfactorTypeName",
        '--' "hisTradeDate"
        from auction_tradedetail t
        left join enumdicitem dic1 on dic1.enumitemname=t.buyorsell and dic1.enumdiccode='buyOrSell'
        left join auction_orderinfo t2 on t.goodsid=t2.goodsid
        left join market m on t.marketid=m.marketid
        left join deliverygoods dg on t.deliverygoodsid=dg.deliverygoodsid
        left join useraccount mem on t.memberuserid=mem.userid
        left join enumdicitem dic2 on dic2.enumitemname=t.feealgorithm and dic2.enumdiccode='SCFRiskMode'
        <where>
            <if test="planid != null and planid != ''">
                and t.tradeid=#{planid}
            </if>
            <if test="buyorsell != null">
                <![CDATA[and t.buyorsell=#{buyOrSell}]]>
            </if>
        </where>
    </select>

    <!--竞拍成交单历史表查询 -->
    <select id="queryHisReportInfo" resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceReportResult">
        select t.tradeid "tradeId",
        t.buyorsell "buyOrSell",
        dic1.enumdicname "buyOrSellText",
        t.orderid "orderId",
        t.tradedate "tradeDate",
        t.accountid "accountId",
        t.goodsid "goodsId",
        t2.goodsname "goodsName",
        t2.goodscode "goodsCode",
        t2.unit "unit",
        t.marketid "marketId",
        m.marketname "marketName",
        t.deliverygoodsid "deliveryGoodsId",
        dg.deliverygoodscode "deliveryGoodsCode",
        dg.deliverygoodsname "deliveryGoodsName",
        t.memberuserid "memberUserId",
        mem.accountname "memberName",
        t.matchaccountid "matchAccountId",
        t.tradetime "tradeTime",
        t.tradeprice "tradePrice",
        t.tradeqty "tradeQty",
        t.tradeamount "tradeAmount",
        t.charge "charge",
        t.status "status",
        (case when t.status=1 then '违约待处理' when t.status=2 then '违约处理中' when t.status=3 then '完成' when
        t.status=4 then '违约完成'
        when t.status=5 then '履约中' when t.status=6 then '履约完成' when t.status=7 then '流拍' else '--' end)
        "statusText",
        t.handlestatus "handleStatus",
        (case when t.handlestatus=1 then '待处理' when t.handlestatus=2 then '已处理' when t.handlestatus=3 then '处理失败'
        else '--' end) "handleStatusText",
        t.relatedwrid "relatedWrid",
        t.feealgorithm "feeAlgorithm",
        dic2.enumdicname "feeAlgorithmText",
        t.exchagechargevalue "exchageChargeValue",
        t.ismain "isMain",
        (case when t.ismain=1 then '是' else '否' end) "isMainText",
        t.isreckoned "isReckoned",
        (case when t.isreckoned=0 then '未结算' else '已结算' end) "isReckonedText",
        t.performanceplanid "performancePlanId",
        (select to_char(wm_concat(dm.pricemovedesc))
        from deliverygoodspricemove dm
        where dm.deliverygoodsid = t2.deliverygoodsid
        and dm.autoid in (t2.brandid, t2.qualityid, t2.specid,
        t2.warehouseid, t2.deliverymonthid)) "wrfactorTypeName",
        t.histradedate "hisTradeDate"
        from his_auction_tradedetail t
        left join enumdicitem dic1 on dic1.enumitemname=t.buyorsell and dic1.enumdiccode='buyOrSell'
        left join his_auction_orderinfo t2 on t.goodsid=t2.goodsid and t2.histradedate=t.histradedate and t2.isvaliddata
        = 1
        left join market m on t.marketid=m.marketid
        left join deliverygoods dg on t.deliverygoodsid=dg.deliverygoodsid
        left join useraccount mem on t.memberuserid=mem.userid
        left join enumdicitem dic2 on dic2.enumitemname=t.feealgorithm and dic2.enumdiccode='SCFRiskMode'
        <where>
            and t.isvaliddata = 1
            <if test="planid != null and planid != ''">
                and t.tradeid=#{planid}
            </if>
            <if test="buyOrSell != null or buyOrSell == 0">
                <![CDATA[and t.buyorsell=#{buyOrSell}]]>
            </if>
            <if test="histradedate != null and histradedate != ''">
                <![CDATA[and t.histradedate=#{histradedate}]]>
            </if>
        </where>
    </select>
    <select id="performancedelay"
            resultType="com.muchinfo.mtp3pojos.vo.performance.PerformanceDelayApplyResult">
        select p.*, t.steptypename, f.relatedorderid,f.performancetype,ps.executeside
        from PerformanceDelay p
        left join performancestep ps on p.stepid = ps.performancestepid
        left join PerformanceStepType t on ps.steptypeid = t.steptypeid
        left join PerformancePlan f on ps.planid = f.performanceplanid
        <where>
            <if test="vo.relatedorderid != null">
                and f.relatedorderid like '%'||#{vo.relatedorderid}||'%'
            </if>
            <if test="vo.applicant != null">
                and p.applicant = #{vo.applicant}
            </if>
        </where>
        order by p.applytime desc
    </select>
    <select id="breachapply" resultType="com.muchinfo.mtp3pojos.vo.performance.PerformanceDelayApplyResult">
        select a.*, p.performancetype, p.relatedorderid
        from performancebreachapply a
        left join performanceplan p on a.planid = p.performanceplanid
        <where>
            <if test="vo.relatedorderid != null">
                and p.relatedorderid like '%'||#{vo.relatedorderid}||'%'
            </if>
            <if test="vo.applicant != null">
                and a.applicant = #{vo.applicant}
            </if>
        </where>
        order by a.breachapplytime desc
    </select>

    <!-- 预售集采成交单查询 -->
    <select id="queryPreSaleTradeDetail" resultType="com.muchinfo.mtp3pojos.vo.performance.PayPerformanceReportResult">
        select t.tradeid "tradeId",
        t.buyorsell "buyOrSell",
        dic1.enumdicname "buyOrSellText",
        t.orderid "orderId",
        t.listingid "listingId",
        t.tradedate "tradeDate",
        t.accountid "accountId",
        t.memberuserid "memberUserId",
        mem.accountname "memberName",
        t.matchaccountid "matchAccountId",
        to_char(t.tradetime,'yyyy-mm-dd hh24:mi:ss') "tradeTime",
        t.tradeprice "tradePrice",
        t.tradeqty "tradeQty",
        t.tradeamount "tradeAmount",
        t.charge "charge",
        t.handlestatus "handleStatus",
        (case when t.handlestatus=1 then '待处理' when t.handlestatus=2 then '已处理' when t.handlestatus=3 then '处理失败'
        else '--' end) "handleStatusText",
        t.chargealgorithm "chargeAlgorithm",
        dic2.enumdicname "chargeAlgorithmText",
        t.chargevalue "chargeValue",
        t.ismain "isMain",
        (case when t.ismain=1 then '是' else '否' end) "isMainText",
        t.isreckoned "isReckoned",
        (case when t.isreckoned=0 then '未结算' else '已结算' end) "isReckonedText",
        t.performanceplanid "performancePlanId",
        t.goodscode "goodsCode",
        t2.goodsname "goodsName",
        t2.unit "unit",
        m.marketname "marketName",
        '--' "hisTradeDate"
        from PreSale_TradeDetail t
        left join enumdicitem dic1 on dic1.enumitemname=t.buyorsell and dic1.enumdiccode='buyOrSell'
        left join useraccount mem on mem.userid=t.memberuserid
        left join enumdicitem dic2 on dic2.enumitemname=t.chargealgorithm and dic2.enumdiccode='SCFRiskMode'
        left join (
        select t.listingid,t.goodsname,t.unit from his_presale_listinginfo t where t.isvaliddata = 1
        union
        select t.listingid,t.goodsname,t.unit from presale_listinginfo t
        ) t2 on t2.listingid=t.listingid
        left join market m on t.marketid=m.marketid
        <where>
            <if test="planid != null and planid != ''">
                and t.tradeid=#{planid}
            </if>
            <if test="buyOrSell != null">
                <![CDATA[and t.buyorsell=#{buyOrSell}]]>
            </if>
        </where>
    </select>
    <select id="queryBreachDel" resultType="com.muchinfo.mtp3pojos.vo.performance.PerformanceDelayApplyResult">
        select b.*,p.relatedorderid,a.auditor,p.performancetype,a.applicant
        <if test="vo.ishis == 1">
            ,1 as ishis,ps.executeside
        </if>
        <choose>
            <when test="vo.ishis == 1">
                from performancebreach b
                left join performanceplan p on b.planid = p.performanceplanid
                left join performancebreachapply a on b.breachapplyid = a.breachapplyid
            </when>
            <otherwise>
                from his_performancebreach b
                left join his_performanceplan p on b.planid = p.performanceplanid and b.histradedate=p.histradedate and p.isvaliddata = 1
                left join his_performancebreachapply a on b.breachapplyid = a.breachapplyid and b.histradedate=a.histradedate and a.isvaliddata = 1
                left join his_performancestep ps on p.curstepid=ps.performancestepid and p.histradedate=ps.histradedate and ps.isvaliddata = 1
            </otherwise>
        </choose>
        <where>
            <if test="vo.ishis == 1">
                and b.isvaliddata = 1
                <if test="vo.tradedatestart != null and vo.tradedatestart != '' ">
                    and b.histradedate &gt;= #{vo.tradedatestart}
                </if>
                <if test="vo.tradedateend != null and vo.tradedateend != '' ">
                    and b.histradedate &lt;= #{vo.tradedateend}
                </if>
            </if>
            <if test="vo.relatedorderid != null">
                and p.relatedorderid like '%'||#{vo.relatedorderid}||'%'
            </if>
            <if test="vo.applicant != null">
                and a.applicant = #{vo.applicant}
            </if>
        </where>
    </select>
</mapper>
