<?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.aima.mapper.AimaTaskMapper">
    <resultMap type="AimaTask" id="AimaTaskResult">
        <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="phoneId" column="phone_id"/>
        <result property="vehicleId" column="vehicle_id"/>
        <result property="testCaseCount" column="test_case_count"/>
        <result property="taskConfigId" column="task_config_id"/>
    </resultMap>

    <resultMap type="com.cmvr.aima.domain.vo.AimaTaskVo" id="AimaTaskVoResult">
        <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="phoneId" column="phone_id"/>
        <result property="vehicleId" column="vehicle_id"/>
        <result property="testCaseCount" column="test_case_count"/>
        <result property="taskConfigId" column="task_config_id"/>
        <result property="phoneName" column="phone_name"/>
        <result property="vin" column="vin"/>
        <result property="createByName" column="create_by_name"/>
        <result property="updateByName" column="update_by_name"/>
    </resultMap>

    <sql id="selectAimaTaskVo">
        select id, create_by, create_time, update_by, update_time, remark, task_code, task_name, task_type, phone_id, vehicle_id, test_case_count, task_config_id from aima_task
    </sql>

    <select id="selectAimaTaskList" parameterType="AimaTask" resultMap="AimaTaskResult">
        <include refid="selectAimaTaskVo"/>
        <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>
            <if test="phoneId != null and phoneId != ''"> and phone_id = #{phoneId}</if>
            <if test="vehicleId != null and vehicleId != ''"> and vehicle_id = #{vehicleId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAimaTaskById" parameterType="String" resultMap="AimaTaskResult">
        <include refid="selectAimaTaskVo"/> where id = #{id}
    </select>

    <select id="selectAimaTaskVoList" parameterType="AimaTask" resultMap="AimaTaskVoResult">
        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.phone_id, t.vehicle_id,
               t.test_case_count, t.task_config_id,
               p.phone_name, v.vin,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from aima_task t
        left join aima_phone p on t.phone_id = p.id
        left join aima_vehicle v on t.vehicle_id = v.id
        left join sys_user u1 on t.create_by = u1.user_name
        left join sys_user u2 on t.update_by = u2.user_name
        <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="phoneId != null and phoneId != ''"> and t.phone_id = #{phoneId}</if>
            <if test="vehicleId != null and vehicleId != ''"> and t.vehicle_id = #{vehicleId}</if>
        </where>
        order by t.create_time desc
    </select>

    <insert id="insertAimaTask" parameterType="AimaTask">
        insert into aima_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="phoneId != null">phone_id,</if><if test="vehicleId != null">vehicle_id,</if>
            <if test="testCaseCount != null">test_case_count,</if><if test="taskConfigId != null">task_config_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="taskCode != null">#{taskCode},</if><if test="taskName != null">#{taskName},</if><if test="taskType != null">#{taskType},</if>
            <if test="phoneId != null">#{phoneId},</if><if test="vehicleId != null">#{vehicleId},</if>
            <if test="testCaseCount != null">#{testCaseCount},</if><if test="taskConfigId != null">#{taskConfigId},</if>
        </trim>
    </insert>

    <update id="updateAimaTask" parameterType="AimaTask">
        update aima_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="phoneId != null">phone_id = #{phoneId},</if>
            <if test="vehicleId != null">vehicle_id = #{vehicleId},</if>
            <if test="testCaseCount != null">test_case_count = #{testCaseCount},</if><if test="taskConfigId != null">task_config_id = #{taskConfigId},</if>
        </trim> where id = #{id}
    </update>

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