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

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

        <!-- ausxg.id,-->
        <id column="accounting_unified_supplier_x_good_id" jdbcType="BIGINT" property="accountingUnifiedSupplierXGoodsId" />
        <!-- 对账单Id ausxg.accounting_order_id -->
        <result column="accounting_order_id" jdbcType="BIGINT" property="accountingOrderId"/>
        <!-- 对账单Sn ausxg.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="price" property="price" jdbcType="DECIMAL"/>
        <result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
        <result column="quantity" property="quantity" jdbcType="INTEGER"/>

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

    <!-- 以 accountingOrderSn 查出这个对账单 商品信息 目前是为了每个对账单 提供数据源-->
    <select id="checkAmountErrorForFixDimGoods" resultMap="checkAmountErrorForFixDimGoodsResultMap">

        SELECT
            ausuxg.id as accounting_unified_supplier_x_goods_id,
            ausuxg.accounting_order_id,	 							<!-- 对账单Id -->
            ausuxg.accounting_order_sn,	 							<!-- 对账单Sn -->

            ausuxg.unified_order_id,	 								<!-- 统一订单id -->
            ausuxg.order_sn,											<!--  -->

            ausuxg.unified_order_goods_id,	 						<!-- 统一订单商品Id -->
            ausuxg.goods_id, 										<!-- 第三方商城的商品表Id -->
            ausuxg.out_snapshot_order_goods_id,	 					<!-- 第三方商城的订单的商品快照Id -->

            ausuxg.unified_order_credit_id,							<!-- 统一订单流水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 ausu
            LEFT JOIN accounting_unified_supplier_x_goods ausuxg on ausuxg.accounting_order_id = ausu.id
            LEFT JOIN unified_order uo on ausuxg.unified_order_id = uo.id
            LEFT JOIN unified_order_goods uog on ausuxg.unified_order_goods_id = uog.id
        <where>
            <include refid="checkAmountErrorForFixWhereIf"></include>
        </where>
        <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>
    </select>

    <sql id="checkAmountErrorForFixWhereIf">

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

    <!-- cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierXGoods -->
    <insert id="insertBatch" parameterType="java.util.List">

        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into accounting_unified_supplier_x_goods (
        dim_create_by,

        accounting_order_id,
        accounting_order_sn,

        unified_order_id,
        order_sn,

        unified_order_goods_id,
        goods_id,
        out_snapshot_order_goods_id,

        unified_order_credit_id,

        create_time,
        create_user_id,
        update_time,
        update_user_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (
        #{item.dimCreateBy,jdbcType=TINYINT},
        #{item.accountingOrderId,jdbcType=BIGINT},
        #{item.accountingOrderSn,jdbcType=VARCHAR},
        #{item.unifiedOrderId,jdbcType=BIGINT},
        #{item.orderSn,jdbcType=VARCHAR},
        #{item.unifiedOrderGoodsId,jdbcType=BIGINT},
        #{item.goodsId,jdbcType=BIGINT},
        #{item.outSnapshotOrderGoodsId,jdbcType=BIGINT},
        #{item.unifiedOrderCreditId,jdbcType=BIGINT},
        #{item.createTime,jdbcType=TIMESTAMP},
        #{item.createUserId,jdbcType=BIGINT},
        #{item.updateTime,jdbcType=TIMESTAMP},
        #{item.updateUserId,jdbcType=BIGINT}
        )
        </foreach>
    </insert>




</mapper>