From a455c14ce6d7850169545357e9afd7510df25564 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 13 Feb 2025 17:34:19 +0700 Subject: [PATCH] update --- src/controllers/ReportController.ts | 116 +++++++++++++++++++++++- src/entities/view/viewDevScholarship.ts | 26 ++++++ src/interfaces/extension.ts | 43 +++++++++ 3 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 src/entities/view/viewDevScholarship.ts diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index deb87af..5819489 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -623,14 +623,75 @@ export class ReportController extends Controller { * */ @Get("report5") - async report5() { + async report5( + @Query("year") year: number, + // @Query("rootId") rootId: string, + ) { + + const [development, total] = await AppDataSource.getRepository(DevelopmentScholarship) + .createQueryBuilder("developmentScholarship") + .leftJoinAndSelect("developmentScholarship.posLevel", "posLevel") + .leftJoinAndSelect("developmentScholarship.posType", "posType") + .leftJoinAndSelect("developmentScholarship.posLevelguarantor", "posLevelguarantor") + .leftJoinAndSelect("developmentScholarship.posTypeguarantor", "posTypeguarantor") + // .where("developmentScholarship.rootId = :rootId", { rootId: rootId }) + .andWhere( + year !== 0 && year != null && year != undefined + ? "developmentScholarship.scholarshipYear = :scholarshipYear" + : "1=1", + { scholarshipYear: year }, + ) + .andWhere( + "developmentScholarship.scholarshipType = :scholarshipType", { scholarshipType: "RESEARCH" }, + ) + .orderBy("developmentScholarship.scholarshipYear", "DESC") + .addOrderBy("developmentScholarship.createdAt", "DESC") + .getManyAndCount(); + + const totalBudgetApprove = development.reduce((sum, item) => sum + Number(item.budgetApprove), 0); + const formattedData = development.map((item, index) => { + const rawPath = [ + item.course == "-" ? null : item.course, + item.field == "-" ? null : item.course + ]; + + const courseAndfield = rawPath + .filter((path) => path !== undefined && path !== null) + .join("/"); + + const date = [ + Extension.ToThaiNumber(Extension.ToThaiShortDate(item.startDate)), + Extension.ToThaiNumber(Extension.ToThaiShortDate(item.endDate)) + ]; + + const dateDulation = date + .filter((path) => path !== undefined && path !== null) + .join(" - "); + + return { + no: Extension.ToThaiNumber((index + 1).toString()), + id: item.id, + studyTopic: item.studyTopic ? item.studyTopic : "-", + fullName: item.prefix + item.firstName + " " + item.lastName, + position: item.position, + posLevel: item.posLevel ? item.posLevel.posLevelName : "-", + courseAndfield: courseAndfield ?? "-", + place: item.studyPlace ? item.studyPlace : "-", + country: item.studyCountry ? item.studyCountry : "-", + startAndendDate: dateDulation, + budgetApprove: item.budgetApprove ? Extension.ToThaiNumber(item.budgetApprove.toString()) : "-", + }; + }); return new HttpSuccess({ template: "reportFund5", reportName: "reportFund5", data: { - data: "", + year: Extension.ToThaiNumber(year.toString()), + data: formattedData, + total: Extension.ToThaiNumber(total.toString()), + totalBudgetApprove: Extension.ToThaiNumber(totalBudgetApprove.toString()), }, }); } @@ -641,8 +702,57 @@ export class ReportController extends Controller { * */ @Get("report6") - async report6() { + async report6( + @Query("year") year: number, + ) { + const degree = [ + "ปริญญาเอก", + "ปริญญาโท", + "ปริญญาตรี", + "ปวส.", + "ปวช.", + "ม.6", + "ม.3", + ]; + + + // const totalBudgetApprove = development.reduce((sum, item) => sum + Number(item.budgetApprove), 0); + // const formattedData = development.map((item, index) => { + // const rawPath = [ + // item.course == "-" ? null : item.course, + // item.field == "-" ? null : item.course + // ]; + // const courseAndfield = rawPath + // .filter((path) => path !== undefined && path !== null) + // .join("/"); + + // const date = [ + // Extension.ToThaiNumber(Extension.ToThaiShortDate(item.startDate)), + // Extension.ToThaiNumber(Extension.ToThaiShortDate(item.endDate)) + // ]; + + // const dateDulation = date + // .filter((path) => path !== undefined && path !== null) + // .join(" - "); + + // return { + // no: Extension.ToThaiNumber((index + 1).toString()), + // id: item.id, + // rootId: item.rootId, + // root: item.root, + // degree: item.degreeLevel, + // // studyTopic: item.studyTopic ? item.studyTopic : "-", + // // fullName: item.prefix + item.firstName + " " + item.lastName, + // // position: item.position, + // // posLevel: item.posLevel ? item.posLevel.posLevelName : "-", + // // courseAndfield: courseAndfield ?? "-", + // // place: item.studyPlace ? item.studyPlace : "-", + // // country: item.studyCountry ? item.studyCountry : "-", + // // startAndendDate: dateDulation, + // // budgetApprove: item.budgetApprove ? Extension.ToThaiNumber(item.budgetApprove.toString()) : "-", + // }; + // }); return new HttpSuccess({ template: "reportFund6", diff --git a/src/entities/view/viewDevScholarship.ts b/src/entities/view/viewDevScholarship.ts new file mode 100644 index 0000000..4b66501 --- /dev/null +++ b/src/entities/view/viewDevScholarship.ts @@ -0,0 +1,26 @@ +import { ViewColumn, ViewEntity } from "typeorm"; + +@ViewEntity({ + expression: `SELECT MAX(\`rootId\`) AS rootId, + MAX(\`root\`) AS root,\`degreeLevel\`, + COUNT(*) AS numberOfRecords, + COUNT(DISTINCT \`scholarshipType\`) AS numberOfScholarshipTypes, + SUM(\`budgetApprove\`) AS totalBudgetApprove + FROM \`developmentScholarship\` + GROUP BY \`rootId\`,\`degreeLevel\` + `, +}) + +export class viewProfileEvaluation { + @ViewColumn() + rootId: string; + @ViewColumn() + root: string; + @ViewColumn() + numberOfRecords: number; + @ViewColumn() + numberOfScholarshipTypes: number; + @ViewColumn() + totalBudgetApprove: number; + } + diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index 5de8bd4..f8a28d1 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -30,6 +30,37 @@ class Extension { } } + public static ToThaiShortMonth(value: number) { + switch (value) { + case 1: + return "ม.ค."; + case 2: + return "ก.พ."; + case 3: + return "มี.ค."; + case 4: + return "เม.ย."; + case 5: + return "พ.ค."; + case 6: + return "มิ.ย."; + case 7: + return "ก.ค."; + case 8: + return "ส.ค."; + case 9: + return "ก.ย."; + case 10: + return "ต.ค."; + case 11: + return "พ.ย."; + case 12: + return "ธ.ค."; + default: + return ""; + } + } + public static ToThaiYear(value: number) { if (value < 2400) return value + 543; else return value; @@ -86,6 +117,18 @@ class Extension { } return sum; } + + public static ToThaiShortDate(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + value.getDate() + + " " + + Extension.ToThaiShortMonth(value.getMonth() + 1) + + " " + + yy.toString().slice(-2) + ); + } + } export default Extension;