<?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.InspectionMapMapper">
    
    <resultMap type="InspectionMap" id="InspectionMapResult">
        <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="mapName"    column="map_name"    />
        <result property="mapSourceRobotId"    column="map_source_robot_id"    />
        <result property="mapSourceName"    column="map_source_name"    />
        <result property="mapFilePath"    column="map_file_path"    />
        <result property="status"    column="status"    />
        <result property="mapDesc"    column="map_desc"    />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionMapVo" id="InspectionMapVoResult">
        <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="mapName"    column="map_name"    />
        <result property="mapSourceRobotId"    column="map_source_robot_id"    />
        <result property="mapSourceRobotName"    column="map_source_robot_name"    />
        <result property="mapSourceName"    column="map_source_name"    />
        <result property="mapFilePath"    column="map_file_path"    />
        <result property="status"    column="status"    />
        <result property="mapDesc"    column="map_desc"    />
        <result property="createByName"    column="create_by_name"    />
        <result property="updateByName"    column="update_by_name"    />
    </resultMap>

    <sql id="selectInspectionMapVo">
        select id, create_by, create_time, update_by, update_time, remark, map_name, map_source_robot_id, map_source_name, map_file_path, status, map_desc from inspection_map
    </sql>

    <select id="selectInspectionMapList" parameterType="InspectionMap" resultMap="InspectionMapResult">
        <include refid="selectInspectionMapVo"/>
        <where>  
            <if test="mapName != null  and mapName != ''"> and map_name like concat('%', #{mapName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectInspectionMapById" parameterType="String" resultMap="InspectionMapResult">
        <include refid="selectInspectionMapVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionMap" parameterType="InspectionMap">
        insert into inspection_map
        <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="mapName != null">map_name,</if>
            <if test="mapSourceRobotId != null">map_source_robot_id,</if>
            <if test="mapSourceName != null">map_source_name,</if>
            <if test="mapFilePath != null">map_file_path,</if>
            <if test="status != null">status,</if>
            <if test="mapDesc != null">map_desc,</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="mapName != null">#{mapName},</if>
            <if test="mapSourceRobotId != null">#{mapSourceRobotId},</if>
            <if test="mapSourceName != null">#{mapSourceName},</if>
            <if test="mapFilePath != null">#{mapFilePath},</if>
            <if test="status != null">#{status},</if>
            <if test="mapDesc != null">#{mapDesc},</if>
         </trim>
    </insert>

    <update id="updateInspectionMap" parameterType="InspectionMap">
        update inspection_map
        <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="mapName != null">map_name = #{mapName},</if>
            <if test="mapSourceRobotId != null">map_source_robot_id = #{mapSourceRobotId},</if>
            <if test="mapSourceName != null">map_source_name = #{mapSourceName},</if>
            <if test="mapFilePath != null">map_file_path = #{mapFilePath},</if>
            <if test="status != null">status = #{status},</if>
            <if test="mapDesc != null">map_desc = #{mapDesc},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionMapById" parameterType="String">
        delete from inspection_map where id = #{id}
    </delete>

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

    <select id="selectInspectionMapVoList" parameterType="InspectionMap" resultMap="InspectionMapVoResult">
        select m.id, m.create_by, m.create_time, m.update_by, m.update_time, m.remark,
               m.map_name, m.map_source_robot_id, r.robot_name as map_source_robot_name,
               m.map_source_name, m.map_file_path, m.status, m.map_desc,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_map m
        left join inspection_robot r on m.map_source_robot_id = r.id
        left join sys_user u1 on m.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on m.update_by = u2.user_name <!-- 更新人关联用户 -->
        <where>
            <if test="mapName != null and mapName != ''"> and m.map_name like concat('%', #{mapName}, '%')</if>
            <if test="mapDesc != null and mapDesc != ''"> and m.map_desc like concat('%', #{mapDesc}, '%')</if>
            <if test="status != null and status != ''"> and m.status = #{status}</if>
        </where>
    </select>

    <select id="selectInspectionMapVoList" parameterType="InspectionMap" resultMap="InspectionMapVoResult">
        select m.id, m.create_by, m.create_time, m.update_by, m.update_time, m.remark,
        m.map_name, m.map_source_robot_id, r.robot_name as map_source_robot_name,
        m.map_source_name, m.map_file_path, m.status, m.map_desc,
        u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_map m
        left join inspection_robot r on m.map_source_robot_id = r.id
        left join sys_user u1 on m.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on m.update_by = u2.user_name <!-- 更新人关联用户 -->
        <where>
            <if test="mapName != null and mapName != ''"> and m.map_name like concat('%', #{mapName}, '%')</if>
            <if test="mapDesc != null and mapDesc != ''"> and m.map_desc like concat('%', #{mapDesc}, '%')</if>
            <if test="status != null and status != ''"> and m.status = #{status}</if>
        </where>
    </select>

    <select id="selectMapsByRobotId" resultMap="InspectionMapVoResult">
        select m.id, m.create_by, m.create_time, m.update_by, m.update_time, m.remark,
               m.map_name, m.map_source_robot_id, r.robot_name as map_source_robot_name,
               m.map_source_name, m.map_file_path, m.status, m.map_desc,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_map m
                 left join inspection_robot r on m.map_source_robot_id = r.id
                 left join sys_user u1 on m.create_by = u1.user_name
                 left join sys_user u2 on m.update_by = u2.user_name
        where m.map_source_robot_id = #{robotId}
        order by m.create_time desc
    </select>
</mapper>
