<?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.inventorymanage.mapper.TraceNoAppMapper">
    <select id="selectDetailsPage" resultType="com.openhis.web.inventorymanage.dto.TraceNoDetailsPageDto">
        SELECT T4.id,--ID
               T4.name,--药品名
               T4.location_id,--药房药库
               T4.location_name,
               T4.lot_number,--产品批号
               T4.trace_no,--追溯码
               T4.status_enum,--追溯码状态
               T4.unit_code,--追溯码单位
               T4.operation_type,--操作类型
               T4.item_type,--项目类型
               T4.create_time, --进出时间
               T4.tenant_id,
               T4.item_table,
               T4.item_id
        FROM (SELECT T1.id,--ID
                     T2.name,--药品名
                     T1.location_id,--药房药库
                     T3.name AS location_name,
                     T1.lot_number,--产品批号
                     T1.trace_no,--追溯码
                     T1.status_enum,--追溯码状态
                     T1.unit_code,--追溯码单位
                     T1.operation_type,--操作类型
                     1       AS item_type,--项目类型
                     T1.create_time, --进出时间
                     T1.tenant_id,
                     T1.item_table,
                     T1.item_id
              FROM adm_trace_no_manage AS T1
                       LEFT JOIN med_medication_definition AS T2
                                 ON T1.item_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_location AS T3
                                 ON T1.location_id = T3.id
                                     AND T3.delete_flag = '0'
              WHERE T1.item_table = 'med_medication_definition'
                AND T1.delete_flag = '0'
              UNION
              SELECT T1.id,--ID
                     T2.name,--药品名
                     T1.location_id,--药房药库
                     T3.name AS location_name,
                     T1.lot_number,--产品批号
                     T1.trace_no,--追溯码
                     T1.status_enum,--追溯码状态
                     T1.unit_code,--追溯码单位
                     T1.operation_type,--操作类型
                     2       AS item_type,--项目类型
                     T1.create_time, --进出时间
                     T1.tenant_id,
                     T1.item_table,
                     T1.item_id
              FROM adm_trace_no_manage AS T1
                       LEFT JOIN adm_device_definition AS T2
                                 ON T1.item_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_location AS T3
                                 ON T1.location_id = T3.id
                                     AND T3.delete_flag = '0'
              WHERE T1.item_table = 'adm_device_definition'
                AND T1.delete_flag = '0') AS T4
            ${ew.customSqlSegment}
    </select>
</mapper>
