From c5241b7a63618a7ecc949571ab53177d4404b7fc Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 18 Feb 2026 14:53:50 +0700 Subject: [PATCH] #1542 --- src/controllers/PosMasterActController.ts | 165 ++++++++++++++++------ 1 file changed, 120 insertions(+), 45 deletions(-) diff --git a/src/controllers/PosMasterActController.ts b/src/controllers/PosMasterActController.ts index b6e09e5b..dd4acd1b 100644 --- a/src/controllers/PosMasterActController.ts +++ b/src/controllers/PosMasterActController.ts @@ -109,68 +109,143 @@ export class PosMasterActController extends Controller { isAllRoot?: boolean; page?: number; pageSize?: number; + keyword?: string; }, ) { await new permission().PermissionGet(request, "SYS_ACTING"); - const { - page = 1, - pageSize = 100, - } = body + + const { page = 1, pageSize = 100, keyword } = body; + const posMasterMain = await this.posMasterRepository.findOne({ where: { id: body.posmasterId }, relations: ["posMasterActs"], }); - if (posMasterMain == null) { + + if (!posMasterMain) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - let posId: any = posMasterMain.posMasterActs.map((x) => x.posMasterChildId); + + let posId: any[] = posMasterMain.posMasterActs.map( + (x) => x.posMasterChildId + ); posId.push(body.posmasterId); - let typeCondition: any = {}; + + const query = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") + .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") + .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") + .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") + .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("current_holder.posLevel", "posLevel") + .leftJoinAndSelect("current_holder.posType", "posType") + .where("posMaster.current_holderId IS NOT NULL") + .andWhere("posMaster.id NOT IN (:...posId)", { posId }); + if (!body.isAllRoot) { - if (body.isAll == true) { - typeCondition = { - orgRootId: posMasterMain.orgRootId, - orgChild1Id: posMasterMain.orgChild1Id, - orgChild2Id: posMasterMain.orgChild2Id, - orgChild3Id: posMasterMain.orgChild3Id, - orgChild4Id: posMasterMain.orgChild4Id, - current_holderId: Not(IsNull()), - id: Not(In(posId)), - }; + if (body.isAll) { + if (posMasterMain.orgChild4Id) { + query.andWhere("posMaster.orgChild4Id = :id", { + id: posMasterMain.orgChild4Id, + }); + } else if (posMasterMain.orgChild3Id) { + query.andWhere("posMaster.orgChild3Id = :id", { + id: posMasterMain.orgChild3Id, + }); + } else if (posMasterMain.orgChild2Id) { + query.andWhere("posMaster.orgChild2Id = :id", { + id: posMasterMain.orgChild2Id, + }); + } else if (posMasterMain.orgChild1Id) { + query.andWhere("posMaster.orgChild1Id = :id", { + id: posMasterMain.orgChild1Id, + }); + } else { + query.andWhere("posMaster.orgRootId = :id", { + id: posMasterMain.orgRootId, + }); + } } else { - typeCondition = { - orgRootId: posMasterMain.orgRootId == null ? IsNull() : posMasterMain.orgRootId, - orgChild1Id: posMasterMain.orgChild1Id == null ? IsNull() : posMasterMain.orgChild1Id, - orgChild2Id: posMasterMain.orgChild2Id == null ? IsNull() : posMasterMain.orgChild2Id, - orgChild3Id: posMasterMain.orgChild3Id == null ? IsNull() : posMasterMain.orgChild3Id, - orgChild4Id: posMasterMain.orgChild4Id == null ? IsNull() : posMasterMain.orgChild4Id, - current_holderId: Not(IsNull()), - id: Not(In(posId)), - }; + query + .andWhere( + posMasterMain.orgRootId == null + ? "posMaster.orgRootId IS NULL" + : "posMaster.orgRootId = :orgRootId", + { orgRootId: posMasterMain.orgRootId } + ) + .andWhere( + posMasterMain.orgChild1Id == null + ? "posMaster.orgChild1Id IS NULL" + : "posMaster.orgChild1Id = :orgChild1Id", + { orgChild1Id: posMasterMain.orgChild1Id } + ) + .andWhere( + posMasterMain.orgChild2Id == null + ? "posMaster.orgChild2Id IS NULL" + : "posMaster.orgChild2Id = :orgChild2Id", + { orgChild2Id: posMasterMain.orgChild2Id } + ) + .andWhere( + posMasterMain.orgChild3Id == null + ? "posMaster.orgChild3Id IS NULL" + : "posMaster.orgChild3Id = :orgChild3Id", + { orgChild3Id: posMasterMain.orgChild3Id } + ) + .andWhere( + posMasterMain.orgChild4Id == null + ? "posMaster.orgChild4Id IS NULL" + : "posMaster.orgChild4Id = :orgChild4Id", + { orgChild4Id: posMasterMain.orgChild4Id } + ); } } else { - typeCondition = { + query.andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: posMasterMain.orgRootId, - current_holderId: Not(IsNull()), - id: Not(In(posId)), - }; + }); } - const [posMaster, total] = await this.posMasterRepository.findAndCount({ - where: typeCondition, - relations: [ - "orgRoot", - "orgChild1", - "orgChild2", - "orgChild3", - "orgChild4", - "current_holder", - "current_holder.posLevel", - "current_holder.posType", - ], - skip: (page - 1) * pageSize, - take: pageSize, - }); + if (keyword) { + query.andWhere( + new Brackets((qb) => { + qb.where( + `CONCAT(current_holder.prefix, current_holder.firstName, ' ', current_holder.lastName) LIKE :keyword`, + { keyword: `%${keyword}%` } + ) + .orWhere(`current_holder.citizenId LIKE :keyword`, { + keyword: `%${keyword}%`, + }) + .orWhere( + `CONCAT( + CASE + WHEN orgChild4.id IS NOT NULL THEN orgChild4.orgChild4ShortName + WHEN orgChild3.id IS NOT NULL THEN orgChild3.orgChild3ShortName + WHEN orgChild2.id IS NOT NULL THEN orgChild2.orgChild2ShortName + WHEN orgChild1.id IS NOT NULL THEN orgChild1.orgChild1ShortName + WHEN orgRoot.id IS NOT NULL THEN orgRoot.orgRootShortName + ELSE '' + END, + ' ', + posMaster.posMasterNo + ) LIKE :keyword`, + { keyword: `%${keyword}%` } + ) + .orWhere(`posLevel.posLevelName LIKE :keyword`, { + keyword: `%${keyword}%`, + }) + .orWhere(`posType.posTypeName LIKE :keyword`, { + keyword: `%${keyword}%`, + }) + .orWhere(`current_holder.position LIKE :keyword`, { + keyword: `%${keyword}%`, + }) + }) + ); + } + + query.skip((page - 1) * pageSize).take(pageSize); + + const [posMaster, total] = await query.getManyAndCount(); const data = await Promise.all( posMaster