<?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.xinelu.manage.mapper.questioninfo.QuestionInfoMapper">

    <resultMap type="QuestionInfo" id="QuestionInfoResult">
        <result property="id" column="id"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="diseaseTypeId" column="disease_type_id"/>
        <result property="diseaseTypeName" column="disease_type_name"/>
        <result property="questionnaireName" column="questionnaire_name"/>
        <result property="questionnaireDescription" column="questionnaire_description"/>
        <result property="answeringMethod" column="answering_method"/>
        <result property="questionnaireId" column="questionnaire_id"/>
        <result property="questionCount" column="question_count"/>
        <result property="questionnaireTotalScore" column="questionnaire_total_score"/>
        <result property="questionnaireStatus" column="questionnaire_status"/>
        <result property="questionnaireSort" column="questionnaire_sort"/>
        <result property="questionnaireRemark" column="questionnaire_remark"/>
	    <result property="sourceTemplateId" column="source_template_id"/>
        <result property="questionType" column="question_type"/>
	    <result property="scriptInfoId" column="script_info_id"/>
	    <result property="scriptName" column="script_name"/>
	    <result property="robotPublishId" column="robot_publish_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"/>
    </resultMap>

    <sql id="selectQuestionInfoVo">
        select id,
               department_id,
               department_name,
               disease_type_id,
               disease_type_name,
               questionnaire_name,
               questionnaire_description,
               answering_method,
               questionnaire_id,
               question_count,
               questionnaire_total_score,
               questionnaire_status,
               questionnaire_sort,
               questionnaire_remark,
               source_template_id,
               question_type,
               script_info_id,
               script_name,
               robot_publish_id,
	           create_by,
               create_time,
               update_by,
               update_time
        from question_info
    </sql>

    <select id="selectQuestionInfoList" parameterType="QuestionInfo" resultMap="QuestionInfoResult">
        <include refid="selectQuestionInfoVo"/>
        <where>
            <if test="departmentId != null ">
                and department_id = #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="diseaseTypeId != null ">
                and disease_type_id = #{diseaseTypeId}
            </if>
            <if test="diseaseTypeName != null  and diseaseTypeName != ''">
                and disease_type_name like concat('%', #{diseaseTypeName}, '%')
            </if>
            <if test="questionnaireName != null  and questionnaireName != ''">
                and questionnaire_name like concat('%', #{questionnaireName}, '%')
            </if>
            <if test="questionnaireDescription != null  and questionnaireDescription != ''">
                and questionnaire_description = #{questionnaireDescription}
            </if>
            <if test="answeringMethod != null  and answeringMethod != ''">
                and answering_method = #{answeringMethod}
            </if>
            <if test="questionnaireId != null  and questionnaireId != ''">
                and questionnaire_id = #{questionnaireId}
            </if>
            <if test="questionCount != null ">
                and question_count = #{questionCount}
            </if>
            <if test="questionnaireTotalScore != null ">
                and questionnaire_total_score = #{questionnaireTotalScore}
            </if>
            <if test="questionnaireStatus != null  and questionnaireStatus != ''">
                and questionnaire_status = #{questionnaireStatus}
            </if>
            <if test="questionnaireSort != null ">
                and questionnaire_sort = #{questionnaireSort}
            </if>
            <if test="questionnaireRemark != null  and questionnaireRemark != ''">
                and questionnaire_remark = #{questionnaireRemark}
            </if>
            <if test="questionType != null  and questionType != ''">
                and question_type = #{questionType}
            </if>
        </where>
    </select>

    <select id="selectQuestionList" parameterType="com.xinelu.manage.dto.questioninfo.QuestionInfoDto" resultMap="QuestionInfoResult">
        select qi.id,
        qi.department_id,
        qi.department_name,
        qi.disease_type_id,
        qi.disease_type_name,
        qi.questionnaire_name,
        qi.questionnaire_description,
        qi.answering_method,
        qi.questionnaire_id,
        qi.question_count,
        qi.questionnaire_total_score,
        qi.questionnaire_status,
        qi.questionnaire_sort,
        qi.questionnaire_remark,
	    qi.source_template_id,
        qi.question_type,
	    qi.script_info_id,
	    qi.script_name,
	    qi.robot_publish_id,
        qi.create_by,
        qi.create_time,
        qi.update_by,
        qi.update_time
        from question_info qi
        left join department dt ON qi.department_id = dt.id
        <where>
	        <if test="hospitalAgencyId != null ">
		        and dt.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentId != null ">
                and qi.department_id = #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and qi.department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="diseaseTypeId != null ">
                and qi.disease_type_id = #{diseaseTypeId}
            </if>
            <if test="diseaseTypeName != null  and diseaseTypeName != ''">
                and qi.disease_type_name like concat('%', #{diseaseTypeName}, '%')
            </if>
            <if test="questionnaireName != null  and questionnaireName != ''">
                and qi.questionnaire_name like concat('%', #{questionnaireName}, '%')
            </if>
            <if test="questionnaireDescription != null  and questionnaireDescription != ''">
                and qi.questionnaire_description = #{questionnaireDescription}
            </if>
            <if test="answeringMethod != null  and answeringMethod != ''">
                and qi.answering_method = #{answeringMethod}
            </if>
            <if test="questionnaireId != null  and questionnaireId != ''">
                and qi.questionnaire_id = #{questionnaireId}
            </if>
            <if test="questionCount != null ">
                and qi.question_count = #{questionCount}
            </if>
            <if test="questionnaireTotalScore != null ">
                and qi.questionnaire_total_score = #{questionnaireTotalScore}
            </if>
            <if test="questionnaireStatus != null  and questionnaireStatus != ''">
                and qi.questionnaire_status = #{questionnaireStatus}
            </if>
            <if test="questionType != null  and questionType != ''">
                and qi.question_type = #{questionType}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by qi.create_time desc
    </select>

    <select id="selectQuestionInfoById" parameterType="Long"
            resultMap="QuestionInfoResult">
        <include refid="selectQuestionInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertQuestionInfo" parameterType="QuestionInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into question_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">department_id,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="diseaseTypeId != null">disease_type_id,
            </if>
            <if test="diseaseTypeName != null">disease_type_name,
            </if>
            <if test="questionnaireName != null">questionnaire_name,
            </if>
            <if test="questionnaireDescription != null">questionnaire_description,
            </if>
            <if test="answeringMethod != null">answering_method,
            </if>
            <if test="questionnaireId != null">questionnaire_id,
            </if>
            <if test="questionCount != null">question_count,
            </if>
            <if test="questionnaireTotalScore != null">questionnaire_total_score,
            </if>
            <if test="questionnaireStatus != null">questionnaire_status,
            </if>
            <if test="questionnaireSort != null">questionnaire_sort,
            </if>
            <if test="questionnaireRemark != null">questionnaire_remark,
            </if>
	        <if test="sourceTemplateId != null">source_template_id,
	        </if>
            <if test="questionType != null">question_type,
            </if>
	        <if test="scriptInfoId != null">script_info_id,
	        </if>
	        <if test="scriptName != null">script_name,
	        </if>
	        <if test="robotPublishId != null">robot_publish_id,
	        </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">#{departmentId},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="diseaseTypeId != null">#{diseaseTypeId},
            </if>
            <if test="diseaseTypeName != null">#{diseaseTypeName},
            </if>
            <if test="questionnaireName != null">#{questionnaireName},
            </if>
            <if test="questionnaireDescription != null">#{questionnaireDescription},
            </if>
            <if test="answeringMethod != null">#{answeringMethod},
            </if>
            <if test="questionnaireId != null">#{questionnaireId},
            </if>
            <if test="questionCount != null">#{questionCount},
            </if>
            <if test="questionnaireTotalScore != null">#{questionnaireTotalScore},
            </if>
            <if test="questionnaireStatus != null">#{questionnaireStatus},
            </if>
            <if test="questionnaireSort != null">#{questionnaireSort},
            </if>
            <if test="questionnaireRemark != null">#{questionnaireRemark},
            </if>
	        <if test="sourceTemplateId != null">#{sourceTemplateId},
	        </if>
            <if test="questionType != null">#{questionType},
            </if>
	        <if test="scriptInfoId != null">#{scriptInfoId},
	        </if>
	        <if test="scriptName != null">#{scriptName},
	        </if>
	        <if test="robotPublishId != null">#{robotPublishId},
	        </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateQuestionInfo" parameterType="QuestionInfo">
        update question_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="departmentId != null">department_id =
                #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="diseaseTypeId != null">disease_type_id =
                #{diseaseTypeId},
            </if>
            <if test="diseaseTypeName != null">disease_type_name =
                #{diseaseTypeName},
            </if>
            <if test="questionnaireName != null">questionnaire_name =
                #{questionnaireName},
            </if>
            <if test="questionnaireDescription != null">questionnaire_description =
                #{questionnaireDescription},
            </if>
            <if test="answeringMethod != null">answering_method =
                #{answeringMethod},
            </if>
            <if test="questionnaireId != null">questionnaire_id =
                #{questionnaireId},
            </if>
            <if test="questionCount != null">question_count =
                #{questionCount},
            </if>
            <if test="questionnaireTotalScore != null">questionnaire_total_score =
                #{questionnaireTotalScore},
            </if>
            <if test="questionnaireStatus != null">questionnaire_status =
                #{questionnaireStatus},
            </if>
            <if test="questionnaireSort != null">questionnaire_sort =
                #{questionnaireSort},
            </if>
            <if test="questionnaireRemark != null">questionnaire_remark =
                #{questionnaireRemark},
            </if>
	        <if test="sourceTemplateId != null">source_template_id =
		        #{sourceTemplateId},
	        </if>
            <if test="questionType != null">question_type =
                #{questionType},
            </if>
	        <if test="scriptInfoId != null">script_info_id =
		        #{scriptInfoId},
	        </if>
	        <if test="scriptName != null">script_name =
		        #{scriptName},
	        </if>
	        <if test="robotPublishId != null">robot_publish_id =
		        #{robotPublishId},
	        </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQuestionInfoById" parameterType="Long">
        delete
        from question_info
        where id = #{id}
    </delete>

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

    <select id="departmentQuestionByDepartmentName" resultType="com.xinelu.manage.vo.department.DepartmentVO">
        select dt.id,
        dt.department_name,
        dt.department_code,
        count(qi.id) AS countNum,
        (select count(1) from question_info qi left join department dt on dt.id = qi.department_id
        <where>
	        <if test="hospitalAgencyId != null ">
		        and dt.hospital_agency_id = #{hospitalAgencyId}
	        </if>
        	${params.dataScope}
        </where>) totalNumber
        from department dt left join question_info qi on dt.id = qi.department_id
        <where>
	        <if test="hospitalAgencyId != null ">
		        and dt.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentName != null  and departmentName != ''">
                and dt.department_name like concat('%',#{departmentName},'%')
            </if>
            <if test="questionnaireStatus != null  and questionnaireStatus != ''">
                and qi.questionnaire_status =#{questionnaireStatus}
            </if>
            <if test="questionType != null  and questionType != ''">
                and qi.question_type = #{questionType}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        GROUP BY dt.id
    </select>
</mapper>