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

    <resultMap id="pageQueryGenericParamResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdListItemVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="batch_id" jdbcType="VARCHAR" property="batchId"/>
        <result column="jd_order_sn" jdbcType="VARCHAR" property="jdOrderSn"/>
        <result column="jd_original_order_sn" jdbcType="VARCHAR" property="jdOriginalOrderSn"/>
        <result column="payment_no" jdbcType="VARCHAR" property="paymentNo"/>
        <result column="jd_accoumt_user_name" jdbcType="VARCHAR" property="jdAccoumtUserName"/>
        <result column="product_number" jdbcType="VARCHAR" property="productNumber"/>
        <result column="biz_type" jdbcType="VARCHAR" property="bizType"/>
        <result column="jd_create_time" jdbcType="TIMESTAMP" property="jdCreateTime"/>
        <result column="jd_pay_time" jdbcType="TIMESTAMP" property="jdPayTime"/>
        <result column="jd_finish_time" jdbcType="TIMESTAMP" property="jdFinishTime"/>
        <result column="jd_order_amount" jdbcType="DECIMAL" property="jdOrderAmount"/>
        <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="out_account_time" jdbcType="TIMESTAMP" property="outAccountTime"/>
        <result column="jd_accounting_order_sn" jdbcType="VARCHAR" property="jdAccountingOrderSn"/>
        <result column="settled_state" jdbcType="TINYINT" property="settledState"/>
        <result column="settled_state_name" jdbcType="VARCHAR" property="settledStateName"/>
        <result column="settled_time" jdbcType="TIMESTAMP" property="settledTime"/>
        <result column="purchaser_contract_sn" jdbcType="VARCHAR" property="purchaserContractSn"/>
        <result column="purchaser_virt_code" jdbcType="VARCHAR" property="purchaserVirtCode"/>
        <result column="purchaser_virt_name" jdbcType="VARCHAR" property="purchaserVirtName"/>
        <result column="invoiced_state" jdbcType="TINYINT" property="invoicedState"/>
        <result column="invoiced_state_name" jdbcType="VARCHAR" property="invoicedStateName"/>
        <result column="after_sale_state" jdbcType="TINYINT" property="afterSaleState"/>
        <result column="after_sale_state_name" jdbcType="VARCHAR" property="afterSaleStateName"/>
        <result column="repayment_order_sn" jdbcType="VARCHAR" property="repaymentOrderSn"/>
        <result column="invoiced_date" jdbcType="TIMESTAMP" property="invoicedDate"/>
        <result column="repayment_time" jdbcType="TIMESTAMP" property="repaymentTime"/>
        <result column="overdue_days" jdbcType="INTEGER" property="overdueDays"/>
        <result column="jd_order_type" jdbcType="TINYINT" property="jdOrderType"/>
        <result column="jd_order_type_name" jdbcType="VARCHAR" property="jdOrderTypeName"/>
        <result column="jd_pay_type" jdbcType="TINYINT" property="jdPayType"/>
        <result column="jd_pay_type_name" jdbcType="VARCHAR" property="jdPayTypeName"/>
        <result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>
        <result column="invoice_category_code" jdbcType="VARCHAR" property="invoiceCategoryCode"/>
        <result column="total_amount_including_tax" jdbcType="DECIMAL" property="totalAmountIncludingTax"/>
        <result column="purchaser_company_name" jdbcType="VARCHAR" property="purchaserCompanyName"/>
        <result column="invoice_type_id" jdbcType="TINYINT" property="invoiceTypeId"/>
        <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_user_name" jdbcType="VARCHAR" property="createUserName"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_user_name" jdbcType="VARCHAR" property="updateUserName"/>
        <result column="process_result" jdbcType="TINYINT" property="processResult"/>
        <result column="process_report" jdbcType="LONGVARCHAR" property="processReport"/>

        <result column="jd_order_amount_str" jdbcType="VARCHAR" property="jdOrderAmountStr" />
        <result column="jd_repayment_amount_str" jdbcType="VARCHAR" property="jdRepaymentAmountStr" />
        <result column="jd_refund_amount_str" jdbcType="VARCHAR" property="jdRefundAmountStr" />
        <result column="jd_out_standing_amount_str" jdbcType="VARCHAR" property="jdOutStandingAmountStr" />

    </resultMap>

    <select id="pageQueryGenericParam"
            resultMap="pageQueryGenericParamResultMap">

        SELECT
            ausuojd.id,
            ausuojd.batch_id,
            ausuojd.jd_order_sn,
            ausuojd.jd_original_order_sn,
            ausuojd.payment_no,
            ausuojd.jd_accoumt_user_name,
            ausuojd.product_number,
            ausuojd.biz_type,
            ausuojd.jd_create_time,
            ausuojd.jd_pay_time,
            ausuojd.jd_finish_time,
            ausuojd.jd_order_amount,
            ausuojd.jd_repayment_amount,
            ausuojd.jd_refund_amount,
            ausuojd.jd_out_standing_amount,
            ausuojd.jd_order_state,
            ausuojd.jd_order_state_name,
            ausuojd.were_accounted_state,
            ausuojd.were_accounted_state_name,
            ausuojd.out_account_time,
            ausuojd.jd_accounting_order_sn,
            ausuojd.settled_state,
            ausuojd.settled_state_name,
            ausuojd.settled_time,
            ausuojd.purchaser_contract_sn,
            ausuojd.purchaser_virt_code,
            ausuojd.purchaser_virt_name,
            ausuojd.invoiced_state,
            ausuojd.invoiced_state_name,
            ausuojd.after_sale_state,
            ausuojd.after_sale_state_name,
            ausuojd.repayment_order_sn,
            ausuojd.invoiced_date,
            ausuojd.repayment_time,
            ausuojd.overdue_days,
            ausuojd.jd_order_type,
            ausuojd.jd_order_type_name,
            ausuojd.jd_pay_type,
            ausuojd.jd_pay_type_name,
            ausuojd.invoice_number,
            ausuojd.invoice_category_code,
            ausuojd.total_amount_including_tax,
            ausuojd.purchaser_company_name,
            ausuojd.invoice_type_id, invoice_type,
            ausuojd.create_time,
            ausuojd.create_user_id,
            ausuojd.create_user_name,
            ausuojd.update_time,
            ausuojd.update_user_id,
            ausuojd.update_user_name,
            ausuojd.process_result,
            ausuojd.process_report,

            ausuojd.jd_order_amount_str,
            ausuojd.jd_repayment_amount_str,
            ausuojd.jd_refund_amount_str,
            ausuojd.jd_out_standing_amount_str

        FROM
            accounting_unified_supplier_order_jd ausuojd
        <where>

            <include refid="conditionPart"></include>

            <if test="onlyCheckUpException != null and onlyCheckUpException != '' ">
                and ausuojd.jd_order_sn in (
                    select
                        DISTINCT jd_order_sn
                    from
                        accounting_unified_supplier_order_jd_check_up_report ausuojcur
                    <where>

                        and process_result = 2
                        <if test="batchId != null and batchId != '' ">
                            and ausuojcur.batch_id = #{batchId}
                        </if>
                    </where>
                )
            </if>

        </where>

    </select>

    <sql id="conditionPart">

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


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

        <result column="statistics_amount_dim_order_outsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOutsider" />

        <result column="statistics_quantity_dim_order_total" jdbcType="BIGINT" property="statisticsQuantityDimOrderTotal" />
        <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="pageQueryHeadGenericParam"
            resultMap="queryHeadGenericParamResultMap">
        select

            IFNULL(SUM(ausuojd.jd_order_amount),0) AS statistics_amount_dim_order_outsider,

            COUNT(ausuojd.id) AS statistics_quantity_dim_order_total,
            COUNT(ausuojd.jd_order_sn IS NOT NULL OR ausuojd.jd_order_amount != 0 OR NULL) AS statistics_quantity_dim_order_normal,
            COUNT(ausuojd.jd_order_sn IS NULL OR ausuojd.jd_order_amount IS NULL OR ausuojd.jd_order_amount = 0 OR NULL) AS statistics_quantity_dim_order_exception

        from
            accounting_unified_supplier_order_jd ausuojd
        <where>
            <include refid="conditionPart"></include>
        </where>
    </select>


    <resultMap id="statisticsQuantityDimOrderNormalOrExceptionResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdUpperLevelsStatisticsVo">
        <result column="statistics_amount_dim_order_outsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOutsider" />

        <result column="statistics_quantity_dim_order_total" jdbcType="BIGINT" property="statisticsQuantityDimOrderTotal" />
        <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(ausuojd.jd_order_amount),0) AS statistics_amount_dim_order_outsider,
            COUNT(ausuojd.id) AS statistics_quantity_dim_order_total,
            SUM(CASE WHEN ausuojd.process_result = 1 THEN 1 ELSE 0 END) AS statistics_quantity_dim_order_normal,
            SUM(CASE WHEN ausuojd.process_result = 2 or ausuojd.process_result = 0 THEN 1 ELSE 0 END) AS statistics_quantity_dim_order_exception
        FROM
            accounting_unified_supplier_order_jd ausuojd
        <where>
            <include refid="conditionPart"></include>
        </where>

    </select>


    <!-- cn.superdesk.uniorder.dao.entity.thirdpart.jd.AccountingUnifiedSupplierOrderJd -->
    <insert id="replaceBatch" parameterType="java.util.List">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        replace into accounting_unified_supplier_order_jd (batch_id, jd_order_sn, jd_original_order_sn,
        payment_no, jd_accoumt_user_name, product_number,
        biz_type, jd_create_time, jd_pay_time,
        jd_finish_time, jd_order_amount, jd_repayment_amount,
        jd_refund_amount, jd_out_standing_amount, jd_order_state,
        jd_order_state_name, were_accounted_state, were_accounted_state_name,
        out_account_time, jd_accounting_order_sn,
        settled_state, settled_state_name, settled_time,
        purchaser_contract_sn, purchaser_virt_code,
        purchaser_virt_name, invoiced_state, invoiced_state_name,
        after_sale_state, after_sale_state_name, repayment_order_sn,
        invoiced_date, repayment_time, overdue_days,
        jd_order_type, jd_order_type_name, jd_pay_type,
        jd_pay_type_name, invoice_number, invoice_category_code,
        total_amount_including_tax, purchaser_company_name,
        invoice_type_id, invoice_type, create_time,
        create_user_id, create_user_name, update_time,
        update_user_id, update_user_name, process_result,
        jd_order_amount_str, jd_repayment_amount_str,
        jd_refund_amount_str, jd_out_standing_amount_str,
        process_report)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (

        #{item.batchId,jdbcType=VARCHAR},
        #{item.jdOrderSn,jdbcType=VARCHAR},
        #{item.jdOriginalOrderSn,jdbcType=VARCHAR},
        #{item.paymentNo,jdbcType=VARCHAR},
        #{item.jdAccoumtUserName,jdbcType=VARCHAR}, #{item.productNumber,jdbcType=VARCHAR},
        #{item.bizType,jdbcType=VARCHAR}, #{item.jdCreateTime,jdbcType=TIMESTAMP}, #{item.jdPayTime,jdbcType=TIMESTAMP},
        #{item.jdFinishTime,jdbcType=TIMESTAMP}, #{item.jdOrderAmount,jdbcType=DECIMAL}, #{item.jdRepaymentAmount,jdbcType=DECIMAL},
        #{item.jdRefundAmount,jdbcType=DECIMAL}, #{item.jdOutStandingAmount,jdbcType=DECIMAL}, #{item.jdOrderState,jdbcType=TINYINT},
        #{item.jdOrderStateName,jdbcType=VARCHAR}, #{item.wereAccountedState,jdbcType=TINYINT}, #{item.wereAccountedStateName,jdbcType=VARCHAR},
        #{item.outAccountTime,jdbcType=TIMESTAMP}, #{item.jdAccountingOrderSn,jdbcType=VARCHAR},
        #{item.settledState,jdbcType=TINYINT}, #{item.settledStateName,jdbcType=VARCHAR}, #{item.settledTime,jdbcType=TIMESTAMP},
        #{item.purchaserContractSn,jdbcType=VARCHAR}, #{item.purchaserVirtCode,jdbcType=VARCHAR},
        #{item.purchaserVirtName,jdbcType=VARCHAR}, #{item.invoicedState,jdbcType=TINYINT}, #{item.invoicedStateName,jdbcType=VARCHAR},
        #{item.afterSaleState,jdbcType=TINYINT}, #{item.afterSaleStateName,jdbcType=VARCHAR}, #{item.repaymentOrderSn,jdbcType=VARCHAR},
        #{item.invoicedDate,jdbcType=TIMESTAMP}, #{item.repaymentTime,jdbcType=TIMESTAMP}, #{item.overdueDays,jdbcType=INTEGER},
        #{item.jdOrderType,jdbcType=TINYINT}, #{item.jdOrderTypeName,jdbcType=VARCHAR}, #{item.jdPayType,jdbcType=TINYINT},
        #{item.jdPayTypeName,jdbcType=VARCHAR}, #{item.invoiceNumber,jdbcType=VARCHAR}, #{item.invoiceCategoryCode,jdbcType=VARCHAR},
        #{item.totalAmountIncludingTax,jdbcType=DECIMAL}, #{item.purchaserCompanyName,jdbcType=VARCHAR},
        #{item.invoiceTypeId,jdbcType=TINYINT}, #{item.invoiceType,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP},
        #{item.createUserId,jdbcType=BIGINT}, #{item.createUserName,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=TIMESTAMP},
        #{item.updateUserId,jdbcType=BIGINT}, #{item.updateUserName,jdbcType=VARCHAR}, #{item.processResult,jdbcType=TINYINT},
        #{item.jdOrderAmountStr,jdbcType=VARCHAR}, #{item.jdRepaymentAmountStr,jdbcType=VARCHAR},
        #{item.jdRefundAmountStr,jdbcType=VARCHAR}, #{item.jdOutStandingAmountStr,jdbcType=VARCHAR},
        #{item.processReport,jdbcType=LONGVARCHAR}
        )
        </foreach>
    </insert>

    <!-- cn.superdesk.uniorder.dao.entity.thirdpart.jd.AccountingUnifiedSupplierOrderJd -->
    <insert id="insertBatch" parameterType="java.util.List">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into accounting_unified_supplier_order_jd (batch_id, jd_order_sn, jd_original_order_sn,
        payment_no, jd_accoumt_user_name, product_number,
        biz_type, jd_create_time, jd_pay_time,
        jd_finish_time, jd_order_amount, jd_repayment_amount,
        jd_refund_amount, jd_out_standing_amount, jd_order_state,
        jd_order_state_name, were_accounted_state, were_accounted_state_name,
        out_account_time, jd_accounting_order_sn,
        settled_state, settled_state_name, settled_time,
        purchaser_contract_sn, purchaser_virt_code,
        purchaser_virt_name, invoiced_state, invoiced_state_name,
        after_sale_state, after_sale_state_name, repayment_order_sn,
        invoiced_date, repayment_time, overdue_days,
        jd_order_type, jd_order_type_name, jd_pay_type,
        jd_pay_type_name, invoice_number, invoice_category_code,
        total_amount_including_tax, purchaser_company_name,
        invoice_type_id, invoice_type, create_time,
        create_user_id, create_user_name, update_time,
        update_user_id, update_user_name, process_result,
        jd_order_amount_str, jd_repayment_amount_str,
        jd_refund_amount_str, jd_out_standing_amount_str,
        process_report)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (

            #{item.batchId,jdbcType=VARCHAR},
            #{item.jdOrderSn,jdbcType=VARCHAR},
            #{item.jdOriginalOrderSn,jdbcType=VARCHAR},
            #{item.paymentNo,jdbcType=VARCHAR},
            #{item.jdAccoumtUserName,jdbcType=VARCHAR}, #{item.productNumber,jdbcType=VARCHAR},
            #{item.bizType,jdbcType=VARCHAR}, #{item.jdCreateTime,jdbcType=TIMESTAMP}, #{item.jdPayTime,jdbcType=TIMESTAMP},
            #{item.jdFinishTime,jdbcType=TIMESTAMP}, #{item.jdOrderAmount,jdbcType=DECIMAL}, #{item.jdRepaymentAmount,jdbcType=DECIMAL},
            #{item.jdRefundAmount,jdbcType=DECIMAL}, #{item.jdOutStandingAmount,jdbcType=DECIMAL}, #{item.jdOrderState,jdbcType=TINYINT},
            #{item.jdOrderStateName,jdbcType=VARCHAR}, #{item.wereAccountedState,jdbcType=TINYINT}, #{item.wereAccountedStateName,jdbcType=VARCHAR},
            #{item.outAccountTime,jdbcType=TIMESTAMP}, #{item.jdAccountingOrderSn,jdbcType=VARCHAR},
            #{item.settledState,jdbcType=TINYINT}, #{item.settledStateName,jdbcType=VARCHAR}, #{item.settledTime,jdbcType=TIMESTAMP},
            #{item.purchaserContractSn,jdbcType=VARCHAR}, #{item.purchaserVirtCode,jdbcType=VARCHAR},
            #{item.purchaserVirtName,jdbcType=VARCHAR}, #{item.invoicedState,jdbcType=TINYINT}, #{item.invoicedStateName,jdbcType=VARCHAR},
            #{item.afterSaleState,jdbcType=TINYINT}, #{item.afterSaleStateName,jdbcType=VARCHAR}, #{item.repaymentOrderSn,jdbcType=VARCHAR},
            #{item.invoicedDate,jdbcType=TIMESTAMP}, #{item.repaymentTime,jdbcType=TIMESTAMP}, #{item.overdueDays,jdbcType=INTEGER},
            #{item.jdOrderType,jdbcType=TINYINT}, #{item.jdOrderTypeName,jdbcType=VARCHAR}, #{item.jdPayType,jdbcType=TINYINT},
            #{item.jdPayTypeName,jdbcType=VARCHAR}, #{item.invoiceNumber,jdbcType=VARCHAR}, #{item.invoiceCategoryCode,jdbcType=VARCHAR},
            #{item.totalAmountIncludingTax,jdbcType=DECIMAL}, #{item.purchaserCompanyName,jdbcType=VARCHAR},
            #{item.invoiceTypeId,jdbcType=TINYINT}, #{item.invoiceType,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP},
            #{item.createUserId,jdbcType=BIGINT}, #{item.createUserName,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.updateUserId,jdbcType=BIGINT}, #{item.updateUserName,jdbcType=VARCHAR}, #{item.processResult,jdbcType=TINYINT},
            #{item.jdOrderAmountStr,jdbcType=VARCHAR}, #{item.jdRepaymentAmountStr,jdbcType=VARCHAR},
            #{item.jdRefundAmountStr,jdbcType=VARCHAR}, #{item.jdOutStandingAmountStr,jdbcType=VARCHAR},
            #{item.processReport,jdbcType=LONGVARCHAR}
            )
        </foreach>
    </insert>

</mapper>