<?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.SurveyQuestionnaireVersionMapper">

    <resultMap type="SurveyQuestionnaireVersion" id="SurveyQuestionnaireVersionResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="questionnaireId"    column="questionnaire_id"    />
        <result property="versionNo"    column="version_no"    />
        <result property="versionName"    column="version_name"    />
        <result property="formJson"    column="form_json"    />
        <result property="tableName"    column="table_name"    />
        <result property="status"    column="status"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="publishBy"    column="publish_by"    />
        <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"    />
    </resultMap>

    <sql id="selectSurveyQuestionnaireVersionVo">
        select id, company_id, questionnaire_id, version_no, version_name, form_json,
               table_name, status, publish_time, publish_by, create_by, create_time,
               update_by, update_time, remark, del_flag
        from survey_questionnaire_version
    </sql>

    <select id="selectSurveyQuestionnaireVersionList" parameterType="SurveyQuestionnaireVersion" resultMap="SurveyQuestionnaireVersionResult">
        <include refid="selectSurveyQuestionnaireVersionVo"/>
        <where>
            del_flag = '0'
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="questionnaireId != null "> and questionnaire_id = #{questionnaireId}</if>
            <if test="versionNo != null "> and version_no = #{versionNo}</if>
            <if test="versionName != null  and versionName != ''"> and version_name like concat('%', #{versionName}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by version_no desc
    </select>

    <select id="selectSurveyQuestionnaireVersionById" parameterType="Long" resultMap="SurveyQuestionnaireVersionResult">
        <include refid="selectSurveyQuestionnaireVersionVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectVersionListByQuestionnaireId" parameterType="Long" resultMap="SurveyQuestionnaireVersionResult">
        <include refid="selectSurveyQuestionnaireVersionVo"/>
        where questionnaire_id = #{questionnaireId} and del_flag = '0'
        order by version_no desc
    </select>

    <select id="selectMaxVersionNo" parameterType="Long" resultType="Integer">
        select IFNULL(MAX(version_no), 0) from survey_questionnaire_version
        where questionnaire_id = #{questionnaireId} and del_flag = '0'
    </select>
    <select id="getLatestInfo" resultType="com.fs.survey.domain.SurveyQuestionnaireVersion">
        SELECT *
        FROM survey_questionnaire_version
        WHERE questionnaire_id = #{id}
          AND version_no = (
            SELECT MAX(version_no)
            FROM survey_questionnaire_version
            WHERE questionnaire_id = #{id}
        );
    </select>

    <insert id="insertSurveyQuestionnaireVersion" parameterType="SurveyQuestionnaireVersion" useGeneratedKeys="true" keyProperty="id">
        insert into survey_questionnaire_version
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="questionnaireId != null">questionnaire_id,</if>
            <if test="versionNo != null">version_no,</if>
            <if test="versionName != null">version_name,</if>
            <if test="formJson != null and formJson != ''">form_json,</if>
            <if test="tableName != null">table_name,</if>
            <if test="status != null">status,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="publishBy != null">publish_by,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="questionnaireId != null">#{questionnaireId},</if>
            <if test="versionNo != null">#{versionNo},</if>
            <if test="versionName != null">#{versionName},</if>
            <if test="formJson != null and formJson != ''">#{formJson},</if>
            <if test="tableName != null">#{tableName},</if>
            <if test="status != null">#{status},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="publishBy != null">#{publishBy},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime}</if>
        </trim>
    </insert>

    <update id="updateSurveyQuestionnaireVersion" parameterType="SurveyQuestionnaireVersion">
        update survey_questionnaire_version
        <trim prefix="SET" suffixOverrides=",">
            <if test="versionName != null">version_name = #{versionName},</if>
            <if test="formJson != null and formJson != ''">form_json = #{formJson},</if>
            <if test="tableName != null">table_name = #{tableName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="publishBy != null">publish_by = #{publishBy},</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="deleteSurveyQuestionnaireVersionById" parameterType="Long">
        update survey_questionnaire_version set del_flag = '1' where id = #{id}
    </delete>

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

    <delete id="deleteSurveyQuestionnaireVersionByQuestionnaireId" parameterType="Long">
        update survey_questionnaire_version set del_flag = '1' where questionnaire_id = #{questionnaireId}
    </delete>
</mapper>
