<?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.ghy.order.mapper.OrderGoodsMapper">

    <resultMap id="OrderGoodsResult" type="com.ghy.order.domain.OrderGoods">
        <id property="orderGoodsId"             column="order_goods_id"/>
        <result property="orderId"              column="order_id"/>
        <result property="orderDetailId"              column="order_detail_id"/>
        <result property="goodsStandardId"              column="goods_standard_id"/>
        <result property="goodsName"            column="goods_name"/>
        <result property="goodsNum"             column="goods_num"/>
        <result property="serverGoodsNum"       column="server_goods_num"/>
        <result property="finishTime"           column="finish_time"/>
        <result property="createBy"             column="create_by"/>
        <result property="createTime"           column="create_time"/>
        <result property="updateBy"             column="update_by"/>
        <result property="updateTime"           column="update_time"/>
        <result property="remark"               column="remark"/>
    </resultMap>

    <sql id="selectOrderGoods">
        SELECT order_goods_id, order_id, goods_standard_id, goods_name, goods_num, server_goods_num,
               finish_time, create_by, create_time, update_by, update_time, remark
        FROM order_goods
    </sql>

    <update id="updateOrderGoods" parameterType="com.ghy.order.domain.OrderGoods">
        UPDATE order_goods
        <set>
            <if test="orderId != null and orderId != 0">order_id = #{orderId},</if>
            <if test="goodsStandardId != null and goodsStandardId != 0">goods_standard_id = #{goodsStandardId},</if>
            <if test="goodsName != null and goodsName != ''">goods_name = #{goodsName},</if>
            <if test="goodsNum != null">goods_num = #{goodsNum},</if>
            <if test="serverGoodsNum != null">server_goods_num = #{serverGoodsNum},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        WHERE order_goods_id = #{orderGoodsId}
    </update>

    <insert id="insertOrderGoods" parameterType="com.ghy.order.domain.OrderGoods" useGeneratedKeys="true"
            keyProperty="orderGoodsId">
        INSERT INTO order_goods(
            <if test="orderId != null and orderId != 0">order_id,</if>
            <if test="orderDetailId != null and orderDetailId != 0">order_detail_id,</if>
            <if test="goodsStandardId != null and goodsStandardId != 0">goods_standard_id,</if>
            <if test="goodsName != null and goodsName != ''">goods_name,</if>
            <if test="goodsNum != null">goods_num,</if>
            <if test="serverGoodsNum != null">server_goods_num,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )VALUES(
            <if test="orderId != null and orderId != 0">#{orderId},</if>
            <if test="orderDetailId != null and orderDetailId != 0">#{orderDetailId},</if>
            <if test="goodsStandardId != null and goodsStandardId != 0">#{goodsStandardId},</if>
            <if test="goodsName != null and goodsName != ''">#{goodsName},</if>
            <if test="goodsNum != null">#{goodsNum},</if>
            <if test="serverGoodsNum != null">#{serverGoodsNum},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            SYSDATE()
        )
    </insert>

    <select id="selectOrderGoodsList" parameterType="com.ghy.order.domain.OrderGoods" resultMap="OrderGoodsResult">
        <include refid="selectOrderGoods"/>
        <where>
            <if test="orderId != null and orderId != 0">
                AND order_id = #{orderId}
            </if>
            <if test="goodsStandardId != null and goodsStandardId != 0">
                AND goods_standard_id = #{goodsStandardId}
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND goods_name LIKE concat('%', #{goodsName}, '%')
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="OrderGoodsResult">
        <include refid="selectOrderGoods"/>
        <where>
            <if test="orderGoodsId != null and orderGoodsId != 0">
                AND id = #{orderGoodsId}
            </if>
        </where>
    </select>

    <select id="selectByOrderId" parameterType="long" resultMap="OrderGoodsResult">
        <include refid="selectOrderGoods"/>
        <where>
            <if test="orderId != null and orderId != 0">
                AND order_id = #{orderId}
            </if>
        </where>
    </select>

    <select id="selectByOrderDetailId" parameterType="long" resultMap="OrderGoodsResult">
        <include refid="selectOrderGoods"/>
        <where>
            <if test="orderDetailId != null and orderDetailId != 0">
                AND order_detail_id = #{orderDetailId}
            </if>
        </where>
    </select>

    <delete id="deleteOrderGoodsByIds" parameterType="Long">
        DELETE FROM order_goods WHERE order_goods_id IN
        <foreach collection="array" item="orderGoodsId" open="(" separator="," close=")">
            #{orderGoodsId}
        </foreach>
    </delete>

    <select id="getByOrderIdList" resultMap="OrderGoodsResult">
        <include refid="selectOrderGoods" />
        <where>
            and order_id in
            <foreach item="item" index="orderIdList" collection="orderIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

</mapper>
