kboss/b/kpi/user_get_bill.dspy
2025-07-16 14:27:17 +08:00

51 lines
2.6 KiB
Plaintext

async def user_get_bill(ns={}):
start_time = ns['start_time'] if ns.get('start_time') else '2010-10-01'
end_time = ns['end_time'] if ns.get('end_time') else '2099-12-01'
page_size = int(ns['page_size']) if ns.get('page_size') else 20
offset = page_size * (int(ns['current_page']) - 1)
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 根据userid获取用户orgid
# user_orgid_li = await sor.R('users', {'id': ns['userid']})
# user_orgid = user_orgid_li[0]['orgid']
user_orgid = ns['user_orgid']
# 计算总量
bill_count_sql = """select count(*) as total from bill where customerid = '%s' and bill_date >= '%s' and bill_date <= '%s';""" % \
(user_orgid, start_time, end_time)
bill_count_li = await sor.sqlExe(bill_count_sql, {})
if bill_count_li:
total_num = bill_count_li[0]['total']
total_page = total_num / page_size if total_num % page_size == 0 else int(total_num / page_size) + 1
bill_sql = """select * from bill where customerid = '%s' and bill_date >= '%s' and bill_date <= '%s' order by bill_date desc limit %s OFFSET %s;""" % \
(user_orgid, start_time, end_time, page_size, offset)
bill_li = await sor.sqlExe(bill_sql, {})
for bill_detail in bill_li:
product_info = await sor.R('product', {'id': bill_detail['productid']})
bill_detail['productname'] = product_info[0]['name'] if product_info else ''
if bill_detail.get('business_op') == 'RECHARGE':
bill_detail['productname'] = '内部充值'
bill_detail['bill_state'] = 1
if bill_detail.get('business_op') == 'RECHARGE_ALIPAY':
bill_detail['productname'] = '线上充值'
bill_detail['bill_state'] = 1
return {
'status': True,
'msg': '获取账单成功',
'data': {
'total_page': total_page,
'total_num': total_num,
'page_size': page_size,
'current_page': ns['current_page'],
'bill_list': bill_li
}
}
except Exception as e:
return {
'status': False,
'msg': '获取账单报错, %s' % e
}
ret = await user_get_bill(params_kw)
return ret