diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index c937c84e..9e8063a9 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, }, @@ -3501,8 +3562,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3535,6 +3597,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3567,6 +3632,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3599,6 +3667,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3632,6 +3703,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3765,6 +3839,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -3780,7 +3870,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } - + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -3858,7 +3963,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4139,6 +4246,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4154,6 +4277,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -4232,7 +4371,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4521,6 +4662,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4536,6 +4693,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -4614,7 +4787,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4919,6 +5094,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4934,6 +5125,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -5012,7 +5219,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -5321,6 +5530,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -5336,6 +5561,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -5414,7 +5655,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -5632,11 +5875,11 @@ export class ReportController extends Controller { }), ); _node = null; - } - } - } - } - } + } // End for orgChild4 + } // End for orgChild3 + } // End for orgChild2 + } // End for orgChild1 + } // End for orgRoot // const metaData = { template: "report2", reportName: "report2", data: { data } }; // sendWebSocket(metaData); @@ -7848,10 +8091,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 +9393,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 +9578,7 @@ export class ReportController extends Controller { reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), - rootName: orgRootData ? orgRootData.orgRootName : "-", + rootName: rootName ? rootName : "-", data: _reslut, }, });