From f40a5f8e5142b051a9dcbbac0bc39c50282509ea Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 29 Oct 2024 14:05:52 +0700 Subject: [PATCH] checkpoint --- src/controllers/MyController.ts | 11 ++- src/interfaces/utils.ts | 155 +++++++++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 2 deletions(-) diff --git a/src/controllers/MyController.ts b/src/controllers/MyController.ts index 4dde32ca..072ac08e 100644 --- a/src/controllers/MyController.ts +++ b/src/controllers/MyController.ts @@ -1,4 +1,7 @@ -import { Controller, Get, Route, Security, Tags } from "tsoa"; +import { profile } from "console"; +import { Controller, Get, Post, Query, Route, Security, Tags } from "tsoa"; +import { calculateGovAge } from "../interfaces/utils"; +import HttpSuccess from "../interfaces/http-success"; @Route("/hello") @Tags("Test") @@ -8,4 +11,10 @@ export class AppController extends Controller { public async GET() { return { message: "Hello World 1" }; } + + // @Post() + // public async Post(@Query() profileId: string) { + // const result = calculateGovAge(profileId); + // return new HttpSuccess(result); + // } } diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 91070e1d..662601ab 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -4,9 +4,10 @@ import { PosMaster } from "../entities/PosMaster"; import { Position } from "../entities/Position"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeePosition } from "../entities/EmployeePosition"; -import { In } from "typeorm"; +import { In, IsNull, MoreThan, Not } from "typeorm"; import { RequestWithUser } from "../middlewares/user"; import { Command } from "../entities/Command"; +import { ProfileSalary } from "../entities/ProfileSalary"; export function calculateAge(start: Date, end = new Date()) { if (start.getTime() > end.getTime()) return null; @@ -39,6 +40,158 @@ export function calculateAge(start: Date, end = new Date()) { return { year, month, day }; } +export async function calculateGovAge(profileId: string) { + const records = await AppDataSource.getRepository(ProfileSalary).find({ + where: { + profileId: profileId, + }, + select: ["date", "dateGovernment", "isGovernment"], + order: { order: "ASC" }, + }); // หา record ทั้งหมด + + if (!records || records.length === 0) { + return null; + } + + let sumYears = 0; + let sumMonths = 0; + let sumDays = 0; + + let endDateFristRec: any; + endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({ + where: { + profileId: profileId, + dateGovernment: Not(IsNull()), + isGovernment: false, + }, + select: ["date", "dateGovernment", "isGovernment", "order"], + order: { order: "ASC" }, + }); // หา isGovernment: false record แรก + + let totalYears1 = 0; + let totalMonths1 = 0; + let totalDays1 = 0; + + const firstStartDate = new Date(records[0].date); + const firstEndDate = endDateFristRec?new Date(endDateFristRec.dateGovernment):new Date(); + console.log("[firstStartDate]",firstStartDate); + console.log("[firstEndDate]",firstEndDate); + + totalYears1 = firstEndDate.getFullYear() - firstStartDate.getFullYear(); + totalMonths1 = firstEndDate.getMonth() - firstStartDate.getMonth(); + totalDays1 = firstEndDate.getDate() - firstStartDate.getDate(); + + if (totalDays1 < 0) { + totalMonths1 -= 1; // หาวันในเดือนก่อนหน้า + const lastMonth = new Date(firstEndDate.getFullYear(), firstEndDate.getMonth(), 0).getDate(); + totalDays1 += lastMonth; + } + + if (totalMonths1 < 0) { + totalMonths1 += 12; + totalYears1 -= 1; + } + + const records_middle = await AppDataSource.getRepository(ProfileSalary).find({ + where: { + profileId: profileId, + order: MoreThan(endDateFristRec?.order), + dateGovernment: Not(IsNull()), + }, + select: ["date", "dateGovernment", "isGovernment", "order"], + order: { order: "ASC" }, + }); // หา record order ที่ทำหลังจาก endDateFristRec + console.log("[Gov1]", totalYears1, totalMonths1, totalDays1); + if (!records_middle || records_middle.length === 0) { // ถ้าไม่เจอแปลว่ากำลังพักราชการอยู่หรือยังไม่เคยพักราชการ + sumYears = totalYears1, + sumMonths = totalMonths1, + sumDays = totalDays1 + console.log("[SumGov1]", sumYears, sumMonths, sumDays); + return { + sumYears, + sumMonths, + sumDays, + }; + } + + let totalYears2 = 0; + let totalMonths2 = 0; + let totalDays2 = 0; + for (let i = 0; i < records_middle.length; i++) { + const startDate = new Date(records_middle[i].dateGovernment); + const endDate = (i < records_middle.length - 1) + ? new Date(records_middle[i + 1].dateGovernment) + : new Date(); // วันที่ปัจจุบันถ้าเป็นช่วงสุดท้าย + + if (records_middle[i].isGovernment === false && records_middle[i + 1].isGovernment === false) { + break; + } + if (records_middle[i].isGovernment === false && i === records_middle.length - 1) { + break; + } + + console.log("[SDate]", startDate); + console.log("[EDate]", endDate); + + // คำนวณระยะเวลา + let years_mid = endDate.getFullYear() - startDate.getFullYear(); + let months_mid = endDate.getMonth() - startDate.getMonth(); + let days_mid = endDate.getDate() - startDate.getDate(); + + if (days_mid < 0) { + months_mid--; + const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate(); + days_mid += lastMonthDays; // ปรับวันให้ถูกต้อง + } + + // การปรับเดือน + if (months_mid < 0) { + months_mid += 12; + years_mid--; + } + + // รวมเข้ากับ total + totalYears2 += years_mid; + totalMonths2 += months_mid; + totalDays2 += days_mid; + } + + // การปรับค่าหาก totalMonths2 เกิน 12 + if (totalMonths2 >= 12) { + totalYears2 += Math.floor(totalMonths2 / 12); + totalMonths2 = totalMonths2 % 12; + } + + // การปรับค่าหาก totalDays2 เกินจำนวนวันในเดือน + const daysInCurrentMonth = new Date( + new Date().getFullYear(), + new Date().getMonth() + 1, + 0, + ).getDate(); + if (totalDays2 >= daysInCurrentMonth) { + totalMonths2 += Math.floor(totalDays2 / daysInCurrentMonth); + totalDays2 = totalDays2 % daysInCurrentMonth; + } + + sumYears = totalYears1 + totalYears2; + sumMonths = totalMonths1 + totalMonths2; + sumDays = totalDays1 + totalDays2; + + if (sumMonths >= 12) { + sumYears += Math.floor(sumMonths / 12); + sumMonths = sumMonths % 12; + } + console.log("[data1]",totalYears1+"/"+totalMonths1+"/"+totalDays1); + console.log("[data2]",totalYears2+"/"+totalMonths2+"/"+totalDays2); + console.log("[SumGovAge]", sumYears, sumMonths, sumDays); + + return { + sumYears, + sumMonths, + sumDays, + }; +} + export function calculateRetireDate(birthDate: Date) { // let _birthDate = birthDate;