<?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="cn.sh.stc.sict.theme.hpgp.dao.HpgpDepartmentRankMapper">

    <resultMap type="cn.sh.stc.sict.theme.hpgp.model.HpgpDepartmentRank" id="HpgpDepartmentRankMap">
        <result property="standardDept" column="standard_dept"/>
        <result property="hospitalCode" column="hospital_code"/>
        <result property="hospitalName" column="hospital_name"/>
        <result property="deptCode" column="dept_code"/>
        <result property="deptName" column="dept_name"/>
        <result property="rankScore" column="rank_score"/>
    </resultMap>

    <sql id="Base_Column_List"> standard_dept , hospital_code, hospital_name, dept_code, dept_name, rank_score     </sql>

    <select id="getRankByHosAndStandardDept" resultType="cn.sh.stc.sict.theme.hpgp.vo.DeptRankVO">
        select dr.standard_dept standardDept,
               dr.hospital_code hospitalCode,
               dr.hospital_name hospitalName,
               dr.dept_code deptCode,
               dr.dept_name deptName,
               dr.rank_score rankScore,
               d.one_dept_code oneDeptCode,
               d.dept_code subDeptCode
        from hpgp_department_rank dr
            join hp_dept_info d on (dr.hospital_code = d.hos_org_code and dr.one_dept_code = d.one_dept_code and dr.dept_code = d.dept_code)
        <where>
            <choose>
                <when test="hospitalCode != null and hospitalCode != ''">
                    AND dr.hospital_code = #{hospitalCode}
                    AND dr.rank_score = 0
                </when>
                <otherwise>
                    AND dr.rank_score != 0
                </otherwise>
            </choose>
            <if test="deptName != null and deptName != ''">
                AND dr.standard_dept = #{deptName}
            </if>
        </where>
        order by dr.rank_score asc
        <if test="size != null and size != 0">
            limit #{size}
        </if>
    </select>
</mapper>