73 lines
2.6 KiB
Plaintext
73 lines
2.6 KiB
Plaintext
async def customer_count(ns={}):
|
||
"""
|
||
计算不同月份客户量
|
||
:param ns:
|
||
:return:
|
||
"""
|
||
return {"a": 1}
|
||
|
||
|
||
async def operate_kpi(ns={}):
|
||
"""
|
||
运营kpi
|
||
入参: 本机构id
|
||
出参: 每个字段对应数组
|
||
客户数(总量,同比,环比)
|
||
|
||
销售额(总量,同比,环比)
|
||
|
||
利润分析(总额,同比,环比)
|
||
|
||
分销商数(总量,同比,环比)
|
||
|
||
供应商数(总量,同比,环比)
|
||
|
||
:param ns:
|
||
:return:
|
||
"""
|
||
orgid = ns.get('orgid')
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
try:
|
||
# 今年12个月客户量
|
||
current_year = time.localtime().tm_year
|
||
current_year_customer_number = await customer_count({'orgid': orgid, 'current_year': current_year, 'sor': sor})
|
||
# 去年12个月客户量
|
||
last_year_customer_num = await customer_count({'orgid': orgid, 'current_year': current_year - 1, 'sor': sor})
|
||
# 生成日期
|
||
date_m = list(pandas.date_range('1/2022', periods=24, freq='M'))
|
||
last_year_customer_num.extend(current_year_customer_number)
|
||
# customer_num = last_year_customer_num
|
||
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]
|
||
# 构建一个dataframe
|
||
data = pandas.DataFrame({'date_m': date_m, 'customer_num': customer_num})
|
||
data['huanbi'] = data.customer_num.pct_change()
|
||
# 填充
|
||
data.fillna(0, inplace=True)
|
||
data['tongbi'] = data.customer_num.diff(12) # 按月同比,12正好是12个月。
|
||
data['tongbi_01'] = data['tongbi'] / (data['customer_num'] - data['tongbi'])
|
||
data.fillna(0, inplace=True)
|
||
data_filter = data[12:]
|
||
customer_filter = {
|
||
'number': list(data_filter['customer_num']),
|
||
'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 {
|
||
"status": True,
|
||
"msg": "kpi get success",
|
||
"data": {
|
||
'customer': customer_filter
|
||
}
|
||
}
|
||
except Exception as e:
|
||
raise e
|
||
return {
|
||
"status": False,
|
||
"msg": "kpi search failed"
|
||
}
|
||
|
||
ret = await operate_kpi(params_kw)
|
||
return ret |