salescrm/b/floating/apply.dspy
2025-10-27 15:50:44 +08:00

152 lines
6.7 KiB
Plaintext

async def CheckSmsCode(ns={}):
db = DBPools()
async with db.sqlorContext('kboss') as sor:
vcode_sql = "select * from sms_record where mobile = '%s' order by send_time desc limit 1;" % ns.get('mobile')
vcode_li = await sor.sqlExe(vcode_sql, {})
if vcode_li:
date_str = vcode_li[0]['send_time']
target_datetime = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
delta = datetime.datetime.now() - target_datetime
# 阈值限制 10 分钟
if delta.total_seconds() > 60 * 10:
return {
'status': False,
'msg': '验证码超时失效'
}
if json.loads(vcode_li[0]['message']).get('code') == ns.get('vcode'):
return {
'status': True,
'msg': '验证码验证通过'
}
return {
'status': False,
'msg': '验证码错误'
}
async def apply(ns={}):
"""
userid传用户id
customer客户名
phone客户电话
unit客户单位
product客户资源名称
resources客户资源需求
:param ns:
:return:
"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
if ns.get('userid'):
ns['product'] = 1
user = await sor.R('users', {'id': await get_user()})
if not user:
return {'status': False, 'msg': 'User does not exist'}
if not ns['customer'] or not ns['phone'] or not ns['product'] or not ns['unit'] or not ns['resources']:
return {'status': False, 'msg': 'Required information is missing'}
uuids = str(uuid())
uuid_digits = 21
uuid_gen = uuids[:uuid_digits].replace('-', '')
data = {
'id': uuid_gen,
'custid': ns['userid'],
'customer': ns['customer'],
'phone': ns['phone'],
'unit': ns['unit'],
'product': ns['product'],
'resources': str(ns['resources'])
}
await sor.C('cust_message', data)
orgid = user[0]['orgid']
sales = await sor.R('customer', {'customerid': orgid})
if sales:
for sale in sales:
saleid = sale['salemanid']
sales_info = await sor.R('users', {'id': saleid})
if sales_info:
# print(sales_info)
sales_mobile = sales_info[0]['mobile']
# print(sales_mobile)
#发送短信
sms_send_dict_saleman = {
'customer': ns['customer'],
'phone': ns['phone'],
}
# print(sms_send_dict_saleman)
try:
# 执行发送短信
await send_vcode(sales_mobile, '客户申请试用留言通知', sms_send_dict_saleman)
except Exception as e:
return {'status': False, 'msg': f'SMS send failed: {str(e)}'}
# 发送站内信
msg_config = await sor.R('message_config', {'msgtype': '客户申请试用留言通知','del_flg':'0'})
if not msg_config:
return {'status': False, 'msg': '没有找到id记录'}
else:
msgtype_template = msg_config[0]['msgtype']
msgstext_template = msg_config[0]['msgtext']
msgstatus = msg_config[0]['msgstatus']
if msgstatus == '1':
return {'status': False, 'msg': '状态禁止发送站内信'}
elif not msgtype_template:
return {'status': False, 'msg': '找不到该模板类型'}
elif not msgstext_template:
return {'status': False, 'msg': '找不到模板内容'}
else:
customer = ns['customer']
phone = ns['phone']
product = ns['product']
resources = str(ns['resources'])
msgtype = msgtype_template
msgtext = msgstext_template.format(customer=customer, phone=phone, resources=resources,
product=product)
uid = str(uuid())
uid_gen = uid[:21].replace('-', '')
ns_saleman = {
'id': uid_gen,
'senderid': 'inner',
'receiverid': sales_info[0]['id'],
'msgtype': msgtype,
'msgtext': msgtext
}
await sor.C('message', ns_saleman)
else:
ns_inten = {
"id": uuid(),
"name": ns.get('customer'),
"mobile": ns.get('phone'),
"email": "",
"organization": ns.get('unit'),
"intention": str(ns.get('resources'))
}
await sor.C('intention_customer', ns_inten)
return {'status': True, 'msg': 'user is not bingding saleman'}
return {'status': True, 'msg': 'Data inserted successfully and sales information retrieved'}
else:
# check mobile vcode
if ns.get('phone') and ns.get('vcode'):
check_vcode = await CheckSmsCode({'mobile': ns.get('phone'), 'vcode': ns.get('vcode')})
if not check_vcode.get('status'):
return check_vcode
else:
return {
'status': False,
'msg': '没有获取到手机号或短信验证码'
}
ns_inten = {
"id": uuid(),
"name": ns.get('customer'),
"mobile": ns.get('phone'),
"email": "",
"organization": ns.get('unit'),
"intention": "1"
}
await sor.C('intention_customer', ns_inten)
return {'status': True, 'msg': 'User ID is missing'}
ret = await apply(params_kw)
return ret