<?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="cn.superdesk.uniorder.dao.mapper.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportMapperEx">

    <resultMap id="simulateGoodsSelectedDimOrderResultMap"
               type="cn.superdesk.uniorder.dto.param.thirdpart.jd.AccountingUnifiedSupplierJdOpCreateParamDimGoodsSimulateSelectedDimOrder">

        <!-- extends AccountingUnifiedSupplierCreateParamDimGoodsSelected start -->
        <result column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>

        <result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
        <result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
        <!-- extends AccountingUnifiedSupplierCreateParamDimGoodsSelected   end -->

        <result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>

        <result column="order_accounting_state_supplier" jdbcType="INTEGER" property="orderAccountingStateSupplier"/>
        <result column="goods_accounting_state_supplier" jdbcType="INTEGER" property="goodsAccountingStateSupplier"/>
    </resultMap>


    <sql id="subQuery">
        SELECT
            order_sn
        FROM
            accounting_unified_supplier_order_jd_check_up_report
        <where>

        <if test="batchId != null and batchId != '' ">
            and batch_id = #{batchId}
        </if>
        <!-- 对账结果:0-待对账;1-正常;2-异常; 引用 enum ScheduleTaskCheckStateEnum -->
        <if test="processResult != null ">
            and process_result = #{processResult}
        </if>

        <!-- ⑤第三方订单编号:对当前列表数据`第三方订单编号`字段内容,`精确`查询,可能存在重复多个; 订单编号 215961349611 -->
        <if test="jdOrderSn != null and jdOrderSn != '' ">
            and jd_order_sn = #{jdOrderSn}
        </if>

        <choose>
            <when test="orderSn != null and orderSn != '' ">
                and order_sn = #{orderSn}
            </when>
            <otherwise>
                and order_sn IS NOT NULL
            </otherwise>
        </choose>

        </where>

    </sql>
    <select id="simulateGoodsSelectedDimOrder"
            resultMap="simulateGoodsSelectedDimOrderResultMap">

        SELECT
            uo.id as unified_order_id,
            uo.order_sn,
            uo.accounting_state_supplier as order_accounting_state_supplier,
            uog.id as unified_order_goods_id,
            uog.goods_id,
            uog.accounting_state_supplier as goods_accounting_state_supplier
        FROM
            unified_order_goods uog
            LEFT JOIN unified_order uo ON uog.order_sn = uo.order_sn
        <where>
            and (
                uo.order_sn in (
                    <include refid="subQuery"></include>
                )
                or uo.refund_order_sn in (
                    <include refid="subQuery"></include>
                )
            )

        <!-- 供应商对账:1-已对账;2-未对账 -->
        <if test="accountingStateSupplier != null and accountingStateSupplier != ''">
            and uog.accounting_state_supplier = #{accountingStateSupplier}
        </if>
        </where>
    </select>

    <resultMap id="queryGenericParamResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportListItemVo">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="batch_id" jdbcType="VARCHAR" property="batchId" />
        <result column="accounting_unified_supplier_order_jd_id" jdbcType="BIGINT" property="accountingUnifiedSupplierOrderJdId" />
        <result column="jd_accounting_order_sn" jdbcType="VARCHAR" property="jdAccountingOrderSn" />
        <result column="jd_order_sn" jdbcType="VARCHAR" property="jdOrderSn" />
        <result column="jd_original_order_sn" jdbcType="VARCHAR" property="jdOriginalOrderSn" />
        <result column="jd_order_amount" jdbcType="DECIMAL" property="jdOrderAmount" />
        <result column="whole_order_amount" jdbcType="DECIMAL" property="wholeOrderAmount" />
        <result column="jd_repayment_amount" jdbcType="DECIMAL" property="jdRepaymentAmount" />
        <result column="jd_refund_amount" jdbcType="DECIMAL" property="jdRefundAmount" />
        <result column="jd_out_standing_amount" jdbcType="DECIMAL" property="jdOutStandingAmount" />
        <result column="jd_order_state" jdbcType="TINYINT" property="jdOrderState" />
        <result column="jd_order_state_name" jdbcType="VARCHAR" property="jdOrderStateName" />
        <result column="were_accounted_state" jdbcType="TINYINT" property="wereAccountedState" />
        <result column="were_accounted_state_name" jdbcType="VARCHAR" property="wereAccountedStateName" />
        <result column="after_sale_state" jdbcType="TINYINT" property="afterSaleState" />
        <result column="after_sale_state_name" jdbcType="VARCHAR" property="afterSaleStateName" />
        <result column="order_sn" jdbcType="VARCHAR" property="orderSn" />
        <result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
        <result column="order_cost_amount" jdbcType="DECIMAL" property="orderCostAmount" />
        <result column="refund_order_amount" jdbcType="DECIMAL" property="refundOrderAmount" />
        <result column="refund_order_cost_amount" jdbcType="DECIMAL" property="refundOrderCostAmount" />
        <result column="transport_amount" jdbcType="DECIMAL" property="transportAmount" />
        <result column="transport_merchant_assume_amount" jdbcType="DECIMAL" property="transportMerchantAssumeAmount" />
        <result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" />
        <result column="goods_cost_amount" jdbcType="DECIMAL" property="goodsCostAmount" />
        <result column="refund_goods_amount" jdbcType="DECIMAL" property="refundGoodsAmount" />
        <result column="refund_goods_cost_amount" jdbcType="DECIMAL" property="refundGoodsCostAmount" />
        <result column="order_state" jdbcType="TINYINT" property="orderState" />
        <result column="complet_state" jdbcType="TINYINT" property="completState" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="process_result" jdbcType="TINYINT" property="processResult" />
        <result column="process_report" jdbcType="LONGVARCHAR" property="processReport" />

        <result column="compared_whole_amount" jdbcType="DECIMAL" property="comparedWholeAmount" />
        <result column="compared_order_amount" jdbcType="DECIMAL" property="comparedOrderAmount" />
        <result column="compared_refund_order_amount" jdbcType="DECIMAL" property="comparedRefundOrderAmount" />
        <result column="compared_transport_amount" jdbcType="DECIMAL" property="comparedTransportAmount" />
        <result column="define_error" jdbcType="TINYINT" property="defineError" />

        <result column="order_accounting_state_supplier" jdbcType="INTEGER" property="orderAccountingStateSupplier" />
        <result column="goods_accounting_state_supplier_eq_minus_1" jdbcType="INTEGER" property="goodsAccountingStateSupplierEqMinus1" />
        <result column="goods_accounting_state_supplier_eq_2" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq2" />
        <result column="goods_accounting_state_supplier_eq_1" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq1" />
        <result column="goods_accounting_state_supplier_eq_4" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq4" />
        <result column="order_goods_item_quantity" jdbcType="INTEGER" property="orderGoodsItemQuantity" />


<!--        ausuojcurxg.doggod_order_sn_as_whole,-->
<!--        ausuojcurxg.doggod_order_accounting_state_supplier,-->
<!--        ausuojcurxg.doggod_goods_accounting_state_supplier,-->
<!--        ausuojcurxg.order_sn as goddog_order_sn,-->
<!--        ausuojcurxg.process_result as goddog_process_result,-->

        <result column="doggod_order_sn_as_whole" jdbcType="VARCHAR" property="doggodOrderSnAsWhole" />
        <result column="doggod_order_accounting_state_supplier" jdbcType="TINYINT" property="doggodOrderAccountingStateSupplier" />
        <result column="doggod_goods_accounting_state_supplier" jdbcType="TINYINT" property="doggodGoodsAccountingStateSupplier" />
        <result column="goddog_order_sn" jdbcType="VARCHAR" property="goddogOrderSn" />
        <result column="goddog_process_result" jdbcType="TINYINT" property="goddogProcessResult" />


        <result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId" />
        <result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId" />
        <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
        <result column="goods_quantity" jdbcType="INTEGER" property="goodsQuantity" />
        <result column="goods_cost_price" jdbcType="DECIMAL" property="goodsCostPrice" />
        <!-- goods_cost_amount -->
        <result column="goods_cost_price_x_quantity" jdbcType="DECIMAL" property="goodsCostPriceXQuantity" />
    </resultMap>

    <sql id="conditionPart">


        <!-- 1.2.4.1 (5)对账单号:对当前列表数据`对账单号`字段内容,`精确`查询; 批次Id -->
        <if test="batchId != null and batchId != '' ">
            and ausuojcur.batch_id = #{batchId}
        </if>

        <!-- ⑤第三方订单编号:对当前列表数据`第三方订单编号`字段内容,`精确`查询,可能存在重复多个; 订单编号 215961349611 -->
        <if test="jdOrderSn != null and jdOrderSn != '' ">
            and ausuojcur.jd_order_sn = #{jdOrderSn}
        </if>

        <!-- ⑥结算中心订单编号:对当前列表数据中`结算中心订单编号`字段内容,`精确`查询;结算中心订单编号-统一订单-第三方订单编号
        <if test="orderSn != null and orderSn != '' ">
            and ausuojcur.order_sn = #{orderSn}
        </if>
        -->

        <!-- 对账结果:0-待对账;1-正常;2-异常; 引用 enum ScheduleTaskCheckStateEnum -->
        <if test="processResult != null ">
            and ausuojcur.process_result = #{processResult}
        </if>


    </sql>

    <select id="queryGenericParam" resultMap="queryGenericParamResultMap">

        select
            ausuojcur.id,
            ausuojcur.batch_id,
            ausuojcur.accounting_unified_supplier_order_jd_id,
            ausuojcur.jd_accounting_order_sn,
            ausuojcur.jd_order_sn,
            ausuojcur.jd_original_order_sn,

            ausuojcur.jd_order_amount,
            ausuojcur.whole_order_amount,

            ausuojcur.jd_repayment_amount,
            ausuojcur.jd_refund_amount,
            ausuojcur.jd_out_standing_amount,
            ausuojcur.jd_order_state,
            ausuojcur.jd_order_state_name,
            ausuojcur.were_accounted_state,
            ausuojcur.were_accounted_state_name,
            ausuojcur.after_sale_state,
            ausuojcur.after_sale_state_name,
            ausuojcur.order_sn,
            ausuojcur.order_amount,
            ausuojcur.order_cost_amount,
            ausuojcur.refund_order_amount,
            ausuojcur.refund_order_cost_amount,
            ausuojcur.transport_amount,
            ausuojcur.transport_merchant_assume_amount,
            ausuojcur.goods_amount,
            ausuojcur.goods_cost_amount,
            ausuojcur.refund_goods_amount,
            ausuojcur.refund_goods_cost_amount,
            ausuojcur.order_state,
            ausuojcur.complet_state,
            ausuojcur.create_time,
            ausuojcur.process_result,
            ausuojcur.process_report,

            ausuojcur.compared_whole_amount,
            ausuojcur.compared_order_amount,
            ausuojcur.compared_refund_order_amount,
            ausuojcur.compared_transport_amount,
            ausuojcur.define_error,

            ausuojcur.order_accounting_state_supplier,
            ausuojcur.goods_accounting_state_supplier_eq_minus_1,
            ausuojcur.goods_accounting_state_supplier_eq_2,
            ausuojcur.goods_accounting_state_supplier_eq_1,
            ausuojcur.goods_accounting_state_supplier_eq_4,
            ausuojcur.order_goods_item_quantity,

            ausuojcurxg.doggod_order_sn_as_whole,
            ausuojcurxg.doggod_order_accounting_state_supplier,
            ausuojcurxg.doggod_goods_accounting_state_supplier,
            ausuojcurxg.order_sn as goddog_order_sn,
            ausuojcurxg.process_result as goddog_process_result,

            ausuojcurxg.unified_order_id,
            ausuojcurxg.unified_order_goods_id,
            ausuojcurxg.goods_id,
            ausuojcurxg.goods_quantity,
            ausuojcurxg.goods_cost_price,
            ausuojcurxg.goods_cost_amount as goods_cost_price_x_quantity
        from
            accounting_unified_supplier_order_jd_check_up_report ausuojcur
            LEFT JOIN accounting_unified_supplier_order_jd_check_up_report_x_goods ausuojcurxg
                on ausuojcur.batch_id = ausuojcurxg.batch_id
                    and ausuojcur.order_sn = ausuojcurxg.doggod_order_sn_as_whole
                    and ausuojcur.accounting_unified_supplier_order_jd_id = ausuojcurxg.accounting_unified_supplier_order_jd_id
            LEFT JOIN unified_order uo on ausuojcurxg.unified_order_id = uo.id
            LEFT JOIN unified_order_goods uog on ausuojcurxg.unified_order_goods_id = uog.id
        <where>
            <include refid="conditionPart"></include>
        </where>

    </select>


    <resultMap id="pageQueryGenericWoofResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportListItemVoWoof">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="batch_id" jdbcType="VARCHAR" property="batchId" />
        <result column="accounting_unified_supplier_order_jd_id" jdbcType="BIGINT" property="accountingUnifiedSupplierOrderJdId" />
        <result column="jd_accounting_order_sn" jdbcType="VARCHAR" property="jdAccountingOrderSn" />
        <result column="jd_order_sn" jdbcType="VARCHAR" property="jdOrderSn" />
        <result column="jd_original_order_sn" jdbcType="VARCHAR" property="jdOriginalOrderSn" />
        <result column="jd_order_amount" jdbcType="DECIMAL" property="jdOrderAmount" />
        <result column="whole_order_amount" jdbcType="DECIMAL" property="wholeOrderAmount" />
        <result column="jd_repayment_amount" jdbcType="DECIMAL" property="jdRepaymentAmount" />
        <result column="jd_refund_amount" jdbcType="DECIMAL" property="jdRefundAmount" />
        <result column="jd_out_standing_amount" jdbcType="DECIMAL" property="jdOutStandingAmount" />
        <result column="jd_order_state" jdbcType="TINYINT" property="jdOrderState" />
        <result column="jd_order_state_name" jdbcType="VARCHAR" property="jdOrderStateName" />
        <result column="were_accounted_state" jdbcType="TINYINT" property="wereAccountedState" />
        <result column="were_accounted_state_name" jdbcType="VARCHAR" property="wereAccountedStateName" />
        <result column="after_sale_state" jdbcType="TINYINT" property="afterSaleState" />
        <result column="after_sale_state_name" jdbcType="VARCHAR" property="afterSaleStateName" />
        <result column="order_sn" jdbcType="VARCHAR" property="orderSn" />
        <result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
        <result column="order_cost_amount" jdbcType="DECIMAL" property="orderCostAmount" />
        <result column="refund_order_amount" jdbcType="DECIMAL" property="refundOrderAmount" />
        <result column="refund_order_cost_amount" jdbcType="DECIMAL" property="refundOrderCostAmount" />
        <result column="transport_amount" jdbcType="DECIMAL" property="transportAmount" />
        <result column="transport_merchant_assume_amount" jdbcType="DECIMAL" property="transportMerchantAssumeAmount" />
        <result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" />
        <result column="goods_cost_amount" jdbcType="DECIMAL" property="goodsCostAmount" />
        <result column="refund_goods_amount" jdbcType="DECIMAL" property="refundGoodsAmount" />
        <result column="refund_goods_cost_amount" jdbcType="DECIMAL" property="refundGoodsCostAmount" />
        <result column="order_state" jdbcType="TINYINT" property="orderState" />
        <result column="complet_state" jdbcType="TINYINT" property="completState" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="process_result" jdbcType="TINYINT" property="processResult" />
        <result column="process_report" jdbcType="LONGVARCHAR" property="processReport" />

        <result column="compared_whole_amount" jdbcType="DECIMAL" property="comparedWholeAmount" />
        <result column="compared_order_amount" jdbcType="DECIMAL" property="comparedOrderAmount" />
        <result column="compared_refund_order_amount" jdbcType="DECIMAL" property="comparedRefundOrderAmount" />
        <result column="compared_transport_amount" jdbcType="DECIMAL" property="comparedTransportAmount" />
        <result column="define_error" jdbcType="TINYINT" property="defineError" />

        <result column="order_accounting_state_supplier" jdbcType="INTEGER" property="orderAccountingStateSupplier" />
        <result column="goods_accounting_state_supplier_eq_minus_1" jdbcType="INTEGER" property="goodsAccountingStateSupplierEqMinus1" />
        <result column="goods_accounting_state_supplier_eq_2" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq2" />
        <result column="goods_accounting_state_supplier_eq_1" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq1" />
        <result column="goods_accounting_state_supplier_eq_4" jdbcType="INTEGER" property="goodsAccountingStateSupplierEq4" />
        <result column="order_goods_item_quantity" jdbcType="INTEGER" property="orderGoodsItemQuantity" />

    </resultMap>

    <select id="pageQueryGenericWoof" resultMap="pageQueryGenericWoofResultMap">
        select
            ausuojcur.id,
            ausuojcur.batch_id,
            ausuojcur.accounting_unified_supplier_order_jd_id,
            ausuojcur.jd_accounting_order_sn,
            ausuojcur.jd_order_sn,
            ausuojcur.jd_original_order_sn,

            ausuojcur.jd_order_amount,
            ausuojcur.whole_order_amount,

            ausuojcur.jd_repayment_amount,
            ausuojcur.jd_refund_amount,
            ausuojcur.jd_out_standing_amount,
            ausuojcur.jd_order_state,
            ausuojcur.jd_order_state_name,
            ausuojcur.were_accounted_state,
            ausuojcur.were_accounted_state_name,
            ausuojcur.after_sale_state,
            ausuojcur.after_sale_state_name,
            ausuojcur.order_sn,
            ausuojcur.order_amount,
            ausuojcur.order_cost_amount,
            ausuojcur.refund_order_amount,
            ausuojcur.refund_order_cost_amount,
            ausuojcur.transport_amount,
            ausuojcur.transport_merchant_assume_amount,
            ausuojcur.goods_amount,
            ausuojcur.goods_cost_amount,
            ausuojcur.refund_goods_amount,
            ausuojcur.refund_goods_cost_amount,
            ausuojcur.order_state,
            ausuojcur.complet_state,
            ausuojcur.create_time,
            ausuojcur.process_result,
            ausuojcur.process_report,

            ausuojcur.compared_whole_amount,
            ausuojcur.compared_order_amount,
            ausuojcur.compared_refund_order_amount,
            ausuojcur.compared_transport_amount,
            ausuojcur.define_error,

            ausuojcur.order_accounting_state_supplier,
            ausuojcur.goods_accounting_state_supplier_eq_minus_1,
            ausuojcur.goods_accounting_state_supplier_eq_2,
            ausuojcur.goods_accounting_state_supplier_eq_1,
            ausuojcur.goods_accounting_state_supplier_eq_4,
            ausuojcur.order_goods_item_quantity

        from
            accounting_unified_supplier_order_jd_check_up_report ausuojcur
            <!--
            LEFT JOIN accounting_unified_supplier_order_jd_check_up_report_x_goods ausuojcurxg
            on ausuojcur.batch_id = ausuojcurxg.batch_id
            and ausuojcur.order_sn = ausuojcurxg.doggod_order_sn_as_whole
            and ausuojcur.accounting_unified_supplier_order_jd_id = ausuojcurxg.accounting_unified_supplier_order_jd_id
            -->

        <where>
            <!-- 1.2.4.1 (5)对账单号:对当前列表数据`对账单号`字段内容,`精确`查询; 批次Id -->
            <if test="batchId != null and batchId != '' ">
                and ausuojcur.batch_id = #{batchId}
            </if>

            <!-- ⑤第三方订单编号:对当前列表数据`第三方订单编号`字段内容,`精确`查询,可能存在重复多个; 订单编号 215961349611 -->
            <if test="jdOrderSn != null and jdOrderSn != '' ">
                and ausuojcur.jd_order_sn = #{jdOrderSn}
            </if>

            <!-- ⑥结算中心订单编号:对当前列表数据中`结算中心订单编号`字段内容,`精确`查询;结算中心订单编号-统一订单-第三方订单编号 -->
            <!--
            <if test="orderSn != null and orderSn != '' ">
                and ausuojcur.order_sn = #{orderSn}
            </if>
            -->

            <!-- 对账结果:0-待对账;1-正常;2-异常; 引用 enum ScheduleTaskCheckStateEnum -->
            <if test="processResult != null ">
                and ausuojcur.process_result = #{processResult}
            </if>

            <if test="orderSn != null and orderSn != '' ">
                and ausuojcur.accounting_unified_supplier_order_jd_id IN (
                <include refid="pageQueryGenericWoofSubQuery"></include>
                )
            </if>
        </where>
    </select>

    <sql id="pageQueryGenericWoofSubQuery">
        SELECT
            DISTINCT accounting_unified_supplier_order_jd_id
        FROM
            accounting_unified_supplier_order_jd_check_up_report_x_goods
        <where>

            <if test="batchId != null and batchId != '' ">
                and batch_id = #{batchId}
            </if>

            <if test="orderSn != null and orderSn != '' ">
                and order_sn = #{orderSn}
            </if>
            <!--
            <if test="doggodOrderSnAsshole != null and doggodOrderSnAsshole != '' ">
                and doggod_order_sn_as_whole = #{doggod_order_sn_as_whole}
            </if>
            -->

        </where>

    </sql>


    <resultMap id="queryHeadGenericParamResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportDetailStatisticsVo">

        <result column="statistics_quantity_dim_order_outsider" jdbcType="BIGINT" property="statisticsQuantityDimOrderOutsider" />
        <result column="statistics_quantity_dim_order_ownsider" jdbcType="BIGINT" property="statisticsQuantityDimOrderOwnsider" />

        <result column="statistics_amount_dim_order_outsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOutsider" />
        <result column="statistics_amount_dim_order_ownsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOwnsider" />
        <result column="statistics_amount_dim_order_diff" jdbcType="DECIMAL" property="statisticsAmountDimOrderDiff" />
    </resultMap>

    <select id="pageQueryHeadGenericParam"
            resultMap="queryHeadGenericParamResultMap">

        SELECT
            IFNULL(SUM(statistics.statistics_quantity_dim_order_outsider),0) AS statistics_quantity_dim_order_outsider,
            IFNULL(SUM(statistics.statistics_quantity_dim_order_ownsider),0) AS statistics_quantity_dim_order_ownsider,

            IFNULL(SUM(statistics.statistics_amount_dim_order_outsider),0) AS statistics_amount_dim_order_outsider,
            IFNULL(SUM(statistics.statistics_amount_dim_order_ownsider),0) AS statistics_amount_dim_order_ownsider,
            IFNULL(SUM(statistics.statistics_amount_dim_order_diff),0) AS statistics_amount_dim_order_diff
        FROM (
            select

                <!-- 数量 -->
                COUNT(distinct ausuojcur.id) AS statistics_quantity_dim_order_outsider,
                IFNULL(SUM(case when ausuojcurxg.id is not null then 1 else 0 end),0) AS statistics_quantity_dim_order_ownsider,
                <!-- 金额 -->
                IFNULL(SUM(distinct case when ausuojcur.id then ausuojcur.jd_order_amount else 0 end),0) AS statistics_amount_dim_order_outsider,
                IFNULL(SUM(ausuojcurxg.goods_cost_amount),0) AS statistics_amount_dim_order_ownsider,
                0 AS statistics_amount_dim_order_diff

            from
                accounting_unified_supplier_order_jd_check_up_report ausuojcur
                LEFT JOIN accounting_unified_supplier_order_jd_check_up_report_x_goods ausuojcurxg
                    on ausuojcur.batch_id = ausuojcurxg.batch_id
                    and ausuojcur.order_sn = ausuojcurxg.doggod_order_sn_as_whole
                    and ausuojcur.accounting_unified_supplier_order_jd_id = ausuojcurxg.accounting_unified_supplier_order_jd_id
                <!--
                LEFT JOIN unified_order uo on ausuojcurxg.unified_order_id = uo.id
                LEFT JOIN unified_order_goods uog on ausuojcurxg.unified_order_goods_id = uog.id
                -->
            <where>
                <include refid="conditionPart"></include>
                <if test="orderSn != null and orderSn != '' ">
                    and ausuojcur.accounting_unified_supplier_order_jd_id IN (
                    <include refid="pageQueryGenericWoofSubQuery"></include>
                    )
                </if>
            </where>
            <!-- GROUP BY ausuojcur.jd_order_sn -->
            GROUP BY ausuojcur.id
        ) statistics


    </select>



    <resultMap id="statisticsQuantityDimOrderNormalOrExceptionResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportUpperLevelsStatisticsVo">
        <result column="statistics_quantity_dim_order_normal" jdbcType="BIGINT" property="statisticsQuantityDimOrderNormal" />
        <result column="statistics_quantity_dim_order_exception" jdbcType="BIGINT" property="statisticsQuantityDimOrderException" />
    </resultMap>


    <select id="statisticsQuantityDimOrderNormalOrException"
            resultMap="statisticsQuantityDimOrderNormalOrExceptionResultMap">

        SELECT
            IFNULL(SUM(CASE WHEN ausuojcur.process_result = 1 THEN 1 ELSE 0 END),0) AS statistics_quantity_dim_order_normal,
            IFNULL(SUM(CASE WHEN ausuojcur.process_result = 2 or ausuojcur.process_result = 0 THEN 1 ELSE 0 END),0) AS statistics_quantity_dim_order_exception
        FROM
            accounting_unified_supplier_order_jd_check_up_report ausuojcur
        <where>
            <include refid="conditionPart"></include>
        </where>

    </select>

</mapper>
