<?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.fs.qw.mapper.QwWorkTaskMapper">

    <resultMap type="QwWorkTask" id="QwWorkTaskResult">
        <result property="id"    column="id"    />
        <result property="extId"    column="ext_id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="type"    column="type"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="score"    column="score"    />
        <result property="sopId"    column="sop_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="title"    column="title"    />
    </resultMap>

    <sql id="selectQwWorkTaskVo">
        select id, ext_id, qw_user_id, type,title, status, remark, score, sop_id, company_id, company_user_id, create_time, update_time from qw_work_task
    </sql>

    <select id="selectQwWorkTaskList" parameterType="QwWorkTask" resultMap="QwWorkTaskResult">
        <include refid="selectQwWorkTaskVo"/>
        <where>
            <if test="extId != null "> and ext_id = #{extId}</if>
            <if test="qwUserId != null "> and qw_user_id = #{qwUserId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="sopId != null  and sopId != ''"> and sop_id = #{sopId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
    </select>

    <select id="selectQwWorkTaskById" parameterType="Long" resultMap="QwWorkTaskResult">
        <include refid="selectQwWorkTaskVo"/>
        where id = #{id}
    </select>
    <select id="selectQwWorkTaskListVONew" resultType="com.fs.qw.vo.QwWorkTaskListVO">
        SELECT t.*
        FROM qw_work_task t
        INNER JOIN (
        SELECT t_inner.id
        FROM qw_work_task t_inner
        <where>
            DATE(t_inner.create_time) = CURDATE()
            <if test="extId != null "> and t_inner.ext_id = #{extId}</if>
            <if test="qwUserId != null "> and t_inner.qw_user_id = #{qwUserId}</if>
            <if test="type != null "> and t_inner.type = #{type}</if>
            <if test="status != null "> and t_inner.status = #{status}</if>
            <if test="score != null "> and t_inner.score = #{score}</if>
            <if test="sopId != null  and sopId != ''"> and t_inner.sop_id = #{sopId}</if>
            <if test="companyId != null "> and t_inner.company_id = #{companyId}</if>
            <if test="companyUserId != null "> and t_inner.company_user_id = #{companyUserId}</if>
        </where>
        ORDER BY t_inner.score DESC, t_inner.id DESC
        LIMIT ${(pageNum-1)*pageSize}, ${pageSize}
        ) AS filtered_ids ON t.id = filtered_ids.id
        ORDER BY t.score DESC, t.id DESC
    </select>

    <select id="selectQwWorkTaskListByMap" resultType="com.fs.qw.domain.QwWorkTask">
        select
            qwt.*
        from qw_work_task qwt
        where qwt.qw_user_id = #{params.qwUserId}
          and date(qwt.create_time) = #{params.date}
          and qwt.company_id = #{params.companyId}
          and qwt.company_user_id = #{params.companyUserId}
          and qwt.status = 0
    </select>
    <select id="selectQwWorkTaskListVONewCount" resultType="java.lang.Long">
        select count(1) from qw_work_task t
        <where>
            DATE(t.create_time) = CURDATE()
            <if test="extId != null "> and t.ext_id = #{extId}</if>
            <if test="qwUserId != null "> and t.qw_user_id = #{qwUserId}</if>
            <if test="type != null "> and t.type = #{type}</if>
            <if test="status != null "> and t.status = #{status}</if>
            <if test="score != null "> and t.score = #{score}</if>
            <if test="sopId != null  and sopId != ''"> and t.sop_id = #{sopId}</if>
            <if test="companyId != null "> and t.company_id = #{companyId}</if>
            <if test="companyUserId != null "> and t.company_user_id = #{companyUserId}</if>
        </where>
        order by t.score desc,t.id desc
    </select>

    <insert id="insertQwWorkTask" parameterType="QwWorkTask" useGeneratedKeys="true" keyProperty="id">
        insert into qw_work_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="extId != null">ext_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="type != null">type,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="score != null">score,</if>
            <if test="sopId != null">sop_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="title != null">title,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="extId != null">#{extId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="type != null">#{type},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="score != null">#{score},</if>
            <if test="sopId != null">#{sopId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="title != null">#{title},</if>
         </trim>
    </insert>

    <update id="updateQwWorkTask" parameterType="QwWorkTask">
        update qw_work_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="extId != null">ext_id = #{extId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="score != null">score = #{score},</if>
            <if test="sopId != null">sop_id = #{sopId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="title != null">title = #{title},</if>
        </trim>
        where id = #{id}
    </update>
    <insert id="insertQwWorkTaskBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO qw_work_task (
        ext_id,
        qw_user_id,
        status,
        type,
        title,
        remark,
        score,
        sop_id,
        company_id,
        company_user_id,
        create_time,
        update_time
        )
        VALUES
        <foreach collection="qwWorkTasks" item="log" separator=",">
            (
            #{log.extId},
            #{log.qwUserId},
            #{log.status},
            #{log.type},
            #{log.title},
            #{log.remark},
            #{log.score},
            #{log.sopId},
            #{log.companyId},
            #{log.companyUserId},
            #{log.createTime},
            #{log.updateTime}
            )
        </foreach>
    </insert>
    <delete id="deleteQwWorkTaskById" parameterType="Long">
        delete from qw_work_task where id = #{id}
    </delete>

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