kboss/b/bz_order/getbz_order.dspy
2025-10-03 17:38:56 +08:00

159 lines
6.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async def getbz_order(ns={}):
"""查询订单商品详情,带分页功能"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
users_id = await get_user()
if not users_id:
server_error(401)
# 分页参数
page = ns.get('page', 1)
size = ns.get('size', 10)
offset = (page - 1) * size
user = await sor.R('users', {'id': users_id, 'del_flg': '0'})
orgid = await sor.R('organization', {'id': user[0]['orgid'], 'del_flg': '0'})
customerid = orgid[0]['id']
params = {'customerid': customerid, 'del_flg': '0'}
# 构建查询SQL主要查询order_goods并关联bz_order的部分字段
sql = """
SELECT
og.*,
bo.customerid,
bo.order_date,
bo.source,
bo.order_status,
bo.business_op,
bo.ordertype,
bo.originalprice,
bo.autoreneworder
FROM order_goods og
JOIN bz_order bo ON og.orderid = bo.id
WHERE og.del_flg = '0'
AND bo.del_flg = '0'
AND bo.customerid = ${customerid}$
"""
# 构建计数SQL
count_sql = """
SELECT COUNT(*) as total_count
FROM order_goods og
JOIN bz_order bo ON og.orderid = bo.id
WHERE og.del_flg = '0'
AND bo.del_flg = '0'
AND bo.customerid = ${customerid}$
"""
# 根据订单号搜索
if ns.get('id'):
sql += " AND bo.id LIKE ${order_id}$"
count_sql += " AND bo.id LIKE ${order_id}$"
params['order_id'] = f"%{ns.get('id')}%"
# 时间范围查询
if ns.get('start_time'):
sql += " AND bo.order_date >= ${start_time}$"
count_sql += " AND bo.order_date >= ${start_time}$"
params['start_time'] = ns.get('start_time') + ' 00:00:00'
if ns.get('end_time'):
sql += " AND bo.order_date <= ${end_time}$"
count_sql += " AND bo.order_date <= ${end_time}$"
params['end_time'] = ns.get('end_time') + ' 23:59:59'
# 订单状态查询
if ns.get('order_status'):
sql += " AND bo.order_status = ${order_status}$"
count_sql += " AND bo.order_status = ${order_status}$"
params['order_status'] = ns.get('order_status')
# 添加排序
sql += " ORDER BY bo.order_date DESC"
# 获取总记录数
total_result = await sor.sqlExe(count_sql, params)
total_count = total_result[0]['total_count'] if total_result else 0
# 添加分页
sql += " LIMIT ${size}$ OFFSET ${offset}$"
params['size'] = size
params['offset'] = offset
# 执行查询
order_goods_list = await sor.sqlExe(sql, params)
if not order_goods_list:
return {
'status': True,
'data': [],
'pagination': {'page': page, 'size': size, 'total': 0}
}
# 处理数据
for item in order_goods_list:
# 清理折扣数据
if item['discount'] is None or item['discount'] == 1.0:
del item['discount']
else:
item['discount'] = item['discount'] * 10 # 转换为百分比显示
# 处理结果中原价list_price和数量quantity 计算总价
if item['list_price'] is None:
item['list_price'] = 0
if item['quantity'] is None:
item['quantity'] = 0
item['origin_amout'] = float(item['list_price']) * float(item['quantity'])
# 获取产品名称
goods = await sor.R('product', {'id': item['productid'], 'del_flg': '0'})
if len(goods) >= 1:
item['product_name'] = goods[0]['name']
item['product_type'] = goods[0]['ptype']
# 获取产品配置详情
item['spec_data'] = None
if item.get('spec_id'):
specdata_list = await sor.R('specificdata', {'id': item['spec_id'], 'del_flg': '0'})
if specdata_list and specdata_list[0]['spec_data']:
item['spec_data'] = json.loads(specdata_list[0]['spec_data'])
# 格式化订单状态
status_mapping = {
'0': '未支付',
'1': '已支付',
'2': '已关闭',
'3': '已取消',
'4': '后付费'
}
item['order_status_text'] = status_mapping.get(item['order_status'], '未知状态')
# 格式化业务操作
business_op_mapping = {
'BUY': '购买',
'RENEW': '续费',
'BUY_REVERSE': '退款',
# 'UPGRADE': '升级'
}
item['business_op_text'] = business_op_mapping.get(item['business_op'], item['business_op'])
return {
'status': True,
'data': order_goods_list,
'pagination': {
'page': page,
'size': size,
'total': total_count
}
}
except Exception as e:
import traceback
traceback.print_exc()
return {'status': False, 'msg': '信息错误: %s' % str(e) + traceback.format_exc()}
ret = await getbz_order(params_kw)
return ret