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

189 lines
6.8 KiB
Plaintext

async def get_recharge_rank(ns={}):
orgid = ns.get('orgid') if ns.get('orgid') else 'mIWUHBeeDM8mwAFPIQ8pS'
db = DBPools()
async with db.sqlorContext('kboss') as sor:
count_sql = f"""
SELECT o.orgname AS user_name, o.id AS orgid,
SUM(CASE
WHEN r.action = 'RECHARGE' AND recharge_date IS NOT NULL THEN r.recharge_amt
WHEN r.action = 'RECHARGE_ALIPAY' AND r.recharge_sno IS NOT NULL THEN r.recharge_amt
WHEN r.action = 'RECHARGE_REVERSE' THEN -r.recharge_amt
WHEN r.action = 'RECHARGE_ALIPAY_REVERSE' AND r.recharge_sno IS NOT NULL THEN -r.recharge_amt
ELSE 0
END) AS actual_recharge_amt
FROM
recharge_log r
JOIN
organization o ON r.customerid = o.id
AND
o.parentid = '{orgid}'
GROUP BY
o.id, o.orgname
ORDER BY
actual_recharge_amt DESC
LIMIT 20;"""
count_li = await sor.sqlExe(count_sql, {})
for user in count_li:
user['user_name'] = user['user_name'][0] + '*****'
return {
'status': True,
'msg': '获取充值排行记录成功',
'data': count_li
}
async def get_consumer_rank(ns={}):
# ns = {
# 'target': '个人/公司'
# }
orgid = ns.get('orgid') if ns.get('orgid') else 'mIWUHBeeDM8mwAFPIQ8pS'
db = DBPools()
async with db.sqlorContext('kboss') as sor:
if not ns.get('target'):
count_sql = f"""
SELECT o.orgname AS user_name, o.id AS orgid, o.org_type AS org_type,
SUM(b.amount) AS total_purchase_amount
FROM
bill b
JOIN
organization o ON b.customerid = o.id
WHERE
b.business_op = 'BUY'
AND
o.parentid = '{orgid}'
GROUP BY
o.id, o.orgname
ORDER BY
total_purchase_amount ASC
LIMIT 20;"""
else:
count_sql = """
SELECT o.org_type AS org_type,
SUM(b.amount) AS total_purchase_amount
FROM
bill b
JOIN
organization o ON b.customerid = o.id
WHERE
b.business_op = 'BUY'
GROUP BY
org_type
ORDER BY
total_purchase_amount ASC
"""
count_li = await sor.sqlExe(count_sql, {})
if ns.get('target'):
for user in count_li:
total_purchase = sum([i['total_purchase_amount'] for i in count_li])
if user.get('org_type') == '2':
user['org_type'] = '公司客户'
user['rate'] = round(user['total_purchase_amount'] / total_purchase, 3)
else:
user['org_type'] = '个人客户'
user['rate'] = round(user['total_purchase_amount'] / total_purchase, 3)
else:
for index, user in enumerate(count_li):
user['user_name'] = user['user_name'][:2] + '*****'
# user['total_purchase_amount'] = user['total_purchase_amount']
return {
'status': True,
'msg': '获取消费排行记录成功',
'data': count_li
}
async def large_screen_second_page(ns={}):
orgid = ns.get('orgid') if ns.get('orgid') else 'mIWUHBeeDM8mwAFPIQ8pS'
n = 9
# 获取当前时间戳
now = time.time()
# 获取当前年月
current_time = time.localtime(now)
current_year = current_time.tm_year
current_month = current_time.tm_mon
# 打印当前月份
print(f"{current_year}-{current_month:02d}")
x = []
y = []
for i in range(0, n + 1):
# 计算之前的月份
month = current_month - i
year = current_year
if month <= 0:
# 处理年份的变更
year -= (abs(month) // 12) + 1
if month % 12 == 0:
month = 12
else:
month = (month % 12)
x.append(f"{year}-{month:02d}")
y.append(int(f"{year}{month}") * 16 + 78390)
# 获取充值排行
recharge_rank_res = await get_recharge_rank({'orgid': orgid})
recharge_rank_name = []
recharge_rank_value = []
for i in recharge_rank_res['data']:
recharge_rank_name.append(i['user_name'])
recharge_rank_value.append(i['actual_recharge_amt'])
if ns.get('flag') == '1':
LeftDown = {
'configname': '用户充值排行',
'x': recharge_rank_name[:10],
'y': recharge_rank_value[:10]
}
else:
LeftDown = {
'configname': '用户充值排行',
'x': ['阿**信息', '国*科技', '学**教育', '科**科技', '长沙**时代', '希**技术', '小**信息',
'易**科技', '点**科技', '百**科技'],
'y': [4875230, 3689400, 3017600, 2986346, 1804560, 1513220, 1002211, 902366, 796102, 758012]
}
# 获取消费排行
get_consumer_rank_res = await get_consumer_rank({'orgid': orgid})
get_consumer_name = []
get_consumer_value = []
for i in get_consumer_rank_res['data']:
get_consumer_name.append(i['user_name'])
get_consumer_value.append(i['total_purchase_amount'])
if ns.get('flag') == '1':
RightDown = {
'configname': '用户消费排名',
'x': get_consumer_name[:10],
'y': get_consumer_value[:10]
}
else:
RightDown = {
'configname': '用户消费排名',
'x': ['阿**信息', '中科**科技', '学**教育', '摩**科技', '长沙**时代', '象*科技', '小**信息',
'领*科技', '点**科技', '百**科技'],
'y': [402056, 658012, 696102, 702366, 802211, 1013220, 1204560, 2186346, 2717600, 3675230]
}
return {
'LeftUp': {
'configname': '用户种类分析',
'data':[
{'value': 600, 'name': '自动驾驶'},
{'value': 400, 'name': '游戏渲染'},
{'value': 200, 'name': '金融分析'},
{'value': 800, 'name': '智慧医疗'},
{'value': 1000,'name': '智能制造'}
]
},
'LeftDown': LeftDown,
'RightUp': {
'configname': '用户活跃度',
'x': x[::-1],
'y': [621, 842, 892, 763, 931, 1032, 792, 1293, 1432, 985]
},
'RightDown': RightDown,
}
ret = await large_screen_second_page(params_kw)
return ret