<?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.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskDetailMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO">
        <!--@Table robot_task_detail-->
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="robotTaskId" column="robot_task_id" jdbcType="INTEGER"/>
        <result property="taskType" column="task_type" jdbcType="INTEGER"/>
        <result property="releaseType" column="release_type" jdbcType="INTEGER"/>
        <result property="takeType" column="take_type" jdbcType="INTEGER"/>
        <result property="releaseId" column="release_id" jdbcType="INTEGER"/>
        <result property="takeId" column="take_id" jdbcType="INTEGER"/>
        <result property="fromLocationNo" column="from_location_no" jdbcType="VARCHAR"/>
        <result property="fromLocationId" column="from_location_id" jdbcType="INTEGER"/>
        <result property="fromLaneId" column="from_lane_id" jdbcType="INTEGER"/>
        <result property="toLaneId" column="to_lane_id" jdbcType="INTEGER"/>
        <result property="fromLocationNumber" column="from_location_number" jdbcType="INTEGER"/>
        <result property="toLocationNumber" column="to_location_number" jdbcType="INTEGER"/>
        <result property="fromMapItemId" column="from_map_item_id" jdbcType="INTEGER"/>
        <result property="toMapItemId" column="to_map_item_id" jdbcType="INTEGER"/>
        <result property="toLocationNo" column="to_location_no" jdbcType="VARCHAR"/>
        <result property="toLocationId" column="to_location_id" jdbcType="INTEGER"/>
        <result property="fromLocationStorey" column="from_location_storey" jdbcType="INTEGER"/>
        <result property="toLocationStorey" column="to_location_storey" jdbcType="INTEGER"/>
        <result property="priority" column="priority" jdbcType="INTEGER"/>
        <result property="robotNo" column="robot_no" jdbcType="VARCHAR"/>
        <result property="robotAction" column="robot_action" jdbcType="VARCHAR"/>
        <result property="taskStatus" column="task_status" jdbcType="INTEGER"/>
        <result property="taskStage" column="task_stage" jdbcType="INTEGER"/>
        <result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
        <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
        <result property="creator" column="creator" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updater" column="updater" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="deleted" column="deleted" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <sql id="base_sql">
               id,
               robot_task_id,
               task_type,
               release_type,
               take_type,
               release_id,
               take_id,
               from_location_no,
               from_location_id,
               from_lane_id,
               to_lane_id,
               to_location_no,
               to_location_id,
               robot_no,
               robot_action,
               task_status,
               task_stage,
               start_time,
               end_time,
               creator,
               create_time,
               updater,
               update_time,
               deleted,
               tenant_id,
               from_location_storey,
               to_location_storey,
               priority,
               from_location_number,
               to_location_number,
               from_map_item_id,
               to_map_item_id
    </sql>



    <select id="queryDoingTaskByIds" resultMap="BaseResultMap">
        select
            <include refid="base_sql"></include>
        from
            robot_task_detail
        where
            from_location_id in
            <foreach collection="locationIds" item="id" index="index" open="(" close=")"
                     separator=",">
                #{id}
            </foreach>
        and deleted = '0'
        and task_status in ('0','1')
    </select>

    <select id="getSingleUnDoTask"  resultMap="BaseResultMap">
        select
            <include refid="base_sql"></include>
        from
            robot_task_detail
        where
            task_status = '0'
            and deleted = '0'
        <if test="taskIds != null and taskIds.size() > 0">
            and robot_task_id in
            <foreach collection="taskIds" item="taskId" index="index" open="(" close=")"
                     separator=",">
                #{taskId}
            </foreach>
        </if>
        order by priority desc, create_time asc ,robot_no desc
    </select>


    <select id="queryByTaskId"  resultMap="BaseResultMap">
        select
            <include refid="base_sql"></include>
        from
            robot_task_detail
        where
            robot_task_id = #{robotTaskId}
        and deleted = '0'
    </select>

    <select id="getUnDoTaskDetail"  resultMap="BaseResultMap">
        SELECT
            *
        FROM (
            SELECT *,
                ROW_NUMBER() OVER (PARTITION BY robot_task_id order by priority desc, create_time asc ,robot_no desc) AS rn
            FROM
                robot_task_detail
            where
                task_status = '0'
                and deleted = '0'
                <if test="montageTaskIds != null and montageTaskIds.size() > 0">
                    and robot_task_id in
                    <foreach collection="montageTaskIds" item="taskId" index="index" open="(" close=")"
                             separator=",">
                        #{taskId}
                    </foreach>
                </if>
            ) AS user
        WHERE rn = 1
        union
        select
             *, tenant_id as rn
        from
            robot_task_detail
        where
            task_status = '0'
            and deleted = '0'
            <if test="singleTaskIds != null and singleTaskIds.size() > 0">
                and robot_task_id in
                <foreach collection="singleTaskIds" item="taskId" index="index" open="(" close=")"
                         separator=",">
                    #{taskId}
                </foreach>
            </if>
        order by priority desc, create_time asc ,robot_no desc
    </select>


    <select id="getMontageUnDoTask"  resultMap="BaseResultMap">
        SELECT
            *
        FROM (
            SELECT *,
                ROW_NUMBER() OVER (PARTITION BY robot_task_id order by id asc ) AS rn
            FROM robot_task_detail
        where
            task_status = '0'
            and deleted = '0'
            <if test="montageTaskIds != null and montageTaskIds.size() > 0">
                and robot_task_id in
                <foreach collection="montageTaskIds" item="taskId" index="index" open="(" close=")"
                         separator=",">
                    #{taskId}
                </foreach>
            </if>
            ) AS user
        WHERE rn = 1
        order by priority desc, create_time asc ,robot_no desc
    </select>


    <select id="getChargeTaskDetail"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO">
        SELECT
            t2.id,
            t2.robot_task_id as robotTaskId,
            t2.robot_no as robotNo,
            t2.priority
        from
            robot_task t1, robot_task_detail t2
        where
            t1.task_status in('0','1')
          and t1.id = t2.robot_task_id
          and t2.task_status = '0'
          and t2.task_type = '3'
    </select>

    <select id="selectTakeMapItemIds" resultType="java.lang.Long">
        select
            t2.map_item_id
        from
            robot_task_detail t1, ware_house_location t2
        where
            t1.from_location_id = t2.id
            and t1.task_status in ('0','1')
    </select>

    <select id="selectReleaseMapItemIds" resultType="java.lang.Long">
        select
            t2.map_item_id
        from
            robot_task_detail t1, ware_house_location t2
        where
            t1.to_location_id = t2.id
          and t1.task_status in ('0','1')
    </select>

    <select id="selectAutoCreateCycleTask"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO">
        select
            t1.*
        from
            robot_task_detail t1, robot_task t2
        where
             t2.id = t1.robot_task_id
             and t2.remaining_cycle_number > 1
             and t2.task_status = '2'
             and t1.task_status = '2'
             and t1.deleted = '0'
             and t2.deleted = '0'
    </select>


    <!--keyProperty="id" useGeneratedKeys="true"-->
    <insert id="insertBatchList" >
        insert into robot_task_detail(robot_task_id, task_type, release_type, take_type, release_id, take_id,
        from_location_no, from_location_id, to_location_no, to_location_id, robot_no,from_location_storey,
        to_location_storey,priority,from_lane_id,to_lane_id,from_location_number,to_location_number,
        from_map_item_id,to_map_item_id)
        values
        <foreach collection="taskDetailList" item="entity" separator=",">
            (#{entity.robotTaskId}, #{entity.taskType}, #{entity.releaseType}, #{entity.takeType}, #{entity.releaseId},
            #{entity.takeId}, #{entity.fromLocationNo}, #{entity.fromLocationId}, #{entity.toLocationNo},
            #{entity.toLocationId}, #{entity.robotNo}, #{entity.fromLocationStorey}, #{entity.toLocationStorey}
            , #{entity.priority}, #{entity.fromLaneId}, #{entity.toLaneId}, #{entity.fromLocationNumber},
            #{entity.toLocationNumber}, #{entity.fromMapItemId}, #{entity.toMapItemId})
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="updateRobotDetailById">
        update robot_task_detail
        <set>
            <if test="robotTaskId != null">
                robot_task_id = #{robotTaskId},
            </if>
            <if test="taskType != null">
                task_type = #{taskType},
            </if>
            <if test="releaseType != null">
                release_type = #{releaseType},
            </if>
            <if test="takeType != null">
                take_type = #{takeType},
            </if>
            <if test="releaseId != null">
                release_id = #{releaseId},
            </if>
            <if test="takeId != null">
                take_id = #{takeId},
            </if>
            <if test="fromLocationNo != null and fromLocationNo != ''">
                from_location_no = #{fromLocationNo},
            </if>
            <if test="fromLocationId != null">
                from_location_id = #{fromLocationId},
            </if>
            <if test="toLocationNo != null and toLocationNo != ''">
                to_location_no = #{toLocationNo},
            </if>
            <if test="toLocationId != null">
                to_location_id = #{toLocationId},
            </if>
            <if test="robotNo != null and robotNo != ''">
                robot_no = #{robotNo},
            </if>
            <if test="robotAction != null and robotAction != ''">
                robot_action = #{robotAction},
            </if>
            <if test="taskStatus != null">
                task_status = #{taskStatus},
            </if>
            <if test="taskStage != null">
                task_stage = #{taskStage},
            </if>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="creator != null and creator != ''">
                creator = #{creator},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from robot_task_detail
        where id = #{id}
    </delete>


</mapper>