<?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">
    <delete id="deleteByAccountingOrderSn">
		DELETE FROM
			accounting_unified_supplier_order
		WHERE
			accounting_order_sn = #{accountingOrderSn}
	</delete>

    <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.AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder">

		<!-- auso.id as accounting_unified_supplier_x_order_id, -->
		<id column="accounting_unified_supplier_x_order_id" jdbcType="BIGINT" property="accountingUnifiedSupplierXOrderId" />
		<!-- 对账单Id aus.id as accounting_order_id, -->
		<result column="accounting_order_id" jdbcType="BIGINT" property="accountingOrderId" />
		<!-- 对账单Sn aus.accounting_order_sn, -->
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn" />
		<!-- 统一订单id -->
		<result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId" />
		<!-- 订单编号 -->
		<result column="order_sn" jdbcType="VARCHAR" property="orderSn" />
		<!-- 统一订单商品Id -->
		<result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId" />
		<!-- 第三方商城的商品表Id -->
		<result column="goods_id" jdbcType="BIGINT" property="goodsId" />
		<!-- 第三方商城的订单的商品快照Id -->
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
		<!-- 统一订单流水Id,目前维度是没有用的 -->
		<result column="unified_order_credit_id" jdbcType="BIGINT" property="unifiedOrderCreditId" />


		<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="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="direction" property="direction" jdbcType="TINYINT"/>
	</resultMap>



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

		SELECT
			auso.id as accounting_unified_supplier_x_order_id,
			aus.id as accounting_order_id,
			aus.accounting_order_sn,


			uo.id as unified_order_id,
			uo.order_sn, <!-- auso.order_sn, -->

			uog.id as unified_order_goods_id,
			uog.goods_id,
			uog.out_snapshot_order_goods_id,

			0 as unified_order_credit_id,

			uog.goods_name,
			uog.goods_alias,<!-- 可不改 -->

			uog.price,
			uog.cost_price,
			uog.quantity,

			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.direction

		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>
			<include refid="checkAmountErrorForFixWhereIf"></include>
			<choose>
				<when test="orderBy != null and orderBy != '' ">
					order by ${orderBy}
				</when>
				<otherwise>
					order by uo.create_time desc, uo.order_sn desc, uog.id asc
				</otherwise>
			</choose>
		</where>
	</select>

	<sql id="checkAmountErrorForFixWhereIf">

		<if test="accountingOrderSn != null and accountingOrderSn != ''">
			and aus.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>