198 lines
7.0 KiB
XML
198 lines
7.0 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.OpportunityMapper">
|
|
|
|
<select id="selectOpportunities" resultType="com.unis.crm.dto.opportunity.OpportunityItemDTO">
|
|
select
|
|
o.id,
|
|
o.opportunity_code as code,
|
|
o.opportunity_name as name,
|
|
coalesce(c.customer_name, '未命名客户') as client,
|
|
coalesce(u.display_name, '当前用户') as owner,
|
|
o.amount,
|
|
to_char(o.expected_close_date, 'YYYY-MM-DD') as date,
|
|
o.confidence_pct as confidence,
|
|
case coalesce(o.stage, 'initial_contact')
|
|
when 'initial_contact' then '初步沟通'
|
|
when 'solution_discussion' then '方案交流'
|
|
when 'bidding' then '招投标'
|
|
when 'business_negotiation' then '商务谈判'
|
|
when 'won' then '已成交'
|
|
when 'lost' then '已放弃'
|
|
else coalesce(o.stage, '初步沟通')
|
|
end as stage,
|
|
coalesce(o.opportunity_type, '新建') as type,
|
|
coalesce(o.pushed_to_oms, false) as pushedToOms,
|
|
coalesce(o.product_type, 'VDI云桌面') as product,
|
|
coalesce(o.source, '主动开发') as source,
|
|
coalesce(o.description, '') as notes
|
|
from crm_opportunity o
|
|
left join crm_customer c on c.id = o.customer_id
|
|
left join sys_user u on u.user_id = o.owner_user_id
|
|
where o.owner_user_id = #{userId}
|
|
<if test="keyword != null and keyword != ''">
|
|
and (
|
|
o.opportunity_name ilike concat('%', #{keyword}, '%')
|
|
or o.opportunity_code ilike concat('%', #{keyword}, '%')
|
|
or coalesce(c.customer_name, '') ilike concat('%', #{keyword}, '%')
|
|
)
|
|
</if>
|
|
<if test="stage != null and stage != ''">
|
|
and o.stage = #{stage}
|
|
</if>
|
|
order by coalesce(o.updated_at, o.created_at) desc, o.id desc
|
|
</select>
|
|
|
|
<select id="selectOpportunityFollowUps" resultType="com.unis.crm.dto.opportunity.OpportunityFollowUpDTO">
|
|
select
|
|
f.id,
|
|
f.opportunity_id as opportunityId,
|
|
to_char(f.followup_time, 'YYYY-MM-DD HH24:MI') as date,
|
|
coalesce(f.followup_type, '无') as type,
|
|
coalesce(f.content, '无') as content,
|
|
coalesce(u.display_name, '无') as user
|
|
from crm_opportunity_followup f
|
|
join crm_opportunity o on o.id = f.opportunity_id
|
|
left join sys_user u on u.user_id = f.followup_user_id
|
|
where o.owner_user_id = #{userId}
|
|
and f.opportunity_id in
|
|
<foreach collection="opportunityIds" item="id" open="(" separator="," close=")">
|
|
#{id}
|
|
</foreach>
|
|
order by f.followup_time desc, f.id desc
|
|
</select>
|
|
|
|
<select id="selectOwnedCustomerIdByName" resultType="java.lang.Long">
|
|
select id
|
|
from crm_customer
|
|
where owner_user_id = #{userId}
|
|
and customer_name = #{customerName}
|
|
limit 1
|
|
</select>
|
|
|
|
<insert id="insertCustomer">
|
|
insert into crm_customer (
|
|
id,
|
|
customer_code,
|
|
customer_name,
|
|
owner_user_id,
|
|
source,
|
|
status,
|
|
created_at,
|
|
updated_at
|
|
) values (
|
|
#{id},
|
|
'CUS-' || to_char(current_date, 'YYYYMMDD') || '-' || lpad((coalesce((select count(1) from crm_customer), 0) + 1)::text, 3, '0'),
|
|
#{customerName},
|
|
#{userId},
|
|
coalesce(#{source}, '主动开发'),
|
|
'following',
|
|
now(),
|
|
now()
|
|
)
|
|
</insert>
|
|
|
|
<insert id="insertOpportunity" useGeneratedKeys="true" keyProperty="request.id">
|
|
insert into crm_opportunity (
|
|
opportunity_code,
|
|
opportunity_name,
|
|
customer_id,
|
|
owner_user_id,
|
|
amount,
|
|
expected_close_date,
|
|
confidence_pct,
|
|
stage,
|
|
opportunity_type,
|
|
product_type,
|
|
source,
|
|
pushed_to_oms,
|
|
oms_push_time,
|
|
description,
|
|
status,
|
|
created_at,
|
|
updated_at
|
|
) values (
|
|
'OPP-' || to_char(current_date, 'YYYYMMDD') || '-' || lpad((coalesce((select count(1) from crm_opportunity), 0) + 1)::text, 3, '0'),
|
|
#{request.opportunityName},
|
|
#{customerId},
|
|
#{userId},
|
|
#{request.amount},
|
|
#{request.expectedCloseDate},
|
|
#{request.confidencePct},
|
|
#{request.stage},
|
|
#{request.opportunityType},
|
|
#{request.productType},
|
|
#{request.source},
|
|
#{request.pushedToOms},
|
|
case when #{request.pushedToOms} then now() else null end,
|
|
#{request.description},
|
|
case
|
|
when #{request.stage} = 'won' then 'won'
|
|
when #{request.stage} = 'lost' then 'lost'
|
|
else 'active'
|
|
end,
|
|
now(),
|
|
now()
|
|
)
|
|
</insert>
|
|
|
|
<select id="countOwnedOpportunity" resultType="int">
|
|
select count(1)
|
|
from crm_opportunity
|
|
where id = #{id}
|
|
and owner_user_id = #{userId}
|
|
</select>
|
|
|
|
<update id="updateOpportunity">
|
|
update crm_opportunity
|
|
set opportunity_name = #{request.opportunityName},
|
|
customer_id = #{customerId},
|
|
amount = #{request.amount},
|
|
expected_close_date = #{request.expectedCloseDate},
|
|
confidence_pct = #{request.confidencePct},
|
|
stage = #{request.stage},
|
|
opportunity_type = #{request.opportunityType},
|
|
product_type = #{request.productType},
|
|
source = #{request.source},
|
|
pushed_to_oms = #{request.pushedToOms},
|
|
oms_push_time = case
|
|
when #{request.pushedToOms} then coalesce(oms_push_time, now())
|
|
else null
|
|
end,
|
|
description = #{request.description},
|
|
status = case
|
|
when #{request.stage} = 'won' then 'won'
|
|
when #{request.stage} = 'lost' then 'lost'
|
|
else 'active'
|
|
end,
|
|
updated_at = now()
|
|
where id = #{opportunityId}
|
|
and owner_user_id = #{userId}
|
|
</update>
|
|
|
|
<insert id="insertOpportunityFollowUp">
|
|
insert into crm_opportunity_followup (
|
|
opportunity_id,
|
|
followup_time,
|
|
followup_type,
|
|
content,
|
|
next_action,
|
|
followup_user_id,
|
|
created_at,
|
|
updated_at
|
|
) values (
|
|
#{opportunityId},
|
|
#{request.followUpTime},
|
|
#{request.followUpType},
|
|
#{request.content},
|
|
#{request.nextAction},
|
|
#{userId},
|
|
now(),
|
|
now()
|
|
)
|
|
</insert>
|
|
|
|
</mapper>
|