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

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

		<!-- uoc -->
		<id column="unified_order_credit_id" jdbcType="BIGINT" property="unifiedOrderCreditId"/>
		<result column="out_snapshot_order_credit_id" property="outSnapshotOrderCreditId" jdbcType="BIGINT"/>
		<result column="credit_accounting_state" jdbcType="TINYINT" property="creditAccountingState" />
		<!-- uog -->
		<result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId"/>
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />

		<!-- uo -->
		<result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId"/>

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

<!--		uoc.order_sn AS order_sn,-->
<!--		uoc.credit_type,-->
<!--		uoc.credit_name,-->
<!--		uoc.daily_account_type,-->
<!--		uoc.batchs,-->
<!--		uoc.balance,-->
<!--		uoc.invoice_apply_status,-->
<!--		uoc.invoice_make_status,-->
<!--		uoc.back_status,-->

<!--		uo.order_sn AS order_sn,-->
<!--		uo.refund_order_sn AS refund_order_sn,-->

		<result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>
		<result column="refund_order_sn" jdbcType="VARCHAR" property="refundOrderSn"/>

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

		<result column="credit_type" jdbcType="INTEGER" property="creditType" />
		<result column="credit_name" jdbcType="VARCHAR" property="creditName" />
		<result column="daily_account_type" jdbcType="INTEGER" property="dailyAccountType" />
		<result column="batchs" jdbcType="VARCHAR" property="batchs" />
		<result column="balance" jdbcType="DECIMAL" property="balance" />
		<result column="invoice_apply_status" jdbcType="TINYINT" property="invoiceApplyStatus" />
		<result column="invoice_make_status" jdbcType="TINYINT" property="invoiceMakeStatus" />
		<result column="back_status" jdbcType="TINYINT" property="backStatus" />

		<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="purchase_department_name" property="purchaseDepartmentName" jdbcType="VARCHAR"/>

		<!-- uog -->

		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
		<result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />

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

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

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

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

	</resultMap>
	<select id="invoiceApplySelectFocusScoreDimCredit"
			parameterType="cn.superdesk.uniorder.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteria"
			resultMap="InvoiceApplySelectScoreResultMap">

		SELECT

			uoc.id as unified_order_credit_id,
			ascuxc.accounting_state as credit_accounting_state,


			uog.id AS unified_order_goods_id,
			uog.goods_id,
			uog.out_snapshot_order_goods_id,

			uo.id as unified_order_id,

			ascu.id as accounting_order_id,
			ascu.accounting_order_sn as accounting_order_sn,
			<!--
			ascu.invoice_company_name,
			-->


			uo.purchase_department_name,
			uo.order_sn AS order_sn,
			uo.refund_order_sn AS refund_order_sn,

			uoc.direction,
			uoc.credit_type,
			uoc.credit_name,
			uoc.daily_account_type,
			uoc.batchs,
			uoc.balance,
			uoc.invoice_apply_status,
			uoc.invoice_make_status,
			uoc.back_status,

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


			uog.goods_name,
			uog.goods_alias,<!-- 已添加订单商品属性 -->

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

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

			<!--
			uog.fiscal_code,
			uog.tax_code,
			uog.tax_rate,
			-->

			uog.merch_tax_id,
			uog.merch_tax_code,
			uog.merch_tax_rate,
			uog.operation_tax_id,
			uog.operation_tax_code,
			uog.operation_tax_rate,

			sou.realname

		FROM
			( accounting_score_customer ascu
		    RIGHT JOIN accounting_score_customer_x_credit ascuxc on ascu.accounting_order_sn = ascuxc.accounting_order_sn )
			LEFT JOIN unified_order_credit uoc ON ascuxc.unified_order_credit_id = uoc.id
		    LEFT JOIN unified_order_goods uog ON ascuxc.unified_order_goods_id = uog.id
		    LEFT JOIN unified_order uo ON ascuxc.unified_order_id = uo.id
		    LEFT JOIN sys_enterprise se ON ascu.enterprise_id = se.enterprise_id
		    LEFT JOIN sys_oder_user AS sou ON uo.purchase_user_id = sou.user_id

		<where>
		<!-- 对账单 Id -->
		<if test="accountingOrderId != null and accountingOrderId != ''">
			and ascu.id = #{accountingOrderId}
		</if>
		<if test="accountingOrderId != null and accountingOrderId != '' and excludeInvoiceApplied != null and excludeInvoiceApplied !=''">
			and ascuxc.unified_order_credit_id not in (
				SELECT
					invaxg.unified_order_credit_id
				FROM
					invoice_apply_x_goods invaxg
					left join invoice_apply inva on invaxg.invoice_apply_id = inva.id
				<where>
					and invaxg.accounting_order_id = #{accountingOrderId}
					<!-- 1-未审核;2-同意;3-拒绝 -->
					and inva.audit_status in (1,2)
				</where>
			)
		</if>
		<if test="accountingOrderId != null and accountingOrderId != '' and unifiedOrderCreditIdsSelected != null and unifiedOrderCreditIdsSelected != ''">
			and ascuxc.unified_order_credit_id in
			<foreach collection="unifiedOrderCreditIdsSelected" index="index" item="unified_order_credit_id" open="(" close=")" separator=",">
				#{unified_order_credit_id}
			</foreach>
		</if>
		<!-- 流水的客户对账状态 防页面多开 -->
<!--		<if test="uocAccountingStateCustomer != null and uocAccountingStateCustomer != ''">-->
<!--			and uoc.accounting_state_customer = #{uocAccountingStateCustomer}-->
<!--		</if>-->

		<!-- 对账单 Sn -->
		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and ascu.accounting_order_sn = #{accountingOrderSn}
		</if>




		<if test="purchaseDepartmentName != null and purchaseDepartmentName != '' ">
			and uo.purchase_department_name like CONCAT('%',#{purchaseDepartmentName},'%')
		</if>
		</where>

		ORDER BY uoc.order_id asc

	</select>
	<!-- 积分结算 发票申请管理 opt 申请开票 start -->



	<!-- 客户结算 发票申请管理 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_status" jdbcType="TINYINT" property="makeInvoiceStatus"/>
		<result column="make_invoice_time" jdbcType="TIMESTAMP" property="makeInvoiceTime"/>

		<!-- 运营领票部分 -->
		<!-- 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="unified_order_count_quantity" jdbcType="INTEGER" property="unifiedOrderCountQuantity"/>-->

<!--		count(distinct invaxg.unified_order_id) as quantity_order,-->
<!--		count(distinct invaxg.unified_order_credit_id) as quantity_order_credit,-->

<!--		<result column="order_quantity" jdbcType="INTEGER" property="orderQuantity"/>-->
		<result column="credit_quantity" jdbcType="INTEGER" property="creditQuantity"/>

		<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 t6.accounting_order_sn) AS accounting_order_sns_str -->
		<result column="accounting_order_sns_str" jdbcType="VARCHAR" property="accountingOrderSnsStr"/>

		<!--		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>-->

		<!--		<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="company_name" jdbcType="VARCHAR" property="companyName"/>-->
	</resultMap>



	<sql id="findViewWhereIf">

		and inva.accounting_order_type = 2

		and invaxg.accounting_order_type = 2

		<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="backAccountTimeStart != null and backAccountTimeStart != '' and backAccountTimeEnd != null and backAccountTimeEnd !=''">
			and TO_DAYS(invaxg.back_account_time) &gt;= TO_DAYS(#{backAccountTimeStart,jdbcType=VARCHAR})
			and TO_DAYS(invaxg.back_account_time) &lt;= TO_DAYS(#{backAccountTimeEnd,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 == '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 == 'accountingOrderSn' and keywords != null and keywords != ''">
			and ascu.accounting_order_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 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="auditStatus != null">
			and inva.audit_status = #{auditStatus,jdbcType=TINYINT}
		</if>

		<!-- 开票进展部分 -->
		<if test="invoiceProgressingStatus != null and invoiceProgressingStatus.size > 0">
			and inva.invoice_status IN
			<foreach close=")" collection="invoiceProgressingStatus" index="index" item="item" open="(" separator=",">
				#{item}
			</foreach>
		</if>
		<!--		<if test="invoiceStatus != null">-->
		<!--			and inva.invoice_status = #{invoiceStatus,jdbcType=TINYINT}-->
		<!--		</if>-->

		<!-- 开票登记部分 -->
		<if test="makeInvoiceStatus != null and makeInvoiceStatus.size > 0">
			and inva.make_invoice_status IN
			<foreach close=")" collection="makeInvoiceStatus" index="index" item="item" open="(" separator=",">
				#{item}
			</foreach>
		</if>

		<!-- 运营领票部分 -->
		<if test="receiveInvoiceStatus != null and receiveInvoiceStatus.size > 0">
			and inva.receive_invoice_status IN
			<foreach close=")" collection="receiveInvoiceStatus" index="index" item="item" open="(" separator=",">
				#{item}
			</foreach>
		</if>

		<!-- 寄票登记部分 -->
		<if test="mailTickStatus != null and mailTickStatus.size > 0">
			and inva.mail_tick_status IN
			<foreach close=")" collection="mailTickStatus" index="index" item="item" open="(" separator=",">
				#{item}
			</foreach>
		</if>

		<!-- 回款登记部分 -->
		<if test="backAccountStatus != null">
			and inva.back_account_status = #{backAccountStatus,jdbcType=TINYINT}
		</if>


		<!-- 错误 auco 已除 -->
<!--		<if test="merchName != null">-->
<!--			and auco.merch_name = #{merchName,jdbcType=VARCHAR}-->
<!--		</if>-->
		<!-- 错误 auco 已除 -->
<!--		<if test="addressRealname != null">-->
<!--			and auco.address_realname = #{addressRealname,jdbcType=VARCHAR}-->
<!--		</if>-->
		<!-- 错误 auco 已除 -->
<!--		<if test="keywordsField != null and keywordsField != '' and keywordsField == 'order_sn' and keywords != null and keywords != ''">-->
<!--			and auco.order_sn like CONCAT(CONCAT('%',#{keywords,jdbcType=VARCHAR}),'%')-->
<!--		</if>-->


		<if test="purchaser != null">
			AND EXISTS (
				SELECT 1
				from
					sys_oder_user sou1
				WHERE
					sou1.user_id = uo.purchase_user_id
					AND sou1.realname = #{purchaser,jdbcType=VARCHAR}
			)
		</if>



		<if test="id != null">
			and inva.id = #{id,jdbcType=BIGINT}
		</if>

		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and ascu.accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		</if>


		<if test="invoiceApplyIdsSelected != null and invoiceApplyIdsSelected.size > 0">
			and inva.id in
			<foreach collection="invoiceApplyIdsSelected" index="index" item="id" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</if>

		<if test="invoiceApplySnsSelected != null and invoiceApplySnsSelected.size > 0">
			and inva.invoice_apply_sn in
			<foreach collection="invoiceApplySnsSelected" index="index" item="invoice_apply_sn" open="(" close=")" separator=",">
				#{invoice_apply_sn}
			</foreach>
		</if>

		<if test="fpqqlshSelected != null and fpqqlshSelected.size > 0">
			and inva.invoice_apply_sn in
			<foreach collection="fpqqlshSelected" index="index" item="invoice_apply_sn" open="(" close=")" separator=",">
				#{invoice_apply_sn}
			</foreach>
		</if>

		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
			<!-- organization_id -->
			and t10.enterprise_id in
			<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
				#{pid}
			</foreach>
		</if>
		<!-- 错误 auco 已除 -->
<!--		<if test="outServices != null and outServices != ''">-->
<!--			and auco.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>-->

	</sql>

	<select id="findViewByPage"
			parameterType="cn.superdesk.uniorder.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteria"
			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 unified_order_count_quantity,
			count(distinct invaxg.unified_order_id) as order_quantity,
			-->

			count(distinct invaxg.unified_order_credit_id) as credit_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 ascu.id) AS accounting_order_ids_str,
			group_concat(DISTINCT ascu.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_score_customer ascu ON invaxg.accounting_order_id = ascu.id
			LEFT JOIN unified_order uo on invaxg.unified_order_id = uo.id
			LEFT JOIN unified_order_goods uog on invaxg.unified_order_goods_id = uog.id
			LEFT JOIN unified_order_credit uoc on invaxg.unified_order_credit_id = uoc.id

			LEFT JOIN express_company t9 ON inva.express_company_id = t9.id
			LEFT JOIN sys_enterprise t10 ON ascu.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 uo.purchase_user_id = su1.id



		<where>

			<!-- 积分订单 -->
			and inva.accounting_order_type = 2
			and invaxg.accounting_order_type = 2
			<include refid="findViewWhereIf"></include>
		</where>

		GROUP BY inva.id
		order by inva.apply_time DESC
	</select>

	<resultMap id="findViewHeadDataResultMap" 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="findViewHeadData"
			parameterType="cn.superdesk.uniorder.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteria"
			resultMap="findViewHeadDataResultMap">
		SELECT
			<!--
			count(1) AS apply_num,
			SUM(dz_quantity) AS order_num,
			SUM(dz_money) AS count
			-->
			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
			(
			SELECT
				<!--
				count(distinct invaxg.unified_order_id) AS dz_quantity,
				SUM(uoc.balance) AS dz_money
				-->
				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
			FROM
				invoice_apply inva
				LEFT JOIN invoice_apply_x_goods invaxg ON inva.id = invaxg.invoice_apply_id

				LEFT JOIN accounting_score_customer ascu ON invaxg.accounting_order_id = ascu.id

				LEFT JOIN unified_order uo on invaxg.unified_order_id = uo.id
				LEFT JOIN unified_order_goods uog on invaxg.unified_order_goods_id = uog.id
				LEFT JOIN unified_order_credit uoc on invaxg.unified_order_credit_id = uoc.id

				LEFT JOIN express_company t9 ON inva.express_company_id = t9.id
				LEFT JOIN sys_enterprise t10 ON ascu.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 uo.purchase_user_id = su1.id
			<where>
				<include refid="findViewWhereIf"></include>
			</where>

			GROUP BY inva.id
		) a
	</select>
	<!-- 客户结算 发票申请管理 List & Head   end -->


	<select id="findInvoiceApplyDetailDim1Score2Credit" parameterType="java.lang.Long"
			resultMap="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapperExUnifiedCompatible.ViewDzOrderResultMap">
		SELECT
			uo.id AS unified_order_id,

			ascu.accounting_order_sn AS accounting_order_sn,

			uo.order_id AS order_id,
			uo.order_sn AS order_sn,
			uo.parent_order_id AS parent_order_id,
			uo.parent_order_sn AS parent_order_sn,

			uo.address_realname AS address_realname,
			uo.address_mobile AS address_mobile,
			uo.address_address AS address_address,
			uo.companyName AS companyName,
			uo.taxpayersIDcode AS taxpayersIDcode,

			uo.create_time AS create_time,
			uo.pay_time AS pay_time,
			uo.send_time AS send_time,
			uo.finish_time AS finish_time,
			uo.sync_time AS sync_time,

			uo.pay_state AS pay_state,
			uo.pay_type AS pay_type,

			uo.sale_id AS sale_id,
			uo.sale_name AS sale_name,

			uo.search_field1 AS search_field1,
			uo.search_field2 AS search_field2,
			uo.search_field3 AS search_field3,

			uo.remarks AS remarks,
			uo.invoice_account AS invoice_account,
			uo.invoice_bank AS invoice_bank,

			uo.examine_id AS examine_id,
			uo.realname_id AS realname_id,
			uo.detail_url AS detail_url,
			uo.organization_id AS organization_id,
			uo.enterprise_id AS enterprise_id,
			uo.invoice_type AS invoice_type,

			uo.dispatchname AS dispatchname,




			0 AS dispatch_price,

			uog.id AS unified_order_goods_id,
			uog.goods_id AS goods_id,
			uog.goods_name AS optionname,
			uog.unit AS unit,
			uog.price AS unit_price,
			uog.quantity AS quantity,
			uog.quantity*uog.price AS total_price,

			uog.goods_name AS other_field1,
			uog.goods_params AS other_field2,

			uog.tax_rate as tax_rate,
			uog.tax_code AS tax_code,
			uog.fiscal_code AS fiscal_code,

			uoc.credit_type,
			uoc.credit_name,
			uoc.daily_account_type,
			uoc.batchs,
			uoc.balance,

			invaxg.back_time as back_time,
			invaxg.bank_id as bank_id,
			invaxg.back_status as back_status,

			invb.count as water_count
			<!--
			real_order_user.realname AS realname,
			examine_order_user.realname AS examine_name
			-->
		FROM
			invoice_apply inva
			RIGHT JOIN invoice_apply_x_goods invaxg ON invaxg.invoice_apply_id = inva.id
			LEFT JOIN accounting_score_customer ascu ON invaxg.accounting_order_id = ascu.id
			LEFT JOIN unified_order uo ON invaxg.unified_order_id = uo.id
			LEFT JOIN unified_order_goods uog ON invaxg.unified_order_goods_id = uog.id
			LEFT JOIN unified_order_credit uoc ON invaxg.unified_order_credit_id = uoc.id
			LEFT JOIN invoice_bank_statement invb ON invaxg.bank_id = invb.id

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


		<where>

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

	<!-- 是否全部开票的问题 0 未开票 1 全部开票 2 部分开票 -->
	<select id="checkInvoiceStatusByAccountingOrderId" parameterType="java.lang.Long" resultType="java.lang.Integer">
    SELECT
	   CASE
	    WHEN invoice.target_credits = 0 THEN
	        0
	    WHEN accounting.source_credits = invoice.target_credits THEN
	        1
	    WHEN accounting.source_credits - invoice.target_credits > 0 THEN
	        2
		WHEN accounting.source_credits - invoice.target_credits &lt; 0 THEN
			3
	    ELSE
	        99
	    END
    FROM
	(
		SELECT
		    COUNT(1) AS source_credits
		FROM
		    accounting_score_customer ascu
			LEFT JOIN accounting_score_customer_x_credit ascuxc on ascu.id = ascuxc.accounting_order_id

		WHERE
			ascu.id = #{id,jdbcType=BIGINT}
	) accounting,
	(
		SELECT
			COUNT(1) as target_credits
		FROM
			invoice_apply_x_goods invaxg
			LEFT JOIN invoice_apply inva ON invaxg.invoice_apply_id = inva.id
		WHERE
			<!-- 1-未审核;2-同意;3-拒绝 -->
			inva.audit_status in (1,2)
			AND invaxg.accounting_order_id = #{id,jdbcType=BIGINT}
	) invoice
  </select>


	<resultMap id="exportExcelInvoiceApplyFocusScoreResultMap"
			   type="cn.superdesk.uniorder.dto.vo.invoice.customer.ExportExcelInvoiceApplyCustomerForceScoreDimCreditVo">

		<!--		invaxg.unified_order_credit_id,-->
		<id column="unified_order_credit_id" jdbcType="BIGINT" property="unifiedOrderCreditId"/>
		<result column="out_snapshot_order_credit_id" jdbcType="BIGINT" property="outSnapshotOrderCreditId"/>
		<result column="credit_accounting_state" jdbcType="TINYINT" property="creditAccountingState"/>

		<!--		invaxg.unified_order_goods_id as unified_order_goods_id,-->
		<result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId"/>
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId"/>
		<result column="goods_id" jdbcType="BIGINT" property="goodsId"/>


		<!--		invaxg.unified_order_id as unified_order_id,-->
		<result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId"/>

		<!--		invaxg.accounting_order_id as accounting_order_id,-->
		<result column="accounting_order_id" jdbcType="BIGINT" property="accountingOrderId"/>
		<result column="accounting_order_type" jdbcType="TINYINT" property="accountingOrderType"/>

		<!--		invaxg.invoice_apply_id,-->
		<result column="invoice_apply_id" jdbcType="BIGINT" property="invoiceApplyId"/>
		<result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn"/>

		<result column="invoice_progressing_user_id" jdbcType="BIGINT" property="invoiceProgressingUserId"/>
		<result column="invoice_progressing_time" jdbcType="TIMESTAMP" property="invoiceProgressingTime"/>
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus"/>

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

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

		<result column="make_invoice_user_id" jdbcType="BIGINT" property="makeInvoiceUserId"/>
		<result column="make_invoice_time" jdbcType="TIMESTAMP" property="makeInvoiceTime"/>
		<result column="make_invoice_status" jdbcType="TINYINT" property="makeInvoiceStatus"/>

		<result column="receive_invoice_user_id" jdbcType="BIGINT" property="receiveInvoiceUserId"/>
		<result column="receive_invoice_time" jdbcType="TIMESTAMP" property="receiveInvoiceTime"/>
		<result column="receive_invoice_status" jdbcType="TINYINT" property="receiveInvoiceStatus"/>

		<result column="mail_tick_user_id" jdbcType="BIGINT" property="mailTickUserId"/>
		<result column="mail_tick_time" jdbcType="TIMESTAMP" property="mailTickTime"/>
		<result column="mail_tick_status" jdbcType="TINYINT" property="mailTickStatus"/>

		<result column="back_account_user_id" jdbcType="BIGINT" property="backAccountUserId"/>
		<result column="back_account_time" jdbcType="TIMESTAMP" property="backAccountTime"/>
		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus"/>
		<result column="back_account_mode_record" jdbcType="TINYINT" property="backAccountModeRecord"/>

		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>

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

		<result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber"/>
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle"/>
		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType"/>
		<result column="invoice_mode" jdbcType="VARCHAR" property="invoiceMode"/>
		<result column="tax_rate" jdbcType="VARCHAR" property="taxRate"/>

		<result column="free_amount" jdbcType="DECIMAL" property="freeAmount"/>
		<result column="real_amount" jdbcType="DECIMAL" property="realAmount"/>
		<result column="no_check_count" jdbcType="DECIMAL" property="noCheckCount"/>

		<result column="express_company_id" jdbcType="BIGINT" property="expressCompanyId"/>
		<result column="express_no" jdbcType="VARCHAR" property="expressNo"/>
		<!--		t9.company_name as express_company_name-->
		<result column="express_company_name" jdbcType="VARCHAR" property="expressCompanyName"/>


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


		<!--		uoc.order_sn,-->
		<result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>
		<result column="refund_order_sn" jdbcType="VARCHAR" property="refundOrderSn"/>
		<result column="direction" jdbcType="TINYINT" property="direction" />

		<!--		uoc.credit_type,-->
		<result column="credit_type" jdbcType="INTEGER" property="creditType"/>
		<!--		uoc.credit_name,-->
		<result column="credit_name" jdbcType="VARCHAR" property="creditName"/>
		<!--		uoc.batchs,-->
		<result column="batchs" jdbcType="VARCHAR" property="batchs"/>
		<!--		uoc.balance,-->
		<result column="balance" jdbcType="DECIMAL" property="balance"/>
		<!--		uoc.enterprise_id,-->
		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<!--		uoc.member_id,-->
		<result column="member_id" jdbcType="BIGINT" property="memberId"/>


		<!--		uoc.pay_status,-->
		<result column="pay_status" jdbcType="INTEGER" property="payStatus"/>
		<!--		uoc.pay_type,-->
		<result column="pay_type" jdbcType="INTEGER" property="payType"/>
		<!--		uoc.accounting_state_customer,-->
		<result column="accounting_state_customer" jdbcType="TINYINT" property="accountingStateCustomer"/>
		<!--		uoc.accounting_state_supplier,-->
		<result column="accounting_state_supplier" jdbcType="TINYINT" property="accountingStateSupplier"/>
		<!--		uoc.invoice_make_status,-->
		<result column="invoice_make_status" jdbcType="TINYINT" property="invoiceMakeStatus"/>
		<!--		uoc.back_status,-->
		<result column="back_status" jdbcType="TINYINT" property="backStatus"/>


		<result column="goods_name" jdbcType="VARCHAR" property="goodsName"/>
		<result column="quantity" jdbcType="INTEGER" property="quantity"/>
		<result column="unit" jdbcType="VARCHAR" property="unit"/>


		<result column="price" jdbcType="DECIMAL" property="price"/>
		<result column="cost_price" jdbcType="DECIMAL" property="costPrice"/>


		<!-- 弃用 改为 三级分类都记录 20200904 by linjinyu-->
		<!-- uog.cate_name,-->
		<!-- <result column="cate_name" jdbcType="VARCHAR" property="cateName"/>-->
		<!-- ****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ****************************************** -->
		<result column="cate_id_lv1" property="cateIdLv1" jdbcType="BIGINT"/>
		<result column="cate_name_lv1" property="cateNameLv1" jdbcType="VARCHAR"/>

		<result column="cate_id_lv2" property="cateIdLv2" jdbcType="BIGINT"/>
		<result column="cate_name_lv2" property="cateNameLv2" jdbcType="VARCHAR"/>

		<result column="cate_id_lv3" property="cateIdLv3" jdbcType="BIGINT"/>
		<result column="cate_name_lv3" property="cateNameLv3" jdbcType="VARCHAR"/>

		<result column="self_option" jdbcType="VARCHAR" property="selfOption"/>
		<!-- ****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ****************************************** -->

		<!--		uog.fiscal_code as unified_order_goods_fiscal_code,-->
		<result column="unified_order_goods_fiscal_code" jdbcType="VARCHAR" property="unifiedOrderGoodsFiscalCode"/>
		<!--		uog.tax_code as unified_order_goods_tax_code,-->
		<result column="unified_order_goods_tax_code" jdbcType="VARCHAR" property="unifiedOrderGoodsTaxCode"/>
		<!--		uog.tax_rate as unified_order_goods_tax_rate,-->
		<result column="unified_order_goods_tax_rate" jdbcType="DECIMAL" property="unifiedOrderGoodsTaxRate"/>

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


		<!--		uo.sale_id,-->
		<result column="sale_id" jdbcType="BIGINT" property="saleId"/>
		<!--		uo.sale_name,-->
		<result column="sale_name" jdbcType="VARCHAR" property="saleName"/>

		<result column="order_create_time" property="orderCreateTime" jdbcType="TIMESTAMP"/>
		<result column="order_update_time" property="orderUpdateTime" jdbcType="TIMESTAMP"/>
		<result column="order_pay_time"     property="orderPayTime"    jdbcType="TIMESTAMP"/>
		<result column="order_send_time"    property="orderSendTime"   jdbcType="TIMESTAMP"/>
		<result column="order_finish_time"  property="orderFinishTime" jdbcType="TIMESTAMP"/>

		<!--		ascu.accounting_order_sn,-->
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>

		<!--		&lt;!&ndash;		t2.fullname AS apply_user_name,&ndash;&gt;-->
		<!--		<result column="apply_user_name" jdbcType="VARCHAR" property="applyUserName"/>-->
		<!--		&lt;!&ndash;		t3.fullname AS audit_user_name,&ndash;&gt;-->
		<!--		<result column="audit_user_name" jdbcType="VARCHAR" property="auditUserName"/>-->
		<!--		&lt;!&ndash;		t4.fullname AS make_user_name,&ndash;&gt;-->
		<!--		<result column="make_user_name" jdbcType="VARCHAR" property="makeUserName"/>-->


	</resultMap>

	<select id="exportExcelInvoiceApplyFocusScore"
			parameterType="cn.superdesk.uniorder.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteria"
			resultMap="exportExcelInvoiceApplyFocusScoreResultMap">

		SELECT

			invaxg.unified_order_credit_id,
			uoc.out_snapshot_order_credit_id,
			invaxg.accounting_state as credit_accounting_state,

			invaxg.unified_order_goods_id as unified_order_goods_id,
			uog.out_snapshot_order_goods_id,
			uog.goods_id,

			invaxg.unified_order_id as unified_order_id,


			invaxg.accounting_order_id as accounting_order_id,
			inva.accounting_order_type,

			invaxg.invoice_apply_id,
			inva.invoice_apply_sn,

			inva.invoice_progressing_user_id,
			inva.invoice_progressing_time,
			inva.invoice_status,

			inva.apply_user_id,
			inva.apply_time,

			inva.audit_user_id,
			inva.audit_status,
			inva.audit_time,

			inva.make_invoice_user_id,
			inva.make_invoice_time,
			inva.make_invoice_status,

			inva.receive_invoice_user_id,
			inva.receive_invoice_time,
			inva.receive_invoice_status,

			inva.mail_tick_user_id,
			inva.mail_tick_time,
			inva.mail_tick_status,

			inva.back_account_user_id,
			inva.back_account_time,
			inva.back_account_status,
			inva.back_account_mode_record,

			inva.update_time,
			inva.update_user_id,

			inva.sys_enterprise_ext_invoice_id,
			inva.sys_enterprise_ext_invoice_snapshot,

			inva.invoice_number,

			inva.invoice_title,
			inva.invoice_type,
			inva.invoice_mode,

			inva.tax_rate,

			inva.free_amount,
			inva.real_amount,
			inva.no_check_count,

			inva.express_company_id,
			inva.express_no,
			inva.mail_tick_time,
			inva.back_account_status,
			inva.remark,


			uo.order_sn AS order_sn,
			uo.refund_order_sn AS refund_order_sn,

			uoc.direction,


			uoc.credit_type,
			uoc.credit_name,
			uoc.batchs,
			uoc.balance,
			uoc.enterprise_id,
			uoc.member_id,
			uoc.pay_status,
			uoc.pay_type,
			uoc.accounting_state_customer,
			uoc.accounting_state_supplier,
			uoc.invoice_make_status,
			uoc.back_status,


			uog.goods_name,
			uog.quantity,
			uog.unit,
			uog.self_option,
			uog.price,
			uog.cost_price,

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

			uog.fiscal_code as unified_order_goods_fiscal_code,
			uog.tax_code as unified_order_goods_tax_code,
			uog.tax_rate as unified_order_goods_tax_rate,

			uog.merch_tax_id,
			uog.merch_tax_code,
			uog.merch_tax_rate,
			uog.operation_tax_id,
			uog.operation_tax_code,
			uog.operation_tax_rate,

			uo.sale_id,
			uo.sale_name,

			uo.create_time as order_create_time,
			uo.update_time as order_update_time,
			uo.pay_time as order_pay_time,
			uo.send_time as order_send_time,
			uo.finish_time as order_finish_time,

			ascu.accounting_order_sn,

			<!--
			t2.fullname AS apply_user_name,
			t3.fullname AS audit_user_name,
			t4.fullname AS make_user_name,
			-->

			t9.company_name as express_company_name
		FROM
			invoice_apply inva
			LEFT JOIN invoice_apply_x_goods invaxg ON inva.id = invaxg.invoice_apply_id

			LEFT JOIN accounting_score_customer ascu ON invaxg.accounting_order_id = ascu.id

			LEFT JOIN unified_order uo on invaxg.unified_order_id = uo.id
			LEFT JOIN unified_order_goods uog on invaxg.unified_order_goods_id = uog.id
			LEFT JOIN unified_order_credit uoc on invaxg.unified_order_credit_id = uoc.id

			LEFT JOIN express_company t9 ON inva.express_company_id = t9.id
			LEFT JOIN sys_enterprise t10 ON ascu.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 su1 ON uo.purchase_user_id = su1.id
			-->
		<where>
			and 1 = 1
			<include refid="findViewWhereIf"></include>

			<choose>
				<when test="orderByClause != null and orderByClause != '' ">
					order by ${orderByClause}
				</when>
				<otherwise>
					order by inva.apply_time DESC
				</otherwise>
			</choose>
		</where>

	</select>


</mapper>
