<?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.personalization.mapper.OrdersGroupPackageAppMapper">

    <select id="getGroupPackage" resultType="com.openhis.web.personalization.dto.OrdersGroupPackageQueryDto">
        SELECT togp.ID AS group_package_id,
        togp.NAME AS NAME,
        togp.package_type_enum AS package_type_enum,
        ao.id AS organization_id,
        ao.NAME AS organization_name,
        ap.id AS practitioner_id,
        ap.NAME AS practitioner_name
        FROM tmp_orders_group_package AS togp
        LEFT JOIN adm_organization AS ao ON ao.ID = togp.organization_id
        AND ao.delete_flag = '0'
        LEFT JOIN adm_practitioner AS ap ON ap.ID = togp.practitioner_id
        AND ap.delete_flag = '0'
        WHERE togp.delete_flag = '0'
        <if test="searchKey != null and searchKey != ''">
            AND togp.NAME LIKE concat('%', #{searchKey}, '%')
        </if>
        <if test="packageTypeEnum != null">
            AND togp.package_type_enum = #{packageTypeEnum}
        </if>
        <if test="organizationId != null">
            AND togp.organization_id = #{organizationId}
        </if>
        <if test="practitionerId != null">
            AND togp.practitioner_id = #{practitionerId}
        </if>
    </select>

    <select id="getGroupPackageDetail"
            resultType="com.openhis.web.personalization.dto.OrdersGroupPackageDetailQueryDto">
        SELECT togpd.group_package_id,
        togpd.order_definition_id,
        togpd.order_definition_table,
        togpd.quantity,
        togpd.unit_code,
        sdd.dict_label AS unit_code_name,
        CASE
        WHEN togpd.order_definition_table = 'med_medication_definition' THEN
        med.NAME
        WHEN togpd.order_definition_table = 'adm_device_definition' THEN
        adm.NAME
        WHEN togpd.order_definition_table = 'wor_activity_definition' THEN
        wor.NAME
        ELSE NULL
        END AS order_definition_name
        FROM tmp_orders_group_package_detail AS togpd
        LEFT JOIN med_medication_definition AS med
        ON togpd.order_definition_table = 'med_medication_definition'
        AND togpd.order_definition_id = med.ID
        LEFT JOIN adm_device_definition AS adm ON togpd.order_definition_table = 'adm_device_definition'
        AND togpd.order_definition_id = adm.ID
        LEFT JOIN wor_activity_definition AS wor ON togpd.order_definition_table = 'wor_activity_definition'
        AND togpd.order_definition_id = wor.ID
        LEFT JOIN sys_dict_data AS sdd ON sdd.dict_value = togpd.unit_code AND sdd.dict_type = 'unit_code' AND
        sdd.status = '0'
        WHERE togpd.delete_flag = '0'
        <if test="groupPackageIds != null and !groupPackageIds.isEmpty()">
            AND togpd.group_package_id IN
            <foreach collection="groupPackageIds" item="itemId" open="(" separator="," close=")">
                #{itemId}
            </foreach>
        </if>
    </select>

</mapper>