DROP PROCEDURE IF EXISTS sp_GetDailyMetricsSummary;

DELIMITER $$

CREATE PROCEDURE sp_GetDailyMetricsSummary(IN p_date DATE)
BEGIN
  DECLARE v_date_start DATETIME;
  DECLARE v_date_end DATETIME;
  
  DECLARE v_daily_new_replace_count INT;
  DECLARE v_daily_should_replace_count INT;
  DECLARE v_daily_success_count INT;
  DECLARE v_daily_failure_count INT;
  DECLARE v_cumulative_total_replace_count INT;
  DECLARE v_daily_stop_count INT;
  DECLARE v_daily_label_push_count INT;
  DECLARE v_daily_scan_count INT;
  DECLARE v_before_noon_arrived_count INT;
  DECLARE v_afternoon_arrived_count INT;
  DECLARE v_before_noon_passed_count INT;
  DECLARE v_afternoon_passed_count INT;
  
  SET v_date_start = CONVERT_TZ(CONCAT(DATE(p_date), ' 00:00:00'), '-05:00', '+00:00');
  SET v_date_end = CONVERT_TZ(CONCAT(DATE(p_date), ' 23:59:59'), '-05:00', '+00:00');
  
  SET v_daily_should_replace_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    WHERE r.Label IS NOT NULL
      AND DATE(ahf.ReceiptTime) = p_date
  );
  
  SET v_daily_new_replace_count = v_daily_should_replace_count;
  
  SET v_before_noon_arrived_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    WHERE r.Label IS NOT NULL
      AND DATE(ahf.ReceiptTime) = p_date
      AND HOUR(ahf.ReceiptTime) < 16
  );
  
  SET v_afternoon_arrived_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    WHERE r.Label IS NOT NULL
      AND DATE(ahf.ReceiptTime) = p_date
      AND HOUR(ahf.ReceiptTime) >= 16
  );
  
  SET v_daily_success_count = (
    SELECT COUNT(DISTINCT s.NeutralWaybillNumber)
    FROM label_scan_history s
    WHERE s.Result = 0
      AND s.CreatedAt >= v_date_start
      AND s.CreatedAt <= v_date_end
  );
  
  SET v_before_noon_passed_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    INNER JOIN label_scan_history s ON r.NeutralWaybillNumber = s.NeutralWaybillNumber
    WHERE r.Label IS NOT NULL
      AND DATE(ahf.ReceiptTime) = p_date
      AND HOUR(ahf.ReceiptTime) < 16
      AND s.Result = 0
  );
  
  SET v_afternoon_passed_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    INNER JOIN label_scan_history s ON r.NeutralWaybillNumber = s.NeutralWaybillNumber
    WHERE r.Label IS NOT NULL
      AND DATE(ahf.ReceiptTime) = p_date
      AND HOUR(ahf.ReceiptTime) >= 16
      AND s.Result = 0
  );
  
  SET v_daily_stop_count = (
    SELECT COUNT(DISTINCT s.NeutralWaybillNumber)
    FROM label_scan_history s
    WHERE s.CreatedAt >= v_date_start
      AND s.CreatedAt <= v_date_end
      AND s.Description IS NOT NULL
      AND (LOWER(s.Description) LIKE '%stop%' OR s.Description LIKE '%停%' OR s.Description LIKE '%冻%')
  );
  
  SET v_daily_label_push_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    WHERE r.Label IS NOT NULL
      AND r.CreatedAt >= v_date_start
      AND r.CreatedAt <= v_date_end
  );
  
  SET v_daily_scan_count = (
    SELECT COUNT(DISTINCT s.Id)
    FROM label_scan_history s
    WHERE s.CreatedAt >= v_date_start
      AND s.CreatedAt <= v_date_end
  );
  
  SET v_cumulative_total_replace_count = (
    SELECT COUNT(DISTINCT r.Id)
    FROM label_replace_requests r
    LEFT JOIN label_scan_history s ON r.NeutralWaybillNumber = s.NeutralWaybillNumber AND s.Result = 0
    INNER JOIN arrival_handover_forms ahf 
      ON r.BillOfLadingNumber = ahf.HandoverNumber 
      OR r.MasterPackageNumber = ahf.HandoverNumber
    WHERE r.Label IS NOT NULL
      AND ahf.ReceiptTime IS NOT NULL
      AND DATE(ahf.ReceiptTime) < p_date
      AND s.Id IS NULL
  );
  
  SET v_daily_failure_count = GREATEST(0, v_daily_should_replace_count - v_daily_success_count);
  
  SELECT 
    p_date AS MetricsDate,
    v_daily_new_replace_count AS DailyNewReplaceCount,
    v_daily_should_replace_count AS DailyShouldReplaceCount,
    v_daily_success_count AS DailySuccessCount,
    v_cumulative_total_replace_count AS CumulativeTotalReplaceCount,
    v_daily_stop_count AS DailyStopCount,
    v_daily_label_push_count AS DailyLabelPushCount,
    v_daily_scan_count AS DailyScanCount,
    v_before_noon_arrived_count AS BeforeNoonArrivedCount,
    v_afternoon_arrived_count AS AfternoonArrivedCount,
    v_before_noon_passed_count AS BeforeNoonPassedCount,
    v_afternoon_passed_count AS AfternoonPassedCount,
    v_daily_failure_count AS DailyFailureCount,
    NOW() AS DataFetchTime;

END$$

DELIMITER ;
