map data entry

This commit is contained in:
mamoss 2025-06-19 09:24:12 +07:00
parent b56fced2d4
commit afdc758cd0

View file

@ -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<string, any> }) {
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;
}, {});
}
}