{"id":117,"date":"2025-12-09T16:13:34","date_gmt":"2025-12-09T08:13:34","guid":{"rendered":"https:\/\/wiki.tengwangyun.com\/?p=117"},"modified":"2026-04-09T14:03:22","modified_gmt":"2026-04-09T06:03:22","slug":"%e8%b7%91%e7%99%bb%e5%bd%95%e6%97%a5%e5%bf%97%e3%80%81%e6%93%8d%e4%bd%9c%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/wiki.tengwangyun.com\/?p=117","title":{"rendered":"\u8dd1\u767b\u5f55\u65e5\u5fd7\u3001\u64cd\u4f5c\u65e5\u5fd7"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>\u64cd\u4f5c\u65e5\u5fd7\uff1a<\/p>\n\n\n\n<p>CREATE  <code>GenerateActivityLogs<\/code>(<br>IN start_id INT,<br>IN num_rows INT,<br>IN start_date DATE,<br>IN end_date DATE<br>)<br>BEGIN<br>DECLARE i INT DEFAULT 0;<br>DECLARE current_id INT;<br>DECLARE random_log_type INT;<br>DECLARE random_subject_id INT;<br>DECLARE random_causer_id INT;<br>DECLARE random_date DATE;<br>DECLARE random_time TIME;<br>DECLARE random_datetime DATETIME;<br>DECLARE update_datetime DATETIME;<br>DECLARE product_count INT;<br>DECLARE customer_count INT;<br>DECLARE sales_order_count INT;<br>DECLARE production_task_count INT;<br>DECLARE user_count INT;<br>DECLARE days_range INT;<br>DECLARE log_name_val VARCHAR(255) CHARSET utf8mb4;<br>DECLARE description_val VARCHAR(255) CHARSET utf8mb4;<br>DECLARE subject_type_val VARCHAR(255) CHARSET utf8mb4;<br>DECLARE subject_code_val VARCHAR(255) CHARSET utf8mb4;<br>DECLARE properties_json JSON;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u8bbe\u7f6e\u4f1a\u8bdd\u5b57\u7b26\u96c6\nSET NAMES utf8mb4;\nSET CHARACTER SET utf8mb4;\n\n-- \u5f00\u59cb\u4e8b\u52a1\nSTART TRANSACTION;\n\nSET current_id = start_id;\nSET days_range = DATEDIFF(end_date, start_date);\n\n-- \u83b7\u53d6\u5404\u8868\u6570\u91cf\nSELECT COUNT(*) INTO product_count FROM products;\nSELECT COUNT(*) INTO customer_count FROM customers;\nSELECT COUNT(*) INTO sales_order_count FROM sales_orders;\nSELECT COUNT(*) INTO production_task_count FROM production_tasks;\nSELECT COUNT(*) INTO user_count FROM users;\n\nWHILE i &lt; num_rows DO\n    -- \u968f\u673a\u9009\u62e9\u65e5\u5fd7\u7c7b\u578b\uff1a1=\u4ea7\u54c1, 2=\u5ba2\u6237, 3=\u9500\u552e\u8ba2\u5355, 4=\u751f\u4ea7\u4efb\u52a1\n    SET random_log_type = FLOOR(1 + RAND() * 4);\n\n    -- \u968f\u673a\u9009\u62e9\u64cd\u4f5c\u7528\u6237\n    SELECT id INTO random_causer_id FROM users ORDER BY RAND() LIMIT 1;\n\n    -- \u751f\u6210\u968f\u673a\u521b\u5efa\u65e5\u671f\n    SET random_date = DATE_ADD(start_date, INTERVAL FLOOR(RAND() * (days_range + 1)) DAY);\n    SET random_time = SEC_TO_TIME(FLOOR(RAND() * 35400) + 28800);\n    SET random_datetime = TIMESTAMP(random_date, random_time);\n\n    -- \u751f\u6210\u968f\u673a\u66f4\u65b0\u65e5\u671f\uff0850%\u6982\u7387\u4e0e\u521b\u5efa\u65f6\u95f4\u4e0d\u540c\uff09\n    IF RAND() &gt; 0.5 THEN\n        SET update_datetime = DATE_ADD(random_datetime, INTERVAL FLOOR(1 + RAND() * 1440) MINUTE);\n    ELSE\n        SET update_datetime = random_datetime;\n    END IF;\n\n    CASE random_log_type\n        WHEN 1 THEN -- \u4ea7\u54c1\n            IF product_count &gt; 0 THEN\n                SELECT id, CONVERT(code USING utf8mb4) INTO random_subject_id, subject_code_val \n                FROM products ORDER BY RAND() LIMIT 1;\n\n                SET log_name_val = 'products';\n                SET subject_type_val = 'App\\\\Models\\\\Product';\n\n                -- \u751f\u6210\u521b\u5efa\u4ea7\u54c1\u8bb0\u5f55\n                SET description_val = CONCAT('\u521b\u5efa\u4ea7\u54c1{', subject_code_val, '}');\n                SET properties_json = JSON_OBJECT(\n                    'attributes', JSON_OBJECT(\n                        'id', random_subject_id,\n                        'code', subject_code_val,\n                        'created_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s'),\n                        'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                    )\n                );\n\n                INSERT INTO `activity_log` (\n                    `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                    `causer_id`, `causer_type`, `properties`, `location`, \n                    `created_at`, `updated_at`\n                ) VALUES (\n                    current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                    random_causer_id, 'App\\\\Models\\\\User', properties_json, '\u6c5f\u897f\u5b9c\u6625',\n                    random_datetime, random_datetime\n                );\n\n                SET current_id = current_id + 1;\n\n                -- \u5982\u679c\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u4e0d\u540c\uff0c\u751f\u6210\u66f4\u65b0\u4ea7\u54c1\u8bb0\u5f55\n                IF update_datetime != random_datetime THEN\n                    SET description_val = CONCAT('\u66f4\u65b0\u4ea7\u54c1{', subject_code_val, '}');\n                    SET properties_json = JSON_OBJECT(\n                        'old', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                        ),\n                        'attributes', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(update_datetime, '%Y-%m-%d %H:%i:%s')\n                        )\n                    );\n\n                    INSERT INTO `activity_log` (\n                        `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                        `causer_id`, `causer_type`, `properties`, `location`, \n                        `created_at`, `updated_at`\n                    ) VALUES (\n                        current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                        random_causer_id, 'App\\\\Models\\\\User', properties_json, '\u6c5f\u897f\u5b9c\u6625',\n                        update_datetime, update_datetime\n                    );\n\n                    SET current_id = current_id + 1;\n                END IF;\n            END IF;\n\n        WHEN 2 THEN -- \u5ba2\u6237\n            IF customer_count &gt; 0 THEN\n                SELECT id, CONVERT(name USING utf8mb4) INTO random_subject_id, subject_code_val \n                FROM customers ORDER BY RAND() LIMIT 1;\n\n                SET log_name_val = 'customers';\n                SET subject_type_val = 'App\\\\Models\\\\Customer';\n\n                -- \u751f\u6210\u521b\u5efa\u5ba2\u6237\u8bb0\u5f55\n                SET description_val = CONCAT('\u521b\u5efa\u5ba2\u6237{', subject_code_val, '}');\n                SET properties_json = JSON_OBJECT(\n                    'attributes', JSON_OBJECT(\n                        'id', random_subject_id,\n                        'name', subject_code_val,\n                        'created_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s'),\n                        'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                    )\n                );\n\n                INSERT INTO `activity_log` (\n                    `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                    `causer_id`, `causer_type`, `properties`, `ip`, `location`, \n                    `created_at`, `updated_at`\n                ) VALUES (\n                    current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                    random_causer_id, 'App\\\\Models\\\\User', properties_json, '115.153.102.58', '\u6c5f\u897f\u5b9c\u6625',\n                    random_datetime, random_datetime\n                );\n\n                SET current_id = current_id + 1;\n            END IF;\n\n        WHEN 3 THEN -- \u9500\u552e\u8ba2\u5355\n            IF sales_order_count &gt; 0 THEN\n                SELECT id, CONVERT(serial_code USING utf8mb4) INTO random_subject_id, subject_code_val \n                FROM sales_orders ORDER BY RAND() LIMIT 1;\n\n                SET log_name_val = 'sales_orders';\n                SET subject_type_val = 'App\\\\Models\\\\SalesOrder';\n\n                -- \u751f\u6210\u521b\u5efa\u8ba2\u5355\u8bb0\u5f55\n                SET description_val = CONCAT('\u521b\u5efa\u8ba2\u5355{', subject_code_val, '}');\n                SET properties_json = JSON_OBJECT(\n                    'attributes', JSON_OBJECT(\n                        'id', random_subject_id,\n                        'serial_code', subject_code_val,\n                        'created_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s'),\n                        'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                    )\n                );\n\n                INSERT INTO `activity_log` (\n                    `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                    `causer_id`, `causer_type`, `properties`, `ip`, `location`, \n                    `created_at`, `updated_at`\n                ) VALUES (\n                    current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                    random_causer_id, 'App\\\\Models\\\\User', properties_json, '115.153.102.58', '\u6c5f\u897f\u5b9c\u6625',\n                    random_datetime, random_datetime\n                );\n\n                SET current_id = current_id + 1;\n\n                -- \u5982\u679c\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u4e0d\u540c\uff0c\u751f\u6210\u66f4\u65b0\u8ba2\u5355\u8bb0\u5f55\n                IF update_datetime != random_datetime THEN\n                    SET description_val = CONCAT('\u66f4\u65b0\u8ba2\u5355{', subject_code_val, '}');\n                    SET properties_json = JSON_OBJECT(\n                        'old', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                        ),\n                        'attributes', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(update_datetime, '%Y-%m-%d %H:%i:%s')\n                        )\n                    );\n\n                    INSERT INTO `activity_log` (\n                        `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                        `causer_id`, `causer_type`, `properties`, `ip`, `location`, \n                        `created_at`, `updated_at`\n                    ) VALUES (\n                        current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                        random_causer_id, 'App\\\\Models\\\\User', properties_json, '115.153.102.58', '\u6c5f\u897f\u5b9c\u6625',\n                        update_datetime, update_datetime\n                    );\n\n                    SET current_id = current_id + 1;\n\n                    -- \u968f\u673a\u751f\u6210\u6253\u5f00\u8ba2\u5355\u8be6\u60c5\u8bb0\u5f55\uff0830%\u6982\u7387\uff09\n                    IF RAND() &lt; 0.3 THEN\n                        SET description_val = CONCAT('\u6253\u5f00\u8ba2\u5355\u8be6\u60c5{', subject_code_val, '}');\n                        SET properties_json = JSON_OBJECT();\n\n                        INSERT INTO `activity_log` (\n                            `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                            `causer_id`, `causer_type`, `properties`, `ip`, `location`, \n                            `created_at`, `updated_at`\n                        ) VALUES (\n                            current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                            random_causer_id, 'App\\\\Models\\\\User', properties_json, '115.153.102.58', '\u6c5f\u897f\u5b9c\u6625',\n                            update_datetime, update_datetime\n                        );\n\n                        SET current_id = current_id + 1;\n                    END IF;\n                END IF;\n            END IF;\n\n        WHEN 4 THEN -- \u751f\u4ea7\u4efb\u52a1\n            IF production_task_count &gt; 0 THEN\n                SELECT id, CONVERT(serial_code USING utf8mb4) INTO random_subject_id, subject_code_val \n                FROM production_tasks ORDER BY RAND() LIMIT 1;\n\n                SET log_name_val = 'production_tasks';\n                SET subject_type_val = 'App\\\\Models\\\\ProductionTask';\n\n                -- \u751f\u6210\u521b\u5efa\u751f\u4ea7\u4efb\u52a1\u8bb0\u5f55\n                SET description_val = CONCAT('\u521b\u5efa\u751f\u4ea7\u4efb\u52a1{', subject_code_val, '}');\n                SET properties_json = JSON_OBJECT(\n                    'attributes', JSON_OBJECT(\n                        'id', random_subject_id,\n                        'task_code', subject_code_val,\n                        'created_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s'),\n                        'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                    )\n                );\n\n                INSERT INTO `activity_log` (\n                    `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                    `causer_id`, `causer_type`, `properties`, `location`, \n                    `created_at`, `updated_at`\n                ) VALUES (\n                    current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                    random_causer_id, 'App\\\\Models\\\\User', properties_json, '\u6c5f\u897f\u5b9c\u6625',\n                    random_datetime, random_datetime\n                );\n\n                SET current_id = current_id + 1;\n\n                -- \u5982\u679c\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u4e0d\u540c\uff0c\u751f\u6210\u66f4\u65b0\u751f\u4ea7\u4efb\u52a1\u8bb0\u5f55\n                IF update_datetime != random_datetime THEN\n                    SET description_val = CONCAT('\u66f4\u65b0\u751f\u4ea7\u4efb\u52a1{', subject_code_val, '}');\n                    SET properties_json = JSON_OBJECT(\n                        'old', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(random_datetime, '%Y-%m-%d %H:%i:%s')\n                        ),\n                        'attributes', JSON_OBJECT(\n                            'updated_at', DATE_FORMAT(update_datetime, '%Y-%m-%d %H:%i:%s')\n                        )\n                    );\n\n                    INSERT INTO `activity_log` (\n                        `id`, `log_name`, `description`, `subject_id`, `subject_type`, \n                        `causer_id`, `causer_type`, `properties`, `location`, \n                        `created_at`, `updated_at`\n                    ) VALUES (\n                        current_id, log_name_val, description_val, random_subject_id, subject_type_val,\n                        random_causer_id, 'App\\\\Models\\\\User', properties_json, '\u6c5f\u897f\u5b9c\u6625',\n                        update_datetime, update_datetime\n                    );\n\n                    SET current_id = current_id + 1;\n                END IF;\n            END IF;\n    END CASE;\n\n    SET i = i + 1;\nEND WHILE;\n\n-- \u63d0\u4ea4\u4e8b\u52a1\nCOMMIT;\n\n-- \u8fd4\u56de\u6210\u529f\u4fe1\u606f\nSELECT CONCAT('\u6210\u529f\u751f\u6210 ', i, ' \u6761\u6d3b\u52a8\u65e5\u5fd7\u8bb0\u5f55\uff0cID\u8303\u56f4: ', start_id, ' - ', current_id - 1) AS result;<\/code><\/pre>\n\n\n\n<p>END<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8dd1\u767b\u5f55\u65e5\u5fd7\uff0c\u6839\u636e\u65f6\u95f4\u95f4\u9694\u66f4\u6539\uff0c\u5982\u4e0b\uff1a\u65f6\u95f4\u95f4\u9694225=2025\/09\/26-2025\/03\/17<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"938\" height=\"480\" src=\"https:\/\/wiki.tengwangyun.com\/wp-content\/uploads\/2025\/12\/image.png\" alt=\"\" class=\"wp-image-118\" srcset=\"https:\/\/wiki.tengwangyun.com\/wp-content\/uploads\/2025\/12\/image.png 938w, https:\/\/wiki.tengwangyun.com\/wp-content\/uploads\/2025\/12\/image-300x154.png 300w, https:\/\/wiki.tengwangyun.com\/wp-content\/uploads\/2025\/12\/image-768x393.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>CREATE   PROCEDURE <code>GenerateLoginLogData<\/code>(IN num_rows INT)<br>BEGIN<br>DECLARE i INT DEFAULT 0;<br>DECLARE current_id INT DEFAULT 0;<br>DECLARE random_username VARCHAR(255);<br>DECLARE random_name VARCHAR(255);<br>DECLARE random_user_info VARCHAR(255);<br>DECLARE random_date DATE;<br>DECLARE random_time TIME;<br>DECLARE random_datetime DATETIME;<br>DECLARE user_count INT;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u7528\u6237\u8868\u4e2d\u7684\u7528\u6237\u6570\u91cf\nSELECT COUNT(*) INTO user_count FROM users;\n  SELECT MAX(id)+1 into current_id FROM login_log ;\n\n        -- \u751f\u621010\u6761\u6d3b\u52a8\u65e5\u5fd7\uff0c<\/code><\/pre>\n\n\n\n<p>&#8212; CALL GenerateLoginLogData(10&#8242;);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5982\u679c\u7528\u6237\u8868\u4e3a\u7a7a\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\nIF user_count = 0 THEN\n    SET random_user_info = 'admin-\u7cfb\u7edf\u7ba1\u7406\u5458';\nELSE\n    WHILE i &lt; num_rows DO\n        -- \u968f\u673a\u9009\u62e9\u7528\u6237\u540d\u548c\u59d3\u540d\n        SELECT username, name INTO random_username, random_name \n        FROM users \n        ORDER BY RAND() \n        LIMIT 1;\n\n        -- \u62fc\u63a5\u7528\u6237\u4fe1\u606f\n        SET random_user_info = CONCAT(random_username, '-', random_name);\n\n        -- \u751f\u6210\u968f\u673a\u65e5\u671f\uff082025-03-17 \u5230 2025-09-26\uff09\n        SET random_date = DATE_ADD('2025-03-15', INTERVAL FLOOR(RAND() * 225) DAY);\n\n        -- \u751f\u6210\u968f\u673a\u65f6\u95f4\uff0808:00:00 \u5230 17:50:00\uff09\n        SET random_time = SEC_TO_TIME(FLOOR(RAND() * 35400) + 28800); -- 28800\u79d2=8\u5c0f\u65f6, 35400\u79d2=9\u5c0f\u65f650\u5206\u949f\n\n        -- \u7ec4\u5408\u65e5\u671f\u65f6\u95f4\n        SET random_datetime = CONCAT(random_date, ' ', random_time);\n\n        -- \u63d2\u5165\u767b\u5f55\u65e5\u5fd7\n        INSERT INTO `login_log` \n        (`id`, `user_info`, `ip`, `location`, `method`, `user_agent`, `remark`, `created_at`, `updated_at`) \n        VALUES \n        (current_id, random_user_info, '118.212.198.82', '\u6c5f\u897f\u5b9c\u6625', 'POST', \n         'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWeb', '\u767b\u5f55\u6210\u529f', \n         random_datetime, random_datetime);\n\n        SET i = i + 1;\n        SET current_id = current_id + 1;\n    END WHILE;\nEND IF;<\/code><\/pre>\n\n\n\n<p>END<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u64cd\u4f5c\u65e5\u5fd7\uff1a CREATE GenerateActivityLogs(IN start_id INT,IN nu [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-twy"],"_links":{"self":[{"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=117"}],"version-history":[{"count":1,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.tengwangyun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}