<?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.invoice.customer.InvoiceApplyMapperExUnifiedClear">


    <resultMap id="InvoiceApplyDetailDimGoodsV3ResultMap"
               type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimGoodsItemV3Vo">

        <!-- 开票申请 -->
        <result column="invoice_apply_id" jdbcType="BIGINT" property="invoiceApplyId"/>
        <result column="accounting_order_id" jdbcType="BIGINT" property="accountingOrderId"/>
        <result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId"/>
        <result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId"/>
        <result column="accounting_order_type" jdbcType="INTEGER" property="accountingOrderType"/>
        <result column="invoice_dim_create_by" jdbcType="INTEGER" property="invoiceDimCreateBy"/>
        <result column="invoice_back_account_status" jdbcType="INTEGER" property="invoiceBackAccountStatus"/>

        <!-- 对账单 -->
        <result column="accounting_dim_create_by" jdbcType="INTEGER" property="accountingDimCreateBy"/>
        <result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>
        <result column="accounting_amount" jdbcType="DECIMAL" property="accountingAmount"/>
        <result column="accounting_quantity_order" jdbcType="INTEGER" property="accountingQuantityOrder"/>
        <result column="accounting_quantity_goods" jdbcType="INTEGER" property="accountingQuantityGoods"/>
        <result column="accounting_back_account_status" jdbcType="INTEGER" property="accountingBackAccountStatus"/>

        <!-- 订单 -->
        <result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>

        <!-- uo.service_name, 弃用 改用定义 mark by linjinyu at 20220624 -->
        <result column="service_name" jdbcType="VARCHAR" property="serviceName"/>
        <result column="service_no" jdbcType="VARCHAR" property="serviceNo"/>

        <result column="project_id" jdbcType="BIGINT" property="projectId"/>
        <result column="project_name" jdbcType="VARCHAR" property="projectName"/>

        <result column="purchase_virt_id" jdbcType="BIGINT" property="purchaseVirtId"/>
        <result column="purchase_virt_name" jdbcType="VARCHAR" property="purchaseVirtName"/>

        <result column="purchase_user_id" jdbcType="BIGINT" property="purchaseUserId"/>
        <result column="purchase_user_name" jdbcType="VARCHAR" property="purchaseUserName"/>
        <result column="purchase_phone" property="purchasePhone" jdbcType="VARCHAR"/>

        <result column="sale_id" jdbcType="BIGINT" property="saleId"/>
        <result column="sale_name" jdbcType="VARCHAR" property="saleName"/>

        <result column="order_amount" jdbcType="DECIMAL" property="orderAmount"/>
        <result column="order_cost_amount" jdbcType="DECIMAL" property="orderCostAmount"/>
        <result column="order_transport_amount" jdbcType="DECIMAL" property="orderTransportAmount"/>
        <result column="order_transport_merchant_assume_amount" jdbcType="DECIMAL" property="orderTransportMerchantAssumeAmount"/>

        <result column="order_goods_amount" jdbcType="DECIMAL" property="orderGoodsAmount"/>
        <result column="order_goods_cost_amount" jdbcType="DECIMAL" property="orderGoodsCostAmount"/>
        <result column="order_refund_goods_amount" jdbcType="DECIMAL" property="orderRefundGoodsAmount"/>
        <result column="order_refund_goods_cost_amount" jdbcType="DECIMAL" property="orderRefundGoodsCostAmount"/>

        <result column="complet_state" jdbcType="INTEGER" property="completState"/>
        <result column="order_accounting_state_customer" jdbcType="INTEGER" property="orderAccountingStateCustomer"/>
        <result column="order_accounting_state_supplier" jdbcType="INTEGER" property="orderAccountingStateSupplier"/>

        <result column="pay_type" jdbcType="TINYINT" property="payType"/>
        <result column="pay_type_name" jdbcType="VARCHAR" property="payTypeName"/>
        <result column="pay_state" jdbcType="TINYINT" property="payState"/>
        <result column="pay_state_name" jdbcType="VARCHAR" property="payStateName"/>

        <result column="order_state" jdbcType="VARCHAR" property="orderState"/>
        <result column="order_state_name" jdbcType="VARCHAR" property="orderStateName"/>
        <result column="order_back_account_status" jdbcType="VARCHAR" property="orderBackAccountStatus"/>

        <result column="rec_user_name" property="recUserName" jdbcType="VARCHAR"/>
        <result column="rec_user_id" property="recUserId" jdbcType="BIGINT"/>
        <result column="rec_user_phone" property="recUserPhone" jdbcType="VARCHAR"/>
        <result column="rec_address" property="recAddress" jdbcType="VARCHAR"/>

        <result column="express_sn" jdbcType="VARCHAR" property="expressSn"/>
        <result column="express" jdbcType="VARCHAR" property="express"/>
        <result column="express_company" jdbcType="VARCHAR" property="expressCompany"/>

        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="pay_time"     property="payTime"    jdbcType="TIMESTAMP"/>
        <result column="send_time"    property="sendTime"   jdbcType="TIMESTAMP"/>
        <result column="finish_time"  property="finishTime" jdbcType="TIMESTAMP"/>

        <result column="invoice_status" jdbcType="INTEGER" property="invoiceStatus"/>
        <result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>
        <result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn"/>

        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>

        <!-- 订单商品 -->
        <result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
        <result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
        <result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
        <result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />

        <result column="quantity" property="quantity" jdbcType="INTEGER"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
        <result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
        <result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>

        <result column="direction" jdbcType="INTEGER" property="direction"/>

        <result column="cate_id_lv1" property="cateIdLv1" jdbcType="BIGINT"/>
        <result column="cate_name_lv1" property="cateNameLv1" jdbcType="VARCHAR"/>
        <result column="cate_id_lv2" property="cateIdLv2" jdbcType="BIGINT"/>
        <result column="cate_name_lv2" property="cateNameLv2" jdbcType="VARCHAR"/>
        <result column="cate_id_lv3" property="cateIdLv3" jdbcType="BIGINT"/>
        <result column="cate_name_lv3" property="cateNameLv3" jdbcType="VARCHAR"/>
        <result column="self_option" jdbcType="VARCHAR" property="selfOption"/>
        <result column="unit" property="unit" jdbcType="VARCHAR"/>

        <result column="fiscal_code" jdbcType="VARCHAR" property="fiscalCode"/>
        <result column="tax_code" jdbcType="VARCHAR" property="taxCode"/>
        <result column="tax_rate" jdbcType="DECIMAL" property="taxRate"/>
        <result column="merch_tax_id" jdbcType="BIGINT" property="merchTaxId"/>
        <result column="merch_tax_code" jdbcType="VARCHAR" property="merchTaxCode"/>
        <result column="merch_tax_rate" jdbcType="DECIMAL" property="merchTaxRate"/>
        <result column="operation_tax_id" jdbcType="BIGINT" property="operationTaxId"/>
        <result column="operation_tax_code" jdbcType="VARCHAR" property="operationTaxCode"/>
        <result column="operation_tax_rate" jdbcType="DECIMAL" property="operationTaxRate"/>

        <result column="goods_accounting_state_customer" jdbcType="INTEGER" property="goodsAccountingStateCustomer"/>
        <result column="goods_accounting_state_supplier" jdbcType="INTEGER" property="goodsAccountingStateSupplier"/>
        <result column="goods_back_account_status" jdbcType="INTEGER" property="goodsBackAccountStatus"/>

    </resultMap>

    <select id="findInvoiceApplyDetailInnerInfo" parameterType="java.lang.Long"
            resultMap="InvoiceApplyDetailDimGoodsV3ResultMap">

        SELECT
            invo.invoice_apply_id,
            invo.accounting_order_id as accounting_order_id,
            invo.unified_order_id as unified_order_id,
            invo.unified_order_goods_id as unified_order_goods_id,
            invo.accounting_order_type,
            invo.dim_create_by as invoice_dim_create_by,
            invo.back_status as invoice_back_account_status,

            aucu.dim_create_by as accounting_dim_create_by,
            aucu.accounting_order_sn,
            aucu.accounting_amount,
            aucu.quantity_order as accounting_quantity_order,
            aucu.quantity_goods as accounting_quantity_goods,
            aucu.back_account_status as accounting_back_account_status,

            uo.order_sn,

            <!-- 弃用 改用定义 mark by linjinyu at 20220624 -->
            uo.service_name,
            uo.service_no,

            uo.project_id,
            uo.project_name,

            uo.purchase_virt_id,
            uo.purchase_virt_name,
            uo.purchase_user_id,
            uo.purchase_user_name,
            uo.purchase_phone,

            uo.sale_id,
            uo.sale_name,

            uo.amount as order_amount,
            uo.cost_amount as order_cost_amount,
            uo.transport_amount as order_transport_amount,
            uo.transport_merchant_assume_amount as order_transport_merchant_assume_amount,

            uo.goods_amount as order_goods_amount,
            uo.goods_cost_amount as order_goods_cost_amount,
            uo.refund_goods_amount as order_refund_goods_amount,
            uo.refund_goods_cost_amount as order_refund_goods_cost_amount,

            uo.complet_state,
            uo.accounting_state_customer as order_accounting_state_customer,
            uo.accounting_state_supplier as order_accounting_state_supplier,

            uo.pay_type,
            uo.pay_type_name,
            uo.pay_state,
            uo.pay_state_name,

            uo.order_state,
            uo.order_state_name,
            uo.back_account_status as order_back_account_status,

            uo.rec_user_name,
            uo.rec_user_id,
            uo.rec_user_phone,
            uo.rec_address,

            uo.express_sn,
            uo.express,
            uo.express_company,

            uo.create_time,
            uo.update_time,
            uo.pay_time,
            uo.send_time,
            uo.finish_time,

            uo.invoice_status,
            uo.invoice_number,
            uo.invoice_apply_sn,

            uo.remarks,

            uog.goods_id,
            uog.out_snapshot_order_goods_id,
            uog.goods_name,
            uog.goods_alias,

            uog.quantity,
            uog.price,
            uog.cost_price,
            uog.price * uog.quantity as goods_amount,
            uog.cost_price * uog.quantity as goods_cost_amount,

            uog.direction,

            uog.cate_id_lv1,
            uog.cate_name_lv1,
            uog.cate_id_lv2,
            uog.cate_name_lv2,
            uog.cate_id_lv3,
            uog.cate_name_lv3,
            uog.self_option,
            uog.unit,

            uog.fiscal_code,
            uog.tax_code,
            uog.tax_rate,
            uog.merch_tax_id,
            uog.merch_tax_code,
            uog.merch_tax_rate,
            uog.operation_tax_id,
            uog.operation_tax_code,
            uog.operation_tax_rate,

            uog.accounting_state_customer as goods_accounting_state_customer,
            uog.accounting_state_supplier as goods_accounting_state_supplier,

            uog.back_account_status as goods_back_account_status
        FROM
            invoice_apply_x_goods invo
            LEFT JOIN accounting_unified_customer aucu ON invo.accounting_order_id = aucu.id
            LEFT JOIN unified_order uo ON invo.unified_order_id = uo.id
            LEFT JOIN unified_order_goods uog ON invo.unified_order_goods_id = uog.id
        <where>
            <if test="invoiceApplyId != null ">
                and invo.invoice_apply_id = #{invoiceApplyId,jdbcType=BIGINT}
            </if>

        </where>
    </select>
</mapper>