From afdc758cd06b89591de7e521ace05eb15187fdbd Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Thu, 19 Jun 2025 09:24:12 +0700 Subject: [PATCH] map data entry --- src/controllers/ImportDataController.ts | 146 ++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 44346ab4..45403c71 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -92,6 +92,7 @@ import { ProfileEducations } from "../entities/ProfileEducations"; import { ProfileInsignias } from "../entities/ProfileInsignias"; import { ProfileLeave } from "../entities/ProfileLeave"; import { ProfileLeaves } from "../entities/ProfileLeaves"; +import { ProfileSalaryTemp } from "../entities/ProfileSalaryTemp"; @Route("api/v1/org/upload") @Tags("UPLOAD") @Security("bearerAuth") @@ -184,6 +185,8 @@ export class ImportDataController extends Controller { private LeaveSummaryRepo = AppDataSource.getRepository(ProfileLeave); private ProfileInsigniasRepo = AppDataSource.getRepository(ProfileInsignias); private InsigniaRepo = AppDataSource.getRepository(ProfileInsignia); + private salaryTempRepo = AppDataSource.getRepository(ProfileSalaryTemp); + /** * @summary ทะเบียนประวัติ ข้าราชการ */ @@ -4746,4 +4749,147 @@ export class ImportDataController extends Controller { } return new HttpSuccess(); } + + /** + * @summary รวม entry กับตัวหลัก + */ + @Post("tranferEntryToMain") + async tranferEntryToMain(@Request() request: { user: Record }) { + const profiles = await this.profileRepo.find({ + where: { + citizenId: In([ + "3860700270466", + "3909900758770", + "3309900659891", + "3100501312173", + "3360200140185", + "3101900524141", + "3102101307867", + ]), + }, + order: { profileSalary: { commandDateAffect: "ASC" } }, + relations: ["profileSalary"], + }); + for await (const item of profiles) { + // 2. จัดกลุ่มข้อมูลตามวันที่คำสั่งมีผล + const groupedByDate = this.groupOrdersByDate(item.profileSalary); + // 3. ประมวลผลแต่ละกลุ่ม + const processedOrders: any = []; + let num = 0; + for (let [date, orders] of Object.entries(groupedByDate) as any) { + if (orders.length == 1) { + num = num + 1; + //save + orders.isDelete = false; + processedOrders.push(orders[0]); + continue; + } + // หาแถวหลัก (ที่มีเลขที่คำสั่ง) + const mainOrder: ProfileSalary[] = orders.filter((order: ProfileSalary) => order.commandNo); + const orderOrigi: ProfileSalary[] = []; + // ข้ามถ้าไม่มีแถวหลัก + if (mainOrder.length == 0) { + for await (const _item of orders) { + num = num + 1; + _item.isDelete = false; + processedOrders.push(_item); + //save + } + continue; + } + + for (const _item1 of mainOrder) { + let _orders = orders.filter((x: ProfileSalary) => !x.id.includes(_item1.id)); + for (const _item of _orders) { + num = num + 1; + //ปี 4หลัก + const findCommandNo = _item.remark?.includes( + _item1.commandNo + "/" + (_item1.commandYear + 543), + ); + if (findCommandNo) { + _item.isDelete = true; + processedOrders.push(_item); + orders = orders.filter((x: ProfileSalary) => x.id != _item.id); + orderOrigi.push(_item); + continue; + } + + //ปี 2หลัก + const _findCommandNo = _item.remark?.includes( + _item1.commandNo + "/" + (_item1.commandYear + 543).toString().slice(-2), + ); + if (_findCommandNo) { + _item.isDelete = true; + processedOrders.push(_item); + orders = orders.filter((x: ProfileSalary) => x.id != _item.id); + orderOrigi.push(_item); + continue; + } + } + } + for (const _item of orders) { + num = num + 1; + //ปี 4หลัก + const chk1 = orderOrigi.filter( + (x) => + x.remark.includes(_item.commandNo + "/" + (_item.commandYear + 543)) && + (_item.commandName == "แก้ไขคำสั่ง" + ? x.remark.includes("แก้ไขคำสั่ง") + : !x.remark.includes("แก้ไขคำสั่ง")), + ); + if (chk1.length > 0) { + _item.isDelete = false; + _item.remark = chk1[0].remark; + //commandName + processedOrders.push(_item); + continue; + } + + //ปี 2หลัก + const chk2 = orderOrigi.filter( + (x) => + x.remark.includes( + _item.commandNo + "/" + (_item.commandYear + 543).toString().slice(-2), + ) && + (_item.commandName == "แก้ไขคำสั่ง" + ? x.remark.includes("แก้ไขคำสั่ง") + : !x.remark.includes("แก้ไขคำสั่ง")), + ); + if (chk2.length > 0) { + _item.isDelete = false; + _item.remark = chk2[0].remark; + //commandName + processedOrders.push(_item); + continue; + } + _item.isDelete = false; + processedOrders.push(_item); + } + } + let salaryNew = processedOrders.map(({ id, ...rest }: ProfileSalary) => ({ + ...rest, + isEdit: false, + createdUserId: request.user.sub, + createdFullName: request.user.name, + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + })); + await this.salaryTempRepo.save(salaryNew); + // return new HttpSuccess(salaryNew.length); + } + return new HttpSuccess(); + } + // ฟังก์ชันจัดกลุ่มตามวันที่ + groupOrdersByDate(orders: ProfileSalary[]) { + return orders.reduce((groups: any, order) => { + const date = order.commandDateAffect.toDateString(); // เปลี่ยนชื่อฟิลด์ตามจริง + if (!groups[date]) { + groups[date] = []; + } + groups[date].push(order); + return groups; + }, {}); + } }