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

    <resultMap id="ViewDzOrderResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimOrderItemVo">

        <result column="id" property="id" jdbcType="BIGINT"/>
        <result column="dz_id" property="dzId" jdbcType="BIGINT"/>
        <result column="accounting_order_sn" property="accountingOrderSn" jdbcType="VARCHAR"/>

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

        <result column="parent_order_id" property="parentOrderId" jdbcType="INTEGER"/>
        <result column="parent_order_sn" property="parentOrderSn" jdbcType="VARCHAR"/>

        <result column="address_realname" property="addressRealname" jdbcType="VARCHAR"/>
        <result column="address_mobile" property="addressMobile" jdbcType="VARCHAR"/>
        <result column="address_address" property="addressAddress" jdbcType="VARCHAR"/>

        <result column="invoice_company_name" property="invoiceCompanyName" jdbcType="VARCHAR"/>
        <result column="invoice_taxpayers_i_d_code" property="invoiceTaxpayersIDcode" jdbcType="VARCHAR"/>
        <result column="invoice_account" property="invoiceAccount" jdbcType="VARCHAR"/>
        <result column="invoice_bank" property="invoiceBank" jdbcType="VARCHAR"/>
        <result column="invoice_type" property="invoiceType" jdbcType="TINYINT"/>
        <result column="invoice_type_name" property="invoiceTypeName" jdbcType="VARCHAR"/>
        <result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>

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

        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="status_name" property="statusName" jdbcType="VARCHAR"/>

        <result column="merch_name" property="merchName" jdbcType="VARCHAR"/>
        <result column="service_name" property="serviceName" jdbcType="TINYINT"/>

        <result column="pay_type" property="payType" jdbcType="TINYINT"/>
        <result column="pay_type_name" property="payTypeName" jdbcType="VARCHAR"/>

        <result column="search_field1" property="searchField1" jdbcType="VARCHAR"/>
        <result column="search_field2" property="searchField2" jdbcType="VARCHAR"/>
        <result column="search_field3" property="searchField3" jdbcType="VARCHAR"/>

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

        <result column="realname_id" property="realnameId" jdbcType="INTEGER"/>
        <result column="realname" property="realname" jdbcType="VARCHAR"/>
        <result column="examine_id" property="examineId" jdbcType="INTEGER"/>
        <result column="examine_name" property="examineName" jdbcType="VARCHAR"/>

        <result column="detail_url" property="detailUrl" jdbcType="VARCHAR"/>

        <result column="organization_id" property="organizationId" jdbcType="BIGINT"/>
        <result column="enterprise_id" property="enterpriseId" jdbcType="BIGINT"/>

        <result column="dispatch_name" property="dispatchName" jdbcType="VARCHAR"/>
        <result column="dispatch_price" property="dispatchPrice" jdbcType="DECIMAL"/>

        <result column="dz_order_goods_id" property="dzOrderGoodsId" jdbcType="BIGINT"/>
        <result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
        <result column="dz_order_id" property="dzOrderId" jdbcType="BIGINT"/>

        <result column="good_id" property="goodId" jdbcType="BIGINT"/>
        <result column="goodno" property="goodno" jdbcType="VARCHAR"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="total" property="total" jdbcType="INTEGER"/>
        <result column="unit_price" property="unitPrice" jdbcType="DECIMAL"/>
        <result column="optionname" property="optionname" jdbcType="VARCHAR"/>
        <result column="unit" property="unit" jdbcType="VARCHAR"/>

        <result column="other_field1" property="otherField1" jdbcType="VARCHAR"/>
        <result column="other_field2" property="otherField2" jdbcType="VARCHAR"/>

        <result column="total_price" property="totalPrice" jdbcType="DECIMAL"/>
        <result column="tax_code" property="taxCode" jdbcType="VARCHAR"/>
        <result column="fiscal_code" property="fiscalCode" jdbcType="VARCHAR"/>

        <result column="order_price" property="orderPrice" jdbcType="DECIMAL"/>

        <!-- invo.id as invoice_order_id,-->
        <result column="invoice_order_id" property="invoiceOrderId" jdbcType="BIGINT"/>
        <result column="back_time" property="backTime" jdbcType="TIMESTAMP"/>
        <result column="bank_id" property="bankId" jdbcType="BIGINT"/>
        <result column="water_count" property="waterCount" jdbcType="DECIMAL"/>

        <result column="back_status" property="backStatus" jdbcType="TINYINT"/>
    </resultMap>

    <!-- 客户结算 - 发票申请 - 查看详情 一般订单类 TODO 要重构 mark by linjinyu 202011121815-->
    <select id="findInvoiceApplyDetailOldDim1Unified2Goods" parameterType="java.lang.Long" resultMap="ViewDzOrderResultMap">
        SELECT

            aucuo.id AS id,
            aucuo.dz_id AS dz_id,
            aucu.accounting_order_sn AS accounting_order_sn,
            aucuo.order_id AS order_id,
            aucuo.order_sn AS order_sn,
            aucuo.parent_order_id AS parent_order_id,
            aucuo.parent_order_sn AS parent_order_sn,
            aucuo.price AS order_price,
            aucuo.create_time AS create_time,
            aucuo.address_realname AS address_realname,
            aucuo.address_mobile AS address_mobile,
            aucuo.address_address AS address_address,
            aucuo.invoice_company_name AS invoice_company_name,
            aucuo.invoice_taxpayers_i_d_code AS invoice_taxpayers_i_d_code,
            aucuo.send_time AS send_time,
            aucuo.finish_time AS finish_time,
            aucuo.`status` AS `status`,
            CASE
            WHEN aucuo.`status` = - 1 THEN
            '已关闭'
            WHEN aucuo.`status` = 0 THEN
            '待付款'
            WHEN aucuo.`status` = 1 THEN
            '待发货'
            WHEN aucuo.`status` = 2 THEN
            '待收货'
            WHEN aucuo.`status` = 3 THEN
            '已完成'
            ELSE
            '已关闭'
            END AS status_name,
            aucuo.merch_name AS merch_name,

            aucuo.pay_type AS pay_type,
            CASE
            WHEN aucuo.pay_type = 0 THEN
            '未支付'
            WHEN aucuo.pay_type = 1 THEN
            '余额支付'
            WHEN aucuo.pay_type = 2 THEN
            '在线支付'
            WHEN aucuo.pay_type = 3 THEN
            '企业月结'
            WHEN aucuo.pay_type = 11 THEN
            '后台付款'
            WHEN aucuo.pay_type = 21 THEN
            '微信支付'
            WHEN aucuo.pay_type = 22 THEN
            '支付宝支付'
            WHEN aucuo.pay_type = 23 THEN
            '银联支付'
            ELSE
            '未支付'
            END AS pay_type_name,
            aucuo.search_field1 AS search_field1,
            aucuo.search_field2 AS search_field2,
            aucuo.search_field3 AS search_field3,
            aucuo.remark AS remark,
            aucuo.invoice_account AS invoice_account,
            aucuo.invoice_bank AS invoice_bank,
            aucuo.examine_id AS examine_id,
            aucuo.realname_id AS realname_id,
            aucuo.detail_url AS detail_url,
            aucuo.organization_id AS organization_id,
            aucuo.enterprise_id AS enterprise_id,
            aucuo.invoice_type AS invoice_type,
            CASE
            WHEN aucuo.invoice_type = 1 THEN
            '增值税普票'
            ELSE
            '增值税专票'
            END AS invoice_type_name,
            aucuo.dispatch_name AS dispatch_name,
            aucuo.sync_time AS sync_time,
            aucuo.pay_time AS pay_time,

            aucog.id AS dz_order_goods_id,
            aucog.dz_order_id AS dz_order_id,
            aucog.good_id AS good_id,
            aucog.price AS price,
            aucog.total AS total,
            aucog.unit_price AS unit_price,
            aucog.optionname AS optionname,
            aucog.unit AS unit,
            aucog.other_field1 AS other_field1,
            aucog.other_field2 AS other_field2,
            aucog.dispatch_price AS dispatch_price,
            aucog.total_price AS total_price,

            aucog.tax_code AS tax_code,
            aucog.fiscal_code AS fiscal_code,

            invo.id as invoice_order_id,
            invo.back_time as back_time,
            invo.bank_id as bank_id,
            invo.back_status as back_status,

            invb.count as water_count

        FROM
            invoice_apply_x_goods invo
            LEFT JOIN accounting_unified_customer aucu ON invo.accounting_order_id = aucu.id
            LEFT JOIN accounting_unified_customer_order aucuo ON invo.unified_order_id = aucuo.id
            <!--
            LEFT JOIN accounting_unified_customer_order_goods aucog ON invo.unified_order_goods_id = aucog.id
            -->
            LEFT JOIN accounting_unified_customer_order_goods aucog ON aucuo.id = aucog.dz_order_id
            LEFT JOIN invoice_bank_statement invb ON invo.bank_id = invb.id


        <where>

            <!--
            这个对账单的对账状态为什么要 = 1
            已取消(-1, "已取消"),
            未确认(0, "未确认"),
            已确认(1, "已确认"),
            已拒绝(2, "已拒绝"),
            待提交(3, "待提交"),
            -->
            <!--
            AND aucu.accounting_status =1
            -->
            <if test="invoiceApplyId != null ">
                and invo.invoice_apply_id = #{invoiceApplyId,jdbcType=BIGINT}
            </if>
        </where>
    </select>

    <!-- accounting_unified_customer_order 这个表已经弃用 -->
    <select id="findInvoiceApplyDetail20210201Dim1Unified2Goods" parameterType="java.lang.Long" resultMap="ViewDzOrderResultMap">

        SELECT

            aucu.accounting_order_sn AS accounting_order_sn,

            aucuo.id AS dz_order_id,
            aucuo.id AS id,
            aucuo.dz_id AS dz_id,
            aucuo.order_id AS order_id,
            aucuo.order_sn AS order_sn,
            aucuo.price AS order_price,
            aucuo.parent_order_id AS parent_order_id,
            aucuo.parent_order_sn AS parent_order_sn,
            aucuo.create_time AS create_time,
            aucuo.address_realname AS address_realname,
            aucuo.address_mobile AS address_mobile,
            aucuo.address_address AS address_address,
            aucuo.invoice_company_name AS invoice_company_name,
            aucuo.invoice_taxpayers_i_d_code AS invoice_taxpayers_i_d_code,
            aucuo.send_time AS send_time,
            aucuo.finish_time AS finish_time,
            aucuo.`status` AS order_status,
            aucuo.pay_type AS pay_type,
            aucuo.merch_name AS merch_name,
            aucuo.search_field1 AS search_field1,
            aucuo.search_field2 AS search_field2,
            aucuo.search_field3 AS search_field3,
            aucuo.remark AS remark,
            aucuo.invoice_account AS invoice_account,
            aucuo.invoice_bank AS invoice_bank,

            aucuo.examine_id AS examine_id,
            aucuo.realname_id AS realname_id,

            aucuo.detail_url AS detail_url,
            aucuo.organization_id AS organization_id,
            aucuo.enterprise_id AS enterprise_id,
            aucuo.invoice_type AS invoice_type,

            aucuo.dispatch_name AS dispatch_name,
            aucuo.sync_time AS sync_time,
            aucuo.pay_time AS pay_time,

            uog.id AS unified_order_goods_id,

            uog.goods_id AS good_id,

            uog.quantity * uog.price AS price,
            uog.quantity AS total,
            uog.price AS unit_price,
            uog.goods_name AS optionname,
            uog.unit AS unit,
            uog.goods_name AS other_field1,
            uog.goods_params AS other_field2,
            0 AS dispatch_price,
            uog.quantity * uog.price AS total_price,
            uog.tax_code AS tax_code,
            uog.fiscal_code AS fiscal_code,

            invo.id as invoice_order_id,
            invo.unified_order_goods_id as dz_order_goods_id,
            invo.back_time as back_time,
            invo.bank_id as bank_id,
            invo.back_status as back_status,

            invb.count as water_count

        FROM
            invoice_apply_x_goods invo
            LEFT JOIN unified_order_goods uog ON invo.unified_order_goods_id = uog.id
            LEFT JOIN accounting_unified_customer_order aucuo on invo.unified_order_id = aucuo.id
            LEFT JOIN accounting_unified_customer aucu ON invo.accounting_order_id = aucu.id
            LEFT JOIN invoice_bank_statement invb ON invo.bank_id = invb.id

        <where>

            <!--
            这个对账单的对账状态为什么要 = 1
            已取消(-1, "已取消"),
            未确认(0, "未确认"),
            已确认(1, "已确认"),
            已拒绝(2, "已拒绝"),
            待提交(3, "待提交"),
            -->
            AND aucu.accounting_status =1
            <if test="invoiceApplyId != null ">
                and invo.invoice_apply_id = #{invoiceApplyId,jdbcType=BIGINT}
            </if>
        </where>

    </select>

    <!-- 客户结算 - 发票申请 - 查看详情 一般订单类 TODO 要重构 mark by linjinyu 202011121815 TODO 关联性修改还有没做 -->
    <!-- TODO 再次证明有逻辑问题 不能用 出发点就已经有问题 mark by linjinyu 20210201 -->
    <select id="findInvoiceApplyDetailNewDim1Unified2Goods" parameterType="java.lang.Long" resultMap="ViewDzOrderResultMap">
        SELECT

            aucu.accounting_order_sn AS accounting_order_sn,

            aucuo.id AS dz_order_id,
            aucuo.id AS id,
            aucuo.dz_id AS dz_id,
            aucuo.order_id AS order_id,
            aucuo.order_sn AS order_sn,
            aucuo.price AS order_price,
            aucuo.parent_order_id AS parent_order_id,
            aucuo.parent_order_sn AS parent_order_sn,
            aucuo.create_time AS create_time,
            aucuo.address_realname AS address_realname,
            aucuo.address_mobile AS address_mobile,
            aucuo.address_address AS address_address,
            aucuo.invoice_company_name AS invoice_company_name,
            aucuo.invoice_taxpayers_i_d_code AS invoice_taxpayers_i_d_code,
            aucuo.send_time AS send_time,
            aucuo.finish_time AS finish_time,
            aucuo.`status` AS order_status,
            aucuo.pay_type AS pay_type,
            aucuo.merch_name AS merch_name,
            aucuo.search_field1 AS search_field1,
            aucuo.search_field2 AS search_field2,
            aucuo.search_field3 AS search_field3,
            aucuo.remark AS remark,
            aucuo.invoice_account AS invoice_account,
            aucuo.invoice_bank AS invoice_bank,

            aucuo.examine_id AS examine_id,
            aucuo.realname_id AS realname_id,

            aucuo.detail_url AS detail_url,
            aucuo.organization_id AS organization_id,
            aucuo.enterprise_id AS enterprise_id,
            aucuo.invoice_type AS invoice_type,

            aucuo.dispatch_name AS dispatch_name,
            aucuo.sync_time AS sync_time,
            aucuo.pay_time AS pay_time,

            uog.id AS dz_order_goods_id,
            uog.id AS unified_order_goods_id,

            uog.goods_id AS good_id,
            uog.quantity*uog.price AS price,
            uog.quantity AS total,
            uog.price AS unit_price,
            uog.goods_name AS optionname,
            uog.unit AS unit,
            uog.goods_name AS other_field1,
            uog.goods_params AS other_field2,
            0 AS dispatch_price,
            uog.quantity * uog.price AS total_price,
            uog.tax_code AS tax_code,
            uog.fiscal_code AS fiscal_code,

            invo.id as invoice_order_id,
            invo.unified_order_goods_id as dz_order_goods_id,
            invo.back_time as back_time,
            invo.bank_id as bank_id,
            invo.back_status as back_status,

            invb.count as water_count

        FROM
            (accounting_unified_customer_order aucuo
            RIGHT JOIN unified_order_goods uog ON aucuo.order_sn = uog.order_sn)

            LEFT JOIN accounting_unified_customer aucu ON aucuo.dz_id = aucu.id
            LEFT JOIN invoice_apply_x_goods invo ON invo.unified_order_goods_id = uog.id
            LEFT JOIN invoice_bank_statement invb ON invb.id = invo.bank_id

        <!--
        LEFT JOIN sys_oder_user AS real_order_user ON aucuo.realname_id = real_order_user.user_id
        LEFT JOIN sys_oder_user AS examine_order_user ON aucuo.examine_id = examine_order_user.user_id
        -->

        <where>

            <!--
            这个对账单的对账状态为什么要 = 1
            已取消(-1, "已取消"),
            未确认(0, "未确认"),
            已确认(1, "已确认"),
            已拒绝(2, "已拒绝"),
            待提交(3, "待提交"),
            -->
            AND aucu.accounting_status =1
            <if test="invoiceApplyId != null ">
                and invo.invoice_apply_id = #{invoiceApplyId,jdbcType=BIGINT}
            </if>
        </where>
    </select>

    <!-- 客户结算 - 发票申请 - 查看详情 OLD 等 TODO 原来是不等 20210120 mark by linjinyu findInvoiceApplyDetailNewDim1Unified2Goods -->
    <select id="findDzOrderByGoodId" parameterType="java.lang.Long" resultMap="ViewDzOrderResultMap">
        SELECT

            aucu.accounting_order_sn AS accounting_order_sn,

            aucuo.id AS id,
            aucuo.dz_id AS dz_id,
            aucuo.order_id AS order_id,
            aucuo.order_sn AS order_sn,
            aucuo.parent_order_id AS parent_order_id,
            aucuo.parent_order_sn AS parent_order_sn,
            aucuo.create_time AS create_time,
            aucuo.address_realname AS address_realname,
            aucuo.address_mobile AS address_mobile,
            aucuo.address_address AS address_address,
            aucuo.invoice_company_name AS invoice_company_name,
            aucuo.invoice_taxpayers_i_d_code AS invoice_taxpayers_i_d_code,
            aucuo.send_time AS send_time,
            aucuo.finish_time AS finish_time,
            aucuo.`status` AS `status`,
            CASE
            WHEN aucuo.`status` = - 1 THEN
            '已关闭'
            WHEN aucuo.`status` = 0 THEN
            '待付款'
            WHEN aucuo.`status` = 1 THEN
            '待发货'
            WHEN aucuo.`status` = 2 THEN
            '待收货'
            WHEN aucuo.`status` = 3 THEN
            '已完成'
            ELSE
            '已关闭'
            END AS status_name,
            aucuo.merch_name AS merch_name,
            <!--
            auco.service_name AS service_name,
            -->
            aucuo.pay_type AS pay_type,
            CASE
            WHEN aucuo.pay_type = 0 THEN
            '未支付'
            WHEN aucuo.pay_type = 1 THEN
            '余额支付'
            WHEN aucuo.pay_type = 2 THEN
            '在线支付'
            WHEN aucuo.pay_type = 3 THEN
            '企业月结'
            WHEN aucuo.pay_type = 11 THEN
            '后台付款'
            WHEN aucuo.pay_type = 21 THEN
            '微信支付'
            WHEN aucuo.pay_type = 22 THEN
            '支付宝支付'
            WHEN aucuo.pay_type = 23 THEN
            '银联支付'
            ELSE
            '未支付'
            END AS pay_type_name,
            aucuo.search_field1 AS search_field1,
            aucuo.search_field2 AS search_field2,
            aucuo.search_field3 AS search_field3,
            aucuo.remark AS remark,
            aucuo.invoice_account AS invoice_account,
            aucuo.invoice_bank AS invoice_bank,
            aucuo.examine_id AS examine_id,
            aucuo.realname_id AS realname_id,
            aucuo.detail_url AS detail_url,
            aucuo.organization_id AS organization_id,
            aucuo.enterprise_id AS enterprise_id,
            aucuo.invoice_type AS invoice_type,
            CASE
            WHEN aucuo.invoice_type = 1 THEN
            '增值税普票'
            ELSE
            '增值税专票'
            END AS invoice_type_name,
            aucuo.dispatch_name AS dispatch_name,
            aucuo.sync_time AS sync_time,
            aucuo.pay_time AS pay_time,
            aucog.id AS dz_order_good_id,
            aucog.dz_order_id AS dz_order_id,
            aucog.good_id AS good_id,
            <!--
            aucog.goodno AS goodno,
            -->
            aucog.price AS price,
            aucog.total AS total,
            aucog.unit_price AS unit_price,
            aucog.optionname AS optionname,
            aucog.unit AS unit,
            aucog.other_field1 AS other_field1,
            aucog.other_field2 AS other_field2,
            aucog.dispatch_price AS dispatch_price,
            aucog.total_price AS total_price,
            aucog.tax_code AS tax_code,
            aucog.fiscal_code AS fiscal_code,
            aucuo.price AS order_price,

            invo.back_time as back_time,
            invo.bank_id as bank_id,
            invo.back_status as back_status,

            invb.count as water_count

            <!--
            real_order_user.realname AS realname,
            examine_order_user.realname AS examine_name,
            -->

        FROM
            accounting_unified_customer_order aucuo
            RIGHT JOIN accounting_unified_customer_order_goods aucog ON aucuo.id = aucog.dz_order_id
            LEFT JOIN accounting_unified_customer aucu ON aucuo.dz_id = aucu.id
            LEFT JOIN invoice_apply_x_goods invo ON aucog.id = invo.unified_order_goods_id
            LEFT JOIN invoice_bank_statement invb ON invo.bank_id = invb.id
            <!--
            LEFT JOIN sys_oder_user AS real_order_user ON auco.realname_id = real_order_user.user_id
            LEFT JOIN sys_oder_user AS examine_order_user ON auco.examine_id = examine_order_user.user_id
            -->
        <where>
            AND aucu.accounting_status =1
            <if test="invoiceApplyId != null ">
                and invo.invoice_apply_id = #{invoiceApplyId,jdbcType=BIGINT}
            </if>
        </where>
    </select>

</mapper>