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

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierOrderMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.AccountingUnifiedSupplierOrderMapper.Base_Column_List"/>
		from
			accounting_unified_supplier_order
		<where>
		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		</if>
		</where>
	</select>

	<insert id="createRelation">
		INSERT INTO accounting_unified_supplier_order (dim_create_by, accounting_order_sn, order_sn, amount, cost_amount, amount_with_freight, cost_amount_with_freight)
		SELECT
			#{dimCreateBy,jdbcType=TINYINT},#{accountingOrderSn,jdbcType=VARCHAR},order_sn,amount,cost_amount,amount + transport_amount, cost_amount + transport_amount
		from
			unified_order
		<where>
			id in
			<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</where>
	</insert>

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

<!--		uo.order_sn,-->

<!--		uo.cost_amount as order_cost_amount,-->
<!--		uo.transport_amount as order_transport_amount,-->

<!--		uog1.goods_amount as goods_cost_amount,-->
<!--		ifnull(uog2.goods_amount,0) as goods_refund_cost_amount,-->
<!--		ifnull(uog0.goods_amount,0) as goods_transport_amount-->
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

		<result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>
		<result column="order_transport_amount" property="orderTransportAmount" jdbcType="DECIMAL"/>

		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>
		<result column="goods_refund_cost_amount" property="goodsRefundCostAmount" jdbcType="DECIMAL"/>
		<result column="goods_transport_amount" property="goodsTransportAmount" jdbcType="DECIMAL"/>
	</resultMap>



	<select id="checkAmountErrorForFixDimOrder" resultMap="checkAmountErrorForFixDimOrderResultMap">

	SELECT
		uo.order_sn,

		uo.cost_amount as order_cost_amount,
		uo.transport_amount as order_transport_amount,

		uog1.goods_amount as goods_cost_amount,
		ifnull(uog2.goods_amount,0) as goods_refund_cost_amount,
		ifnull(uog0.goods_amount,0) as goods_transport_amount

	FROM
		unified_order uo

		LEFT JOIN (
			SELECT sum(cost_price*quantity) as goods_amount , order_sn
			FROM unified_order_goods
			WHERE order_sn in (
				SELECT
					order_sn
				FROM
					accounting_unified_supplier_order
				<where>
					<include refid="checkAmountErrorForFixWhereIf"></include>
					AND direction =1
					AND goods_id !=0
				</where>

			)
			GROUP by order_sn
		) uog1 on uo.order_sn = uog1.order_sn

		LEFT JOIN (
			SELECT sum(cost_price*quantity) as goods_amount , order_sn
			FROM unified_order_goods
			WHERE order_sn in (
				SELECT
					order_sn
				FROM
					accounting_unified_supplier_order
				<where>
					<include refid="checkAmountErrorForFixWhereIf"></include>
					AND direction = -1
				</where>

			)
			GROUP by order_sn
		) uog2 on uo.order_sn = uog2.order_sn

		LEFT JOIN (
			SELECT sum(cost_price*quantity) as goods_amount , order_sn
			FROM unified_order_goods
			WHERE order_sn in (
				SELECT
					order_sn
				FROM
					accounting_unified_supplier_order
				<where>
					<include refid="checkAmountErrorForFixWhereIf"></include>
					AND direction =1
					AND goods_id = 0
				</where>
			)
			GROUP by order_sn
		) uog0 on uo.order_sn = uog0.order_sn

	WHERE uo.order_sn in (
		SELECT
			order_sn
		FROM
			accounting_unified_supplier_order
		<where>
			<include refid="checkAmountErrorForFixWhereIf"></include>
		</where>

	)
	GROUP by uo.order_sn
	</select>





	<sql id="checkAmountErrorForFixWhereIf">

		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and accounting_order_sn = #{accountingOrderSn,jdbcType=VARCHAR}
		</if>

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

	</sql>


</mapper>