async def tongbi_huanbi(ns={}): """ 计算同比, 环比 :param ns: :return: """ date_m = ns.get('date_m') number = ns.get('number') # 构建一个dataframe data = pandas.DataFrame({'date_m': date_m, 'number': number}) data['huanbi'] = data.number.pct_change() # 填充 data.fillna(0, inplace=True) data['tongbi'] = data.number.diff(12) # 按月同比,12正好是12个月。 data['tongbi_01'] = data['tongbi'] / (data['number'] - data['tongbi']) data.fillna(0, inplace=True) data_filter = data[12:] data_filter = { 'number': list(data_filter['number']), 'date': [str(item) for item in data_filter['date_m'].values.astype("datetime64[D]")], 'tongbi': list(data_filter['tongbi_01']), 'huanbi': list(data_filter['huanbi']), } return data_filter async def two_year_num(ns={}): """ 两年内数量合并 :param ns: :return: """ sor = ns.get('sor') current_year = time.localtime().tm_year if ns.get('orgid') and ns.get('org_type'): orgid = ns.get('orgid') org_type = ns.get('org_type') # 今年12个月客户量 dic = { 'orgid': orgid, 'current_year': current_year, 'org_type': org_type, 'sor': sor } if ns.get('salemanid'): dic = { 'sor': sor, 'salemanid': ns.get('salemanid'), 'current_year': current_year } current_year_customer_number = await calculate_count(dic) # 运营去年12个月客户量 dic['current_year'] = current_year - 1 last_year_customer_num = await calculate_count(dic) last_year_customer_num.extend(current_year_customer_number) return last_year_customer_num async def calculate_count(ns={}): """ 计算不同月份客户量 :param ns: :return: """ sor = ns.get('sor') current_year = ns.get('current_year') if ns.get('orgid') and ns.get('org_type'): orgid = ns.get('orgid') org_type = ns.get('org_type') # 总量,同比,环比 if org_type == '2, 3': count_sql = """SELECT count(*) AS 'user_count', date_format(create_at, '%%m') AS month FROM organization WHERE year(create_at) = ${current_year}$ AND parentid = ${orgid}$ AND org_type IN (2, 3) AND del_flg = '0' GROUP BY month;""" else: count_sql = """SELECT count(*) AS 'user_count', date_format(create_at, '%%m') AS month FROM organization WHERE year(create_at) = ${current_year}$ AND parentid = ${orgid}$ AND org_type = ${org_type}$ AND del_flg = '0' GROUP BY month;""" count_li = await sor.sqlExe(count_sql, {'orgid': orgid, 'current_year': current_year, 'org_type': org_type}) if ns.get('salemanid'): count_sql = """SELECT count(*) AS 'user_count', date_format(create_at, '%%m') AS month FROM customer WHERE year(create_at) = ${current_year}$ AND salemanid = ${salemanid}$ AND del_flg = '0' GROUP BY month;""" count_li = await sor.sqlExe(count_sql, {'current_year': current_year, 'salemanid': ns.get('salemanid')}) # 12个月总量 count_init = [item * 0 for item in range(12)] for user_count in count_li: count_init[int(user_count['month']) - 1] = user_count['user_count'] return count_init async def single_person_consumption(ns={}): """ 个人消费 :param ns: :return: """ ns = { 'userid': 'sa-9_B-VjlnhaUzAzUvZt', 'current_year': '2023-01-01', # 'current_month': '2023-08-16' } userid = await get_user() current_year = ns.get('current_year') current_month = ns.get('current_month') db = DBPools() async with db.sqlorContext('kboss') as sor: orgid = (await sor.R('users', {'id': userid, 'del_flg': '0'}))[0]['orgid'] accountid = (await sor.R('account', {'orgid': orgid, 'del_flg': '0'}))[0]['id'] # 消费总计 account_sum_sql = """select sum(amount) as sum_count from acc_detail where accountid = '%s' and summary = 'BUY' and del_flg = '0';""" % accountid account_count = (await sor.sqlExe(account_sum_sql, {}))[0]['sum_count'] # 本年/本月消费 if current_year: sum_sql = f"""select sum(amount) as sum_count, date_format(acc_date, '%%Y') months from acc_detail where accountid = '{accountid}' and summary = 'BUY' and YEAR(acc_date)=YEAR('{current_year}') and del_flg = '0' group by months;""" else: sum_sql = f"""select sum(amount) as sum_count, date_format(acc_date, '%%m') months from acc_detail where accountid = '{accountid}' and summary = 'BUY' and MONTH(acc_date)=MONTH('{current_month}') and del_flg = '0' group by months;""" account_sum_li = await sor.sqlExe(sum_sql, {}) if account_sum_li: account_sum = account_sum_li[0]['sum_count'] else: account_sum = 0 print(account_sum) async def operate_kpi(ns={}): """ 运营kpi 入参: 本机构id 出参: 每个字段对应数组 客户数(总量,同比,环比) 销售额(总量,同比,环比) 利润分析(总额,同比,环比) 分销商数(总量,同比,环比) 供应商数(总量,同比,环比) :param ns: :return: """ db = DBPools() async with db.sqlorContext('kboss') as sor: try: if ns.get('type') == '运营' and await get_user(): orgid_li = await sor.R('users', {'id': await get_user()}) orgid = orgid_li[0]['orgid'] # 运营客户数 customer_num = await two_year_num({'orgid': orgid, 'org_type': '2, 3', 'sor': sor}) # customer_num = [random.randint(30, 80) for i in range(0, 24)] customer_num = [36, 46, 56, 55, 50, 60, 43, 54, 78, 60, 67, 39, 73, 42, 53, 62, 38, 47, 73, 78, 75, 36, 44, 30] # 运营供应商数 provider_num = await two_year_num({'orgid': orgid, 'org_type': '4', 'sor': sor}) provider_num = [39, 40, 50, 55, 20, 60, 33, 54, 99, 60, 12, 39, 73, 34, 53, 98, 38, 89, 73, 31, 75, 40, 44, 50] # 运营分销商数 reseller_num = '' reseller_num = [39, 4, 30, 50, 25, 60, 80, 54, 100, 60, 4, 39, 60, 34, 53, 20, 38, 89, 10, 31, 75, 20, 44, 90] # 运营销售额 sales = '' sales = [198614, 450739, 188721, 145228, 191228, 264328, 196085, 364975, 386814, 280621, 262761, 492403, 253561, 407107, 122927, 162369, 221448, 344153, 275094, 141592, 101578, 407980, 371456, 216048] # 利润分析 profit = '' profit = [482993, 468981, 283445, 286613, 458930, 179825, 134570, 314190, 50974, 325716, 82643, 220191, 242830, 159058, 209069, 254864, 336909, 266201, 471213, 80043, 267612, 65655, 81858, 148810] # 生成日期 date_m = list(pandas.date_range('1/%s' % (time.localtime().tm_year - 1), periods=24, freq='M')) customer_filter = await tongbi_huanbi({'date_m': date_m, 'number': customer_num}) provider_filter = await tongbi_huanbi({'date_m': date_m, 'number': provider_num}) reseller_filter = await tongbi_huanbi({'date_m': date_m, 'number': reseller_num}) sales_filter = await tongbi_huanbi({'date_m': date_m, 'number': sales}) profit_filter = await tongbi_huanbi({'date_m': date_m, 'number': profit}) username_li = await sor.R('users', {'id': await get_user(), 'del_flg': '0'}) if username_li: username = username_li[0]['username'] else: username = '' return { "status": True, "msg": "kpi get success", 'bind_type': ns.get('type'), 'bind_name': username, "data": { 'customer': customer_filter, 'provider': provider_filter, 'reseller': reseller_filter, 'sales': sales_filter, 'profit': profit_filter } } elif ns.get('type') == '销售' and await get_user(): salemanid = await get_user() # 销售客户数 customer_num = await two_year_num({'salemanid': salemanid, 'sor': sor}) customer_num = [36, 46, 56, 55, 50, 60, 43, 54, 78, 60, 67, 39, 73, 42, 53, 62, 38, 47, 73, 78, 75, 36, 44, 30] # 销售额 sales = [198614, 450739, 188721, 145228, 191228, 264328, 196085, 364975, 386814, 280621, 262761, 492403, 253561, 407107, 122927, 162369, 221448, 344153, 275094, 141592, 101578, 407980, 371456, 216048] # 分销商数 reseller_num = [39, 4, 30, 50, 25, 60, 80, 54, 100, 60, 4, 39, 60, 34, 53, 20, 38, 89, 10, 31, 75, 20, 44, 90] # 生成日期 date_m = list(pandas.date_range('1/%s' % (time.localtime().tm_year - 1), periods=24, freq='M')) customer_filter = await tongbi_huanbi({'date_m': date_m, 'number': customer_num}) sales_filter = await tongbi_huanbi({'date_m': date_m, 'number': sales}) reseller_filter = await tongbi_huanbi({'date_m': date_m, 'number': reseller_num}) saleman_name_li = await sor.R('users', {'id': salemanid}) if saleman_name_li: saleman_name = saleman_name_li[0]['username'] else: saleman_name = '' return { "status": True, "msg": "kpi get success", 'bind_type': ns.get('type'), 'bind_name': saleman_name, "data": { 'customer': customer_filter, 'sales': sales_filter, 'reseller': reseller_filter, } } except Exception as e: raise e return { "status": False, "msg": "kpi search failed" } ret = await operate_kpi(params_kw) return ret