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

	<!-- cn.superdesk.uniorder.dao.entity.invoice.supplier.InvoiceApplySupplierXGoods -->
	<insert id="insertBatch" parameterType="java.util.List">
		<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into invoice_apply_supplier_x_goods (
		accounting_order_id,
		accounting_order_sn,
		accounting_order_type,
		dim_create_by,

		invoice_apply_id,
		invoice_apply_sn,

		unified_order_id,
		order_sn,

		unified_order_goods_id,
		goods_id,

		back_status,
		bank_id,
		back_time,

		deleted
		)
		values
		<foreach collection="list" item="item" index="index" separator=",">
		(
		#{item.accountingOrderId,jdbcType=BIGINT},
		#{item.accountingOrderSn,jdbcType=VARCHAR},
		#{item.accountingOrderType,jdbcType=TINYINT},
		#{item.dimCreateBy,jdbcType=TINYINT},

		#{item.invoiceApplyId,jdbcType=BIGINT},
		#{item.invoiceApplySn,jdbcType=VARCHAR},

		#{item.unifiedOrderId,jdbcType=BIGINT},
		#{item.orderSn,jdbcType=VARCHAR},

		#{item.unifiedOrderGoodsId,jdbcType=BIGINT},
		#{item.goodsId,jdbcType=BIGINT},

		#{item.backStatus,jdbcType=TINYINT},
		#{item.bankId,jdbcType=BIGINT},
		#{item.backTime,jdbcType=TIMESTAMP},

		#{item.deleted,jdbcType=TINYINT}
		)
		</foreach>
	</insert>

	<resultMap id="InvoiceApplySupplierDetailInnerGoodsListResultMap"
			   type="cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierDetailDimGoodsItemVo">

		<result column="invoice_apply_supplier_x_goods_id" property="invoiceApplySupplierXGoodsId" jdbcType="BIGINT" />

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

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

<!--		invas.invoice_type,-->
<!--		invas.invoice_title,-->
<!--		invas.sys_platform_ext_invoice_id,-->
<!--		invas.sys_platform_ext_invoice_snapshot,-->

		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
		<result column="invoice_title" jdbcType="VARCHAR" property="invoiceTitle" />
		<result column="sys_platform_ext_invoice_id" jdbcType="BIGINT" property="sysPlatformExtInvoiceId" />
		<result column="sys_platform_ext_invoice_snapshot" jdbcType="LONGVARCHAR" property="sysPlatformExtInvoiceSnapshot" />

		<result column="sale_id" property="saleId" jdbcType="BIGINT"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

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

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

		<result column="order_state" property="orderState" jdbcType="TINYINT"/>
		<result column="order_state_name" property="orderStateName" jdbcType="VARCHAR"/>

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>

		<result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
		<result column="purchase_user_name" property="purchaseUserName" jdbcType="VARCHAR"/>

		<result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
		<result column="service_no" property="serviceNo" jdbcType="VARCHAR"/>

		<result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
		<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="direction" jdbcType="TINYINT" property="direction" />

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

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

		<result column="goods_params" property="goodsParams" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="getInvoiceApplySupplierDetailInnerGoodsList"
			resultMap="InvoiceApplySupplierDetailInnerGoodsListResultMap">
		SELECT

			invasxg.id as invoice_apply_supplier_x_goods_id,

			invas.id as invoice_apply_id,
			invas.invoice_apply_sn,

			ausu.id as accounting_order_id,
			ausu.accounting_order_sn,


			invas.invoice_type,
			invas.invoice_title,
			invas.sys_platform_ext_invoice_id,
			invas.sys_platform_ext_invoice_snapshot,

			invas.sale_id,
			invas.sale_name,

		    <!-- 弃用 改用定义 mark by linjinyu at 20220624 -->
			uo.service_name,
			uo.service_no,
			uo.order_sn,

			uo.pay_type,
			uo.order_state,
			uo.order_state_name,


			uo.purchase_virt_name,
			uo.purchase_user_name,


			uo.create_time,
			uo.update_time,

			uog.id as unified_order_goods_id,
			uog.goods_id,
			uog.out_snapshot_order_goods_id,
			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.direction,

			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,

			uog.goods_params

		from
			invoice_apply_supplier_x_goods invasxg
			left join invoice_apply_supplier invas on invasxg.invoice_apply_sn = invas.invoice_apply_sn
			left join accounting_unified_supplier ausu on invasxg.accounting_order_sn = ausu.accounting_order_sn
			left join unified_order uo on invasxg.order_sn = uo.order_sn
			left join unified_order_goods uog on invasxg.unified_order_goods_id = uog.id
		<where>
		<if test="invoiceApplyId != null and invoiceApplyId != '' ">
			and invas.id = #{invoiceApplyId}
		</if>
		<if test="invoiceApplyIdSelect != null and invoiceApplyIdSelect != '' and invoiceApplyIdSelect.size > 0">
			and invas.id in
			<foreach collection="invoiceApplyIdSelect" index="index" item="invoiceApplyId" open="(" close=")" separator=",">
				#{invoiceApplyId}
			</foreach>
		</if>

		<if test="invoiceApplySn != null and invoiceApplySn != '' ">
			and invasxg.invoice_apply_sn = #{invoiceApplySn}
		</if>
		<if test="invoiceApplySnSelect != null and invoiceApplySnSelect != '' and invoiceApplySnSelect.size > 0">
			and invasxg.invoice_apply_sn in
			<foreach collection="invoiceApplySnSelect" index="index" item="invoiceApplySn" open="(" close=")" separator=",">
				#{invoiceApplySn,jdbcType=VARCHAR}
			</foreach>
		</if>
		</where>
	</select>

	<delete id="deleteByInvoiceApplySn" parameterType="java.lang.String">
		delete from
			invoice_apply_supplier_x_goods
		where
			invoice_apply_sn = #{invoiceApplySn,jdbcType=VARCHAR}
	</delete>

	<delete id="deleteByInvoiceApplySns">
		delete from
			invoice_apply_supplier_x_goods
		where
			invoice_apply_sn in
			<foreach collection="invoiceApplySns" index="index" item="invoiceApplySn" open="(" close=")" separator=",">
				#{invoiceApplySn}
			</foreach>
	</delete>

	<resultMap id="InvoiceApplyXUnifiedOrderGoodsEdResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierXUnifiedOrderGoodsEdVo">

<!--		usii.id as invoice_apply_x_unified_order_goods_id ,-->
		<result column="invoice_apply_x_unified_order_goods_id" property="invoiceApplyXUnifiedOrderGoodsId" jdbcType="BIGINT" />

<!--		usi.id as invoice_apply_id ,-->
		<result column="invoice_apply_id"  property="invoiceApplyId" jdbcType="BIGINT"/>

<!--		usii.invoice_apply_sn as invoice_apply_sn,-->
		<result column="invoice_apply_sn" property="invoiceApplySn" jdbcType="VARCHAR"/>

<!--		0 as accounting_order_id ,-->
		<result column="accounting_order_id" property="accountingOrderId" jdbcType="BIGINT" />

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

<!--		usii.order_sn ,-->
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

<!--		usii.goods_id as unified_order_goods_id-->
		<result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT" />

	</resultMap>

	<select id="getInvoiceApplyXUnifiedOrderGoodsEd" resultMap="InvoiceApplyXUnifiedOrderGoodsEdResultMap">

		SELECT
			invasuxg.id as invoice_apply_x_unified_order_goods ,
			invasu.id as invoice_apply_id ,
			invasuxg.invoice_apply_sn as invoice_apply_sn,
			invasuxg.accounting_order_id ,
			invasuxg.accounting_order_sn,
			invasuxg.order_sn ,
			invasuxg.goods_id as unified_order_goods_id
		FROM
			invoice_apply_supplier_x_goods invasuxg
			LEFT JOIN invoice_apply_supplier invasu on invasuxg.invoice_apply_sn = invasu.invoice_apply_sn
		<where>
			<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and invasuxg.accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
			</if>
			and invasu.audit_status in (1,2)
		</where>
		order by invasuxg.order_sn asc
	</select>

	<select id="checkInvoiceApplyStatusByAccountingOrderSn" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT
			CASE
			WHEN target_table.target_goods_count = 0 THEN 0
			WHEN source_table.source_goods_count = target_table.target_goods_count THEN 1
			WHEN source_table.source_goods_count - target_table.target_goods_count > 0 THEN 2
			ELSE 0
			END
		FROM
		(
			SELECT
				COUNT(1) AS source_goods_count
			FROM
				accounting_unified_supplier ausu
				LEFT JOIN accounting_unified_supplier_x_goods assuxg on ausu.id = assuxg.accounting_order_id
			WHERE
				ausu.accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		) source_table,
		(
			SELECT
				COUNT(1) as target_goods_count
			FROM
				invoice_apply_supplier_x_goods invg
				LEFT JOIN invoice_apply_supplier inva ON invg.invoice_apply_sn = inva.invoice_apply_sn
			WHERE
				<!-- 1-未审核;2-同意;3-拒绝 -->
				inva.audit_status in (1,2)
				AND invg.accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		) target_table
	</select>

</mapper>