import salary

This commit is contained in:
mamoss 2025-10-25 19:27:41 +07:00
parent f111132184
commit 6fab6c724e

View file

@ -1,6 +1,6 @@
import { Controller, Post, Route, Security, Tags, Request, UploadedFile } from "tsoa";
import { AppDataSource } from "../database/data-source";
import { In, IsNull, LessThanOrEqual, Not } from "typeorm";
import { In, IsNull, LessThanOrEqual, Not, Between } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import { UseInterceptors } from "@nestjs/common";
import { Profile } from "../entities/Profile";
@ -631,465 +631,529 @@ export class ImportDataController extends Controller {
async UploadFileSQLSalary(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
let _null: any = null;
let sqlStatements: string[] = [];
const batchSize = 200; // เพิ่ม batch size เพื่อประสิทธิภาพที่ดีขึ้น
const [profiles, total] = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .where("profile.citizenId = '3101702379675'")
// .where({
// citizenId: In([
// // "1100600109451",
// // "1209900075508",
// // "1739900231556",
// // "1809900305214",
// // "1920600228762",
// // "3101600963742",
// // "3102401171243",
// // "3120100454406",
// // "3180100306172",
// // "3700100094722",
// // "3809900116957",
// "3940900213929",
// ]),
// })
.leftJoinAndSelect("profile.profileSalary", "profileSalary")
// .where({ citizenId: "3101702379675" })
.where("profileSalary.id IS NULL")
// .skip(0)
// .take(10000)
.getManyAndCount();
for await (const _item of profiles) {
// ดึงข้อมูลมาโดยไม่ใส่ order
const existingProfile = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: _item.citizenId, FLAG_PERSON_TYPE: "1" },
// นับจำนวน profiles ทั้งหมดก่อน
const profileRepo = AppDataSource.getRepository(Profile);
const totalProfiles = await profileRepo.count();
console.log(
`Starting OPTIMIZED batch processing: ${totalProfiles} profiles, batch size: ${batchSize}`,
);
for (let offset = 0; offset < totalProfiles; offset += batchSize) {
const profiles = await profileRepo
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
.skip(offset)
.take(batchSize)
.getMany();
console.log(
`Processing batch ${Math.floor(offset / batchSize) + 1}/${Math.ceil(totalProfiles / batchSize)} - Querying ${profiles.length} profiles`,
);
const batchRecords = []; // Array สำหรับเก็บ records ที่จะ batch insert
// OPTIMIZATION 1: ดึงข้อมูล HR ทั้งหมดของ batch ในครั้งเดียว
const citizenIds = profiles.map((p) => p.citizenId);
const allHrData = await this.HR_POSITION_OFFICERRepo.createQueryBuilder("hr")
.where("hr.CIT IN (:...citizenIds)", { citizenIds })
.andWhere("hr.FLAG_PERSON_TYPE = :flag", { flag: "1" })
.orderBy("hr.CIT", "ASC")
.addOrderBy(
"CASE WHEN hr.MP_POS_DATE IS NULL OR hr.MP_POS_DATE = '' THEN 1 ELSE 0 END",
"ASC",
)
.addOrderBy("STR_TO_DATE(SUBSTRING_INDEX(hr.MP_POS_DATE, ' ', 1), '%d/%m/%Y')", "ASC")
.addOrderBy("hr.ORDER_MOVE_POSITION", "ASC")
.getMany();
// จัดกลุ่มข้อมูล HR ตาม citizenId
const hrDataByCitizenId = new Map();
allHrData.forEach((hr) => {
if (!hrDataByCitizenId.has(hr.CIT)) {
hrDataByCitizenId.set(hr.CIT, []);
}
hrDataByCitizenId.get(hr.CIT).push(hr);
});
// sort ด้วย JavaScript
existingProfile.sort((a, b) => {
let dateA = new Date().getTime();
let dateB = new Date().getTime();
if (a.MP_POS_DATE) {
const [datePart] = a.MP_POS_DATE.split(" ");
const [day, month, year] = datePart.split("/");
dateA = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime();
}
if (b.MP_POS_DATE) {
const [datePart] = b.MP_POS_DATE.split(" ");
const [day, month, year] = datePart.split("/");
dateB = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime();
}
if (dateA !== dateB) {
return dateA - dateB; // ASC
}
return a.ORDER_MOVE_POSITION - b.ORDER_MOVE_POSITION; // ASC
// OPTIMIZATION 2: ดึง existing ProfileSalary ทั้งหมดของ batch ในครั้งเดียว
const allHrIds = allHrData.map((hr) => hr.id.toString());
const existingSalaries = await this.salaryRepo.find({
where: { refId: In(allHrIds) },
select: ["refId"],
});
const existingSalaryRefIds = new Set(existingSalaries.map((s) => s.refId));
let order = 1;
for await (const item of existingProfile) {
rowCount++;
const profileSalary: any = new ProfileSalary();
profileSalary.profileId = _item.id;
profileSalary.order = order;
order = order + 1;
profileSalary.commandNo = isNaN(item.MP_COMMAND_NUM) ? null : item.MP_COMMAND_NUM;
profileSalary.commandYear = isNaN(item.MP_COMMAND_NUM)
? null
: item.CUR_YEAR > 2500
? item.CUR_YEAR - 543
: item.CUR_YEAR;
for (const _item of profiles) {
const existingProfile = hrDataByCitizenId.get(_item.citizenId) || [];
let MP_COMMAND_DATE = "";
if (item.MP_COMMAND_DATE) {
const [datePart] = item.MP_COMMAND_DATE.split(" ");
const [day, month, year] = datePart.split("/");
MP_COMMAND_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
}
let MP_POS_DATE = "";
if (item.MP_POS_DATE) {
const [datePart] = item.MP_POS_DATE.split(" ");
const [day, month, year] = datePart.split("/");
MP_POS_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
}
profileSalary.commandDateSign = MP_COMMAND_DATE == null ? _null : new Date(MP_COMMAND_DATE);
profileSalary.commandDateAffect = MP_POS_DATE == null ? _null : new Date(MP_POS_DATE);
if (
[
"0",
"11",
"22",
"31",
"39",
"45",
"46",
"47",
"49",
"50",
"51",
"60",
"61",
"62",
"99",
].includes(item.FLAG_TO_NAME_CODE)
) {
profileSalary.commandCode = "0";
profileSalary.commandName = "อื่น ๆ";
} else if (["1", "58"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "1";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้สอบแข่งขันได้";
} else if (["23"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "2";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้ได้รับคัดเลือก";
} else if (["3", "6", "34", "36", "37"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "3";
profileSalary.commandName = "แต่งตั้ง ย้าย";
} else if (["10", "55", "56"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "4";
profileSalary.commandName = "เลื่อน";
} else if (["14"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "5";
profileSalary.commandName = "เลื่อนเงินเดือนตามปกติ";
} else if (
["8", "20", "24", "25", "43", "44", "52", "66", "67"].includes(item.FLAG_TO_NAME_CODE)
) {
profileSalary.commandCode = "6";
profileSalary.commandName = "เลื่อนเงินเดือนกรณีอื่น ๆ";
} else if (["-"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "7";
profileSalary.commandName = "เงินพิเศษอื่น ๆ";
} else if (["38", "40", "53", "54"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "8";
profileSalary.commandName = "ปรับโครงสร้าง";
} else if (["12"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "9";
profileSalary.commandName = "พ้นทดลองปฏิบัติราชการ";
} else if (["2", "18"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "10";
profileSalary.commandName = "บรรจุกลับ";
} else if (["4", "32", "33"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "11";
profileSalary.commandName = "รับโอน";
} else if (["5"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "12";
profileSalary.commandName = "ให้โอน";
} else if (["15", "95"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "13";
profileSalary.commandName = "แก้ไขคำสั่ง";
} else if (["19"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "14";
profileSalary.commandName = "ยกเลิกคำสั่ง";
} else if (["27", "35"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "15";
profileSalary.commandName = "ลาออกจากราชการ";
} else if (["13", "17", "21", "28", "29", "30", "59"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "16";
profileSalary.commandName = "พ้นจากราชการ";
} else if (["7", "9", "16", "26", "63", "68"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "17";
profileSalary.commandName = "รักษาราชการ, ช่วยราชการ";
}
if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ลาศึกษาต่อ") {
profileSalary.commandCode = "0";
profileSalary.commandName = "อื่น ๆ";
} else if (
item.FLAG_TO_NAME_CODE == null &&
(item.FLAG_TO_NAME == "เลื่อน 1 ขั้นและเลื่อนระดับ" ||
item.FLAG_TO_NAME == "เลื่อน 0.5 ขั้นและเลื่อนระดับ")
) {
profileSalary.commandCode = "4";
profileSalary.commandName = "เลื่อน";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "เลื่อนเงินเดือน") {
profileSalary.commandCode = "5";
profileSalary.commandName = "เลื่อนเงินเดือนตามปกติ";
} else if (
item.FLAG_TO_NAME_CODE == null &&
(item.FLAG_TO_NAME == "ปรับตามบัญชีเงินเดือนใหม่" ||
item.FLAG_TO_NAME == "เลื่อนเงินเดือน" ||
item.FLAG_TO_NAME == "ปรับเงินเดือนตาม กพ.")
) {
profileSalary.commandCode = "6";
profileSalary.commandName = "เลื่อนเงินเดือนกรณีอื่น ๆ";
} else if (
item.FLAG_TO_NAME_CODE == null &&
item.FLAG_TO_NAME == "แต่งตั้งตามการปรับปรุงโครงฯ"
) {
profileSalary.commandCode = "8";
profileSalary.commandName = "ปรับโครงสร้าง";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "พ้นทดลองปฏิบัติราชการ") {
profileSalary.commandCode = "9";
profileSalary.commandName = "พ้นทดลองปฏิบัติราชการ";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ให้โอนมา") {
profileSalary.commandCode = "11";
profileSalary.commandName = "รับโอน";
} else if (
item.FLAG_TO_NAME_CODE == null &&
item.FLAG_TO_NAME == "โอนไปปฏิบัติราชการที่อื่น"
) {
profileSalary.commandCode = "12";
profileSalary.commandName = "ให้โอน";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ยกเลิกคำสั่ง") {
profileSalary.commandCode = "14";
profileSalary.commandName = "ยกเลิกคำสั่ง";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "รักษาการในตำแหน่ง") {
profileSalary.commandCode = "17";
profileSalary.commandName = "รักษาราชการ, ช่วยราชการ";
}
if (
(profileSalary.commandCode == null || profileSalary.commandCode == undefined) &&
(profileSalary.commandName == null || profileSalary.commandName == undefined)
) {
let order = 1;
for (const item of existingProfile) {
rowCount++;
// ใช้ Set lookup แทนการ query database
if (existingSalaryRefIds.has(item.id.toString())) continue;
const profileSalary: any = new ProfileSalary();
profileSalary.profileId = _item.id;
profileSalary.order = order;
order = order + 1;
profileSalary.commandNo = isNaN(item.MP_COMMAND_NUM) ? null : item.MP_COMMAND_NUM;
// แก้ไข logic การกำหนด commandYear
if (isNaN(item.CUR_YEAR) || item.CUR_YEAR == null) {
profileSalary.commandYear = null;
} else {
profileSalary.commandYear = item.CUR_YEAR > 2500 ? item.CUR_YEAR - 543 : item.CUR_YEAR;
}
let MP_COMMAND_DATE = "";
if (item.MP_COMMAND_DATE && item.MP_COMMAND_DATE.trim() !== "") {
try {
const [datePart] = item.MP_COMMAND_DATE.split(" ");
const [day, month, year] = datePart.split("/");
if (day && month && year) {
MP_COMMAND_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
}
} catch (error) {
console.warn(`Invalid MP_COMMAND_DATE format: ${item.MP_COMMAND_DATE}`);
}
}
let MP_POS_DATE = "";
if (item.MP_POS_DATE && item.MP_POS_DATE.trim() !== "") {
try {
const [datePart] = item.MP_POS_DATE.split(" ");
const [day, month, year] = datePart.split("/");
if (day && month && year) {
MP_POS_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
}
} catch (error) {
console.warn(`Invalid MP_POS_DATE format: ${item.MP_POS_DATE}`);
}
}
profileSalary.commandDateSign =
MP_COMMAND_DATE == null ? _null : new Date(MP_COMMAND_DATE);
profileSalary.commandDateAffect = MP_POS_DATE == null ? _null : new Date(MP_POS_DATE);
if (
[
"อื่นๆ",
"กลับไปปฏิบัติงานทางต้นสังกัด",
"เปลี่ยนประเภทข้าราชการ",
"โอนสับเปลี่ยน",
"เข้ารับฝึกอบรม",
"ดูงาน",
"ศึกษาต่อ",
"ขยายเวลาเข้ารับการฝึกอบรม",
"ขยายเวลาศึกษาต่อ",
"รายงานตัวกลับเข้าปฏิบัติราชการ",
"ไม่ได้เลื่อนขั้น",
"ตัดเงินเดือน",
"ลดขั้นเงินเดือน",
"ให้ข้าราชการกลับเข้ารับราชการ",
"ไม่ระบุ",
].includes(item.FLAG_TO_NAME)
"0",
"11",
"22",
"31",
"39",
"45",
"46",
"47",
"49",
"50",
"51",
"60",
"61",
"62",
"99",
].includes(item.FLAG_TO_NAME_CODE)
) {
profileSalary.commandCode = "0";
profileSalary.commandName = "อื่น ๆ";
} else if (
[
"บรรจุและแต่งตั้งผู้สอบแข่งขันได้",
"ทดลองปฎิบัติราชการ",
"ทดลองปฏิบัติราชการและปรับวุฒิ",
"บรรจุใหม่",
].includes(item.FLAG_TO_NAME)
) {
} else if (["1", "58"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "1";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้สอบแข่งขันได้";
} else if (["บรรจุและแต่งตั้งผู้ได้รับการคัดเลือก"].includes(item.FLAG_TO_NAME)) {
} else if (["23"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "2";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้ได้รับคัดเลือก";
} else if (
[
"แต่งตั้ง (ย้ายสับเปลี่ยน)",
"แต่งตั้ง (ย้าย)",
"แต่งตั้ง",
"เปลี่ยนสายงาน",
"เปลี่ยนตำแหน่ง",
"ตัดโอนตำแหน่ง",
].includes(item.FLAG_TO_NAME)
) {
} else if (["3", "6", "34", "36", "37"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "3";
profileSalary.commandName = "แต่งตั้ง ย้าย";
} else if (
["เลื่อนและแต่งตั้ง", "เลื่อนระดับ", "เลื่อนเงินเดือนและระดับ"].includes(
item.FLAG_TO_NAME,
)
) {
} else if (["10", "55", "56"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "4";
profileSalary.commandName = "เลื่อน";
} else if (["เลื่อนขั้นเงินเดือน", "เลื่อนเงินเดือน"].includes(item.FLAG_TO_NAME)) {
} else if (["14"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "5";
profileSalary.commandName = "เลื่อนเงินเดือนตามปกติ";
} else if (
[
"ปรับเงินเดือนตามคุณวุฒิ",
"ได้รับเงินตอบแทนพิเศษ",
"เงินเพิ่มการครองชีพชั่วคราว",
"เลื่อนขั้นเงินเดือนกรณีพิเศษ",
"ปรับอัตราเงินเดือนตามบัญชีอัตราเงินเดือนใหม่ ท้าย พ.ร.บ. เงินเดือนและเงินประจำตำ",
"ปรับอัตราเงินเดือนตามพระราชกฤษฎีกา การปรับอัตราเงินเดือนของข้าราชการ",
"เลื่อนขั้นเงินเดือน (เพิ่มเติม)",
"ปรับอัตราเงินเดือน",
"ให้ข้าราชการได้รับเงินเดือนตามคุณวุฒิ",
].includes(item.FLAG_TO_NAME)
["8", "20", "24", "25", "43", "44", "52", "66", "67"].includes(item.FLAG_TO_NAME_CODE)
) {
profileSalary.commandCode = "6";
profileSalary.commandName = "เลื่อนเงินเดือนกรณีอื่น ๆ";
} else if (["--"].includes(item.FLAG_TO_NAME)) {
} else if (["-"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "7";
profileSalary.commandName = "เงินพิเศษอื่น ๆ";
} else if (["38", "40", "53", "54"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "8";
profileSalary.commandName = "ปรับโครงสร้าง";
} else if (["12"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "9";
profileSalary.commandName = "พ้นทดลองปฏิบัติราชการ";
} else if (["2", "18"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "10";
profileSalary.commandName = "บรรจุกลับ";
} else if (["4", "32", "33"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "11";
profileSalary.commandName = "รับโอน";
} else if (["5"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "12";
profileSalary.commandName = "ให้โอน";
} else if (["15", "95"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "13";
profileSalary.commandName = "แก้ไขคำสั่ง";
} else if (["19"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "14";
profileSalary.commandName = "ยกเลิกคำสั่ง";
} else if (["27", "35"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "15";
profileSalary.commandName = "ลาออกจากราชการ";
} else if (["13", "17", "21", "28", "29", "30", "59"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "16";
profileSalary.commandName = "พ้นจากราชการ";
} else if (["7", "9", "16", "26", "63", "68"].includes(item.FLAG_TO_NAME_CODE)) {
profileSalary.commandCode = "17";
profileSalary.commandName = "รักษาราชการ, ช่วยราชการ";
}
if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ลาศึกษาต่อ") {
profileSalary.commandCode = "0";
profileSalary.commandName = "อื่น ๆ";
} else if (
["ปรับโครงสร้าง", "แต่งตั้ง (จัดคนลงกรอบ)", "แต่งตั้งตามแผนอัตรากำลังฯ"].includes(
item.FLAG_TO_NAME,
)
item.FLAG_TO_NAME_CODE == null &&
(item.FLAG_TO_NAME == "เลื่อน 1 ขั้นและเลื่อนระดับ" ||
item.FLAG_TO_NAME == "เลื่อน 0.5 ขั้นและเลื่อนระดับ")
) {
profileSalary.commandCode = "4";
profileSalary.commandName = "เลื่อน";
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "เลื่อนเงินเดือน") {
profileSalary.commandCode = "5";
profileSalary.commandName = "เลื่อนเงินเดือนตามปกติ";
} else if (
item.FLAG_TO_NAME_CODE == null &&
(item.FLAG_TO_NAME == "ปรับตามบัญชีเงินเดือนใหม่" ||
item.FLAG_TO_NAME == "เลื่อนเงินเดือน" ||
item.FLAG_TO_NAME == "ปรับเงินเดือนตาม กพ.")
) {
profileSalary.commandCode = "6";
profileSalary.commandName = "เลื่อนเงินเดือนกรณีอื่น ๆ";
} else if (
item.FLAG_TO_NAME_CODE == null &&
item.FLAG_TO_NAME == "แต่งตั้งตามการปรับปรุงโครงฯ"
) {
profileSalary.commandCode = "8";
profileSalary.commandName = "ปรับโครงสร้าง";
} else if (["พ้นทดลองปฏิบัติราชการ"].includes(item.FLAG_TO_NAME)) {
} else if (
item.FLAG_TO_NAME_CODE == null &&
item.FLAG_TO_NAME == "พ้นทดลองปฏิบัติราชการ"
) {
profileSalary.commandCode = "9";
profileSalary.commandName = "พ้นทดลองปฏิบัติราชการ";
} else if (
[
"บรรจุกลับ",
"บรรจุกลับข้าราชการ",
"บรรจุและแต่งตั้งผู้ไปรับราชการทหารกลับเข้ารับราชการ",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "10";
profileSalary.commandName = "บรรจุกลับ";
} else if (
[
"รับโอนข้าราชการตามกฎหมายอื่น",
"รับโอนข้าราชการตามกฎหมายอื่น ผู้สอบแข่งขันได้",
"รับโอนข้าราชการตามกฏหมายอื่น โดยการคัดเลือก",
].includes(item.FLAG_TO_NAME)
) {
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ให้โอนมา") {
profileSalary.commandCode = "11";
profileSalary.commandName = "รับโอน";
} else if (["ให้โอน"].includes(item.FLAG_TO_NAME)) {
} else if (
item.FLAG_TO_NAME_CODE == null &&
item.FLAG_TO_NAME == "โอนไปปฏิบัติราชการที่อื่น"
) {
profileSalary.commandCode = "12";
profileSalary.commandName = "ให้โอน";
} else if (["แก้ไขคำสั่ง"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "13";
profileSalary.commandName = "แก้ไขคำสั่ง";
} else if (["ยกเลิกคำสั่ง"].includes(item.FLAG_TO_NAME)) {
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "ยกเลิกคำสั่ง") {
profileSalary.commandCode = "14";
profileSalary.commandName = "ยกเลิกคำสั่ง";
} else if (
[
"ลาออกจากราชการ",
"พ้นจากราชการ/ลาออกจากราชการตามมาตรการพัฒนาและบริหารกำลังคน",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "15";
profileSalary.commandName = "ลาออกจากราชการ";
} else if (
[
"พ้นจากราชการ/เพื่อไปปฏิบัติราชการทหาร",
"เกษียณ",
"ไม่พ้นทดลองปฏิบัติราชการ",
"พ้นจากราชการ/ให้ออก",
"พ้นจากราชการ/ไล่ออก",
"พ้นจากราชการ/เสียชีวิต",
"พ้นจากราชการ/ปลดออก",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "16";
profileSalary.commandName = "พ้นจากราชการ";
} else if (
[
"แต่งตั้งข้าราชการรักษาราชการแทน",
"ช่วยราชการ",
"รักษาการ",
"รักษาราชการแทน",
"มอบหมายให้ปฏิบัติหน้าที่",
"มอบหมายข้าราชการปฏิบัติหน้าที่แทน",
].includes(item.FLAG_TO_NAME)
) {
} else if (item.FLAG_TO_NAME_CODE == null && item.FLAG_TO_NAME == "รักษาการในตำแหน่ง") {
profileSalary.commandCode = "17";
profileSalary.commandName = "รักษาราชการ, ช่วยราชการ";
} else {
profileSalary.commandCode = "0";
profileSalary.commandName = item.FLAG_TO_NAME;
}
}
profileSalary.posNoAbb = item.POS_NUM_NAME;
profileSalary.posNo = item.POS_NUM_CODE;
profileSalary.positionName = item.WORK_LINE_NAME;
var positionType = _null;
var positionLevel = _null;
if (item.MP_CEE == "21") {
positionType = "ทั่วไป";
positionLevel = "ปฏิบัติงาน";
} else if (item.MP_CEE == "22") {
positionType = "ทั่วไป";
positionLevel = "ชำนาญงาน";
} else if (item.MP_CEE == "23") {
positionType = "ทั่วไป";
positionLevel = "อาวุโส";
} else if (item.MP_CEE == "24") {
positionType = "ทั่วไป";
positionLevel = "อาวุโสเฉพาะสายงานที่กำหนด";
} else if (item.MP_CEE == "25") {
positionType = "ทั่วไป";
positionLevel = "ทักษะพิเศษ";
} else if (item.MP_CEE == "26") {
positionType = "วิชาการ";
positionLevel = "ปฏิบัติการ";
} else if (item.MP_CEE == "27") {
positionType = "วิชาการ";
positionLevel = "ชำนาญการ";
} else if (item.MP_CEE == "28") {
positionType = "วิชาการ";
positionLevel = "ชำนาญการพิเศษ";
} else if (item.MP_CEE == "29") {
positionType = "วิชาการ";
positionLevel = "เชี่ยวชาญ";
} else if (item.MP_CEE == "30") {
positionType = "วิชาการ";
positionLevel = "ทรงคุณวุฒิ";
} else if (item.MP_CEE == "31") {
positionType = "วิชาการ";
positionLevel = "ทรงคุณวุฒิเฉพาะสายงานที่กำหนด";
} else if (item.MP_CEE == "32") {
positionType = "อำนวยการ";
positionLevel = "ต้น";
} else if (item.MP_CEE == "33") {
positionType = "อำนวยการ";
positionLevel = "สูง";
} else if (item.MP_CEE == "34") {
positionType = "บริหาร";
positionLevel = "ต้น";
} else if (item.MP_CEE == "35") {
positionType = "บริหาร";
positionLevel = "สูง";
} else {
profileSalary.positionCee = item.MP_CEE;
}
profileSalary.positionType = positionType;
profileSalary.positionLevel = positionLevel;
profileSalary.orgRoot = item.DEPARTMENT_NAME;
profileSalary.orgChild1 = item.DIVISION_NAME;
profileSalary.orgChild2 = item.SECTION_NAME;
profileSalary.orgChild3 = item.JOB_NAME;
if (item.DEPARTMENT_CODE == "50") {
profileSalary.orgRoot = item.DIVISION_NAME;
profileSalary.orgChild1 = item.SECTION_NAME;
profileSalary.orgChild2 = item.JOB_NAME;
}
profileSalary.positionExecutive = item.ADMIN_NAME ?? _null;
profileSalary.amount = isNaN(item.SALARY) ? null : item.SALARY;
profileSalary.remark = item.REMARK;
profileSalary.refId = item.id;
profileSalary.isEntry = false;
if (
(profileSalary.commandCode == null || profileSalary.commandCode == undefined) &&
(profileSalary.commandName == null || profileSalary.commandName == undefined)
) {
if (
[
"อื่นๆ",
"กลับไปปฏิบัติงานทางต้นสังกัด",
"เปลี่ยนประเภทข้าราชการ",
"โอนสับเปลี่ยน",
"เข้ารับฝึกอบรม",
"ดูงาน",
"ศึกษาต่อ",
"ขยายเวลาเข้ารับการฝึกอบรม",
"ขยายเวลาศึกษาต่อ",
"รายงานตัวกลับเข้าปฏิบัติราชการ",
"ไม่ได้เลื่อนขั้น",
"ตัดเงินเดือน",
"ลดขั้นเงินเดือน",
"ให้ข้าราชการกลับเข้ารับราชการ",
"ไม่ระบุ",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "0";
profileSalary.commandName = "อื่น ๆ";
} else if (
[
"บรรจุและแต่งตั้งผู้สอบแข่งขันได้",
"ทดลองปฎิบัติราชการ",
"ทดลองปฏิบัติราชการและปรับวุฒิ",
"บรรจุใหม่",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "1";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้สอบแข่งขันได้";
} else if (["บรรจุและแต่งตั้งผู้ได้รับการคัดเลือก"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "2";
profileSalary.commandName = "บรรจุและแต่งตั้งผู้ได้รับคัดเลือก";
} else if (
[
"แต่งตั้ง (ย้ายสับเปลี่ยน)",
"แต่งตั้ง (ย้าย)",
"แต่งตั้ง",
"เปลี่ยนสายงาน",
"เปลี่ยนตำแหน่ง",
"ตัดโอนตำแหน่ง",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "3";
profileSalary.commandName = "แต่งตั้ง ย้าย";
} else if (
["เลื่อนและแต่งตั้ง", "เลื่อนระดับ", "เลื่อนเงินเดือนและระดับ"].includes(
item.FLAG_TO_NAME,
)
) {
profileSalary.commandCode = "4";
profileSalary.commandName = "เลื่อน";
} else if (["เลื่อนขั้นเงินเดือน", "เลื่อนเงินเดือน"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "5";
profileSalary.commandName = "เลื่อนเงินเดือนตามปกติ";
} else if (
[
"ปรับเงินเดือนตามคุณวุฒิ",
"ได้รับเงินตอบแทนพิเศษ",
"เงินเพิ่มการครองชีพชั่วคราว",
"เลื่อนขั้นเงินเดือนกรณีพิเศษ",
"ปรับอัตราเงินเดือนตามบัญชีอัตราเงินเดือนใหม่ ท้าย พ.ร.บ. เงินเดือนและเงินประจำตำ",
"ปรับอัตราเงินเดือนตามพระราชกฤษฎีกา การปรับอัตราเงินเดือนของข้าราชการ",
"เลื่อนขั้นเงินเดือน (เพิ่มเติม)",
"ปรับอัตราเงินเดือน",
"ให้ข้าราชการได้รับเงินเดือนตามคุณวุฒิ",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "6";
profileSalary.commandName = "เลื่อนเงินเดือนกรณีอื่น ๆ";
} else if (["--"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "7";
profileSalary.commandName = "เงินพิเศษอื่น ๆ";
} else if (
["ปรับโครงสร้าง", "แต่งตั้ง (จัดคนลงกรอบ)", "แต่งตั้งตามแผนอัตรากำลังฯ"].includes(
item.FLAG_TO_NAME,
)
) {
profileSalary.commandCode = "8";
profileSalary.commandName = "ปรับโครงสร้าง";
} else if (["พ้นทดลองปฏิบัติราชการ"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "9";
profileSalary.commandName = "พ้นทดลองปฏิบัติราชการ";
} else if (
[
"บรรจุกลับ",
"บรรจุกลับข้าราชการ",
"บรรจุและแต่งตั้งผู้ไปรับราชการทหารกลับเข้ารับราชการ",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "10";
profileSalary.commandName = "บรรจุกลับ";
} else if (
[
"รับโอนข้าราชการตามกฎหมายอื่น",
"รับโอนข้าราชการตามกฎหมายอื่น ผู้สอบแข่งขันได้",
"รับโอนข้าราชการตามกฏหมายอื่น โดยการคัดเลือก",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "11";
profileSalary.commandName = "รับโอน";
} else if (["ให้โอน"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "12";
profileSalary.commandName = "ให้โอน";
} else if (["แก้ไขคำสั่ง"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "13";
profileSalary.commandName = "แก้ไขคำสั่ง";
} else if (["ยกเลิกคำสั่ง"].includes(item.FLAG_TO_NAME)) {
profileSalary.commandCode = "14";
profileSalary.commandName = "ยกเลิกคำสั่ง";
} else if (
[
"ลาออกจากราชการ",
"พ้นจากราชการ/ลาออกจากราชการตามมาตรการพัฒนาและบริหารกำลังคน",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "15";
profileSalary.commandName = "ลาออกจากราชการ";
} else if (
[
"พ้นจากราชการ/เพื่อไปปฏิบัติราชการทหาร",
"เกษียณ",
"ไม่พ้นทดลองปฏิบัติราชการ",
"พ้นจากราชการ/ให้ออก",
"พ้นจากราชการ/ไล่ออก",
"พ้นจากราชการ/เสียชีวิต",
"พ้นจากราชการ/ปลดออก",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "16";
profileSalary.commandName = "พ้นจากราชการ";
} else if (
[
"แต่งตั้งข้าราชการรักษาราชการแทน",
"ช่วยราชการ",
"รักษาการ",
"รักษาราชการแทน",
"มอบหมายให้ปฏิบัติหน้าที่",
"มอบหมายข้าราชการปฏิบัติหน้าที่แทน",
].includes(item.FLAG_TO_NAME)
) {
profileSalary.commandCode = "17";
profileSalary.commandName = "รักษาราชการ, ช่วยราชการ";
} else {
profileSalary.commandCode = "0";
profileSalary.commandName = item.FLAG_TO_NAME;
}
}
profileSalary.posNoAbb = item.POS_NUM_NAME;
profileSalary.posNo = item.POS_NUM_CODE;
profileSalary.positionName = item.WORK_LINE_NAME;
var positionType = _null;
var positionLevel = _null;
if (item.MP_CEE == "21") {
positionType = "ทั่วไป";
positionLevel = "ปฏิบัติงาน";
} else if (item.MP_CEE == "22") {
positionType = "ทั่วไป";
positionLevel = "ชำนาญงาน";
} else if (item.MP_CEE == "23") {
positionType = "ทั่วไป";
positionLevel = "อาวุโส";
} else if (item.MP_CEE == "24") {
positionType = "ทั่วไป";
positionLevel = "อาวุโสเฉพาะสายงานที่กำหนด";
} else if (item.MP_CEE == "25") {
positionType = "ทั่วไป";
positionLevel = "ทักษะพิเศษ";
} else if (item.MP_CEE == "26") {
positionType = "วิชาการ";
positionLevel = "ปฏิบัติการ";
} else if (item.MP_CEE == "27") {
positionType = "วิชาการ";
positionLevel = "ชำนาญการ";
} else if (item.MP_CEE == "28") {
positionType = "วิชาการ";
positionLevel = "ชำนาญการพิเศษ";
} else if (item.MP_CEE == "29") {
positionType = "วิชาการ";
positionLevel = "เชี่ยวชาญ";
} else if (item.MP_CEE == "30") {
positionType = "วิชาการ";
positionLevel = "ทรงคุณวุฒิ";
} else if (item.MP_CEE == "31") {
positionType = "วิชาการ";
positionLevel = "ทรงคุณวุฒิเฉพาะสายงานที่กำหนด";
} else if (item.MP_CEE == "32") {
positionType = "อำนวยการ";
positionLevel = "ต้น";
} else if (item.MP_CEE == "33") {
positionType = "อำนวยการ";
positionLevel = "สูง";
} else if (item.MP_CEE == "34") {
positionType = "บริหาร";
positionLevel = "ต้น";
} else if (item.MP_CEE == "35") {
positionType = "บริหาร";
positionLevel = "สูง";
} else {
profileSalary.positionCee = item.MP_CEE;
}
profileSalary.positionType = positionType;
profileSalary.positionLevel = positionLevel;
profileSalary.orgRoot = item.DEPARTMENT_NAME;
profileSalary.orgChild1 = item.DIVISION_NAME;
profileSalary.orgChild2 = item.SECTION_NAME;
profileSalary.orgChild3 = item.JOB_NAME;
if (item.DEPARTMENT_CODE == "50") {
profileSalary.orgRoot = item.DIVISION_NAME;
profileSalary.orgChild1 = item.SECTION_NAME;
profileSalary.orgChild2 = item.JOB_NAME;
}
profileSalary.positionExecutive = item.ADMIN_NAME ?? _null;
profileSalary.amount = isNaN(item.SALARY) ? null : item.SALARY;
profileSalary.remark = item.REMARK;
profileSalary.refId = item.id;
profileSalary.isEntry = false;
const sal_pos_amount_1: any =
item.SAL_POS_AMOUNT_1 == null || item.SAL_POS_AMOUNT_1 == ""
? _null
: Number(item.SAL_POS_AMOUNT_1);
const sal_pos_amount_2: any =
item.SAL_POS_AMOUNT_2 == null || item.SAL_POS_AMOUNT_2 == ""
? _null
: Number(item.SAL_POS_AMOUNT_2);
profileSalary.positionSalaryAmount = sal_pos_amount_1 ?? sal_pos_amount_2;
const special_amt: any =
item.SPECIAL_AMT == null || item.SPECIAL_AMT == "" ? _null : Number(item.SPECIAL_AMT);
profileSalary.amountSpecial = special_amt;
profileSalary.posNumCodeSit = item.POS_NUM_CODE_SIT;
profileSalary.posNumCodeSitAbb = item.POS_NUM_CODE_SIT_ABB;
const sal_pos_amount_1: any =
item.SAL_POS_AMOUNT_1 == null || item.SAL_POS_AMOUNT_1 == ""
? _null
: isNaN(Number(item.SAL_POS_AMOUNT_1))
? _null
: Number(item.SAL_POS_AMOUNT_1);
const sal_pos_amount_2: any =
item.SAL_POS_AMOUNT_2 == null || item.SAL_POS_AMOUNT_2 == ""
? _null
: isNaN(Number(item.SAL_POS_AMOUNT_2))
? _null
: Number(item.SAL_POS_AMOUNT_2);
profileSalary.positionSalaryAmount = sal_pos_amount_1 ?? sal_pos_amount_2;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalary.createdAt = new Date().toISOString().split("T")[0];
profileSalary.lastUpdatedAt = new Date().toISOString().split("T")[0];
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
const special_amt: any =
item.SPECIAL_AMT == null || item.SPECIAL_AMT == ""
? _null
: isNaN(Number(item.SPECIAL_AMT))
? _null
: Number(item.SPECIAL_AMT);
profileSalary.amountSpecial = special_amt;
profileSalary.posNumCodeSit = item.POS_NUM_CODE_SIT;
profileSalary.posNumCodeSitAbb = item.POS_NUM_CODE_SIT_ABB;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalary.createdAt = new Date().toISOString().split("T")[0];
profileSalary.lastUpdatedAt = new Date().toISOString().split("T")[0];
// เพิ่มลงใน batch array แทนการ save ทันที
batchRecords.push(profileSalary);
}
order = 1;
}
// Batch insert สำหรับ batch นี้
if (batchRecords.length > 0) {
try {
await this.salaryRepo.save(profileSalary);
// Debug: ตรวจสอบ NaN values ก่อน save
const hasNaN = batchRecords.some((record) => {
return Object.values(record).some((value) => typeof value === "number" && isNaN(value));
});
if (hasNaN) {
console.warn(
`Warning: Found NaN values in batch ${Math.floor(offset / batchSize) + 1}`,
);
// แทนที่ NaN ด้วย null
batchRecords.forEach((record) => {
Object.keys(record).forEach((key) => {
if (typeof record[key] === "number" && isNaN(record[key])) {
console.warn(`Replacing NaN in field ${key} with null`);
record[key] = null;
}
});
});
}
await this.salaryRepo.save(batchRecords);
console.log(
`Saved ${batchRecords.length} records for batch ${Math.floor(offset / batchSize) + 1}`,
);
} catch (error) {
console.error("Error executing function from controller:", item.CIT);
console.error(`Error saving batch ${Math.floor(offset / batchSize) + 1}:`, error);
}
}
order = 1;
}
console.log(`Total processed: ${rowCount} records`);
return new HttpSuccess();
}
@ -6226,4 +6290,26 @@ export class ImportDataController extends Controller {
return groups;
}, {});
}
@Post("updateCommandYearNull")
async updateCommandYearNull(@Request() request: { user: Record<string, any> }) {
const profiles = await this.salaryRepo.find({
where: {
createdAt: Between(
new Date("2025-10-23T00:00:00.000Z"),
new Date("2025-10-25T00:00:00.000Z"),
),
commandYear: IsNull(),
},
});
for (const item of profiles) {
console.log(item.id);
if (item.commandDateAffect) {
item.commandYear = item.commandDateAffect.getFullYear();
await this.salaryRepo.save(item);
}
}
return new HttpSuccess();
}
}