<?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.positionmap.PositionMapItemMapper">

    <!--
        一般情况下，尽可能使用 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.positionmap.PositionMapItemDO">
        <!--@Table ware_position_map_item-->
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="positionMapId" column="position_map_id" jdbcType="INTEGER"/>
        <result property="areaId" column="area_id" jdbcType="INTEGER"/>
        <result property="laneId" column="lane_id" jdbcType="INTEGER"/>
        <result property="locationX" column="location_x" jdbcType="VARCHAR"/>
        <result property="locationY" column="location_y" jdbcType="VARCHAR"/>
        <result property="actualLocationX" column="actual_location_x" jdbcType="VARCHAR"/>
        <result property="actualLocationY" column="actual_location_y" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="dataJson" column="data_json" jdbcType="VARCHAR"/>
        <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,
               position_map_id,
               area_id,
               lane_id,
               location_x,
               location_y,
               actual_location_x,
               actual_location_y,
               type,
               data_json,
               creator,
               create_time,
               updater,
               update_time,
               deleted,
               tenant_id
    </sql>

    <select id="selectByLocationId" resultType="cn.iocoder.yudao.module.mqtt.api.task.dto.Pose2ds">
        select
            t1.location_x as x,
            t1.location_y as y,
            t2.location_yaw as yaw
        from
            ware_position_map_item t1 ,ware_house_location t2
        where t1.id = t2.map_item_id
        and t2.id = #{locationId}
          and t1.deleted = '0'
          and t2.deleted = '0'
    </select>

    <select id="getAllByMapId" resultType="cn.iocoder.yudao.module.mqtt.api.path.dto.PositionMapItemSynDTO">
        select
            id,
            actual_location_x as x,
            actual_location_y as y,
            location_yaw as yaw,
            sort_num as sortNum,
            type
        from
            ware_position_map_item
        where
            position_map_id = #{positionMapId}
        and deleted = '0'
    </select>

    <select id="selectInWaitList"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapItemDO">
        select
            <include refid="base_sql"></include>
        from
            ware_position_map_item
        where (position_map_id, actual_location_x,actual_location_y,type) IN
        <foreach collection="list" item="item" open="(" separator=")," close=")">
            (#{item.positionMapId}, #{item.actualLocationX}, #{item.actualLocationY}, #{item.type})
        </foreach>
    </select>
    <select id="selectSortNumByIds" resultType="cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapItemDO">
        select
            id,
            sort_num as sortNum
        from
            ware_position_map_item
        where
            id IN
        <foreach item="id" collection="ids" separator="," open="(" close=")" index="index">
            #{id}
        </foreach>
    </select>


    <!--通过主键修改数据-->
    <update id="update">
        update ware_position_map_item
        <set>
            <if test="positionMapId != null">
                position_map_id = #{positionMapId},
            </if>
            <if test="areaId != null">
                area_id = #{areaId},
            </if>
            <if test="laneId != null">
                lane_id = #{laneId},
            </if>
            <if test="locationX != null and locationX != ''">
                location_x = #{locationX},
            </if>
            <if test="locationY != null and locationY != ''">
                location_y = #{locationY},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="dataJson != null and dataJson != ''">
                data_json = #{dataJson},
            </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>
            <if test="tenantId != null">
                tenant_id = #{tenantId},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="emptyAreaId">
        update ware_position_map_item
        set area_id = null
        where position_map_id = #{mapId}
        and area_id = #{areaId}
    </update>
    <update id="emptyLaneId">
        update ware_position_map_item
        set lane_id = null
        where position_map_id = #{mapId}
        and lane_id = #{laneId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from ware_position_map_item
        where id = #{id}
    </delete>
    <update id="deleteByMapId">
        update ware_position_map_item
        set deleted = 1
        where position_map_id = #{mapId}
    </update>

    <update id="updateRobotNo">
        update
            ware_position_map_item
        set
            robot_no = #{newRobotNo}
        where
            robot_no = #{oldRobotNo}
          and deleted = '0'
    </update>
    <update id="clearRobotNo">
        update
            ware_position_map_item
        set
            robot_no = '',
            use_status = '0'
        where
            robot_no = #{robotNo}
          and deleted = '0'
    </update>

    <update id="setMapItemUse">
        update
            ware_position_map_item
        set
            use_status = '2'
        where
            robot_no = #{robotNo}
          and id = #{id}
          and deleted = '0'
    </update>
</mapper>
