<?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.openhis.web.document.mapper.DocDefinitionAppMapper">
    <!-- 患者信息分页查询-->
    <select id="getDefinationList" parameterType="java.util.Map"
            resultType="com.openhis.web.document.dto.DocDefinitionDto">
        SELECT
        dd.id,
        dd.name,
        dd.version,
        dd.primary_menu_enum ,
        dd.sub_menu ,
        dd.bus_no ,
        dd.vue_router ,
        dd.hospital_id ,
        dd.is_valid ,
        dd.display_order ,
        dd.use_range_enum ,
        dd.doctor_permission_enum ,
        dd.nursing_permission_enum ,
        dd.medical_permission_enum ,
        dd.pharmacist_permission_enum ,
        ARRAY_AGG(ddo.organization_id) FILTER (WHERE ddo.organization_id IS NOT NULL) AS organization_Ids
        FROM doc_definition dd
        LEFT JOIN doc_definition_organization ddo
        ON dd.id = ddo.definition_id -- 使用 definition_id 关联
        WHERE dd.delete_flag = '0'
        and dd.hospital_id = #{hospitalId}
        <if test="useRanges != null and !useRanges.isEmpty()">
            AND dd.use_range_enum IN
            <foreach collection="useRanges" item="useRange" open="(" separator="," close=")">
                #{useRange}
            </foreach>
        </if>
        <if test="organizationId != null and organizationId != 0">
            AND ddo.organization_id = #{organizationId} OR dd.use_range_enum = 1
        </if>
        <if test="name != null and !name.isEmpty()">
            AND dd.name LIKE CONCAT('%',#{name},'%')
        </if>
        GROUP BY
        dd.id, dd.primary_menu_enum ,
        dd.sub_menu

    </select>


</mapper>