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 get_sales_volume(ns={}): orgid = ns.get('orgid') if ns.get('orgid') else 'mIWUHBeeDM8mwAFPIQ8pS' db = DBPools() async with db.sqlorContext('kboss') as sor: count_sql = f""" SELECT month_list.month, COALESCE(SUM(b.amount), 0) AS total_sales FROM (SELECT DATE_FORMAT(date_add(@start_date, INTERVAL t.i MONTH), '%%Y-%%m') AS month FROM (SELECT @start_date := '2024-03-01') AS vars JOIN (SELECT @row := @row + 1 AS i FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) a, (SELECT @row := -1) b ) t WHERE DATE_FORMAT(date_add(@start_date, INTERVAL t.i MONTH), '%%Y-%%m') <= DATE_FORMAT(NOW(), '%%Y-%%m') ) month_list LEFT JOIN (SELECT o.id AS orgid, bc.* FROM bill bc JOIN organization o ON bc.customerid = o.id WHERE bc.business_op = 'BUY' AND o.parentid = '{orgid}') b ON DATE_FORMAT(b.bill_date, '%%Y-%%m') = month_list.month AND b.business_op = 'BUY' GROUP BY month_list.month ORDER BY month_list.month LIMIT 20;""" count_li = await sor.sqlExe(count_sql, {}) # for index, sale in enumerate(count_li): # sale['total_sales'] = round(sale['total_sales'] + 4 * 1000000 + (index + 1) * 10024, 2) return { 'status': True, 'msg': '获取销售额记录成功', 'data': count_li } async def get_product_volume_rank(ns={}): orgid = ns.get('orgid') if ns.get('orgid') else 'mIWUHBeeDM8mwAFPIQ8pS' # ns = { # 'target': 'month' # } db = DBPools() async with db.sqlorContext('kboss') as sor: if not ns.get('target'): count_sql = f""" SELECT p.id, p.name AS product_name, SUM(b.amount) AS total_sales FROM (SELECT o.id AS orgid, bc.* FROM bill bc JOIN organization o ON bc.customerid = o.id WHERE bc.business_op = 'BUY' AND o.parentid = '{orgid}') b JOIN product p ON b.productid = p.id WHERE b.business_op = 'BUY' GROUP BY p.id, p.name ORDER BY total_sales DESC LIMIT 20;""" else: count_sql = """ SELECT p.id, p.name AS product_name, DATE_FORMAT(b.bill_date, '%%Y-%%m') AS month, SUM(b.amount) AS total_sales FROM bill b JOIN product p ON b.productid = p.id WHERE b.business_op = 'BUY' GROUP BY p.id, month ORDER BY month, total_sales; -- 可根据需求调整排序 """ count_li = await sor.sqlExe(count_sql, {}) # for index, sale in enumerate(count_li): # sale['total_sales'] = round(sale['total_sales'] + 4 * 10000 + (index + 1) * 10024, 2) return { 'status': True, 'msg': '获取产品销售额成功', 'data': count_li } async def large_screen_fourth_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 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) y = [6580120, 6961020, 7023660, 8022110, 10132200, 1204560, 21863460, 17176000, 9089400, 5675230] # 获取产品销售额排名 get_product_volume_rank_res = await get_product_volume_rank({'orgid': orgid}) get_product_volume_name = [] get_product_volume_value = [] for i in get_product_volume_rank_res['data']: get_product_volume_name.append(i['product_name']) get_product_volume_value.append(i['total_sales']) if ns.get('flag') == '1': LeftUp = { 'configname': '产品销售额排名', 'x': get_product_volume_name, 'y': get_product_volume_value } else: LeftUp = { 'configname': '产品销售额排名', 'x': ['定制SAAS', '互联DCI', '专线加速', '定制托管', '融合云', '智算A800', 'RTX3090', '智算4090'], 'y': [3801057, 3002232, 2700227, 2222940, 1902310, 1702326, 1501230, 1235000] } # 获取销售额 get_sales_volume_res = await get_sales_volume({'orgid': orgid}) get_sales_volume_month = [] get_sales_volume_value = [] for i in get_sales_volume_res['data']: get_sales_volume_month.append(i['month']) get_sales_volume_value.append(i['total_sales']) if ns.get('flag') == '1': RightDown = { 'configname': '销售额', 'x': get_sales_volume_month, 'y': get_sales_volume_value } else: RightDown = { 'configname': '销售额', 'x': x[::-1], 'y': [1502350, 1701232, 1602326, 1902310, 1802294, 2202278, 2262262, 2302246, 3115382, 3315366, 3631550] } # 获取消费排行 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': RightUp = { 'configname': '用户消费排名', 'x': get_consumer_name[:10], 'y': get_consumer_value[:10] } else: RightUp = { 'configname': '用户消费排名', 'data': [ {'name': '阿**信息', 'value': '702056'}, {'name': '北**科技', 'value': '758012'}, {'name': '学**教育', 'value': '796102'}, {'name': '摩**科技', 'value': '812366'}, {'name': '长**时代', 'value': '852211'}, {'name': '象*科技', 'value': '1013220'}, {'name': '小**信息', 'value': '1204560'}, {'name': '领*科技', 'value': '1986346'}, {'name': '点*科技', 'value': '2717600'}, {'name': '百*科技', 'value': '2789400'} ] } return { 'LeftUp': LeftUp, 'LeftDown': { 'configname': '地域销售额排行', 'x': ['济南', '无锡', '广州', '青岛', '北京', '长沙', '深圳', '天津', '乌兰察布', '乌鲁木齐'], 'y': [1002210, 1513220, 1804560, 902211, 1513220, 1804560, 2486346, 2817600, 3189400, 3875230] }, 'RightUp': RightUp, 'RightDown': RightDown } ret = await large_screen_fourth_page(params_kw) return ret