订单更新

This commit is contained in:
ping 2025-10-03 17:38:56 +08:00
parent 8e3a34706c
commit f927fb4619
3 changed files with 273 additions and 98 deletions

View File

@ -582,7 +582,23 @@ async def get_baidu_orderlist(ns={}):
nss['unit'] = i.get('timeUnit') nss['unit'] = i.get('timeUnit')
nss['resourceids'] = ','.join(i['shortIds']) if i.get('shortIds') else '' nss['resourceids'] = ','.join(i['shortIds']) if i.get('shortIds') else ''
nss['orderkey'] = i.get('key') nss['orderkey'] = i.get('key')
try:
# 保存配置configurations 存入specdata表中
if i.get('configurations'):
specdata = json.dumps(i['configurations'], ensure_ascii=False)
ns_specificdata = {
'id': uuid(),
'productid': product[0]['id'],
'spec_data': specdata,
}
nss['spec_id'] = ns_specificdata['id']
await sor.C('specificdata', ns_specificdata)
except Exception as e:
print('保存配置configurations失败', str(e))
with open('baidu_error.log', 'a') as f:
f.write('保存配置configurations失败' + str(e) + '\n')
# 如果是续费订单 由于没有返回日期, 重新计算日期 # 如果是续费订单 由于没有返回日期, 重新计算日期
if order_type == 'RENEW': if order_type == 'RENEW':
history_time_sql = "select resourcestarttime, resourceendtime from order_goods where FIND_IN_SET('%s', resourceids) order by resourceendtime desc;" % \ history_time_sql = "select resourcestarttime, resourceendtime from order_goods where FIND_IN_SET('%s', resourceids) order by resourceendtime desc;" % \

View File

@ -1,110 +1,159 @@
async def getbz_order(ns={}): async def getbz_order(ns={}):
"""查询订单详情/我的所有订单""" """查询订单商品详情,带分页功能"""
db = DBPools() db = DBPools()
async with db.sqlorContext('kboss') as sor: async with db.sqlorContext('kboss') as sor:
try: try:
users_id = await get_user() users_id = await get_user()
ns['del_flg'] = '0' if not users_id:
ns['sort'] = 'create_at desc' server_error(401)
if ns.get('id') and not ns.get('type'):
# 查询订单详情
reacs = await sor.R('bz_order', ns)
# 兼容第三范式产品展示 这段代码可注释 # 分页参数
if reacs and reacs[0].get('specdataid'): page = ns.get('page', 1)
specdata = await sor.R('specificdata', {'id': reacs[0].get('specdataid'), 'del_flg': '0'}) size = ns.get('size', 10)
if specdata: offset = (page - 1) * size
specdata_dumps = json.dumps(specdata[0]) if isinstance(specdata[0], dict) else specdata[0]
if 'internalip' in specdata_dumps and 'externalip' in specdata_dumps: user = await sor.R('users', {'id': users_id, 'del_flg': '0'})
reacs[0]['spec_data'] = json.loads(json.loads(specdata_dumps)['spec_data']) 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')
reacsgoods = await sor.R('order_goods', {'orderid': ns['id'], 'del_flg': '0'}) # 添加排序
for i in reacsgoods: sql += " ORDER BY bo.order_date DESC"
if i['discount'] == None or i['discount'] == 1.0:
del i['discount'] # 获取总记录数
i['discount'] = i['discount'] * 10 if i.get('discount') else None total_result = await sor.sqlExe(count_sql, params)
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'}) total_count = total_result[0]['total_count'] if total_result else 0
if len(goods) >= 1:
i['productid'] = goods[0]['name'] # 添加分页
i['ptype'] = goods[0]['ptype'] sql += " LIMIT ${size}$ OFFSET ${offset}$"
reacs[0]['order_goods'] = reacsgoods params['size'] = size
return {'status': True, 'data': reacs} params['offset'] = offset
elif ns.get('type') == '200':
#筛选支付类型 # 执行查询
user = await sor.R('users', {'id': users_id, 'del_flg': '0'}) order_goods_list = await sor.sqlExe(sql, params)
orgid = await sor.R('organization', {'id': user[0]['orgid'], 'del_flg': '0'})
ns['customerid'] = orgid[0]['id'] if not order_goods_list:
if ns.get('id'): return {
#根据订单号搜索 'status': True,
reacs = await sor.R('bz_order', {'id': ns['id']}) 'data': [],
elif ns.get('start_time'): 'pagination': {'page': page, 'size': size, 'total': 0}
sql = """select * from bz_order where del_flg = 0 AND customerid = ${customerid}$ AND order_date >= ${start_time}$ AND order_date <= ${end_time}$ ORDER BY order_date DESC """ }
start_time = ns.get('start_time') + ' 00:00:00'
end_time = ns.get('end_time') + ' 23:59:59'
reacs = await sor.sqlExe(sql, {'start_time': start_time,'end_time': end_time,'customerid' :ns['customerid']})
# 处理数据
for item in order_goods_list:
# 清理折扣数据
if item['discount'] is None or item['discount'] == 1.0:
del item['discount']
else: else:
reacs = await sor.R('bz_order', {'customerid':ns['customerid'],'sort':'order_date desc','order_status':ns.get('order_status'),'del_flg':'0'}) item['discount'] = item['discount'] * 10 # 转换为百分比显示
all_price = 0
if len(reacs) >= 1: # 处理结果中原价list_price和数量quantity 计算总价
for j in reacs: if item['list_price'] is None:
reacsgoods = await sor.R('order_goods', {'orderid': j['id'], 'del_flg': '0'}) item['list_price'] = 0
countlist_price = 0 if item['quantity'] is None:
for i in reacsgoods: item['quantity'] = 0
if i['discount'] == None or i['discount'] == 1.0: item['origin_amout'] = float(item['list_price']) * float(item['quantity'])
del i['discount']
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'})
if len(goods) >= 1: # 获取产品名称
i['productid'] = goods[0]['name'] goods = await sor.R('product', {'id': item['productid'], 'del_flg': '0'})
j['order_goods'] = reacsgoods if len(goods) >= 1:
countlist_price += i['list_price'] item['product_name'] = goods[0]['name']
countlist_price *= i['quantity'] item['product_type'] = goods[0]['ptype']
if j['order_status'] == '1':
all_price += countlist_price # 获取产品配置详情
j['countprice'] = round(countlist_price,2) item['spec_data'] = None
if j['countprice'] == 0: if item.get('spec_id'):
j['countprice'] = j['originalprice'] specdata_list = await sor.R('specificdata', {'id': item['spec_id'], 'del_flg': '0'})
return {'status': True, 'data': reacs, 'all_price': round(all_price, 2)} if specdata_list and specdata_list[0]['spec_data']:
else: item['spec_data'] = json.loads(specdata_list[0]['spec_data'])
# 我的所有订单
ns['del_flg'] = '0' # 格式化订单状态
ns['sort'] = 'create_at desc' status_mapping = {
user = await sor.R('users', {'id': users_id, 'del_flg': '0'}) '0': '未支付',
orgid = await sor.R('organization', {'id': user[0]['orgid'], 'del_flg': '0'}) '1': '已支付',
ns['customerid'] = orgid[0]['id'] '2': '已关闭',
reacs = await sor.R('bz_order', {'customerid':ns['customerid'],'sort':'create_at desc','del_flg':'0'}) '3': '已取消',
actual_all_price = 0 '4': '后付费'
for j in reacs: }
reacsgoods = await sor.R('order_goods', {'orderid': j['id'], 'del_flg': '0'}) item['order_status_text'] = status_mapping.get(item['order_status'], '未知状态')
all_price = 0
countlist_price = 0 # 格式化业务操作
business_op = j['business_op'] business_op_mapping = {
for i in reacsgoods: 'BUY': '购买',
if i['discount'] == None or i['discount'] == 1.0: 'RENEW': '续费',
del i['discount'] 'BUY_REVERSE': '退款',
i['discount'] = i['discount'] * 10 if i.get('discount') else None # 'UPGRADE': '升级'
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'}) }
if len(goods) < 1: item['business_op_text'] = business_op_mapping.get(item['business_op'], item['business_op'])
continue
i['ptype'] = goods[0]['ptype'] return {
if len(goods) >= 1: 'status': True,
i['productid'] = goods[0]['name'] 'data': order_goods_list,
j['order_goods'] = reacsgoods 'pagination': {
countlist_price = i['list_price'] * i['quantity'] 'page': page,
actual_price = i['price'] * i['quantity'] 'size': size,
# countlist_price += i['list_price'] 'total': total_count
# countlist_price *= i['quantity'] }
if j['order_status'] == '1': }
all_price += countlist_price
if business_op != 'BUY_REVERSE':
actual_all_price += actual_price
j['countprice'] = round(all_price,2)
if j['countprice'] == 0:
j['countprice'] = j['originalprice']
return {'status': True, 'data': reacs,'all_price': round(actual_all_price,2)}
except Exception as e: except Exception as e:
raise e import traceback
return {'status': False, 'msg': '信息错误'} traceback.print_exc()
return {'status': False, 'msg': '信息错误: %s' % str(e) + traceback.format_exc()}
ret = await getbz_order(params_kw) ret = await getbz_order(params_kw)
return ret return ret

View File

@ -0,0 +1,110 @@
async def getbz_order(ns={}):
"""查询订单详情/我的所有订单"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
users_id = await get_user()
ns['del_flg'] = '0'
ns['sort'] = 'create_at desc'
if ns.get('id') and not ns.get('type'):
# 查询订单详情
reacs = await sor.R('bz_order', ns)
# 兼容第三范式产品展示 这段代码可注释
if reacs and reacs[0].get('specdataid'):
specdata = await sor.R('specificdata', {'id': reacs[0].get('specdataid'), 'del_flg': '0'})
if specdata:
specdata_dumps = json.dumps(specdata[0]) if isinstance(specdata[0], dict) else specdata[0]
if 'internalip' in specdata_dumps and 'externalip' in specdata_dumps:
reacs[0]['spec_data'] = json.loads(json.loads(specdata_dumps)['spec_data'])
reacsgoods = await sor.R('order_goods', {'orderid': ns['id'], 'del_flg': '0'})
for i in reacsgoods:
if i['discount'] == None or i['discount'] == 1.0:
del i['discount']
i['discount'] = i['discount'] * 10 if i.get('discount') else None
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'})
if len(goods) >= 1:
i['productid'] = goods[0]['name']
i['ptype'] = goods[0]['ptype']
reacs[0]['order_goods'] = reacsgoods
return {'status': True, 'data': reacs}
elif ns.get('type') == '200':
#筛选支付类型
user = await sor.R('users', {'id': users_id, 'del_flg': '0'})
orgid = await sor.R('organization', {'id': user[0]['orgid'], 'del_flg': '0'})
ns['customerid'] = orgid[0]['id']
if ns.get('id'):
#根据订单号搜索
reacs = await sor.R('bz_order', {'id': ns['id']})
elif ns.get('start_time'):
sql = """select * from bz_order where del_flg = 0 AND customerid = ${customerid}$ AND order_date >= ${start_time}$ AND order_date <= ${end_time}$ ORDER BY order_date DESC """
start_time = ns.get('start_time') + ' 00:00:00'
end_time = ns.get('end_time') + ' 23:59:59'
reacs = await sor.sqlExe(sql, {'start_time': start_time,'end_time': end_time,'customerid' :ns['customerid']})
else:
reacs = await sor.R('bz_order', {'customerid':ns['customerid'],'sort':'order_date desc','order_status':ns.get('order_status'),'del_flg':'0'})
all_price = 0
if len(reacs) >= 1:
for j in reacs:
reacsgoods = await sor.R('order_goods', {'orderid': j['id'], 'del_flg': '0'})
countlist_price = 0
for i in reacsgoods:
if i['discount'] == None or i['discount'] == 1.0:
del i['discount']
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'})
if len(goods) >= 1:
i['productid'] = goods[0]['name']
j['order_goods'] = reacsgoods
countlist_price += i['list_price']
countlist_price *= i['quantity']
if j['order_status'] == '1':
all_price += countlist_price
j['countprice'] = round(countlist_price,2)
if j['countprice'] == 0:
j['countprice'] = j['originalprice']
return {'status': True, 'data': reacs, 'all_price': round(all_price, 2)}
else:
# 我的所有订单
ns['del_flg'] = '0'
ns['sort'] = 'create_at desc'
user = await sor.R('users', {'id': users_id, 'del_flg': '0'})
orgid = await sor.R('organization', {'id': user[0]['orgid'], 'del_flg': '0'})
ns['customerid'] = orgid[0]['id']
reacs = await sor.R('bz_order', {'customerid':ns['customerid'],'sort':'create_at desc','del_flg':'0'})
actual_all_price = 0
for j in reacs:
reacsgoods = await sor.R('order_goods', {'orderid': j['id'], 'del_flg': '0'})
all_price = 0
countlist_price = 0
business_op = j['business_op']
for i in reacsgoods:
if i['discount'] == None or i['discount'] == 1.0:
del i['discount']
i['discount'] = i['discount'] * 10 if i.get('discount') else None
goods = await sor.R('product', {'id': i['productid'], 'del_flg': '0'})
if len(goods) < 1:
continue
i['ptype'] = goods[0]['ptype']
if len(goods) >= 1:
i['productid'] = goods[0]['name']
j['order_goods'] = reacsgoods
countlist_price = i['list_price'] * i['quantity']
actual_price = i['price'] * i['quantity']
# countlist_price += i['list_price']
# countlist_price *= i['quantity']
if j['order_status'] == '1':
all_price += countlist_price
if business_op != 'BUY_REVERSE':
actual_all_price += actual_price
j['countprice'] = round(all_price,2)
if j['countprice'] == 0:
j['countprice'] = j['originalprice']
return {'status': True, 'data': reacs,'all_price': round(actual_all_price,2)}
except Exception as e:
raise e
return {'status': False, 'msg': '信息错误'}
ret = await getbz_order(params_kw)
return ret