<?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.pharmacyDispensarymanage.mapper.PharmacyDispensaryDispensingOrderMapper">
    <select id="getInfo"
            resultType="com.openhis.web.pharmacyDispensarymanage.dto.PharmacyDispensaryDetailDto">
     SELECT  string_agg(T1.id::text, ',' ORDER BY T1.id) AS dispense_ids,--药品发放id
             T1.status_enum,--发放状态
             CASE T3.category_enum
               WHEN 1 THEN 20
               WHEN 2 THEN 19 END AS category_enum, --分类
             T3.medication_id AS item_id,--物品编码
             T3.unit_code,--物品计量单位
             T3.quantity AS item_quantity,--数量
             T3.lot_number,  --产品批号
             T3.perform_location AS source_location_id,   --源仓库
             T4.organization_id AS purpose_location_id,--目的仓库
             T1.practitioner_id AS  approver_id,--审批人
             T1.dispense_time AS approval_time, --审批时间
             T1.patient_id AS applicant_id,--申请人
             T3.req_authored_time AS apply_time  --申请时间
     FROM med_medication_dispense AS T1
             INNER JOIN med_medication_request AS T3
               ON T1.med_req_id = T3.id
               AND T3.delete_flag = '0'
             LEFT JOIN med_medication_definition AS T2
               ON T3.medication_id = T2.id
               AND T2.delete_flag = '0'
             LEFT JOIN adm_encounter AS T4
               ON T3.encounter_id = T4.id
               AND T4.delete_flag = '0'
     WHERE T1.id IN
        <foreach collection="dispenseIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
       AND T1.status_enum = #{dispenseStatus}
       AND T1.delete_flag = '0'
     GROUP BY T1.status_enum,
              T3.category_enum,
              T3.medication_id,
              T3.unit_code,
              T3.quantity,
              T3.lot_number,
              T3.perform_location,
              T4.organization_id,
              T1.practitioner_id,
              T1.dispense_time,
              T1.patient_id,
              T3.req_authored_time
    </select>
</mapper>