unis_crm/backend/target/classes/mapper/dashboard/DashboardMapper.xml

207 lines
7.3 KiB
XML

<?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.unis.crm.mapper.DashboardMapper">
<select id="selectDefaultUserId" resultType="java.lang.Long">
select user_id
from sys_user
where status = 1
order by user_id asc
limit 1
</select>
<select id="selectUserWelcome" resultType="com.unis.crm.dto.dashboard.UserWelcomeDTO">
select
u.user_id as userId,
u.display_name as realName,
null as jobTitle,
null as deptName,
null as hireDate
from sys_user u
where u.user_id = #{userId}
and u.status = 1
limit 1
</select>
<select id="selectDashboardStats" resultType="com.unis.crm.dto.dashboard.DashboardStatDTO">
select '本月新增商机' as name,
count(1)::bigint as value,
'monthlyOpportunities' as metricKey
from crm_opportunity
where owner_user_id = #{userId}
and date_trunc('month', created_at) = date_trunc('month', now())
union all
select '跟进中客户' as name,
count(1)::bigint as value,
'followingCustomers' as metricKey
from crm_customer
where owner_user_id = #{userId}
and status = 'following'
union all
select '已成单项目' as name,
count(1)::bigint as value,
'wonProjects' as metricKey
from crm_opportunity
where owner_user_id = #{userId}
and stage = 'won'
union all
select '本月打卡天数' as name,
count(distinct checkin_date)::bigint as value,
'monthlyCheckins' as metricKey
from work_checkin
where user_id = #{userId}
and date_trunc('month', checkin_date::timestamp) = date_trunc('month', now())
</select>
<select id="selectPendingTodos" resultType="com.unis.crm.dto.dashboard.DashboardTodoDTO">
select
id,
title,
biz_type as bizType,
biz_id as bizId,
priority,
status,
due_date as dueDate,
created_at as createdAt
from work_todo
where user_id = #{userId}
and status = 'todo'
order by
case priority
when 'high' then 1
when 'medium' then 2
else 3
end,
coalesce(due_date, created_at) asc
limit 6
</select>
<select id="selectLatestActivities" resultType="com.unis.crm.dto.dashboard.DashboardActivityDTO">
with latest_report_comment as (
select distinct on (c.report_id)
c.report_id,
c.reviewer_user_id,
c.score,
c.comment_content,
c.reviewed_at
from work_daily_report_comment c
order by c.report_id, c.reviewed_at desc, c.id desc
),
activity_union as (
select
l.id,
l.biz_type as bizType,
l.biz_id as bizId,
l.action_type as actionType,
l.title,
l.content,
l.operator_user_id as operatorUserId,
l.created_at as createdAt
from sys_activity_log l
where l.operator_user_id = #{userId}
or l.operator_user_id is null
union all
select
(1000000000 + o.id) as id,
'opportunity' as bizType,
o.id as bizId,
'stage_update' as actionType,
'商机阶段更新' as title,
o.opportunity_name || ' 已推进至' ||
case o.stage
when 'initial_contact' then '初步沟通'
when 'solution_discussion' then '方案交流'
when 'bidding' then '招投标'
when 'business_negotiation' then '商务谈判'
when 'won' then '已成交'
when 'lost' then '已输单'
else o.stage
end || '阶段' as content,
o.owner_user_id as operatorUserId,
o.updated_at as createdAt
from crm_opportunity o
where o.owner_user_id = #{userId}
and o.updated_at > o.created_at
union all
select
(2000000000 + r.id) as id,
'report' as bizType,
r.id as bizId,
case
when r.status = 'reviewed' or lc.score is not null then 'report_reviewed'
else 'report_read'
end as actionType,
case
when r.status = 'reviewed' or lc.score is not null then '日报已点评'
else '日报已阅'
end as title,
case
when lc.score is not null then '主管对你' || to_char(r.report_date, 'MM-DD') || '的日报给出了 ' || lc.score || ' 分'
when r.status = 'reviewed' then '你的' || to_char(r.report_date, 'MM-DD') || '日报已完成主管点评'
else '你的' || to_char(r.report_date, 'MM-DD') || '日报已被查阅'
end as content,
coalesce(lc.reviewer_user_id, r.user_id) as operatorUserId,
coalesce(lc.reviewed_at, r.updated_at, r.created_at) as createdAt
from work_daily_report r
left join latest_report_comment lc on lc.report_id = r.id
where r.user_id = #{userId}
and r.status in ('read', 'reviewed')
union all
select
(3000000000 + c.id) as id,
'channel' as bizType,
c.id as bizId,
'channel_created' as actionType,
'新渠道录入' as title,
'成功录入 ' || c.channel_name || ' 渠道商信息' as content,
c.owner_user_id as operatorUserId,
c.created_at as createdAt
from crm_channel_expansion c
where c.owner_user_id = #{userId}
union all
select
(4000000000 + f.id) as id,
'opportunity_followup' as bizType,
f.opportunity_id as bizId,
'opportunity_followup' as actionType,
'商机跟进新增' as title,
o.opportunity_name || ' 新增了一条' || f.followup_type || '跟进记录' as content,
f.followup_user_id as operatorUserId,
f.followup_time as createdAt
from crm_opportunity_followup f
join crm_opportunity o on o.id = f.opportunity_id
where f.followup_user_id = #{userId}
)
select
a.id,
a.bizType,
a.bizId,
a.actionType,
a.title,
a.content,
a.operatorUserId,
u.display_name as operatorName,
a.createdAt
from activity_union a
left join sys_user u on u.user_id = a.operatorUserId
order by a.createdAt desc nulls last
limit 8
</select>
</mapper>