{ "properties" : { }, "id" : "2b3c4d5e6f748590a1b2c3d4e5f6789", "script" : null, "groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1", "name" : "实时交易统计", "createTime" : 1780876800000, "updateTime" : 1780803706100, "lock" : null, "createBy" : "admin", "updateBy" : "admin", "path" : "/real-time-stats", "method" : "GET", "parameters" : [ { "name" : "dimension", "value" : null, "description" : "时间维度:day(当天)/week(近一周)/month(近一月)/year(当年),不传则返回全部", "required" : false, "dataType" : "String", "type" : null, "defaultValue" : null, "validateType" : null, "error" : null, "expression" : null, "children" : null } ], "options" : [ ], "requestBody" : "", "headers" : [ ], "paths" : [ ], "responseBody" : null, "description" : "实时交易统计:牦牛交易总量、订单交易总量、销售商户数量、采购商户数量,按当天/近一周/近一月/当年统计(只读查询)", "requestBodyDefinition" : null, "responseBodyDefinition" : null } ================================ // 只读统计已完成订单,不修改任何数据 var sql = """ SELECT dim, COALESCE(SUM(quantity), 0) AS yak_total_volume, COUNT(*) AS order_total_volume, COUNT(DISTINCT seller_id) AS seller_count, COUNT(DISTINCT buyer_id) AS buyer_count FROM ( SELECT 'day' AS dim, quantity, seller_id, buyer_id FROM yak_sn_order WHERE del_flag = '0' AND status = 'COMPLETED' AND transaction_time >= CURRENT_DATE AND transaction_time < CURRENT_DATE + INTERVAL '1 day' UNION ALL SELECT 'week' AS dim, quantity, seller_id, buyer_id FROM yak_sn_order WHERE del_flag = '0' AND status = 'COMPLETED' AND transaction_time >= CURRENT_DATE - INTERVAL '6 days' AND transaction_time < CURRENT_DATE + INTERVAL '1 day' UNION ALL SELECT 'month' AS dim, quantity, seller_id, buyer_id FROM yak_sn_order WHERE del_flag = '0' AND status = 'COMPLETED' AND transaction_time >= CURRENT_DATE - INTERVAL '29 days' AND transaction_time < CURRENT_DATE + INTERVAL '1 day' UNION ALL SELECT 'year' AS dim, quantity, seller_id, buyer_id FROM yak_sn_order WHERE del_flag = '0' AND status = 'COMPLETED' AND transaction_time >= DATE_TRUNC('year', CURRENT_DATE) AND transaction_time < CURRENT_DATE + INTERVAL '1 day' ) t GROUP BY dim ORDER BY dim """ var rows = db.select(sql) var createEmptyStats = () => { return { yakTotalVolume: 0, orderTotalVolume: 0, sellerCount: 0, buyerCount: 0 } } var result = { day: createEmptyStats(), week: createEmptyStats(), month: createEmptyStats(), year: createEmptyStats() } for (row in rows) { result[row.dim] = { yakTotalVolume: row.yakTotalVolume, orderTotalVolume: row.orderTotalVolume, sellerCount: row.sellerCount, buyerCount: row.buyerCount } } var dim = dimension if (dim && result[dim]) { return result[dim] } return result