<?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.TQuestionnaireMapper">
    
    <resultMap type="TQuestionnaire" id="TQuestionnaireResult">
        <result property="id"    column="id"    />
        <result property="auditId"    column="audit_id"    />
        <result property="chapterId"    column="chapter_id"    />
        <result property="year"    column="year"    />
        <result property="type"    column="type"    />
        <result property="directory"    column="directory"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="yesNoNa"    column="yes_no_na"    />
        <result property="minimumStandard"    column="minimum_standard"    />
        <result property="goodPractices"    column="good_practices"    />
        <result property="standard"    column="standard"    />
        <result property="completionStatus"    column="completion_status"    />
        <result property="personInCharge"    column="person_in_charge"    />
        <result property="reviewer"    column="reviewer"    />
        <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"    />
    </resultMap>

    <sql id="selectTQuestionnaireVo">
        select id, audit_id, chapter_id, year, type, directory, code, name, yes_no_na, minimum_standard,
        good_practices, standard, completion_status, person_in_charge, reviewer, remarks, q.dept_id,
        SUBSTR(q.directory, 1, INSTR(q.directory, '.') - 1) as codeSubStr,
        u1.nick_name as person_in_charge_name, u2.nick_name as reviewer_name
        from t_questionnaire q
        left join sys_user u1 on q.person_in_charge = u1.user_id
        left join sys_user u2 on q.reviewer = u2.user_id
    </sql>

    <select id="selectTQuestionnaireList" parameterType="TQuestionnaire" resultMap="TQuestionnaireResult">
        <include refid="selectTQuestionnaireVo"/>
        <where>  
            <if test="auditId != null "> and audit_id = #{auditId}</if>
            <if test="chapterId != null "> and chapter_id = #{chapterId}</if>
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="directory != null  and directory != ''"> and directory = #{directory}</if>
            <if test="code != null "> and code = #{code}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="yesNoNa != null  and yesNoNa != ''"> and yes_no_na = #{yesNoNa}</if>
            <if test="minimumStandard != null  and minimumStandard != ''"> and minimum_standard = #{minimumStandard}</if>
            <if test="goodPractices != null  and goodPractices != ''"> and good_practices = #{goodPractices}</if>
            <if test="standard != null  and standard != ''"> and standard = #{standard}</if>
            <if test="completionStatus != null  and completionStatus != ''"> and completion_status = #{completionStatus}</if>
            <if test="personInCharge != null "> and person_in_charge = #{personInCharge}</if>
            <if test="reviewer != null "> and reviewer = #{reviewer}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="deptId != null  and deptId != ''"> and dept_id = #{deptId}</if>
        </where>
        order by (SUBSTR(q.directory, 1, INSTR(q.directory, '.') - 1) + 0) asc
    </select>
    
    <select id="selectTQuestionnaireById" parameterType="Long" resultMap="TQuestionnaireResult">
        <include refid="selectTQuestionnaireVo"/>
        where id = #{id}
    </select>

    <insert id="insertTQuestionnaire" parameterType="TQuestionnaire" useGeneratedKeys="true" keyProperty="id">
        insert into t_questionnaire
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="auditId != null">audit_id,</if>
            <if test="chapterId != null">chapter_id,</if>
            <if test="year != null">year,</if>
            <if test="type != null">type,</if>
            <if test="directory != null">directory,</if>
            <if test="code != null">code,</if>
            <if test="name != null">name,</if>
            <if test="yesNoNa != null">yes_no_na,</if>
            <if test="minimumStandard != null">minimum_standard,</if>
            <if test="goodPractices != null">good_practices,</if>
            <if test="standard != null">standard,</if>
            <if test="completionStatus != null">completion_status,</if>
            <if test="personInCharge != null">person_in_charge,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="auditId != null">#{auditId},</if>
            <if test="chapterId != null">#{chapterId},</if>
            <if test="year != null">#{year},</if>
            <if test="type != null">#{type},</if>
            <if test="directory != null">#{directory},</if>
            <if test="code != null">#{code},</if>
            <if test="name != null">#{name},</if>
            <if test="yesNoNa != null">#{yesNoNa},</if>
            <if test="minimumStandard != null">#{minimumStandard},</if>
            <if test="goodPractices != null">#{goodPractices},</if>
            <if test="standard != null">#{standard},</if>
            <if test="completionStatus != null">#{completionStatus},</if>
            <if test="personInCharge != null">#{personInCharge},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTQuestionnaire" parameterType="TQuestionnaire">
        update t_questionnaire
        <trim prefix="SET" suffixOverrides=",">
            <if test="auditId != null">audit_id = #{auditId},</if>
            <if test="chapterId != null">chapter_id = #{chapterId},</if>
            <if test="year != null">year = #{year},</if>
            <if test="type != null">type = #{type},</if>
            <if test="directory != null">directory = #{directory},</if>
            <if test="code != null">code = #{code},</if>
            <if test="name != null">name = #{name},</if>
            <if test="yesNoNa != null">yes_no_na = #{yesNoNa},</if>
            <if test="minimumStandard != null">minimum_standard = #{minimumStandard},</if>
            <if test="goodPractices != null">good_practices = #{goodPractices},</if>
            <if test="standard != null">standard = #{standard},</if>
            <if test="completionStatus != null">completion_status = #{completionStatus},</if>
            <if test="personInCharge != null">person_in_charge = #{personInCharge},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTQuestionnaireById" parameterType="Long">
        delete from t_questionnaire where id = #{id}
    </delete>

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