From aa2831307bcc78fbf67ecd12b9c13f86c8c41b37 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 17 Sep 2025 17:58:44 +0700 Subject: [PATCH] #158 --- src/controllers/PositionController.ts | 1 + src/controllers/ReportController.ts | 124 ++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 7b5c27c4..21e79209 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2355,6 +2355,7 @@ export class PositionController extends Controller { .addOrderBy("orgChild3.orgChild3Order", "ASC") .addOrderBy("orgChild4.orgChild4Order", "ASC") .addOrderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.createdAt", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 2c309096..2c4be4c4 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -691,7 +691,13 @@ export class ReportController extends Controller { // ดึงข้อมูล orgRoot ตาม nodeId และลูกทั้งหมด (cascade 0..4) const orgRootData = await this.orgRootRepository.find({ where: { id: _nodeId }, - order: { orgRootOrder: "ASC" }, + order: { + orgRootOrder: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -708,7 +714,13 @@ export class ReportController extends Controller { const orgRootIds = orgRootData.map((r) => r.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds) }, - order: { orgChild1Order: "ASC" }, + order: { + orgChild1Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -720,7 +732,13 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { orgChild2Order: "ASC" }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -732,7 +750,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -744,7 +768,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1348,7 +1378,13 @@ export class ReportController extends Controller { // ดึง orgChild1 และลูก (orgChild2, orgChild3, orgChild4) const orgChild1Data = await this.child1Repository.find({ where: { id: _nodeId }, - order: { orgChild1Order: "ASC" }, + order: { + orgChild1Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1365,7 +1401,13 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { orgChild2Order: "ASC" }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1377,7 +1419,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1389,7 +1437,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1869,8 +1923,16 @@ export class ReportController extends Controller { case 2: { // ดึง orgChild2 และลูก (orgChild3, orgChild4) const orgChild2Data = await this.child2Repository.find({ - where: { id: _nodeId }, - order: { orgChild2Order: "ASC" }, + where: { + id: _nodeId, + }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1887,7 +1949,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1899,7 +1967,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2263,7 +2337,13 @@ export class ReportController extends Controller { // ดึง orgChild3 และลูก (orgChild4) const orgChild3Data = await this.child3Repository.find({ where: { id: _nodeId }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2280,7 +2360,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2527,7 +2613,13 @@ export class ReportController extends Controller { // ดึงข้อมูล orgChild4 ตาม nodeId const orgChild4Data = await this.child4Repository.find({ where: { id: _nodeId }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions",