From 2ce104b852ed422108c8acb30dbd8ed6db8e37d9 Mon Sep 17 00:00:00 2001 From: harid Date: Fri, 22 May 2026 13:55:55 +0700 Subject: [PATCH] =?UTF-8?q?condition=20=E0=B8=97=E0=B8=B5=E0=B9=88?= =?UTF-8?q?=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=95=E0=B8=B3=E0=B9=81?= =?UTF-8?q?=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87,=20=E0=B8=9B=E0=B8=A3?= =?UTF-8?q?=E0=B8=B0=E0=B9=80=E0=B8=A0=E0=B8=97=20=E0=B9=81=E0=B8=A5?= =?UTF-8?q?=E0=B8=B0=20=E0=B8=A3=E0=B8=B0=E0=B8=94=E0=B8=B1=E0=B8=9A?= =?UTF-8?q?=E0=B8=8B=E0=B9=89=E0=B8=B3=E0=B8=81=E0=B8=B1=E0=B8=99=20?= =?UTF-8?q?=E0=B9=83=E0=B8=AB=E0=B9=89=E0=B9=83=E0=B8=8A=E0=B9=89=E0=B9=81?= =?UTF-8?q?=E0=B8=96=E0=B8=A7=E0=B8=A5=E0=B8=B3=E0=B8=94=E0=B8=B1=E0=B8=9A?= =?UTF-8?q?=E0=B9=81=E0=B8=A3=E0=B8=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 46 ++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 42685c20..0b2c1b12 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3834,6 +3834,7 @@ export class CommandController extends Controller { const positionBy7Fields = await this.positionRepository.findOne({ where: whereCondition, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy7Fields) { @@ -3849,10 +3850,11 @@ export class CommandController extends Controller { where: { posMasterId: posMaster.id, positionName: item.positionName, - posTypeId: item.positionType, // positionType = posTypeId - posLevelId: item.positionLevel, // positionLevel = posLevelId + posTypeId: item.positionType, + posLevelId: item.positionLevel, }, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy3Fields) { @@ -4138,6 +4140,7 @@ export class CommandController extends Controller { commandCode?: string | null; commandName?: string | null; remark: string | null; + positionId?: string | null; positionTypeNew?: string | null; positionLevelNew?: string | null; positionNameNew?: string | null; @@ -4403,11 +4406,36 @@ export class CommandController extends Controller { // posMaster.isCondition = false; await this.posMasterRepository.save(posMaster); - // Match position ตามลำดับ priority + // Match position ตามลำดับ priority: + // Condition 1: match จาก positionId + // Condition 2: match 7 ฟิลด์ (positionName, posTypeId, posLevelId, positionField, positionArea, positionExecutiveField, posExecutiveId) + // Condition 3: match 3 ฟิลด์ (positionName, posTypeId, posLevelId) + // Fallback: เลือก position แรกใน posMaster + let positionNew: Position | null = null; - // CONDITION 1: Match 7 ฟิลด์ (ไม่มี positionId ใน body สำหรับกรณีพักราชการ) - if (item.positionNameNew && item.positionTypeNew && item.positionLevelNew) { + // ═══════════════════════════════════════════════════════════ + // CONDITION 1: เช็คจาก positionId ตรง + // ═══════════════════════════════════════════════════════════ + if (item.positionId) { + const positionById = await this.positionRepository.findOne({ + where: { + id: item.positionId, + posMasterId: posMaster.id, // ต้องอยู่ใน posMaster ที่ถูกต้อง + }, + relations: ["posExecutive"], + }); + + if (positionById) { + positionNew = positionById; + } + } + + // ═══════════════════════════════════════════════════════════ + // CONDITION 2: Match 7 ฟิลด์ (ถ้า Condition 1 ไม่ match) + // ═══════════════════════════════════════════════════════════ + if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) { + // สร้าง where clause แบบ dynamic - ใส่เฉพาะฟิลด์ที่มีค่า const whereCondition: any = { posMasterId: posMaster.id, positionName: item.positionNameNew, @@ -4431,6 +4459,7 @@ export class CommandController extends Controller { const positionBy7Fields = await this.positionRepository.findOne({ where: whereCondition, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy7Fields) { @@ -4438,7 +4467,9 @@ export class CommandController extends Controller { } } - // CONDITION 2: Match 3 ฟิลด์ (ถ้า Condition 1 ไม่ match) + // ═══════════════════════════════════════════════════════════ + // CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match) + // ═══════════════════════════════════════════════════════════ if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) { const positionBy3Fields = await this.positionRepository.findOne({ where: { @@ -4448,6 +4479,7 @@ export class CommandController extends Controller { posLevelId: item.positionLevelNew, }, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy3Fields) { @@ -7305,6 +7337,7 @@ export class CommandController extends Controller { const positionBy7Fields = await this.positionRepository.findOne({ where: whereCondition, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy7Fields) { @@ -7324,6 +7357,7 @@ export class CommandController extends Controller { posLevelId: item.bodyPosition.posLevelId, }, relations: ["posExecutive"], + order: { orderNo: "ASC" } }); if (positionBy3Fields) {