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 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()
)
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}
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()
)