รายงานทะเบียนประวัติ
This commit is contained in:
parent
d0732a87e3
commit
13a7ccc997
6 changed files with 698 additions and 192 deletions
|
|
@ -13,7 +13,7 @@ import { AppDataSource } from "../database/data-source";
|
|||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { In, LessThan } from "typeorm";
|
||||
import { In, LessThan, Brackets } from "typeorm";
|
||||
import { OrgRevision } from "../entities/OrgRevision";
|
||||
import { OrgRoot } from "../entities/OrgRoot";
|
||||
import { OrgChild1 } from "../entities/OrgChild1";
|
||||
|
|
@ -28,6 +28,8 @@ import Extension from "../interfaces/extension";
|
|||
import { LeaveType } from "../entities/LeaveType";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { Profile } from "../entities/Profile";
|
||||
import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer";
|
||||
import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee";
|
||||
@Route("api/v1/org/report")
|
||||
@Tags("Report")
|
||||
@Security("bearerAuth")
|
||||
|
|
@ -47,238 +49,514 @@ export class ReportController extends Controller {
|
|||
private posMasterRepository = AppDataSource.getRepository(PosMaster);
|
||||
private profileRepository = AppDataSource.getRepository(Profile);
|
||||
private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster);
|
||||
|
||||
/**
|
||||
* API รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ
|
||||
*
|
||||
* @summary รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ
|
||||
*
|
||||
*/
|
||||
// @Get("registry-officer")
|
||||
// async registryOfficer(
|
||||
// @Query() rootId?: string,
|
||||
// @Query() year?: number,
|
||||
// @Query() ageMin?: number,
|
||||
// @Query() ageMax?: number,
|
||||
// ) {
|
||||
// if (ageMin && (ageMin < 18 || ageMin > 60)) {
|
||||
// throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60");
|
||||
// }
|
||||
|
||||
// if (ageMax && (ageMax < 18 || ageMax > 60)) {
|
||||
// throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60");
|
||||
// }
|
||||
|
||||
// const minAge = ageMin ?? 18;
|
||||
// const maxAge = ageMax ?? 60;
|
||||
|
||||
// if (minAge > maxAge) {
|
||||
// throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
|
||||
// }
|
||||
// const yearInAD = year ? year : null;
|
||||
// const currentRevision = await this.orgRevisionRepository.findOne({
|
||||
// where: {
|
||||
// orgRevisionIsCurrent: true,
|
||||
// },
|
||||
// });
|
||||
// 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('current_holder.profileEducations', 'profileEducations')
|
||||
// .where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id })
|
||||
// .andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId })
|
||||
// .andWhere('posMaster.current_holderId Is Not Null')
|
||||
// .andWhere('positions.positionIsSelected = :positionIsSelected', { positionIsSelected: true })
|
||||
// .andWhere( yearInAD && yearInAD != null? 'YEAR(current_holder.dateAppoint) = :year': "1=1", { year: yearInAD }) //ตอนนี้ where ที่วันที่บรรจุ (รอ prove) ว่าจะ where ที่ไหน
|
||||
// .andWhere(`
|
||||
// TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
|
||||
// AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
|
||||
// `,
|
||||
// { minAge, maxAge },
|
||||
// )
|
||||
// .orderBy("posType.posTypeName", "ASC")
|
||||
// .getMany();
|
||||
// if (!rawdataProfile) {
|
||||
// throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
||||
// }
|
||||
// const mapData = rawdataProfile.map((x) => {
|
||||
// const latestEducation = x.current_holder.profileEducations.sort(
|
||||
// (a: any, b: any) => b.endDate - a.startDate,
|
||||
// )[0];
|
||||
// return {
|
||||
// name: x.current_holder.firstName + " " + x.current_holder.lastName,
|
||||
// affiliation: x.orgRoot.orgRootName ?? "-",
|
||||
// gender: x.current_holder.gender ?? "-",
|
||||
// positionName: x.positions[0] ? x.positions[0].positionName : "-",
|
||||
// status: x.current_holder.relationship ?? "-",
|
||||
// posType: x.current_holder.posType.posTypeName ?? "-",
|
||||
// posLevel: x.current_holder.posLevel.posLevelName ?? "-",
|
||||
// degree: latestEducation ? latestEducation.educationLevel : "-",
|
||||
// posExecutive: x.positions[0].posExecutive
|
||||
// ? x.positions[0].posExecutive.posExecutiveName
|
||||
// : "-",
|
||||
// currentPreiodPos: "-",
|
||||
// levelPeriodPos: "-",
|
||||
// };
|
||||
// });
|
||||
|
||||
// const groupedData = mapData.reduce((acc: any, item) => {
|
||||
// const key = `${item.posType} - ${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.positionName} - ${item.posLevel} - ${item.posExecutive || "ไม่พบข้อมูล"} `;
|
||||
// if (!acc[key]) {
|
||||
// acc[key] = {
|
||||
// posType: item.posType && item.posType != "" ? item.posType : "-",
|
||||
// affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
|
||||
// gender: item.gender && item.gender != "" ? item.gender : "-",
|
||||
// degree: item.degree && item.degree != "" ? item.degree : "-",
|
||||
// status: item.status && item.status != "" ? item.status : "-",
|
||||
// positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
|
||||
// posLevel: item.posLevel && item.posLevel != "" ? item.posLevel : "-",
|
||||
// posExecutive: item.posExecutive && item.posExecutive != "" ? item.posExecutive : "-",
|
||||
// currentPreiodPos: "-",
|
||||
// levelPeriodPos: "-",
|
||||
// count: 0,
|
||||
// };
|
||||
// }
|
||||
// acc[key].count++;
|
||||
|
||||
// return acc;
|
||||
// }, {});
|
||||
|
||||
// const result = Object.values(groupedData).map((item: any) => ({
|
||||
// ...item,
|
||||
// count: Extension.ToThaiNumber(item.count.toString()),
|
||||
// }));
|
||||
// return new HttpSuccess({
|
||||
// template: "registry-officer",
|
||||
// reportName: "xlsx-report",
|
||||
// data: {
|
||||
// year: year
|
||||
// ? Extension.ToThaiNumber((year + 543).toString())
|
||||
// : Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
|
||||
// date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
// list: result,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
@Get("registry-officer")
|
||||
async registryOfficer(
|
||||
@Query() rootId?: string,
|
||||
@Query() year?: number,
|
||||
@Query() node?: number,
|
||||
@Query() nodeId?: string,
|
||||
@Query() posTypeName?: string,
|
||||
@Query() posLevelName?: string,
|
||||
@Query() position?: string,
|
||||
@Query() posExecutiveName?: string,
|
||||
@Query() gender?: string,
|
||||
@Query() relationship?: string,
|
||||
@Query() degree?: string,
|
||||
@Query() startDateAppoint?: Date,
|
||||
@Query() endDateAppoint?: Date,
|
||||
@Query() ageMin?: number,
|
||||
@Query() ageMax?: number,
|
||||
@Query() sortBy: string = "posMasterNo",
|
||||
@Query() sort: "ASC"|"DESC" = "ASC",
|
||||
) {
|
||||
if (ageMin && (ageMin < 18 || ageMin > 60)) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60");
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60");
|
||||
}
|
||||
|
||||
if (ageMax && (ageMax < 18 || ageMax > 60)) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60");
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60");
|
||||
}
|
||||
|
||||
const minAge = ageMin ?? 18;
|
||||
const maxAge = ageMax ?? 60;
|
||||
|
||||
if (minAge > maxAge) {
|
||||
if (ageMin && ageMax && (ageMin > ageMax)) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
|
||||
}
|
||||
const yearInAD = year ? year : null;
|
||||
const currentRevision = await this.orgRevisionRepository.findOne({
|
||||
where: {
|
||||
orgRevisionIsCurrent: true,
|
||||
},
|
||||
});
|
||||
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('current_holder.profileEducations', 'profileEducations')
|
||||
.where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id })
|
||||
.andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId })
|
||||
.andWhere('posMaster.current_holderId Is Not Null')
|
||||
.andWhere('positions.positionIsSelected = :positionIsSelected', { positionIsSelected: true })
|
||||
.andWhere( yearInAD && yearInAD != null? 'YEAR(current_holder.dateAppoint) = :year': "1=1", { year: yearInAD }) //ตอนนี้ where ที่วันที่บรรจุ (รอ prove) ว่าจะ where ที่ไหน
|
||||
.andWhere(`
|
||||
TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
|
||||
AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
|
||||
`,
|
||||
{ minAge, maxAge },
|
||||
)
|
||||
.orderBy("posType.posTypeName", "ASC")
|
||||
.getMany();
|
||||
if (!rawdataProfile) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
||||
ageMin = ageMin ?? 18;
|
||||
ageMax = ageMax ?? 60;
|
||||
|
||||
let nodeCondition = "1=1";
|
||||
if (node === 0 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgRootId = :nodeId";
|
||||
}
|
||||
else if (node === 1 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild1Id = :nodeId";
|
||||
}
|
||||
else if (node === 2 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild2Id = :nodeId";
|
||||
}
|
||||
const mapData = rawdataProfile.map((x) => {
|
||||
const latestEducation = x.current_holder.profileEducations.sort(
|
||||
(a: any, b: any) => b.endDate - a.startDate,
|
||||
)[0];
|
||||
return {
|
||||
name: x.current_holder.firstName + " " + x.current_holder.lastName,
|
||||
affiliation: x.orgRoot.orgRootName ?? "-",
|
||||
gender: x.current_holder.gender ?? "-",
|
||||
positionName: x.positions[0] ? x.positions[0].positionName : "-",
|
||||
status: x.current_holder.relationship ?? "-",
|
||||
posType: x.current_holder.posType.posTypeName ?? "-",
|
||||
posLevel: x.current_holder.posLevel.posLevelName ?? "-",
|
||||
degree: latestEducation ? latestEducation.educationLevel : "-",
|
||||
posExecutive: x.positions[0].posExecutive
|
||||
? x.positions[0].posExecutive.posExecutiveName
|
||||
: "-",
|
||||
currentPreiodPos: "-",
|
||||
levelPeriodPos: "-",
|
||||
};
|
||||
});
|
||||
else if (node === 3 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild3Id = :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";
|
||||
} else if (startDateAppoint) {
|
||||
dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint";
|
||||
} else if (endDateAppoint) {
|
||||
dateAppointCondition = "DATE(registryOfficer.dateAppoint) <= :endDateAppoint";
|
||||
}
|
||||
const [lists, total] = await AppDataSource.getRepository(viewRegistryOfficer)
|
||||
.createQueryBuilder("registryOfficer")
|
||||
.where(nodeCondition, {
|
||||
nodeId: nodeId
|
||||
})
|
||||
.andWhere("registryOfficer.age BETWEEN :ageMin AND :ageMax", {
|
||||
ageMin, ageMax
|
||||
})
|
||||
.andWhere(dateAppointCondition, {
|
||||
startDateAppoint: startDateAppoint?.toISOString().split("T")[0],
|
||||
endDateAppoint: endDateAppoint?.toISOString().split("T")[0]
|
||||
})
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.orWhere(
|
||||
posTypeName != null && posTypeName != ""
|
||||
? "registryOfficer.posTypeName LIKE :posTypeName"
|
||||
: "1=1",
|
||||
{
|
||||
posTypeName: `%${posTypeName}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
posLevelName != null && posLevelName != ""
|
||||
? "registryOfficer.posLevelName LIKE :posLevelName"
|
||||
: "1=1",
|
||||
{
|
||||
posLevelName: `%${posLevelName}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
position != null && position != ""
|
||||
? "registryOfficer.position LIKE :position"
|
||||
: "1=1",
|
||||
{
|
||||
position: `%${position}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
posExecutiveName != null && posExecutiveName != ""
|
||||
? "registryOfficer.posExecutiveName LIKE :posExecutiveName"
|
||||
: "1=1",
|
||||
{
|
||||
posExecutiveName: `%${posExecutiveName}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
gender != null && gender != ""
|
||||
? "registryOfficer.gender LIKE :gender"
|
||||
: "1=1",
|
||||
{
|
||||
gender: `%${gender}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
relationship != null && relationship != ""
|
||||
? "registryOfficer.relationship LIKE :relationship"
|
||||
: "1=1",
|
||||
{
|
||||
relationship: `%${relationship}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
degree != null && degree != ""
|
||||
? "registryOfficer.degree LIKE :degree"
|
||||
: "1=1",
|
||||
{
|
||||
degree: `%${degree}%`,
|
||||
},
|
||||
)
|
||||
}),
|
||||
)
|
||||
.orderBy(`registryOfficer.${sortBy}`, sort)
|
||||
.getManyAndCount();
|
||||
|
||||
const groupedData = mapData.reduce((acc: any, item) => {
|
||||
const key = `${item.posType} - ${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.positionName} - ${item.posLevel} - ${item.posExecutive || "ไม่พบข้อมูล"} `;
|
||||
if (!acc[key]) {
|
||||
acc[key] = {
|
||||
posType: item.posType && item.posType != "" ? item.posType : "-",
|
||||
affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
|
||||
gender: item.gender && item.gender != "" ? item.gender : "-",
|
||||
degree: item.degree && item.degree != "" ? item.degree : "-",
|
||||
status: item.status && item.status != "" ? item.status : "-",
|
||||
positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
|
||||
posLevel: item.posLevel && item.posLevel != "" ? item.posLevel : "-",
|
||||
posExecutive: item.posExecutive && item.posExecutive != "" ? item.posExecutive : "-",
|
||||
currentPreiodPos: "-",
|
||||
levelPeriodPos: "-",
|
||||
count: 0,
|
||||
};
|
||||
}
|
||||
acc[key].count++;
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const result = Object.values(groupedData).map((item: any) => ({
|
||||
...item,
|
||||
count: Extension.ToThaiNumber(item.count.toString()),
|
||||
}));
|
||||
return new HttpSuccess({
|
||||
template: "registry-officer",
|
||||
reportName: "xlsx-report",
|
||||
data: {
|
||||
year: year
|
||||
? Extension.ToThaiNumber((year + 543).toString())
|
||||
: Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
|
||||
date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
list: result,
|
||||
},
|
||||
// template: "registry-officer",
|
||||
// reportName: "xlsx-report",
|
||||
// data: {
|
||||
// date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
// data: lists,
|
||||
// total: total
|
||||
// },
|
||||
data: lists,
|
||||
total: total
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายงานสถิติข้อมูลลูกจ้างประจำ กทม.
|
||||
*
|
||||
* @summary รายงานสถิติข้อมูลลูกจ้างประจำ กทม.
|
||||
*
|
||||
*/
|
||||
// @Get("registry-emp")
|
||||
// async registryEmp(
|
||||
// @Query() rootId?: string,
|
||||
// @Query() year?: number,
|
||||
// @Query() ageMin?: number,
|
||||
// @Query() ageMax?: number,
|
||||
// ) {
|
||||
// if (ageMin && (ageMin < 18 || ageMin > 60)) {
|
||||
// throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60");
|
||||
// }
|
||||
|
||||
// if (ageMax && (ageMax < 18 || ageMax > 60)) {
|
||||
// throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60");
|
||||
// }
|
||||
|
||||
// const minAge = ageMin ?? 18;
|
||||
// const maxAge = ageMax ?? 60;
|
||||
|
||||
// if (minAge > maxAge) {
|
||||
// throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
|
||||
// }
|
||||
// const yearInAD = year ? year : null;
|
||||
// const currentRevision = await this.orgRevisionRepository.findOne({
|
||||
// where: {
|
||||
// orgRevisionIsCurrent: true,
|
||||
// },
|
||||
// });
|
||||
// const rawdataProfile = await this.empPosMasterRepository
|
||||
// .createQueryBuilder("posMaster")
|
||||
// .leftJoinAndSelect("posMaster.current_holder", "current_holder")
|
||||
// .leftJoinAndSelect("posMaster.positions", "positions")
|
||||
// .leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
|
||||
// .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 })
|
||||
// .andWhere("posMaster.current_holderId Is Not Null")
|
||||
// .andWhere("positions.positionIsSelected = :positionIsSelected", { positionIsSelected: true })
|
||||
// .andWhere(yearInAD && yearInAD != null ? "YEAR(current_holder.dateAppoint) = :year" : "1=1", {
|
||||
// year: yearInAD,
|
||||
// })
|
||||
// .andWhere(
|
||||
// `
|
||||
// TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
|
||||
// AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
|
||||
// `,
|
||||
// { minAge, maxAge },
|
||||
// )
|
||||
// .orderBy("posType.posTypeName", "ASC")
|
||||
// .getMany();
|
||||
// if (!rawdataProfile) {
|
||||
// throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
||||
// }
|
||||
// const mapData = rawdataProfile.map((x) => {
|
||||
// const latestEducation = x.current_holder.profileEducations.sort(
|
||||
// (a: any, b: any) => b.endDate - a.startDate,
|
||||
// )[0];
|
||||
// return {
|
||||
// name: x.current_holder.firstName + " " + x.current_holder.lastName,
|
||||
// affiliation: x.orgRoot.orgRootName ?? "-",
|
||||
// gender: x.current_holder.gender ?? "-",
|
||||
// positionName: x.positions[0] ? x.positions[0].positionName : "-",
|
||||
// status: x.current_holder.relationship ?? "-",
|
||||
// posType: x.current_holder.posType.posTypeName ?? "-",
|
||||
// posLevel: x.current_holder.posLevel.posLevelName ?? "-",
|
||||
// degree: latestEducation ? latestEducation.educationLevel : "-",
|
||||
// period: "-",
|
||||
// };
|
||||
// });
|
||||
|
||||
// const groupedData = mapData.reduce((acc: any, item) => {
|
||||
// const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel}
|
||||
// `;
|
||||
// if (!acc[key]) {
|
||||
// acc[key] = {
|
||||
// affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
|
||||
// gender: item.gender && item.gender != "" ? item.gender : "-",
|
||||
// degree: item.degree && item.degree != "" ? item.degree : "-",
|
||||
// status: item.status && item.status != "" ? item.status : "-",
|
||||
// posType: item.posType && item.posType != "" ? item.posType : "-",
|
||||
// positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
|
||||
// posLevel: Extension.ToThaiNumber(item.posLevel.toString()),
|
||||
// period: "-",
|
||||
// count: 0,
|
||||
// };
|
||||
// }
|
||||
// acc[key].count++;
|
||||
|
||||
// return acc;
|
||||
// }, {});
|
||||
|
||||
// const result = Object.values(groupedData).map((item: any) => ({
|
||||
// ...item,
|
||||
// count: Extension.ToThaiNumber(item.count.toString()),
|
||||
// }));
|
||||
|
||||
// return new HttpSuccess({
|
||||
// template: "registry-emp",
|
||||
// reportName: "xlsx-report",
|
||||
// data: {
|
||||
// year: year
|
||||
// ? Extension.ToThaiNumber((year + 543).toString())
|
||||
// : Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
|
||||
// date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
// list: result,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
@Get("registry-emp")
|
||||
async registryEmp(
|
||||
@Query() rootId?: string,
|
||||
@Query() year?: number,
|
||||
async registryEmployee(
|
||||
@Query() node?: number,
|
||||
@Query() nodeId?: string,
|
||||
@Query() posTypeName?: string,
|
||||
@Query() posLevelName?: string,
|
||||
@Query() position?: string,
|
||||
@Query() gender?: string,
|
||||
@Query() relationship?: string,
|
||||
@Query() degree?: string,
|
||||
@Query() startDateAppoint?: Date,
|
||||
@Query() endDateAppoint?: Date,
|
||||
@Query() ageMin?: number,
|
||||
@Query() ageMax?: number,
|
||||
@Query() sortBy: string = "posMasterNo",
|
||||
@Query() sort: "ASC"|"DESC" = "ASC",
|
||||
) {
|
||||
if (ageMin && (ageMin < 18 || ageMin > 60)) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60");
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60");
|
||||
}
|
||||
|
||||
if (ageMax && (ageMax < 18 || ageMax > 60)) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60");
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60");
|
||||
}
|
||||
|
||||
const minAge = ageMin ?? 18;
|
||||
const maxAge = ageMax ?? 60;
|
||||
|
||||
if (minAge > maxAge) {
|
||||
if (ageMin && ageMax && (ageMin > ageMax)) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
|
||||
}
|
||||
const yearInAD = year ? year : null;
|
||||
const currentRevision = await this.orgRevisionRepository.findOne({
|
||||
where: {
|
||||
orgRevisionIsCurrent: true,
|
||||
},
|
||||
});
|
||||
const rawdataProfile = await this.empPosMasterRepository
|
||||
.createQueryBuilder("posMaster")
|
||||
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
|
||||
.leftJoinAndSelect("posMaster.positions", "positions")
|
||||
.leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
|
||||
.leftJoinAndSelect("current_holder.posType", "posType")
|
||||
.leftJoinAndSelect("current_holder.posLevel", "posLevel")
|
||||
.leftJoinAndSelect("current_holder.profileEducations", "profileEducations")
|
||||
.where("posMaster.orgRevisionId = :currentRevisionId", {
|
||||
currentRevisionId: currentRevision?.id,
|
||||
ageMin = ageMin ?? 18;
|
||||
ageMax = ageMax ?? 60;
|
||||
|
||||
let nodeCondition = "1=1";
|
||||
if (node === 0 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgRootId = :nodeId";
|
||||
}
|
||||
else if (node === 1 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild1Id = :nodeId";
|
||||
}
|
||||
else if (node === 2 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild2Id = :nodeId";
|
||||
}
|
||||
else if (node === 3 && nodeId) {
|
||||
nodeCondition = "registryOfficer.orgChild3Id = :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";
|
||||
} else if (startDateAppoint) {
|
||||
dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint";
|
||||
} else if (endDateAppoint) {
|
||||
dateAppointCondition = "DATE(registryOfficer.dateAppoint) <= :endDateAppoint";
|
||||
}
|
||||
const [lists, total] = await AppDataSource.getRepository(viewRegistryEmployee)
|
||||
.createQueryBuilder("registryOfficer")
|
||||
.where(nodeCondition, {
|
||||
nodeId: nodeId
|
||||
})
|
||||
.andWhere(rootId ? "posMaster.orgRootId = :rootId" : "1=1", { rootId: rootId })
|
||||
.andWhere("posMaster.current_holderId Is Not Null")
|
||||
.andWhere("positions.positionIsSelected = :positionIsSelected", { positionIsSelected: true })
|
||||
.andWhere(yearInAD && yearInAD != null ? "YEAR(current_holder.dateAppoint) = :year" : "1=1", {
|
||||
year: yearInAD,
|
||||
.andWhere("registryOfficer.age BETWEEN :ageMin AND :ageMax", {
|
||||
ageMin, ageMax
|
||||
})
|
||||
.andWhere(dateAppointCondition, {
|
||||
startDateAppoint: startDateAppoint?.toISOString().split("T")[0],
|
||||
endDateAppoint: endDateAppoint?.toISOString().split("T")[0]
|
||||
})
|
||||
.andWhere(
|
||||
`
|
||||
TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
|
||||
AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
|
||||
`,
|
||||
{ minAge, maxAge },
|
||||
new Brackets((qb) => {
|
||||
qb.orWhere(
|
||||
posTypeName != null && posTypeName != ""
|
||||
? "registryOfficer.posTypeName LIKE :posTypeName"
|
||||
: "1=1",
|
||||
{
|
||||
posTypeName: `%${posTypeName}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
posLevelName != null && posLevelName != ""
|
||||
? "registryOfficer.posLevelName LIKE :posLevelName"
|
||||
: "1=1",
|
||||
{
|
||||
posLevelName: `%${posLevelName}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
position != null && position != ""
|
||||
? "registryOfficer.position LIKE :position"
|
||||
: "1=1",
|
||||
{
|
||||
position: `%${position}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
gender != null && gender != ""
|
||||
? "registryOfficer.gender LIKE :gender"
|
||||
: "1=1",
|
||||
{
|
||||
gender: `%${gender}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
relationship != null && relationship != ""
|
||||
? "registryOfficer.relationship LIKE :relationship"
|
||||
: "1=1",
|
||||
{
|
||||
relationship: `%${relationship}%`,
|
||||
},
|
||||
)
|
||||
qb.orWhere(
|
||||
degree != null && degree != ""
|
||||
? "registryOfficer.degree LIKE :degree"
|
||||
: "1=1",
|
||||
{
|
||||
degree: `%${degree}%`,
|
||||
},
|
||||
)
|
||||
}),
|
||||
)
|
||||
.orderBy("posType.posTypeName", "ASC")
|
||||
.getMany();
|
||||
if (!rawdataProfile) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
||||
}
|
||||
const mapData = rawdataProfile.map((x) => {
|
||||
const latestEducation = x.current_holder.profileEducations.sort(
|
||||
(a: any, b: any) => b.endDate - a.startDate,
|
||||
)[0];
|
||||
return {
|
||||
name: x.current_holder.firstName + " " + x.current_holder.lastName,
|
||||
affiliation: x.orgRoot.orgRootName ?? "-",
|
||||
gender: x.current_holder.gender ?? "-",
|
||||
positionName: x.positions[0] ? x.positions[0].positionName : "-",
|
||||
status: x.current_holder.relationship ?? "-",
|
||||
posType: x.current_holder.posType.posTypeName ?? "-",
|
||||
posLevel: x.current_holder.posLevel.posLevelName ?? "-",
|
||||
degree: latestEducation ? latestEducation.educationLevel : "-",
|
||||
period: "-",
|
||||
};
|
||||
});
|
||||
|
||||
const groupedData = mapData.reduce((acc: any, item) => {
|
||||
const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel}
|
||||
`;
|
||||
if (!acc[key]) {
|
||||
acc[key] = {
|
||||
affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
|
||||
gender: item.gender && item.gender != "" ? item.gender : "-",
|
||||
degree: item.degree && item.degree != "" ? item.degree : "-",
|
||||
status: item.status && item.status != "" ? item.status : "-",
|
||||
posType: item.posType && item.posType != "" ? item.posType : "-",
|
||||
positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
|
||||
posLevel: Extension.ToThaiNumber(item.posLevel.toString()),
|
||||
period: "-",
|
||||
count: 0,
|
||||
};
|
||||
}
|
||||
acc[key].count++;
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const result = Object.values(groupedData).map((item: any) => ({
|
||||
...item,
|
||||
count: Extension.ToThaiNumber(item.count.toString()),
|
||||
}));
|
||||
.orderBy(`registryOfficer.${sortBy}`, sort)
|
||||
.getManyAndCount();
|
||||
|
||||
return new HttpSuccess({
|
||||
template: "registry-emp",
|
||||
reportName: "xlsx-report",
|
||||
data: {
|
||||
year: year
|
||||
? Extension.ToThaiNumber((year + 543).toString())
|
||||
: Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
|
||||
date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
list: result,
|
||||
},
|
||||
// template: "registry-officer",
|
||||
// reportName: "xlsx-report",
|
||||
// data: {
|
||||
// date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
|
||||
// data: lists,
|
||||
// total: total
|
||||
// },
|
||||
data: lists,
|
||||
total: total
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue