diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 4f7f6626..903fa891 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -328,8 +328,8 @@ export class ImportDataController extends Controller { .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") - // .skip(0) - // .take(1000) + .skip(0) + .take(10000) .getManyAndCount(); // for (var i = 1; i <= total / BATCH_SIZE; i++) { // const profiles = await AppDataSource.getRepository(Profile) @@ -342,6 +342,7 @@ export class ImportDataController extends Controller { await Promise.all( profiles.map(async (_item) => { + console.log(">>>>>>>>>>>>>>>>>>>" + _item.citizenId); const existingProfile = await this.HR_POSITION_OFFICERRepo.find({ where: { CIT: _item.citizenId, FLAG_PERSON_TYPE: "1" }, select: [ diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 68ae5b05..a351cab2 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1671,7 +1671,7 @@ export class ProfileEmployeeTempController extends Controller { } const [profiles, total] = await this.profileRepo .createQueryBuilder("profileEmployee") - .leftJoinAndSelect("profileEmployee.next_holders", "next_holders") + .leftJoinAndSelect("profileEmployee.next_holderTemps", "next_holderTemps") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") .where( @@ -1682,6 +1682,7 @@ export class ProfileEmployeeTempController extends Controller { position: `%${requestBody.position}%`, }, ) + .andWhere("profileEmployee.isLeave IS FALSE") .andWhere( new Brackets((qb) => { qb.where( @@ -1738,17 +1739,17 @@ export class ProfileEmployeeTempController extends Controller { new Brackets((qb) => { qb.where("profileEmployee.id NOT IN (:...ids)", { ids: - orgRevision.employeePosMasters + orgRevision.employeeTempPosMasters .filter((x) => x.next_holderId != null) .map((x) => x.next_holderId).length == 0 ? ["zxc"] - : orgRevision.employeePosMasters + : orgRevision.employeeTempPosMasters .filter((x) => x.next_holderId != null) .map((x) => x.next_holderId), }); }), ) - .andWhere("profileEmployee.employeeClass = :employeeClass", { employeeClass: "PERM" }) + .andWhere("profileEmployee.employeeClass = :employeeClass", { employeeClass: "TEMP" }) .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 0a0776c0..e9eeee3f 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -30,6 +30,7 @@ import HttpStatus from "../interfaces/http-status"; import { Profile } from "../entities/Profile"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; +import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; @Route("api/v1/org/report") @Tags("Report") @Security("bearerAuth") @@ -49,6 +50,7 @@ export class ReportController extends Controller { private posMasterRepository = AppDataSource.getRepository(PosMaster); private profileRepository = AppDataSource.getRepository(Profile); private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + private empTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); /** * API รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ @@ -85,12 +87,12 @@ export class ReportController extends Controller { // }); // const rawdataProfile = await this.posMasterRepository // .createQueryBuilder('posMaster') - // .leftJoinAndSelect('posMaster.current_holder', 'current_holder') - // .leftJoinAndSelect('posMaster.positions', 'positions') - // .leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') - // .leftJoinAndSelect('positions.posExecutive', 'posExecutive') - // .leftJoinAndSelect('current_holder.posType', 'posType') - // .leftJoinAndSelect('current_holder.posLevel', 'posLevel') + // .leftJoinAndSelect('posMaster.current_holder', 'current_holder') + // .leftJoinAndSelect('posMaster.positions', 'positions') + // .leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') + // .leftJoinAndSelect('positions.posExecutive', 'posExecutive') + // .leftJoinAndSelect('current_holder.posType', 'posType') + // .leftJoinAndSelect('current_holder.posLevel', 'posLevel') // .leftJoinAndSelect('current_holder.profileEducations', 'profileEducations') // .where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id }) // .andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId }) @@ -187,7 +189,7 @@ export class ReportController extends Controller { @Query() isRetire?: boolean, @Query() retireType?: string, @Query() sortBy: string = "posMasterNo", - @Query() sort: "ASC"|"DESC" = "ASC", + @Query() sort: "ASC" | "DESC" = "ASC", ) { if (ageMin && (ageMin < 18 || ageMin > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60"); @@ -195,7 +197,7 @@ export class ReportController extends Controller { if (ageMax && (ageMax < 18 || ageMax > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60"); } - if (ageMin && ageMax && (ageMin > ageMax)) { + if (ageMin && ageMax && ageMin > ageMax) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); } ageMin = ageMin ?? 18; @@ -204,22 +206,19 @@ export class ReportController extends Controller { let nodeCondition = "1=1"; if (node === 0 && nodeId) { nodeCondition = "registryOfficer.orgRootId = :nodeId"; - } - else if (node === 1 && nodeId) { + } else if (node === 1 && nodeId) { nodeCondition = "registryOfficer.orgChild1Id = :nodeId"; - } - else if (node === 2 && nodeId) { + } else if (node === 2 && nodeId) { nodeCondition = "registryOfficer.orgChild2Id = :nodeId"; - } - else if (node === 3 && nodeId) { + } else if (node === 3 && nodeId) { nodeCondition = "registryOfficer.orgChild3Id = :nodeId"; - } - else if (node === 4 && nodeId) { + } else if (node === 4 && nodeId) { nodeCondition = "registryOfficer.orgChild4Id = :nodeId"; } let dateAppointCondition = "1=1"; if (startDateAppoint && endDateAppoint) { - dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint AND DATE(registryOfficer.dateAppoint) <= :endDateAppoint"; + dateAppointCondition = + "DATE(registryOfficer.dateAppoint) >= :startDateAppoint AND DATE(registryOfficer.dateAppoint) <= :endDateAppoint"; } else if (startDateAppoint) { dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint"; } else if (endDateAppoint) { @@ -236,15 +235,16 @@ export class ReportController extends Controller { const [lists, total] = await AppDataSource.getRepository(viewRegistryOfficer) .createQueryBuilder("registryOfficer") - .where(nodeCondition, { - nodeId: nodeId + .where(nodeCondition, { + nodeId: nodeId, }) - .andWhere("registryOfficer.age BETWEEN :ageMin AND :ageMax", { - ageMin, ageMax + .andWhere("registryOfficer.age BETWEEN :ageMin AND :ageMax", { + ageMin, + ageMax, }) - .andWhere(dateAppointCondition, { - startDateAppoint: startDateAppoint?.toISOString().split("T")[0], - endDateAppoint: endDateAppoint?.toISOString().split("T")[0] + .andWhere(dateAppointCondition, { + startDateAppoint: startDateAppoint?.toISOString().split("T")[0], + endDateAppoint: endDateAppoint?.toISOString().split("T")[0], }) .andWhere("registryOfficer.isProbation = :isProbation", { isProbation: isProbation, @@ -259,7 +259,7 @@ export class ReportController extends Controller { { posTypeName: `%${posTypeName}%`, }, - ) + ); qb.orWhere( posLevelName != null && posLevelName != "" ? "registryOfficer.posLevelName LIKE :posLevelName" @@ -267,15 +267,13 @@ export class ReportController extends Controller { { posLevelName: `%${posLevelName}%`, }, - ) + ); qb.orWhere( - position != null && position != "" - ? "registryOfficer.position LIKE :position" - : "1=1", + position != null && position != "" ? "registryOfficer.position LIKE :position" : "1=1", { position: `%${position}%`, }, - ) + ); qb.orWhere( posExecutiveName != null && posExecutiveName != "" ? "registryOfficer.posExecutiveName LIKE :posExecutiveName" @@ -283,15 +281,13 @@ export class ReportController extends Controller { { posExecutiveName: `%${posExecutiveName}%`, }, - ) + ); qb.orWhere( - gender != null && gender != "" - ? "registryOfficer.gender LIKE :gender" - : "1=1", + gender != null && gender != "" ? "registryOfficer.gender LIKE :gender" : "1=1", { gender: `%${gender}%`, }, - ) + ); qb.orWhere( relationship != null && relationship != "" ? "registryOfficer.relationship LIKE :relationship" @@ -299,20 +295,18 @@ export class ReportController extends Controller { { relationship: `%${relationship}%`, }, - ) + ); qb.orWhere( - degree != null && degree != "" - ? "registryOfficer.degree LIKE :degree" - : "1=1", + degree != null && degree != "" ? "registryOfficer.degree LIKE :degree" : "1=1", { degree: `%${degree}%`, }, - ) + ); }), ) .orderBy(`registryOfficer.${sortBy}`, sort) .getManyAndCount(); - const mapData = lists.map(x => ({ + const mapData = lists.map((x) => ({ profileId: x.profileId, citizenId: x.citizenId, prefix: x.prefix, @@ -350,7 +344,7 @@ export class ReportController extends Controller { })); return new HttpSuccess({ data: mapData, - total: total + total: total, }); } @@ -434,7 +428,7 @@ export class ReportController extends Controller { // }); // const groupedData = mapData.reduce((acc: any, item) => { - // const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel} + // const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel} // `; // if (!acc[key]) { // acc[key] = { @@ -489,7 +483,7 @@ export class ReportController extends Controller { @Query() ageMin?: number, @Query() ageMax?: number, @Query() sortBy: string = "posMasterNo", - @Query() sort: "ASC"|"DESC" = "ASC", + @Query() sort: "ASC" | "DESC" = "ASC", ) { if (ageMin && (ageMin < 18 || ageMin > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60"); @@ -497,7 +491,7 @@ export class ReportController extends Controller { if (ageMax && (ageMax < 18 || ageMax > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60"); } - if (ageMin && ageMax && (ageMin > ageMax)) { + if (ageMin && ageMax && ageMin > ageMax) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); } ageMin = ageMin ?? 18; @@ -506,22 +500,19 @@ export class ReportController extends Controller { let nodeCondition = "1=1"; if (node === 0 && nodeId) { nodeCondition = "registryEmployee.orgRootId = :nodeId"; - } - else if (node === 1 && nodeId) { + } else if (node === 1 && nodeId) { nodeCondition = "registryEmployee.orgChild1Id = :nodeId"; - } - else if (node === 2 && nodeId) { + } else if (node === 2 && nodeId) { nodeCondition = "registryEmployee.orgChild2Id = :nodeId"; - } - else if (node === 3 && nodeId) { + } else if (node === 3 && nodeId) { nodeCondition = "registryEmployee.orgChild3Id = :nodeId"; - } - else if (node === 4 && nodeId) { + } else if (node === 4 && nodeId) { nodeCondition = "registryEmployee.orgChild4Id = :nodeId"; } let dateAppointCondition = "1=1"; if (startDateAppoint && endDateAppoint) { - dateAppointCondition = "DATE(registryEmployee.dateAppoint) >= :startDateAppoint AND DATE(registryEmployee.dateAppoint) <= :endDateAppoint"; + dateAppointCondition = + "DATE(registryEmployee.dateAppoint) >= :startDateAppoint AND DATE(registryEmployee.dateAppoint) <= :endDateAppoint"; } else if (startDateAppoint) { dateAppointCondition = "DATE(registryEmployee.dateAppoint) >= :startDateAppoint"; } else if (endDateAppoint) { @@ -537,15 +528,16 @@ export class ReportController extends Controller { } const [lists, total] = await AppDataSource.getRepository(viewRegistryEmployee) .createQueryBuilder("registryEmployee") - .where(nodeCondition, { - nodeId: nodeId + .where(nodeCondition, { + nodeId: nodeId, }) - .andWhere("registryEmployee.age BETWEEN :ageMin AND :ageMax", { - ageMin, ageMax + .andWhere("registryEmployee.age BETWEEN :ageMin AND :ageMax", { + ageMin, + ageMax, }) - .andWhere(dateAppointCondition, { - startDateAppoint: startDateAppoint?.toISOString().split("T")[0], - endDateAppoint: endDateAppoint?.toISOString().split("T")[0] + .andWhere(dateAppointCondition, { + startDateAppoint: startDateAppoint?.toISOString().split("T")[0], + endDateAppoint: endDateAppoint?.toISOString().split("T")[0], }) .andWhere("registryEmployee.isProbation = :isProbation", { isProbation: isProbation, @@ -561,7 +553,7 @@ export class ReportController extends Controller { { posTypeName: `%${posTypeName}%`, }, - ) + ); qb.orWhere( posLevelName != null && posLevelName != "" ? "registryEmployee.posLevelName LIKE :posLevelName" @@ -569,23 +561,19 @@ export class ReportController extends Controller { { posLevelName: `%${posLevelName}%`, }, - ) + ); qb.orWhere( - position != null && position != "" - ? "registryEmployee.position LIKE :position" - : "1=1", + position != null && position != "" ? "registryEmployee.position LIKE :position" : "1=1", { position: `%${position}%`, }, - ) + ); qb.orWhere( - gender != null && gender != "" - ? "registryEmployee.gender LIKE :gender" - : "1=1", + gender != null && gender != "" ? "registryEmployee.gender LIKE :gender" : "1=1", { gender: `%${gender}%`, }, - ) + ); qb.orWhere( relationship != null && relationship != "" ? "registryEmployee.relationship LIKE :relationship" @@ -593,20 +581,18 @@ export class ReportController extends Controller { { relationship: `%${relationship}%`, }, - ) + ); qb.orWhere( - degree != null && degree != "" - ? "registryEmployee.degree LIKE :degree" - : "1=1", + degree != null && degree != "" ? "registryEmployee.degree LIKE :degree" : "1=1", { degree: `%${degree}%`, }, - ) + ); }), ) .orderBy(`registryEmployee.${sortBy}`, sort) .getManyAndCount(); - const mapData = lists.map(x => ({ + const mapData = lists.map((x) => ({ profileId: x.profileEmployeeId, citizenId: x.citizenId, prefix: x.prefix, @@ -643,7 +629,7 @@ export class ReportController extends Controller { })); return new HttpSuccess({ data: mapData, - total: total + total: total, }); } @@ -7091,4 +7077,122 @@ export class ReportController extends Controller { }, }); } + + @Get("report4-employee-temp/{rootId}") + async findReportEmp4Temp(@Path() rootId: string) { + const orgRootData = await this.orgRootRepository.findOne({ + where: { id: rootId }, + }); + if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + + const posMaster = await this.empTempPosMasterRepository + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.positions", "position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .where("posMaster.orgRootId = :rootId", { rootId }) + .orderBy("posType.posTypeRank", "ASC") + .addOrderBy("posLevel.posLevelRank", "ASC") + .getMany(); + + const _posMaster = posMaster.map((x) => ({ + type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), + typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), + level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), + levelRank: [ + ...new Set( + x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`), + ), + ].join(""), + positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""), + })); + + const groupedData = _posMaster.reduce((acc: any, curr: any) => { + const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; + if (!acc[key]) { + acc[key] = { ...curr, total: 1 }; + } else { + acc[key].total += 1; + } + return acc; + }, {}); + + let result = Object.values(groupedData) + .map((x: any) => ({ + type: x.type, + typeRank: parseInt(x.typeRank), + level: x.level, + levelRank: parseInt(x.levelRank), + + total: x.total, + remark: x.positions, + })) + .sort((x, y) => { + if (x.typeRank !== y.typeRank) { + return x.typeRank - y.typeRank; + } + return x.levelRank - y.levelRank; + }); + let tmpType: string = ""; + let allTotal: number = 0; + let total: number = 0; + let _total: number = 0; + let _reslut = new Array(); + + result.forEach((x: any, idx: number) => { + allTotal += x.total; + total += x.total; + if (x.type === tmpType) { + _reslut.push({ + ...x, + type: "", + }); + } else { + if (x.type !== tmpType && tmpType != "") { + _total = total - x.total; + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: _total, + remark: "", + }); + total = x.total; + _total = 0; + } + _reslut.push({ + ...x, + }); + } + tmpType = x.type; + }); + + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: total, + remark: "", + }); + _reslut.push({ + type: "", + typeRank: "", + level: "รวมทั้งสิ้น", + levelRank: "", + total: allTotal, + remark: "", + }); + + return new HttpSuccess({ + template: "report4", + reportName: "report4", + data: { + dateCurrent: Extension.ToThaiShortDate(new Date()), + rootName: orgRootData ? orgRootData.orgRootName : "-", + data: _reslut, + }, + }); + } }