<?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.ruoyi.mapper.ReviewSceneChangeTaskMapper">

    <resultMap id="BaseResultMap" type="com.ruoyi.domain.ReviewSceneChangeTask">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="taskNo" column="no" jdbcType="VARCHAR"/>
            <result property="chapter" column="chapter" jdbcType="VARCHAR"/>
            <result property="standard" column="standard" jdbcType="VARCHAR"/>
            <result property="keypoint" column="keypoint" jdbcType="VARCHAR"/>
            <result property="details" column="details" jdbcType="VARCHAR"/>
            <result property="operation" column="operation" jdbcType="VARCHAR"/>
            <result property="sort" column="sort" jdbcType="VARCHAR"/>
            <result property="operationSort" column="operation_sort" jdbcType="VARCHAR"/>
            <result property="oldText" column="old_text" jdbcType="VARCHAR"/>
            <result property="newText" column="new_text" jdbcType="VARCHAR"/>
            <result property="taskInitiator" column="task_initiator" jdbcType="VARCHAR"/>
            <result property="taskInitiatorDept" column="task_initiator_dept" jdbcType="VARCHAR"/>
            <result property="taskStatus" column="task_status" jdbcType="VARCHAR"/>
            <result property="taskResult" column="task_result" jdbcType="VARCHAR"/>
            <result property="taskBeginTime" column="task_begin_time" jdbcType="TIMESTAMP"/>
            <result property="taskEndTime" column="task_end_time" jdbcType="TIMESTAMP"/>
            <result property="inspectStandardId" column="inspect_standard_id" jdbcType="BIGINT"/>
            <result property="inspectStandardNo" column="inspect_standard_no" jdbcType="VARCHAR"/>
            <result property="innovationBaseResult" column="innovation_base_result" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="ResponseResultMap" type="com.ruoyi.web.response.ReviewSceneChangeTaskResponse">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="taskNo" column="no" jdbcType="VARCHAR"/>
        <result property="chapter" column="chapter" jdbcType="VARCHAR"/>
        <result property="standard" column="standard" jdbcType="VARCHAR"/>
        <result property="keypoint" column="keypoint" jdbcType="VARCHAR"/>
        <result property="details" column="details" jdbcType="VARCHAR"/>
        <result property="operation" column="operation" jdbcType="VARCHAR"/>
        <result property="sort" column="sort" jdbcType="VARCHAR"/>
        <result property="operationSort" column="operation_sort" jdbcType="VARCHAR"/>
        <result property="oldText" column="old_text" jdbcType="VARCHAR"/>
        <result property="newText" column="new_text" jdbcType="VARCHAR"/>
        <result property="taskInitiator" column="task_initiator" jdbcType="VARCHAR"/>
        <result property="taskInitiatorDept" column="task_initiator_dept" jdbcType="VARCHAR"/>
        <result property="taskStatus" column="task_status" jdbcType="VARCHAR"/>
        <result property="taskResult" column="task_result" jdbcType="VARCHAR"/>
        <result property="taskBeginTime" column="task_begin_time" jdbcType="TIMESTAMP"/>
        <result property="taskEndTime" column="task_end_time" jdbcType="TIMESTAMP"/>
        <result property="decision" column="decision" jdbcType="VARCHAR"/>
        <result property="inspectStandardId" column="inspect_standard_id" jdbcType="BIGINT"/>
        <result property="inspectStandardNo" column="inspect_standard_no" jdbcType="VARCHAR"/>
        <result property="innovationBaseResult" column="innovation_base_result" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="findLastChangeByTaskId" resultType="com.ruoyi.domain.ReviewSceneChangeTask">
        SELECT * FROM t_review_scene_change_task WHERE id IN
           (
               SELECT MAX(id) FROM t_review_scene_change_task WHERE task_status = 'FINISH' AND task_result = 'PASS' AND id IN (SELECT review_scene_change_task_id FROM t_review_task_inbox WHERE review_task_id = #{taskId} AND is_confirm = 0) GROUP BY review_scene_id
           )
    </select>

    <select id="findPendingListByUserId" resultType="com.ruoyi.web.response.ReviewSceneChangeTaskResponse">
        SELECT sct.*, sctu.decision FROM t_review_scene_change_task sct
        LEFT JOIN t_review_scene_change_task_user_relation sctu ON sct.id = sctu.task_id
        WHERE sctu.user_id = #{userId} and sctu.decision IS NULL and sctu.role_id = 103
        /*and sct.innovation_base_result = 'PASS'*/
        <if test="request.operationSort != null and request.operationSort != ''">
            and sct.operation_sort = #{request.operationSort}
        </if>
        <if test="request.type != null and request.type != ''">
            and sct.type = #{request.type}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and (sct.old_text like concat('%',#{request.keyword},'%') || sct.new_text like concat('%',#{request.keyword},'%'))
        </if>
        <if test="request.inspectStandardId != null and request.inspectStandardId != ''">
            and inspect_standard_id = #{request.inspectStandardId}
        </if>
        ORDER BY task_begin_time ASC
    </select>

    <select id="findFinishListByUserId" resultType="com.ruoyi.web.response.ReviewSceneChangeTaskResponse">
        SELECT sct.*, sctu.decision FROM t_review_scene_change_task sct
        LEFT JOIN t_review_scene_change_task_user_relation sctu ON sct.id = sctu.task_id
        WHERE sctu.user_id = #{userId} and sctu.decision IS NOT NULL and sctu.role_id = 103
        /*and sct.innovation_base_result = 'PASS'*/
        <if test="request.operationSort != null and request.operationSort != ''">
            and sct.operation_sort = #{request.operationSort}
        </if>
        <if test="request.type != null and request.type != ''">
            and sct.type = #{request.type}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and (sct.old_text like concat('%',#{request.keyword},'%') || sct.new_text like concat('%',#{request.keyword},'%'))
        </if>
        <if test="request.inspectStandardId != null and request.inspectStandardId != ''">
            and inspect_standard_id = #{request.inspectStandardId}
        </if>
        ORDER BY task_begin_time ASC
    </select>

    <select id="findCreateListByUserId" resultType="com.ruoyi.domain.ReviewSceneChangeTask">
        SELECT * FROM t_review_scene_change_task
        WHERE task_initiator_id = #{userId} /*and innovation_base_result = 'PASS'*/
        <if test="request.operationSort != null and request.operationSort != ''">
            and operation_sort = #{request.operationSort}
        </if>
        <if test="request.type != null and request.type != ''">
            and type = #{request.type}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and (old_text like concat('%',#{request.keyword},'%') || new_text like concat('%',#{request.keyword},'%'))
        </if>
        <if test="request.taskStatus != null and request.taskStatus != ''">
            and task_status = #{request.taskStatus}
        </if>
        <if test="request.inspectStandardId != null and request.inspectStandardId != ''">
            and inspect_standard_id = #{request.inspectStandardId}
        </if>
        ORDER BY task_begin_time ASC
    </select>

    <select id="findSceneChangeTaskById" resultType="com.ruoyi.domain.ReviewSceneChangeTask">
        select * from t_review_scene_change_task where id = #{id}
    </select>

    <select id="findPendingList" resultType="com.ruoyi.web.response.ReviewSceneChangeTaskResponse">
        SELECT sct.*, sctu.decision FROM t_review_scene_change_task sct
        LEFT JOIN t_review_scene_change_task_user_relation sctu ON sct.id = sctu.task_id
        WHERE sctu.user_id = #{userId} and sctu.decision IS NULL
        <if test="request.operationSort != null and request.operationSort != ''">
            and sct.operation_sort = #{request.operationSort}
        </if>
        <if test="request.type != null and request.type != ''">
            and sct.type = #{request.type}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and (sct.old_text like concat('%',#{request.keyword},'%') || sct.new_text like concat('%',#{request.keyword},'%'))
        </if>
        <if test="request.inspectStandardId != null and request.inspectStandardId != ''">
            and inspect_standard_id = #{request.inspectStandardId}
        </if>
    </select>

    <select id="findFinishList" resultType="com.ruoyi.web.response.ReviewSceneChangeTaskResponse">
        SELECT sct.*, sctu.decision FROM t_review_scene_change_task sct
        LEFT JOIN t_review_scene_change_task_user_relation sctu ON sct.id = sctu.task_id
        WHERE sctu.user_id = #{userId} and sctu.decision IS NOT NULL
        <if test="request.operationSort != null and request.operationSort != ''">
            and sct.operation_sort = #{request.operationSort}
        </if>
        <if test="request.type != null and request.type != ''">
            and sct.type = #{request.type}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and (sct.old_text like concat('%',#{request.keyword},'%') || sct.new_text like concat('%',#{request.keyword},'%'))
        </if>
        <if test="request.inspectStandardId != null and request.inspectStandardId != ''">
            and inspect_standard_id = #{request.inspectStandardId}
        </if>
    </select>
    <select id="selectReviewerListByTaskId" resultType="com.ruoyi.web.response.ReviewerListResponse">
        select name as reviewer
        from t_review_scene_change_task_user_relation
        where task_id = #{id}
    </select>
    <select id="getReviewerOpinion" resultType="com.ruoyi.web.response.ReviewOpinionResponse">
        select name,comment,decision,decision_time
        from t_review_scene_change_task_user_relation
        where task_id = #{id}
    </select>

</mapper>