<?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="invoiceApplySelectFocusUnifiedDimOrder" resultMap="InvoiceApplySelectUnifiedResultMap">
		SELECT
			auc.id as accounting_order_id,
			auc.accounting_order_sn,
			auc.invoice_company_name,

			<!-- c -->
			aucuo.id,
			aucuo.order_sn AS order_sn,

			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,

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

			real_order_user.realname
		FROM
			( accounting_unified_customer_order aucuo RIGHT JOIN unified_order_goods uog ON aucuo.order_sn = uog.order_sn )
			LEFT JOIN sys_oder_user real_order_user ON aucuo.realname_id = real_order_user.user_id
			LEFT JOIN accounting_unified_customer auc ON aucuo.dz_id = auc.id
			LEFT JOIN sys_enterprise se ON auc.enterprise_id = se.enterprise_id
		<where>
			NOT EXISTS (
			SELECT
				1
			FROM
				invoice_apply_x_goods invaxg
				JOIN invoice_apply inva ON invaxg.invoice_apply_id = inva.id
			<where>
				and invaxg.unified_order_goods_id = uog.id
				AND inva.audit_status IN (1, 2)
			</where>
			)

		<if test="orderSn != null and orderSn != ''">
			and aucuo.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>



	<select id="invoiceApplySelectFocusUnifiedDimGoods" resultMap="InvoiceApplySelectUnifiedResultMap">
		SELECT
			aucu.id as accounting_order_id,
			aucu.accounting_order_sn,
			aucu.invoice_company_name,

			<!-- c -->
			uo.id,
			uo.order_sn AS order_sn,

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

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

			real_order_user.realname
		FROM
			( accounting_unified_customer aucu RIGHT JOIN accounting_unified_customer_x_goods aucuxg ON aucu.accounting_order_sn = aucuxg.accounting_order_sn )
			LEFT JOIN unified_order_goods uog ON aucuxg.unified_order_goods_id = uog.id
			LEFT JOIN unified_order uo ON aucuxg.unified_order_id = uo.id
			LEFT JOIN sys_oder_user real_order_user ON uo.purchase_user_id = real_order_user.user_id
			LEFT JOIN sys_enterprise se ON aucu.enterprise_id = se.enterprise_id
		<where>
			NOT EXISTS (
				SELECT
					1
				FROM
					invoice_apply_x_goods invaxg
					JOIN invoice_apply inva ON invaxg.invoice_apply_id = inva.id
				<where>
					AND invaxg.unified_order_goods_id = uog.id
					AND inva.audit_status IN (1, 2)
				</where>
			)

			<if test="orderSn != null and orderSn != ''">
				and uo.order_sn like 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('%',#{purchaseVirtName,jdbcType=VARCHAR},'%')
			</if>

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


	<!-- 客户结算 发票申请管理 List & Head start -->
	<resultMap id="InvoiceApplyPageResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyCustomerPageVo">

		<id column="id" jdbcType="BIGINT" property="id"/>

		<result column="accounting_order_type" jdbcType="TINYINT" property="accountingOrderType"/>
		<result column="dim_create_by" jdbcType="TINYINT" property="dimCreateBy"/>

		<result column="out_virt_names_str" jdbcType="VARCHAR" property="outVirtNamesStr"/>

		<result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn"/>
		<result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle"/>
		<result column="invoice_type" property="invoiceType" jdbcType="TINYINT"/>

		<result column="tax_rate" property="taxRate" jdbcType="VARCHAR"/>

		<!-- 发票申请部分 -->
		<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="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="audit_status" jdbcType="TINYINT" property="auditStatus"/>

		<!-- 开票进展部分 -->
		<!-- inva.invoice_status,-->
		<!-- inva.invoice_progressing_user_id,-->
		<!-- inva.invoice_progressing_time,-->
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus"/>
		<result column="invoice_progressing_user_id" jdbcType="BIGINT" property="invoiceProgressingUserId"/>
		<result column="invoice_progressing_time" jdbcType="TIMESTAMP" property="invoiceProgressingTime"/>

		<!-- 发票登记部分 -->
		<result column="make_invoice_user_id" jdbcType="BIGINT" property="makeInvoiceUserId"/>
		<result column="make_invoice_user_name" jdbcType="VARCHAR" property="makeInvoiceUserName"/>
		<result column="make_invoice_time" jdbcType="TIMESTAMP" property="makeInvoiceTime"/>
		<result column="make_invoice_status" jdbcType="TINYINT" property="makeInvoiceStatus"/>

		<!-- 运营领票部分 -->
		<!-- inva.receive_invoice_status,-->
		<!-- inva.receive_invoice_user_id,-->
		<!-- inva.receive_invoice_time,-->
		<result column="receive_invoice_status" jdbcType="TINYINT" property="receiveInvoiceStatus"/>
		<result column="receive_invoice_user_id" jdbcType="BIGINT" property="receiveInvoiceUserId"/>
		<result column="receive_invoice_time" jdbcType="TIMESTAMP" property="receiveInvoiceTime"/>

		<!-- 寄票登记部分 -->
		<result column="mail_tick_user_id" jdbcType="BIGINT" property="mailTickUserId"/>
		<result column="mail_tick_user_name" jdbcType="TIMESTAMP" property="mailTickUserName"/>
		<result column="mail_tick_time" jdbcType="TIMESTAMP" property="mailTickTime"/>
		<result column="mail_tick_status" jdbcType="TINYINT" property="mailTickStatus"/>

		<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"/>

		<!-- 回款登记部分 -->
		<!-- inva.back_account_status,-->
		<!-- inva.back_account_mode_record,-->
		<!-- inva.back_account_user_id,-->
		<!-- inva.back_account_time,-->
		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus"/>
		<result column="back_account_mode_record" jdbcType="TINYINT" property="backAccountModeRecord"/>
		<result column="back_account_user_id" jdbcType="BIGINT" property="backAccountUserId"/>
		<result column="back_account_time" jdbcType="TIMESTAMP" property="backAccountTime"/>

		<result column="sys_enterprise_ext_invoice_id" jdbcType="BIGINT" property="sysEnterpriseExtInvoiceId" />
		<result column="sys_enterprise_ext_invoice_snapshot" jdbcType="LONGVARCHAR" property="sysEnterpriseExtInvoiceSnapshot" />

		<result column="remark" jdbcType="VARCHAR" property="remark"/>
		<result column="remark2" jdbcType="VARCHAR" property="remark2"/>

		<result column="attachment_upload_files_uids" jdbcType="VARCHAR" property="attachmentUploadFilesUids" />

		<result column="order_quantity" jdbcType="INTEGER" property="orderQuantity"/>

		<result column="invoice_apply_amount" jdbcType="DECIMAL" property="invoiceApplyAmount"/>
		<result column="free_amount" jdbcType="DECIMAL" property="freeAmount"/>
		<result column="real_amount" jdbcType="DECIMAL" property="realAmount"/>
		<result column="no_back_money" jdbcType="DECIMAL" property="noBackMoney"/>

		<result column="max_back_time" jdbcType="TIMESTAMP" property="maxBackTime"/>

		<!-- group_concat(DISTINCT aucu.id) AS accounting_order_ids_str,-->
		<result column="accounting_order_ids_str" jdbcType="VARCHAR" property="accountingOrderIdsStr"/>
		<!-- group_concat(DISTINCT aucu.accounting_order_sn) AS accounting_order_sns_str -->
		<result column="accounting_order_sns_str" jdbcType="VARCHAR" property="accountingOrderSnsStr"/>

	</resultMap>

	<select id="selectByCondition" resultMap="InvoiceApplyPageResultMap">
		<!-- 条件搜索 -->
		SELECT
			inva.id,
			inva.accounting_order_type,
			inva.dim_create_by,

			group_concat(DISTINCT t10.enterprise_name) AS out_virt_names_str,

			inva.invoice_apply_sn,

			inva.invoice_number,
			inva.invoice_title,
			inva.invoice_type,
			inva.tax_rate,

			<!-- 发票申请部分 -->
			inva.apply_user_id,
			t2.fullname AS apply_user_name,
			inva.apply_time,

			<!-- 发票申请审核部分 -->
			inva.audit_user_id,
			t3.fullname AS audit_user_name,
			inva.audit_status,
			inva.audit_time,

			<!-- 开票进展部分 -->
			inva.invoice_status,
			inva.invoice_progressing_user_id,
			inva.invoice_progressing_time,

			<!-- 发票登记部分 -->
			inva.make_invoice_user_id,
			t4.fullname AS make_invoice_user_name,
			inva.make_invoice_time,
			inva.make_invoice_status,

			<!-- 运营领票部分 -->
			inva.receive_invoice_status,
			inva.receive_invoice_user_id,
			inva.receive_invoice_time,

			<!-- 寄票登记部分 -->
			inva.mail_tick_user_id,
			t5.fullname AS mail_tick_user_name,
			inva.mail_tick_status,
			inva.mail_tick_time,

			inva.express_company_id,
			inva.express_no,
			t9.company_name as express_company_name,

			<!-- 回款登记部分 -->
			inva.back_account_status,
			inva.back_account_mode_record,
			inva.back_account_user_id,
			inva.back_account_time,

			<!-- 企业信息扩展-发票抬头信息 -->
			inva.sys_enterprise_ext_invoice_id,
			inva.sys_enterprise_ext_invoice_snapshot,

			inva.remark,
			inva.remark2,

			inva.attachment_upload_files_uids,

			inva.relation_mode,
			inva.error_check_relation_01,
			inva.error_check_relation_02,

			count(distinct invaxg.unified_order_id) as order_quantity,

			inva.invoice_apply_amount,
			inva.free_amount,
			inva.real_amount,
			inva.no_check_count as no_back_money,

			MAX(invaxg.back_time) AS max_back_time,

			group_concat(DISTINCT aucu.id) AS accounting_order_ids_str,
			group_concat(DISTINCT aucu.accounting_order_sn) AS accounting_order_sns_str
		FROM
			invoice_apply inva
			LEFT JOIN invoice_apply_x_goods invaxg ON inva.id = invaxg.invoice_apply_id

			LEFT JOIN accounting_unified_customer aucu ON invaxg.accounting_order_id = aucu.id
			LEFT JOIN unified_order uo on invaxg.unified_order_id = uo.id
			<!--
			LEFT JOIN accounting_unified_customer_order aucuo ON invaxg.unified_order_id = aucuo.id
			-->

			LEFT JOIN express_company t9 ON inva.express_company_id = t9.id
			LEFT JOIN sys_enterprise t10 ON aucu.enterprise_id = t10.enterprise_id

			LEFT JOIN sys_user t2 ON inva.apply_user_id = t2.id
			LEFT JOIN sys_user t3 ON inva.audit_user_id = t3.id
			LEFT JOIN sys_user t4 ON inva.make_invoice_user_id = t4.id
			LEFT JOIN sys_user t5 ON inva.mail_tick_user_id = t5.id
			<!--
			LEFT JOIN sys_user su1 ON aucuo.realname_id = su1.id
			LEFT JOIN sys_user su1 ON uo.purchase_user_id = su1.id  好像是不需求
			-->

		<where>

			<!-- 一般订单 -->
			and inva.accounting_order_type = 1
			and invaxg.accounting_order_type = 1

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

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

			<if test="accountingOrderSn !=null and accountingOrderSn != ''">
				and aucu.accounting_order_sn like CONCAT('%',#{accountingOrderSn,jdbcType=VARCHAR},'%')
			</if>


			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_apply_sn' and keywords != null and keywords != ''">
				and inva.invoice_apply_sn 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 == 'accountingOrderSn' and keywords != null and keywords != ''">
				and aucu.accounting_order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>

			<!--
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">
				and aucuo.order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			-->
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">
				and uo.order_sn 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 inva.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 inva.invoice_number like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="invoiceNumber != null and invoiceNumber != ''">
				and inva.invoice_number like CONCAT(CONCAT('%',#{invoiceNumber}),'%')
			</if>
			<if test="auditStatus != null">
				and inva.audit_status = #{auditStatus,jdbcType=TINYINT}
			</if>
			<if test="invoiceStatus != null">
				and inva.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
			</if>
			<if test="backAccountStatus != null">
				and inva.back_account_status = #{backAccountStatus,jdbcType=TINYINT}
			</if>
			<if test="id != null">
				and inva.id = #{id,jdbcType=BIGINT}
			</if>

			<if test="makeInvoiceStatus != null">
				and inva.make_invoice_status IN
				<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
					#{item}
				</foreach>
			</if>
			<if test="merchName != null">
				and aucuo.merch_name = #{merchName,jdbcType=VARCHAR}
			</if>
			<if test="addressRealname != null">
				and aucuo.address_realname = #{addressRealname,jdbcType=VARCHAR}
			</if>
			<if test="purchaser != null">
				AND EXISTS (SELECT 1 from sys_oder_user sou1 WHERE sou1.user_id = aucuo.realname_id AND sou1.realname =
				#{purchaser,jdbcType=VARCHAR})
			</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 aucuo.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 inva.id
		order by inva.apply_time DESC
	</select>

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

		<result column="count_invoice_apply_quantity" jdbcType="INTEGER" property="countInvoiceApplyQuantity"/>
		<result column="sum_unified_order_count_quantity" jdbcType="INTEGER" property="sumUnifiedOrderCountQuantity"/>
		<result column="sum_invoice_apply_amount" jdbcType="DECIMAL" property="sumInvoiceApplyAmount"/>
		<result column="sum_free_amount" jdbcType="DECIMAL" property="sumFreeAmount"/>
		<result column="sum_real_amount" jdbcType="DECIMAL" property="sumRealAmount"/>
	</resultMap>

	<!-- **** 现在 存在的问题 一般 积分 订单的不能共用 -->
	<select id="findViewHeadDataFocusUnified" resultMap="findViewDataMap">
		SELECT
			count(1) AS count_invoice_apply_quantity,
			SUM(unified_order_count_quantity) AS sum_unified_order_count_quantity,
			SUM(sum_invoice_apply_amount) AS sum_invoice_apply_amount,
			SUM(sum_free_amount) AS sum_free_amount,
			SUM(sum_real_amount) AS sum_real_amount
		FROM
		(
			<include refid="findViewHeadDataFocusUnifiedInner"></include>
		) a
	</select>

	<sql id="findViewHeadDataFocusUnifiedInner">

		SELECT
			count(distinct invaxg.unified_order_id) AS unified_order_count_quantity,
			<!--
			SUM(inva.invoice_apply_amount) as sum_invoice_apply_amount,
			SUM(inva.free_amount) as sum_free_amount,
			SUM(inva.real_amount) as sum_real_amount
			-->
			inva.invoice_apply_amount as sum_invoice_apply_amount,
			inva.free_amount as sum_free_amount,
			inva.real_amount as sum_real_amount
		FROM
			invoice_apply inva
			LEFT JOIN invoice_apply_x_goods invaxg ON inva.id = invaxg.invoice_apply_id

			LEFT JOIN accounting_unified_customer aucu ON invaxg.accounting_order_id = aucu.id
			LEFT JOIN unified_order uo on invaxg.unified_order_id = uo.id

			<!--
			LEFT JOIN accounting_unified_customer_order aucuo ON invaxg.unified_order_id = aucuo.id
			LEFT JOIN accounting_unified_customer_order_goods aucuog ON invaxg.unified_order_goods_id = aucuog.id
			-->

			LEFT JOIN sys_enterprise t9 ON aucu.enterprise_id = t9.enterprise_id
			LEFT JOIN sys_user t2 ON inva.apply_user_id = t2.id
			LEFT JOIN sys_user t3 ON inva.audit_user_id = t3.id
			LEFT JOIN sys_user t4 ON inva.make_invoice_user_id = t4.id
			LEFT JOIN sys_user t5 ON inva.mail_tick_user_id = t5.id
		<where>

			and inva.accounting_order_type = 1
			and invaxg.accounting_order_type = 1

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

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

			<if test="accountingOrderSn !=null and accountingOrderSn != ''">
				and aucu.accounting_order_sn like CONCAT('%',#{accountingOrderSn,jdbcType=VARCHAR},'%')
			</if>


			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'invoice_apply_sn' and keywords != null and keywords != ''">
				and inva.invoice_apply_sn 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 inva.invoice_number like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="invoiceNumber != null and invoiceNumber !=''">
				and inva.invoice_number like CONCAT(CONCAT('%',#{invoiceNumber}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'accountingOrderSn' and keywords != null and keywords != ''">
				and aucu.accounting_order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">
				and aucuo.order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')
			</if>
			<if test="makeInvoiceStatus != null">
				and inva.make_invoice_status IN
				<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
					#{item}
				</foreach>
			</if>
			<if test="auditStatus != null">
				and inva.audit_status = #{auditStatus,jdbcType=TINYINT}
			</if>
			<if test="invoiceStatus != null">
				and inva.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
			</if>
			<if test="backAccountStatus != null">
				and inva.back_account_status = #{backAccountStatus,jdbcType=TINYINT}
			</if>
			<if test="id != null">
				and inva.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 aucuo.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 inva.id
	</sql>
	<!-- 客户结算 发票申请管理 List & Head   end -->



	<!-- 是否全部开票的问题 0 未开票 1 全部开票 2 部分开票 TODO accounting_unified_customer_order_goods 还用这个表就是错的 -->
	<!-- TODO 坑位标记逻辑对不上20210120 -->
	<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 aucuog
	        LEFT JOIN accounting_unified_customer_order aucuo ON aucuog.dz_order_id = aucuo.id
	        LEFT JOIN accounting_unified_customer aucu ON aucuo.dz_id = aucu.id
        WHERE
            aucu.id = #{id,jdbcType=BIGINT}
	) t1,
	(
		SELECT
			COUNT(1) as bindGood
		FROM
			invoice_apply_x_goods 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>





	<resultMap id="exportExcelInvoiceApplyFocusUnifiedResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.ExportExcelInvoiceApplyForceUnifiedDimGoodsVo">
		<result column="id" jdbcType="BIGINT" property="id"/>
		<result column="dz_id" jdbcType="BIGINT" property="dzId"/>

		<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="address_realname" jdbcType="VARCHAR" property="addressRealname"/>
		<result column="address_mobile" jdbcType="VARCHAR" property="addressMobile"/>
		<result column="address_address" jdbcType="VARCHAR" property="addressAddress"/>

		<result column="invoice_company_name" jdbcType="VARCHAR" property="invoiceCompanyName"/>
		<result column="invoice_taxpayers_i_d_code" jdbcType="VARCHAR" property="invoiceTaxpayersIDcode"/>
		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType"/>
		<result column="invoice_type_name" jdbcType="VARCHAR" property="invoiceTypeName"/>
		<result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn"/>
		<result column="invoice_remark" jdbcType="VARCHAR" property="invoiceRemark"/>
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle"/>
		<result column="invoice_account" jdbcType="VARCHAR" property="invoiceAccount"/>
		<result column="invoice_bank" jdbcType="VARCHAR" property="invoiceBank"/>

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

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

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

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


		<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="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="organization_name" jdbcType="VARCHAR" property="organizationName"/>

		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<result column="enterprise_name" jdbcType="VARCHAR" property="enterpriseName"/>

		<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_name" jdbcType="VARCHAR" property="dispatchName"/>
		<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="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"/>

	</resultMap>
	<select id="exportExcelInvoiceApplyFocusUnified"
			resultMap="exportExcelInvoiceApplyFocusUnifiedResultMap">
		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.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.create_time AS create_time,
		aucuo.send_time AS send_time,
		aucuo.finish_time AS finish_time,

		aucuo.`status` AS order_status,
		aucuo.merch_name AS merch_name,

		aucuo.pay_type AS pay_type,

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

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

		examine_order_user.realname AS examine_name,
		invoice_apply_x_goods.invoice_apply_id,


		express_company.company_name AS express_company_name,

		invoice_apply.express_company_id,
		invoice_apply.express_no,

		invoice_apply.invoice_apply_sn,
		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,
		aucuo.price AS order_price
		FROM
			(accounting_unified_customer_order aucuo
			RIGHT JOIN `accounting_unified_customer_order_goods` aucog ON aucuo.`id` = aucog.`dz_order_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

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

			RIGHT JOIN invoice_apply_x_goods ON aucog.id = invoice_apply_x_goods.dz_good_id

			LEFT JOIN invoice_apply ON invoice_apply_x_goods.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_apply_sn' and keywords != null and keywords != ''">
				and invoice_apply.invoice_apply_sn 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 aucuo.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>
