-- 1. 建辅助数字表
DROP TABLE IF EXISTS tmp_num_100;
CREATE TABLE tmp_num_100 (n TINYINT UNSIGNED PRIMARY KEY);
INSERT INTO tmp_num_100(n)
SELECT a.n + b.n AS n FROM (
  SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
  UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) a
CROSS JOIN (
  SELECT 0 n UNION ALL SELECT 10 UNION ALL SELECT 20 UNION ALL SELECT 30 UNION ALL SELECT 40
  UNION ALL SELECT 50 UNION ALL SELECT 60 UNION ALL SELECT 70 UNION ALL SELECT 80 UNION ALL SELECT 90
) b
ORDER BY a.n + b.n;

DROP TABLE IF EXISTS tmp_num_500;
CREATE TABLE tmp_num_500 (n SMALLINT UNSIGNED PRIMARY KEY);
INSERT INTO tmp_num_500(n)
SELECT t.n FROM (
  SELECT @r:=@r+1 n
  FROM (SELECT 0 FROM tmp_num_100) a,
       (SELECT 0 FROM tmp_num_100) b,
       (SELECT @r:=-1) init
  LIMIT 500
) t;

-- 2. 目标表（若未建）
-- CREATE TABLE cdr_event_smsc (
--   id BIGINT AUTO_INCREMENT PRIMARY KEY,
--   ne_type VARCHAR(16),
--   ne_name VARCHAR(64),
--   rm_uid VARCHAR(64),
--   `timestamp` BIGINT,
--   cdr_json JSON,
--   created_at DATETIME,
--   tenant_id VARCHAR(64)
-- ) ENGINE=InnoDB;

-- 3. 关闭一些约束提高插入速度（可选）
SET autocommit = 0;
SET unique_checks = 0;
SET foreign_key_checks = 0;

-- 4. 插入 500 万（100 * 100 * 500）
-- 随机时间：过去一年内的任意秒（不均匀）
SET @start_ts = UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 365 DAY));
SET @range_sec = 365 * 24 * 3600;

INSERT INTO cdr_event_smsc
  (ne_type, ne_name, rm_uid, `timestamp`, cdr_json, created_at)
SELECT
  'SMSC' AS ne_type,
  'SMSC_001' AS ne_name,
  '4400HXSMSC001' AS rm_uid,
  @start_ts + FLOOR(RAND()*@range_sec) AS `timestamp`,
  '{\"calledParty\":\"15731123455\",\"callerParty\":\"18165338055\",\"cause\":28,\"recordType\":\"MTSM\",\"result\":0,\"serviceType\":\"Normal\",\"updateTime\":\"2025-08-18T11:09:52.703Z\"}' AS cdr_json,
  NOW() AS created_at
FROM tmp_num_100 a
CROSS JOIN tmp_num_100 b
CROSS JOIN tmp_num_500 c;

COMMIT;

-- 5. 恢复
SET unique_checks = 1;
SET foreign_key_checks = 1;
SET autocommit = 1;
