<?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.rc.mapper.TOpenItemMapper">
    
    <resultMap type="TOpenItem" id="TOpenItemResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="questionnaireId"    column="questionnaire_id"    />
        <result property="description"    column="description"    />
        <result property="level"    column="level"    />
        <result property="openTime"    column="open_time"    />
        <result property="closeTime"    column="close_time"    />
        <result property="status"    column="status"    />
        <result property="personInCharge"    column="person_in_charge"    />
        <result property="reviewer"    column="reviewer"    />
        <result property="result"    column="result"    />
        <result property="deadline"    column="deadline"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
        <result property="personInChargeName"    column="person_in_charge_name"    />
        <result property="reviewerName"    column="reviewer_name"    />
        <result property="apNo"    column="ap_no"    />
        <result property="processId"    column="process_id"    />
    </resultMap>

    <sql id="selectTOpenItemVo">
        select id, type, questionnaire_id, description, level, open_time, close_time, d.status, person_in_charge, reviewer, result, deadline, remarks, d.dept_id,
        d.ap_no, d.process_id,
        u1.nick_name as person_in_charge_name, u2.nick_name as reviewer_name
        from t_open_item as d
        left join sys_user u1 on d.person_in_charge = u1.user_id
        left join sys_user u2 on d.reviewer = u2.user_id
    </sql>

    <select id="selectTOpenItemList" parameterType="TOpenItem" resultMap="TOpenItemResult">
        <include refid="selectTOpenItemVo"/>
        <where>  
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="questionnaireId != null "> and questionnaire_id = #{questionnaireId}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="level != null  and level != ''"> and level = #{level}</if>
            <if test="openTime != null "> and open_time = #{openTime}</if>
            <if test="closeTime != null "> and close_time = #{closeTime}</if>
            <if test="status != null  and status != ''"> and d.status = #{status}</if>
            <if test="personInCharge != null "> and person_in_charge = #{personInCharge}</if>
            <if test="reviewer != null "> and reviewer = #{reviewer}</if>
            <if test="result != null  and result != ''"> and result = #{result}</if>
            <if test="deadline != null "> and deadline = #{deadline}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="deptId != null  and deptId != ''"> and d.dept_id = #{deptId}</if>
            <if test="apNo != null "> and p.ap_no = #{apNo}</if>
            <if test="processId != null "> and p.process_id = #{processId}</if>
            ${params.dataScope}
        </where>
    </select>
    
    <select id="selectTOpenItemById" parameterType="Long" resultMap="TOpenItemResult">
        <include refid="selectTOpenItemVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTOpenItem" parameterType="TOpenItem" useGeneratedKeys="true" keyProperty="id">
        insert into t_open_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="questionnaireId != null">questionnaire_id,</if>
            <if test="description != null">description,</if>
            <if test="level != null">level,</if>
            <if test="openTime != null">open_time,</if>
            <if test="closeTime != null">close_time,</if>
            <if test="status != null">status,</if>
            <if test="personInCharge != null">person_in_charge,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="result != null">result,</if>
            <if test="deadline != null">deadline,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="apNo != null">ap_no,</if>
            <if test="processId != null">process_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="questionnaireId != null">#{questionnaireId},</if>
            <if test="description != null">#{description},</if>
            <if test="level != null">#{level},</if>
            <if test="openTime != null">#{openTime},</if>
            <if test="closeTime != null">#{closeTime},</if>
            <if test="status != null">#{status},</if>
            <if test="personInCharge != null">#{personInCharge},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="result != null">#{result},</if>
            <if test="deadline != null">#{deadline},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="apNo != null">#{apNo},</if>
            <if test="processId != null">#{processId},</if>
         </trim>
    </insert>

    <update id="updateTOpenItem" parameterType="TOpenItem">
        update t_open_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="questionnaireId != null">questionnaire_id = #{questionnaireId},</if>
            <if test="description != null">description = #{description},</if>
            <if test="level != null">level = #{level},</if>
            <if test="openTime != null">open_time = #{openTime},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="personInCharge != null">person_in_charge = #{personInCharge},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="result != null">result = #{result},</if>
            <if test="deadline != null">deadline = #{deadline},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="apNo != null">ap_no = #{apNo},</if>
            <if test="processId != null">process_id = #{processId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTOpenItemById" parameterType="Long">
        delete from t_open_item where id = #{id}
    </delete>

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