async def cumulative_order(ns={}): """查询订单商品详情,带分页功能""" db = DBPools() async with db.sqlorContext('kboss') as sor: try: users_id = await get_user() if not users_id: server_error(401) 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'] # 统计全部 累计支付金额和累计优惠金额 不包含各种筛选条件 # 累计支付金额=BUY+RENEW-BUY_REVERSE并且对应order_status=1是实际支付金额 累计优惠金额=BUY+RENEW-BUY_REVERSE并且对应order_status=1是优惠金额 total_amount_sql = """ SELECT COALESCE(SUM( CASE WHEN bo.order_status = 1 AND bo.business_op IN ('BUY', 'RENEW') THEN og.amount WHEN bo.order_status = 1 AND bo.business_op = 'BUY_REVERSE' THEN -og.amount ELSE 0 END ), 0) AS total_paid_amount, COALESCE(SUM( CASE WHEN bo.order_status = 1 AND bo.business_op IN ('BUY', 'RENEW') THEN (og.list_price * og.quantity - og.amount) WHEN bo.order_status = 1 AND bo.business_op = 'BUY_REVERSE' THEN -(og.list_price * og.quantity - og.amount) ELSE 0 END ), 0) AS total_discount_amount 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}$ """ total_amount_result = await sor.sqlExe(total_amount_sql, {'customerid': customerid}) total_paid_amount = float(total_amount_result[0]['total_paid_amount']) if total_amount_result else 0.0 total_discount_amount = float(total_amount_result[0]['total_discount_amount']) if total_amount_result else 0.0 # 将累计支付金额和累计优惠金额添加到返回结果中 ns['total_paid_amount'] = total_paid_amount ns['total_discount_amount'] = total_discount_amount return { 'status': True, 'data': ns } except Exception as e: import traceback traceback.print_exc() return {'status': False, 'msg': '信息错误: %s' % str(e) + traceback.format_exc()} ret = await cumulative_order(params_kw) return ret