<?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.departmentdiseasetype.DepartmentDiseaseTypeMapper">

    <resultMap type="DepartmentDiseaseType" id="DepartmentDiseaseTypeResult">
        <result property="id" column="id"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="diseaseTypeName" column="disease_type_name"/>
        <result property="diseaseTypeCode" column="disease_type_code"/>
        <result property="diagnosisInfo" column="diagnosis_info"/>
        <result property="diseaseTypeRemark" column="disease_type_remark"/>
        <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="selectDepartmentDiseaseTypeVo">
        select id,
               department_id,
               department_name,
               disease_type_name,
               disease_type_code,
               diagnosis_info,
               disease_type_remark,
               create_by,
               create_time,
               update_by,
               update_time
        from department_disease_type
    </sql>

    <select id="selectDepartmentDiseaseTypeList" parameterType="com.xinelu.manage.dto.departmentdiseasetype.DepartmentDiseaseTypeDto"
            resultMap="DepartmentDiseaseTypeResult">
	    select dt.id,
	    dt.department_id,
	    dt.department_name,
	    dt.disease_type_name,
	    dt.disease_type_code,
	    dt.diagnosis_info,
	    dt.disease_type_remark,
	    dt.create_by,
	    dt.create_time,
	    dt.update_by,
	    dt.update_time
	    from department_disease_type dt
	    left join department d on d.id = dt.department_id
        <where>
	        <if test="hospitalAgencyId != null ">
		        and d.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentId != null ">
                and dt.department_id = #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and dt.department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="diseaseTypeName != null  and diseaseTypeName != ''">
                and dt.disease_type_name like concat('%', #{diseaseTypeName}, '%')
            </if>
            <if test="diseaseTypeCode != null  and diseaseTypeCode != ''">
                and dt.disease_type_code = #{diseaseTypeCode}
            </if>
            <if test="diagnosisInfo != null  and diagnosisInfo != ''">
                and dt.diagnosis_info = #{diagnosisInfo}
            </if>
            <if test="diseaseTypeRemark != null  and diseaseTypeRemark != ''">
                and dt.disease_type_remark = #{diseaseTypeRemark}
            </if>
        </where>
    </select>

    <select id="selectDepartmentDiseaseTypeById" parameterType="Long"
            resultMap="DepartmentDiseaseTypeResult">
        <include refid="selectDepartmentDiseaseTypeVo"/>
        where id = #{id}
    </select>

    <insert id="insertDepartmentDiseaseType" parameterType="DepartmentDiseaseType" useGeneratedKeys="true"
            keyProperty="id">
        insert into department_disease_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">department_id,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="diseaseTypeName != null">disease_type_name,
            </if>
            <if test="diseaseTypeCode != null">disease_type_code,
            </if>
            <if test="diagnosisInfo != null">diagnosis_info,
            </if>
            <if test="diseaseTypeRemark != null">disease_type_remark,
            </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="diseaseTypeName != null">#{diseaseTypeName},
            </if>
            <if test="diseaseTypeCode != null">#{diseaseTypeCode},
            </if>
            <if test="diagnosisInfo != null">#{diagnosisInfo},
            </if>
            <if test="diseaseTypeRemark != null">#{diseaseTypeRemark},
            </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="updateDepartmentDiseaseType" parameterType="DepartmentDiseaseType">
        update department_disease_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="departmentId != null">department_id =
                #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="diseaseTypeName != null">disease_type_name =
                #{diseaseTypeName},
            </if>
            <if test="diseaseTypeCode != null">disease_type_code =
                #{diseaseTypeCode},
            </if>
            <if test="diagnosisInfo != null">diagnosis_info =
                #{diagnosisInfo},
            </if>
            <if test="diseaseTypeRemark != null">disease_type_remark =
                #{diseaseTypeRemark},
            </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="deleteDepartmentDiseaseTypeById" parameterType="Long">
        delete
        from department_disease_type
        where id = #{id}
    </delete>

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

    <select id="selectDiseaseTypeCount" resultType="com.xinelu.manage.vo.department.DepartmentVO">
        select dt.id,
        dt.department_name,
        dt.department_code,
        count(ddt.id) AS countNum
        from department dt left join department_disease_type ddt on dt.id = ddt.department_id
        <where>
	        <if test="hospitalAgencyId != null  and hospitalAgencyId != ''">
		        and  dt.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentName != null  and departmentName != ''">
               and  dt.department_name like concat('%',#{departmentName},'%')
            </if>
        </where>
        GROUP BY dt.id
    </select>
</mapper>
