<?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.RobotTaskMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDO">
        <!--@Table robot_task-->
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="montageTask" column="montage_task" jdbcType="INTEGER"/>
        <result property="montageNumber" column="montage_number" jdbcType="INTEGER"/>
        <result property="skuInfo" column="sku_info" jdbcType="VARCHAR"/>
        <result property="skuBatch" column="sku_batch" jdbcType="VARCHAR"/>
        <result property="skuNumber" column="sku_number" jdbcType="INTEGER"/>
        <result property="priority" column="priority" jdbcType="INTEGER"/>
        <result property="otherMsg" column="other_msg" jdbcType="VARCHAR"/>
        <result property="doCycle" column="do_cycle" jdbcType="INTEGER"/>
        <result property="doMoveAll" column="do_move_all" jdbcType="INTEGER"/>
        <result property="cycleNumber" column="cycle_number" jdbcType="INTEGER"/>
        <result property="remainingCycleNumber" column="remaining_cycle_number" jdbcType="INTEGER"/>
        <result property="taskNo" column="task_no" 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,
               montage_task,
               montage_number,
               sku_info,
               sku_batch,
               sku_number,
               priority,
               other_msg,
               do_cycle,
               do_move_all,
               cycle_number,
               remaining_cycle_number,
               task_no,
               task_status,
               task_stage,
               start_time,
               end_time,
               creator,
               create_time,
               updater,
               update_time,
               deleted,
               tenant_id
    </sql>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="BaseResultMap">
        select
        id, montage_task, montage_number, sku_info, sku_batch, sku_number, priority, other_msg, do_cycle, do_move_all,
        cycle_number, remaining_cycle_number, task_no, task_status, task_stage, start_time, end_time, creator,
        create_time, updater, update_time, deleted, tenant_id
        from robot_task
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="montageTask != null">
                and montage_task = #{montageTask}
            </if>
            <if test="montageNumber != null">
                and montage_number = #{montageNumber}
            </if>
            <if test="skuInfo != null and skuInfo != ''">
                and sku_info = #{skuInfo}
            </if>
            <if test="skuBatch != null and skuBatch != ''">
                and sku_batch = #{skuBatch}
            </if>
            <if test="skuNumber != null">
                and sku_number = #{skuNumber}
            </if>
            <if test="priority != null">
                and priority = #{priority}
            </if>
            <if test="otherMsg != null and otherMsg != ''">
                and other_msg = #{otherMsg}
            </if>
            <if test="doCycle != null">
                and do_cycle = #{doCycle}
            </if>
            <if test="doMoveAll != null">
                and do_move_all = #{doMoveAll}
            </if>
            <if test="cycleNumber != null">
                and cycle_number = #{cycleNumber}
            </if>
            <if test="remainingCycleNumber != null">
                and remaining_cycle_number = #{remainingCycleNumber}
            </if>
            <if test="taskNo != null and taskNo != ''">
                and task_no = #{taskNo}
            </if>
            <if test="taskStatus != null">
                and task_status = #{taskStatus}
            </if>
            <if test="taskStage != null">
                and task_stage = #{taskStage}
            </if>
            <if test="startTime != null">
                and start_time = #{startTime}
            </if>
            <if test="endTime != null">
                and end_time = #{endTime}
            </if>
            <if test="creator != null and creator != ''">
                and creator = #{creator}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updater != null and updater != ''">
                and updater = #{updater}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
            <if test="deleted != null">
                and deleted = #{deleted}
            </if>
            <if test="tenantId != null">
                and tenant_id = #{tenantId}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="BaseResultMap">
    select
    id, montage_task, montage_number, sku_info, sku_batch, sku_number, priority, other_msg, do_cycle, do_move_all,
    cycle_number, remaining_cycle_number, task_no, task_status, task_stage, start_time, end_time, creator, create_time,
    updater, update_time, deleted, tenant_id
    from robot_task
    </select>
    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from robot_task
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="montageTask != null">
                and montage_task = #{montageTask}
            </if>
            <if test="montageNumber != null">
                and montage_number = #{montageNumber}
            </if>
            <if test="skuInfo != null and skuInfo != ''">
                and sku_info = #{skuInfo}
            </if>
            <if test="skuBatch != null and skuBatch != ''">
                and sku_batch = #{skuBatch}
            </if>
            <if test="skuNumber != null">
                and sku_number = #{skuNumber}
            </if>
            <if test="priority != null">
                and priority = #{priority}
            </if>
            <if test="otherMsg != null and otherMsg != ''">
                and other_msg = #{otherMsg}
            </if>
            <if test="doCycle != null">
                and do_cycle = #{doCycle}
            </if>
            <if test="doMoveAll != null">
                and do_move_all = #{doMoveAll}
            </if>
            <if test="cycleNumber != null">
                and cycle_number = #{cycleNumber}
            </if>
            <if test="remainingCycleNumber != null">
                and remaining_cycle_number = #{remainingCycleNumber}
            </if>
            <if test="taskNo != null and taskNo != ''">
                and task_no = #{taskNo}
            </if>
            <if test="taskStatus != null">
                and task_status = #{taskStatus}
            </if>
            <if test="taskStage != null">
                and task_stage = #{taskStage}
            </if>
            <if test="startTime != null">
                and start_time = #{startTime}
            </if>
            <if test="endTime != null">
                and end_time = #{endTime}
            </if>
            <if test="creator != null and creator != ''">
                and creator = #{creator}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updater != null and updater != ''">
                and updater = #{updater}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
            <if test="deleted != null">
                and deleted = #{deleted}
            </if>
            <if test="tenantId != null">
                and tenant_id = #{tenantId}
            </if>
        </where>
    </select>

    <select id="getUnDoAndDoingTaskIds" resultType="java.lang.Long">
        select
            id
        from
            robot_task
        where
            deleted = '0'
        and montage_task = #{montageTask}
        and task_status in ('0','1')
        <if test= " ids != null and ids.size() > 0">
            AND id not in
            <foreach collection="ids" item="id" index="index" open="(" close=")"
                     separator=",">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectPageList" resultMap="BaseResultMap">
        select
            DISTINCT
            t1.id,
            t1.montage_task,
            t1.montage_number,
            t1.sku_info,
            t1.sku_batch,
            t1.sku_number,
            t1.priority,
            t1.other_msg,
            t1.do_cycle,
            t1.do_move_all,
            t1.cycle_number,
            t1.remaining_cycle_number,
            t1.task_no,
            t1.task_status,
            t1.task_stage,
            t1.start_time,
            t1.end_time,
            t1.create_time
        from
        robot_task t1 left join robot_task_detail t2
        on t1.id = t2.robot_task_id
        <where>
            t1.deleted = '0'
            and t2.deleted = '0'
            <if test="pageReqVO.taskNo != null and pageReqVO.taskNo != ''">
                and t1.task_no like concat('%', #{pageReqVO.taskNo}, '%')
            </if>
            <if test="pageReqVO.taskStatus != null">
                and t1.task_status = #{pageReqVO.taskStatus}
            </if>
            <if test="pageReqVO.taskStage != null">
                and t2.task_stage = #{pageReqVO.taskStage}
            </if>
            <if test="pageReqVO.robotNo != null">
                and t2.robot_no like concat('%', #{pageReqVO.robotNo}, '%')
            </if>
        </where>
        order by t1.create_time desc
    </select>

    <select id="selectLogPageList"
            resultType="cn.iocoder.yudao.module.system.controller.admin.robot.detail.RobotTaskDetailLogResoVO">
        select
            t1.task_no as taskNo,
            t1.create_time as createTime,
            t2.id,
            t2.task_type as taskType,
            t2.task_status as taskStatus,
            t2.start_time as startTime,
            t2.end_time as endTime,
            t2.from_location_no as fromLocationNo,
            t2.to_location_no as toLocationNo,
            t2.task_stage as taskStage
        from
            robot_task t1 inner join  robot_task_detail t2
        on t1.id = t2.robot_task_id
        <where>
            t1.deleted = '0'
            and t2.deleted = '0'
            <if test="pageReqVO.taskNo != null and pageReqVO.taskNo != ''">
                and t1.task_no like concat('%', #{pageReqVO.taskNo}, '%')
            </if>
            <if test="pageReqVO.taskType != null and pageReqVO.taskType != ''">
                and t2.task_type = #{pageReqVO.taskType}
            </if>
            <if test="pageReqVO.taskStatus != null">
                and t2.task_status = #{pageReqVO.taskStatus}
            </if>
            <if test="pageReqVO.msg != null and pageReqVO.msg != ''">
                and (t2.from_location_no like concat('%', #{pageReqVO.msg}, '%') or t2.to_location_no like concat('%', #{pageReqVO.msg}, '%'))
            </if>
            <if test="pageReqVO.startTime!=null ">
                AND t1.create_time >= #{pageReqVO.startTime}
            </if>
            <if test="pageReqVO.endTime!=null ">
                AND t1.create_time &lt;= #{pageReqVO.endTime}
            </if>
            <if test="pageReqVO.taskStage != null">
                and t2.task_stage = #{pageReqVO.taskStage}
            </if>
        </where>
        order by t1.create_time desc
    </select>

    <select id="selectDoingTaskByRobotNo"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDO">
        select
            DISTINCT
            t1.*
        from
        robot_task t1 left join robot_task_detail t2
        on t1.id = t2.robot_task_id
        where
            t1.deleted = '0'
            and t2.deleted = '0'
            and t2.robot_no = #{robotNo}
            and t1.task_status in ('0','1')
            and t2.task_status in ('0','1')
    </select>


    <!--新增所有列-->
    <insert id="insertEntity" keyProperty="id" useGeneratedKeys="true">
        insert into robot_task(montage_task, montage_number, sku_info, sku_batch, sku_number, priority, other_msg,
                               do_cycle, do_move_all, cycle_number, remaining_cycle_number, task_no, task_status,
                               task_stage, start_time, end_time, creator, create_time, updater, update_time, deleted,
                               tenant_id)
        values (#{montageTask}, #{montageNumber}, #{skuInfo}, #{skuBatch}, #{skuNumber}, #{priority}, #{otherMsg},
                #{doCycle}, #{doMoveAll}, #{cycleNumber}, #{remainingCycleNumber}, #{taskNo}, #{taskStatus},
                #{taskStage}, #{startTime}, #{endTime}, #{creator}, #{createTime}, #{updater}, #{updateTime},
                #{deleted}, #{tenantId})
    </insert>

    <insert id="insertBatchList" keyProperty="id" useGeneratedKeys="true">
        insert into robot_task(montage_task, montage_number, sku_info, sku_batch, sku_number, priority, other_msg,
        do_cycle, do_move_all, cycle_number, remaining_cycle_number, task_no, task_status, task_stage, start_time,
        end_time, creator, create_time, updater, update_time, deleted, tenant_id)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.montageTask}, #{entity.montageNumber}, #{entity.skuInfo}, #{entity.skuBatch}, #{entity.skuNumber},
            #{entity.priority}, #{entity.otherMsg}, #{entity.doCycle}, #{entity.doMoveAll}, #{entity.cycleNumber},
            #{entity.remainingCycleNumber}, #{entity.taskNo}, #{entity.taskStatus}, #{entity.taskStage},
            #{entity.startTime}, #{entity.endTime}, #{entity.creator}, #{entity.createTime}, #{entity.updater},
            #{entity.updateTime}, #{entity.deleted}, #{entity.tenantId})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into robot_task(montage_task, montage_number, sku_info, sku_batch, sku_number, priority, other_msg,
        do_cycle, do_move_all, cycle_number, remaining_cycle_number, task_no, task_status, task_stage, start_time,
        end_time, creator, create_time, updater, update_time, deleted, tenant_id)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.montageTask}, #{entity.montageNumber}, #{entity.skuInfo}, #{entity.skuBatch}, #{entity.skuNumber},
            #{entity.priority}, #{entity.otherMsg}, #{entity.doCycle}, #{entity.doMoveAll}, #{entity.cycleNumber},
            #{entity.remainingCycleNumber}, #{entity.taskNo}, #{entity.taskStatus}, #{entity.taskStage},
            #{entity.startTime}, #{entity.endTime}, #{entity.creator}, #{entity.createTime}, #{entity.updater},
            #{entity.updateTime}, #{entity.deleted}, #{entity.tenantId})
        </foreach>
        on duplicate key update
        montage_task = values(montage_task),
        montage_number = values(montage_number),
        sku_info = values(sku_info),
        sku_batch = values(sku_batch),
        sku_number = values(sku_number),
        priority = values(priority),
        other_msg = values(other_msg),
        do_cycle = values(do_cycle),
        do_move_all = values(do_move_all),
        cycle_number = values(cycle_number),
        remaining_cycle_number = values(remaining_cycle_number),
        task_no = values(task_no),
        task_status = values(task_status),
        task_stage = values(task_stage),
        start_time = values(start_time),
        end_time = values(end_time),
        creator = values(creator),
        create_time = values(create_time),
        updater = values(updater),
        update_time = values(update_time),
        deleted = values(deleted),
        tenant_id = values(tenant_id)
    </insert>

    <!--通过主键修改数据-->
    <update id="updateRobot">
        update robot_task
        <set>
            <if test="montageTask != null">
                montage_task = #{montageTask},
            </if>
            <if test="montageNumber != null">
                montage_number = #{montageNumber},
            </if>
            <if test="skuInfo != null and skuInfo != ''">
                sku_info = #{skuInfo},
            </if>
            <if test="skuBatch != null and skuBatch != ''">
                sku_batch = #{skuBatch},
            </if>
            <if test="skuNumber != null">
                sku_number = #{skuNumber},
            </if>
            <if test="priority != null">
                priority = #{priority},
            </if>
            <if test="otherMsg != null and otherMsg != ''">
                other_msg = #{otherMsg},
            </if>
            <if test="doCycle != null">
                do_cycle = #{doCycle},
            </if>
            <if test="doMoveAll != null">
                do_move_all = #{doMoveAll},
            </if>
            <if test="cycleNumber != null">
                cycle_number = #{cycleNumber},
            </if>
            <if test="remainingCycleNumber != null">
                remaining_cycle_number = #{remainingCycleNumber},
            </if>
            <if test="taskNo != null and taskNo != ''">
                task_no = #{taskNo},
            </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>
    <update id="updateDoneByIds">
        update
            robot_task
        set
            task_status = '2',
            task_stage = '5'
        where
            id in
        <foreach collection="ids" item="id" index="index" open="(" close=")"
                 separator=",">
            #{id}
        </foreach>
    </update>

    <update id="setTaskUnDo">
        update
            robot_task
        set
            task_status = '0',
            task_stage = '0'
        where
        id in
        <foreach collection="ids" item="id" index="index" open="(" close=")"
                 separator=",">
            #{id}
        </foreach>
    </update>

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

</mapper>