import { Controller, Post, Route, Security, Tags, Request, UploadedFile } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { In, IsNull, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import { UseInterceptors } from "@nestjs/common"; import { Profile } from "../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileSalary } from "../entities/ProfileSalary"; import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; import { ProfileFamilyMother } from "../entities/ProfileFamilyMother"; import { ProfileFamilyFather } from "../entities/ProfileFamilyFather"; import { ProfileEducation } from "../entities/ProfileEducation"; import { PosLevel } from "../entities/PosLevel"; import { PosType } from "../entities/PosType"; import { FileInterceptor } from "@nestjs/platform-express"; import * as xlsx from "xlsx"; import Extension from "../interfaces/extension"; import { calculateRetireDate, calculateRetireLaw } from "../interfaces/utils"; import * as fs from "fs"; import * as path from "path"; const { createConnection } = require("typeorm"); import csvParser from "csv-parser"; import { HR_PERSONAL_OFFICER_FAMILY } from "../entities/HR_PERSONAL_OFFICER_FAMILY"; const BATCH_SIZE = 1000; // import { EducationMis } from "../entities/EducationMis"; import { EducationMis } from "../entities/EducationMis"; 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"; 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"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { IMPORT_ORG } from "../entities/IMPORT_ORG"; import { OrgRevision } from "../entities/OrgRevision"; import { OFFICER } from "../entities/OFFICER"; import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; import { positionOfficer } from "../entities/positionOfficer"; @Route("api/v1/org/upload") @Tags("UPLOAD") @Security("bearerAuth") export class ImportDataController extends Controller { private profileEducationRepo = AppDataSource.getRepository(ProfileEducation); private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple); private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother); private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather); private salaryRepo = AppDataSource.getRepository(ProfileSalary); private profileRepo = AppDataSource.getRepository(Profile); private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); private posLevelRepo = AppDataSource.getRepository(PosLevel); private posTypeRepo = AppDataSource.getRepository(PosType); private positionOfficerRepo = AppDataSource.getRepository(positionOfficer); private HR_PERSONAL_OFFICER_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_OFFICER_FAMILY); 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); private educationMisRepo = AppDataSource.getRepository(EducationMis); 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); private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); private orgRootRepo = AppDataSource.getRepository(OrgRoot); private orgChild1Repo = AppDataSource.getRepository(OrgChild1); private orgChild2Repo = AppDataSource.getRepository(OrgChild2); private orgChild3Repo = AppDataSource.getRepository(OrgChild3); private orgChild4Repo = AppDataSource.getRepository(OrgChild4); private IMPORT_ORGRepo = AppDataSource.getRepository(IMPORT_ORG); private OFFICERRepo = AppDataSource.getRepository(OFFICER); private positionRepo = AppDataSource.getRepository(Position); private posMasterRepo = AppDataSource.getRepository(PosMaster); /** * @summary ทะเบียนประวัติ ข้าราชการ */ @Post("uploadProfile-Officer") async UploadFileSqlOfficer(@Request() request: { user: Record }) { const OFFICER = await this.OFFICERRepo.find(); // let users = []; let rowCount = 0; // const filePath = path.resolve(__dirname, "OFFICER.csv"); // Corrected file path // const readStream = fs.createReadStream(filePath).pipe(csvParser()); // let profiles: any = []; let null_: any = null; let profile: any; // await Promise.all( // OFFICER.map(async (item) => { for await (const item of OFFICER) { // readStream.map(async (item: any) => { rowCount++; let type_: any = null; let level_: any = null; profile = null; profile = new Profile(); // if (item["FLAG_RETIRE_STATUSxxxx != "" && item["FLAG_RETIRE_STATUSxxxx != null) { // continue; // } // if (item["FLAG_PERSON_TYPExxxx != "1") { // continue; // } // if (new Date(item.RET_YEAR).getFullYear() >= 2567) { // return; // } const existingProfile = await this.profileRepo.findOne({ where: { citizenId: item.id.toString() }, }); if (existingProfile) { profile.id = existingProfile.id; // continue; } if (item.MP_CATEGORY) { type_ = await this.posTypeRepo.findOne({ where: { posTypeName: item.MP_CATEGORY }, }); } if (item.MP_LEVEL) { if (type_ == null) { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item.MP_LEVEL, }, }); } else { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item.MP_LEVEL, posTypeId: type_.id, }, }); } } let dateRetire = new Date(item.BORN); profile.citizenId = item.id.toString() == "" ? "" : item.id.toString(); profile.rank = item.RANK_NAME == "" || item.RANK_NAME == "นาย" || item.RANK_NAME == "นาง" || item.RANK_NAME == "นางสาว" ? null : item.RANK_NAME; profile.prefix = item.RANK_NAME == "" ? null : item.RANK_NAME; profile.prefixMain = item.RANK_NAME == "" || (item.RANK_NAME != "นาย" && item.RANK_NAME != "นาง" && item.RANK_NAME != "นางสาว") ? null : item.RANK_NAME; profile.firstName = item.FNAME == "" ? null : item.FNAME; profile.lastName = item.LNAME == "" ? null : item.LNAME; profile.gender = item.SEX == "1" ? "ชาย" : item.SEX == "2" ? "หญิง" : null_; profile.birthDate = item.BORN == "" ? null_ : new Date(item.BORN); profile.dateAppoint = item.BEGIN_ENTRY_DATE == "" ? null_ : new Date(item.BEGIN_ENTRY_DATE); profile.dateStart = item.BEGIN_ENTRY_DATE == "" ? null_ : new Date(item.BEGIN_ENTRY_DATE); 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 = type_ != null && type_.posTypeName == item.MP_CATEGORY && type_ ? type_.id : null; profile.posLevelId = level_ != null && level_.posLevelName == item.MP_LEVEL && level_ ? level_.id : null; // profile.relationship = // item.สถานภาพ == "" ? "" : Extension.CheckRelationship(item.สถานภาพ); // profile.position = // item.WORK_LINE_NAME == "" ? "" : Extension.CheckRelationship(item.WORK_LINE_NAME); profile.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"]; // const level = await this.posLevelRepo.findOne({ // where: { // posLevelName: item.MP_LEVEL, // posType: { // posTypeName: item.MP_Type, // }, // }, // }); // profile.posLevelId = level?.id ?? null_; // const type = await this.posTypeRepo.findOne({ // where: { posTypeName: item.MP_Type }, // }); // profile.posTypeId = type?.id ?? null_; profile.amount = item.SALARY == "" ? 0 : Number(Extension.CheckRelationship(item.SALARY)); // profile.isLeave = // item.FLAG_RETIRE_STATUSxxxx == "" || item.FLAG_RETIRE_STATUSxxxx == null ? false : true; 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(); // profiles.push(profile); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // if (profiles.length === BATCH_SIZE) { await this.profileRepo.save(profile); // console.log(profile); // profiles = await []; // if (global.gc) { // global.gc(); // } // } } // }), // ); // ) // ); // console.log(rowCount); // await this.profileRepo.save(profiles); return new HttpSuccess(); } /** * @summary ทะเบียนประวัติ ลูกจ้างประจำ */ @Post("uploadProfile-Employee") async UploadFileSQL(@Request() request: { user: Record }) { let users = []; let rowCount = 0; const filePath = path.resolve(__dirname, "EMP.csv"); // Corrected file path const readStream = fs.createReadStream(filePath).pipe(csvParser()); let profiles: any = []; let null_: any = null; let profileEmp: any; for await (const item of readStream) { rowCount++; profileEmp = null; profileEmp = new ProfileEmployee(); // if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) { // continue; // } // if (item["FLAG_PERSON_TYPE"] != "6") { // continue; // } // if (new Date(item["RET_YEAR"]).getFullYear() >= 2567) { // continue; // } const existingProfile = await this.profileEmpRepo.findOne({ where: { citizenId: item["ID"] }, }); if (existingProfile) { profileEmp.id = existingProfile.id; // continue; } let dateRetire = new Date(item["MP_FORCE_DATE"]); profileEmp.citizenId = item["ID"] == "" ? "" : item["ID"]; profileEmp.employeeClass = item["FLAG_PERSON_TYPE"] == "6" ? "PERM" : item["FLAG_PERSON_TYPE"] == "7" ? "TEMP" : ""; profileEmp.rank = item["RANK_NAME"] == "" || item["RANK_NAME"] == "นาย" || item["RANK_NAME"] == "นาง" || item["RANK_NAME"] == "นางสาว" ? null : item["RANK_NAME"]; profileEmp.prefix = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; profileEmp.prefixMain = item["RANK_NAME"] == "" || (item["RANK_NAME"] != "นาย" && item["RANK_NAME"] != "นาง" && item["RANK_NAME"] != "นางสาว") ? null : item["RANK_NAME"]; profileEmp.firstName = item["FNAME"] == "" ? null : item["FNAME"]; profileEmp.lastName = item["LNAME"] == "" ? null : item["LNAME"]; profileEmp.gender = item["SEX"] == "1" ? "ชาย" : item["SEX"] == "2" ? "หญิง" : null_; profileEmp.birthDate = item["BORN"] == "" ? null_ : new Date(item["BORN"]); profileEmp.dateAppoint = item["BEGIN_ENTRY_DATE"] == "" ? null_ : new Date(item["BEGIN_ENTRY_DATE"]); profileEmp.dateStart = item["MP_FORCE_DATE"] == "" ? null_ : new Date(item["MP_FORCE_DATE"]); 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.amount = item["SALARY"] == "" ? 0 : Number(Extension.CheckRelationship(item.SALARY)); 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(); // profiles.push(profileEmp); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // if (profiles.length === BATCH_SIZE) { await this.profileEmpRepo.save(profileEmp); // profiles = await []; // if (global.gc) { // global.gc(); // } // } } // console.log(rowCount); // await this.profileEmpRepo.save(profiles); return new HttpSuccess(); } /** * @summary เงินเดือน ข้าราชการ */ @Post("uploadProfileSalary-Officer") async UploadFileSQLSalary(@Request() request: { user: Record }) { let rowCount = 0; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") .where("profile.citizenId = '3101702379675'") // .skip(0) // .take(10000) .getManyAndCount(); await Promise.all( profiles.map(async (_item) => { const existingProfile = await this.positionOfficerRepo.find({ where: { citizenId: _item.citizenId, flag_person_type: "1" }, order: { mp_pos_date: "ASC", order_move_position: "ASC", }, }); await Promise.all( existingProfile.map(async (item) => { rowCount++; const profileSalary: any = new ProfileSalary(); profileSalary.profileId = _item.id; profileSalary.order = item.order_move_position; /// profileSalary.commandNo = item.mp_command_num; profileSalary.commandYear = item.cur_year; profileSalary.commandDateSign = item.mp_command_date; profileSalary.commandDateAffect = item.mp_pos_date; profileSalary.commandCode = item.flag_to_name_code; profileSalary.commandName = item.flag_to_name; profileSalary.posNoAbb = item.pos_num_name; profileSalary.posNo = item.pos_num_code; profileSalary.positionName = item.work_line_name; profileSalary.positionCee = item.mp_cee; var positionType = ""; var positionLevel = ""; 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 = "สูง"; } var _type = await this.posTypeRepo.findOne({ where: { posTypeName: positionType }, }); profileSalary.positionType = _type == null ? null_ : _type.id; if (_type != null) { var _level = await this.posLevelRepo.findOne({ where: { posLevelName: positionLevel, posTypeId: _type.id, }, }); profileSalary.positionLevel = _level == null ? null_ : _level.id; } 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; profileSalary.amount = item.salary; profileSalary.remark = item.remark; 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.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(); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // save to file // await this.salaryRepo.save(profileSalary); }), ); }), ); console.log(rowCount); return new HttpSuccess(); } /** * @summary เงินเดือน ลูกจ้างประจำ */ @Post("uploadProfileSalary-Employee") async UploadFileSQLSalaryEmp(@Request() request: { user: Record }) { let rowCount = 0; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") .skip(0) .take(10000) .getManyAndCount(); await Promise.all( profiles.map(async (_item) => { const existingProfile = await this.positionOfficerRepo.find({ where: { citizenId: _item.citizenId, flag_person_type: "7" }, }); await Promise.all( existingProfile.map(async (item) => { rowCount++; const profileSalary: any = new ProfileSalary(); profileSalary.profileId = _item.id; profileSalary.order = item.order_move_position; /// profileSalary.commandNo = item.mp_command_num; profileSalary.commandYear = item.cur_year; profileSalary.commandDateSign = item.mp_command_date; profileSalary.commandDateAffect = item.mp_pos_date; profileSalary.commandCode = item.flag_to_name_code; profileSalary.commandName = item.flag_to_name; profileSalary.posNoAbb = item.pos_num_name; profileSalary.posNo = item.pos_num_code; profileSalary.positionName = item.work_line_name; profileSalary.positionCee = item.mp_cee; profileSalary.positionType = item.mp_cee; profileSalary.positionLevel = item.mp_cee; 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; profileSalary.amount = item.salary; profileSalary.remark = item.remark; 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.commandId; 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(); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // save to file // await this.salaryRepo.save(profileSalary); }), ); }), ); console.log(rowCount); return new HttpSuccess(); } /** * @summary ครอบครัว ข้าราชการ */ @Post("uploadProfileFamily-Officer") async UploadFileSQLFamily(@Request() request: { user: Record }) { let rowCount = 0; let fathers: any = []; let mothers: any = []; let couples: any = []; const [profiles, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(1000) .getManyAndCount(); // 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(); 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(); 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(); 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; profileCouple.relationship = existingProfile.MARRIAGE_STATE == "1" ? "โสด" : existingProfile.MARRIAGE_STATE == "2" ? "สมรส" : existingProfile.MARRIAGE_STATE == "3" ? "หย่าร้าง" : existingProfile.MARRIAGE_STATE == "4" ? "หม้าย" : "-"; // 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(); // 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 []; // } }), ); // } // console.log(rowCount); // await Promise.all([ // this.profileFamilyFatherRepository.save(fathers), // this.profileFamilyMotherRepository.save(mothers), // this.profileFamilyCoupleRepository.save(couples), // ]); return new HttpSuccess(); } /** * @summary ครอบครัว ลูกจ้างประจำ */ @Post("uploadProfileFamily-Employee") async UploadFileSQLFamilyEmp(@Request() request: { user: Record }) { let rowCount = 0; let fathers: any = []; let mothers: any = []; let couples: any = []; const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(20) .getManyAndCount(); // 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(); await Promise.all( profiles.map(async (_item) => { const existingProfile = await this.HR_PERSONAL_EMP_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.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(); 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(); 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; profileCouple.relationship = existingProfile.MARRIAGE_STATE == "1" ? "โสด" : existingProfile.MARRIAGE_STATE == "2" ? "สมรส" : existingProfile.MARRIAGE_STATE == "3" ? "หย่าร้าง" : existingProfile.MARRIAGE_STATE == "4" ? "หม้าย" : "-"; // 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(); // 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 []; // } }), ); // } // console.log(rowCount); // await Promise.all([ // this.profileFamilyFatherRepository.save(fathers), // this.profileFamilyMotherRepository.save(mothers), // this.profileFamilyCoupleRepository.save(couples), // ]); return new HttpSuccess(); } /** * @summary Import Education */ @Post("ImportEducation") @UseInterceptors(FileInterceptor("file")) async UploadFileSQLEducationCode( @UploadedFile() file: Express.Multer.File, @Request() request: { user: Record }, ) { const workbook = xlsx.read(file.buffer, { type: "buffer" }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const getExcel = xlsx.utils.sheet_to_json(sheet); let educationMis_: any = []; 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(); educationMis_.push(educationMis); }), ); await this.educationMisRepo.save(educationMis_); return new HttpSuccess(educationMis_); } /** * @summary ประวัติการศึกษา ข้าราชการ */ @Post("uploadProfileEducation-Officer") async UploadFileSQLEducation(@Request() request: { user: Record }) { let rowCount = 0; let educations: any = []; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(20) .getManyAndCount(); // 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(); 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", "EDUCATION_SEQ", ], }); const educationLevel = await this.profileEducationRepo.findOne({ select: ["id", "level", "profileId"], where: { profileId: _item.id }, order: { level: "DESC" }, }); // 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 ? Extension.ConvertToDateTimeV2(item.START_EDUCATION_YEAR) : null_; // startDate = startDate ? new Date(startDate, 0, 1) : null_; let endDate = item.EDUCATION_YEAR ? Extension.ConvertToDateTimeV2(item.EDUCATION_YEAR) : null_; // endDate = endDate ? new Date(endDate, 0, 1) : null_; education.level = educationLevel == null ? 1 : educationLevel.level + 1; education.profileId = _item.id; education.degree = educationCode ? educationCode.EDUCATION_NAME : ""; education.institute = item.INSTITUE; education.level = item.EDUCATION_SEQ ? null_ : Number(item.EDUCATION_SEQ); 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(); // await educations.push(await education); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); await this.profileEducationRepo.save(await education); }), ); // await this.profileEducationRepo.save(educations); // educations = await []; }), ); // } // console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // await this.profileEducationRepo.save(educations); return new HttpSuccess(); } /** * @summary ประวัติการศึกษา ลูกจ้างประจำ */ @Post("uploadProfileEducation-Employee") async UploadFileSQLEducationEmp(@Request() request: { user: Record }) { let rowCount = 0; let educations: any = []; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(20) .getManyAndCount(); // 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(); 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", "EDUCATION_SEQ", ], }); const educationLevel = await this.profileEducationRepo.findOne({ select: ["id", "level", "profileId"], where: { profileEmployeeId: _item.id }, order: { level: "DESC" }, }); 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 ? Extension.ConvertToDateTimeV2(item.START_EDUCATION_YEAR) : null_; // startDate = startDate ? new Date(startDate, 0, 1) : null_; let endDate = item.EDUCATION_YEAR ? Extension.ConvertToDateTimeV2(item.EDUCATION_YEAR) : null_; // endDate = endDate ? new Date(endDate, 0, 1) : null_; education.level = educationLevel == null ? 1 : educationLevel.level + 1; education.profileEmployeeId = _item.id; education.degree = educationCode ? educationCode.EDUCATION_NAME : ""; education.institute = item.INSTITUE; education.level = item.EDUCATION_SEQ ? null_ : Number(item.EDUCATION_SEQ); 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(); // educations.push(education); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); await this.profileEducationRepo.save(education); }), ); // await this.profileEducationRepo.save(educations); }), ); // } // console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // await this.profileEducationRepo.save(educations); return new HttpSuccess(); } /** * @summary Import Province */ @Post("ImportProvince") @UseInterceptors(FileInterceptor("file")) async ImportProvince( @UploadedFile() file: Express.Multer.File, @Request() request: { user: Record }, ) { const workbook = xlsx.read(file.buffer, { type: "buffer" }); const sheetName = workbook.SheetNames[0]; 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(); 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 }, ) { const workbook = xlsx.read(file.buffer, { type: "buffer" }); const sheetName = workbook.SheetNames[0]; 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(); 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 }, ) { const workbook = xlsx.read(file.buffer, { type: "buffer" }); const sheetName = workbook.SheetNames[0]; 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(); subDistrict_.push(subD); }), ); await this.subDistrictRepo.save(subDistrict_); return new HttpSuccess(subDistrict_); } /** * @summary ที่อยู่ตามทะเบียนบ้าน-ปัจจุบัน ข้าราชการ */ @Post("uploadProfileAddress-Officer") async UploadFileSQLAddress(@Request() request: { user: Record }) { let rowCount = 0; let profileDatas: any = []; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(20) .getManyAndCount(); // 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 }, }); if (!existingProfile) { return; } 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, }, }); _item.registrationProvinceId = provinceId ? provinceId.id : null_; } } 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: { name: districtRegis_.AMPHUR_NAME, }, }); _item.registrationDistrictId = districtId ? districtId.id : null_; } } 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: { name: subDistrictRegis_.DISTRICT_NAME, }, }); _item.registrationSubDistrictId = subDistrictId ? subDistrictId.id : null_; } } //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, }, }); _item.currentProvinceId = provinceId ? provinceId.id : null_; } } 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: { name: districtCurr_.AMPHUR_NAME, }, }); _item.currentDistrictId = districtId ? districtId.id : null_; } } 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: { name: subDistrictCurr_.DISTRICT_NAME, }, }); _item.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_; } } _item.registrationAddress = existingProfile.H_NUMBER; _item.registrationZipCode = existingProfile.ZIPCODE; _item.currentAddress = existingProfile.CONTACT_H_NUMBER; _item.currentZipCode = existingProfile.CONTACT_ZIPCODE; _item.createdUserId = request.user.sub; _item.createdFullName = request.user.name; _item.lastUpdateUserId = request.user.sub; _item.lastUpdateFullName = request.user.name; _item.createdAt = new Date(); _item.lastUpdatedAt = new Date(); // profileDatas.push(_item); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); await this.profileRepo.save(_item); }), ); // await this.profileRepo.save(profileDatas); // } // console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); return new HttpSuccess(); } /** * @summary ที่อยู่ตามทะเบียนบ้าน-ปัจจุบัน ลูกจ้างประจำ */ @Post("uploadProfileAddress-Employee") async UploadFileSQLAddressEmp(@Request() request: { user: Record }) { let rowCount = 0; let profileDatas: any = []; let null_: any = null; const [profiles, total] = await AppDataSource.getRepository(ProfileEmployee) .createQueryBuilder("profile") .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .skip(0) // .take(20) .getManyAndCount(); // 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 }, }); if (!existingProfile) { return; } 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, }, }); _item.registrationProvinceId = provinceId ? provinceId.id : null_; } } 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: { name: districtRegis_.AMPHUR_NAME, }, }); _item.registrationDistrictId = districtId ? districtId.id : null_; } } 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: { name: subDistrictRegis_.DISTRICT_NAME, }, }); _item.registrationSubDistrictId = subDistrictId ? subDistrictId.id : null_; } } //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, }, }); _item.currentProvinceId = provinceId ? provinceId.id : null_; } } 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: { name: districtCurr_.AMPHUR_NAME, }, }); _item.currentDistrictId = districtId ? districtId.id : null_; } } 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: { name: subDistrictCurr_.DISTRICT_NAME, }, }); _item.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_; } } _item.registrationAddress = existingProfile.H_NUMBER; _item.registrationZipCode = existingProfile.ZIPCODE; _item.currentAddress = existingProfile.CONTACT_H_NUMBER; _item.currentZipCode = existingProfile.CONTACT_ZIPCODE; _item.createdUserId = request.user.sub; _item.createdFullName = request.user.name; _item.lastUpdateUserId = request.user.sub; _item.lastUpdateFullName = request.user.name; _item.createdAt = new Date(); _item.lastUpdatedAt = new Date(); // profileDatas.push(_item); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); await this.profileEmpRepo.save(_item); }), ); // await this.profileEmpRepo.save(profileDatas); // } // console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); return new HttpSuccess(); } /** * @summary ทะเบียนประวัติ ข้าราชการ */ @Post("cleardataupload") async ClearDataUpload(@Request() request: { user: Record }) { 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 positionOfficer = await this.positionOfficerRepo.find({ where: { citizenId: Not(In(allId)) }, take: 1000, skip: 0, }); this.positionOfficerRepo.remove(positionOfficer); // 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); } return new HttpSuccess(); } /** * @summary Import Org */ @Post("ImportOrg") async ImportOrg(@Request() request: { user: Record }) { const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); if (orgRevision == null) return new HttpSuccess(); //create root const IMPORT_CHILD = await this.IMPORT_ORGRepo.find({ where: { orgRoot: Not(""), orgChild1: "", orgChild2: "", orgChild3: "", }, }); await Promise.all( IMPORT_CHILD.map(async (item) => { const orgRoot = new OrgRoot(); orgRoot.orgRootName = item.orgRoot; orgRoot.orgRootShortName = item.orgShortname; const rank: any = item.orgRank; orgRoot.orgRootRank = rank; orgRoot.orgRootRankSub = item.orgSubRank; orgRoot.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgRoot.DIVISION_CODE = item.DIVISION_CODE; orgRoot.SECTION_CODE = item.SECTION_CODE; orgRoot.JOB_CODE = item.JOB_CODE; orgRoot.orgRevisionId = orgRevision.id; orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.createdAt = new Date(); orgRoot.lastUpdatedAt = new Date(); await this.orgRootRepo.save(orgRoot); }), ); //create child1 const IMPORT_CHILD1 = await this.IMPORT_ORGRepo.find({ where: { orgRoot: Not(""), orgChild1: Not(""), orgChild2: "", orgChild3: "", }, }); await Promise.all( IMPORT_CHILD1.map(async (item) => { const orgChild1 = new OrgChild1(); let orgRoot = await this.orgRootRepo.findOne({ where: { orgRootName: item.orgRoot }, }); if (orgRoot == null) { orgRoot = new OrgRoot(); orgRoot.orgRootName = item.orgRoot; orgRoot.orgRootShortName = item.orgShortname; const rank: any = item.orgRank; orgRoot.orgRootRank = rank; orgRoot.orgRootRankSub = item.orgSubRank; orgRoot.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgRoot.DIVISION_CODE = item.DIVISION_CODE; orgRoot.SECTION_CODE = item.SECTION_CODE; orgRoot.JOB_CODE = item.JOB_CODE; orgRoot.orgRevisionId = orgRevision.id; orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.createdAt = new Date(); orgRoot.lastUpdatedAt = new Date(); await this.orgRootRepo.save(orgRoot); } orgChild1.orgRootId = orgRoot.id; orgChild1.orgChild1Name = item.orgChild1; orgChild1.orgChild1ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild1.orgChild1Rank = rank; orgChild1.orgChild1RankSub = item.orgSubRank; orgChild1.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild1.DIVISION_CODE = item.DIVISION_CODE; orgChild1.SECTION_CODE = item.SECTION_CODE; orgChild1.JOB_CODE = item.JOB_CODE; orgChild1.orgRevisionId = orgRevision.id; orgChild1.createdUserId = request.user.sub; orgChild1.createdFullName = request.user.name; orgChild1.lastUpdateUserId = request.user.sub; orgChild1.lastUpdateFullName = request.user.name; orgChild1.createdAt = new Date(); orgChild1.lastUpdatedAt = new Date(); await this.orgChild1Repo.save(orgChild1); }), ); //create child2 const IMPORT_CHILD2 = await this.IMPORT_ORGRepo.find({ where: { orgRoot: Not(""), orgChild1: Not(""), orgChild2: Not(""), orgChild3: "", }, }); await Promise.all( IMPORT_CHILD2.map(async (item) => { const orgChild2 = new OrgChild2(); let orgRoot = await this.orgRootRepo.findOne({ where: { orgRootName: item.orgRoot }, }); if (orgRoot == null) { orgRoot = new OrgRoot(); orgRoot.orgRootName = item.orgRoot; orgRoot.orgRootShortName = item.orgShortname; const rank: any = item.orgRank; orgRoot.orgRootRank = rank; orgRoot.orgRootRankSub = item.orgSubRank; orgRoot.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgRoot.DIVISION_CODE = item.DIVISION_CODE; orgRoot.SECTION_CODE = item.SECTION_CODE; orgRoot.JOB_CODE = item.JOB_CODE; orgRoot.orgRevisionId = orgRevision.id; orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.createdAt = new Date(); orgRoot.lastUpdatedAt = new Date(); await this.orgRootRepo.save(orgRoot); } let orgChild1 = await this.orgChild1Repo.findOne({ where: { orgChild1Name: item.orgChild1 }, }); if (orgChild1 == null) { orgChild1 = new OrgChild1(); orgChild1.orgRootId = orgRoot.id; orgChild1.orgChild1Name = item.orgChild1; orgChild1.orgChild1ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild1.orgChild1Rank = rank; orgChild1.orgChild1RankSub = item.orgSubRank; orgChild1.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild1.DIVISION_CODE = item.DIVISION_CODE; orgChild1.SECTION_CODE = item.SECTION_CODE; orgChild1.JOB_CODE = item.JOB_CODE; orgChild1.orgRevisionId = orgRevision.id; orgChild1.createdUserId = request.user.sub; orgChild1.createdFullName = request.user.name; orgChild1.lastUpdateUserId = request.user.sub; orgChild1.lastUpdateFullName = request.user.name; orgChild1.createdAt = new Date(); orgChild1.lastUpdatedAt = new Date(); await this.orgChild1Repo.save(orgChild1); } orgChild2.orgRootId = orgRoot.id; orgChild2.orgChild1Id = orgChild1.id; orgChild2.orgChild2Name = item.orgChild2; orgChild2.orgChild2ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild2.orgChild2Rank = rank; orgChild2.orgChild2RankSub = item.orgSubRank; orgChild2.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild2.DIVISION_CODE = item.DIVISION_CODE; orgChild2.SECTION_CODE = item.SECTION_CODE; orgChild2.JOB_CODE = item.JOB_CODE; orgChild2.orgRevisionId = orgRevision.id; orgChild2.createdUserId = request.user.sub; orgChild2.createdFullName = request.user.name; orgChild2.lastUpdateUserId = request.user.sub; orgChild2.lastUpdateFullName = request.user.name; orgChild2.createdAt = new Date(); orgChild2.lastUpdatedAt = new Date(); await this.orgChild2Repo.save(orgChild2); }), ); //create child3 const IMPORT_CHILD3 = await this.IMPORT_ORGRepo.find({ where: { orgRoot: Not(""), orgChild1: Not(""), orgChild2: Not(""), orgChild3: Not(""), }, }); await Promise.all( IMPORT_CHILD3.map(async (item) => { const orgChild3 = new OrgChild3(); let orgRoot = await this.orgRootRepo.findOne({ where: { orgRootName: item.orgRoot }, }); if (orgRoot == null) { orgRoot = new OrgRoot(); orgRoot.orgRootName = item.orgRoot; orgRoot.orgRootShortName = item.orgShortname; const rank: any = item.orgRank; orgRoot.orgRootRank = rank; orgRoot.orgRootRankSub = item.orgSubRank; orgRoot.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgRoot.DIVISION_CODE = item.DIVISION_CODE; orgRoot.SECTION_CODE = item.SECTION_CODE; orgRoot.JOB_CODE = item.JOB_CODE; orgRoot.orgRevisionId = orgRevision.id; orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.createdAt = new Date(); orgRoot.lastUpdatedAt = new Date(); await this.orgRootRepo.save(orgRoot); } let orgChild1 = await this.orgChild1Repo.findOne({ where: { orgChild1Name: item.orgChild1 }, }); if (orgChild1 == null) { orgChild1 = new OrgChild1(); orgChild1.orgRootId = orgRoot.id; orgChild1.orgChild1Name = item.orgChild1; orgChild1.orgChild1ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild1.orgChild1Rank = rank; orgChild1.orgChild1RankSub = item.orgSubRank; orgChild1.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild1.DIVISION_CODE = item.DIVISION_CODE; orgChild1.SECTION_CODE = item.SECTION_CODE; orgChild1.JOB_CODE = item.JOB_CODE; orgChild1.orgRevisionId = orgRevision.id; orgChild1.createdUserId = request.user.sub; orgChild1.createdFullName = request.user.name; orgChild1.lastUpdateUserId = request.user.sub; orgChild1.lastUpdateFullName = request.user.name; orgChild1.createdAt = new Date(); orgChild1.lastUpdatedAt = new Date(); await this.orgChild1Repo.save(orgChild1); } let orgChild2 = await this.orgChild2Repo.findOne({ where: { orgChild2Name: item.orgChild2 }, }); if (orgChild2 == null) { orgChild2 = new OrgChild2(); orgChild2.orgRootId = orgRoot.id; orgChild2.orgChild1Id = orgChild1.id; orgChild2.orgChild2Name = item.orgChild2; orgChild2.orgChild2ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild2.orgChild2Rank = rank; orgChild2.orgChild2RankSub = item.orgSubRank; orgChild2.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild2.DIVISION_CODE = item.DIVISION_CODE; orgChild2.SECTION_CODE = item.SECTION_CODE; orgChild2.JOB_CODE = item.JOB_CODE; orgChild2.orgRevisionId = orgRevision.id; orgChild2.createdUserId = request.user.sub; orgChild2.createdFullName = request.user.name; orgChild2.lastUpdateUserId = request.user.sub; orgChild2.lastUpdateFullName = request.user.name; orgChild2.createdAt = new Date(); orgChild2.lastUpdatedAt = new Date(); await this.orgChild2Repo.save(orgChild2); } orgChild3.orgRootId = orgRoot.id; orgChild3.orgChild1Id = orgChild1.id; orgChild3.orgChild2Id = orgChild2.id; orgChild3.orgChild3Name = item.orgChild3; orgChild3.orgChild3ShortName = item.orgShortname; const rank: any = item.orgRank; orgChild3.orgChild3Rank = rank; orgChild3.orgChild3RankSub = item.orgSubRank; orgChild3.DEPARTMENT_CODE = item.DEPARTMENT_CODE; orgChild3.DIVISION_CODE = item.DIVISION_CODE; orgChild3.SECTION_CODE = item.SECTION_CODE; orgChild3.JOB_CODE = item.JOB_CODE; orgChild3.orgRevisionId = orgRevision.id; orgChild3.createdUserId = request.user.sub; orgChild3.createdFullName = request.user.name; orgChild3.lastUpdateUserId = request.user.sub; orgChild3.lastUpdateFullName = request.user.name; orgChild3.createdAt = new Date(); orgChild3.lastUpdatedAt = new Date(); await this.orgChild3Repo.save(orgChild3); }), ); return new HttpSuccess(); } /** * @summary ทะเบียนประวัติ ข้าราชการ */ @Post("mapposition-Officer") async MapPositionOfficer(@Request() request: { user: Record }) { const [officer, total] = await AppDataSource.getRepository(OFFICER) .createQueryBuilder("OFFICER") // .skip(0) // .take(20) .getManyAndCount(); let rowCount = 0; let null_: any = null; let type_: any = null; let level_: any = null; const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); if (orgRevision == null) return new HttpSuccess(); for await (const item of officer) { rowCount++; const existingProfile = await this.profileRepo.findOne({ where: { citizenId: item.id.toString() }, }); if (existingProfile == null) { continue; } const orgRoot = await this.orgRootRepo.findOne({ where: { orgRootName: item.DEPARTMENT_NAME, orgRevisionId: orgRevision.id }, }); if (orgRoot == null) { continue; } const orgChild1 = await this.orgChild1Repo.findOne({ where: { orgChild1Name: item.DIVISION_NAME, orgRoot: { orgRootName: item.DEPARTMENT_NAME }, orgRevisionId: orgRevision.id, }, }); const orgChild2 = await this.orgChild2Repo.findOne({ where: { orgChild2Name: item.SECTION_NAME, orgChild1: { orgChild1Name: item.DIVISION_NAME, orgRoot: { orgRootName: item.DEPARTMENT_NAME }, }, orgRevisionId: orgRevision.id, }, }); const orgChild3 = await this.orgChild3Repo.findOne({ where: { orgChild3Name: item.JOB_NAME, orgChild2: { orgChild2Name: item.SECTION_NAME, orgChild1: { orgChild1Name: item.DIVISION_NAME, orgRoot: { orgRootName: item.DEPARTMENT_NAME }, }, }, orgRevisionId: orgRevision.id, }, }); let posMaster = new PosMaster(); posMaster.orgRootId = orgRoot?.id ?? null_; posMaster.orgChild1Id = orgChild1?.id ?? null_; posMaster.orgChild2Id = orgChild2?.id ?? null_; posMaster.orgChild3Id = orgChild3?.id ?? null_; posMaster.statusReport = "PENDING"; posMaster.isCondition = false; posMaster.isStaff = false; posMaster.isDirector = false; posMaster.isSit = false; posMaster.current_holderId = existingProfile.id; posMaster.posMasterNo = item.SALARY == "" ? null_ : Number(item.POS_NUM_CODE); posMaster.orgRevisionId = orgRevision.id; posMaster.posMasterCreatedAt = new Date(); posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; posMaster.createdAt = new Date(); posMaster.lastUpdatedAt = new Date(); await this.posMasterRepo.save(posMaster); posMaster.ancestorDNA = posMaster.id; await this.posMasterRepo.save(posMaster); let position = new Position(); if (item.MP_CATEGORY) { type_ = await this.posTypeRepo.findOne({ where: { posTypeName: item.MP_CATEGORY }, }); } if (item.MP_LEVEL) { if (type_ == null) { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item.MP_LEVEL, }, }); } else { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item.MP_LEVEL, posTypeId: type_.id, }, }); } } position.posMasterId = posMaster.id; position.isSpecial = false; position.positionIsSelected = true; position.createdUserId = request.user.sub; position.createdFullName = request.user.name; position.lastUpdateUserId = request.user.sub; position.lastUpdateFullName = request.user.name; position.createdAt = new Date(); position.lastUpdatedAt = new Date(); position.positionName = item.WORK_LINE_NAME == "" ? null_ : item.WORK_LINE_NAME; position.posTypeId = type_ != null && type_.posTypeName == item.MP_CATEGORY && type_ ? type_.id : null; position.posLevelId = level_ != null && level_.posLevelName == item.MP_LEVEL && level_ ? level_.id : null; await this.positionRepo.save(position); } return new HttpSuccess(); } }