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


	<!-- 客户结算 发票申请管理 opt 申请开票 start -->
	<!-- **** 已较正 20200715 by linjinyu 类 内有注释 -->
	<resultMap id="InvoiceApplySelectUnifiedResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplySelectUnifiedVo">

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

		<result column="accounting_order_id" jdbcType="BIGINT" property="accountingOrderId"/>
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>

		<result column="invoice_company_name" jdbcType="VARCHAR" property="invoiceCompanyName"/>

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

		<result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
		<result column="price" jdbcType="DECIMAL" property="price"/>
		<result column="quantity" jdbcType="INTEGER" property="quantity"/>
		<result column="unit" jdbcType="VARCHAR" property="unit"/>
		<result column="unit_price" jdbcType="DECIMAL" property="unitPrice"/>
		<result column="goods_name" jdbcType="VARCHAR" property="goodsName"/>

		<!-- uog.fiscal_code,-->
		<result column="fiscal_code" jdbcType="VARCHAR" property="fiscalCode"/>
		<!-- uog.tax_code,-->
		<result column="tax_code" jdbcType="VARCHAR" property="taxCode"/>
		<!-- uog.tax_rate,-->
		<result column="tax_rate" jdbcType="DECIMAL" property="taxRate"/>

	</resultMap>
	<select id="invoiceApplySelectFocusUnified" resultMap="InvoiceApplySelectUnifiedResultMap">
		SELECT
			auc.id as accounting_order_id,
			auc.accounting_order_sn,
			auc.invoice_company_name,

			auco.id,
			auco.order_sn AS order_sn,

			se.enterprise_id AS purchase_virt_id,
			se.enterprise_name AS purchase_virt_name,

			uog.`id` AS goods_id,
			uog.quantity * uog.price as `price`,
			uog.quantity as quantity,
			uog.unit as unit,
			uog.price as unit_price,
			uog.goods_name AS goods_name,

			uog.fiscal_code,
			uog.tax_code,
			uog.tax_rate,

			real_order_user.realname
		FROM
			( accounting_unified_customer_order auco RIGHT JOIN unified_order_goods uog ON auco.order_sn = uog.order_sn )
			LEFT JOIN sys_oder_user real_order_user ON auco.realname_id = real_order_user.user_id
			LEFT JOIN accounting_unified_customer auc ON auco.dz_id = auc.id
			LEFT JOIN sys_enterprise se ON auc.enterprise_id = se.enterprise_id
		<where>
			NOT EXISTS (
			SELECT
				1
			FROM
				invoice_order io
				JOIN invoice_apply ir ON io.invoice_apply_id = ir.id
			WHERE
				io.dz_good_id = uog.id
				AND ir.audit_status IN (1, 2)
			)

		<if test="orderSn != null and orderSn != ''">
			and auco.order_sn like CONCAT(CONCAT('%',#{orderSn,jdbcType=VARCHAR}),'%')
		</if>


		<if test="purchaseVirtId != null and purchaseVirtId != '' ">
			and se.enterprise_id = #{purchaseVirtId}
		</if>
		<if test="purchaseVirtName != null and purchaseVirtName != ''">
			and se.enterprise_name like CONCAT(CONCAT('%',#{purchaseVirtName,jdbcType=VARCHAR}),'%')
		</if>

		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and auc.accounting_order_sn like CONCAT(CONCAT('%',#{accountingOrderSn,jdbcType=VARCHAR}),'%')
		</if>
		<if test="accountingStatus != null and accountingStatus != ''">
			and auc.accounting_status = #{accountingStatus}
		</if>
		</where>
	</select>
	<!-- 客户结算 发票申请管理 opt 申请开票   end -->


	<!-- 客户结算 发票申请管理 List & Head start -->
	<resultMap id="InvoiceApplyPage" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyPage">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="invoice_no" jdbcType="VARCHAR" property="invoiceNo"/>
		<result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>

		<result column="apply_user_id" jdbcType="BIGINT" property="applyUserId"/>
		<result column="apply_user_name" jdbcType="VARCHAR" property="applyUserName"/>
		<result column="apply_time" jdbcType="TIMESTAMP" property="applyTime"/>

		<result column="dz_quantity" jdbcType="INTEGER" property="dzQuantity"/>
		<result column="dz_money" jdbcType="DECIMAL" property="dzMoney"/>

		<result column="back_money" jdbcType="DECIMAL" property="backMoney"/>
		<result column="no_back_money" jdbcType="DECIMAL" property="noBackMoney"/>
		<result column="max_back_time" jdbcType="TIMESTAMP" property="maxBackTime"/>

		<result column="audit_status" jdbcType="TINYINT" property="auditStatus"/>
		<result column="audit_user_id" jdbcType="BIGINT" property="auditUserId"/>
		<result column="audit_user_name" jdbcType="VARCHAR" property="auditUserName"/>
		<result column="audit_time" jdbcType="TIMESTAMP" property="auditTime"/>

		<result column="make_invoice_status" jdbcType="TINYINT" property="makeInvoiceStatus"/>
		<result column="make_invoice_time" jdbcType="TIMESTAMP" property="makeInvoiceTime"/>
		<result column="make_user_id" jdbcType="BIGINT" property="makeUserId"/>
		<result column="make_user_name" jdbcType="VARCHAR" property="makeUserName"/>
		<result column="mail_tick_time" jdbcType="TIMESTAMP" property="mailTickTime"/>

		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus"/>
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle"/>

		<result column="remark" jdbcType="VARCHAR" property="remark"/>
		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus"/>
		<result column="company_name" jdbcType="VARCHAR" property="companyName"/>
		<result column="express_company_id" jdbcType="BIGINT" property="expressCompanyId"/>
		<result column="express_company_name" jdbcType="VARCHAR" property="expressCompanyName"/>
		<result column="express_no" jdbcType="VARCHAR" property="expressNo"/>

		<result column="inv_type" property="invType" jdbcType="VARCHAR"/>
		<result column="tax_rate" property="taxRate" jdbcType="VARCHAR"/>
		<result column="free_amount" property="freeAmount" jdbcType="DECIMAL"/>
		<result column="real_amount" property="realAmount" jdbcType="DECIMAL"/>
	</resultMap>

	<select id="selectByCondition" resultMap="InvoiceApplyPage">
		<!-- 条件搜索 -->
		SELECT
		count(distinct t5.dz_order_id) as dz_quantity,
		ia.real_amount as dz_money,
		ia.real_amount as back_money,
		ia.no_check_count as no_back_money,
		MAX(t5.back_time) AS max_back_time,
		ia.*, t2.fullname AS apply_user_name,
		t3.fullname AS audit_user_name,
		t4.fullname AS make_user_name,
		group_concat(DISTINCT t10.enterprise_name) AS company_name,

		t9.company_name as express_company_name
		FROM
			invoice_apply ia
			LEFT JOIN sys_user t2 ON ia.apply_user_id = t2.id
			LEFT JOIN sys_user t3 ON ia.audit_user_id = t3.id
			LEFT JOIN sys_user t4 ON ia.make_user_id = t4.id


			LEFT JOIN invoice_order t5 ON ia.id = t5.invoice_apply_id

			LEFT JOIN accounting_unified_customer t6 ON t5.dz_id = t6.id
			LEFT JOIN accounting_unified_customer_order t7 ON t5.dz_order_id = t7.id

			LEFT JOIN express_company t9 ON ia.express_company_id = t9.id
			LEFT JOIN sys_enterprise t10 ON t6.enterprise_id = t10.enterprise_id
			LEFT JOIN sys_user su1 ON t7.realname_id = su1.id
		<where>

			and ia.accounting_order_type = 1

			and t5.accounting_order_type = 1

			<if test="timeField != null and timeField != '' and startTime != null and startTime != '' and endTime != null and endTime != ''">
				and TO_DAYS(ia.${timeField}) &gt;= TO_DAYS(#{startTime,jdbcType=VARCHAR})
				and TO_DAYS(ia.${timeField}) &lt;= TO_DAYS(#{endTime,jdbcType=VARCHAR})

			</if>
			<if test="backtimestar != null and backtimestar != '' and backtimeend != null and backtimeend !=''">
				and TO_DAYS(t5.back_time) &gt;= TO_DAYS(#{backtimestar,jdbcType=VARCHAR})
				and TO_DAYS(t5.back_time) &lt;= TO_DAYS(#{backtimeend,jdbcType=VARCHAR})
			</if>


			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_no' and keywords != null and keywords != ''">
				and ia.invoice_no like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'apply_user_name' and keywords != null and keywords != ''">
				and t2.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'audit_user_name' and keywords != null and keywords != ''">
				and t3.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'company_name' and keywords != null and keywords != ''">
				and t10.enterprise_name like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>


			<if test="companyName != null and companyName != '' ">
				and t10.enterprise_name like CONCAT(CONCAT('%',#{companyName,jdbcType=VARCHAR}),'%')
			</if>
			<if test="invoiceTitle != null and invoiceTitle != '' ">
				and ia.invoice_title like CONCAT(CONCAT('%',#{invoiceTitle,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_number' and keywords != null and keywords != ''">
				and ia.invoice_number like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="invNumber != null and invNumber != ''">
				and ia.invoice_number like CONCAT(CONCAT('%',#{invNumber}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'dz_sn' and keywords != null and keywords != ''">
				and t6.dz_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">
				and t7.order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="makeInvoiceStatus != null">
				and ia.make_invoice_status IN
				<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
					#{item}
				</foreach>
			</if>
			<if test="merchName != null">
				and t7.merch_name = #{merchName,jdbcType=VARCHAR}
			</if>
			<if test="addressRealname != null">
				and t7.address_realname = #{addressRealname,jdbcType=VARCHAR}
			</if>
			<if test="purchaser != null">
				AND EXISTS (SELECT 1 from sys_oder_user sou1 WHERE sou1.user_id = t7.realname_id AND sou1.realname =
				#{purchaser,jdbcType=VARCHAR})
			</if>
			<if test="auditStatus != null">
				and ia.audit_status = #{auditStatus,jdbcType=TINYINT}
			</if>
			<if test="invoiceStatus != null">
				and ia.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
			</if>
			<if test="backAccountStatus != null">
				and ia.back_account_status = #{backAccountStatus,jdbcType=TINYINT}
			</if>
			<if test="id != null">
				and ia.id = #{id,jdbcType=BIGINT}
			</if>
			<!-- organization_id -->
			<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
				and t10.enterprise_id in
				<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
					#{pid}
				</foreach>
			</if>
			<if test="outServices != null and outServices != ''">
				and t7.order_sn in (
				SELECT order_sn from unified_order where service_no in
				(
				SELECT service_no from unified_service_group where group_code in
				<foreach collection="outServices" index="index" item="serviceno" open="(" close=")" separator=",">
					#{serviceno}
				</foreach>
				)
				)
			</if>
		</where>
		GROUP BY ia.id
		order by ia.apply_time DESC
	</select>

	<resultMap id="findViewDataMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDataVo">
		<result column="apply_num" jdbcType="VARCHAR" property="applyNum"/>
		<result column="order_num" jdbcType="VARCHAR" property="orderNum"/>
		<result column="count" jdbcType="VARCHAR" property="count"/>
	</resultMap>

	<!-- **** 现在 存在的问题 一般 积分 订单的不能共用 -->
	<select id="findViewData" resultMap="findViewDataMap">
		SELECT
			count(1) AS apply_num,
			SUM(dz_quantity) AS order_num,
			SUM(dz_money) AS count
		FROM
		(
			SELECT
				count(distinct t5.dz_order_id) AS dz_quantity,
				SUM(t8.price) AS dz_money
			FROM
				invoice_apply ia
				LEFT JOIN invoice_order t5 ON ia.id = t5.invoice_apply_id
				LEFT JOIN accounting_unified_customer t6 ON t5.dz_id = t6.id
				LEFT JOIN accounting_unified_customer_order t7 ON t5.dz_order_id = t7.id
				LEFT JOIN accounting_unified_customer_order_goods t8 ON t5.dz_good_id = t8.id
				LEFT JOIN sys_enterprise t9 ON t6.enterprise_id = t9.enterprise_id
				LEFT JOIN sys_user t2 ON ia.apply_user_id = t2.id
				LEFT JOIN sys_user t3 ON ia.audit_user_id = t3.id
				LEFT JOIN sys_user t4 ON ia.make_user_id = t4.id
			<where>

				and ia.accounting_order_type = 1

				and t5.accounting_order_type = 1

				<if test="timeField != null and timeField != '' and startTime != null and startTime != '' and endTime != null and endTime != ''">
					and TO_DAYS(ia.${timeField}) &gt;= TO_DAYS(#{startTime,jdbcType=VARCHAR})
					and TO_DAYS(ia.${timeField}) &lt;= TO_DAYS(#{endTime,jdbcType=VARCHAR})

				</if>
				<if test="backtimestar != null and backtimestar != '' and backtimeend != null and backtimeend !=''">
					and TO_DAYS(t5.back_time) &gt;= TO_DAYS(#{backtimestar,jdbcType=VARCHAR})
					and TO_DAYS(t5.back_time) &lt;= TO_DAYS(#{backtimeend,jdbcType=VARCHAR})
				</if>


				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_no' and keywords != null and keywords != ''">
					and ia.invoice_no like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'apply_user_name' and keywords != null and keywords != ''">
					and t2.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'audit_user_name' and keywords != null and keywords != ''">
					and t3.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'company_name' and keywords != null and keywords != ''">
					and t9.enterprise_name like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_number' and keywords != null and keywords != ''">
					and ia.invoice_number like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="invNumber != null and invNumber !=''">
					and ia.invoice_number like CONCAT(CONCAT('%',#{invNumber}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'dz_sn' and keywords != null and keywords != ''">
					and t6.dz_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">
					and t7.order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
				</if>
				<if test="makeInvoiceStatus != null">
					and ia.make_invoice_status IN
					<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="auditStatus != null">
					and ia.audit_status = #{auditStatus,jdbcType=TINYINT}
				</if>
				<if test="invoiceStatus != null">
					and ia.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
				</if>
				<if test="backAccountStatus != null">
					and ia.back_account_status = #{backAccountStatus,jdbcType=TINYINT}
				</if>
				<if test="id != null">
					and ia.id = #{id,jdbcType=BIGINT}
				</if>
				<!-- organization_id -->
				<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
					and t9.enterprise_id in
					<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
						#{pid}
					</foreach>
				</if>
				<if test="outServices != null and outServices != ''">
					and t7.order_sn in (
						SELECT
							order_sn
						from
							unified_order
						where
							service_no in
							(
								SELECT service_no from unified_service_group where group_code in
								<foreach collection="outServices" index="index" item="serviceno" open="(" close=")" separator=",">
									#{serviceno}
								</foreach>
							)
					)
				</if>
			</where>
			GROUP BY ia.id
		) a
	</select>
	<!-- 客户结算 发票申请管理 List & Head   end -->



	<!-- 是否全部开票的问题 0 未开票 1 全部开票 2 部分开票 -->
	<select id="checkInvoiceStatusByAccountingOrderId" parameterType="java.lang.Long" resultType="java.lang.Integer">
    SELECT
	   CASE
	    WHEN t2.bindGood = 0 THEN
	        0
	    WHEN t1.allGood = t2.bindGood THEN
	        1
	    WHEN t1.allGood - t2.bindGood > 0 THEN
	        2
	    ELSE
	        0
	    END
    FROM
	(
		SELECT
            COUNT(1) AS allGood
        FROM
            accounting_unified_customer_order_goods t1
	        LEFT JOIN accounting_unified_customer_order t2 ON t1.dz_order_id = t2.id
	        LEFT JOIN accounting_unified_customer t3 ON t2.dz_id = t3.id
        WHERE
            t3.id = #{id,jdbcType=BIGINT}
	) t1,
	(
		SELECT
			COUNT(1) as bindGood
		FROM
			invoice_order t1
			LEFT JOIN invoice_apply t2 ON t1.invoice_apply_id = t2.id
		WHERE
			t2.audit_status in (1,2)
		AND t1.dz_id = #{id,jdbcType=BIGINT}
	) t2
  </select>


	<!-- 客户结算 - 发票申请 - 查看详情 一般订单类 -->
	<select id="findInvoiceApplyDetailNewDim1Unified2Goods" parameterType="java.lang.Long" resultMap="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapperEx.ViewDzOrderResultMap">
		SELECT
			auco.id AS id,
			auco.dz_id AS dz_id,

			aucu.accounting_order_sn AS accounting_order_sn,

			auco.order_id AS order_id,
			auco.order_sn AS order_sn,
			auco.parent_order_id AS parent_order_id,
			auco.parent_order_sn AS parent_order_sn,
			auco.create_time AS create_time,
			auco.address_realname AS address_realname,
			auco.address_mobile AS address_mobile,
			auco.address_address AS address_address,
			auco.companyName AS companyName,
			auco.taxpayersIDcode AS taxpayersIDcode,
			auco.send_time AS send_time,
			auco.finish_time AS finish_time,
			auco.`status` AS order_status,
			auco.pay_type AS pay_type,
			auco.merch_name AS merch_name,
			auco.search_field1 AS search_field1,
			auco.search_field2 AS search_field2,
			auco.search_field3 AS search_field3,
			auco.remark AS remark,
			auco.invoice_account AS invoice_account,
			auco.invoice_bank AS invoice_bank,
			auco.examine_id AS examine_id,
			auco.realname_id AS realname_id,
			auco.detail_url AS detail_url,
			auco.organization_id AS organization_id,
			auco.enterprise_id AS enterprise_id,
			auco.invoice_type AS invoice_type,

			auco.dispatchname AS dispatchname,
			auco.sync_time AS sync_time,
			auco.pay_time AS pay_time,

			uog.id AS dz_order_good_id,

			auco.id AS dz_order_id,

			uog.goods_id AS good_id,
			'' AS goodno,
			uog.quantity*uog.price AS price,
			uog.quantity AS total,
			uog.price AS unit_price,
			uog.goods_name AS optionname,
			'' 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,
			'' AS tax_code,
			'' AS fiscal_code,

			real_order_user.realname AS realname,
			examine_order_user.realname AS examine_name,

			auco.price AS order_price,

			invoice_order.back_time as back_time,
			invoice_order.bank_id as bank_id,
			invoice_order.back_status as back_status,

			invoice_bank.count as water_count
		FROM
			(accounting_unified_customer_order auco
			RIGHT JOIN unified_order_goods uog ON auco.order_sn = uog.order_sn)

			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

			LEFT JOIN accounting_unified_customer aucu ON auco.dz_id = aucu.id
			LEFT JOIN invoice_order ON uog.id = invoice_order.dz_good_id
			LEFT JOIN invoice_bank ON invoice_order.bank_id = invoice_bank.id
		<where>

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


	<resultMap id="InvoiceApplyExcelResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyExcel">
		<result column="id" jdbcType="BIGINT" property="id"/>
		<result column="dz_id" jdbcType="BIGINT" property="dzId"/>
		<!--
		aucu.`dz_sn` AS `dz_sn`,
		<result column="dz_sn" jdbcType="VARCHAR" property="dzSn"/>
		-->
		<!--
		aucu.accounting_order_sn AS accounting_order_sn,
		-->
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>


		<result column="order_id" jdbcType="BIGINT" property="orderId"/>
		<result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>
		<result column="parent_order_id" jdbcType="INTEGER" property="parentOrderId"/>
		<result column="parent_order_sn" jdbcType="VARCHAR" property="parentOrderSn"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="address_realname" jdbcType="VARCHAR" property="addressRealname"/>
		<result column="address_mobile" jdbcType="VARCHAR" property="addressMobile"/>
		<result column="address_address" jdbcType="VARCHAR" property="addressAddress"/>
		<result column="companyName" jdbcType="VARCHAR" property="companyname"/>
		<result column="taxpayersIDcode" jdbcType="VARCHAR" property="taxpayersidcode"/>

		<result column="send_time" jdbcType="TIMESTAMP" property="sendTime"/>
		<result column="finish_time" jdbcType="TIMESTAMP" property="finishTime"/>

		<result column="status" jdbcType="TINYINT" property="status"/>
		<result column="merch_name" jdbcType="VARCHAR" property="merchName"/>
		<result column="service_name" jdbcType="VARCHAR" property="serviceName"/>
		<result column="pay_type" jdbcType="TINYINT" property="payType"/>
		<result column="search_field1" jdbcType="VARCHAR" property="searchField1"/>
		<result column="search_field2" jdbcType="VARCHAR" property="searchField2"/>
		<result column="search_field3" jdbcType="VARCHAR" property="searchField3"/>
		<result column="remark" jdbcType="VARCHAR" property="remark"/>
		<result column="invoice_account" jdbcType="VARCHAR" property="invoiceAccount"/>
		<result column="invoice_bank" jdbcType="VARCHAR" property="invoiceBank"/>
		<result column="examine_id" jdbcType="INTEGER" property="examineId"/>
		<result column="realname_id" jdbcType="INTEGER" property="realnameId"/>
		<result column="detail_url" jdbcType="VARCHAR" property="detailUrl"/>
		<result column="organization_id" jdbcType="BIGINT" property="organizationId"/>
		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType"/>
		<result column="dispatchname" jdbcType="VARCHAR" property="dispatchname"/>
		<result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
		<result column="pay_time" jdbcType="TIMESTAMP" property="payTime"/>
		<result column="dz_order_good_id" jdbcType="BIGINT" property="dzOrderGoodId"/>
		<result column="dz_order_id" jdbcType="BIGINT" property="dzOrderId"/>
		<result column="good_id" jdbcType="BIGINT" property="goodId"/>
		<result column="goodno" jdbcType="VARCHAR" property="goodno"/>
		<result column="price" jdbcType="DECIMAL" property="price"/>
		<result column="total" jdbcType="INTEGER" property="total"/>
		<result column="unit_price" jdbcType="DECIMAL" property="unitPrice"/>
		<result column="optionname" jdbcType="VARCHAR" property="optionname"/>
		<result column="unit" jdbcType="VARCHAR" property="unit"/>
		<result column="other_field1" jdbcType="VARCHAR" property="otherField1"/>
		<result column="other_field2" jdbcType="VARCHAR" property="otherField2"/>
		<result column="dispatch_price" jdbcType="DECIMAL" property="dispatchPrice"/>
		<result column="total_price" jdbcType="DECIMAL" property="totalPrice"/>
		<result column="tax_code" jdbcType="VARCHAR" property="taxCode"/>
		<result column="fiscal_code" jdbcType="VARCHAR" property="fiscalCode"/>
		<result column="realname" jdbcType="VARCHAR" property="realname"/>
		<result column="examine_name" jdbcType="VARCHAR" property="examineName"/>
		<result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>
		<result column="order_price" jdbcType="DECIMAL" property="orderPrice"/>
		<result column="status_name" jdbcType="VARCHAR" property="statusName"/>
		<result column="pay_type_name" jdbcType="VARCHAR" property="paytypeName"/>
		<result column="invoice_type_name" jdbcType="VARCHAR" property="invoiceTypeName"/>
		<result column="organization_name" jdbcType="VARCHAR" property="organizationName"/>
		<result column="mobile" jdbcType="VARCHAR" property="mobile"/>
		<result column="express_company_id" jdbcType="BIGINT" property="expressCompanyId"/>
		<result column="express_company_name" jdbcType="VARCHAR" property="expressCompanyName"/>
		<result column="express_no" jdbcType="VARCHAR" property="expressNo"/>
		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus"/>
		<result column="back_account_status_name" jdbcType="VARCHAR" property="backAccountStatusName"/>
		<result column="make_invoice_time" jdbcType="TIMESTAMP" property="makeInvoiceTime"/>
		<result column="make_invoice_status" jdbcType="TINYINT" property="makeInvoiceStatus"/>
		<result column="make_invoice_status_name" jdbcType="VARCHAR" property="makeInvoiceStatusName"/>
		<result column="invoice_no" jdbcType="VARCHAR" property="invoiceNo"/>
		<result column="invoice_remark" jdbcType="VARCHAR" property="invoiceRemark"/>
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle"/>
	</resultMap>
	<select id="exportInvoiceApplyExcelFocusUnified" resultMap="InvoiceApplyExcelResultMap">
		SELECT
		auco.`id` AS `id`,
		auco.`dz_id` AS `dz_id`,

		<!--
		aucu.`dz_sn` AS `dz_sn`,
		-->
		aucu.accounting_order_sn AS accounting_order_sn,

		auco.`order_id` AS `order_id`,
		auco.`order_sn` AS `order_sn`,

		auco.`parent_order_id` AS `parent_order_id`,
		auco.`parent_order_sn` AS `parent_order_sn`,

		auco.`address_realname` AS `address_realname`,
		auco.`address_mobile` AS `address_mobile`,
		auco.`address_address` AS `address_address`,
		auco.`companyName` AS `companyName`,
		auco.`taxpayersIDcode` AS `taxpayersIDcode`,

		auco.create_time AS create_time,
		auco.send_time AS send_time,
		auco.finish_time AS finish_time,

		auco.`status` AS order_status,
		auco.merch_name AS merch_name,

		auco.`pay_type` AS `pay_type`,

		auco.search_field1 AS search_field1,
		auco.search_field2 AS search_field2,
		auco.search_field3 AS search_field3,

		auco.remark AS remark,

		auco.`invoice_account` AS `invoice_account`,
		auco.`invoice_bank` AS `invoice_bank`,

		auco.`examine_id` AS `examine_id`,
		auco.`realname_id` AS `realname_id`,
		auco.`detail_url` AS `detail_url`,
		auco.`organization_id` AS `organization_id`,
		`sys_organization`.`organization_name` AS `organization_name`,
		auco.`enterprise_id` AS `enterprise_id`,
		`sys_enterprise`.`enterprise_name` AS `enterprise_name`,
		auco.`invoice_type` AS `invoice_type`,
		CASE
		WHEN auco.invoice_type = 1 THEN
		'增值税普票'
		ELSE
		'增值税专票'
		END AS invoice_type_name,
		auco.`dispatchname` AS `dispatchname`,
		auco.`sync_time` AS `sync_time`,
		auco.`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`,

		real_order_user.realname AS realname,
		real_order_user.mobile AS mobile,

		examine_order_user.realname AS examine_name,
		invoice_order.invoice_apply_id,


		express_company.company_name AS express_company_name,

		invoice_apply.express_company_id,
		invoice_apply.express_no,

		invoice_apply.invoice_no,
		invoice_apply.invoice_number,

		invoice_apply.remark as invoice_remark,
		invoice_apply.make_invoice_time,
		invoice_apply.make_invoice_status,
		invoice_apply.invoice_title,

		CASE
		WHEN invoice_apply.make_invoice_status = 0 THEN
		'未开票'
		WHEN invoice_apply.make_invoice_status = 1 THEN
		'已开票'
		WHEN invoice_apply.make_invoice_status = 2 THEN
		'重新开票'
		ELSE
		'未开票'
		END AS make_invoice_status_name,

		invoice_apply.back_account_status,
		CASE
		WHEN invoice_apply.back_account_status = 0 THEN
		'未回款'
		WHEN invoice_apply.back_account_status = 1 THEN
		'部分回款'
		WHEN invoice_apply.back_account_status = 2 THEN
		'全部回款'
		ELSE
		'未回款'
		END AS back_account_status_name,
		auco.price AS order_price
		FROM
			(accounting_unified_customer_order auco
			RIGHT JOIN `accounting_unified_customer_order_goods` aucog ON auco.`id` = aucog.`dz_order_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

			LEFT JOIN accounting_unified_customer aucu ON auco.dz_id = aucu.id
			LEFT JOIN sys_organization ON auco.organization_id = sys_organization.organization_id
			LEFT JOIN sys_enterprise ON auco.enterprise_id = sys_enterprise.enterprise_id

			RIGHT JOIN invoice_order ON aucog.id = invoice_order.dz_good_id

			LEFT JOIN invoice_apply ON invoice_order.invoice_apply_id = invoice_apply.id
			LEFT JOIN express_company ON invoice_apply.express_company_id = express_company.id

			LEFT JOIN sys_user t2 ON invoice_apply.apply_user_id = t2.id
			LEFT JOIN sys_user t3 ON invoice_apply.audit_user_id = t3.id
		<where>
			<if test="timeField != null and timeField != '' and startTime != null and startTime != '' and endTime != null and endTime != ''">
				and TO_DAYS(invoice_apply.${timeField}) &gt;= TO_DAYS(#{startTime,jdbcType=VARCHAR})
				and TO_DAYS(invoice_apply.${timeField}) &lt;= TO_DAYS(#{endTime,jdbcType=VARCHAR})
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_no' and keywords != null and keywords != ''">
				and invoice_apply.invoice_no like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'apply_user_name' and keywords != null and keywords != ''">
				and t2.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'audit_user_name' and keywords != null and keywords != ''">
				and t3.fullname like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="makeInvoiceStatus != null">
				and invoice_apply.make_invoice_status IN
				<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
					#{item}
				</foreach>
			</if>
			<if test="auditStatus != null">
				and invoice_apply.audit_status = #{auditStatus,jdbcType=TINYINT}
			</if>
			<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
				and auco.order_sn in
				(
				SELECT
					sn
				from
					unified_order
				where
					purchase_virt_id in
					<!-- project_id -->
					<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
						#{pid}
					</foreach>
					<if test="outServices != null and outServices != ''">
						and service_no in
						(
						SELECT
							service_no
						from
							unified_service_group
						where
							group_code in
							<foreach collection="outServices" index="index" item="serviceno" open="(" close=")" separator=",">
								#{serviceno}
							</foreach>
						)
					</if>
				)

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