<?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.InspectionWaypointMapper">
    
    <resultMap type="InspectionWaypoint" id="InspectionWaypointResult">
        <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="waypointName"    column="waypoint_name"    />
        <result property="enabled"    column="enabled"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="detectItemId"    column="detect_item_id"    />
        <result property="mapId" column="map_id" />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionWaypointVo" id="InspectionWaypointVoResult">
        <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="waypointName"    column="waypoint_name"    />
        <result property="enabled"    column="enabled"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="detectItemId"    column="detect_item_id"    />
        <result property="configStatus" column="config_status" />
        <result property="mapId" column="map_id" />
        <result property="mapName" column="map_name" />
        <result property="createByName" column="create_by_name" />
        <result property="updateByName" column="update_by_name" />
    </resultMap>

    <sql id="selectInspectionWaypointVo">
        select id, create_by, create_time, update_by, update_time, remark, waypoint_name, enabled, sort_order, detect_item_id, map_id from inspection_waypoint
    </sql>

    <select id="selectInspectionWaypointById" parameterType="String" resultMap="InspectionWaypointResult">
        <include refid="selectInspectionWaypointVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionWaypoint" parameterType="InspectionWaypoint">
        insert into inspection_waypoint
        <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="waypointName != null">waypoint_name,</if>
            <if test="enabled != null">enabled,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="detectItemId != null">detect_item_id,</if>
            <if test="mapId != null">map_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="waypointName != null">#{waypointName},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="detectItemId != null">#{detectItemId},</if>
            <if test="mapId != null">#{mapId},</if>
         </trim>
    </insert>

    <update id="updateInspectionWaypoint" parameterType="InspectionWaypoint">
        update inspection_waypoint
        <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="waypointName != null">waypoint_name = #{waypointName},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="detectItemId != null">detect_item_id = #{detectItemId},</if>
            <if test="mapId != null">map_id = #{mapId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionWaypointById" parameterType="String">
        delete from inspection_waypoint where id = #{id}
    </delete>

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

    <select id="selectInspectionWaypointVoList" parameterType="InspectionWaypoint" resultMap="InspectionWaypointVoResult">
        select w.id, w.create_by, w.create_time, w.update_by, w.update_time, w.remark,
               w.waypoint_name, w.enabled, w.sort_order, w.detect_item_id, w.map_id, m.map_name as map_name,
               d.is_deploy as config_status,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_waypoint w
        left join te_detection_item d on w.detect_item_id = d.id
        left join inspection_map m on w.map_id = m.id
        left join sys_user u1 on w.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on w.update_by = u2.user_name <!-- 更新人关联用户 -->
        <where>
            <if test="waypointName != null and waypointName != ''"> and w.waypoint_name like concat('%', #{waypointName}, '%')</if>
            <if test="enabled != null and enabled != ''"> and w.enabled = #{enabled}</if>
            <if test="mapId != null and mapId != ''"> and w.map_id = #{mapId}</if>
        </where>
        order by w.sort_order
    </select>
</mapper>
