<?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.scriptInfo.ScriptInfoMapper">

    <resultMap type="ScriptInfo" id="ScriptInfoResult">
        <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="commonScriptName" column="common_script_name"/>
        <result property="scriptName" column="script_name"/>
        <result property="scriptId" column="script_id"/>
        <result property="platformId" column="platform_id"/>
        <result property="scriptStatus" column="script_status"/>
        <result property="scriptIntroduction" column="script_introduction"/>
        <result property="scriptSort" column="script_sort"/>
        <result property="scriptRemark" column="script_remark"/>
        <result property="scriptFilePath" column="script_file_path"/>
        <result property="flowScheme" column="flow_scheme"/>
	    <result property="sourceTemplateId" column="source_template_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>

    <resultMap type="com.xinelu.manage.vo.scriptInfo.ScriptVO" id="ScriptVOResult">
        <result property="scriptInfoId" column="scriptInfoId"/>
        <result property="flowScheme" column="flow_scheme"/>
    </resultMap>


    <sql id="selectScriptInfoVo">
        select id,
               department_id,
               department_name,
               disease_type_id,
               disease_type_name,
               common_script_name,
               script_name,
               script_id,
               platform_id,
               script_status,
               script_introduction,
               script_sort,
               script_remark,
               script_file_path,
               flow_scheme,
	    	   source_template_id,
               create_by,
               create_time,
               update_by,
               update_time
        from script_info
    </sql>

    <select id="selectScriptInfoList" parameterType="com.xinelu.manage.dto.script.ScriptInfoDto" resultMap="ScriptInfoResult">
        select si.id,
        si.department_id,
        si.department_name,
        si.disease_type_id,
        si.disease_type_name,
        si.common_script_name,
        si.script_name,
        si.script_id,
        si.platform_id,
        si.script_status,
        si.script_introduction,
        si.script_sort,
        si.script_remark,
        si.script_file_path,
        si.flow_scheme,si.source_template_id,
        si.create_by,
        si.create_time,
        si.update_by,
        si.update_time
        from script_info si
        left join department d on d.id = si.department_id
        <where>
	        <if test="hospitalAgencyId != null ">
		        and d.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentId != null ">
                and si.department_id = #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and si.department_name like concat('%',#{departmentName}, '%' )
            </if>
            <if test="diseaseTypeId != null ">
                and si.disease_type_id =#{diseaseTypeId}
            </if>
            <if test="diseaseTypeName != null  and diseaseTypeName != ''">
                and si.disease_type_name like concat('%', #{diseaseTypeName},'%')
            </if>
            <if test="commonScriptName != null  and commonScriptName != ''">
                and si.common_script_name like concat('%',#{commonScriptName},'%' )
            </if>
            <if test="scriptName != null  and scriptName != ''">
                and si.script_name like concat('%', #{scriptName},'%' )
            </if>
            <if test="scriptId != null  and scriptId != ''">
                and si.script_id =#{scriptId}
            </if>
            <if test="platformId != null  and platformId != ''">
                and si.platform_id =#{platformId}
            </if>
            <if test="scriptStatus != null  and scriptStatus != ''">
                and si.script_status =#{scriptStatus}
            </if>
            <if test="scriptIntroduction != null  and scriptIntroduction != ''">
                and si.script_introduction =#{scriptIntroduction}
            </if>
            <if test="scriptSort != null ">
                and si.script_sort =#{scriptSort}
            </if>
            <if test="scriptRemark != null  and scriptRemark != ''">
                and si.script_remark =#{scriptRemark}
            </if>
            <if test="scriptFilePath != null  and scriptFilePath != ''">
                and si.script_file_path =#{scriptFilePath}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by script_sort
    </select>

    <select id="selectScriptInfoById" parameterType="Long"
            resultMap="ScriptInfoResult">
        <include refid="selectScriptInfoVo"/>
        where id = #{id}
    </select>

    <select id="countByScriptInfo" resultType="java.lang.Integer"
            parameterType="com.xinelu.manage.domain.scriptInfo.ScriptInfo">
        select count(*)
        from script_info
        where department_id = #{departmentId}
          and common_script_name = #{commonScriptName}
          and script_name = #{scriptName}
    </select>

    <select id="countByScriptNameExcludingId" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM script_info
        WHERE common_script_name = #{commonScriptName}
          and department_id = #{departmentId}
          and script_name = #{scriptName}
          and id != #{id}
    </select>

    <insert id="insertScriptInfo" parameterType="ScriptInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into script_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="commonScriptName != null">common_script_name,
            </if>
            <if test="scriptName != null">script_name,
            </if>
            <if test="scriptId != null">script_id,
            </if>
            <if test="platformId != null">platform_id,
            </if>
            <if test="scriptStatus != null">script_status,
            </if>
            <if test="scriptIntroduction != null">script_introduction,
            </if>
            <if test="scriptSort != null">script_sort,
            </if>
            <if test="scriptRemark != null">script_remark,
            </if>
            <if test="scriptFilePath != null">script_file_path,
            </if>
            <if test="flowScheme != null">flow_scheme,
            </if>
	        <if test="sourceTemplateId != null">source_template_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="commonScriptName != null">#{commonScriptName},
            </if>
            <if test="scriptName != null">#{scriptName},
            </if>
            <if test="scriptId != null">#{scriptId},
            </if>
            <if test="platformId != null">#{platformId},
            </if>
            <if test="scriptStatus != null">#{scriptStatus},
            </if>
            <if test="scriptIntroduction != null">#{scriptIntroduction},
            </if>
            <if test="scriptSort != null">#{scriptSort},
            </if>
            <if test="scriptRemark != null">#{scriptRemark},
            </if>
            <if test="scriptFilePath != null">#{scriptFilePath},
            </if>
            <if test="flowScheme != null">#{flowScheme},
            </if>
	        <if test="sourceTemplateId != null">#{sourceTemplateId},
	        </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="updateScriptInfo" parameterType="ScriptInfo">
        update script_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="departmentId != null">department_id =
                #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            disease_type_id = #{diseaseTypeId},
            disease_type_name = #{diseaseTypeName},
            <if test="commonScriptName != null">common_script_name =
                #{commonScriptName},
            </if>
            <if test="scriptName != null">script_name =
                #{scriptName},
            </if>
            <if test="scriptId != null">script_id =
                #{scriptId},
            </if>
            <if test="platformId != null">platform_id =
                #{platformId},
            </if>
            <if test="scriptStatus != null">script_status =
                #{scriptStatus},
            </if>
            <if test="scriptIntroduction != null">script_introduction =
                #{scriptIntroduction},
            </if>
            <if test="scriptSort != null">script_sort =
                #{scriptSort},
            </if>
            <if test="scriptRemark != null">script_remark =
                #{scriptRemark},
            </if>
            <if test="scriptFilePath != null">script_file_path=
                #{scriptFilePath},
            </if>
            <if test="flowScheme != null">flow_scheme =
                #{flowScheme},
            </if>
	        <if test="sourceTemplateId != null">source_template_id =
		        #{sourceTemplateId},
	        </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="deleteScriptInfoById" parameterType="Long">
        delete
        from script_info
        where id = #{id}
    </delete>

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

    <select id="selectScriptEdgeNode" resultType="com.xinelu.manage.vo.scriptInfo.ScriptVO">
        select id scriptInfoId,
               flow_scheme
        from script_info
        where id = #{id}
    </select>

    <select id="selectScriptInfoEdgeIds" resultType="java.lang.Long">
        select id
        from script_info_edge
        where script_info_id = #{id}
    </select>

    <select id="selectScriptInfoNodeIds" resultType="java.lang.Long">
        select id
        from script_info_node
        where script_info_id = #{id}
    </select>

    <delete id="deleteScriptInfoNode">
        delete
        from script_info_node
        where script_info_id = #{id}
    </delete>

    <delete id="deleteScriptInfoEdge">
        delete
        from script_info_edge
        where script_info_id = #{id}
    </delete>
</mapper>