package com.talentcard.server.mapper; /** MyBatis SQL provider:政府补贴表达式在运行期拼接,兼容 MySQL 5.7。 */ public class StatisticsQuerySql { public String selectSummary() { return sql(""" select coalesce(sum(/*AMOUNT*/), 0) as subsidyAmountCents, (select count(*) from benefit_order verified_order where verified_order.deleted = 0 and verified_order.order_status = 1 and verified_order.verify_status = 1 and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = verified_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = verified_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), verified_order.updated_at ) >= date_format(current_date, '%Y-%m-01') and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = verified_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = verified_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), verified_order.updated_at ) < date_add(last_day(current_date), interval 1 day)) as monthlyVerificationCount, (select count(*) from talent_user u where u.deleted = 0) as talentCount, (select count(*) from benefit_order po where po.deleted = 0 and po.order_status = 0) as pendingOrderCount, (select count(*) from merchant_product pp where pp.deleted = 0 and pp.audit_status = 1) as pendingProductCount, (select count(*) from service_application sa where sa.deleted = 0 and sa.status in ('待处理', '待审核')) as pendingApplicationCount from benefit_order o left join merchant_product p on p.id = o.product_id and p.deleted = 0 left join merchant m on m.id = o.merchant_id and m.deleted = 0 left join merchant_coupon c on c.id = o.coupon_id and c.deleted = 0 where o.deleted = 0 and o.order_status = 1 """); } public String selectCompareSummary() { return sql(""" select coalesce((select count(*) from talent_user u where u.deleted = 0 and u.created_at >= current_date and u.created_at < date_add(current_date, interval 1 day)), 0) as talentCompare, (coalesce((select count(*) from benefit_order current_month_order where current_month_order.deleted = 0 and current_month_order.order_status = 1 and current_month_order.verify_status = 1 and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = current_month_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = current_month_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), current_month_order.updated_at ) >= date_format(current_date, '%Y-%m-01') and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = current_month_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = current_month_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), current_month_order.updated_at ) < date_add(last_day(current_date), interval 1 day)), 0) - coalesce((select count(*) from benefit_order previous_month_order where previous_month_order.deleted = 0 and previous_month_order.order_status = 1 and previous_month_order.verify_status = 1 and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = previous_month_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = previous_month_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), previous_month_order.updated_at ) >= date_sub(date_format(current_date, '%Y-%m-01'), interval 1 month) and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = previous_month_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = previous_month_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), previous_month_order.updated_at ) < date_format(current_date, '%Y-%m-01')), 0)) as monthlyVerificationCompare, coalesce(sum(/*AMOUNT*/), 0) as subsidyCompareCents from benefit_order o left join merchant_product p on p.id = o.product_id and p.deleted = 0 left join merchant m on m.id = o.merchant_id and m.deleted = 0 left join merchant_coupon c on c.id = o.coupon_id and c.deleted = 0 where o.deleted = 0 and o.order_status = 1 and /*ORDER_TIME*/ >= current_date and /*ORDER_TIME*/ < date_add(current_date, interval 1 day) """); } public String selectSummaryTrend() { return sql(""" select date_format(days.bucket_day, '%c/%e') as time, (select count(*) from talent_user u where u.deleted = 0 and u.created_at < date_add(days.bucket_day, interval 1 day)) as talent, (select count(*) from benefit_order verified_order where verified_order.deleted = 0 and verified_order.order_status = 1 and verified_order.verify_status = 1 and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = verified_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = verified_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), verified_order.updated_at ) >= days.bucket_day and coalesce( (select max(v.verify_time) from verify_record v where v.order_id = verified_order.id and v.status = 1), (select max(pt.paid_at) from payment_transaction pt where pt.order_no = verified_order.order_no and pt.status = 'SUCCEEDED' and pt.paid_at is not null), verified_order.updated_at ) < date_add(days.bucket_day, interval 1 day)) as verification, round(coalesce((select sum(/*AMOUNT*/) from benefit_order o left join merchant_product p on p.id = o.product_id and p.deleted = 0 left join merchant m on m.id = o.merchant_id and m.deleted = 0 left join merchant_coupon c on c.id = o.coupon_id and c.deleted = 0 where o.deleted = 0 and o.order_status = 1 and /*ORDER_TIME*/ < date_add(days.bucket_day, interval 1 day) ), 0) / 100, 2) as subsidy from ( select date_sub(current_date, interval 6 day) as bucket_day union all select date_sub(current_date, interval 5 day) union all select date_sub(current_date, interval 4 day) union all select date_sub(current_date, interval 3 day) union all select date_sub(current_date, interval 2 day) union all select date_sub(current_date, interval 1 day) union all select current_date ) days order by days.bucket_day """); } public String selectPeriodSummaries() { return sql(""" """); } public String selectMerchantOverviews() { return sql(""" """); } public String selectSettlementExportDetails() { return sql(""" """); } public String selectDashboardCards() { return sql(""" select coalesce(( -- 只算已核销的实付金额:未核销的钱还扣在通道的延迟户里,核销后才分账给商户; -- 退款中的订单在通道结果收敛前仍是 order_status = 1,只有「已核销」能立刻挡住它。 select sum(o.pay_amount) from benefit_order o where o.deleted = 0 and o.order_status = 1 and o.verify_status = 1 and o.merchant_id = #{merchantId} ), 0) as transactionAmountCents, coalesce(( select count(*) from verify_record r where r.status = 1 and r.merchant_id = #{merchantId} and r.verify_time >= #{todayStart} and r.verify_time < #{todayEnd} ), 0) as todayVerificationCount, coalesce(( select count(*) from verify_record r where r.status = 1 and r.merchant_id = #{merchantId} and r.verify_time >= #{weekStart} and r.verify_time < #{weekEnd} ), 0) as weekVerificationCount, coalesce(( select sum(/*AMOUNT*/) from benefit_order o left join merchant_product p on p.id = o.product_id and p.deleted = 0 left join merchant m on m.id = o.merchant_id and m.deleted = 0 left join merchant_coupon c on c.id = o.coupon_id and c.deleted = 0 where o.deleted = 0 and o.order_status = 1 and o.merchant_id = #{merchantId} ), 0) + coalesce(( select sum(r.subsidy_amount) from verify_record r where r.status = 1 and r.order_id is null and upper(r.verify_type) = 'HOTEL_FREE' and r.merchant_id = #{merchantId} ), 0) as subsidyAmountCents """); } private static String sql(String template) { String amount = template.contains("