<?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.accounting.unified.supplier.AccountingUnifiedSupplierMapperEx">

	<resultMap extends="cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierMapper.BaseResultMap"
			   id="UnifiedSupplierStatementOfAccountVoResultMap"
			   type="cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierListItemVo">

		<!-- 已经开字段了 -->
<!--		<result column="accounting_order_amount" property="accountingOrderAmount" jdbcType="DECIMAL"/>-->
<!--		<result column="accounting_order_cost_amount" property="accountingOrderCostAmount" jdbcType="DECIMAL"/>-->
<!--		<result column="accounting_order_amount_with_freight" property="accountingOrderAmountWithFreight" jdbcType="DECIMAL"/>-->
<!--		<result column="accounting_order_cost_amount_with_freight" property="accountingOrderCostAmountWithFreight" jdbcType="DECIMAL"/>-->

	</resultMap>

	<resultMap id="UnifiedSupplierStatementOfAccountHeadSumResultMap"
			   type="cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSuplierHeadVo">


		<result column="accounting_order_quantity" jdbcType="INTEGER" property="accountingOrderQuantity" />
		<result column="order_quantity" jdbcType="INTEGER" property="orderQuantity" />
		<result column="goods_quantity" jdbcType="INTEGER" property="goodsQuantity" />
		<result column="goods_item_quantity" jdbcType="INTEGER" property="goodsItemQuantity" />
		<result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
		<result column="refund_order_amount" jdbcType="DECIMAL" property="refundOrderAmount" />
		<result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" />
		<result column="refund_goods_amount" jdbcType="DECIMAL" property="refundGoodsAmount" />
		<result column="goods_transport_amount" jdbcType="DECIMAL" property="goodsTransportAmount" />
		<result column="refund_goods_transport_amount" jdbcType="DECIMAL" property="refundGoodsTransportAmount" />
		<result column="order_cost_amount" jdbcType="DECIMAL" property="orderCostAmount" />
		<result column="refund_order_cost_amount" jdbcType="DECIMAL" property="refundOrderCostAmount" />
		<result column="goods_cost_amount" jdbcType="DECIMAL" property="goodsCostAmount" />
		<result column="refund_goods_cost_amount" jdbcType="DECIMAL" property="refundGoodsCostAmount" />

		<result column="accounting_order_amount" jdbcType="DECIMAL" property="accountingOrderAmount" />
		<result column="accounting_order_amount_with_freight" jdbcType="DECIMAL" property="accountingOrderAmountWithFreight" />

		<result column="accounting_order_cost_amount" jdbcType="DECIMAL" property="accountingOrderCostAmount" />
		<result column="accounting_order_cost_amount_with_freight" jdbcType="DECIMAL" property="accountingOrderCostAmountWithFreight" />
	</resultMap>

	<!-- 供应商结算 - 对账单 head Dim 订单维度-->
<!--	<select id="getStatementOfAccountHeadSum" resultMap="UnifiedSupplierStatementOfAccountHeadSumResultMap">-->
<!--		SELECT-->
<!--			count(ausu.id) as total,-->
<!--			sum(auso.order_quantity) as order_total,-->
<!--			sum(auso.amount) as amount,-->
<!--			sum(auso.cost_amount) as cost_amount-->
<!--		FROM-->
<!--			accounting_unified_supplier ausu left join-->
<!--				(-->
<!--					SELECT-->
<!--						count(order_sn) order_quantity,-->
<!--						sum(amount) as amount,-->
<!--						sum(cost_amount) as cost_amount,-->
<!--						accounting_order_sn-->
<!--					from accounting_unified_supplier_order-->
<!--					GROUP BY accounting_order_sn-->
<!--				) auso-->
<!--			on ausu.accounting_order_sn = auso.accounting_order_sn-->
<!--		<where>-->
<!--			<include refid="AcountingDataWhereIf"></include>-->
<!--		</where>-->
<!--	</select>-->

	<select id="getStatementOfAccountHeadSum" resultMap="UnifiedSupplierStatementOfAccountHeadSumResultMap">
		SELECT
			count(ausu.id) as accounting_order_quantity,

			sum(ausu.order_quantity) as order_quantity,
			sum(ausu.goods_quantity) as goods_quantity,
			sum(ausu.goods_item_quantity) as goods_item_quantity,

			sum(ausu.order_amount) as order_amount,
			sum(ausu.goods_amount) as goods_amount,

			sum(ausu.refund_order_amount) as refund_order_amount,
			sum(ausu.refund_goods_amount) as refund_goods_amount,

			sum(ausu.order_cost_amount) as order_cost_amount,
			sum(ausu.refund_order_cost_amount) as refund_order_cost_amount,

			sum(ausu.goods_cost_amount) as goods_cost_amount,
			sum(ausu.refund_goods_cost_amount) as refund_goods_cost_amount,

			sum(ausu.goods_transport_amount) as goods_transport_amount,
			sum(ausu.refund_goods_transport_amount) as refund_goods_transport_amount,

			sum(ausu.accounting_order_amount) as accounting_order_amount,
			sum(ausu.accounting_order_amount_with_freight) as accounting_order_amount_with_freight,

			sum(ausu.accounting_order_cost_amount) as accounting_order_cost_amount,
			sum(ausu.accounting_order_cost_amount_with_freight) as accounting_order_cost_amount_with_freight
		FROM
			accounting_unified_supplier ausu

		<where>
			<include refid="AcountingDataWhereIf"></include>
		</where>
	</select>

	<!-- 供应商结算 - 对账单 List -->
	<select id="getStatementOfAccountList" resultMap="UnifiedSupplierStatementOfAccountVoResultMap">
		SELECT
			ausu.id,
			ausu.dim_create_by,
			ausu.accounting_order_sn,
			ausu.accounting_status,
			ausu.invoice_status,
			ausu.sale_id,
			ausu.sale_name,

			ausu.source_from,
			ausu.service_no,

			ausu.invoice_title,
			ausu.tax_code,
			ausu.invoice_type,
			ausu.address,
			ausu.is_send_email,
			ausu.send_email_time,
			ausu.start_time,
			ausu.end_time,
			ausu.back_account_status,

			ausu.create_user_id,
			ausu.create_time,
			ausu.update_user_id,
			ausu.update_time,
			ausu.remark,

			ausu.need_revise_mark,

			ausu.order_quantity,
			ausu.goods_quantity,
			ausu.goods_item_quantity,

			ausu.order_amount,
			ausu.goods_amount,

			ausu.refund_order_amount,
			ausu.refund_goods_amount,

			ausu.order_cost_amount,
			ausu.refund_order_cost_amount,

			ausu.goods_cost_amount,
			ausu.refund_goods_cost_amount,

			ausu.goods_transport_amount,
			ausu.refund_goods_transport_amount,

			accounting_order_amount,
			accounting_order_amount_with_freight,

			accounting_order_cost_amount,
			accounting_order_cost_amount_with_freight
		FROM
			accounting_unified_supplier ausu
			<!--
			left join
					(SELECT
						count(order_sn) order_quantity,
						sum(amount) as amount,
						sum(cost_amount) as cost_amount,
						sum(amount_with_freight) as amount_with_freight,
						sum(cost_amount_with_freight) as cost_amount_with_freight,
						accounting_order_sn
					from
						accounting_unified_supplier_order

					GROUP BY accounting_order_sn
					) auso on ausu.accounting_order_sn = auso.accounting_order_sn
			-->
		<where>
			<include refid="AcountingDataWhereIf"></include>
		</where>

		<!-- order by ausu.id desc -->
		order by ausu.create_time desc
	</select>

	<sql id="AcountingDataWhereIf">

		<if test="accountingOrderIds != null and accountingOrderIds != ''">
			and ausu.id in
			<foreach collection="accountingOrderIds" index="index" item="id" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</if>

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

		<!-- 20210220 添加订单号查哪些对账单包含 -->
		<if test="orderSn != null and orderSn != '' ">
			and ausu.accounting_order_sn in (select accounting_order_sn from accounting_unified_supplier_order where order_sn like CONCAT('%',#{orderSn},'%'))
			and ausu.accounting_order_sn in (SELECT accounting_order_sn FROM accounting_unified_supplier_x_goods WHERE order_sn like CONCAT('%',#{orderSn},'%'))
		</if>


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

		<!--
		<if test="accountingStatus != null and accountingStatus != ''">
			and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
		</if>
		-->

		<!-- 对账单状态:3-待提交;0-未确认;1-已确认;2-已拒绝;-1-已取消; -->
		<!-- and outLogin != '' -->
		<!-- and accountingStatus != '' -->
		<choose>
			<when test="outLogin != null and outLogin == 0">

				<choose>
					<when test="accountingStatus != null ">
						and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
					</when>
					<otherwise>
						and ausu.accounting_status not in (-1)
					</otherwise>
				</choose>

			</when>
			<when test="outLogin != null and outLogin == 2">

				<choose>
					<when test="accountingStatus != null ">
						and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
					</when>
					<otherwise>
						and ausu.accounting_status in (0,1,2)
					</otherwise>
				</choose>

			</when>
			<when test="outLogin == null ">
				<if test="accountingStatus != null ">
					and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
				</if>
			</when>
			<otherwise>
				and ausu.accounting_status not in (-1)
			</otherwise>
		</choose>



		<!-- and invoiceStatus != '' -->
		<if test="invoiceStatus != null ">
			and ausu.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
		</if>

		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(ausu.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(ausu.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>

		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
			and ausu.accounting_order_sn in (
				select
					accounting_order_sn
				from
					accounting_unified_supplier_order
				where
					order_sn in (
						select
							order_sn
						from
							unified_order
						where
							<!-- project_id -->
							purchase_virt_id in
							<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>

		<if test="needReviseMark != null">
			and ausu.need_revise_mark = #{needReviseMark}
		</if>

		<if test="sourceFromsSelected != null and sourceFromsSelected != '' and sourceFromsSelected.size > 0">
			and ausu.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 ausu.service_no in
			<foreach collection="serviceNosSelected" index="index" item="service_no" open="(" close=")" separator=",">
				#{service_no}
			</foreach>
		</if>
	</sql>


	<select id="getByAnnountingOrderSn" parameterType="java.lang.String"
			resultMap="cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierMapper.BaseResultMap">

		select
			<include refid="cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierMapper.Base_Column_List" />
		from
			accounting_unified_supplier
		WHERE
			accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		Limit 1
	</select>






</mapper>