-- 按周统计（UTC-5时间）- 按客户维度（每周日到周六） 
SELECT 
    CustomerId,
    CustomerCode,
    CustomerName,
    week_start,
    COUNT(DISTINCT lr_id) AS total_orders,
    SUM(CASE WHEN ReplaceStatus = 'N' THEN 1 ELSE 0 END) AS cancelled_orders,
    (SELECT COUNT(*) 
     FROM label_scan_history lsh 
     JOIN label_replace_requests lrr2 ON lsh.NeutralWaybillNumber = lrr2.NeutralWaybillNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId) AS scan_count,
    (SELECT COUNT(*) 
     FROM label_scan_history lsh 
     JOIN label_replace_requests lrr2 ON lsh.NeutralWaybillNumber = lrr2.NeutralWaybillNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId 
       AND lsh.Result = 0) AS success_scan_count,
    (SELECT COUNT(*) 
     FROM label_scan_history lsh 
     JOIN label_replace_requests lrr2 ON lsh.NeutralWaybillNumber = lrr2.NeutralWaybillNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(lsh.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId 
       AND lsh.Result != 0) AS failed_scan_count,
    (SELECT COUNT(DISTINCT btw.id) 
     FROM bag_tags bt 
     JOIN bag_tag_waybills btw ON bt.TagNumber = btw.TagNumber 
     JOIN label_replace_requests lrr2 ON btw.FinalMileTrackingNumber = lrr2.FinalMileTrackingNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(btw.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(btw.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId) AS bag_count,
    (SELECT COUNT(*) 
     FROM order_logs ol 
     JOIN label_replace_requests lrr2 ON ol.NeutralWaybillNumber = lrr2.NeutralWaybillNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(ol.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(ol.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId 
       AND ol.OperationType = '集包') AS bag_scan_count,
    (SELECT COUNT(*) 
     FROM order_logs ol 
     JOIN label_replace_requests lrr2 ON ol.NeutralWaybillNumber = lrr2.NeutralWaybillNumber 
     WHERE DATE(DATE_ADD(DATE_SUB(DATE_ADD(ol.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(ol.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) = outer_query.week_start 
       AND lrr2.CustomerId = outer_query.CustomerId 
       AND ol.OperationResult = '失败') AS order_failed_operations
FROM (
    SELECT 
        c.Id AS CustomerId,
        c.CustomerCode,
        c.CustomerName,
        lr.Id AS lr_id,
        lr.ReplaceStatus,
        DATE(DATE_ADD(DATE_SUB(DATE_ADD(lr.createdat, INTERVAL -5 HOUR), INTERVAL (WEEKDAY(DATE_ADD(lr.createdat, INTERVAL -5 HOUR)) + 1) % 7 DAY), INTERVAL 0 HOUR)) AS week_start
    FROM customers c 
    JOIN label_replace_requests lr ON c.Id = lr.CustomerId
) outer_query
GROUP BY CustomerId, CustomerCode, CustomerName, week_start
ORDER BY CustomerName, week_start;
