From 4fd22aac36acecb3d925429040a130a1acb61b31 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 18 Sep 2025 11:13:46 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20report?= =?UTF-8?q?=202=203=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ReportController.ts | 215 ++++++++++++++++++++++++++-- 1 file changed, 206 insertions(+), 9 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a5df992d..036e6285 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3487,9 +3487,70 @@ export class ReportController extends Controller { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } // (async () => { + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgRoot.id; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild1.orgRootId; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild2.orgRootId; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild3.orgRootId; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild4.orgRootId; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const orgRootData = await this.orgRootRepository.find({ where: { - id: reqBody.nodeId, + id: nodeId, // id: rootId, // orgRevisionId: orgRevision.id, }, @@ -7848,10 +7909,70 @@ export class ReportController extends Controller { // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgRoot.id; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild1.orgRootId; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild2.orgRootId; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild3.orgRootId; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild4.orgRootId; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } const orgRootData = await this.orgRootRepository.find({ where: { - id: reqBody.nodeId, + id: nodeId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, @@ -9090,17 +9211,93 @@ export class ReportController extends Controller { ) { // @Get("report4/{rootId}") // async findReport4(@Path() rootId: string) { - const orgRootData = await this.orgRootRepository.findOne({ - where: { id: reqBody.nodeId }, - }); - if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - let rootId:string = reqBody.nodeId; + + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + let rootName:string = ""; + let whereCase: string = ""; + let params: any = {}; + + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id", "orgRootName"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgRoot.orgRootName; + whereCase = "posMaster.orgRootId = :nodeId"; + params = { nodeId }; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgChild1Name"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild1.orgChild1Name; + whereCase = "posMaster.orgChild1Id = :nodeId"; + params = { nodeId }; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgChild2Name"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild2.orgChild2Name; + whereCase = "posMaster.orgChild2Id = :nodeId"; + params = { nodeId }; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgChild3Name"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild3.orgChild3Name; + whereCase = "posMaster.orgChild3Id = :nodeId"; + params = { nodeId }; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgChild4Name"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild4.orgChild4Name; + whereCase = "posMaster.orgChild4Id = :nodeId"; + params = { nodeId }; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + // const orgRootData = await this.orgRootRepository.findOne({ + // where: { id: reqBody.nodeId }, + // }); + // if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // let rootId:string = reqBody.nodeId; const posMaster = await this.posMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("position.posType", "posType") .leftJoinAndSelect("position.posLevel", "posLevel") - .where("posMaster.orgRootId = :rootId", { rootId }) + // .where("posMaster.orgRootId = :rootId", { rootId }) + .where(whereCase, params) .orderBy("posType.posTypeRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); @@ -9199,7 +9396,7 @@ export class ReportController extends Controller { reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), - rootName: orgRootData ? orgRootData.orgRootName : "-", + rootName: rootName ? rootName : "-", data: _reslut, }, });