<?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="com.tmerclub.cloud.order.mapper.AllotOrderMapper">

    <resultMap id="AllotOrderVOMap" type="com.tmerclub.cloud.order.vo.AllotOrderVO">
        <result column="allot_order_id" property="allotOrderId" />
        <result column="out_warehouse_id" property="outWarehouseId" />
        <result column="out_stock_point_type" property="outStockPointType" />
        <result column="out_stock_point_name" property="outStockPointName" />
        <result column="in_warehouse_id" property="inWarehouseId" />
        <result column="in_stock_point_type" property="inStockPointType" />
        <result column="in_stock_point_name" property="inStockPointName" />
        <result column="dvy_company_id" property="dvyCompanyId" />
        <result column="dvy_type" property="dvyType" />
        <result column="dvy_order_number" property="dvyOrderNumber" />
        <result column="status" property="status" />
        <result column="total_allot_count" property="totalAllotCount" />
        <result column="remark" property="remark" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="shop_id" property="shopId" />
        <result column="sys_type" property="sysType" />
        <collection property="allotOrderItemVOList" ofType="com.tmerclub.cloud.order.vo.AllotOrderItemVO">
            <result column="allot_order_item_id" property="allotOrderItemId" />
            <result column="spu_id" property="spuId" />
            <result column="sku_id" property="skuId" />
            <result column="party_code" property="partyCode" />
            <result column="allot_count" property="allotCount" />
            <result column="allot_in_count" property="allotInCount" />
        </collection>
    </resultMap>

    <select id="getAllotOrder" resultMap="AllotOrderVOMap">
        SELECT
            ao.*,
            aoi.*
        FROM
            allot_order ao
            LEFT JOIN allot_order_item aoi ON ao.allot_order_id = aoi.allot_order_id
        WHERE
            ao.allot_order_id = #{allotOrderDTO.allotOrderId}
            <if test="allotOrderDTO.shopId != null">
                AND ao.shop_id = #{allotOrderDTO.shopId}
            </if>
            <if test="allotOrderDTO.sysType != null">
                AND ao.sys_type = #{allotOrderDTO.sysType}
            </if>
    </select>

    <insert id="addAllotOrder">
        INSERT INTO allot_order(`allot_order_id`, `out_warehouse_id`, `out_stock_point_type`, `in_warehouse_id`, `in_stock_point_type`, `dvy_company_id`, `dvy_type`,
            `dvy_order_number`, `status`, `total_allot_count`, `remark`, `shop_id`, `sys_type`, `create_time`, `update_time`)
        VALUES
            (#{allotOrder.allotOrderId}, #{allotOrder.outWarehouseId}, #{allotOrder.outStockPointType}, #{allotOrder.inWarehouseId},
            #{allotOrder.inStockPointType}, #{allotOrder.dvyCompanyId}, #{allotOrder.dvyType}, #{allotOrder.dvyOrderNumber}, #{allotOrder.status}, #{allotOrder.totalAllotCount},
            #{allotOrder.remark}, #{allotOrder.shopId}, #{allotOrder.sysType}, #{allotOrder.createTime}, #{allotOrder.updateTime})
    </insert>

    <update id="updateAllotOrder">
        UPDATE
            allot_order
        <set>
            <if test="allotOrder.status != null">
                `status` = #{allotOrder.status},
            </if>
            <if test="allotOrder.remark != null and allotOrder.remark != ''">
                `remark` = #{allotOrder.remark},
            </if>
            <if test="allotOrder.updateTime != null">
                `update_time` = #{allotOrder.updateTime}
            </if>
        </set>
        WHERE
            allot_order_id = #{allotOrder.allotOrderId}
    </update>

</mapper>