<?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.zehong.system.mapper.RobotArmCommandMapper">
    
    <resultMap type="RobotArmCommand" id="RobotArmCommandResult">
        <result property="robotArmCommandId"    column="f_robot_arm_command_id"    />
        <result property="trayCode"    column="f_tray_code"    />
        <result property="storeyCode"    column="f_storey_code"    />
        <result property="type"    column="f_type"    />
        <result property="status"    column="f_status"    />
        <result property="startExecutionTime"    column="f_start_execution_time"    />
        <result property="endExecutionTime"    column="f_end_execution_time"    />
        <result property="createTime"    column="f_create_time"    />
        <result property="command"    column="f_command"    />
    </resultMap>

    <sql id="selectRobotArmCommandVo">
        select f_robot_arm_command_id,
               f_tray_code,
               f_storey_code,
               f_type,
               f_status,
               f_start_execution_time,
               f_end_execution_time,
               f_create_time,
               f_command
        from t_robot_arm_command
    </sql>

    <select id="findByType" parameterType="string" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        where f_type = #{type} and f_status != '4'
    </select>

    <select id="selectRobotArmCommandList" parameterType="RobotArmCommand" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        <where>  
            <if test="trayCode != null  and trayCode != ''"> and f_tray_code = #{trayCode}</if>
            <if test="storeyCode != null  and storeyCode != ''"> and f_storey_code = #{storeyCode}</if>
            <if test="type != null  and type != ''"> and f_type = #{type}</if>
            <if test="status != null  and status != ''"> and f_status = #{status}</if>
            <if test="startExecutionTime != null "> and f_start_execution_time = #{startExecutionTime}</if>
            <if test="endExecutionTime != null "> and f_end_execution_time = #{endExecutionTime}</if>
            <if test="createTime != null "> and f_create_time = #{createTime}</if>
        </where>
    </select>
    <select id="selectTopRunningRobotArmCommands" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        where f_status = '2'
    </select>

    <select id="findNotCompletedCommand" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        where f_command = #{command} and f_status != '4'
    </select>
    <select id="selectRobotArmCommandById" parameterType="Long" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        where f_robot_arm_command_id = #{robotArmCommandId}
    </select>
    <!-- 查找执行中的指令 -->
    <select id="findExecutingCommand" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_tray_code = #{trayCode}
        AND f_storey_code = #{storeyCode}
        AND f_status = '1'  <!-- 状态为执行中 -->
        LIMIT 1
    </select>

    <select id="findExecutingCommandByType" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_tray_code = #{trayCode}
        AND f_storey_code = #{storeyCode}
        AND f_type = #{type}
        AND f_status = '1'  <!-- 状态为执行中 -->
        LIMIT 1
    </select>

    <!-- 查找最近扫码但未处理的托盘 -->
    <select id="selectLatestPendingTray" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_status = '0' AND f_type = '0'
        ORDER BY f_create_time DESC
        LIMIT 1
    </select>

    <!-- 根据托盘编号和状态查找指令 -->
    <select id="findByTrayAndStatus" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_tray_code = #{trayCode} AND f_status = #{status}
        LIMIT 1
    </select>
    <insert id="insertRobotArmCommand" parameterType="RobotArmCommand" useGeneratedKeys="true" keyProperty="robotArmCommandId">
        insert into t_robot_arm_command
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trayCode != null">f_tray_code,</if>
            <if test="storeyCode != null">f_storey_code,</if>
            <if test="type != null">f_type,</if>
            <if test="status != null">f_status,</if>
            <if test="startExecutionTime != null">f_start_execution_time,</if>
            <if test="endExecutionTime != null">f_end_execution_time,</if>
            <if test="createTime != null">f_create_time,</if>
            <if test="command != null">f_command,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="trayCode != null">#{trayCode},</if>
            <if test="storeyCode != null">#{storeyCode},</if>
            <if test="type != null">#{type},</if>
            <if test="status != null">#{status},</if>
            <if test="startExecutionTime != null">#{startExecutionTime},</if>
            <if test="endExecutionTime != null">#{endExecutionTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="command != null">#{command},</if>
         </trim>
    </insert>

    <update id="updateRobotArmCommand" parameterType="RobotArmCommand">
        update t_robot_arm_command
        <trim prefix="SET" suffixOverrides=",">
            <if test="trayCode != null">f_tray_code = #{trayCode},</if>
            <if test="storeyCode != null">f_storey_code = #{storeyCode},</if>
            <if test="type != null">f_type = #{type},</if>
            <if test="status != null">f_status = #{status},</if>
            <if test="startExecutionTime != null">f_start_execution_time = #{startExecutionTime},</if>
            <if test="endExecutionTime != null">f_end_execution_time = #{endExecutionTime},</if>
            <if test="createTime != null">f_create_time = #{createTime},</if>
            <if test="command != null">f_command = #{command},</if>
        </trim>
        where f_robot_arm_command_id = #{robotArmCommandId}
    </update>
    <!-- 在 XML 中添加 -->
    <!-- 更新执行中状态为完成状态 -->
    <update id="updateExecutingToCompleted">
        UPDATE t_robot_arm_command
        SET f_status = '2'
        WHERE f_status = '1' and f_end_execution_time IS NOT NULL
    </update>

    <!-- 获取待执行的上料指令 -->
    <select id="selectPendingLoadingCommands" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_type = '0' AND f_status = '1'
        ORDER BY f_create_time ASC
        LIMIT 1
    </select>

    <!-- 获取待执行的下料指令 -->
    <select id="selectPendingUnloadingCommands" resultMap="RobotArmCommandResult">
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_type = '1' AND f_status = '1'
        ORDER BY f_create_time ASC
        LIMIT 1
    </select>
    <!-- 同时获取待执行的上料指令（type=0）和下料指令（type=1），各1条 -->
    <select id="selectPendingLoadUnloadCommands" resultMap="RobotArmCommandResult">
        <!-- 子查询1：上料指令（type=0），取最早创建的1条 -->
        SELECT * FROM (
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_type = '0' AND f_status = '1'
        ORDER BY f_create_time ASC
        LIMIT 1
        ) AS loading
        UNION ALL
        <!-- 子查询2：下料指令（type=1），取最早创建的1条 -->
        SELECT * FROM (
        <include refid="selectRobotArmCommandVo"/>
        WHERE f_type = '1' AND f_status = '1'
        ORDER BY f_create_time ASC
        LIMIT 1
        ) AS unloading
    </select>
    <delete id="deleteRobotArmCommandById" parameterType="Long">
        delete from t_robot_arm_command where f_robot_arm_command_id = #{robotArmCommandId}
    </delete>

    <delete id="deleteRobotArmCommandByIds" parameterType="String">
        delete from t_robot_arm_command where f_robot_arm_command_id in 
        <foreach item="robotArmCommandId" collection="array" open="(" separator="," close=")">
            #{robotArmCommandId}
        </foreach>
    </delete>
</mapper>