<?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.InspectionTaskMapper">
    
    <resultMap type="InspectionTask" id="InspectionTaskResult">
        <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="taskCode"    column="task_code"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskType"    column="task_type"    />
        <result property="taskConfigId"    column="task_config_id"    />
        <result property="mapId" column="map_id" />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionTaskVo" id="InspectionTaskVoResult">
        <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="taskCode"    column="task_code"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskType"    column="task_type"    />
        <result property="createByName"    column="create_by_name"    />
        <result property="updateByName"    column="update_by_name"    />
        <result property="taskConfigId"    column="task_config_id"    />
        <result property="mapId" column="map_id" />
        <result property="mapName" column="map_name" />
    </resultMap>

    <sql id="selectInspectionTaskVo">
        select id, create_by, create_time, update_by, update_time, remark, task_code, task_name, task_type, task_config_id, map_id, waypoint_count from inspection_task
    </sql>

    <select id="selectInspectionTaskList" parameterType="InspectionTask" resultMap="InspectionTaskResult">
        <include refid="selectInspectionTaskVo"/>
        <where>  
            <if test="taskCode != null  and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="taskName != null  and taskName != ''"> and task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskType != null  and taskType != ''"> and task_type = #{taskType}</if>
        </where>
    </select>
    
    <select id="selectInspectionTaskById" parameterType="String" resultMap="InspectionTaskResult">
        <include refid="selectInspectionTaskVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionTask" parameterType="InspectionTask">
        insert into inspection_task
        <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="taskCode != null">task_code,</if>
            <if test="taskName != null">task_name,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskConfigId != null">task_config_id,</if>
            <if test="mapId != null">map_id,</if>
            <if test="waypointCount != null">waypoint_count,</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="taskCode != null">#{taskCode},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskConfigId != null">#{taskConfigId},</if>
            <if test="mapId != null">#{mapId},</if>
            <if test="waypointCount != null">#{waypointCount},</if>
         </trim>
    </insert>

    <update id="updateInspectionTask" parameterType="InspectionTask">
        update inspection_task
        <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="taskCode != null">task_code = #{taskCode},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskConfigId != null">task_config_id = #{taskConfigId},</if>
            <if test="mapId != null">map_id = #{mapId},</if>
            <if test="waypointCount != null">waypoint_count = #{waypointCount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionTaskById" parameterType="String">
        delete from inspection_task where id = #{id}
    </delete>

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

    <select id="selectInspectionTaskVoList" parameterType="InspectionTask" resultMap="InspectionTaskVoResult">
        select t.id, t.create_by, t.create_time, t.update_by, t.update_time, t.remark,
               t.task_code, t.task_name, t.task_type, t.task_config_id, t.map_id, t.waypoint_count,
               m.map_name,
               u1.user_name as create_by_name, u2.user_name as update_by_name
        from inspection_task t
        left join sys_user u1 on t.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on t.update_by = u2.user_name <!-- 更新人关联用户 -->
                                      <!-- 任务关联地图 -->
      left join inspection_map m on t.map_id = m.id
        <where>
            <if test="taskCode != null and taskCode != ''"> and t.task_code = #{taskCode}</if>
            <if test="taskName != null and taskName != ''"> and t.task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskType != null and taskType != ''"> and t.task_type = #{taskType}</if>
            <if test="mapId != null and mapId != ''"> and t.map_id = #{mapId}</if>
        </where>
    </select>
</mapper>
