From f7cf9688702a1ca508fcf0eed42b9adc10183b07 Mon Sep 17 00:00:00 2001 From: kittapath Date: Thu, 31 Oct 2024 20:59:14 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99?= =?UTF-8?q?=E0=B9=88=E0=B8=87=E0=B8=95=E0=B8=B4=E0=B8=94=E0=B9=80=E0=B8=87?= =?UTF-8?q?=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=99=E0=B9=84=E0=B8=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 357 ++++++++++++++++++ src/entities/PosMaster.ts | 14 + ...73-updata_table_posmaster_add_condition.ts | 18 + 3 files changed, 389 insertions(+) create mode 100644 src/migration/1730381986173-updata_table_posmaster_add_condition.ts diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index d063d06a..45f2ec23 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -4403,4 +4403,361 @@ 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(); + } } diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index d32fc598..938067e9 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -202,6 +202,20 @@ export class PosMaster extends EntityBase { }) statusReport: string; + @Column({ + comment: "ตำแหน่งติดเงื่อนไข", + default: false, + }) + isCondition: boolean; + + @Column({ + nullable: true, + comment: "หมายเหตุตำแหน่งติดเงื่อนไข", + type: "text", + default: null, + }) + conditionReason: string; + @ManyToOne(() => AuthRole, (authRole) => authRole.posMasters) @JoinColumn({ name: "authRoleId" }) authRole: AuthRole; diff --git a/src/migration/1730381986173-updata_table_posmaster_add_condition.ts b/src/migration/1730381986173-updata_table_posmaster_add_condition.ts new file mode 100644 index 00000000..a1be9ce8 --- /dev/null +++ b/src/migration/1730381986173-updata_table_posmaster_add_condition.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdataTablePosmasterAddCondition1730381986173 implements MigrationInterface { + name = 'UpdataTablePosmasterAddCondition1730381986173' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`metaStateOperator\` DROP COLUMN \`Column18\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`isCondition\` tinyint NOT NULL COMMENT 'ตำแหน่งติดเงื่อนไข' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`conditionReason\` text NULL COMMENT 'หมายเหตุตำแหน่งติดเงื่อนไข'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`conditionReason\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`isCondition\``); + await queryRunner.query(`ALTER TABLE \`metaStateOperator\` ADD \`Column18\` varchar(50) NULL`); + } + +}