From e04ec1243d84da0b0cf67fe6d63325ac1ca275f9 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 3 Dec 2025 17:31:40 +0700 Subject: [PATCH] 251203:1700 Prepare to version 1.6 --- docs/8_lcbp3_v1_5_1.sql | 66 ++++++++++++++---------------------- docs/8_lcbp3_v1_5_1_seed.sql | 47 ++++++++++++++----------- lcbp3.code-workspace | 30 ++++++++++------ 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/docs/8_lcbp3_v1_5_1.sql b/docs/8_lcbp3_v1_5_1.sql index 23bf0e4..65f0609 100644 --- a/docs/8_lcbp3_v1_5_1.sql +++ b/docs/8_lcbp3_v1_5_1.sql @@ -141,10 +141,12 @@ CREATE TABLE organizations ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'ID ของตาราง', organization_code VARCHAR(20) NOT NULL UNIQUE COMMENT 'รหัสองค์กร', organization_name VARCHAR(255) NOT NULL COMMENT 'ชื่อองค์กร', - -- role_id INT COMMENT 'บทบาทขององค์กร', + role_id INT COMMENT 'บทบาทขององค์กร', is_active BOOLEAN DEFAULT TRUE COMMENT 'สถานะการใช้งาน', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'วันที่สร้าง', - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'วันที่แก้ไขล่าสุด' -- FOREIGN KEY (role_id) REFERENCES organization_roles(id) ON DELETE SET NULL + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'วันที่แก้ไขล่าสุด', + FOREIGN KEY (role_id) REFERENCES organization_roles(id) ON DELETE + SET NULL ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'ตาราง Master เก็บข้อมูลองค์กรทั้งหมดที่เกี่ยวข้องในระบบ'; -- ตาราง Master เก็บข้อมูลโครงการ CREATE TABLE projects ( @@ -789,41 +791,38 @@ CREATE TABLE document_number_counters ( rfa_type_id INT DEFAULT 0 COMMENT '[v1.5.1 NEW] ประเภท RFA เช่น SHD, RPT, MAT (0 = ไม่ใช่ RFA)', discipline_id INT DEFAULT 0 COMMENT 'สาขางาน เช่น TER, STR, GEO (0 = ไม่ระบุ)', current_year INT NOT NULL COMMENT 'ปี ค.ศ. ของตัวนับ (auto-reset ทุกปี)', - -- Counter Data version INT DEFAULT 0 NOT NULL COMMENT 'Optimistic Lock Version (TypeORM @VersionColumn)', last_number INT DEFAULT 0 COMMENT 'เลขที่ล่าสุดที่ใช้ไปแล้ว (auto-increment)', - -- [v1.5.1 UPDATE] Primary Key: 5 columns -> 8 columns -- ใช้ COALESCE เพื่อรองรับ NULL ใน recipient_organization_id PRIMARY KEY ( project_id, originator_organization_id, - COALESCE(recipient_organization_id, 0), -- [v1.5.1 NEW] Handle NULL values + COALESCE(recipient_organization_id, 0), + -- [v1.5.1 NEW] Handle NULL values correspondence_type_id, - sub_type_id, -- [v1.5.1 NEW] - rfa_type_id, -- [v1.5.1 NEW] + sub_type_id, + -- [v1.5.1 NEW] + rfa_type_id, + -- [v1.5.1 NEW] discipline_id, current_year ), - -- Foreign Keys FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE, FOREIGN KEY (originator_organization_id) REFERENCES organizations(id) ON DELETE CASCADE, FOREIGN KEY (recipient_organization_id) REFERENCES organizations(id) ON DELETE CASCADE, FOREIGN KEY (correspondence_type_id) REFERENCES correspondence_types(id) ON DELETE CASCADE, - -- [v1.5.1 NEW] Performance Indexes INDEX idx_counter_lookup (project_id, correspondence_type_id, current_year), INDEX idx_counter_org (originator_organization_id, current_year), - -- [v1.5.1 NEW] Data Validation Constraints CONSTRAINT chk_last_number_positive CHECK (last_number >= 0), - CONSTRAINT chk_current_year_valid CHECK (current_year BETWEEN 2020 AND 2100) - -) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci - COMMENT = '[v1.5.1 UPDATE] ตารางเก็บ Running Number Counters - รองรับ 8-column composite PK'; - + CONSTRAINT chk_current_year_valid CHECK ( + current_year BETWEEN 2020 AND 2100 + ) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '[v1.5.1 UPDATE] ตารางเก็บ Running Number Counters - รองรับ 8-column composite PK'; -- ========================================================== -- [v1.5.1 NEW] ตารางเก็บ Audit Trail สำหรับการสร้างเลขที่เอกสาร -- เพิ่มตาราง: document_number_audit @@ -832,39 +831,30 @@ CREATE TABLE document_number_counters ( -- ========================================================== CREATE TABLE document_number_audit ( id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT 'ID ของ audit record', - -- Document Info document_id INT NOT NULL COMMENT 'ID ของเอกสารที่สร้างเลขที่ (correspondences.id)', generated_number VARCHAR(100) NOT NULL COMMENT 'เลขที่เอกสารที่สร้าง (ผลลัพธ์)', counter_key JSON NOT NULL COMMENT 'Counter key ที่ใช้ (JSON format) - 8 fields', template_used VARCHAR(200) NOT NULL COMMENT 'Template ที่ใช้ในการสร้าง', - -- User Info user_id INT NOT NULL COMMENT 'ผู้ขอสร้างเลขที่', ip_address VARCHAR(45) COMMENT 'IP address ของผู้ขอ (IPv4/IPv6)', user_agent TEXT COMMENT 'User agent string (browser info)', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'วันที่/เวลาที่สร้าง', - -- Performance & Error Tracking retry_count INT DEFAULT 0 COMMENT 'จำนวนครั้งที่ retry ก่อนสำเร็จ', lock_wait_ms INT COMMENT 'เวลารอ Redis lock (milliseconds)', total_duration_ms INT COMMENT 'เวลารวมทั้งหมดในการสร้าง (milliseconds)', - fallback_used ENUM('NONE', 'DB_LOCK', 'RETRY') DEFAULT 'NONE' - COMMENT 'Fallback strategy ที่ถูกใช้ (NONE=normal, DB_LOCK=Redis down, RETRY=conflict)', - + fallback_used ENUM('NONE', 'DB_LOCK', 'RETRY') DEFAULT 'NONE' COMMENT 'Fallback strategy ที่ถูกใช้ (NONE=normal, DB_LOCK=Redis down, RETRY=conflict)', -- Indexes for performance INDEX idx_document_id (document_id), INDEX idx_user_id (user_id), INDEX idx_created_at (created_at), INDEX idx_generated_number (generated_number), - -- Foreign Keys FOREIGN KEY (document_id) REFERENCES correspondences(id) ON DELETE CASCADE, FOREIGN KEY (user_id) REFERENCES users(user_id) - -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci - COMMENT='[v1.5.1 NEW] Audit Trail สำหรับการสร้างเลขที่เอกสาร - เก็บ ≥ 7 ปี'; - +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '[v1.5.1 NEW] Audit Trail สำหรับการสร้างเลขที่เอกสาร - เก็บ ≥ 7 ปี'; -- ========================================================== -- [v1.5.1 NEW] ตารางเก็บ Error Logs สำหรับ Document Numbering -- เพิ่มตาราง: document_number_errors @@ -873,38 +863,34 @@ CREATE TABLE document_number_audit ( -- ========================================================== CREATE TABLE document_number_errors ( id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT 'ID ของ error record', - -- Error Classification error_type ENUM( - 'LOCK_TIMEOUT', -- Redis lock timeout - 'VERSION_CONFLICT', -- Optimistic lock version mismatch - 'DB_ERROR', -- Database connection/query error - 'REDIS_ERROR', -- Redis connection error - 'VALIDATION_ERROR' -- Template/input validation error + 'LOCK_TIMEOUT', + -- Redis lock timeout + 'VERSION_CONFLICT', + -- Optimistic lock version mismatch + 'DB_ERROR', + -- Database connection/query error + 'REDIS_ERROR', + -- Redis connection error + 'VALIDATION_ERROR' -- Template/input validation error ) NOT NULL COMMENT 'ประเภท error (5 types)', - -- Error Details error_message TEXT COMMENT 'ข้อความ error (stack top)', stack_trace TEXT COMMENT 'Stack trace แบบเต็ม (สำหรับ debugging)', context_data JSON COMMENT 'Context ของ request (user, project, counter_key, etc.)', - -- User Info user_id INT COMMENT 'ผู้ที่เกิด error', ip_address VARCHAR(45) COMMENT 'IP address', - -- Timestamps created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'วันที่เกิด error', resolved_at TIMESTAMP NULL COMMENT 'วันที่แก้ไขแล้ว (NULL = ยังไม่แก้)', - -- Indexes for troubleshooting INDEX idx_error_type (error_type), INDEX idx_created_at (created_at), INDEX idx_user_id (user_id), INDEX idx_unresolved (resolved_at) -- Find unresolved errors - -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci - COMMENT='[v1.5.1 NEW] Error Log สำหรับ Document Numbering System'; - +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '[v1.5.1 NEW] Error Log สำหรับ Document Numbering System'; -- ===================================================== -- 10. ⚙️ System & Logs (ระบบและ Log) -- ===================================================== diff --git a/docs/8_lcbp3_v1_5_1_seed.sql b/docs/8_lcbp3_v1_5_1_seed.sql index 1f967d1..8472933 100644 --- a/docs/8_lcbp3_v1_5_1_seed.sql +++ b/docs/8_lcbp3_v1_5_1_seed.sql @@ -1,23 +1,32 @@ -INSERT INTO organizations (id, organization_code, organization_name) +INSERT INTO organization_roles (id, role_name) VALUES - ( 1, 'กทท.' , 'การท่าเรือแห่งประเทศไทย'), - ( 10, 'สคฉ.3' , 'โครงการพัฒนาท่าเรือแหลมฉบัง ระยะที่ 3' ), - ( 11, 'สคฉ.3-01' , 'ตรวจรับพัสดุ ที่ปรึกษาควบคุมงาน'), - ( 12, 'สคฉ.3-02' , 'ตรวจรับพัสดุ งานทางทะเล'), - ( 13, 'สคฉ.3-03' , 'ตรวจรับพัสดุ อาคารและระบบสาธารณูปโภค'), - ( 14, 'สคฉ.3-04' , 'ตรวจรับพัสดุ ตรวจสอบผลกระทบสิ่งแวดล้อม'), - ( 15, 'สคฉ.3-05' , 'ตรวจรับพัสดุ เยียวยาการประมง'), - ( 16, 'สคฉ.3-06' , 'ตรวจรับพัสดุ งานก่อสร้าง ส่วนที่ 3'), - ( 17, 'สคฉ.3-07' , 'ตรวจรับพัสดุ งานก่อสร้าง ส่วนที่ 4'), - ( 18, 'สคฉ.3-xx' , 'ตรวจรับพัสดุ ที่ปรึกษาออกแบบ ส่วนที่ 4'), - ( 21, 'TEAM' , 'Designer Consulting Ltd.'), - ( 22, 'คคง.' , 'Construction Supervision Ltd.'), - ( 41, 'ผรม.1' , 'Contractor งานทางทะเล'), - ( 42, 'ผรม.2' , 'Contractor อาคารและระบบ'), - ( 43, 'ผรม.3' , 'Contractor #3 Ltd.'), - ( 44, 'ผรม.4' , 'Contractor #4 Ltd.'), - ( 31, 'EN' , 'Third Party Environment'), - ( 32, 'CAR' , 'Third Party Fishery Care'); + ( 1, 'OWNER' ), + ( 2, 'DESIGNER' ), + ( 3, 'CONSULTANT' ), + ( 4, 'CONTRACTOR' ), + ( 5, 'THIRD PARTY' ), + ( 6, 'GUEST' ); + +INSERT INTO organizations (id, organization_code, organization_name, role_id) +VALUES + ( 1, 'กทท.' , 'การท่าเรือแห่งประเทศไทย', 1), + ( 10, 'สคฉ.3' , 'โครงการพัฒนาท่าเรือแหลมฉบัง ระยะที่ 3', 1), + ( 11, 'สคฉ.3-01' , 'ตรวจรับพัสดุ ที่ปรึกษาควบคุมงาน', 1), + ( 12, 'สคฉ.3-02' , 'ตรวจรับพัสดุ งานทางทะเล', 1), + ( 13, 'สคฉ.3-03' , 'ตรวจรับพัสดุ อาคารและระบบสาธารณูปโภค', 1), + ( 14, 'สคฉ.3-04' , 'ตรวจรับพัสดุ ตรวจสอบผลกระทบสิ่งแวดล้อม', 1), + ( 15, 'สคฉ.3-05' , 'ตรวจรับพัสดุ เยียวยาการประมง', 1), + ( 16, 'สคฉ.3-06' , 'ตรวจรับพัสดุ งานก่อสร้าง ส่วนที่ 3', 1), + ( 17, 'สคฉ.3-07' , 'ตรวจรับพัสดุ งานก่อสร้าง ส่วนที่ 4', 1), + ( 18, 'สคฉ.3-xx' , 'ตรวจรับพัสดุ ที่ปรึกษาออกแบบ ส่วนที่ 4', 1), + ( 21, 'TEAM' , 'Designer Consulting Ltd.', 2), + ( 22, 'คคง.' , 'Construction Supervision Ltd.', 3), + ( 41, 'ผรม.1' , 'Contractor งานทางทะเล', 4), + ( 42, 'ผรม.2' , 'Contractor อาคารและระบบ', 4), + ( 43, 'ผรม.3' , 'Contractor #3 Ltd.', 4), + ( 44, 'ผรม.4' , 'Contractor #4 Ltd.', 4), + ( 31, 'EN' , 'Third Party Environment', 5), + ( 32, 'CAR' , 'Third Party Fishery Care', 5); -- Seed project INSERT INTO projects (project_code, project_name) VALUES diff --git a/lcbp3.code-workspace b/lcbp3.code-workspace index df634f4..6552246 100644 --- a/lcbp3.code-workspace +++ b/lcbp3.code-workspace @@ -81,16 +81,17 @@ "language": "sql", "params": { "keywordCase": "upper", // ทำให้ INSERT, VALUES เป็นตัวใหญ่ - "tabWidth": 2, // เยื้อง 3 ช่องว่าง (ปรับจาก 4 เป็น 3 เพื่อ match ตัวอย่าง) - "expressionWidth": 80, // ความยาวสูงสุดต่อบรรทัดก่อนตีบรรทัดใหม่ (ป้องกันคอลัมน์แยกบรรทัด) "reservedWordCase": "upper", // ทำให้คำสงวนเป็นตัวใหญ่ - "linesBetweenQueries": 1, + "tabWidth": 2, // เยื้อง 2 ช่องว่าง + "expressionWidth": 100, // ความยาวสูงสุดต่อบรรทัดก่อนตีบรรทัดใหม่ (ป้องกันคอลัมน์แยกบรรทัด) "logicalOperatorNewline": "before", "aliasAs": "before", "commaPosition": "after", + "linesBetweenQueries": 1, "linesAroundComments": 0, "tabulateAlias": false, - "newlineBeforeSemicolon": false + "newlineBeforeSemicolon": false, + "newlineBeforeOnClauseItems": false } }, "sqltools.formatOnSave": true, @@ -655,18 +656,25 @@ "debug.openDebug": "openOnDebugBreak", "debug.showBreakpointsInOverviewRuler": true, "prettier.configPath": "./.prettierrc", + "terminal.integrated.copyOnSelection": true, + "terminal.integrated.tabs.defaultColor": "terminal.ansiBlue", "sqltools.connections": [ { - "name": "MariaDB", + "mysqlOptions": { + "authProtocol": "default", + "enableSsl": "Disabled" + }, + "ssh": "Disabled", + "previewLimit": 50, "server": "localhost", "port": 3306, - "user": "root", - "password": "", - "database": "lcbp3" + "driver": "MySQL", + "name": "lcbp3_dev", + "database": "lcbp3_dev", + "username": "root", + "password": "Center#2025" } - ], - "terminal.integrated.copyOnSelection": true, - "terminal.integrated.tabs.defaultColor": "terminal.ansiBlue" + ] }, // ========================================