diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 3ad049d3..212dac51 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -60,118 +60,6 @@ export class ReportController extends Controller { * @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() node?: number, @@ -195,8 +83,8 @@ export class ReportController extends Controller { @Query() tenureMin?: number, @Query() tenureMax?: number, @Query() positionArea?: string, - @Query() educationLevels?: string, - @Query() fields?: string, + @Query() educationLevel?: string, + @Query() field?: string, @Query() sortBy: string = "posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", ) { @@ -345,153 +233,162 @@ export class ReportController extends Controller { }, ) .andWhere( - educationLevels != null && educationLevels != "" + educationLevel != null && educationLevel != "" ? "registryOfficer.educationLevels LIKE :educationLevels" : "1=1", { - educationLevels: `%${educationLevels}%`, + educationLevels: `%${educationLevel}%`, }, ) .andWhere( - fields != null && fields != "" + field != null && field != "" ? "registryOfficer.fields LIKE :fields" : "1=1", { - fields: `%${fields}%`, + fields: `%${field}%`, }, ) .orderBy(`registryOfficer.${sortBy}`, sort) .getManyAndCount(); - const mapData = await Promise.all( - lists.map(async (x) => { - return { - profileId: x.profileId, - citizenId: x.citizenId, - prefix: x.prefix, - firstName: x.firstName, - lastName: x.lastName, - isProbation: x.isProbation, - isLeave: x.isLeave, - isRetirement: x.isRetirement, - leaveType: x.leaveType, - posMasterNo: x.posMasterNo, - orgRootId: x.orgRootId, - orgChild1Id: x.orgChild1Id, - orgChild2Id: x.orgChild2Id, - orgChild3Id: x.orgChild3Id, - orgChild4Id: x.orgChild4Id, - orgRootName: x.orgRootName, - orgChild1Name: x.orgChild1Name, - orgChild2Name: x.orgChild2Name, - orgChild3Name: x.orgChild3Name, - orgChild4Name: x.orgChild4Name, - org: x.org, - searchShortName: x.searchShortName, - posExecutiveName: x.posExecutiveName, - position: x.position, - posTypeName: x.posTypeName, - posLevelName: x.posLevelName, - gender: x.gender, - relationship: x.relationship, - dateAppoint: x.dateAppoint, - dateRetire: x.dateRetire, - dateRetireLaw: x.dateRetireLaw, - birthdate: x.birthdate, - degree: x.degrees, - age: x.age, - currentPosition: null, - lengthPosition: null, - positionDate: { - Years: x.Years ? x.Years : 0, - Months: x.Months ? x.Months : 0, - Days: x.Days ? x.Days : 0, - }, - levelDate: { - posExecutiveYears: x.levelYears, - posExecutiveMonths: x.levelMonths, - posExecutiveDays: x.levelDays, - }, - }; - }), - ); - // const mapData = []; - // for await (const x of lists) { - // let _educations:any = [] - // if(education == "" && educationLevels == "" && fields == "") { - // _educations = (x.Educations as any[]).filter( - // (i: any) => i.isEducation === true - // ); - // if(_educations.length == 0) { - // _educations = (x.Educations as any[]).filter( - // (i: any) => i.isHigh === true - // ); - // if(_educations.length == 0) { - // _educations = (x.Educations as any[])[0] || [] - // } - // } - // } - // else { - // _educations = (x.Educations as any[]).filter( - // (i: any) => - // i.degrees === education || - // i.educationLevel === educationLevels || - // i.field === fields - // ); - // } - // mapData.push({ - // profileId: x.profileId, - // citizenId: x.citizenId, - // prefix: x.prefix, - // firstName: x.firstName, - // lastName: x.lastName, - // isProbation: x.isProbation, - // isLeave: x.isLeave, - // isRetirement: x.isRetirement, - // leaveType: x.leaveType, - // posMasterNo: x.posMasterNo, - // orgRootId: x.orgRootId, - // orgChild1Id: x.orgChild1Id, - // orgChild2Id: x.orgChild2Id, - // orgChild3Id: x.orgChild3Id, - // orgChild4Id: x.orgChild4Id, - // orgRootName: x.orgRootName, - // orgChild1Name: x.orgChild1Name, - // orgChild2Name: x.orgChild2Name, - // orgChild3Name: x.orgChild3Name, - // orgChild4Name: x.orgChild4Name, - // org: x.org, - // searchShortName: x.searchShortName, - // posExecutiveName: x.posExecutiveName, - // position: x.position, - // positionArea: x.positionArea, - // posTypeName: x.posTypeName, - // posLevelName: x.posLevelName, - // gender: x.gender, - // relationship: x.relationship, - // dateAppoint: x.dateAppoint, - // dateRetire: x.dateRetire, - // dateRetireLaw: x.dateRetireLaw, - // birthdate: x.birthdate, - // education: _educations, - // degree: x.degrees, - // educationLevels: x.educationLevels, - // fields: x.fields, - // age: x.age, - // currentPosition: null, - // lengthPosition: null, - // positionDate: { - // Years: x.Years ? x.Years : 0, - // Months: x.Months ? x.Months : 0, - // Days: x.Days ? x.Days : 0, - // }, - // levelDate: { - // posExecutiveYears: x.levelYears ? x.levelYears : 0, - // posExecutiveMonths: x.levelMonths ? x.levelMonths : 0, - // posExecutiveDays: x.levelDays ? x.levelDays : 0, - // }, - // }); - // } + // const mapData1 = await Promise.all( + // lists.map(async (x) => { + // return { + // profileId: x.profileId, + // citizenId: x.citizenId, + // prefix: x.prefix, + // firstName: x.firstName, + // lastName: x.lastName, + // isProbation: x.isProbation, + // isLeave: x.isLeave, + // isRetirement: x.isRetirement, + // leaveType: x.leaveType, + // posMasterNo: x.posMasterNo, + // orgRootId: x.orgRootId, + // orgChild1Id: x.orgChild1Id, + // orgChild2Id: x.orgChild2Id, + // orgChild3Id: x.orgChild3Id, + // orgChild4Id: x.orgChild4Id, + // orgRootName: x.orgRootName, + // orgChild1Name: x.orgChild1Name, + // orgChild2Name: x.orgChild2Name, + // orgChild3Name: x.orgChild3Name, + // orgChild4Name: x.orgChild4Name, + // org: x.org, + // searchShortName: x.searchShortName, + // posExecutiveName: x.posExecutiveName, + // position: x.position, + // posTypeName: x.posTypeName, + // posLevelName: x.posLevelName, + // gender: x.gender, + // relationship: x.relationship, + // dateAppoint: x.dateAppoint, + // dateRetire: x.dateRetire, + // dateRetireLaw: x.dateRetireLaw, + // birthdate: x.birthdate, + // degree: x.degrees, + // age: x.age, + // currentPosition: null, + // lengthPosition: null, + // positionDate: { + // Years: x.Years ? x.Years : 0, + // Months: x.Months ? x.Months : 0, + // Days: x.Days ? x.Days : 0, + // }, + // levelDate: { + // posExecutiveYears: x.levelYears, + // posExecutiveMonths: x.levelMonths, + // posExecutiveDays: x.levelDays, + // }, + // }; + // }), + // ); + + const mapData = []; + for await (const x of lists) { + let _educations:any = [] + if(!education && !educationLevel && !field) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => i.isEducation === true + ) + : [] + if(_educations.length == 0) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => i.isHigh === true + ) + : [] + if(_educations.length == 0) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[])[0] + : [] + } + } + } + else { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => + i.degree === education || + i.educationLevel === educationLevel || + i.field === field + ) + : [] + } + mapData.push({ + profileId: x.profileId, + citizenId: x.citizenId, + prefix: x.prefix, + firstName: x.firstName, + lastName: x.lastName, + isProbation: x.isProbation, + isLeave: x.isLeave, + isRetirement: x.isRetirement, + leaveType: x.leaveType, + posMasterNo: x.posMasterNo, + orgRootId: x.orgRootId, + orgChild1Id: x.orgChild1Id, + orgChild2Id: x.orgChild2Id, + orgChild3Id: x.orgChild3Id, + orgChild4Id: x.orgChild4Id, + orgRootName: x.orgRootName, + orgChild1Name: x.orgChild1Name, + orgChild2Name: x.orgChild2Name, + orgChild3Name: x.orgChild3Name, + orgChild4Name: x.orgChild4Name, + org: x.org, + searchShortName: x.searchShortName, + posExecutiveName: x.posExecutiveName, + position: x.position, + positionArea: x.positionArea, + posTypeName: x.posTypeName, + posLevelName: x.posLevelName, + gender: x.gender, + relationship: x.relationship, + dateAppoint: x.dateAppoint, + dateRetire: x.dateRetire, + dateRetireLaw: x.dateRetireLaw, + birthdate: x.birthdate, + education: _educations, + degree: x.degrees, + educationLevel: x.educationLevels, + field: x.fields, + age: x.age, + currentPosition: null, + lengthPosition: null, + positionDate: { + Years: x.Years ? x.Years : 0, + Months: x.Months ? x.Months : 0, + Days: x.Days ? x.Days : 0, + }, + levelDate: { + posExecutiveYears: x.levelYears ? x.levelYears : 0, + posExecutiveMonths: x.levelMonths ? x.levelMonths : 0, + posExecutiveDays: x.levelDays ? x.levelDays : 0, + }, + }); + } return new HttpSuccess({ data: mapData, @@ -505,117 +402,6 @@ export class ReportController extends Controller { * @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 registryEmployee( @Query() node?: number, @@ -637,8 +423,8 @@ export class ReportController extends Controller { @Query() tenureType?: string, @Query() tenureMin?: number, @Query() tenureMax?: number, - @Query() educationLevels?: string, - @Query() fields?: string, + @Query() educationLevel?: string, + @Query() field?: string, @Query() sortBy: string = "posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", ) { @@ -781,12 +567,59 @@ export class ReportController extends Controller { degrees: `%${education}%`, }, ) + .andWhere( + educationLevel != null && educationLevel != "" + ? "registryEmployee.educationLevels LIKE :educationLevels" + : "1=1", + { + educationLevels: `%${educationLevel}%`, + }, + ) + .andWhere( + field != null && field != "" + ? "registryEmployee.fields LIKE :fields" + : "1=1", + { + fields: `%${field}%`, + }, + ) .orderBy(`registryEmployee.${sortBy}`, sort) .getManyAndCount(); - - const mapData = await Promise.all( - lists.map(async (x) => { - return { + + + const mapData = []; + for await (const x of lists) { + let _educations:any = [] + if(!education && !educationLevel && !field) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => i.isEducation === true + ) + : [] + if(_educations.length == 0) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => i.isHigh === true + ) + : [] + if(_educations.length == 0) { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[])[0] + : [] + } + } + } + else { + _educations = Array.isArray(x.Educations) && x.Educations != null + ? (x.Educations as any[]).filter( + (i: any) => + i.degree === education || + i.educationLevel === educationLevel || + i.field === field + ) + : [] + } + mapData.push({ profileId: x.profileEmployeeId, citizenId: x.citizenId, prefix: x.prefix, @@ -818,28 +651,30 @@ export class ReportController extends Controller { dateRetire: x.dateRetire, dateRetireLaw: x.dateRetireLaw, birthdate: x.birthdate, + education: _educations, degree: x.degrees, + educationLevel: x.educationLevels, + field: x.fields, age: x.age, currentPosition: null, lengthPosition: null, positionDate: { - Years: x.Years, - Months: x.Months, - Days: x.Days, + Years: x.Years ? x.Years : 0, + Months: x.Months ? x.Months : 0, + Days: x.Days ? x.Days : 0, }, levelDate: { - posExecutiveYears: x.levelYears, - posExecutiveMonths: x.levelMonths, - posExecutiveDays: x.levelDays, + posExecutiveYears: x.levelYears ? x.levelYears : 0, + posExecutiveMonths: x.levelMonths ? x.levelMonths : 0, + posExecutiveDays: x.levelDays ? x.levelDays : 0, }, - }; - }), - ); - return new HttpSuccess({ - data: mapData, - total: total, - }); - } + }); + } + return new HttpSuccess({ + data: mapData, + total: total, + }); +} /** * API Report1