<?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.ycwl.basic.mapper.TaskMapper">
    <insert id="add">
        insert into task(id, worker_id, member_id, face_id, template_id, scenic_id, task_params, video_url, `status`, result, automatic)
        values (#{id}, #{workerId}, #{memberId}, #{faceId}, #{templateId}, #{scenicId}, #{taskParams}, #{videoUrl}, #{status}, #{result}, #{automatic})
    </insert>
    <update id="update">
        update task
        <set>
            <if test="workerId!= null">worker_id = #{workerId}, </if>
            <if test="memberId!= null">member_id = #{memberId}, </if>
            <if test="faceId!= null">face_id = #{faceId}, </if>
            <if test="templateId!= null">template_id = #{templateId}, </if>
            <if test="scenicId!= null">scenic_id = #{scenicId}, </if>
            <if test="taskParams!= null">task_params = #{taskParams}, </if>
            <if test="videoUrl!= null">video_url = #{videoUrl}, </if>
            <if test="status!= null">status = #{status}, </if>
            <if test="result!= null">result = #{result}, </if>
        </set>
            where id = #{id}
    </update>
    <update id="updateStatus">
        update task
        set status = #{status}
        where id = #{id}
    </update>
    <update id="assignToWorker">
        update task
        set worker_id = #{workerId}, status = 2
        where id = #{taskId}
    </update>
    <update id="deassign">
        update task
        set worker_id = null
        where id = #{taskId}
    </update>
    <update id="setStart">
        update task
        set start_time = now(), end_time = null, worker_id = #{workerId}
        where id = #{id}
    </update>
    <update id="setSuccess">
        update task
        set end_time = now(), status = 1, result = #{result}
        where id = #{id}
    </update>
    <update id="setFail">
        update task
        set end_time = now(), status = 3, result = #{result}
        where id = #{id}
    </update>
    <delete id="deleteById">
        delete from task where id = #{id}
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.task.resp.TaskRespVO">
        select id, worker_id, face_id, member_id, template_id, scenic_id, task_params, video_url, `status`, result, create_time, update_time, start_time, end_time
        from task
        <where>
            <if test="workerId!= null">and worker_id = #{workerId} </if>
            <if test="memberId!= null">and member_id = #{memberId} </if>
            <if test="taskParams!= null">and task_params = #{taskParams} </if>
            <if test="faceId!= null">and face_id = #{faceId} </if>
            <if test="templateId!= null">and template_id = #{templateId} </if>
            <if test="scenicId!= null">and scenic_id = #{scenicId} </if>
            <if test="status!= null">and `status` = #{status} </if>
            <if test="startTime!= null">and create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and create_time &lt;= #{endTime} </if>
        </where>
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.task.resp.TaskRespVO">
        select id, worker_id, face_id, member_id, template_id, scenic_id, task_params, video_url, `status`, result, create_time, update_time
        from task
        where id = #{id}
    </select>
    <select id="countByMemberIdStauFinish" resultType="java.lang.Integer">
        select count(1)
        from task
        where member_id = #{userId}
          and status = 1
    </select>
    <select id="selectNotRunning" resultType="com.ycwl.basic.model.pc.task.resp.TaskRespVO">
        select id, worker_id, member_id, template_id, scenic_id, task_params, video_url, `status`, result, create_time, update_time
        from task
        where status = 0 and worker_id is null
        limit 1
    </select>
    <select id="selectAllNotRunning" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select *
        from task
        where status = 0 and worker_id is null
    </select>
    <select id="countTask" resultType="java.lang.Integer">
        select count(1) from task
        <where>
            <if test="workerId!= null">and worker_id = #{workerId} </if>
            <if test="memberId!= null">and member_id = #{memberId} </if>
            <if test="faceId!= null">and face_id = #{faceId} </if>
            <if test="templateId!= null">and template_id = #{templateId} </if>
            <if test="scenicId!= null">and scenic_id = #{scenicId} </if>
            <if test="status!= null">and `status` = #{status} </if>
            <if test="startTime!= null">and create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and create_time &lt;= #{endTime} </if>
        </where>
    </select>
    <select id="getFaceAutomaticTask" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select *
        from task
        where face_id = #{faceId} and automatic = 1
        limit 1
    </select>
    <select id="get" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select *
        from task
        where id = #{id}
    </select>
    <select id="listEntity" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select *
        from task
        <where>
            <if test="workerId!= null">and worker_id = #{workerId} </if>
            <if test="memberId!= null">and member_id = #{memberId} </if>
            <if test="faceId!= null">and face_id = #{faceId} </if>
            <if test="templateId!= null">and template_id = #{templateId} </if>
            <if test="scenicId!= null">and scenic_id = #{scenicId} </if>
            <if test="status!= null">and `status` = #{status} </if>
            <if test="startTime!= null">and create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and create_time &lt;= #{endTime} </if>
        </where>
    </select>
    <select id="selectAllRunning" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select *
        from task
        where status = 2
    </select>
</mapper>
