120 lines
5.1 KiB
Plaintext
120 lines
5.1 KiB
Plaintext
async def get_recharge_record(ns={}):
|
|
# 列出充值记录和充值总额
|
|
recharge_records = []
|
|
recharge_amount = 0
|
|
recharge_reverse_amount = 0
|
|
|
|
action_mapping = {
|
|
'RECHARGE': '线下充值',
|
|
'RECHARGE_REVERSE': '线下充值冲账',
|
|
'RECHARGE_ALIPAY': '支付宝充值',
|
|
'RECHARGE_ALIPAY_REVERSE': '支付宝充值冲账'
|
|
}
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
user_id = await get_user()
|
|
if not user_id:
|
|
server_error(401)
|
|
|
|
result = await sor.R('users', {'id': user_id})
|
|
if not result:
|
|
return {'status': False, 'message': 'No orgid found for the user'}
|
|
|
|
orgid = result[0].get('orgid')
|
|
start_time = ns.get('start_time')
|
|
end_time = ns.get('end_time')
|
|
# recharge_path = 0
|
|
recharge_path = ns.get('recharge_path')
|
|
params = {'start_time': start_time, 'end_time': end_time, 'recharge_path': recharge_path, 'orgid': orgid}
|
|
|
|
# 增加分页功能
|
|
page = ns.get('page', 1)
|
|
size = ns.get('size', 20)
|
|
offset = (page - 1) * size
|
|
|
|
# 构建COUNT查询
|
|
total_sql = "SELECT COUNT(*) AS total_count FROM recharge_log WHERE del_flg = 0 AND customerid = ${orgid}$"
|
|
|
|
sql = "SELECT * FROM recharge_log WHERE del_flg = 0 AND customerid = ${orgid}$"
|
|
if start_time and end_time:
|
|
sql += " AND create_at BETWEEN ${start_time}$ AND ${end_time}$"
|
|
total_sql += " AND create_at BETWEEN ${start_time}$ AND ${end_time}$"
|
|
if recharge_path is not None:
|
|
sql += " AND recharge_path = ${recharge_path}$"
|
|
total_sql += " AND recharge_path = ${recharge_path}$"
|
|
|
|
# 添加排序但不添加分页限制到主查询
|
|
sql += " ORDER BY create_at DESC"
|
|
|
|
# 执行COUNT查询获取总记录数
|
|
total_result = await sor.sqlExe(total_sql, params)
|
|
total_count = total_result[0]['total_count'] if total_result else 0
|
|
|
|
# 执行查询获取所有匹配记录
|
|
reacs = await sor.sqlExe(sql, params)
|
|
|
|
if not reacs:
|
|
return {'status': True, 'message': 'No recharge records', 'pagination': {'page': page, 'size': size, 'total': 0}}
|
|
|
|
for recharge in reacs:
|
|
if recharge['recharge_sno']:
|
|
is_payment_received = 1
|
|
else:
|
|
is_payment_received = 2
|
|
|
|
if recharge['recharge_path'] == '0': # 将字符串形式的0和2进行匹配
|
|
recharge_path = '支付宝充值'
|
|
elif recharge['recharge_path'] == '2':
|
|
recharge_path = '线下充值'
|
|
elif recharge['recharge_path'] == '1' and recharge['weixin_sno']:
|
|
recharge_path = '微信充值'
|
|
else:
|
|
recharge_path = '未知'
|
|
|
|
if is_payment_received == 1 or recharge_path == '线下充值' or recharge_path == '微信充值':
|
|
record_data = {
|
|
'is_payment_received': bool(recharge['recharge_sno']),
|
|
'recharge_path': recharge_path,
|
|
'create_at': recharge['create_at'],
|
|
'recharge_amt': round(float(recharge['recharge_amt']), 2),
|
|
'recharge_sno': recharge['recharge_sno'],
|
|
'action': action_mapping.get(recharge['action'], '未知'),
|
|
'id': recharge['customerid'],
|
|
}
|
|
|
|
if recharge_path == '微信充值':
|
|
record_data['action'] = '微信充值'
|
|
record_data['recharge_sno'] = recharge.get('weixin_sno')
|
|
recharge_records.append(record_data)
|
|
|
|
if recharge['action'] in ['RECHARGE', 'RECHARGE_ALIPAY']:
|
|
recharge_amount += round(float(recharge['recharge_amt']), 2)
|
|
elif recharge['action'] in ['RECHARGE_REVERSE', 'RECHARGE_ALIPAY_REVERSE']:
|
|
recharge_reverse_amount += round(float(recharge['recharge_amt']), 2)
|
|
|
|
# 在过滤后应用分页
|
|
recharge_records.sort(key=lambda x: x['create_at'], reverse=True)
|
|
# 应用分页
|
|
paginated_records = recharge_records[offset:offset + size]
|
|
except Exception as e:
|
|
raise
|
|
|
|
total_amount = recharge_amount - recharge_reverse_amount
|
|
return {
|
|
'status': True,
|
|
'message': 'ok',
|
|
'recharge_records': paginated_records,
|
|
'actual_amount': round(total_amount, 2),
|
|
'recharge_amount': round(recharge_amount, 2),
|
|
'charged_amount': round(recharge_reverse_amount, 2),
|
|
'pagination': {
|
|
'page': page,
|
|
'size': size,
|
|
'total': len(recharge_records) # 使用过滤后的总记录数
|
|
}
|
|
}
|
|
|
|
ret = await get_recharge_record(params_kw)
|
|
return ret |