-- =====================================================
-- Update position fields in profile table
-- อัพเดทฟิลด์ตำแหน่งในตาราง profile
--
-- Fields:
-- - positionField (สายงาน)
-- - posExecutive (ตำแหน่งทางการบริหาร)
-- - positionArea (ด้าน/สาขา)
-- - positionExecutiveField (ด้านทางการบริหาร)
-- - posMasterNo (เลขที่ตำแหน่ง) - format: orgShortName + space + number
-- - org (สังกัด)
--
-- Run each query separately to verify results
-- =====================================================
USE hrms_organization;
-- 1. Update positionField (สายงาน)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionField = pos.positionField
WHERE p.positionField IS NULL;

-- 2. Update posExecutive (ตำแหน่งทางการบริหาร)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
INNER JOIN posExecutive pe ON pos.posExecutiveId = pe.id
SET p.posExecutive = pe.posExecutiveName
WHERE p.posExecutive IS NULL;

-- 3. Update positionArea (ด้าน/สาขา)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionArea = pos.positionArea
WHERE p.positionArea IS NULL;

-- 4. Update positionExecutiveField (ด้านทางการบริหาร)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionExecutiveField = pos.positionExecutiveField
WHERE p.positionExecutiveField IS NULL;

-- 5. Update posMasterNo (เลขที่ตำแหน่ง) - format: orgShortName + space + number
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id
SET p.posMasterNo = TRIM(CONCAT(
    CASE
        WHEN pm.orgChild1Id IS NULL THEN r.orgRootShortName
        WHEN pm.orgChild2Id IS NULL THEN c1.orgChild1ShortName
        WHEN pm.orgChild3Id IS NULL THEN c2.orgChild2ShortName
        WHEN pm.orgChild4Id IS NULL THEN c3.orgChild3ShortName
        ELSE c4.orgChild4ShortName
    END,
    ' ',
    CONCAT_WS('', pm.posMasterNoPrefix, pm.posMasterNo, pm.posMasterNoSuffix)
))
WHERE p.posMasterNo IS NULL;

-- 6. Update org (สังกัด) - combine all org levels
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id
SET p.org = TRIM(CONCAT_WS(
    CHAR(10),
    c4.orgChild4Name,
    c3.orgChild3Name,
    c2.orgChild2Name,
    c1.orgChild1Name,
    r.orgRootName
))
WHERE p.org IS NULL;

-- =====================================================
-- เช็คผลลัพธ์ (Check results)
-- =====================================================

-- เช็คจำนวนที่ update ได้
SELECT
    COUNT(CASE WHEN positionField IS NOT NULL THEN 1 END) AS has_positionField,
    COUNT(CASE WHEN posExecutive IS NOT NULL THEN 1 END) AS has_posExecutive,
    COUNT(CASE WHEN positionArea IS NOT NULL THEN 1 END) AS has_positionArea,
    COUNT(CASE WHEN positionExecutiveField IS NOT NULL THEN 1 END) AS has_positionExecutiveField,
    COUNT(CASE WHEN posMasterNo IS NOT NULL THEN 1 END) AS has_posMasterNo,
    COUNT(CASE WHEN org IS NOT NULL THEN 1 END) AS has_org
FROM profile;

-- =====================================================
-- SELECT query สำหรับทดสอบก่อนรัน (Test before run)
-- =====================================================

SELECT
    p.id,
    p.firstName,
    p.lastName,
    p.citizenId,

    p.positionField as old_positionField,
    p.posExecutive as old_posExecutive,
    p.positionArea as old_positionArea,
    p.positionExecutiveField as old_positionExecutiveField,
    p.posMasterNo as old_posMasterNo,
    p.org as old_org,

    pos.positionField as new_positionField,
    pe.posExecutiveName as new_posExecutive,
    pos.positionArea as new_positionArea,
    pos.positionExecutiveField as new_positionExecutiveField,

    TRIM(CONCAT(
        CASE
            WHEN pm.orgChild1Id IS NULL THEN r.orgRootShortName
            WHEN pm.orgChild2Id IS NULL THEN c1.orgChild1ShortName
            WHEN pm.orgChild3Id IS NULL THEN c2.orgChild2ShortName
            WHEN pm.orgChild4Id IS NULL THEN c3.orgChild3ShortName
            ELSE c4.orgChild4ShortName
        END,
        ' ',
        pm.posMasterNo
    )) as new_posMasterNo,

    TRIM(CONCAT_WS(CHAR(10), c4.orgChild4Name, c3.orgChild3Name, c2.orgChild2Name, c1.orgChild1Name, r.orgRootName)) as new_org

FROM profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
LEFT JOIN posExecutive pe ON pos.posExecutiveId = pe.id
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id

-- ใส่ WHERE ทดสอบ 1 คน (Test 1 person)
WHERE p.id = 'ใส่ profile_id ที่ต้องการทดสอบ'
-- หรือทดสอบ 10 คน (Test 10 persons)
-- LIMIT 10;
