<?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.ruoyi.business.mapper.OrderManageMapper">
    
    <resultMap type="OrderManage" id="OrderManageResult">
        <result property="orderId"    column="order_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="needNum"    column="need_num"    />
        <result property="finishNum"    column="finish_num"    />
        <result property="waitNum"    column="wait_num"    />
        <result property="actualNum"    column="actual_num"    />
        <result property="status"    column="status"    />
        <result property="updateTime"    column="update_time"    />
        <result property="sortIndex"    column="sort_index"    />
    </resultMap>

    <sql id="selectOrderManageVo">
        select order_id, goods_id, need_num, finish_num, wait_num, actual_num, status, update_time, sort_index from order_manage
    </sql>

    <select id="selectOrderManageList" parameterType="OrderManage" resultMap="OrderManageResult">
        <include refid="selectOrderManageVo"/>
        <where>
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="goodsId != null  and goodsId != ''"> and goods_id = #{goodsId}</if>
            <if test="needNum != null "> and need_num = #{needNum}</if>
            <if test="finishNum != null "> and finish_num = #{finishNum}</if>
            <if test="waitNum != null "> and wait_num = #{waitNum}</if>
            <if test="actualNum != null "> and actual_num = #{actualNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="sortIndex != null  and sortIndex != ''"> and sort_index = #{sortIndex}</if>
            and status != "-1"
        </where>
        order by goods_id, sort_index
    </select>
    
    <select id="selectOrderManageByOrderId" parameterType="String" resultMap="OrderManageResult">
        <include refid="selectOrderManageVo"/>
        where order_id = #{orderId}
    </select>

    <select id="selectNextOrder" parameterType="String" resultMap="OrderManageResult">
        <include refid="selectOrderManageVo"/>
        where goods_id = #{goodsId} and status = "0"
        order by sort_index
        limit 0, 1
    </select>

    <select id="selectPreOrder" parameterType="OrderManage" resultMap="OrderManageResult">
        <include refid="selectOrderManageVo"/>
        <where>
            <if test="goodsId != null  and goodsId != ''"> and goods_id = #{goodsId}</if>
            <if test="sortIndex != null  and sortIndex != ''"> and sort_index &lt; #{sortIndex}</if>
            and status = "0"
        </where>
        order by sort_index desc
        limit 0, 1
    </select>

    <select id="selectOrderListForOut" parameterType="OrderManage" resultMap="OrderManageResult">
        select goods_id, sum(need_num) as need_num, sum(finish_num) as finish_num, sum(wait_num) as wait_num
        from order_manage
        <where>
            <if test="goodsId != null  and goodsId != ''"> and goods_id = #{goodsId}</if>
            <if test="needNum != null "> and need_num = #{needNum}</if>
            <if test="finishNum != null "> and finish_num = #{finishNum}</if>
            <if test="waitNum != null "> and wait_num = #{waitNum}</if>
            <if test="actualNum != null "> and actual_num = #{actualNum}</if>
            <if test="sortIndex != null  and sortIndex != ''"> and sort_index = #{sortIndex}</if>
            and status = "0"
        </where>
        group by goods_id
    </select>
        
    <insert id="insertOrderManage" parameterType="OrderManage">
        insert into order_manage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="needNum != null">need_num,</if>
            <if test="finishNum != null">finish_num,</if>
            <if test="waitNum != null">wait_num,</if>
            <if test="actualNum != null">actual_num,</if>
            <if test="status != null">status,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="sortIndex != null">sort_index,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="needNum != null">#{needNum},</if>
            <if test="finishNum != null">#{finishNum},</if>
            <if test="waitNum != null">#{waitNum},</if>
            <if test="actualNum != null">#{actualNum},</if>
            <if test="status != null">#{status},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="sortIndex != null">#{sortIndex},</if>
         </trim>
    </insert>

    <update id="updateOrderManage" parameterType="OrderManage">
        update order_manage
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="needNum != null">need_num = #{needNum},</if>
            <if test="finishNum != null">finish_num = #{finishNum},</if>
            <if test="waitNum != null">wait_num = #{waitNum},</if>
            <if test="actualNum != null">actual_num = #{actualNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="sortIndex != null">sort_index = #{sortIndex},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <update id="cancelOrdersByOrderIds" parameterType="String">
        update order_manage set status = "-1"
        where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </update>

    <update id="upOneOrder" parameterType="OrderManage">
        update order_manage
        set sort_index = #{sortIndex}-1
        where order_id = #{orderId}
    </update>

    <delete id="deleteOrderManageByOrderId" parameterType="String">
        delete from order_manage where order_id = #{orderId}
    </delete>

    <delete id="deleteOrderManageByOrderIds" parameterType="String">
        delete from order_manage where order_id in 
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>

</mapper>