diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 5a63d0d8..045e4d32 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -10,6 +10,8 @@ import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; +import { PosType } from "../entities/PosType"; +import { PosLevel } from "../entities/PosLevel"; import Extension from "../interfaces/extension"; @Route("api/v1/org/report") @Tags("Report") @@ -25,6 +27,8 @@ export class ReportController extends Controller { private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + private posTypepository = AppDataSource.getRepository(PosType); + private posLevelRepository = AppDataSource.getRepository(PosLevel); /** * API Report1 @@ -4087,8 +4091,7 @@ export class ReportController extends Controller { } @Get("report4/{rootId}") - async findReport4(@Path() rootId: string) { - + async findReport4(@Path() rootId: string ="71bf0e3d-ba38-4b6c-b87e-c27c8d42edce") { const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, @@ -4102,87 +4105,77 @@ export class ReportController extends Controller { "posMasters.positions.posExecutive", ], }); - let data: any = []; - var _data = [{ - type: "ทั่วไป", - level: "ปฎิบัติงาน", - total: "0", - remark: "-" - }, - { - type: "", - level: "ชำนาญงาน", - total: "0", - remark: "-" - }, - { - type: "", - level: "อาวุโส", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", + if(!rootId) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + const posType = await this.posTypepository.find({ + order: { + posTypeRank: "ASC", + posLevels: { "posLevelRank": "ASC" } + }, + relations: ["posLevels"] + }); + + const posLevel = await this.posLevelRepository.find({ + order: {"posLevelRank": "ASC"} + }); + + const mapData = posType.map((type) => ({ + id: type.id, + typeName: type.posTypeName, + levels: posLevel + .filter((level) => level.posTypeId === type.id) + .map((level) => ({ levelName: level.posLevelName, id: level.id })), + })); + + let _data: any = []; + mapData.forEach((type, idx) => { + type.levels.forEach((level, idy) => { + _data.push({ + typeName: idy == 0 ? type.typeName : "", + levelName: level.levelName, + total: orgRootData.length > 0 + ? orgRootData[0].posMasters + .flatMap((x) => x.positions + .filter((y) => y.posLevelId === level.id && y.posTypeId === type.id ) + .map((z) => ({ + position: z.positionName || "" + }) + )).length + : 0, + remark: "" + }); + }); + _data.push({ + typeName: "", + levelName: "รวม", + total: orgRootData.length > 0 + ? orgRootData[0].posMasters + .flatMap((x) => x.positions + .filter((y) => y.posTypeId === type.id ) + .map((z) => ({ + position: z.positionName || "" + }) + )).length + : 0, + remark: "" + }); + }); + _data.push({ + typeName: "", + levelName: "รวมทั้งสิ้น", + total: orgRootData.length > 0 + ? orgRootData[0].posMasters.flatMap((x) => x.positions).length + : 0, remark: "" - }, - { - type: "วิชาการ", - level: "ปฎิบัติการ", - total: "0", - remark: "-" - }, - { - type: "", - level: "ชำนาญการ", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - }, - { - type: "อำนวยการ", - level: "ต้น", - total: "0", - remark: "-" - }, - { - type: "", - level: "สูง", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - }, - { - type: "บริหาร", - level: "ต้น", - total: "0", - remark: "-" - }, - { - type: "", - level: "สูง", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - } - ] - data.push(_data); - return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()) , data: _data } }); + }); + + return new HttpSuccess({ + template: "report4", + reportName: "report4", + data: { + dateCurrent: Extension.ToThaiShortDate(new Date()), + rootName: orgRootData.length > 0 ? orgRootData[0].orgRootName : "-", + data: _data + } + }); } }