<?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.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportXGoodsMapperEx">


    <!-- cn.superdesk.uniorder.dao.entity.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportXGoods -->
    <insert id="insertBatch" parameterType="java.util.List">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        <!-- insert -->
        replace into accounting_unified_supplier_order_jd_check_up_report_x_goods (
        dim_create_by,

        batch_id,
        accounting_unified_supplier_order_jd_id,

        accounting_order_id,
        accounting_order_sn,

        doggod_order_sn_as_whole,
        doggod_order_accounting_state_supplier,
        doggod_goods_accounting_state_supplier,
        process_result,

        unified_order_id,
        order_sn,

        unified_order_goods_id,
        goods_id,
        out_snapshot_order_goods_id,
        goods_quantity,
        goods_cost_price,
        goods_cost_amount,

        unified_order_credit_id,
        create_time,
        create_user_id
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (
        #{item.dimCreateBy,jdbcType=TINYINT},

        #{item.batchId,jdbcType=VARCHAR},
        #{item.accountingUnifiedSupplierOrderJdId,jdbcType=BIGINT},

        #{item.accountingOrderId,jdbcType=BIGINT},
        #{item.accountingOrderSn,jdbcType=VARCHAR},

        #{item.doggodOrderSnAsWhole,jdbcType=VARCHAR},
        #{item.doggodOrderAccountingStateSupplier,jdbcType=TINYINT},
        #{item.doggodGoodsAccountingStateSupplier,jdbcType=TINYINT},
        #{item.processResult,jdbcType=TINYINT},

        #{item.unifiedOrderId,jdbcType=BIGINT},
        #{item.orderSn,jdbcType=VARCHAR},

        #{item.unifiedOrderGoodsId,jdbcType=BIGINT},
        #{item.goodsId,jdbcType=BIGINT},
        #{item.outSnapshotOrderGoodsId,jdbcType=BIGINT},

        #{item.goodsQuantity,jdbcType=INTEGER},
        #{item.goodsCostPrice,jdbcType=DECIMAL},
        #{item.goodsCostAmount,jdbcType=DECIMAL},

        #{item.unifiedOrderCreditId,jdbcType=BIGINT},

        #{item.createTime,jdbcType=TIMESTAMP},
        #{item.createUserId,jdbcType=BIGINT}
        )
        </foreach>
    </insert>

    <sql id="conditionPart">

        <if test="batchId != null and batchId != '' ">
            and ausuojcurxg.batch_id = #{batchId}
        </if>

        <if test="accountingUnifiedSupplierOrderJdId != null and accountingUnifiedSupplierOrderJdId != '' ">
            and ausuojcurxg.accounting_unified_supplier_order_jd_id = #{accountingUnifiedSupplierOrderJdId}
        </if>

        <if test="accountingUnifiedSupplierOrderJdIdSelected != null and accountingUnifiedSupplierOrderJdIdSelected != '' and accountingUnifiedSupplierOrderJdIdSelected.size > 0">
            and ausuojcurxg.accounting_unified_supplier_order_jd_id in
            <foreach collection="accountingUnifiedSupplierOrderJdIdSelected" index="index" item="ref_id" open="(" close=")" separator=",">
                #{ref_id}
            </foreach>
        </if>

        <choose>
            <when test="processResult != null">
                and ausuojcurxg.process_result = #{processResult}
            </when>
            <otherwise>
                <!--
                and ausuojcurxg.process_result = 1
                -->
            </otherwise>
        </choose>
    </sql>


    <resultMap id="simulateGoodsSelectedDimGoodsResultMap"
               type="cn.superdesk.uniorder.dto.param.thirdpart.jd.AccountingUnifiedSupplierJdOpCreateParamDimGoodsSimulateSelectedDimGoods">

        <!-- extends AccountingUnifiedSupplierCreateParamDimGoodsSelected start -->
        <result column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>

        <result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
        <result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
        <!-- extends AccountingUnifiedSupplierCreateParamDimGoodsSelected   end -->

        <result column="order_sn" jdbcType="VARCHAR" property="orderSn"/>
    </resultMap>

    <select id="simulateGoodsSelectedDimGoods" resultMap="simulateGoodsSelectedDimGoodsResultMap">

        SELECT
            ausuojcurxg.unified_order_id,
            ausuojcurxg.unified_order_goods_id,
            ausuojcurxg.goods_id,
            ausuojcurxg.order_sn

        FROM
            accounting_unified_supplier_order_jd_check_up_report_x_goods ausuojcurxg
        <where>

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

    <resultMap id="pageQueryGenericWoofResultMap" type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.AccountingUnifiedSupplierOrderJdCheckUpReportXGoodsListItemVoWoof">

        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="dim_create_by" jdbcType="TINYINT" property="dimCreateBy" />
        <result column="batch_id" jdbcType="VARCHAR" property="batchId" />
        <result column="accounting_unified_supplier_order_jd_id" jdbcType="BIGINT" property="accountingUnifiedSupplierOrderJdId" />


        <!--        ausuojcurxg.doggod_order_sn_as_whole,-->
        <!--        ausuojcurxg.doggod_order_accounting_state_supplier,-->
        <!--        ausuojcurxg.doggod_goods_accounting_state_supplier,-->
        <!--        ausuojcurxg.order_sn as goddog_order_sn,-->
        <!--        ausuojcurxg.process_result as goddog_process_result,-->

        <result column="doggod_order_sn_as_whole" jdbcType="VARCHAR" property="doggodOrderSnAsWhole" />
        <result column="doggod_order_accounting_state_supplier" jdbcType="TINYINT" property="doggodOrderAccountingStateSupplier" />
        <result column="doggod_goods_accounting_state_supplier" jdbcType="TINYINT" property="doggodGoodsAccountingStateSupplier" />
        <result column="goddog_order_sn" jdbcType="VARCHAR" property="goddogOrderSn" />
        <result column="goddog_process_result" jdbcType="TINYINT" property="goddogProcessResult" />

        <result column="unified_order_id" jdbcType="BIGINT" property="unifiedOrderId" />
        <result column="unified_order_goods_id" jdbcType="BIGINT" property="unifiedOrderGoodsId" />
        <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
        <result column="goods_quantity" jdbcType="INTEGER" property="goodsQuantity" />
        <result column="goods_cost_price" jdbcType="DECIMAL" property="goodsCostPrice" />
        <!-- goods_cost_amount -->
        <result column="goods_cost_price_x_quantity" jdbcType="DECIMAL" property="goodsCostPriceXQuantity" />

    </resultMap>

    <select id="pageQueryGenericWoof" resultMap="pageQueryGenericWoofResultMap">

        SELECT
            ausuojcurxg.id,
            ausuojcurxg.dim_create_by,
            ausuojcurxg.batch_id,
            ausuojcurxg.accounting_unified_supplier_order_jd_id,

            ausuojcurxg.doggod_order_sn_as_whole,
            ausuojcurxg.doggod_order_accounting_state_supplier,
            ausuojcurxg.doggod_goods_accounting_state_supplier,
            ausuojcurxg.order_sn as goddog_order_sn,
            ausuojcurxg.process_result as goddog_process_result,

            ausuojcurxg.unified_order_id,
            ausuojcurxg.unified_order_goods_id,
            ausuojcurxg.goods_id,
            ausuojcurxg.goods_quantity,
            ausuojcurxg.goods_cost_price,
            ausuojcurxg.goods_cost_amount as goods_cost_price_x_quantity

        FROM
            accounting_unified_supplier_order_jd_check_up_report_x_goods ausuojcurxg
        <where>
            <include refid="conditionPart"></include>
        </where>
        <choose>
            <when test="orderByClause != null and orderByClause != '' ">
                order by ${orderByClause}
            </when>
            <otherwise>
                order by ausuojcurxg.id ASC
            </otherwise>
        </choose>
        <!--
        limit 0,10
        -->
    </select>
</mapper>