Commit 41f3ac0e authored by tianbo's avatar tianbo

refactor(amos-boot-module-jg): 优化使用登记列表查询 SQL

- 重构了 getListPage1 查询,使用 WITH 子句优化复杂查询结构 - 将设备过滤条件和公共主表过滤条件分离,提高可读性 - 使用 EXISTS 子查询替代左连接,提升查询性能 - 优化了字符串聚合和类别映射的处理方式 - 调整了排序和分页逻辑,提高查询效率
parent 44019c76
......@@ -380,37 +380,11 @@
</select>
<select id="getListPage1" resultType="java.util.Map">
<choose>
<!-- 情况1:有设备过滤条件 -->
<when test="dto.equList != null or dto.equCategory != null or dto.equDefine != null or
dto.equCode != null or dto.code96333 != null or dto.supervisoryCode != null">
WITH pre_filtered_equipment AS (
SELECT DISTINCT re.equip_transfer_id
WITH pre_filtered_equipment AS NOT MATERIALIZED (
SELECT re.equip_transfer_id
FROM tzs_jg_use_registration_eq re
WHERE EXISTS (
SELECT 1 FROM idx_biz_jg_register_info jri,idx_biz_jg_other_info other
WHERE jri."RECORD" = re.equ_id AND other."RECORD" = re.equ_id
<!-- 设备表过滤条件 -->
<if test="dto.equList != null and dto.equList != ''">
AND jri."EQU_LIST" = #{dto.equList}
</if>
<if test="dto.equCategory != null and dto.equCategory != ''">
AND jri."EQU_CATEGORY" = #{dto.equCategory}
</if>
<if test="dto.equDefine != null and dto.equDefine != ''">
AND jri."EQU_DEFINE" = #{dto.equDefine}
</if>
<if test="dto.equCode != null and dto.equCode != ''">
AND jri."EQU_CODE" LIKE CONCAT(#{dto.equCode}, '%')
</if>
<if test="dto.code96333 != null and dto.code96333 != ''">
AND other."CODE96333" LIKE CONCAT(#{dto.code96333}, '%')
</if>
<if test="dto.supervisoryCode != null and dto.supervisoryCode != ''">
AND other."SUPERVISORY_CODE" LIKE CONCAT(#{dto.supervisoryCode}, '%')
</if>
)
AND EXISTS (
WHERE
EXISTS (
SELECT 1 FROM tzs_jg_use_registration fu
WHERE fu.sequence_nbr = re.equip_transfer_id
AND fu.is_delete = '0'
......@@ -466,12 +440,45 @@
OR fu.transfer_to_user_ids LIKE CONCAT('%', #{dto.currentUserId}, '%'))
</if>
)
<if test="dto.equList != null or dto.equCategory != null or dto.equDefine != null or
dto.equCode != null">
AND EXISTS (
SELECT 1 FROM idx_biz_jg_register_info jri
WHERE jri."RECORD" = re.equ_id
<!-- 设备表过滤条件 -->
<if test="dto.equList != null and dto.equList != ''">
AND jri."EQU_LIST" = #{dto.equList}
</if>
<if test="dto.equCategory != null and dto.equCategory != ''">
AND jri."EQU_CATEGORY" = #{dto.equCategory}
</if>
<if test="dto.equDefine != null and dto.equDefine != ''">
AND jri."EQU_DEFINE" = #{dto.equDefine}
</if>
<if test="dto.equCode != null and dto.equCode != ''">
AND jri."EQU_CODE" LIKE CONCAT(#{dto.equCode}, '%')
</if>
)
</if>
<if test="dto.code96333 != null or dto.supervisoryCode != null">
AND EXISTS (
SELECT 1 FROM idx_biz_jg_other_info other
WHERE other."RECORD" = re.equ_id
<!-- 设备表过滤条件 -->
<if test="dto.code96333 != null and dto.code96333 != ''">
AND other."CODE96333" LIKE CONCAT(#{dto.code96333}, '%')
</if>
<if test="dto.supervisoryCode != null and dto.supervisoryCode != ''">
AND other."SUPERVISORY_CODE" LIKE CONCAT(#{dto.supervisoryCode}, '%')
</if>
)
</if>
),
main_data AS (
SELECT
fu.sequence_nbr AS sequenceNbr,
fu.audit_status AS auditStatus,
DATE_FORMAT ( fu.reg_date, '%Y-%m-%d' ) AS regDate,
DATE_FORMAT(fu.reg_date, '%Y-%m-%d') AS regDate,
fu.use_unit_name AS useUnitName,
fu.supervision_org_code AS supervisionOrgCode,
fu.status,
......@@ -482,9 +489,8 @@
fu.next_execute_ids AS nextExecuteIds,
fu.promoter,
fu.use_registration_code AS useRegistrationCode,
DATE_FORMAT ( fu.audit_pass_date, '%Y-%m-%d' ) AS auditPassDate,
DATE_FORMAT ( fu.create_date, '%Y-%m-%d' ) AS createDate,
fu.receive_org_name AS receiveOrgName,
DATE_FORMAT(fu.audit_pass_date, '%Y-%m-%d') AS auditPassDate,
DATE_FORMAT(fu.create_date, '%Y-%m-%d') AS createDate,
fu.next_execute_user_ids AS nextExecuteUserIds,
fu.next_task_id AS nextTaskId,
fu.create_user_id AS createUserId,
......@@ -500,49 +506,77 @@
fu.use_address AS fullAddress,
fu.cancel_reason AS cancelReason,
fu.project_contraption_id AS projectContraptionId,
( SELECT company_name FROM privilege_company WHERE org_code = fu.supervision_org_code AND is_deleted = FALSE LIMIT 1 ) AS orgBranchName
(
SELECT company_name
FROM privilege_company
WHERE org_code = fu.supervision_org_code
AND is_deleted = FALSE
LIMIT 1
) AS orgBranchName
FROM tzs_jg_use_registration fu
WHERE EXISTS (
SELECT 1
FROM pre_filtered_equipment pfe
JOIN tzs_jg_use_registration fu ON pfe.equip_transfer_id = fu.sequence_nbr
WHERE pfe.equip_transfer_id = fu.sequence_nbr
)
ORDER BY fu.create_date DESC, fu.apply_no DESC
LIMIT 20 OFFSET 0
LIMIT #{size} OFFSET #{offset}
),
equipment_aggregates AS MATERIALIZED (
SELECT
re.equip_transfer_id,
STRING_AGG(DISTINCT other."CODE96333", ',') AS code96333,
STRING_AGG(DISTINCT other."SUPERVISORY_CODE", ',') AS supervisoryCode,
STRING_AGG(DISTINCT jri."PRODUCT_NAME", ',') AS productName,
STRING_AGG(DISTINCT jri."EQU_CODE", ',') AS equCode,
STRING_AGG(DISTINCT jri."EQU_LIST", ',') AS equListCode,
STRING_AGG(DISTINCT jri."EQU_CATEGORY", ',') AS equCategoryCode,
STRING_AGG(DISTINCT jri."EQU_DEFINE", ',') AS equDefineCode
FROM main_data md
JOIN tzs_jg_use_registration_eq re ON md.sequenceNbr = re.equip_transfer_id
LEFT JOIN idx_biz_jg_other_info other ON re.equ_id = other."RECORD"
LEFT JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
GROUP BY re.equip_transfer_id
),
category_mapping AS MATERIALIZED (
SELECT
code,
name
FROM tz_equipment_category
WHERE code IN (
SELECT DISTINCT unnest(string_to_array(agg.equListCode || ',' || agg.equCategoryCode || ',' || agg.equDefineCode, ','))
FROM equipment_aggregates agg
WHERE agg.equListCode IS NOT NULL
OR agg.equCategoryCode IS NOT NULL
OR agg.equDefineCode IS NOT NULL
)
)
SELECT
md.*,
(SELECT STRING_AGG(DISTINCT other.code96333, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
JOIN idx_biz_jg_other_info other ON jri."RECORD" = other."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS code96333,
(SELECT STRING_AGG(DISTINCT other.supervisory_code, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
JOIN idx_biz_jg_other_info other ON jri."RECORD" = other."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS supervisoryCode,
(SELECT STRING_AGG(DISTINCT jri.PRODUCT_NAME, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS productName ,
(SELECT STRING_AGG(DISTINCT jri.EQU_CODE, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS equCode,
(SELECT STRING_AGG(DISTINCT jri.EQU_LIST, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS equListCode ,
(SELECT STRING_AGG(DISTINCT jri.EQU_CATEGORY, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS equCategoryCode ,
(SELECT STRING_AGG(DISTINCT jri.EQU_DEFINE, ',')
FROM tzs_jg_use_registration_eq re
JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
WHERE re.equip_transfer_id = md.sequenceNbr) AS equDefineCode,
(SELECT name from tz_equipment_category where code = equListCode ) AS equListName,
(SELECT name from tz_equipment_category where code = equCategoryCode ) AS equCategory,
(SELECT name from tz_equipment_category where code = equDefineCode ) AS equDefine
ea.code96333,
ea.supervisoryCode,
ea.productName,
ea.equCode,
ea.equListCode,
ea.equCategoryCode,
ea.equDefineCode,
(
SELECT STRING_AGG(DISTINCT cm.name, ',')
FROM unnest(string_to_array(ea.equListCode, ',')) AS equ_list(code)
JOIN category_mapping cm ON cm.code = equ_list.code
) AS equListName,
(
SELECT STRING_AGG(DISTINCT cm.name, ',')
FROM unnest(string_to_array(ea.equCategoryCode, ',')) AS equ_category(code)
JOIN category_mapping cm ON cm.code = equ_category.code
) AS equCategory,
(
SELECT STRING_AGG(DISTINCT cm.name, ',')
FROM unnest(string_to_array(ea.equDefineCode, ',')) AS equ_define(code)
JOIN category_mapping cm ON cm.code = equ_define.code
) AS equDefine
FROM main_data md
GROUP BY md.sequenceNbr
JOIN equipment_aggregates ea ON md.sequenceNbr = ea.equip_transfer_id
ORDER BY
<choose>
<when test="sort != null and sort.field != null and sort.sortType != null">
......@@ -559,166 +593,22 @@
md.create_date DESC, md.applyNo DESC
</otherwise>
</choose>
LIMIT #{size} OFFSET #{offset}
</when>
<!-- 情况2:无设备过滤条件 -->
<otherwise>
WITH paginated_ids AS (
SELECT sequence_nbr
FROM tzs_jg_use_registration fu
WHERE fu.is_delete = '0'
<!-- 公共主表过滤条件 -->
<if test="dto.status != null and dto.status != ''">
AND fu.status = #{dto.status}
</if>
<if test="dto.applicationDate != null">
AND DATE(fu.create_date) = DATE(#{dto.applicationDate})
</if>
<if test="dto.fullAddress != null and dto.fullAddress != ''">
AND fu.use_address LIKE CONCAT('%', #{dto.fullAddress}, '%')
</if>
<if test="dto.useUnitName != null and dto.useUnitName != ''">
AND fu.use_unit_name LIKE CONCAT('%', #{dto.useUnitName}, '%')
</if>
<if test="dto.applyNo != null and dto.applyNo != ''">
AND fu.apply_no LIKE CONCAT(#{dto.applyNo}, '%')
</if>
<if test="dto.useRegistrationCode != null and dto.useRegistrationCode != ''">
AND fu.use_registration_code LIKE CONCAT(#{dto.useRegistrationCode}, '%')
</if>
<if test="dto.useUnitCode != null and dto.useUnitCode != ''">
AND fu.use_unit_credit_code = #{dto.useUnitCode}
</if>
<if test="dto.orgBranchCode != null and dto.orgBranchCode != ''">
<choose>
<when test="client == 'jgLook'">
AND fu.supervision_org_code LIKE CONCAT(#{dto.orgBranchCode}, '%')
</when>
<otherwise>
AND fu.supervision_org_code = #{dto.orgBranchCode}
</otherwise>
</choose>
</if>
<if test="dto.auditPassDateStart != null and dto.auditPassDateEnd != null">
AND fu.audit_pass_date BETWEEN #{dto.auditPassDateStart} AND #{dto.auditPassDateEnd}
</if>
<if test="dto.dataType == 'supervision' ">
<choose>
<when test="client == 'jgAudit'">
AND (fu.receive_company_code = #{dto.receiveCompanyCode}
OR fu.transfer_to_user_ids LIKE CONCAT('%', #{dto.currentUserId}, '%'))
</when>
<otherwise>
AND fu.supervision_org_code LIKE CONCAT(#{dto.supervisionOrgCode}, '%')
</otherwise>
</choose>
AND fu.status != '使用单位待提交'
</if>
<if test="dto.dataType == 'company' ">
AND (fu.use_unit_credit_code = #{dto.unitCode}
OR fu.transfer_to_user_ids LIKE CONCAT('%', #{dto.currentUserId}, '%'))
</if>
ORDER BY
<choose>
<when test="sort != null and sort.field != null and sort.sortType != null">
<choose>
<when test="sort.field == 'create_date'">fu.create_date</when>
<when test="sort.field == 'apply_no'">fu.apply_no</when>
<when test="sort.field == 'reg_date'">fu.reg_date</when>
<when test="sort.field == 'audit_pass_date'">fu.audit_pass_date</when>
<otherwise>${sort.field}</otherwise>
</choose>
${sort.sortType}
</when>
<otherwise>
fu.create_date DESC, fu.apply_no DESC
</otherwise>
</choose>
LIMIT #{size} OFFSET #{offset}
)
SELECT
fu.sequence_nbr AS sequenceNbr,
fu.audit_status AS auditStatus,
DATE_FORMAT(fu.reg_date,'%Y-%m-%d') AS regDate,
fu.use_unit_name AS useUnitName,
fu.supervision_org_code AS supervisionOrgCode,
fu.status,
fu.receive_org_name AS receiveOrgName,
fu.use_address AS place,
fu.instance_id AS instanceId,
fu.apply_no AS applyNo,
fu.next_execute_ids AS nextExecuteIds,
fu.promoter,
fu.use_registration_code AS useRegistrationCode,
DATE_FORMAT(fu.audit_pass_date,'%Y-%m-%d') AS auditPassDate,
DATE_FORMAT(fu.create_date,'%Y-%m-%d') AS createDate,
fu.receive_org_name AS receiveOrgName,
fu.next_execute_user_ids AS nextExecuteUserIds,
fu.next_task_id AS nextTaskId,
fu.create_user_id AS createUserId,
fu.rec_date AS recDate,
fu.manage_type AS manageType,
fu.reg_type AS regType,
fu.is_delete,
fu.create_date,
fu.receive_company_code,
fu.use_unit_credit_code,
fu.transfer_to_user_ids,
fu.supervision_org_code,
fu.use_address AS fullAddress,
fu.cancel_reason AS cancelReason,
fu.project_contraption_id AS projectContraptionId,
(select company_name from privilege_company where org_code = fu.supervision_org_code and is_deleted = false limit 1) AS orgBranchName,
COALESCE(GROUP_CONCAT(DISTINCT re.equ_id), '') AS equipId,
COALESCE(GROUP_CONCAT(DISTINCT other.supervisory_code), '') AS supervisoryCode,
COALESCE(GROUP_CONCAT(DISTINCT other.CODE96333), '') AS code96333,
COALESCE(GROUP_CONCAT(DISTINCT jri.PRODUCT_NAME), '') AS productName,
COALESCE(GROUP_CONCAT(DISTINCT jri.EQU_CODE), '') AS equCode,
COALESCE(GROUP_CONCAT(DISTINCT jri.EQU_LIST), '') AS equListCode,
COALESCE(GROUP_CONCAT(DISTINCT jri.EQU_CATEGORY), '') AS equCategoryCode,
COALESCE(GROUP_CONCAT(DISTINCT jri.EQU_DEFINE), '') AS equDefineCode,
COALESCE(GROUP_CONCAT(DISTINCT c_list.name), '') AS equListName,
COALESCE(GROUP_CONCAT(DISTINCT c_cate.name), '') AS equCategory,
COALESCE(GROUP_CONCAT(DISTINCT c_def.name), '') AS equDefine
FROM paginated_ids pi
JOIN tzs_jg_use_registration fu ON pi.sequence_nbr = fu.sequence_nbr
LEFT JOIN tzs_jg_use_registration_eq re ON fu.sequence_nbr = re.equip_transfer_id
LEFT JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
LEFT JOIN idx_biz_jg_other_info other ON jri."RECORD" = other."RECORD"
LEFT JOIN tz_equipment_category c_list ON jri.EQU_LIST = c_list.code
LEFT JOIN tz_equipment_category c_cate ON jri.EQU_CATEGORY = c_cate.code
LEFT JOIN tz_equipment_category c_def ON jri.EQU_DEFINE = c_def.code
GROUP BY fu.sequence_nbr
ORDER BY
<choose>
<when test="sort != null and sort.field != null and sort.sortType != null">
<choose>
<when test="sort.field == 'create_date'">fu.create_date</when>
<when test="sort.field == 'apply_no'">fu.apply_no</when>
<when test="sort.field == 'reg_date'">fu.reg_date</when>
<when test="sort.field == 'audit_pass_date'">fu.audit_pass_date</when>
<otherwise>${sort.field}</otherwise>
</choose>
${sort.sortType}
</when>
<otherwise>
fu.create_date DESC, fu.apply_no DESC
</otherwise>
</choose>
</otherwise>
</choose>
</select>
<select id="getListPageCount" resultType="long">
SELECT <![CDATA[/*+ set(query_dop 16)*/]]> COUNT(DISTINCT ur.sequence_nbr)
SELECT <![CDATA[/*+ set(query_dop 16)*/]]> COUNT(*)
FROM tzs_jg_use_registration ur
LEFT JOIN tzs_jg_use_registration_eq re ON ur.sequence_nbr = re.equip_transfer_id
LEFT JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
LEFT JOIN idx_biz_jg_other_info other ON re.equ_id = other."RECORD"
<if test="dto.equList != null or dto.equCategory != null or dto.equDefine != null or
dto.equCode != null or dto.code96333 != null or dto.supervisoryCode != null">
INNER JOIN tzs_jg_use_registration_eq re ON ur.sequence_nbr = re.equip_transfer_id
INNER JOIN idx_biz_jg_register_info jri ON re.equ_id = jri."RECORD"
<if test="dto.code96333 != null or dto.supervisoryCode != null">
INNER JOIN idx_biz_jg_other_info other ON re.equ_id = other."RECORD"
</if>
</if>
WHERE ur.is_delete = 0
<if test="dto.equList != null or dto.equCategory != null or dto.equDefine != null or
dto.equCode != null or dto.code96333 != null or dto.supervisoryCode != null">
<!-- 统一设备过滤条件 -->
<if test="dto.equList != null and dto.equList != ''">
AND jri."EQU_LIST" = #{dto.equList}
......@@ -743,6 +633,8 @@
<if test="dto.supervisoryCode != null and dto.supervisoryCode != ''">
AND other."SUPERVISORY_CODE" LIKE CONCAT('%', #{dto.supervisoryCode}, '%')
</if>
</if>
<if test="dto.status != null and dto.status != ''">
AND ur.status = #{dto.status}
</if>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment