<?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 id="AcountResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedSupplierDzVo">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="accounting_order_sn" property="supDzSn" jdbcType="VARCHAR"/>
		<result column="dz_status" property="dzStatus" jdbcType="TINYINT"/>
		<result column="dz_invstatus" property="dzInvstatus" jdbcType="TINYINT"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="cost_amount" property="costAmount" jdbcType="DECIMAL"/>
		<result column="order_quantity" property="orderQuantity" jdbcType="INTEGER"/>
	</resultMap>

	<resultMap id="HSResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedSuplierDzHE">
		<result column="total" property="total" jdbcType="INTEGER"/>
		<result column="order_total" property="orderTotal" jdbcType="INTEGER"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="cost_amount" property="costAmount" jdbcType="DECIMAL"/>
	</resultMap>

	<select id="getAcountDataHS" resultMap="HSResultMap">
		SELECT
			count(a.id) as total,
			sum(b.order_quantity) as order_total,
			sum(b.amount) as amount,
			sum(b.cost_amount) as cost_amount
		FROM
			accounting_unified_supplier a 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
				) b
			on a.accounting_order_sn = b.accounting_order_sn
		<where>
		<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and a.accounting_order_sn like CONCAT('%',#{accountingOrderSn},'%')
		</if>

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


		<if test="sn != null and sn != '' ">
			and a.accounting_order_sn in (select accounting_order_sn from accounting_unified_supplier_order where sn like
			CONCAT('%',#{cususername},'%'))
		</if>


		<if test="dzStatus != null and dzStatus != ''">
			and a.dz_status = #{dzStatus,jdbcType=TINYINT}
		</if>
		<if test="dzInvstatus != null and dzInvstatus != ''">
			and a.dz_invstatus = #{dzInvstatus,jdbcType=TINYINT}
		</if>
		<if test="startime != null and startime != ''">
			and DATE_FORMAT(a.create_time,'%Y-%m-%d') &gt;= #{startime,jdbcType=VARCHAR}
		</if>
		<if test="endtime != null and endtime != ''">
			and DATE_FORMAT(a.create_time,'%Y-%m-%d') &lt;= #{endtime,jdbcType=VARCHAR}
		</if>


		<if test="outVirtIds != null and outVirtIds != ''">
			and a.accounting_order_sn in (select accounting_order_sn from accounting_unified_supplier_order where sn in (
			select sn from unified_order where project_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>
		</where>
	</select>

	<!-- 供应商结算 - 对账单 -->
	<select id="getAcountData" resultMap="AcountResultMap">
		SELECT
			a.id,
			a.accounting_order_sn,
			a.dz_status,
			a.dz_invstatus,
			a.sale_name,
			a.create_time,
			b.amount,
			b.cost_amount,
			b.order_quantity
		FROM
			accounting_unified_supplier a
			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
					) b
		on a.accounting_order_sn = b.accounting_order_sn
		<where>
		<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and a.accounting_order_sn like CONCAT('%',#{accountingOrderSn},'%')
		</if>

		<if test="orderSn != null and orderSn != '' ">
			and a.accounting_order_sn in (select accounting_order_sn from accounting_unified_supplier_order where order_sn like CONCAT('%',#{orderSn},'%'))
		</if>


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


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


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

		<if test="outVirtIds != null and outVirtIds != ''">
			and a.accounting_order_sn in (select accounting_order_sn from accounting_unified_supplier_order where sn in (
			select sn from unified_order where project_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>

		</where>

		order by a.id desc
	</select>


</mapper>