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



	<select id="findInvoiceByOrderSn" resultMap="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapper.Base_Column_List"/>
		from
			invoice_apply
		<where>
			and make_invoice_status = 1
			and audit_status = 2
			and id in (
				SELECT
					invoice_apply_id
				from
					invoice_apply_x_goods
				where
					unified_order_id in (
						SELECT
							id
						from
							accounting_unified_customer_order
						<where>
							<if test="orderSn != null and orderSn != ''">
								and order_sn = #{orderSn}
							</if>
						</where>
					)
			)
		</where>
	</select>


	<!-- 比BaseResultMap 就多了个 orderSn  -->
	<resultMap id="findInvoiceByOrderSnListResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.OrderSnMapInvoiceApplyVo">

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

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

		<result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn" />

		<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="invoice_number" jdbcType="VARCHAR" property="invoiceNumber" />
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus" />
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle" />
		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
		<result column="tax_rate" jdbcType="VARCHAR" property="taxRate" />

		<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_check_count" jdbcType="DECIMAL" property="noCheckCount" />

		<result column="express_company_id" jdbcType="BIGINT" property="expressCompanyId" />
		<result column="express_no" jdbcType="VARCHAR" property="expressNo" />
		<result column="mail_tick_time" jdbcType="TIMESTAMP" property="mailTickTime" />
		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />
	</resultMap>
	<select id="findInvoiceByOrderSnList" resultMap="findInvoiceByOrderSnListResultMap">
		SELECT
		    DISTINCT aucuo.order_sn as order_sn,
		    inva.id,
		    inva.accounting_order_type,
		    <!--
		    ia.accounting_order_id,
		    ia.accounting_order_sn,
		    -->
		    inva.invoice_apply_sn,

		    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.invoice_number,
		    inva.invoice_status,
		    inva.invoice_title,
		    inva.invoice_type,
		    inva.tax_rate,

		    inva.invoice_apply_amount,
		    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
		FROM
		    invoice_apply inva
			LEFT JOIN invoice_apply_x_goods invo on inva.id = invo.invoice_apply_id
			LEFT JOIN accounting_unified_customer_order aucuo on invo.unified_order_id = aucuo.id
		<where>
		    and inva.make_invoice_status = 1
		    AND inva.audit_status = 2

			<if test="orderSnList != null">
				and aucuo.order_sn IN
				<foreach close=")" collection="orderSnList" index="index" item="item" open="(" separator=",">
					#{item}
				</foreach>
			</if>
		</where>
	</select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapper.Base_Column_List"/>
		from
			invoice_apply
		<where>
			<if test="invoiceTitle != null and invoiceTitle != ''">
				and invoice_title like CONCAT('%',#{invoiceTitle},'%')
			</if>
			<if test="noCheckCount != null and noCheckCount == 1">
				and no_check_count &gt; 0
			</if>
			<if test="makeInvoiceStatus != null and makeInvoiceStatus != ''">
				and make_invoice_status = #{makeInvoiceStatus}
			</if>
			<if test="invoiceNumber != null and invoiceNumber != ''">
				and invoice_number like CONCAT('%',#{invoiceNumber},'%')
			</if>
		</where>
		order by make_invoice_time

	</select>

</mapper>