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

481 lines
19 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.ExpansionMapper">
<select id="selectDictItems" resultType="com.unis.crm.dto.expansion.DictOptionDTO">
select
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')
</select>
<select id="selectSalesExpansions" resultType="com.unis.crm.dto.expansion.SalesExpansionItemDTO">
select
s.id,
'sales' as type,
coalesce(s.employee_no, '无') as employeeNo,
s.candidate_name as name,
coalesce(office_dict.item_value, s.office_name, '') as officeCode,
coalesce(office_dict.item_label, nullif(s.office_name, ''), '无') as officeName,
coalesce(s.mobile, '无') as phone,
coalesce(s.email, '无') as email,
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,
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
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
where 1 = 1
<if test="keyword != null and keyword != ''">
and (
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}, '%')
)
</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,
coalesce(c.channel_code, '') as channelCode,
c.channel_name as name,
coalesce(c.province, '无') as province,
coalesce(c.office_address, '无') as officeAddress,
coalesce(c.channel_industry, c.industry, '无') as channelIndustry,
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,
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(c.channel_attribute, '') as channelAttributeCode,
coalesce(channel_attribute_dict.item_label, c.channel_attribute, '无') as channelAttribute,
coalesce(c.internal_attribute, '') as internalAttributeCode,
coalesce(internal_attribute_dict.item_label, c.internal_attribute, '无') as internalAttribute,
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
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
where 1 = 1
<if test="keyword != null and keyword != ''">
and (
c.channel_name ilike concat('%', #{keyword}, '%')
or coalesce(c.channel_code, '') ilike concat('%', #{keyword}, '%')
or coalesce(c.channel_industry, c.industry, '') ilike concat('%', #{keyword}, '%')
or coalesce(c.province, '') ilike concat('%', #{keyword}, '%')
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}, '%')
)
)
)
</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,
to_char(f.followup_time, 'YYYY-MM-DD HH24:MI') as date,
f.followup_type as type,
coalesce(f.content, '无') as content,
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
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
where f.biz_id in
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by f.followup_time desc, f.id desc
</select>
<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
where o.sales_expansion_id in
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by coalesce(o.updated_at, o.created_at) desc, o.id desc
</select>
<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,
to_char(f.followup_time, 'YYYY-MM-DD HH24:MI') as date,
f.followup_type as type,
coalesce(f.content, '无') as content,
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
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
where f.biz_id in
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by f.followup_time desc, f.id desc
</select>
<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
where cc.channel_expansion_id in
<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
where o.channel_expansion_id in
<foreach collection="bizIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by coalesce(o.updated_at, o.created_at) desc, o.id desc
</select>
<insert id="insertSalesExpansion" useGeneratedKeys="true" keyProperty="request.id">
insert into crm_sales_expansion (
employee_no,
candidate_name,
office_name,
mobile,
email,
target_dept,
industry,
title,
intent_level,
stage,
has_desktop_exp,
in_progress,
employment_status,
expected_join_date,
owner_user_id,
remark
) values (
#{request.employeeNo},
#{request.candidateName},
#{request.officeName},
#{request.mobile},
#{request.email},
#{request.targetDept},
#{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 (
channel_code,
province,
channel_name,
office_address,
channel_industry,
annual_revenue,
staff_size,
contact_established_date,
intent_level,
has_desktop_exp,
contact_name,
contact_title,
contact_mobile,
channel_attribute,
internal_attribute,
stage,
landed_flag,
expected_sign_date,
owner_user_id,
remark
) values (
'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'),
#{request.province},
#{request.channelName},
#{request.officeAddress},
#{request.channelIndustry},
#{request.annualRevenue},
#{request.staffSize},
#{request.contactEstablishedDate},
#{request.intentLevel},
#{request.hasDesktopExp},
null,
null,
null,
#{request.channelAttribute},
#{request.internalAttribute},
#{request.stage},
#{request.landedFlag},
#{request.expectedSignDate},
#{userId},
#{request.remark}
)
</insert>
<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>
<update id="updateSalesExpansion">
update crm_sales_expansion s
set employee_no = #{request.employeeNo},
candidate_name = #{request.candidateName},
office_name = #{request.officeName},
mobile = #{request.mobile},
email = #{request.email},
target_dept = #{request.targetDept},
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 s.id = #{id}
</update>
<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>
<update id="updateChannelExpansion">
update crm_channel_expansion c
set channel_name = #{request.channelName},
province = #{request.province},
office_address = #{request.officeAddress},
channel_industry = #{request.channelIndustry},
annual_revenue = #{request.annualRevenue},
staff_size = #{request.staffSize},
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},
stage = #{request.stage},
landed_flag = #{request.landedFlag},
expected_sign_date = #{request.expectedSignDate},
remark = #{request.remark}
where c.id = #{id}
</update>
<select id="countOwnedSalesExpansion" resultType="int">
select count(1)
from crm_sales_expansion s
where s.id = #{id}
</select>
<select id="countOwnedChannelExpansion" resultType="int">
select count(1)
from crm_channel_expansion c
where c.id = #{id}
</select>
<insert id="insertExpansionFollowUp">
insert into crm_expansion_followup (
biz_type,
biz_id,
followup_time,
followup_type,
content,
next_action,
followup_user_id,
visit_start_time,
evaluation_content,
next_plan
) values (
#{bizType},
#{bizId},
#{request.followUpTime},
#{request.followUpType},
#{request.content},
#{request.nextAction},
#{userId},
#{request.visitStartTime},
#{request.evaluationContent},
#{request.nextPlan}
)
</insert>
</mapper>