From e8920ede29c42cb51dd735fc9a8523f2a5b2dde4 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 1 Nov 2024 10:43:41 +0700 Subject: [PATCH] fix search --- src/controllers/PositionController.ts | 379 ++------------------------ 1 file changed, 17 insertions(+), 362 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 45f2ec23..05888df9 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1700,12 +1700,10 @@ export class PositionController extends Controller { ...checkChildConditions, ...typeCondition, ...revisionCondition, - ...(body.keyword && - (masterId.length > 0 - ? { id: In(masterId) } - : { posMasterNo: Like(`%${body.keyword}%`) })), + ...(masterId.length > 0 ? { id: In(masterId) } : {}), }, ]; + let [posMaster, total] = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") @@ -1812,6 +1810,20 @@ export class PositionController extends Controller { .andWhere(typeCondition) .andWhere(revisionCondition); }), + ) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? body.isAll == false + ? searchShortName + : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` + : "1=1", + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), ); }), ) @@ -3199,7 +3211,7 @@ export class PositionController extends Controller { ) { let typeCondition: any = {}; let conditionA = - "posType.posTypeName LIKE :posType AND posLevel.posLevelName LIKE :posLevel AND positions.positionName LIKE :position"; + "positions.posTypeId LIKE :posType AND positions.posLevelId LIKE :posLevel AND positions.positionName LIKE :position"; let posType = await this.posTypeRepository.findOne({ where: { id: String(body.posType) }, @@ -4403,361 +4415,4 @@ export class PositionController extends Controller { return new HttpSuccess(_posMaster); } - - /** - * API รายการอัตรากำลัง - * - * @summary ORG_070 - รายการอัตรากำลัง (ADMIN) #56 - * - */ - @Post("master/position-condition") - async listญositionCondition( - @Request() request: RequestWithUser, - @Body() - body: { - id: string; - revisionId: string; - type: number; - isAll: boolean; - page: number; - pageSize: number; - keyword?: string; - }, - ) { - let typeCondition: any = {}; - let checkChildConditions: any = {}; - let keywordAsInt: any; - let searchShortName = ""; - let searchShortName0 = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName1 = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName2 = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName3 = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName4 = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let _data = await new permission().PermissionOrgList(request, "SYS_POS_CONDITION"); - if (body.type === 0) { - typeCondition = { - orgRootId: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild1Id: IsNull(), - }; - searchShortName = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 1) { - typeCondition = { - orgChild1Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild2Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 2) { - typeCondition = { - orgChild2Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild3Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 3) { - typeCondition = { - orgChild3Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild4Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 4) { - typeCondition = { - orgChild4Id: body.id, - }; - searchShortName = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } - let findPosition: any; - let masterId = new Array(); - if (body.keyword != null && body.keyword != "") { - const findTypes: PosType[] = await this.posTypeRepository.find({ - where: { posTypeName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posTypeId: In(findTypes.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - const findLevel: PosLevel[] = await this.posLevelRepository.find({ - where: { posLevelName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posLevelId: In(findLevel.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ - where: { posExecutiveName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - findPosition = await this.positionRepository.find({ - where: { positionName: Like(`%${body.keyword}%`) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); - if (isNaN(keywordAsInt)) { - keywordAsInt = "P@ssw0rd!z"; - } - masterId = [...new Set(masterId)]; - } - - const revisionCondition = { - orgRevisionId: body.revisionId, - }; - const chkRevision = await this.orgRevisionRepository.findOne({ - where: { id: body.revisionId }, - }); - if (chkRevision != null && chkRevision.orgRevisionIsDraft == true) - _data = { - root: null, - child1: null, - child2: null, - child3: null, - child4: null, - privilege: "OWNER", - }; - const conditions = [ - { - ...checkChildConditions, - ...typeCondition, - ...revisionCondition, - ...(body.keyword && - (masterId.length > 0 - ? { id: In(masterId) } - : { posMasterNo: Like(`%${body.keyword}%`) })), - }, - ]; - let [posMaster, total] = 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") - .where(conditions) - .andWhere({ - current_holderId: Not(IsNull()), - }) - .andWhere( - _data.root != undefined && _data.root != null - ? _data.root[0] != null - ? `posMaster.orgRootId IN (:...root)` - : `posMaster.orgRootId is null` - : "1=1", - { - root: _data.root, - }, - ) - .andWhere( - _data.child1 != undefined && _data.child1 != null - ? _data.child1[0] != null - ? `posMaster.orgChild1Id IN (:...child1)` - : `posMaster.orgChild1Id is null` - : "1=1", - { - child1: _data.child1, - }, - ) - .andWhere( - _data.child2 != undefined && _data.child2 != null - ? _data.child2[0] != null - ? `posMaster.orgChild2Id IN (:...child2)` - : `posMaster.orgChild2Id is null` - : "1=1", - { - child2: _data.child2, - }, - ) - .andWhere( - _data.child3 != undefined && _data.child3 != null - ? _data.child3[0] != null - ? `posMaster.orgChild3Id IN (:...child3)` - : `posMaster.orgChild3Id is null` - : "1=1", - { - child3: _data.child3, - }, - ) - .andWhere( - _data.child4 != undefined && _data.child4 != null - ? _data.child4[0] != null - ? `posMaster.orgChild4Id IN (:...child4)` - : `posMaster.orgChild4Id is null` - : "1=1", - { - child4: _data.child4, - }, - ) - .andWhere( - body.keyword != null && body.keyword != "" - ? body.isAll == false - ? searchShortName - : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` - : "1=1", - ) - .orderBy("posMaster.posMasterOrder", "ASC") - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); - - //แก้ค้นหา - let _position: any[] = []; - let x: any = null; - let y: any = null; - if (body.keyword != null && body.keyword != "") { - const position = await this.positionRepository.find({ - relations: ["posType", "posLevel", "posExecutive"], - where: { posMasterId: In(posMaster.map((x) => x.id)) }, - order: { createdAt: "ASC" }, - }); - for (let data of position) { - x = data.posMasterId; - if (y != x) { - if ( - data.positionName.includes(body.keyword) || - data.posType.posTypeName.includes(body.keyword) || - data.posLevel.posLevelName.includes(body.keyword) - ) { - _position.push(data); - } - } - y = x; - } - } - - if (_position.length > 0) { - posMaster = posMaster.filter((x) => _position.some((y) => y.posMasterId === x.id)); - } - - const formattedData = await Promise.all( - posMaster.map(async (posMaster) => { - const positions = await this.positionRepository.find({ - where: { - posMasterId: posMaster.id, - }, - relations: ["posLevel", "posType", "posExecutive"], - order: { - createdAt: "ASC", - }, - }); - let shortName = ""; - - if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id == null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null - ) { - shortName = posMaster.orgRoot.orgRootShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null - ) { - shortName = posMaster.orgChild1.orgChild1ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id == null - ) { - shortName = posMaster.orgChild2.orgChild2ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id !== null - ) { - shortName = posMaster.orgChild3.orgChild3ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id !== null - ) { - shortName = posMaster.orgChild4.orgChild4ShortName; - } - - return { - id: posMaster.id, - isCondition: posMaster.isCondition, - conditionReason: posMaster.conditionReason, - orgShortname: shortName, - profilePosition: positions[0]?.positionName, - profilePostype: positions[0]?.posType?.posTypeName ?? null, - profilePoslevel: positions[0]?.posLevel?.posLevelName ?? null, - positions: positions.map((position: any) => ({ - id: position.id, - positionName: position.positionName, - positionField: position.positionField, - posTypeName: position.posType == null ? null : position.posType.posTypeName, - posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - posExecutiveName: - position.posExecutive == null ? null : position.posExecutive.posExecutiveName, - positionExecutiveField: position.positionExecutiveField, - positionArea: position.positionArea, - })), - }; - }), - ); - return new HttpSuccess({ data: formattedData, total }); - } - - /** - * API แก้ไขตำแหน่งเงื่อนไข - * - * @summary แก้ไขตำแหน่งเงื่อนไข (ADMIN) - * - */ - @Put("master/position-condition/{id}") - async updatePositionCondition( - @Path() id: string, - @Body() - requestBody: { - isCondition: boolean | null; - conditionReason: string | null; - }, - @Request() request: RequestWithUser, - ) { - await new permission().PermissionUpdate(request, "SYS_POS_CONDITION"); - const posMaster = await this.posMasterRepository.findOne({ - where: { id: id }, - }); - if (!posMaster) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } - - Object.assign(posMaster, requestBody); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - return new HttpSuccess(); - } }