<?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.supplier.InvoiceApplySupplierMapperEx">

	<resultMap id="HeadResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierHeadVo">

		<result column="total" property="total" jdbcType="INTEGER"/>

		<result column="accounting_order_quantity" jdbcType="INTEGER" property="accountingOrderQuantity" />
		<result column="order_quantity" jdbcType="INTEGER" property="orderQuantity" />
		<result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
		<result column="goods_quantity" jdbcType="INTEGER" property="goodsQuantity" />
		<result column="goods_item_quantity" jdbcType="INTEGER" property="goodsItemQuantity" />
		<result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" />
		<result column="goods_cost_amount" jdbcType="DECIMAL" property="goodsCostAmount" />
		<result column="invoice_apply_amount" jdbcType="DECIMAL" property="invoiceApplyAmount" />
	</resultMap>


	<!-- 20200713 已较正 -->
	<resultMap id="InvoiceApplySupplierSelectGoodsResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierSelectGoodsVo">

		<result column="accounting_order_sn" property="accountingOrderSn" jdbcType="VARCHAR"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
		<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"/>


<!--		弃用 与彩娇对过 这个是不需要 mark by 20201103 linjinyu-->
<!--		<result column="goods_params" property="goodsParams" jdbcType="VARCHAR"/>-->
		<result column="unit" property="unit" jdbcType="VARCHAR"/>

	</resultMap>
	<!-- 用于 供应商发票申请 基于 对账单Sn 来返回 对账单 商品数据 弃用 mark by 20201103 linjinyu-->
<!--	<select id="getStatementOfAccountDimOrder2GoodsList" resultMap="InvoiceApplySupplierSelectGoodsResultMap">-->
<!--		SELECT-->
<!--			aus.accounting_order_sn,-->
<!--			auso.order_sn,-->

<!--			uog.id as goods_id,-->
<!--			uog.goods_name,-->
<!--			uog.goods_params,-->
<!--			uog.cost_price,-->
<!--			uog.quantity,-->
<!--			uog.unit-->


<!--		from-->
<!--			accounting_unified_supplier aus-->
<!--			LEFT JOIN accounting_unified_supplier_order auso on auso.accounting_order_sn=aus.accounting_order_sn-->
<!--			LEFT JOIN unified_order uo on auso.order_sn =uo.order_sn-->
<!--			LEFT JOIN unified_order_goods uog on auso.order_sn=uog.order_sn-->
<!--		<where>-->

<!--			已取消(-1, "已取消"),-->
<!--			未确认(0, "未确认"),-->
<!--			已确认(1, "已确认"),-->
<!--			已拒绝(2, "已拒绝"),-->
<!--			待提交(3, "待提交"),-->
<!--			and aus.accounting_status = 1-->

<!--			已开票(1, "已开票"),-->
<!--			未开票(2, "未开票"),-->
<!--			部分开票(3, "部分开票");-->
<!--			and aus.invoice_status != 1-->

<!--			and uog.id not in (-->
<!--				select-->
<!--					goods_id-->
<!--				from-->
<!--					invoice_apply_supplier_x_goods-->
<!--				)-->

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

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

<!--			<if test="supplierId != null and supplierId != '' ">-->
<!--				and uo.sale_id = #{supplierId}-->
<!--			</if>-->
<!--			<if test="supplierName != null and supplierName != '' ">-->
<!--				and uo.sale_name like CONCAT('%',#{supplierName},'%')-->
<!--			</if>-->

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

	<select id="getDataPageHead" resultMap="HeadResultMap">
		select
			count(id) as total,
			sum(accounting_order_quantity) as accounting_order_quantity,
			sum(order_quantity) as order_quantity,
			sum(order_amount) as order_amount,
			sum(goods_quantity) as goods_quantity,
			sum(goods_item_quantity) as goods_item_quantity,
			sum(goods_amount) as goods_amount,
			sum(goods_cost_amount) as goods_cost_amount,
			sum(invoice_apply_amount) as invoice_apply_amount
		from
			invoice_apply_supplier
		<where>
			<include refid="InvoiceApplySupplierWhereIf"></include>
		</where>
	</select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.invoice.supplier.InvoiceApplySupplierMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.invoice.supplier.InvoiceApplySupplierMapper.Base_Column_List"/>
		from
			invoice_apply_supplier
		<where>
			<include refid="InvoiceApplySupplierWhereIf"></include>
		</where>
		order by apply_time desc,invoice_apply_sn asc
	</select>


	<!-- Base_Column_List BaseResultMap -->
	<!-- Blob_Column_List ResultMapWithBLOBs -->

	<select id="queryInvoiceApplySupplierPageVoList" resultMap="cn.superdesk.uniorder.dao.mapper.invoice.supplier.InvoiceApplySupplierMapper.ResultMapWithBLOBs">
		select

			<include refid="cn.superdesk.uniorder.dao.mapper.invoice.supplier.InvoiceApplySupplierMapper.Base_Column_List" />
			,
			<include refid="cn.superdesk.uniorder.dao.mapper.invoice.supplier.InvoiceApplySupplierMapper.Blob_Column_List" />

		from
			invoice_apply_supplier
		<where>
			<include refid="InvoiceApplySupplierWhereIf"></include>
		</where>
		order by apply_time desc,invoice_apply_sn asc
	</select>

	<sql id="InvoiceApplySupplierWhereIf">

		<!-- 发票种类 -->
		<if test="invoiceTypeId != null and invoiceTypeId != '' ">
			and invoice_type = #{invoiceTypeId}
		</if>

		<!-- 所属对账单 -->
		<if test="accountingOrderId != null and accountingOrderId != '' ">
			and accounting_order_id = #{accountingOrderId}
		</if>
		<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and accounting_order_sn = #{accountingOrderSn}
		</if>

		<!-- 开票申请 -->
		<if test="invoiceApplyId != null and invoiceApplyId != '' ">
			and id = #{invoiceApplyId}
		</if>
		<if test="invoiceApplySn != null and invoiceApplySn != '' ">
			and invoice_apply_sn = #{invoiceApplySn}
		</if>
		<!--
		<if test="invoiceApplySn != null and invoiceApplySn != '' ">
			and invoice_apply_sn like CONCAT('%',#{invoiceApplySn},'%')
		</if>
		-->

		<if test="invoiceNumber != null and invoiceNumber != '' ">
			and invoice_number = #{invoiceNumber}
		</if>


		<if test="applyUserId != null and applyUserId != '' ">
			and apply_user_id = #{applyUserId}
		</if>
		<if test="applyUserName != null and applyUserName != '' ">
			and apply_user_name = #{applyUserName}
		</if>
		<!--
		<if test="applyUserName != null and applyUserName != '' ">
			and apply_user_name like CONCAT('%',#{applyUserName},'%')
		</if>
		-->

		<!-- 发票申请审核部分 -->
		<if test="auditStatus != null and auditStatus != '' ">
			and audit_status = #{auditStatus}
		</if>
		<if test="auditStatusSelected != null and auditStatusSelected != '' and auditStatusSelected.size > 0 ">
			and audit_status in
			<foreach collection="auditStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="auditUserId != null and auditUserId != '' ">
			and audit_user_id = #{auditUserId}
		</if>
		<if test="auditUserName != null and auditUserName != '' ">
			and audit_user_name = #{auditUserName}
		</if>

		<!-- 开票进展部分 -->
		<if test="invoiceStatus != null and invoiceStatus != '' ">
			and invoice_status = #{invoiceStatus}
		</if>
		<if test="invoiceStatusSelected != null and invoiceStatusSelected != '' and invoiceStatusSelected.size > 0 ">
			and invoice_status in
			<foreach collection="invoiceStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="invoiceProgressingUserId != null and invoiceProgressingUserId != '' ">
			and audit_user_id = #{invoiceProgressingUserId}
		</if>
		<!--
		<if test="invoiceProgressingUserName != null and invoiceProgressingUserName != '' ">
			and invoice_progressing_user__name = #{invoiceProgressingUserName}
		</if>
		-->

		<!-- 开票登记部分 --><!-- and makeInvoiceStatus != '' -->
		<if test="makeInvoiceStatus != null ">
			and make_invoice_status = #{makeInvoiceStatus}
		</if>
		<if test="makeInvoiceStatusSelected != null and makeInvoiceStatusSelected != '' and makeInvoiceStatusSelected.size > 0 ">
			and make_invoice_status in
			<foreach collection="makeInvoiceStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="makeInvoiceUserId != null and makeInvoiceUserId != '' ">
			and make_invoice_user_id = #{auditUserId}
		</if>
		<if test="makeInvoiceUserName != null and makeInvoiceUserName != '' ">
			and make_invoice_user_name = #{makeInvoiceUserName}
		</if>

		<!-- 寄票登记部分 --><!-- and mailTickStatus != ''  -->
		<if test="mailTickStatus != null ">
			and mail_tick_status = #{mailTickStatus}
		</if>
		<if test="mailTickStatusSelected != null and mailTickStatusSelected != '' and mailTickStatusSelected.size > 0 ">
			and mail_tick_status in
			<foreach collection="mailTickStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="mailTickUserId != null and mailTickUserId != '' ">
			and mail_tick_user_id = #{mailTickUserId}
		</if>
		<!--
		<if test="mailTickUserName != null and mailTickUserName != '' ">
			and mail_tick_user_name = #{mailTickUserName}
		</if>
		-->

		<!-- 收票登记部分 --> <!-- and receiveInvoiceStatus != '' -->
		<if test="receiveInvoiceStatus != null ">
			and receive_invoice_status = #{receiveInvoiceStatus}
		</if>
		<if test="receiveInvoiceStatusSelected != null and receiveInvoiceStatusSelected != '' and receiveInvoiceStatusSelected.size > 0 ">
			and receive_invoice_status in
			<foreach collection="receiveInvoiceStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="receiveInvoiceUserId != null and receiveInvoiceUserId != '' ">
			and receive_invoice_user_id = #{receiveInvoiceUserId}
		</if>
		<!--
		<if test="receiveInvoiceUserName != null and receiveInvoiceUserName != '' ">
			and receive_invoice_user_name = #{receiveInvoiceUserName}
		</if>
		-->

		<!-- 付款登记部分 and payoffAccountStatus != ''  -->
		<if test="payoffAccountStatus != null ">
			and payoff_account_status = #{payoffAccountStatus}
		</if>
		<if test="payoffAccountStatusSelected != null and payoffAccountStatusSelected != '' and payoffAccountStatusSelected.size > 0 ">
			and payoff_account_status in
			<foreach collection="payoffAccountStatusSelected" index="index" item="statusValue" open="(" close=")" separator=",">
				#{statusValue}
			</foreach>
		</if>
		<if test="payoffAccountUserId != null and payoffAccountUserId != '' ">
			and payoff_account_user_id = #{payoffAccountUserId}
		</if>
		<!--
		<if test="payoffAccountUserName != null and payoffAccountUserName != '' ">
			and payoff_account_user_name = #{payoffAccountUserName}
		</if>
		-->

		<!-- 供应商信息部分 -->
		<if test="outSaleId != null and outSaleId != '' ">
			and sale_id = #{outSaleId}
		</if>
		<if test="outSaleName != null and outSaleName != '' ">
			and sale_name like CONCAT('%',#{outSaleName},'%')
		</if>

		<!-- 开票申请时间 -->
		<if test="applyStartTime != null and applyStartTime != ''">
			and DATE_FORMAT(apply_time,'%Y-%m-%d') &gt;= #{applyStartTime,jdbcType=VARCHAR}
		</if>
		<if test="applyEndTime != null and applyEndTime != ''">
			and DATE_FORMAT(apply_time,'%Y-%m-%d') &lt;= #{applyEndTime,jdbcType=VARCHAR}
		</if>

		<!-- 开票时间 -->
		<if test="makeInvoiceStartTime != null and makeInvoiceStartTime != ''">
			and DATE_FORMAT(make_invoice_time,'%Y-%m-%d') &gt;= #{makeInvoiceStartTime,jdbcType=VARCHAR}
		</if>
		<if test="makeInvoiceEndTime != null and makeInvoiceEndTime != ''">
			and DATE_FORMAT(make_invoice_time,'%Y-%m-%d') &lt;= #{makeInvoiceEndTime,jdbcType=VARCHAR}
		</if>

		<!-- 收票时间 -->
		<if test="receiveInvoiceStartTime != null and receiveInvoiceStartTime != ''">
			and DATE_FORMAT(receive_invoice_time,'%Y-%m-%d') &gt;= #{receiveInvoiceStartTime,jdbcType=VARCHAR}
		</if>
		<if test="receiveInvoiceEndTime != null and receiveInvoiceEndTime != ''">
			and DATE_FORMAT(receive_invoice_time,'%Y-%m-%d') &lt;= #{receiveInvoiceEndTime,jdbcType=VARCHAR}
		</if>

		<!-- 付款时间 -->
		<if test="payoffAccountStartTime != null and payoffAccountStartTime != ''">
			and DATE_FORMAT(payoff_account_time,'%Y-%m-%d') &gt;= #{payoffAccountStartTime,jdbcType=VARCHAR}
		</if>
		<if test="payoffAccountEndTime != null and makeInvoiceEndTime != ''">
			and DATE_FORMAT(payoff_account_time,'%Y-%m-%d') &lt;= #{payoffAccountEndTime,jdbcType=VARCHAR}
		</if>

		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 and outVirtIds.size > 0-->
			and invoice_apply_sn in (
			SELECT
				invoice_apply_sn
			from
				invoice_apply_supplier_x_goods
			where
				order_sn in
				(
				SELECT
					order_sn
				from
					unified_order
				where
					purchase_virt_id in
					<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
						#{pid}
					</foreach>
					<if test="outServices != null and outServices != ''"><!--  and outServices.size > 0 -->
						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>
		<if test="sourceFromsSelected != null and sourceFromsSelected != '' and sourceFromsSelected.size > 0">
			and source_from in
			<foreach collection="sourceFromsSelected" index="index" item="source_from" open="(" close=")" separator=",">
				#{source_from}
			</foreach>
		</if>

		<if test="serviceNosSelected != null and serviceNosSelected != '' and serviceNosSelected.size > 0">
			and service_no in
			<foreach collection="serviceNosSelected" index="index" item="service_no" open="(" close=")" separator=",">
				#{service_no}
			</foreach>
		</if>
	</sql>
</mapper>
