import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, Query, UploadedFile, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { Brackets, Double } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; 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 { calculateAge, calculateRetireDate, calculateRetireLaw, calculateRetireYear, removeProfileInOrganize, } from "../interfaces/utils"; import * as fs from "fs"; import * as path from "path"; import csv from "csv-parser"; // src/importCsv.ts 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"; import moment from "moment"; 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"; @Route("api/v1/org/upload") @Tags("UPLOAD") @Security("bearerAuth") export class ImportDataController extends Controller { private educationRepository = AppDataSource.getRepository(ProfileEducation); 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); private HR_POSITION_OFFICERRepo = AppDataSource.getRepository(HR_POSITION_OFFICER); 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); /** * @summary ทะเบียนประวัติ ข้าราชการ */ @Post("uploadProfile-Officer") async UploadFileSqlOfficer(@Request() request: { user: Record }) { 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; for await (const item of readStream) { // readStream.map(async (item: any) => { rowCount++; let type_: any = null; let level_: any = null; const profile = new Profile(); // if (item["FLAG_RETIRE_STATUSxxxx"] != "" && item["FLAG_RETIRE_STATUSxxxx"] != null) { // continue; // } // if (item["FLAG_PERSON_TYPExxxx"] != "1") { // continue; // } const existingProfile = await this.profileRepo.findOne({ where: { citizenId: item["ID"] }, }); if (existingProfile) { continue; } if (item["TYPE"]) { type_ = await this.posTypeRepo.findOne({ where: { posTypeName: item["TYPE"] }, }); } if (item["LEVEL"]) { if (type_ == null) { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item["LEVEL"], }, }); } else { level_ = await this.posLevelRepo.findOne({ where: { posLevelName: item["LEVEL"], posTypeId: type_.id, }, }); } } let dateRetire = Extension.ConvertToDateTime(item["BORN"]); profile.citizenId = item["ID"] == "" ? "" : item["ID"]; profile.rank = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; profile.prefix = 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_ : Extension.ConvertToDateTime(item["BORN"]); profile.dateAppoint = item["BEGIN_ENTRY_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["BEGIN_ENTRY_DATE"]); profile.dateStart = item["BEGIN_ENTRY_DATE"] == "" ? null_ : Extension.ConvertToDateTime(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["TYPE"] && type_ ? type_.id : null; profile.posLevelId = level_ != null && level_.posLevelName == item["LEVEL"] && level_ ? level_.id : null; profile.relationship = item["สถานภาพ"] == "" ? "" : Extension.CheckRelationship(item["สถานภาพ"]); // 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; profiles.push(profile); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); if (profiles.length === BATCH_SIZE) { await this.profileRepo.save(profiles); 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; for await (const item of readStream) { rowCount++; const profileEmp = new ProfileEmployee(); // if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) { // continue; // } if (item["FLAG_PERSON_TYPE"] != "6") { continue; } const existingProfile = await this.profileEmpRepo.findOne({ where: { citizenId: item["ID"] }, }); if (existingProfile) { continue; } let dateRetire = Extension.ConvertToDateTime(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"] == "" ? null : item["RANK_NAME"]; profileEmp.prefix = 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_ : Extension.ConvertToDateTime(item["BORN"]); profileEmp.dateAppoint = item["BEGIN_ENTRY_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["BEGIN_ENTRY_DATE"]); profileEmp.dateStart = item["MP_FORCE_DATE"] == "" ? null_ : Extension.ConvertToDateTime(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.createdUserId = request.user.sub; profileEmp.createdFullName = request.user.name; profileEmp.lastUpdateUserId = request.user.sub; profileEmp.lastUpdateFullName = request.user.name; profiles.push(profileEmp); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); if (profiles.length === BATCH_SIZE) { await this.profileEmpRepo.save(profiles); 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 profileSalarys: 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 <= 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_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", ], }); // profileSalarys = await []; await Promise.all( existingProfile.map(async (item) => { rowCount++; const profileSalary = new ProfileSalary(); profileSalary.date = item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE); 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; profileSalarys.push(profileSalary); // await this.salaryRepository.save(profileSalary); if (profileSalarys.length === BATCH_SIZE) { await this.salaryRepository.save(profileSalarys); profileSalarys = await []; } }), ); // await this.salaryRepository.save(profileSalarys); // profileSalarys = await []; }), ); await this.salaryRepository.save(profileSalarys); // } console.log(rowCount); return new HttpSuccess(); } /** * @summary เงินเดือน ลูกจ้างประจำ */ @Post("uploadProfileSalary-Employee") async UploadFileSQLSalaryEmp(@Request() request: { user: Record }) { let rowCount = 0; let profileSalarys: 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_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", ], }); profileSalarys = await []; await Promise.all( existingProfile.map(async (item) => { rowCount++; const profileSalary = new ProfileSalary(); profileSalary.date = item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE); 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; 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; profileSalarys.push(profileSalary); // await this.salaryRepository.save(profileSalary); if (profileSalarys.length === BATCH_SIZE) { await this.salaryRepository.save(profileSalarys); profileSalarys = await []; } console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); }), ); // await this.salaryRepository.save(profileSalarys); // profileSalarys = await []; }), ); // } await this.salaryRepository.save(profileSalarys); 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(20) .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; 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; 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; // 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; // 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; 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; 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; // 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; // 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_.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"], }); // 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.ConvertToDateTime(item.START_EDUCATION_YEAR) : null_; startDate = startDate ? new Date(startDate, 0, 1) : null_; let endDate = item.EDUCATION_YEAR ? Extension.ConvertToDateTime(item.EDUCATION_YEAR) : null_; endDate = endDate ? new Date(endDate, 0, 1) : null_; 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; // await educations.push(await education); await this.educationRepository.save(await education); }), ); // await this.educationRepository.save(educations); // educations = await []; }), ); // } console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // await this.educationRepository.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"], }); 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.ConvertToDateTime(item.START_EDUCATION_YEAR) : null_; startDate = startDate ? new Date(startDate, 0, 1) : null_; let endDate = item.EDUCATION_YEAR ? Extension.ConvertToDateTime(item.EDUCATION_YEAR) : null_; endDate = endDate ? new Date(endDate, 0, 1) : null_; 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; educations.push(education); }), ); await this.educationRepository.save(educations); }), ); // } console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); // await this.educationRepository.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; 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; 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; 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.lastUpdateUserId = request.user.sub; _item.lastUpdateFullName = request.user.name; // profileDatas.push(_item); 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) { 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) { 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) { 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) { 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.lastUpdateUserId = request.user.sub; _item.lastUpdateFullName = request.user.name; // profileDatas.push(_item); await this.profileEmpRepo.save(_item); }), ); // await this.profileEmpRepo.save(profileDatas); // } console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); return new HttpSuccess(); } }