<?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.survey.mapper.SurveyQuestionnaireMapper">

    <resultMap type="SurveyQuestionnaire" id="SurveyQuestionnaireResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="name"    column="name"    />
        <result property="description"    column="description"    />
        <result property="status"    column="status"    />
        <result property="currentVersionId"    column="current_version_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="surveyQuestionnaireType"    column="survey_questionnaire_type"    />
    </resultMap>

    <!-- 修改基础查询SQL，添加公司表关联 -->
    <sql id="selectSurveyQuestionnaireVo">
        select
        sq.id,
        sq.company_id,
        c.company_name,
        sq.name,
        sq.survey_questionnaire_type,
        sq.description,
        sq.status,
        sq.current_version_id,
        sq.create_by,
        sq.create_time,
        sq.update_by,
        sq.update_time,
        sq.remark,
        sq.del_flag
        from survey_questionnaire sq
        left join company c on sq.company_id = c.company_id and c.is_del = '0'
    </sql>

    <select id="selectSurveyQuestionnaireList" parameterType="SurveyQuestionnaire" resultMap="SurveyQuestionnaireResult">
        <include refid="selectSurveyQuestionnaireVo"/>
        <where>
            sq.del_flag = '0'
            <if test="companyId != null "> and sq.company_id = #{companyId}</if>
            <if test="name != null  and name != ''"> and sq.name like concat('%', #{name}, '%')</if>
            <if test="status != null "> and sq.status = #{status}</if>
            <if test="companyName != null  and companyName != ''"> and c.company_name like concat('%', #{companyName}, '%')</if>
            <if test="surveyQuestionnaireType != null "> and sq.survey_questionnaire_type = #{surveyQuestionnaireType}</if>

        </where>
        order by sq.create_time desc
    </select>

    <select id="selectSurveyQuestionnaireById" parameterType="Long" resultMap="SurveyQuestionnaireResult">
        <include refid="selectSurveyQuestionnaireVo"/>
        where sq.id = #{id} and sq.del_flag = '0'
    </select>

    <insert id="insertSurveyQuestionnaire" parameterType="SurveyQuestionnaire" useGeneratedKeys="true" keyProperty="id">
        insert into survey_questionnaire
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="surveyQuestionnaireType != null">survey_questionnaire_type,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="description != null">description,</if>
            <if test="status != null">status,</if>
            <if test="currentVersionId != null">current_version_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="surveyQuestionnaireType != null">#{surveyQuestionnaireType},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="description != null">#{description},</if>
            <if test="status != null">#{status},</if>
            <if test="currentVersionId != null">#{currentVersionId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSurveyQuestionnaire" parameterType="SurveyQuestionnaire">
        update survey_questionnaire
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="surveyQuestionnaireType != null">survey_questionnaire_type = #{surveyQuestionnaireType},</if>
            <if test="description != null">description = #{description},</if>
            <if test="status != null">status = #{status},</if>
            <if test="currentVersionId != null">current_version_id = #{currentVersionId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurveyQuestionnaireById" parameterType="Long">
        update survey_questionnaire set del_flag = '1' where id = #{id}
    </delete>

    <delete id="deleteSurveyQuestionnaireByIds" parameterType="Long">
        update survey_questionnaire set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>