hrms-api-org/src/controllers/ImportDataController.ts

1372 lines
54 KiB
TypeScript
Raw Normal View History

2024-10-18 11:33:04 +07:00
import { Controller, Post, Route, Security, Tags, Request, UploadedFile } from "tsoa";
2024-06-06 11:15:26 +07:00
import { AppDataSource } from "../database/data-source";
2024-10-18 11:33:04 +07:00
import { In, Not } from "typeorm";
2024-06-06 11:15:26 +07:00
import HttpSuccess from "../interfaces/http-success";
import { UseInterceptors } from "@nestjs/common";
2024-06-10 17:11:54 +07:00
import { Profile } from "../entities/Profile";
2024-07-26 14:41:41 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee";
2024-06-10 17:11:54 +07:00
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
import { ProfileFamilyMother } from "../entities/ProfileFamilyMother";
import { ProfileFamilyFather } from "../entities/ProfileFamilyFather";
2024-07-26 14:41:41 +07:00
import { ProfileEducation } from "../entities/ProfileEducation";
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
2024-07-26 14:41:41 +07:00
import { FileInterceptor } from "@nestjs/platform-express";
import * as xlsx from "xlsx";
2024-07-25 16:20:48 +07:00
import Extension from "../interfaces/extension";
2024-10-18 11:33:04 +07:00
import { calculateRetireDate, calculateRetireLaw } from "../interfaces/utils";
2024-07-26 14:44:33 +07:00
import * as fs from "fs";
import * as path from "path";
const { createConnection } = require("typeorm");
import csvParser from "csv-parser";
import { HR_POSITION_OFFICER } from "../entities/HR_POSITION_OFFICER";
import { HR_PERSONAL_OFFICER_FAMILY } from "../entities/HR_PERSONAL_OFFICER_FAMILY";
const BATCH_SIZE = 1000;
// import { EducationMis } from "../entities/EducationMis";
2024-07-25 16:20:48 +07:00
import { EducationMis } from "../entities/EducationMis";
2024-07-26 14:41:41 +07:00
import { ProvinceImport } from "../entities/ProvinceImport";
import { AmphurImport } from "../entities/AmphurImport";
import { SubDistrictImport } from "../entities/SubDistrictImport";
import { Province } from "../entities/Province";
import { District } from "../entities/District";
import { SubDistrict } from "../entities/SubDistrict";
2024-07-26 15:52:32 +07:00
import { HR_EDUCATION } from "../entities/HR_EDUCATION";
import { HR_PERSONAL_OFFICER_ADDRESS } from "../entities/HR_PERSONAL_OFFICER_ADDRESS";
import { HR_EDUCATION_EMP } from "../entities/HR_EDUCATION_EMP";
import { HR_PERSONAL_EMP_ADDRESS } from "../entities/HR_PERSONAL_EMP_ADDRESS";
import { HR_PERSONAL_EMP_FAMILY } from "../entities/HR_PERSONAL_EMP_FAMILY";
2024-06-06 11:15:26 +07:00
2024-06-06 11:20:37 +07:00
@Route("api/v1/org/upload")
@Tags("UPLOAD")
2024-06-06 11:15:26 +07:00
@Security("bearerAuth")
export class ImportDataController extends Controller {
2025-01-06 23:58:24 +07:00
private profileEducationRepo = AppDataSource.getRepository(ProfileEducation);
2024-06-10 17:11:54 +07:00
private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple);
private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother);
private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather);
private salaryRepository = AppDataSource.getRepository(ProfileSalary);
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
private posLevelRepo = AppDataSource.getRepository(PosLevel);
private posTypeRepo = AppDataSource.getRepository(PosType);
2024-07-26 14:44:33 +07:00
private HR_POSITION_OFFICERRepo = AppDataSource.getRepository(HR_POSITION_OFFICER);
private HR_PERSONAL_OFFICER_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_OFFICER_FAMILY);
2024-07-26 15:52:32 +07:00
private HR_EDUCATIONRepo = AppDataSource.getRepository(HR_EDUCATION);
private HR_PERSONAL_OFFICER_ADDRESSRepo = AppDataSource.getRepository(
HR_PERSONAL_OFFICER_ADDRESS,
);
private HR_EDUCATION_EMPRepo = AppDataSource.getRepository(HR_EDUCATION_EMP);
private HR_PERSONAL_EMP_ADDRESSRepo = AppDataSource.getRepository(HR_PERSONAL_EMP_ADDRESS);
private HR_PERSONAL_EMP_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_EMP_FAMILY);
2024-07-26 14:44:33 +07:00
2024-07-25 16:20:48 +07:00
private educationMisRepo = AppDataSource.getRepository(EducationMis);
2024-07-26 14:41:41 +07:00
private provincsRepo = AppDataSource.getRepository(ProvinceImport);
private amphurRepo = AppDataSource.getRepository(AmphurImport);
private subDistrictRepo = AppDataSource.getRepository(SubDistrictImport);
private provinceIdRepo = AppDataSource.getRepository(Province);
private districtIdRepo = AppDataSource.getRepository(District);
private subDistrictIdRepo = AppDataSource.getRepository(SubDistrict);
2024-06-06 11:15:26 +07:00
/**
2024-07-25 16:20:48 +07:00
* @summary
2024-06-06 11:15:26 +07:00
*/
2024-07-25 16:20:48 +07:00
@Post("uploadProfile-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSqlOfficer(@Request() request: { user: Record<string, any> }) {
let users = [];
let rowCount = 0;
const filePath = path.resolve(__dirname, "OFFICER.csv"); // Corrected file path
const readStream = fs.createReadStream(filePath).pipe(csvParser());
2024-06-10 17:11:54 +07:00
let profiles: any = [];
2024-07-25 16:20:48 +07:00
let null_: any = null;
2024-07-26 14:44:33 +07:00
for await (const item of readStream) {
// readStream.map(async (item: any) => {
rowCount++;
let type_: any = null;
let level_: any = null;
const profile = new Profile();
2024-07-31 15:34:50 +07:00
// if (item["FLAG_RETIRE_STATUSxxxx"] != "" && item["FLAG_RETIRE_STATUSxxxx"] != null) {
// continue;
// }
// if (item["FLAG_PERSON_TYPExxxx"] != "1") {
// continue;
// }
2024-09-18 19:18:16 +07:00
if (new Date(item["RET_YEAR"]).getFullYear() >= 2567) {
continue;
}
2024-07-31 09:43:23 +07:00
const existingProfile = await this.profileRepo.findOne({
where: { citizenId: item["ID"] },
});
if (existingProfile) {
2024-09-18 19:18:16 +07:00
profile.id = existingProfile.id;
// continue;
2024-07-31 09:43:23 +07:00
}
2024-07-26 14:44:33 +07:00
2024-07-31 15:34:50 +07:00
if (item["TYPE"]) {
2024-07-26 14:44:33 +07:00
type_ = await this.posTypeRepo.findOne({
2024-07-31 15:34:50 +07:00
where: { posTypeName: item["TYPE"] },
2024-06-10 17:11:54 +07:00
});
2024-07-26 14:44:33 +07:00
}
2024-07-31 15:34:50 +07:00
if (item["LEVEL"]) {
2024-07-26 14:44:33 +07:00
if (type_ == null) {
level_ = await this.posLevelRepo.findOne({
where: {
2024-07-31 15:34:50 +07:00
posLevelName: item["LEVEL"],
2024-07-26 14:44:33 +07:00
},
});
} else {
2024-07-25 16:20:48 +07:00
level_ = await this.posLevelRepo.findOne({
2024-07-26 14:44:33 +07:00
where: {
2024-07-31 15:34:50 +07:00
posLevelName: item["LEVEL"],
2024-07-26 14:44:33 +07:00
posTypeId: type_.id,
},
});
2024-07-25 16:20:48 +07:00
}
2024-07-26 14:44:33 +07:00
}
2024-09-18 19:18:16 +07:00
let dateRetire = new Date(item["BORN"]);
2024-07-26 14:44:33 +07:00
profile.citizenId = item["ID"] == "" ? "" : item["ID"];
2024-11-22 12:18:12 +07:00
profile.rank =
item["RANK_NAME"] == "" ||
item["RANK_NAME"] == "นาย" ||
item["RANK_NAME"] == "นาง" ||
item["RANK_NAME"] == "นางสาว"
? null
: item["RANK_NAME"];
2024-07-26 14:44:33 +07:00
profile.prefix = item["RANK_NAME"] == "" ? null : item["RANK_NAME"];
2024-11-22 12:18:12 +07:00
profile.prefixMain =
item["RANK_NAME"] == "" ||
(item["RANK_NAME"] != "นาย" && item["RANK_NAME"] != "นาง" && item["RANK_NAME"] != "นางสาว")
? null
: item["RANK_NAME"];
2024-07-26 14:44:33 +07:00
profile.firstName = item["FNAME"] == "" ? null : item["FNAME"];
profile.lastName = item["LNAME"] == "" ? null : item["LNAME"];
profile.gender = item["SEX"] == "1" ? "ชาย" : item["SEX"] == "2" ? "หญิง" : null_;
2024-09-18 19:18:16 +07:00
profile.birthDate = item["BORN"] == "" ? null_ : new Date(item["BORN"]);
2024-07-26 14:44:33 +07:00
profile.dateAppoint =
2024-09-18 19:18:16 +07:00
item["BEGIN_ENTRY_DATE"] == "" ? null_ : new Date(item["BEGIN_ENTRY_DATE"]);
2024-07-26 14:44:33 +07:00
profile.dateStart =
2024-09-18 19:18:16 +07:00
item["BEGIN_ENTRY_DATE"] == "" ? null_ : new Date(item["BEGIN_ENTRY_DATE"]);
2024-07-26 14:44:33 +07:00
profile.dateRetire = dateRetire == null ? null_ : calculateRetireDate(dateRetire);
profile.dateRetireLaw = dateRetire == null ? null_ : calculateRetireLaw(dateRetire);
profile.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"];
profile.posTypeId =
2024-07-31 15:34:50 +07:00
type_ != null && type_.posTypeName == item["TYPE"] && type_ ? type_.id : null;
2024-07-26 14:44:33 +07:00
profile.posLevelId =
2024-07-31 15:34:50 +07:00
level_ != null && level_.posLevelName == item["LEVEL"] && level_ ? level_.id : null;
2024-07-26 14:44:33 +07:00
profile.relationship =
2024-07-31 15:34:50 +07:00
item["สถานภาพ"] == "" ? "" : Extension.CheckRelationship(item["สถานภาพ"]);
// profile.isLeave =
// item["FLAG_RETIRE_STATUSxxxx"] == "" || item["FLAG_RETIRE_STATUSxxxx"] == null ? false : true;
2024-07-26 14:44:33 +07:00
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
profile.createdAt = new Date();
profile.lastUpdatedAt = new Date();
2024-09-18 19:18:16 +07:00
// profiles.push(profile);
2024-07-26 14:44:33 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-07-25 16:20:48 +07:00
2024-09-18 19:18:16 +07:00
// if (profiles.length === BATCH_SIZE) {
await this.profileRepo.save(profile);
// profiles = await [];
// if (global.gc) {
// global.gc();
// }
// }
2024-07-26 14:44:33 +07:00
}
// )
// );
2024-09-18 19:18:16 +07:00
// console.log(rowCount);
// await this.profileRepo.save(profiles);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfile-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQL(@Request() request: { user: Record<string, any> }) {
let users = [];
let rowCount = 0;
const filePath = path.resolve(__dirname, "EMP.csv"); // Corrected file path
const readStream = fs.createReadStream(filePath).pipe(csvParser());
2024-06-10 17:11:54 +07:00
let profiles: any = [];
2024-07-26 14:44:33 +07:00
let null_: any = null;
for await (const item of readStream) {
rowCount++;
const profileEmp = new ProfileEmployee();
2024-08-01 16:49:26 +07:00
// if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) {
// continue;
// }
2024-07-31 09:43:23 +07:00
if (item["FLAG_PERSON_TYPE"] != "6") {
continue;
}
2024-09-18 19:18:16 +07:00
if (new Date(item["RET_YEAR"]).getFullYear() >= 2567) {
continue;
}
2024-07-26 14:44:33 +07:00
const existingProfile = await this.profileEmpRepo.findOne({
where: { citizenId: item["ID"] },
});
if (existingProfile) {
2024-09-18 19:18:16 +07:00
profileEmp.id = existingProfile.id;
// continue;
2024-07-26 14:44:33 +07:00
}
2024-09-18 19:18:16 +07:00
let dateRetire = new Date(item["MP_FORCE_DATE"]);
2024-07-26 14:44:33 +07:00
profileEmp.citizenId = item["ID"] == "" ? "" : item["ID"];
profileEmp.employeeClass =
item["FLAG_PERSON_TYPE"] == "6" ? "PERM" : item["FLAG_PERSON_TYPE"] == "7" ? "TEMP" : "";
2024-11-22 12:18:12 +07:00
profileEmp.rank =
item["RANK_NAME"] == "" ||
item["RANK_NAME"] == "นาย" ||
item["RANK_NAME"] == "นาง" ||
item["RANK_NAME"] == "นางสาว"
? null
: item["RANK_NAME"];
2024-07-26 14:44:33 +07:00
profileEmp.prefix = item["RANK_NAME"] == "" ? null : item["RANK_NAME"];
2024-11-22 12:18:12 +07:00
profileEmp.prefixMain =
item["RANK_NAME"] == "" ||
(item["RANK_NAME"] != "นาย" && item["RANK_NAME"] != "นาง" && item["RANK_NAME"] != "นางสาว")
? null
: item["RANK_NAME"];
2024-07-26 14:44:33 +07:00
profileEmp.firstName = item["FNAME"] == "" ? null : item["FNAME"];
profileEmp.lastName = item["LNAME"] == "" ? null : item["LNAME"];
profileEmp.gender = item["SEX"] == "1" ? "ชาย" : item["SEX"] == "2" ? "หญิง" : null_;
2024-09-18 19:18:16 +07:00
profileEmp.birthDate = item["BORN"] == "" ? null_ : new Date(item["BORN"]);
2024-07-26 14:44:33 +07:00
profileEmp.dateAppoint =
2024-09-18 19:18:16 +07:00
item["BEGIN_ENTRY_DATE"] == "" ? null_ : new Date(item["BEGIN_ENTRY_DATE"]);
profileEmp.dateStart = item["MP_FORCE_DATE"] == "" ? null_ : new Date(item["MP_FORCE_DATE"]);
2024-07-26 14:44:33 +07:00
profileEmp.dateRetire = dateRetire == null ? null_ : calculateRetireDate(dateRetire);
profileEmp.dateRetireLaw = dateRetire == null ? null_ : calculateRetireLaw(dateRetire);
profileEmp.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"];
profileEmp.salaryLevel = item["SALARY_LEVEL_CODE"] == "" ? null : item["SALARY_LEVEL_CODE"];
profileEmp.relationship =
item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]);
profileEmp.createdUserId = request.user.sub;
profileEmp.createdFullName = request.user.name;
profileEmp.lastUpdateUserId = request.user.sub;
profileEmp.lastUpdateFullName = request.user.name;
profileEmp.createdAt = new Date();
profileEmp.lastUpdatedAt = new Date();
2024-09-18 19:18:16 +07:00
// profiles.push(profileEmp);
2024-07-26 14:44:33 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-09-18 19:18:16 +07:00
// if (profiles.length === BATCH_SIZE) {
await this.profileEmpRepo.save(profileEmp);
// profiles = await [];
// if (global.gc) {
// global.gc();
// }
// }
2024-07-26 14:44:33 +07:00
}
2024-09-18 19:18:16 +07:00
// console.log(rowCount);
// await this.profileEmpRepo.save(profiles);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileSalary-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSQLSalary(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(Profile)
2024-07-26 14:44:33 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 14:44:33 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(Profile)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
2024-07-26 14:44:33 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: _item.citizenId, FLAG_PERSON_TYPE: "1" },
2024-07-31 09:43:23 +07:00
select: [
"CIT",
"MP_POS_DATE",
"SALARY",
"MP_COMMAND_NUM",
"POS_NUM_NAME",
"POS_NUM_CODE",
"FLAG_TO_NAME",
"WORK_LINE_NAME",
"SPECIALIST_NAME",
"ADMIN_NAME",
"REMARK",
"ORDER_MOVE_POSITION",
],
});
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const profileSalary = new ProfileSalary();
profileSalary.date =
item.MP_POS_DATE == ""
? null_
: new Date(item.MP_POS_DATE.replace(" +0700 +07:00", ""));
2024-07-31 09:43:23 +07:00
const SALARY: any =
item.SALARY == null || item.SALARY == "" ? null_ : Number(item.SALARY);
profileSalary.amount = SALARY;
profileSalary.profileId = _item.id;
profileSalary.refCommandNo = item.MP_COMMAND_NUM;
profileSalary.posNo = item.POS_NUM_NAME + item.POS_NUM_CODE;
profileSalary.position = item.FLAG_TO_NAME;
profileSalary.positionLine = item.WORK_LINE_NAME;
profileSalary.positionPathSide = item.SPECIALIST_NAME;
profileSalary.positionExecutive = item.ADMIN_NAME;
profileSalary.templateDoc = item.REMARK;
const ORDER_MOVE_POSITION: any =
item.ORDER_MOVE_POSITION == null || item.ORDER_MOVE_POSITION == ""
? null_
: Number(item.ORDER_MOVE_POSITION);
profileSalary.order = ORDER_MOVE_POSITION;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalary.createdAt = new Date();
profileSalary.lastUpdatedAt = new Date();
2024-08-05 10:16:26 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
await this.salaryRepository.save(profileSalary);
2024-07-31 09:43:23 +07:00
}),
);
}),
);
2024-07-26 14:44:33 +07:00
console.log(rowCount);
2024-07-31 09:43:23 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileSalary-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQLSalaryEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
2024-07-26 14:44:33 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 14:44:33 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(ProfileEmployee)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
2024-07-26 14:44:33 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: _item.citizenId },
select: [
"CIT",
"MP_POS_DATE",
"SALARY",
"MP_COMMAND_NUM",
"POS_NUM_NAME",
"POS_NUM_CODE",
"FLAG_TO_NAME",
"WORK_LINE_NAME",
"SPECIALIST_NAME",
"ADMIN_NAME",
"REMARK",
"ORDER_MOVE_POSITION",
],
});
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const profileSalary = new ProfileSalary();
profileSalary.date =
2024-09-18 19:18:16 +07:00
item.MP_POS_DATE == ""
? null_
: new Date(item.MP_POS_DATE.replace(" +0700 +07:00", ""));
2024-07-31 09:43:23 +07:00
const SALARY: any =
item.SALARY == null || item.SALARY == "" ? null_ : Number(item.SALARY);
profileSalary.amount = SALARY;
profileSalary.profileEmployeeId = _item.id;
profileSalary.refCommandNo = item.MP_COMMAND_NUM;
profileSalary.posNo = item.POS_NUM_NAME + item.POS_NUM_CODE;
2025-01-07 10:56:23 +07:00
// profileSalary.position = item.FLAG_TO_NAME;
profileSalary.position = item.WORK_LINE_NAME;
2024-07-31 09:43:23 +07:00
profileSalary.positionPathSide = item.SPECIALIST_NAME;
profileSalary.positionExecutive = item.ADMIN_NAME;
profileSalary.templateDoc = item.REMARK;
const ORDER_MOVE_POSITION: any =
item.ORDER_MOVE_POSITION == null || item.ORDER_MOVE_POSITION == ""
? null_
: Number(item.ORDER_MOVE_POSITION);
profileSalary.order = ORDER_MOVE_POSITION;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalary.createdAt = new Date();
profileSalary.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-08-05 10:16:26 +07:00
await this.salaryRepository.save(profileSalary);
2024-07-31 09:43:23 +07:00
}),
);
}),
);
// }
console.log(rowCount);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileFamily-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSQLFamily(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let fathers: any = [];
let mothers: any = [];
let couples: any = [];
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(Profile)
2024-07-26 14:44:33 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 14:44:33 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(Profile)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
2024-07-26 14:44:33 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_PERSONAL_OFFICER_FAMILYRepo.findOne({
where: { CIT: _item.citizenId },
select: [
"CIT",
"FATHER_RANK_NAME",
"FATHER_FNAME",
"FATHER_LNAME",
"MOTHER_RANK_NAME",
"MOTHER_FNAME",
"MOTHER_LNAME",
"SPOUSE_RANK_NAME",
"SPOUSE_FNAME",
"SPOUSE_LNAME",
"SPOUSE_ID",
"MARRIAGE_STATE",
],
});
if (!existingProfile) {
return;
}
rowCount++;
const profileFather = new ProfileFamilyFather();
const profileMother = new ProfileFamilyMother();
const profileCouple = new ProfileFamilyCouple();
profileFather.profileId = _item.id;
profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME;
profileFather.fatherFirstName = existingProfile.FATHER_FNAME;
profileFather.fatherLastName = existingProfile.FATHER_LNAME;
profileFather.createdUserId = request.user.sub;
profileFather.createdFullName = request.user.name;
profileFather.lastUpdateUserId = request.user.sub;
profileFather.lastUpdateFullName = request.user.name;
profileFather.createdAt = new Date();
profileFather.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
profileMother.profileId = _item.id;
profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME;
profileMother.motherFirstName = existingProfile.MOTHER_FNAME;
profileMother.motherLastName = existingProfile.MOTHER_LNAME;
profileMother.createdUserId = request.user.sub;
profileMother.createdFullName = request.user.name;
profileMother.lastUpdateUserId = request.user.sub;
profileMother.lastUpdateFullName = request.user.name;
profileMother.createdAt = new Date();
profileMother.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
profileCouple.profileId = _item.id;
profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME;
profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME;
profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME;
profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID;
2024-08-05 20:58:44 +07:00
profileCouple.relationship =
existingProfile.MARRIAGE_STATE == "1"
? "โสด"
: existingProfile.MARRIAGE_STATE == "2"
? "สมรส"
: existingProfile.MARRIAGE_STATE == "3"
? "หย่าร้าง"
: existingProfile.MARRIAGE_STATE == "4"
? "หม้าย"
: "-";
2024-07-31 09:43:23 +07:00
// profileCouple.coupleLive = existingProfile.LIFE_SPOUSE;
profileCouple.createdUserId = request.user.sub;
profileCouple.createdFullName = request.user.name;
profileCouple.lastUpdateUserId = request.user.sub;
profileCouple.lastUpdateFullName = request.user.name;
profileCouple.createdAt = new Date();
profileCouple.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
// fathers.push(profileFather);
// mothers.push(profileMother);
// couples.push(profileCouple);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
// if (fathers.length === BATCH_SIZE) {
await this.profileFamilyFatherRepository.save(profileFather);
// fathers = await [];
// }
// if (mothers.length === BATCH_SIZE) {
await this.profileFamilyMotherRepository.save(profileMother);
// mothers = await [];
// }
// if (couples.length === BATCH_SIZE) {
await this.profileFamilyCoupleRepository.save(profileCouple);
// couples = await [];
// }
}),
);
2024-07-26 14:44:33 +07:00
// }
2024-08-05 10:16:26 +07:00
// console.log(rowCount);
2024-07-25 16:20:48 +07:00
2024-07-31 09:43:23 +07:00
// await Promise.all([
// this.profileFamilyFatherRepository.save(fathers),
// this.profileFamilyMotherRepository.save(mothers),
// this.profileFamilyCoupleRepository.save(couples),
// ]);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileFamily-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQLFamilyEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let fathers: any = [];
let mothers: any = [];
let couples: any = [];
2024-06-10 17:11:54 +07:00
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
2024-07-26 14:44:33 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 14:44:33 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(ProfileEmployee)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
2024-07-26 14:44:33 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
2024-08-01 16:49:26 +07:00
const existingProfile = await this.HR_PERSONAL_EMP_FAMILYRepo.findOne({
2024-07-31 09:43:23 +07:00
where: { CIT: _item.citizenId },
select: [
"CIT",
"FATHER_RANK_NAME",
"FATHER_FNAME",
"FATHER_LNAME",
"MOTHER_RANK_NAME",
"MOTHER_FNAME",
"MOTHER_LNAME",
"SPOUSE_RANK_NAME",
"SPOUSE_FNAME",
"SPOUSE_LNAME",
"SPOUSE_ID",
"MARRIAGE_STATE",
],
});
if (!existingProfile) {
return;
}
rowCount++;
const profileFather = new ProfileFamilyFather();
const profileMother = new ProfileFamilyMother();
const profileCouple = new ProfileFamilyCouple();
profileFather.profileEmployeeId = _item.id;
profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME;
profileFather.fatherFirstName = existingProfile.FATHER_FNAME;
profileFather.fatherLastName = existingProfile.FATHER_LNAME;
profileFather.createdUserId = request.user.sub;
profileFather.createdFullName = request.user.name;
profileFather.lastUpdateUserId = request.user.sub;
profileFather.lastUpdateFullName = request.user.name;
profileFather.createdAt = new Date();
profileFather.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
profileMother.profileEmployeeId = _item.id;
profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME;
profileMother.motherFirstName = existingProfile.MOTHER_FNAME;
profileMother.motherLastName = existingProfile.MOTHER_LNAME;
profileMother.createdUserId = request.user.sub;
profileMother.createdFullName = request.user.name;
profileMother.lastUpdateUserId = request.user.sub;
profileMother.lastUpdateFullName = request.user.name;
profileMother.createdAt = new Date();
profileMother.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
profileCouple.profileEmployeeId = _item.id;
profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME;
profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME;
profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME;
profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID;
2024-08-05 20:58:44 +07:00
profileCouple.relationship =
existingProfile.MARRIAGE_STATE == "1"
? "โสด"
: existingProfile.MARRIAGE_STATE == "2"
? "สมรส"
: existingProfile.MARRIAGE_STATE == "3"
? "หย่าร้าง"
: existingProfile.MARRIAGE_STATE == "4"
? "หม้าย"
: "-";
2024-07-31 09:43:23 +07:00
// profileCouple.coupleLive = existingProfile.LIFE_SPOUSE;
profileCouple.createdUserId = request.user.sub;
profileCouple.createdFullName = request.user.name;
profileCouple.lastUpdateUserId = request.user.sub;
profileCouple.lastUpdateFullName = request.user.name;
profileCouple.createdAt = new Date();
profileCouple.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
// fathers.push(profileFather);
// mothers.push(profileMother);
// couples.push(profileCouple);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
// if (fathers.length === BATCH_SIZE) {
await this.profileFamilyFatherRepository.save(profileFather);
// fathers = await [];
// }
// if (mothers.length === BATCH_SIZE) {
await this.profileFamilyMotherRepository.save(profileMother);
// mothers = await [];
// }
// if (couples.length === BATCH_SIZE) {
await this.profileFamilyCoupleRepository.save(profileCouple);
// couples = await [];
// }
}),
);
// }
2024-08-05 10:16:26 +07:00
// console.log(rowCount);
2024-07-25 16:20:48 +07:00
2024-07-31 09:43:23 +07:00
// await Promise.all([
// this.profileFamilyFatherRepository.save(fathers),
// this.profileFamilyMotherRepository.save(mothers),
// this.profileFamilyCoupleRepository.save(couples),
// ]);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
}
2024-07-25 16:20:48 +07:00
2024-07-26 14:41:41 +07:00
/**
* @summary Import Education
*/
@Post("ImportEducation")
@UseInterceptors(FileInterceptor("file"))
async UploadFileSQLEducationCode(
@UploadedFile() file: Express.Multer.File,
@Request() request: { user: Record<string, any> },
) {
const workbook = xlsx.read(file.buffer, { type: "buffer" });
const sheetName = workbook.SheetNames[0];
2024-07-26 14:41:41 +07:00
const sheet = workbook.Sheets[sheetName];
const getExcel = xlsx.utils.sheet_to_json(sheet);
let educationMis_: any = [];
2024-07-26 14:41:41 +07:00
await Promise.all(
getExcel.map(async (item: any) => {
const educationMis = new EducationMis();
educationMis.EDUCATION_CODE = item.EDUCATION_CODE;
educationMis.EDUCATION_NAME = item.EDUCATION_NAME;
educationMis.EDUCATION_ABB_NAME = item.EDUCATION_ABB_NAME;
educationMis.createdUserId = request.user.sub;
educationMis.createdFullName = request.user.name;
educationMis.lastUpdateUserId = request.user.sub;
educationMis.lastUpdateFullName = request.user.name;
educationMis.createdAt = new Date();
educationMis.lastUpdatedAt = new Date();
2024-07-26 14:41:41 +07:00
educationMis_.push(educationMis);
}),
);
await this.educationMisRepo.save(educationMis_);
return new HttpSuccess(educationMis_);
}
/**
* @summary
*/
@Post("uploadProfileEducation-Officer")
2024-07-26 15:52:32 +07:00
async UploadFileSQLEducation(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-26 14:41:41 +07:00
let educations: any = [];
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(Profile)
2024-07-26 15:52:32 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 15:52:32 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= 2; i++) {
// const profiles = await AppDataSource.getRepository(Profile)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * 5)
// .take(5)
// .getMany();
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_EDUCATIONRepo.find({
where: { CIT: _item.citizenId },
select: ["CIT", "EDUCATION_CODE", "START_EDUCATION_YEAR", "EDUCATION_YEAR", "INSTITUE"],
});
2025-01-06 23:58:24 +07:00
const educationLevel = await this.profileEducationRepo.findOne({
select: ["id", "level", "profileId"],
where: { profileId: _item.id },
order: { level: "DESC" },
});
2024-07-31 09:43:23 +07:00
// educations = await [];
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const education = new ProfileEducation();
const educationCode = await this.educationMisRepo.findOne({
where: { EDUCATION_CODE: item.EDUCATION_CODE },
});
let startDate = item.START_EDUCATION_YEAR
2024-08-05 20:58:44 +07:00
? Extension.ConvertToDateTimeV2(item.START_EDUCATION_YEAR)
2024-07-31 09:43:23 +07:00
: null_;
2024-08-05 20:58:44 +07:00
// startDate = startDate ? new Date(startDate, 0, 1) : null_;
2024-07-31 09:43:23 +07:00
let endDate = item.EDUCATION_YEAR
2024-08-05 20:58:44 +07:00
? Extension.ConvertToDateTimeV2(item.EDUCATION_YEAR)
2024-07-31 09:43:23 +07:00
: null_;
2024-08-05 20:58:44 +07:00
// endDate = endDate ? new Date(endDate, 0, 1) : null_;
2024-07-31 09:43:23 +07:00
2025-01-06 23:58:24 +07:00
education.level = educationLevel == null ? 1 : educationLevel.level + 1;
2024-07-31 09:43:23 +07:00
education.profileId = _item.id;
education.degree = educationCode ? educationCode.EDUCATION_NAME : "";
education.institute = item.INSTITUE;
education.startDate = startDate;
education.endDate = endDate;
education.createdUserId = request.user.sub;
education.createdFullName = request.user.name;
education.lastUpdateUserId = request.user.sub;
education.lastUpdateFullName = request.user.name;
education.createdAt = new Date();
education.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
// await educations.push(await education);
2024-08-05 10:16:26 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2025-01-06 23:58:24 +07:00
await this.profileEducationRepo.save(await education);
2024-07-31 09:43:23 +07:00
}),
);
2025-01-06 23:58:24 +07:00
// await this.profileEducationRepo.save(educations);
2024-07-31 09:43:23 +07:00
// educations = await [];
}),
);
// }
2024-08-05 10:16:26 +07:00
// console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2025-01-06 23:58:24 +07:00
// await this.profileEducationRepo.save(educations);
2024-07-26 14:41:41 +07:00
return new HttpSuccess();
}
2024-07-26 14:41:41 +07:00
/**
* @summary
*/
@Post("uploadProfileEducation-Employee")
2024-07-26 15:52:32 +07:00
async UploadFileSQLEducationEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-26 14:41:41 +07:00
let educations: any = [];
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
2024-07-26 15:52:32 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 15:52:32 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(ProfileEmployee)
// .createQueryBuilder("profile")
// .select(["profile.citizenId", "profile.id"])
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_EDUCATION_EMPRepo.find({
where: { CIT: _item.citizenId },
select: ["CIT", "EDUCATION_CODE", "START_EDUCATION_YEAR", "EDUCATION_YEAR", "INSTITUE"],
});
2025-01-06 23:58:24 +07:00
const educationLevel = await this.profileEducationRepo.findOne({
select: ["id", "level", "profileId"],
where: { profileEmployeeId: _item.id },
order: { level: "DESC" },
});
2024-07-31 09:43:23 +07:00
educations = await [];
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const education = new ProfileEducation();
const educationCode = await this.educationMisRepo.findOne({
where: { EDUCATION_CODE: item.EDUCATION_CODE },
});
let startDate = item.START_EDUCATION_YEAR
2024-08-05 20:58:44 +07:00
? Extension.ConvertToDateTimeV2(item.START_EDUCATION_YEAR)
2024-07-31 09:43:23 +07:00
: null_;
2024-08-05 20:58:44 +07:00
// startDate = startDate ? new Date(startDate, 0, 1) : null_;
2024-07-31 09:43:23 +07:00
let endDate = item.EDUCATION_YEAR
2024-08-05 20:58:44 +07:00
? Extension.ConvertToDateTimeV2(item.EDUCATION_YEAR)
2024-07-31 09:43:23 +07:00
: null_;
2024-08-05 20:58:44 +07:00
// endDate = endDate ? new Date(endDate, 0, 1) : null_;
2024-07-31 09:43:23 +07:00
2025-01-06 23:58:24 +07:00
education.level = educationLevel == null ? 1 : educationLevel.level + 1;
2024-07-31 09:43:23 +07:00
education.profileEmployeeId = _item.id;
education.degree = educationCode ? educationCode.EDUCATION_NAME : "";
education.institute = item.INSTITUE;
education.startDate = startDate;
education.endDate = endDate;
education.createdUserId = request.user.sub;
education.createdFullName = request.user.name;
education.lastUpdateUserId = request.user.sub;
education.lastUpdateFullName = request.user.name;
education.createdAt = new Date();
education.lastUpdatedAt = new Date();
2024-08-05 10:16:26 +07:00
// educations.push(education);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2025-01-06 23:58:24 +07:00
await this.profileEducationRepo.save(education);
2024-07-31 09:43:23 +07:00
}),
);
2025-01-06 23:58:24 +07:00
// await this.profileEducationRepo.save(educations);
2024-07-31 09:43:23 +07:00
}),
);
// }
2024-08-05 10:16:26 +07:00
// console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2025-01-06 23:58:24 +07:00
// await this.profileEducationRepo.save(educations);
2024-07-26 14:41:41 +07:00
return new HttpSuccess();
}
/**
* @summary Import Province
*/
@Post("ImportProvince")
@UseInterceptors(FileInterceptor("file"))
async ImportProvince(
@UploadedFile() file: Express.Multer.File,
@Request() request: { user: Record<string, any> },
) {
const workbook = xlsx.read(file.buffer, { type: "buffer" });
const sheetName = workbook.SheetNames[0];
2024-07-26 14:41:41 +07:00
const sheet = workbook.Sheets[sheetName];
const getExcel = xlsx.utils.sheet_to_json(sheet);
let provinces_: any = [];
await Promise.all(
getExcel.map(async (item: any) => {
const prov = new ProvinceImport();
prov.PROVINCE_CODE = item.PROVINCE_CODE;
prov.PROVINCE_NAME = item.PROVINCE_NAME;
prov.createdUserId = request.user.sub;
prov.createdFullName = request.user.name;
prov.lastUpdateUserId = request.user.sub;
prov.lastUpdateFullName = request.user.name;
prov.createdAt = new Date();
prov.lastUpdatedAt = new Date();
2024-07-26 14:41:41 +07:00
provinces_.push(prov);
}),
);
await this.provincsRepo.save(provinces_);
return new HttpSuccess(provinces_);
}
/**
* @summary Import Amphur
*/
@Post("ImportAmphur")
@UseInterceptors(FileInterceptor("file"))
async ImportAmphur(
@UploadedFile() file: Express.Multer.File,
@Request() request: { user: Record<string, any> },
) {
const workbook = xlsx.read(file.buffer, { type: "buffer" });
const sheetName = workbook.SheetNames[0];
2024-07-26 14:41:41 +07:00
const sheet = workbook.Sheets[sheetName];
const getExcel = xlsx.utils.sheet_to_json(sheet);
let amphur_: any = [];
await Promise.all(
getExcel.map(async (item: any) => {
const amh = new AmphurImport();
amh.PROVINCE_CODE = item.PROVINCE_CODE;
amh.AMPHUR_CODE = item.AMPHUR_CODE;
amh.AMPHUR_NAME = item.AMPHUR_NAME;
amh.createdUserId = request.user.sub;
amh.createdFullName = request.user.name;
amh.lastUpdateUserId = request.user.sub;
amh.lastUpdateFullName = request.user.name;
amh.createdAt = new Date();
amh.lastUpdatedAt = new Date();
2024-07-26 14:41:41 +07:00
amphur_.push(amh);
}),
);
await this.amphurRepo.save(amphur_);
return new HttpSuccess(amphur_);
}
/**
* @summary Import SubDistrict
*/
@Post("ImportSubDistrict")
@UseInterceptors(FileInterceptor("file"))
async ImportSubDistrict(
@UploadedFile() file: Express.Multer.File,
@Request() request: { user: Record<string, any> },
) {
const workbook = xlsx.read(file.buffer, { type: "buffer" });
const sheetName = workbook.SheetNames[0];
2024-07-26 14:41:41 +07:00
const sheet = workbook.Sheets[sheetName];
const getExcel = xlsx.utils.sheet_to_json(sheet);
let subDistrict_: any = [];
await Promise.all(
getExcel.map(async (item: any) => {
const subD = new SubDistrictImport();
subD.PROVINCE_CODE = item.PROVINCE_CODE;
subD.AMPHUR_CODE = item.AMPHUR_CODE;
subD.DISTRICT_CODE = item.DISTRICT_CODE;
subD.DISTRICT_NAME = item.DISTRICT_NAME;
subD.createdUserId = request.user.sub;
subD.createdFullName = request.user.name;
subD.lastUpdateUserId = request.user.sub;
subD.lastUpdateFullName = request.user.name;
subD.createdAt = new Date();
subD.lastUpdatedAt = new Date();
2024-07-26 14:41:41 +07:00
subDistrict_.push(subD);
}),
);
await this.subDistrictRepo.save(subDistrict_);
return new HttpSuccess(subDistrict_);
}
/**
* @summary -
*/
@Post("uploadProfileAddress-Officer")
2024-07-26 15:52:32 +07:00
async UploadFileSQLAddress(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
let profileDatas: any = [];
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(Profile)
2024-07-26 15:52:32 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 15:52:32 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= 2; i++) {
// const profiles = await AppDataSource.getRepository(Profile)
// .createQueryBuilder("profile")
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * 5)
// .take(5)
// .getMany();
// profileDatas = await [];
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_PERSONAL_OFFICER_ADDRESSRepo.findOne({
where: { CIT: _item.citizenId },
});
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
if (!existingProfile) {
return;
}
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
rowCount++;
let provinceRegis_: any = null;
let districtRegis_: any = null;
let subDistrictRegis_: any = null;
let provinceCurr_: any = null;
let districtCurr_: any = null;
let subDistrictCurr_: any = null;
//registration address
if (existingProfile.PROVINCE_CODE) {
provinceRegis_ = await this.provincsRepo.findOne({
where: { PROVINCE_CODE: existingProfile.PROVINCE_CODE },
});
if (provinceRegis_) {
let provinceId = await this.provinceIdRepo.findOne({
where: {
name: provinceRegis_.PROVINCE_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationProvinceId = provinceId ? provinceId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
if (existingProfile.AMPHUR_CODE && provinceRegis_) {
districtRegis_ = await this.amphurRepo.findOne({
where: {
AMPHUR_CODE: existingProfile.AMPHUR_CODE,
PROVINCE_CODE: provinceRegis_.PROVINCE_CODE,
},
});
if (districtRegis_) {
let districtId = await this.districtIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: districtRegis_.AMPHUR_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationDistrictId = districtId ? districtId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
if (existingProfile.DISTRICT_CODE && districtRegis_ && provinceRegis_) {
subDistrictRegis_ = await this.subDistrictRepo.findOne({
where: {
DISTRICT_CODE: existingProfile.DISTRICT_CODE,
AMPHUR_CODE: districtRegis_.AMPHUR_CODE,
PROVINCE_CODE: provinceRegis_.PROVINCE_CODE,
},
});
if (subDistrictRegis_) {
let subDistrictId = await this.subDistrictIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: subDistrictRegis_.DISTRICT_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationSubDistrictId = subDistrictId ? subDistrictId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
//current address
if (existingProfile.CONTACT_PROVINCE_CODE) {
provinceCurr_ = await this.provincsRepo.findOne({
where: { PROVINCE_CODE: existingProfile.CONTACT_PROVINCE_CODE },
});
if (provinceCurr_) {
let provinceId = await this.provinceIdRepo.findOne({
where: {
name: provinceCurr_.PROVINCE_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentProvinceId = provinceId ? provinceId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
if (existingProfile.CONTACT_AMPHUR_CODE && provinceCurr_) {
districtCurr_ = await this.amphurRepo.findOne({
where: {
AMPHUR_CODE: existingProfile.CONTACT_AMPHUR_CODE,
PROVINCE_CODE: provinceCurr_.PROVINCE_CODE,
},
});
if (districtCurr_) {
let districtId = await this.districtIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: districtCurr_.AMPHUR_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentDistrictId = districtId ? districtId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
if (existingProfile.CONTACT_DISTRICT_CODE && districtCurr_ && provinceCurr_) {
subDistrictCurr_ = await this.subDistrictRepo.findOne({
where: {
DISTRICT_CODE: existingProfile.CONTACT_DISTRICT_CODE,
AMPHUR_CODE: districtCurr_.AMPHUR_CODE,
PROVINCE_CODE: provinceCurr_.PROVINCE_CODE,
},
});
if (subDistrictCurr_) {
let subDistrictId = await this.subDistrictIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: subDistrictCurr_.DISTRICT_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
_item.registrationAddress = existingProfile.H_NUMBER;
_item.registrationZipCode = existingProfile.ZIPCODE;
_item.currentAddress = existingProfile.CONTACT_H_NUMBER;
_item.currentZipCode = existingProfile.CONTACT_ZIPCODE;
2024-08-05 10:16:26 +07:00
_item.createdUserId = request.user.sub;
_item.createdFullName = request.user.name;
2024-07-31 09:43:23 +07:00
_item.lastUpdateUserId = request.user.sub;
_item.lastUpdateFullName = request.user.name;
_item.createdAt = new Date();
_item.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
// profileDatas.push(_item);
2024-08-05 10:16:26 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-07-31 09:43:23 +07:00
await this.profileRepo.save(_item);
}),
);
// await this.profileRepo.save(profileDatas);
// }
2024-08-05 10:16:26 +07:00
// console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-07-26 14:41:41 +07:00
return new HttpSuccess();
}
2024-07-26 14:41:41 +07:00
/**
* @summary -
*/
@Post("uploadProfileAddress-Employee")
2024-07-26 15:52:32 +07:00
async UploadFileSQLAddressEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
let profileDatas: any = [];
2024-07-26 14:41:41 +07:00
let null_: any = null;
2024-07-31 09:43:23 +07:00
const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
2024-07-26 15:52:32 +07:00
.createQueryBuilder("profile")
2024-07-31 09:43:23 +07:00
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
// .skip(0)
// .take(20)
2024-07-26 15:52:32 +07:00
.getManyAndCount();
2024-07-31 09:43:23 +07:00
// for (var i = 1; i <= total / BATCH_SIZE; i++) {
// const profiles = await AppDataSource.getRepository(ProfileEmployee)
// .createQueryBuilder("profile")
// .orderBy("profile.citizenId", "ASC")
// .skip((i - 1) * BATCH_SIZE)
// .take(BATCH_SIZE)
// .getMany();
// profileDatas = await [];
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_PERSONAL_EMP_ADDRESSRepo.findOne({
where: { CIT: _item.citizenId },
});
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
if (!existingProfile) {
return;
}
2024-07-26 15:52:32 +07:00
2024-07-31 09:43:23 +07:00
rowCount++;
let provinceRegis_: any = null;
let districtRegis_: any = null;
let subDistrictRegis_: any = null;
let provinceCurr_: any = null;
let districtCurr_: any = null;
let subDistrictCurr_: any = null;
//registration address
if (existingProfile.PROVINCE_CODE) {
provinceRegis_ = await this.provincsRepo.findOne({
where: { PROVINCE_CODE: existingProfile.PROVINCE_CODE },
});
if (provinceRegis_) {
let provinceId = await this.provinceIdRepo.findOne({
where: {
name: provinceRegis_.PROVINCE_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationProvinceId = provinceId ? provinceId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
2024-08-05 10:16:26 +07:00
if (existingProfile.AMPHUR_CODE && provinceRegis_) {
2024-07-31 09:43:23 +07:00
districtRegis_ = await this.amphurRepo.findOne({
where: {
AMPHUR_CODE: existingProfile.AMPHUR_CODE,
PROVINCE_CODE: provinceRegis_.PROVINCE_CODE,
},
});
if (districtRegis_) {
let districtId = await this.districtIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: districtRegis_.AMPHUR_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationDistrictId = districtId ? districtId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
2024-08-05 10:16:26 +07:00
if (existingProfile.DISTRICT_CODE && districtRegis_ && provinceRegis_) {
2024-07-31 09:43:23 +07:00
subDistrictRegis_ = await this.subDistrictRepo.findOne({
where: {
DISTRICT_CODE: existingProfile.DISTRICT_CODE,
AMPHUR_CODE: districtRegis_.AMPHUR_CODE,
PROVINCE_CODE: provinceRegis_.PROVINCE_CODE,
},
});
if (subDistrictRegis_) {
let subDistrictId = await this.subDistrictIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: subDistrictRegis_.DISTRICT_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.registrationSubDistrictId = subDistrictId ? subDistrictId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
//current address
if (existingProfile.CONTACT_PROVINCE_CODE) {
provinceCurr_ = await this.provincsRepo.findOne({
where: { PROVINCE_CODE: existingProfile.CONTACT_PROVINCE_CODE },
});
if (provinceCurr_) {
let provinceId = await this.provinceIdRepo.findOne({
where: {
name: provinceCurr_.PROVINCE_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentProvinceId = provinceId ? provinceId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
2024-08-05 10:16:26 +07:00
if (existingProfile.CONTACT_AMPHUR_CODE && provinceCurr_) {
2024-07-31 09:43:23 +07:00
districtCurr_ = await this.amphurRepo.findOne({
where: {
AMPHUR_CODE: existingProfile.CONTACT_AMPHUR_CODE,
PROVINCE_CODE: provinceCurr_.PROVINCE_CODE,
},
});
if (districtCurr_) {
let districtId = await this.districtIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: districtCurr_.AMPHUR_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentDistrictId = districtId ? districtId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
2024-08-05 10:16:26 +07:00
if (existingProfile.CONTACT_DISTRICT_CODE && districtCurr_ && provinceCurr_) {
2024-07-31 09:43:23 +07:00
subDistrictCurr_ = await this.subDistrictRepo.findOne({
where: {
DISTRICT_CODE: existingProfile.CONTACT_DISTRICT_CODE,
AMPHUR_CODE: districtCurr_.AMPHUR_CODE,
PROVINCE_CODE: provinceCurr_.PROVINCE_CODE,
},
});
if (subDistrictCurr_) {
let subDistrictId = await this.subDistrictIdRepo.findOne({
where: {
2024-07-31 09:43:23 +07:00
name: subDistrictCurr_.DISTRICT_NAME,
},
2024-07-26 14:41:41 +07:00
});
2024-07-31 09:43:23 +07:00
_item.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_;
2024-07-26 14:41:41 +07:00
}
2024-07-31 09:43:23 +07:00
}
_item.registrationAddress = existingProfile.H_NUMBER;
_item.registrationZipCode = existingProfile.ZIPCODE;
_item.currentAddress = existingProfile.CONTACT_H_NUMBER;
_item.currentZipCode = existingProfile.CONTACT_ZIPCODE;
2024-08-05 10:16:26 +07:00
_item.createdUserId = request.user.sub;
_item.createdFullName = request.user.name;
2024-07-31 09:43:23 +07:00
_item.lastUpdateUserId = request.user.sub;
_item.lastUpdateFullName = request.user.name;
_item.createdAt = new Date();
_item.lastUpdatedAt = new Date();
2024-07-31 09:43:23 +07:00
// profileDatas.push(_item);
2024-08-05 10:16:26 +07:00
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-07-31 09:43:23 +07:00
await this.profileEmpRepo.save(_item);
}),
);
// await this.profileEmpRepo.save(profileDatas);
// }
2024-08-05 10:16:26 +07:00
// console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
return new HttpSuccess();
}
/**
* @summary
*/
@Post("cleardataupload")
async ClearDataUpload(@Request() request: { user: Record<string, any> }) {
const profileOff = await this.profileRepo.find({
select: ["citizenId"],
});
const profileEmp = await this.profileEmpRepo.find({
select: ["citizenId"],
});
const _profileOff = profileOff.map((x) => x.citizenId);
const _profileEmp = profileEmp.map((x) => x.citizenId);
const allId = _profileOff.concat(_profileEmp);
for (var i = 1; i <= 1000; i++) {
const HR_POSITION_OFFICER = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: Not(In(allId)) },
take: 1000,
skip: 0,
});
this.HR_POSITION_OFFICERRepo.remove(HR_POSITION_OFFICER);
// const HR_PERSONAL_OFFICER_FAMILY = await this.HR_PERSONAL_OFFICER_FAMILYRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_PERSONAL_OFFICER_FAMILYRepo.remove(HR_PERSONAL_OFFICER_FAMILY);
// const HR_EDUCATION = await this.HR_EDUCATIONRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_EDUCATIONRepo.remove(HR_EDUCATION);
// const HR_PERSONAL_OFFICER_ADDRESS = await this.HR_PERSONAL_OFFICER_ADDRESSRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_PERSONAL_OFFICER_ADDRESSRepo.remove(HR_PERSONAL_OFFICER_ADDRESS);
// const HR_EDUCATION_EMP = await this.HR_EDUCATION_EMPRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_EDUCATION_EMPRepo.remove(HR_EDUCATION_EMP);
// const HR_PERSONAL_EMP_ADDRESS = await this.HR_PERSONAL_EMP_ADDRESSRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_PERSONAL_EMP_ADDRESSRepo.remove(HR_PERSONAL_EMP_ADDRESS);
// const HR_PERSONAL_EMP_FAMILY = await this.HR_PERSONAL_EMP_FAMILYRepo.find({
// where: { CIT: Not(In(allId)) },
// take: 1000,
// skip: 0,
// });
// this.HR_PERSONAL_EMP_FAMILYRepo.remove(HR_PERSONAL_EMP_FAMILY);
}
2024-07-26 14:41:41 +07:00
return new HttpSuccess();
}
2024-06-06 11:15:26 +07:00
}