unis_crm/backend/src/main/resources/mapper/expansion/ExpansionMapper.xml

483 lines
19 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.ExpansionMapper">
2026-03-26 09:29:55 +00:00
<select id="selectDictItems" resultType="com.unis.crm.dto.expansion.DictOptionDTO">
2026-03-20 08:39:07 +00:00
select
2026-03-26 09:29:55 +00:00
item_label as label,
item_value as value
from sys_dict_item
where type_code = #{typeCode}
and status = 1
and coalesce(is_deleted, 0) = 0
order by sort_order asc nulls last, dict_item_id asc
</select>
<select id="selectNextChannelCode" resultType="java.lang.String">
select 'QD-' || to_char(current_date, 'YYYYMMDD') || '-' || lpad((
coalesce((
select count(1)
from crm_channel_expansion
where created_at::date = current_date
), 0) + 1
)::text, 3, '0')
2026-03-20 08:39:07 +00:00
</select>
<select id="selectSalesExpansions" resultType="com.unis.crm.dto.expansion.SalesExpansionItemDTO">
select
s.id,
'sales' as type,
2026-03-26 09:29:55 +00:00
coalesce(s.employee_no, '无') as employeeNo,
2026-03-20 08:39:07 +00:00
s.candidate_name as name,
2026-03-26 09:29:55 +00:00
coalesce(office_dict.item_value, s.office_name, '') as officeCode,
coalesce(office_dict.item_label, nullif(s.office_name, ''), '无') as officeName,
2026-03-20 08:39:07 +00:00
coalesce(s.mobile, '无') as phone,
coalesce(s.email, '无') as email,
2026-03-26 09:29:55 +00:00
coalesce(s.target_dept, '') as targetDept,
coalesce(nullif(s.target_dept, ''), '无') as dept,
coalesce(industry_dict.item_value, s.industry, '') as industryCode,
coalesce(industry_dict.item_label, nullif(s.industry, ''), '无') as industry,
2026-03-20 08:39:07 +00:00
coalesce(s.title, '无') as title,
s.intent_level as intentLevel,
case s.intent_level
when 'high' then '高'
when 'medium' then '中'
when 'low' then '低'
else '无'
end as intent,
s.stage as stageCode,
case s.stage
when 'initial_contact' then '初步沟通'
when 'solution_discussion' then '方案交流'
when 'bidding' then '招投标'
when 'business_negotiation' then '商务谈判'
when 'won' then '已成交'
when 'lost' then '已放弃'
else coalesce(s.stage, '无')
end as stage,
s.has_desktop_exp as hasExp,
s.in_progress as inProgress,
(s.employment_status = 'active') as active,
s.employment_status as employmentStatus,
coalesce(to_char(s.expected_join_date, 'YYYY-MM-DD'), '无') as expectedJoinDate,
coalesce(s.remark, '无') as notes
from crm_sales_expansion s
2026-03-26 09:29:55 +00:00
left join sys_dict_item office_dict
on office_dict.type_code = 'tz_bsc'
and office_dict.item_value = s.office_name
and office_dict.status = 1
and coalesce(office_dict.is_deleted, 0) = 0
left join sys_dict_item industry_dict
on industry_dict.type_code = 'tz_sshy'
and industry_dict.item_value = s.industry
and industry_dict.status = 1
and coalesce(industry_dict.is_deleted, 0) = 0
2026-03-27 04:22:00 +00:00
where 1 = 1
2026-03-20 08:39:07 +00:00
<if test="keyword != null and keyword != ''">
and (
2026-03-26 09:29:55 +00:00
coalesce(s.employee_no, '') ilike concat('%', #{keyword}, '%')
or s.candidate_name ilike concat('%', #{keyword}, '%')
or coalesce(office_dict.item_label, '') ilike concat('%', #{keyword}, '%')
or coalesce(s.target_dept, '') ilike concat('%', #{keyword}, '%')
or coalesce(industry_dict.item_label, '') ilike concat('%', #{keyword}, '%')
2026-03-20 08:39:07 +00:00
)
</if>
order by s.updated_at desc, s.id desc
</select>
<select id="selectChannelExpansions" resultType="com.unis.crm.dto.expansion.ChannelExpansionItemDTO">
select
c.id,
'channel' as type,
2026-03-26 09:29:55 +00:00
coalesce(c.channel_code, '') as channelCode,
2026-03-20 08:39:07 +00:00
c.channel_name as name,
coalesce(c.province, '无') as province,
2026-03-26 09:29:55 +00:00
coalesce(c.office_address, '无') as officeAddress,
coalesce(c.channel_industry, c.industry, '无') as channelIndustry,
2026-03-20 08:39:07 +00:00
coalesce(cast(c.annual_revenue as varchar), '') as annualRevenue,
case
when c.annual_revenue is null then '无'
when c.annual_revenue >= 10000 then trim(to_char(c.annual_revenue / 10000.0, 'FM999999990.##')) || '万'
else trim(to_char(c.annual_revenue, 'FM999999990.##'))
end as revenue,
coalesce(c.staff_size, 0) as size,
2026-03-26 09:29:55 +00:00
coalesce(primary_contact.contact_name, c.contact_name, '无') as primaryContactName,
coalesce(primary_contact.contact_title, c.contact_title, '无') as primaryContactTitle,
coalesce(primary_contact.contact_mobile, c.contact_mobile, '无') as primaryContactMobile,
coalesce(to_char(c.contact_established_date, 'YYYY-MM-DD'), '无') as establishedDate,
c.intent_level as intentLevel,
case c.intent_level
when 'high' then '高'
when 'medium' then '中'
when 'low' then '低'
else '无'
end as intent,
coalesce(c.has_desktop_exp, false) as hasDesktopExp,
coalesce(channel_attribute_dict.item_label, c.channel_attribute, '无') as channelAttribute,
coalesce(internal_attribute_dict.item_label, c.internal_attribute, '无') as internalAttribute,
2026-03-20 08:39:07 +00:00
c.stage as stageCode,
case c.stage
when 'initial_contact' then '初步接触'
when 'solution_discussion' then '方案交流'
when 'bidding' then '招投标'
when 'business_negotiation' then '合作洽谈'
when 'won' then '已合作'
when 'lost' then '已终止'
else coalesce(c.stage, '无')
end as stage,
c.landed_flag as landed,
coalesce(to_char(c.expected_sign_date, 'YYYY-MM-DD'), '无') as expectedSignDate,
coalesce(c.remark, '无') as notes
from crm_channel_expansion c
2026-03-26 09:29:55 +00:00
left join lateral (
select
contact_name,
contact_title,
contact_mobile
from crm_channel_expansion_contact cc
where cc.channel_expansion_id = c.id
order by cc.sort_order asc nulls last, cc.id asc
limit 1
) primary_contact on true
left join sys_dict_item channel_attribute_dict
on channel_attribute_dict.type_code = 'tz_qdsx'
and channel_attribute_dict.item_value = c.channel_attribute
and channel_attribute_dict.status = 1
and coalesce(channel_attribute_dict.is_deleted, 0) = 0
left join sys_dict_item internal_attribute_dict
on internal_attribute_dict.type_code = 'tz_xhsnbsx'
and internal_attribute_dict.item_value = c.internal_attribute
and internal_attribute_dict.status = 1
and coalesce(internal_attribute_dict.is_deleted, 0) = 0
2026-03-27 04:22:00 +00:00
where 1 = 1
2026-03-20 08:39:07 +00:00
<if test="keyword != null and keyword != ''">
and (
c.channel_name ilike concat('%', #{keyword}, '%')
2026-03-26 09:29:55 +00:00
or coalesce(c.channel_code, '') ilike concat('%', #{keyword}, '%')
or coalesce(c.channel_industry, c.industry, '') ilike concat('%', #{keyword}, '%')
2026-03-20 08:39:07 +00:00
or coalesce(c.province, '') ilike concat('%', #{keyword}, '%')
2026-03-26 09:29:55 +00:00
or exists (
select 1
from crm_channel_expansion_contact cc
where cc.channel_expansion_id = c.id
and (
coalesce(cc.contact_name, '') ilike concat('%', #{keyword}, '%')
or coalesce(cc.contact_mobile, '') ilike concat('%', #{keyword}, '%')
or coalesce(cc.contact_title, '') ilike concat('%', #{keyword}, '%')
)
)
2026-03-20 08:39:07 +00:00
)
</if>
order by c.updated_at desc, c.id desc
</select>
<select id="selectSalesFollowUps" resultType="com.unis.crm.dto.expansion.ExpansionFollowUpDTO">
select
f.id,
f.biz_id as bizId,
f.biz_type as bizType,
f.followup_time as followUpTime,
2026-03-26 09:29:55 +00:00
to_char(f.followup_time, 'YYYY-MM-DD HH24:MI') as date,
2026-03-20 08:39:07 +00:00
f.followup_type as type,
coalesce(f.content, '无') as content,
2026-03-26 09:29:55 +00:00
coalesce(u.display_name, '无') as user,
coalesce(to_char(f.visit_start_time, 'YYYY-MM-DD HH24:MI'), '无') as visitStartTime,
coalesce(f.evaluation_content, '无') as evaluationContent,
coalesce(f.next_plan, '无') as nextPlan
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'
left join sys_user u on u.user_id = f.followup_user_id
2026-03-27 04:22:00 +00:00
where f.biz_id in
2026-03-20 08:39:07 +00:00
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by f.followup_time desc, f.id desc
</select>
2026-03-26 09:29:55 +00:00
<select id="selectSalesRelatedProjects" resultType="com.unis.crm.dto.expansion.RelatedProjectSummaryDTO">
select
o.sales_expansion_id as salesExpansionId,
o.id as opportunityId,
o.opportunity_code as opportunityCode,
o.opportunity_name as opportunityName,
o.amount
from crm_opportunity o
2026-03-27 04:22:00 +00:00
where o.sales_expansion_id in
2026-03-26 09:29:55 +00:00
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by coalesce(o.updated_at, o.created_at) desc, o.id desc
</select>
2026-03-20 08:39:07 +00:00
<select id="selectChannelFollowUps" resultType="com.unis.crm.dto.expansion.ExpansionFollowUpDTO">
select
f.id,
f.biz_id as bizId,
f.biz_type as bizType,
f.followup_time as followUpTime,
2026-03-26 09:29:55 +00:00
to_char(f.followup_time, 'YYYY-MM-DD HH24:MI') as date,
2026-03-20 08:39:07 +00:00
f.followup_type as type,
coalesce(f.content, '无') as content,
2026-03-26 09:29:55 +00:00
coalesce(u.display_name, '无') as user,
coalesce(to_char(f.visit_start_time, 'YYYY-MM-DD HH24:MI'), '无') as visitStartTime,
coalesce(f.evaluation_content, '无') as evaluationContent,
coalesce(f.next_plan, '无') as nextPlan
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'
left join sys_user u on u.user_id = f.followup_user_id
2026-03-27 04:22:00 +00:00
where f.biz_id in
2026-03-20 08:39:07 +00:00
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by f.followup_time desc, f.id desc
</select>
2026-03-26 09:29:55 +00:00
<select id="selectChannelContacts" resultType="com.unis.crm.dto.expansion.ChannelExpansionContactDTO">
select
cc.id,
cc.channel_expansion_id as channelExpansionId,
coalesce(cc.contact_name, '无') as name,
coalesce(cc.contact_mobile, '无') as mobile,
coalesce(cc.contact_title, '无') as title
from crm_channel_expansion_contact cc
join crm_channel_expansion c on c.id = cc.channel_expansion_id
2026-03-27 04:22:00 +00:00
where cc.channel_expansion_id in
2026-03-26 09:29:55 +00:00
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by cc.sort_order asc nulls last, cc.id asc
</select>
<select id="selectChannelRelatedProjects" resultType="com.unis.crm.dto.expansion.ChannelRelatedProjectSummaryDTO">
select
o.channel_expansion_id as channelExpansionId,
o.id as opportunityId,
o.opportunity_code as opportunityCode,
o.opportunity_name as opportunityName,
o.amount
from crm_opportunity o
2026-03-27 04:22:00 +00:00
where o.channel_expansion_id in
2026-03-26 09:29:55 +00:00
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by coalesce(o.updated_at, o.created_at) desc, o.id desc
</select>
2026-03-20 08:39:07 +00:00
<insert id="insertSalesExpansion" useGeneratedKeys="true" keyProperty="request.id">
insert into crm_sales_expansion (
2026-03-26 09:29:55 +00:00
employee_no,
2026-03-20 08:39:07 +00:00
candidate_name,
2026-03-26 09:29:55 +00:00
office_name,
2026-03-20 08:39:07 +00:00
mobile,
email,
2026-03-26 09:29:55 +00:00
target_dept,
2026-03-20 08:39:07 +00:00
industry,
title,
intent_level,
stage,
has_desktop_exp,
in_progress,
employment_status,
expected_join_date,
owner_user_id,
remark
) values (
2026-03-26 09:29:55 +00:00
#{request.employeeNo},
2026-03-20 08:39:07 +00:00
#{request.candidateName},
2026-03-26 09:29:55 +00:00
#{request.officeName},
2026-03-20 08:39:07 +00:00
#{request.mobile},
#{request.email},
2026-03-26 09:29:55 +00:00
#{request.targetDept},
2026-03-20 08:39:07 +00:00
#{request.industry},
#{request.title},
#{request.intentLevel},
#{request.stage},
#{request.hasDesktopExp},
#{request.inProgress},
#{request.employmentStatus},
#{request.expectedJoinDate},
#{userId},
#{request.remark}
)
</insert>
<insert id="insertChannelExpansion" useGeneratedKeys="true" keyProperty="request.id">
insert into crm_channel_expansion (
2026-03-26 09:29:55 +00:00
channel_code,
2026-03-20 08:39:07 +00:00
province,
2026-03-26 09:29:55 +00:00
channel_name,
office_address,
channel_industry,
2026-03-20 08:39:07 +00:00
annual_revenue,
staff_size,
2026-03-26 09:29:55 +00:00
contact_established_date,
intent_level,
has_desktop_exp,
2026-03-20 08:39:07 +00:00
contact_name,
contact_title,
contact_mobile,
2026-03-26 09:29:55 +00:00
channel_attribute,
internal_attribute,
2026-03-20 08:39:07 +00:00
stage,
landed_flag,
expected_sign_date,
owner_user_id,
remark
) values (
2026-03-26 09:29:55 +00:00
'QD-' || to_char(current_date, 'YYYYMMDD') || '-' || lpad((
coalesce((
select count(1)
from crm_channel_expansion
where created_at::date = current_date
), 0) + 1
)::text, 3, '0'),
2026-03-20 08:39:07 +00:00
#{request.province},
2026-03-26 09:29:55 +00:00
#{request.channelName},
#{request.officeAddress},
#{request.channelIndustry},
2026-03-20 08:39:07 +00:00
#{request.annualRevenue},
#{request.staffSize},
2026-03-26 09:29:55 +00:00
#{request.contactEstablishedDate},
#{request.intentLevel},
#{request.hasDesktopExp},
null,
null,
null,
#{request.channelAttribute},
#{request.internalAttribute},
2026-03-20 08:39:07 +00:00
#{request.stage},
#{request.landedFlag},
#{request.expectedSignDate},
#{userId},
#{request.remark}
)
</insert>
2026-03-26 09:29:55 +00:00
<insert id="insertChannelContact">
insert into crm_channel_expansion_contact (
channel_expansion_id,
contact_name,
contact_mobile,
contact_title,
sort_order,
created_at,
updated_at
) values (
#{channelExpansionId},
#{contact.name},
#{contact.mobile},
#{contact.title},
#{sortOrder},
now(),
now()
)
</insert>
<delete id="deleteChannelContacts">
delete from crm_channel_expansion_contact
where channel_expansion_id = #{channelExpansionId}
</delete>
2026-03-20 08:39:07 +00:00
<update id="updateSalesExpansion">
update crm_sales_expansion
2026-03-26 09:29:55 +00:00
set employee_no = #{request.employeeNo},
candidate_name = #{request.candidateName},
office_name = #{request.officeName},
2026-03-20 08:39:07 +00:00
mobile = #{request.mobile},
email = #{request.email},
2026-03-26 09:29:55 +00:00
target_dept = #{request.targetDept},
2026-03-20 08:39:07 +00:00
industry = #{request.industry},
title = #{request.title},
intent_level = #{request.intentLevel},
stage = #{request.stage},
has_desktop_exp = #{request.hasDesktopExp},
in_progress = #{request.inProgress},
employment_status = #{request.employmentStatus},
expected_join_date = #{request.expectedJoinDate},
remark = #{request.remark}
where id = #{id}
and owner_user_id = #{userId}
</update>
2026-03-26 09:29:55 +00:00
<select id="countSalesExpansionByEmployeeNo" resultType="int">
select count(1)
from crm_sales_expansion
where owner_user_id = #{userId}
and employee_no = #{employeeNo}
</select>
<select id="countSalesExpansionByEmployeeNoExcludingId" resultType="int">
select count(1)
from crm_sales_expansion
where owner_user_id = #{userId}
and employee_no = #{employeeNo}
and id &lt;&gt; #{excludeId}
</select>
2026-03-20 08:39:07 +00:00
<update id="updateChannelExpansion">
update crm_channel_expansion
set channel_name = #{request.channelName},
province = #{request.province},
2026-03-26 09:29:55 +00:00
office_address = #{request.officeAddress},
channel_industry = #{request.channelIndustry},
2026-03-20 08:39:07 +00:00
annual_revenue = #{request.annualRevenue},
staff_size = #{request.staffSize},
2026-03-26 09:29:55 +00:00
contact_established_date = #{request.contactEstablishedDate},
intent_level = #{request.intentLevel},
has_desktop_exp = #{request.hasDesktopExp},
contact_name = null,
contact_title = null,
contact_mobile = null,
channel_attribute = #{request.channelAttribute},
internal_attribute = #{request.internalAttribute},
2026-03-20 08:39:07 +00:00
stage = #{request.stage},
landed_flag = #{request.landedFlag},
expected_sign_date = #{request.expectedSignDate},
remark = #{request.remark}
where id = #{id}
and owner_user_id = #{userId}
</update>
<select id="countOwnedSalesExpansion" resultType="int">
select count(1)
from crm_sales_expansion
where id = #{id}
and owner_user_id = #{userId}
</select>
<select id="countOwnedChannelExpansion" resultType="int">
select count(1)
from crm_channel_expansion
where id = #{id}
and owner_user_id = #{userId}
</select>
<insert id="insertExpansionFollowUp">
insert into crm_expansion_followup (
biz_type,
biz_id,
followup_time,
followup_type,
content,
next_action,
2026-03-26 09:29:55 +00:00
followup_user_id,
visit_start_time,
evaluation_content,
next_plan
2026-03-20 08:39:07 +00:00
) values (
#{bizType},
#{bizId},
#{request.followUpTime},
#{request.followUpType},
#{request.content},
#{request.nextAction},
2026-03-26 09:29:55 +00:00
#{userId},
#{request.visitStartTime},
#{request.evaluationContent},
#{request.nextPlan}
2026-03-20 08:39:07 +00:00
)
</insert>
</mapper>