refactor(mapper): 替换MyBatis XML中的case表达式为choose标签

将多个SQL查询里的case when分组逻辑替换为更易读的mybatis choose标签,同时修正用户手机号字段名匹配错误
main
kangwenjing 2026-09-15 10:57:28 +08:00
parent 61355c5809
commit b5b01d4f79
1 changed files with 42 additions and 29 deletions

View File

@ -911,10 +911,10 @@
cross join users_scope u cross join users_scope u
) )
select select
case <choose>
when #{groupBy} = 'owner' then e.user_name <when test="groupBy == 'owner'">e.user_name</when>
else to_char(e.report_date, 'YYYY-MM-DD') <otherwise>to_char(e.report_date, 'YYYY-MM-DD')</otherwise>
end as "groupName", </choose> as "groupName",
count(1)::bigint as "expectedCount", count(1)::bigint as "expectedCount",
count(r.id)::bigint as "submittedCount", count(r.id)::bigint as "submittedCount",
(count(1) - count(r.id))::bigint as "missingCount", (count(1) - count(r.id))::bigint as "missingCount",
@ -924,7 +924,11 @@
on r.user_id = e.user_id on r.user_id = e.user_id
and r.report_date = e.report_date and r.report_date = e.report_date
and coalesce(r.status, 'submitted') in ('submitted', 'read', 'reviewed') and coalesce(r.status, 'submitted') in ('submitted', 'read', 'reviewed')
group by case when #{groupBy} = 'owner' then e.user_name else to_char(e.report_date, 'YYYY-MM-DD') end group by
<choose>
<when test="groupBy == 'owner'">e.user_name</when>
<otherwise>to_char(e.report_date, 'YYYY-MM-DD')</otherwise>
</choose>
order by "groupName" asc order by "groupName" asc
limit #{limit} limit #{limit}
offset #{offset} offset #{offset}
@ -932,11 +936,11 @@
<select id="checkinSummary" resultType="java.util.LinkedHashMap"> <select id="checkinSummary" resultType="java.util.LinkedHashMap">
select select
case <choose>
when #{groupBy} = 'owner' then coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), c.user_name, '') <when test="groupBy == 'owner'">coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), c.user_name, '')</when>
when #{groupBy} = 'status' then coalesce(c.status, 'normal') <when test="groupBy == 'status'">coalesce(c.status, 'normal')</when>
else to_char(c.checkin_date, 'YYYY-MM-DD') <otherwise>to_char(c.checkin_date, 'YYYY-MM-DD')</otherwise>
end as "groupName", </choose> as "groupName",
count(1)::bigint as "checkinCount", count(1)::bigint as "checkinCount",
count(distinct c.user_id)::bigint as "userCount", count(distinct c.user_id)::bigint as "userCount",
count(distinct c.biz_id) filter (where c.biz_id is not null)::bigint as "bizCount" count(distinct c.biz_id) filter (where c.biz_id is not null)::bigint as "bizCount"
@ -945,11 +949,12 @@
where c.checkin_date between #{startDate} and #{endDate} where c.checkin_date between #{startDate} and #{endDate}
<if test="ownerUserId != null">and c.user_id = #{ownerUserId}</if> <if test="ownerUserId != null">and c.user_id = #{ownerUserId}</if>
<if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = c.user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if> <if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = c.user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if>
group by case group by
when #{groupBy} = 'owner' then coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), c.user_name, '') <choose>
when #{groupBy} = 'status' then coalesce(c.status, 'normal') <when test="groupBy == 'owner'">coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), c.user_name, '')</when>
else to_char(c.checkin_date, 'YYYY-MM-DD') <when test="groupBy == 'status'">coalesce(c.status, 'normal')</when>
end <otherwise>to_char(c.checkin_date, 'YYYY-MM-DD')</otherwise>
</choose>
order by "checkinCount" desc, "groupName" asc order by "checkinCount" desc, "groupName" asc
limit #{limit} limit #{limit}
offset #{offset} offset #{offset}
@ -1003,21 +1008,22 @@
<select id="customerSummary" resultType="java.util.LinkedHashMap"> <select id="customerSummary" resultType="java.util.LinkedHashMap">
select select
case <choose>
when #{groupBy} = 'status' then coalesce(c.status, 'unknown') <when test="groupBy == 'status'">coalesce(c.status, 'unknown')</when>
when #{groupBy} = 'source' then coalesce(c.source, 'unknown') <when test="groupBy == 'source'">coalesce(c.source, 'unknown')</when>
else coalesce(c.industry, 'unknown') <otherwise>coalesce(c.industry, 'unknown')</otherwise>
end as "groupName", </choose> as "groupName",
count(1)::bigint as "customerCount" count(1)::bigint as "customerCount"
from crm_customer c from crm_customer c
where c.created_at::date between #{startDate} and #{endDate} where c.created_at::date between #{startDate} and #{endDate}
<if test="ownerUserId != null">and c.owner_user_id = #{ownerUserId}</if> <if test="ownerUserId != null">and c.owner_user_id = #{ownerUserId}</if>
<if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = c.owner_user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if> <if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = c.owner_user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if>
group by case group by
when #{groupBy} = 'status' then coalesce(c.status, 'unknown') <choose>
when #{groupBy} = 'source' then coalesce(c.source, 'unknown') <when test="groupBy == 'status'">coalesce(c.status, 'unknown')</when>
else coalesce(c.industry, 'unknown') <when test="groupBy == 'source'">coalesce(c.source, 'unknown')</when>
end <otherwise>coalesce(c.industry, 'unknown')</otherwise>
</choose>
order by "customerCount" desc, "groupName" asc order by "customerCount" desc, "groupName" asc
limit #{limit} limit #{limit}
offset #{offset} offset #{offset}
@ -1025,7 +1031,10 @@
<select id="todoSummary" resultType="java.util.LinkedHashMap"> <select id="todoSummary" resultType="java.util.LinkedHashMap">
select select
case when #{groupBy} = 'status' then t.status else t.priority end as "groupName", <choose>
<when test="groupBy == 'status'">t.status</when>
<otherwise>t.priority</otherwise>
</choose> as "groupName",
count(1)::bigint as "todoCount", count(1)::bigint as "todoCount",
count(case when t.status = 'todo' then 1 end)::bigint as "pendingCount", count(case when t.status = 'todo' then 1 end)::bigint as "pendingCount",
count(case when t.status = 'done' then 1 end)::bigint as "doneCount" count(case when t.status = 'done' then 1 end)::bigint as "doneCount"
@ -1033,7 +1042,11 @@
where t.created_at::date between #{startDate} and #{endDate} where t.created_at::date between #{startDate} and #{endDate}
<if test="ownerUserId != null">and t.user_id = #{ownerUserId}</if> <if test="ownerUserId != null">and t.user_id = #{ownerUserId}</if>
<if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = t.user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if> <if test="tenantId != null">and exists (select 1 from sys_tenant_user tu where tu.user_id = t.user_id and tu.tenant_id = #{tenantId} and coalesce(tu.is_deleted, 0) = 0)</if>
group by case when #{groupBy} = 'status' then t.status else t.priority end group by
<choose>
<when test="groupBy == 'status'">t.status</when>
<otherwise>t.priority</otherwise>
</choose>
order by "todoCount" desc, "groupName" asc order by "todoCount" desc, "groupName" asc
limit #{limit} limit #{limit}
offset #{offset} offset #{offset}
@ -1151,7 +1164,7 @@
u.user_id as "userId", u.user_id as "userId",
u.username as "username", u.username as "username",
coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), '') as "displayName", coalesce(nullif(btrim(u.display_name), ''), nullif(btrim(u.username), ''), '') as "displayName",
coalesce(u.mobile, '') as "mobile", coalesce(u.phone, '') as "mobile",
coalesce(u.email, '') as "email", coalesce(u.email, '') as "email",
coalesce(u.status, 1) as "status", coalesce(u.status, 1) as "status",
org_info.org_ids as "orgIds", org_info.org_ids as "orgIds",
@ -1186,7 +1199,7 @@
coalesce(u.username, '') ilike concat('%', #{keyword}, '%') coalesce(u.username, '') ilike concat('%', #{keyword}, '%')
or coalesce(u.display_name, '') ilike concat('%', #{keyword}, '%') or coalesce(u.display_name, '') ilike concat('%', #{keyword}, '%')
or coalesce(u.username, '') ilike concat('%', #{keyword}, '%') or coalesce(u.username, '') ilike concat('%', #{keyword}, '%')
or coalesce(u.mobile, '') ilike concat('%', #{keyword}, '%') or coalesce(u.phone, '') ilike concat('%', #{keyword}, '%')
or coalesce(u.email, '') ilike concat('%', #{keyword}, '%') or coalesce(u.email, '') ilike concat('%', #{keyword}, '%')
or coalesce(org_info.org_names, '') ilike concat('%', #{keyword}, '%') or coalesce(org_info.org_names, '') ilike concat('%', #{keyword}, '%')
or coalesce(role_info.role_names, '') ilike concat('%', #{keyword}, '%') or coalesce(role_info.role_names, '') ilike concat('%', #{keyword}, '%')