<?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.cmvr.inspection.mapper.InspectionRobotMapper">
    
    <resultMap type="InspectionRobot" id="InspectionRobotResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="robotCode"    column="robot_code"    />
        <result property="robotName"    column="robot_name"    />
        <result property="robotModel"    column="robot_model"    />
        <result property="robotType"    column="robot_type"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="port"    column="port"    />
        <result property="currentMapId"    column="current_map_id"    />
        <result property="status"    column="status"    />
        <result property="batteryLevel"    column="battery_level"    />
        <result property="currentPosition"    column="current_position"    />
        <result property="terminalId"    column="terminal_id"    />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionRobotVo" id="InspectionRobotVoResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="robotCode"    column="robot_code"    />
        <result property="robotName"    column="robot_name"    />
        <result property="robotModel"    column="robot_model"    />
        <result property="robotType"    column="robot_type"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="port"    column="port"    />
        <result property="currentMapId"    column="current_map_id"    />
        <result property="currentMapName"    column="current_map_name"    />
        <result property="robotMapName" column="robot_map_name" />
        <result property="status"    column="status"    />
        <result property="batteryLevel"    column="battery_level"    />
        <result property="currentPosition"    column="current_position"    />
        <result property="createByName"    column="create_by_name"    />
        <result property="updateByName"    column="update_by_name"    />
        <result property="terminalId"    column="terminal_id"    />
    </resultMap>

    <sql id="selectInspectionRobotVo">
        select id, create_by, create_time, update_by, update_time, remark, robot_code, robot_name, robot_model, robot_type, ip_address, port, current_map_id, status, battery_level, current_position, terminal_id from inspection_robot
    </sql>

    <select id="selectInspectionRobotList" parameterType="InspectionRobot" resultMap="InspectionRobotResult">
        <include refid="selectInspectionRobotVo"/>
        <where>  
            <if test="robotCode != null  and robotCode != ''"> and robot_code = #{robotCode}</if>
            <if test="robotName != null  and robotName != ''"> and robot_name like concat('%', #{robotName}, '%')</if>
            <if test="robotModel != null  and robotModel != ''"> and robot_model = #{robotModel}</if>
            <if test="robotType != null  and robotType != ''"> and robot_type = #{robotType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="currentMapId != null  and currentMapId != ''"> and current_map_id = #{currentMapId}</if>
        </where>
    </select>
    
    <select id="selectInspectionRobotById" parameterType="String" resultMap="InspectionRobotResult">
        <include refid="selectInspectionRobotVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionRobot" parameterType="InspectionRobot">
        insert into inspection_robot
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="robotCode != null">robot_code,</if>
            <if test="robotName != null">robot_name,</if>
            <if test="robotModel != null">robot_model,</if>
            <if test="robotType != null">robot_type,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="port != null">port,</if>
            <if test="currentMapId != null">current_map_id,</if>
            <if test="status != null">status,</if>
            <if test="batteryLevel != null">battery_level,</if>
            <if test="currentPosition != null">current_position,</if>
            <if test="terminalId != null">terminal_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="robotCode != null">#{robotCode},</if>
            <if test="robotName != null">#{robotName},</if>
            <if test="robotModel != null">#{robotModel},</if>
            <if test="robotType != null">#{robotType},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="port != null">#{port},</if>
            <if test="currentMapId != null">#{currentMapId},</if>
            <if test="status != null">#{status},</if>
            <if test="batteryLevel != null">#{batteryLevel},</if>
            <if test="currentPosition != null">#{currentPosition},</if>
            <if test="terminalId != null">#{terminalId},</if>
         </trim>
    </insert>

    <update id="updateInspectionRobot" parameterType="InspectionRobot">
        update inspection_robot
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="robotCode != null">robot_code = #{robotCode},</if>
            <if test="robotName != null">robot_name = #{robotName},</if>
            <if test="robotModel != null">robot_model = #{robotModel},</if>
            <if test="robotType != null">robot_type = #{robotType},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="port != null">port = #{port},</if>
            <if test="currentMapId != null">current_map_id = #{currentMapId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="batteryLevel != null">battery_level = #{batteryLevel},</if>
            <if test="currentPosition != null">current_position = #{currentPosition},</if>
            <if test="terminalId != null">terminal_id = #{terminalId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionRobotById" parameterType="String">
        delete from inspection_robot where id = #{id}
    </delete>

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

    <select id="selectInspectionRobotVoList" parameterType="InspectionRobot" resultMap="InspectionRobotVoResult">
        select r.id, r.create_by, r.create_time, r.update_by, r.update_time, r.remark,
               r.robot_code, r.robot_name, r.robot_model, r.robot_type,
               r.ip_address, r.port, r.current_map_id,
               m.map_name as current_map_name,
               r.status, r.battery_level, r.current_position, r.terminal_id,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name, m.map_source_name as robot_map_name
        from inspection_robot r
        left join inspection_map m on r.current_map_id = m.id
        left join sys_user u1 on r.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on r.update_by = u2.user_name <!-- 更新人关联用户 -->
        <where>
            <if test="robotCode != null and robotCode != ''"> and r.robot_code = #{robotCode}</if>
            <if test="robotName != null and robotName != ''"> and r.robot_name like concat('%', #{robotName}, '%')</if>
            <if test="robotModel != null and robotModel != ''"> and r.robot_model = #{robotModel}</if>
            <if test="robotType != null and robotType != ''"> and r.robot_type = #{robotType}</if>
            <if test="status != null and status != ''"> and r.status = #{status}</if>
            <if test="currentMapId != null and currentMapId != ''"> and r.current_map_id = #{currentMapId}</if>
            <if test="ipAddress != null and ipAddress != ''"> and r.ip_address = #{ipAddress}</if>
        </where>
    </select>
</mapper>
