unis_crm/backend/target/classes/mapper/work/WorkMapper.xml

375 lines
13 KiB
XML
Raw Normal View History

2026-03-20 08:39:07 +00:00
<?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.WorkMapper">
<select id="selectTodayCheckIn" resultType="com.unis.crm.dto.work.WorkCheckInDTO">
select
id,
to_char(checkin_date, 'YYYY-MM-DD') as date,
to_char(checkin_time, 'HH24:MI') as time,
coalesce(location_text, '') as locationText,
coalesce(remark, '') as remark,
coalesce(status, 'normal') as status,
longitude,
latitude
from work_checkin
where user_id = #{userId}
and checkin_date = current_date
order by checkin_time desc nulls last, id desc
limit 1
</select>
<select id="selectTodayReport" resultType="com.unis.crm.dto.work.WorkDailyReportDTO">
select
r.id,
to_char(r.report_date, 'YYYY-MM-DD') as date,
to_char(r.submit_time, 'YYYY-MM-DD HH24:MI') as submitTime,
coalesce(r.work_content, '') as workContent,
coalesce(r.tomorrow_plan, '') as tomorrowPlan,
coalesce(r.source_type, 'manual') as sourceType,
coalesce(r.status, 'submitted') as status,
c.score,
c.comment_content as comment
from work_daily_report r
left join (
select distinct on (report_id)
report_id,
score,
comment_content
from work_daily_report_comment
order by report_id, reviewed_at desc nulls last, id desc
) c on c.report_id = r.id
where r.user_id = #{userId}
and r.report_date = current_date
order by r.submit_time desc nulls last, r.id desc
limit 1
</select>
2026-03-23 01:03:27 +00:00
<select id="selectTodayWorkContentActions" resultType="com.unis.crm.dto.work.WorkSuggestedActionDTO">
select
group_name as groupName,
detail
2026-03-20 08:39:07 +00:00
from (
select
coalesce(s.created_at, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(s.candidate_name, '销售拓展') as group_name,
'新增销售拓展' ||
2026-03-20 08:39:07 +00:00
case
when s.title is not null and btrim(s.title) &lt;&gt; '' then ',岗位:' || s.title
else ''
end ||
case
when s.intent_level is not null and btrim(s.intent_level) &lt;&gt; '' then ',意向:' || s.intent_level
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_sales_expansion s
where s.owner_user_id = #{userId}
and s.created_at::date = current_date
union all
select
coalesce(c.created_at, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(c.channel_name, '渠道拓展') as group_name,
'新增渠道拓展' ||
2026-03-20 08:39:07 +00:00
case
when c.province is not null and btrim(c.province) &lt;&gt; '' then ',地区:' || c.province
else ''
end ||
case
when c.industry is not null and btrim(c.industry) &lt;&gt; '' then ',行业:' || c.industry
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_channel_expansion c
where c.owner_user_id = #{userId}
and c.created_at::date = current_date
union all
select
coalesce(o.created_at, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(nullif(btrim(cust.customer_name), ''), nullif(btrim(o.opportunity_name), ''), '商机客户') as group_name,
2026-03-20 08:39:07 +00:00
'新增商机:' ||
coalesce(o.opportunity_name, '未命名商机') ||
case
when o.amount is not null then ',金额:¥' || trim(to_char(o.amount, 'FM9999999999990.00'))
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_opportunity o
left join crm_customer cust on cust.id = o.customer_id
where o.owner_user_id = #{userId}
and o.created_at::date = current_date
union all
select
coalesce(f.followup_time, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(s.candidate_name, '销售拓展') as group_name,
'销售拓展跟进' ||
2026-03-20 08:39:07 +00:00
case
when f.followup_type is not null and btrim(f.followup_type) &lt;&gt; '' then ',方式:' || f.followup_type
else ''
end ||
case
when f.content is not null and btrim(f.content) &lt;&gt; '' then ',内容:' || f.content
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_expansion_followup f
join crm_sales_expansion s on s.id = f.biz_id and f.biz_type = 'sales'
where f.followup_user_id = #{userId}
and f.followup_time::date = current_date
union all
select
coalesce(f.followup_time, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(c.channel_name, '渠道拓展') as group_name,
'渠道拓展跟进' ||
2026-03-20 08:39:07 +00:00
case
when f.followup_type is not null and btrim(f.followup_type) &lt;&gt; '' then ',方式:' || f.followup_type
else ''
end ||
case
when f.content is not null and btrim(f.content) &lt;&gt; '' then ',内容:' || f.content
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_expansion_followup f
join crm_channel_expansion c on c.id = f.biz_id and f.biz_type = 'channel'
where f.followup_user_id = #{userId}
and f.followup_time::date = current_date
union all
select
coalesce(f.followup_time, now()) as action_time,
2026-03-23 01:03:27 +00:00
coalesce(nullif(btrim(cust.customer_name), ''), nullif(btrim(o.opportunity_name), ''), '商机客户') as group_name,
'商机跟进' ||
2026-03-20 08:39:07 +00:00
case
when f.followup_type is not null and btrim(f.followup_type) &lt;&gt; '' then ',方式:' || f.followup_type
else ''
end ||
case
when f.content is not null and btrim(f.content) &lt;&gt; '' then ',内容:' || f.content
else ''
2026-03-23 01:03:27 +00:00
end as detail
2026-03-20 08:39:07 +00:00
from crm_opportunity_followup f
join crm_opportunity o on o.id = f.opportunity_id
2026-03-23 01:03:27 +00:00
left join crm_customer cust on cust.id = o.customer_id
2026-03-20 08:39:07 +00:00
where f.followup_user_id = #{userId}
and f.followup_time::date = current_date
) work_lines
2026-03-23 01:03:27 +00:00
order by action_time asc, group_name asc, detail asc
2026-03-20 08:39:07 +00:00
</select>
<select id="selectHistory" resultType="com.unis.crm.dto.work.WorkHistoryItemDTO">
select
id,
type,
date,
time,
content,
status,
score,
comment
from (
select
c.id,
'外勤打卡' as type,
to_char(c.checkin_date, 'YYYY-MM-DD') as date,
to_char(c.checkin_time, 'HH24:MI') as time,
coalesce(c.location_text, '') ||
case
when c.remark is not null and btrim(c.remark) &lt;&gt; '' then E'\n备注' || c.remark
else ''
end as content,
case coalesce(c.status, 'normal')
when 'normal' then '正常'
when 'updated' then '已更新'
else coalesce(c.status, '正常')
end as status,
null::integer as score,
null::text as comment,
coalesce(c.checkin_date::timestamp + c.checkin_time::time, c.created_at) as sort_time
from work_checkin c
where c.user_id = #{userId}
union all
select
r.id,
'日报' as type,
to_char(r.report_date, 'YYYY-MM-DD') as date,
to_char(r.submit_time, 'HH24:MI') as time,
coalesce(r.work_content, '') ||
case
when r.tomorrow_plan is not null and btrim(r.tomorrow_plan) &lt;&gt; '' then E'\n明日计划' || r.tomorrow_plan
else ''
end as content,
case coalesce(rc.comment_content, '')
when '' then
case coalesce(r.status, 'submitted')
when 'submitted' then '已提交'
when 'reviewed' then '已点评'
else coalesce(r.status, '已提交')
end
else '已点评'
end as status,
rc.score,
rc.comment_content as comment,
coalesce(r.report_date::timestamp + r.submit_time::time, r.created_at) as sort_time
from work_daily_report r
left join (
select distinct on (report_id)
report_id,
score,
comment_content
from work_daily_report_comment
order by report_id, reviewed_at desc nulls last, id desc
) rc on rc.report_id = r.id
where r.user_id = #{userId}
) history
order by sort_time desc nulls last, id desc
</select>
<select id="selectTodayCheckInId" resultType="java.lang.Long">
select id
from work_checkin
where user_id = #{userId}
and checkin_date = current_date
order by checkin_time desc nulls last, id desc
limit 1
</select>
<insert id="insertCheckIn">
insert into work_checkin (
id,
user_id,
checkin_date,
checkin_time,
longitude,
latitude,
location_text,
remark,
status,
created_at,
updated_at
) values (
(select coalesce(max(id), 0) + 1 from work_checkin),
#{userId},
current_date,
now(),
#{request.longitude},
#{request.latitude},
#{request.locationText},
#{request.remark},
'normal',
now(),
now()
)
</insert>
<update id="updateCheckIn">
update work_checkin
set checkin_time = now(),
longitude = #{request.longitude},
latitude = #{request.latitude},
location_text = #{request.locationText},
remark = #{request.remark},
status = 'normal',
updated_at = now()
where id = #{checkInId}
</update>
<select id="selectTodayReportId" resultType="java.lang.Long">
select id
from work_daily_report
where user_id = #{userId}
and report_date = current_date
order by submit_time desc nulls last, id desc
limit 1
</select>
<insert id="insertDailyReport">
insert into work_daily_report (
user_id,
report_date,
work_content,
tomorrow_plan,
source_type,
submit_time,
status,
created_at,
updated_at
) values (
#{userId},
current_date,
#{request.workContent},
#{request.tomorrowPlan},
#{request.sourceType},
now(),
'submitted',
now(),
now()
)
</insert>
<update id="updateDailyReport">
update work_daily_report
set work_content = #{request.workContent},
tomorrow_plan = #{request.tomorrowPlan},
source_type = #{request.sourceType},
submit_time = now(),
status = 'submitted',
updated_at = now()
where id = #{reportId}
</update>
<select id="selectTodoIdByBiz" resultType="java.lang.Long">
select id
from work_todo
where user_id = #{userId}
and biz_type = #{bizType}
and biz_id = #{bizId}
limit 1
</select>
<insert id="insertTodo">
insert into work_todo (
id,
user_id,
title,
biz_type,
biz_id,
due_date,
status,
priority,
created_at,
updated_at
) values (
#{todoId},
#{userId},
#{title},
#{bizType},
#{bizId},
current_date::timestamp + interval '1 day' + time '09:00',
'todo',
'medium',
now(),
now()
)
</insert>
<update id="updateTodo">
update work_todo
set title = #{title},
due_date = current_date::timestamp + interval '1 day' + time '09:00',
status = 'todo',
priority = 'medium',
updated_at = now()
where id = #{todoId}
</update>
</mapper>