diff --git a/package-lock.json b/package-lock.json index 84f21d96..b3be71d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@tsoa/runtime": "^6.0.0", "axios": "^1.7.2", "cors": "^2.8.5", + "csv-parser": "^3.0.0", "dotenv": "^16.3.1", "express": "^4.18.2", "fast-jwt": "^3.3.2", @@ -1242,6 +1243,20 @@ "node": ">= 8" } }, + "node_modules/csv-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.0.0.tgz", + "integrity": "sha512-s6OYSXAK3IdKqYO33y09jhypG/bSDHPuyCme/IdEHfWpLf/jKcpitVFyOC6UemgGk8v7Q5u2XE0vvwmanxhGlQ==", + "dependencies": { + "minimist": "^1.2.0" + }, + "bin": { + "csv-parser": "bin/csv-parser" + }, + "engines": { + "node": ">= 10" + } + }, "node_modules/dayjs": { "version": "1.11.10", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", diff --git a/package.json b/package.json index 5eb3416e..88ad52ac 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@tsoa/runtime": "^6.0.0", "axios": "^1.7.2", "cors": "^2.8.5", + "csv-parser": "^3.0.0", "dotenv": "^16.3.1", "express": "^4.18.2", "fast-jwt": "^3.3.2", diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 586831d9..6d98905c 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -14,7 +14,7 @@ import { UploadedFile, } from "tsoa"; import { AppDataSource } from "../database/data-source"; -import { Brackets } from "typeorm"; +import { Brackets, Double } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; @@ -38,7 +38,16 @@ import { calculateRetireYear, removeProfileInOrganize, } from "../interfaces/utils"; -import { EducationMis } from "../entities/EducationMis"; +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"; @Route("api/v1/org/upload") @Tags("UPLOAD") @@ -53,73 +62,106 @@ export class ImportDataController extends Controller { private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); private posLevelRepo = AppDataSource.getRepository(PosLevel); private posTypeRepo = AppDataSource.getRepository(PosType); - private educationMisRepo = AppDataSource.getRepository(EducationMis); - + private HR_POSITION_OFFICERRepo = AppDataSource.getRepository(HR_POSITION_OFFICER); + private HR_PERSONAL_OFFICER_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_OFFICER_FAMILY); + /** * @summary ทะเบียนประวัติ ข้าราชการ */ @Post("uploadProfile-Officer") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSqlOfficer( - @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 getProFile = xlsx.utils.sheet_to_json(sheet); + 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; - await Promise.all( - getProFile.map(async (item: any) => { - let type_: any = null; - let level_: any = null; - let dateRetire: any = null; - const profile = new Profile(); - const existingProfile = await this.profileRepo.findOne({ - where: { citizenId: item["ID"] }, - }); - if (existingProfile) { - return; - } - if(item["MP_CEE_TYPE"]) { - type_ = await this.posTypeRepo.findOne({ - where: { posTypeName: item["MP_CEE_TYPE"] } - }) - } - if(item["MP_CEE_POSITION"]) { - level_ = await this.posLevelRepo.findOne({ - where: { - posLevelName: item["MP_CEE_POSITION"], - posTypeId: type_.id - } - }) - } + for await (const item of readStream) { + // readStream.map(async (item: any) => { + rowCount++; + let type_: any = null; + let level_: any = null; + const profile = new Profile(); + const existingProfile = await this.profileRepo.findOne({ + where: { citizenId: item["ID"] }, + }); + if (existingProfile) { + continue; + } + if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) { + continue; + } + if (item["FLAG_PERSON_TYPE"] != "1") { + continue; + } - dateRetire = Extension.ConvertToDateTime(item["MP_FORCE_DATE"]); - 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["MP_FORCE_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["MP_FORCE_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_CEE_TYPE"] ? type_.id : null; - profile.posLevelId = level_ != null && level_.posLevelName == item["MP_CEE_POSITION"] ? level_.id : null; - profile.relationship = item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]); - profile.isLeave = item["FLAG_RETIRE_STATUS"] == "" || item["FLAG_RETIRE_STATUS"] == 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); - }), - ); + if (item["MP_CEE_TYPE"]) { + type_ = await this.posTypeRepo.findOne({ + where: { posTypeName: item["MP_CEE_TYPE"] }, + }); + } + if (item["MP_CEE_POSITION"]) { + if (type_ == null) { + level_ = await this.posLevelRepo.findOne({ + where: { + posLevelName: item["MP_CEE_POSITION"], + }, + }); + } else { + level_ = await this.posLevelRepo.findOne({ + where: { + posLevelName: item["MP_CEE_POSITION"], + posTypeId: type_.id, + }, + }); + } + } + + let dateRetire = Extension.ConvertToDateTime(item["MP_FORCE_DATE"]); + 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["MP_FORCE_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["MP_FORCE_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_CEE_TYPE"] && type_ ? type_.id : null; + profile.posLevelId = + level_ != null && level_.posLevelName == item["MP_CEE_POSITION"] && level_ + ? level_.id + : null; + profile.relationship = + item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]); + profile.isLeave = + item["FLAG_RETIRE_STATUS"] == "" || item["FLAG_RETIRE_STATUS"] == 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 = []; + if (global.gc) { + global.gc(); + } + } + } + // ) + // ); + console.log(rowCount); await this.profileRepo.save(profiles); return new HttpSuccess(); } @@ -128,50 +170,68 @@ export class ImportDataController extends Controller { * @summary ทะเบียนประวัติ ลูกจ้างประจำ */ @Post("uploadProfile-Employee") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSQL( - @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 getProFile = xlsx.utils.sheet_to_json(sheet); + 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 dateRetire: any = null; - await Promise.all( - getProFile.map(async (item: any) => { - const profileEmp = new ProfileEmployee(); - const existingProfile = await this.profileEmpRepo.findOne({ - where: { citizenId: item["ID"] }, - }); - if (existingProfile) { - return; + let null_: any = null; + + for await (const item of readStream) { + rowCount++; + const profileEmp = new ProfileEmployee(); + const existingProfile = await this.profileEmpRepo.findOne({ + where: { citizenId: item["ID"] }, + }); + console.log(item); + console.log(item["ID"]); + // console.log(existingProfile); + if (existingProfile) { + continue; + } + if (item["FLAG_PERSON_TYPE"] != "6") { + 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 = []; + if (global.gc) { + global.gc(); } - 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); await this.profileEmpRepo.save(profiles); return new HttpSuccess(); } @@ -180,98 +240,163 @@ export class ImportDataController extends Controller { * @summary เงินเดือน ข้าราชการ */ @Post("uploadProfileSalary-Officer") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSQLSalary( - @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); + async UploadFileSQLSalary(@Request() request: { user: Record }) { + let rowCount = 0; let profileSalarys: any = []; - let null_: any=null - await Promise.all( - getExcel - .filter((x:any) => x.FLAG_PERSON_TYPE == 1) - .map(async (item: any) => { + let null_: any = null; - const profileSalary = new ProfileSalary(); - const existingProfile = await this.profileRepo.findOne({ - where: { citizenId: item.ID }, - }); - if (!existingProfile) { - return; - } - - profileSalary.date = item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE); - profileSalary.amount = item.SALARY; - profileSalary.profileId = existingProfile.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; - profileSalary.order = item.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(profileSalarys); - return new HttpSuccess(); + const [_profiles, total] = await AppDataSource.getRepository(Profile) + .createQueryBuilder("profile") + .select(["profile.id"]) + .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(profileSalarys); + profileSalarys = []; + }), + ); + } + console.log(rowCount); + return new HttpSuccess(profileSalarys); } /** * @summary เงินเดือน ลูกจ้างประจำ */ @Post("uploadProfileSalary-Employee") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSQLSalaryEmp( - @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); + async UploadFileSQLSalaryEmp(@Request() request: { user: Record }) { + let rowCount = 0; let profileSalarys: any = []; - let null_: any=null - await Promise.all( - getExcel - .filter((x:any) => x.FLAG_PERSON_TYPE == 6) - .map(async (item: any) => { + let null_: any = null; - const profileSalary = new ProfileSalary(); - const existingProfile = await this.profileEmpRepo.findOne({ - where: { citizenId: item.ID }, - }); - if (!existingProfile) { - return; - } - - profileSalary.date = item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE); - profileSalary.amount = item.SALARY; - profileSalary.profileEmployeeId = existingProfile.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; - profileSalary.order = item.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); - }), - ); + const [_profiles, total] = await AppDataSource.getRepository(ProfileEmployee) + .createQueryBuilder("profile") + .select(["profile.id"]) + .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); + console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); + }), + ); + await this.salaryRepository.save(profileSalarys); + profileSalarys = []; + }), + ); + } + console.log(rowCount); await this.salaryRepository.save(profileSalarys); return new HttpSuccess(); } @@ -280,76 +405,128 @@ export class ImportDataController extends Controller { * @summary ครอบครัว ข้าราชการ */ @Post("uploadProfileFamily-Officer") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSQLFamily( - @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); + async UploadFileSQLFamily(@Request() request: { user: Record }) { + let rowCount = 0; let fathers: any = []; let mothers: any = []; let couples: any = []; - let null_: any = null - await Promise.all( - getExcel.map(async (item: any) => { - const profileFather = new ProfileFamilyFather(); - const profileMother = new ProfileFamilyMother(); - const profileCouple = new ProfileFamilyCouple(); - const existingProfile = await this.profileRepo.findOne({ - where: { citizenId: item.ID }, - }); - if (!existingProfile) { - return; - } - profileFather.profileId = existingProfile.id; - profileFather.fatherPrefix = item.FATHER_RANK_NAME; - profileFather.fatherFirstName = item.FATHER_FNAME; - profileFather.fatherLastName = item.FATHER_LNAME; - profileFather.createdUserId = request.user.sub; - profileFather.createdFullName = request.user.name; - profileFather.lastUpdateUserId = request.user.sub; - profileFather.lastUpdateFullName = request.user.name + const [_profiles, total] = await AppDataSource.getRepository(Profile) + .createQueryBuilder("profile") + .select(["profile.id"]) + .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(); - profileMother.profileId = existingProfile.id; - profileMother.motherPrefix = item.MOTHER_RANK_NAME; - profileMother.motherFirstName = item.MOTHER_FNAME; - profileMother.motherLastName = item.MOTHER_LNAME; - profileMother.createdUserId = request.user.sub; - profileMother.createdFullName = request.user.name; - profileMother.lastUpdateUserId = request.user.sub; - profileMother.lastUpdateFullName = request.user.name + 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; + } - profileCouple.profileId = existingProfile.id; - profileCouple.couplePrefix = item.SPOUSE_RANK_NAME; - profileCouple.coupleFirstName = item.SPOUSE_FNAME; - profileCouple.coupleLastName = item.SPOUSE_LNAME; - profileCouple.coupleCitizenId = item.SPOUSE_ID; - profileCouple.relationship = item.MARRIAGE_STATE; - profileCouple.coupleLive = item.LIFE_SPOUSE; - profileCouple.createdUserId = request.user.sub; - profileCouple.createdFullName = request.user.name; - profileCouple.lastUpdateUserId = request.user.sub; - profileCouple.lastUpdateFullName = request.user.name + // fathers = await []; + // mothers = await []; + // couples = await []; + // await Promise.all( + // existingProfile.map(async (item) => { + rowCount++; + const profileFather = new ProfileFamilyFather(); + const profileMother = new ProfileFamilyMother(); + const profileCouple = new ProfileFamilyCouple(); - fathers.push(profileFather); - mothers.push(profileMother); - couples.push(profileCouple); - }), - ); - const result = { - fathers: fathers, - mothers: mothers, - couples: couples, - }; + 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(fathers); + fathers = await []; + // if (global.gc) { + // global.gc(); + // } + } + if (mothers.length === BATCH_SIZE) { + await this.profileFamilyMotherRepository.save(mothers); + mothers = await []; + // if (global.gc) { + // global.gc(); + // } + } + if (couples.length === BATCH_SIZE) { + await this.profileFamilyCoupleRepository.save(couples); + couples = await []; + // if (global.gc) { + // global.gc(); + // } + } + // }), + // ); + // await this.salaryRepository.save(profileSalarys); + // profileSalarys = []; + }), + ); + } + // } + console.log(rowCount); await Promise.all([ - this.profileFamilyFatherRepository.save(fathers), - this.profileFamilyMotherRepository.save(mothers), - this.profileFamilyCoupleRepository.save(couples) + this.profileFamilyFatherRepository.save(fathers), + this.profileFamilyMotherRepository.save(mothers), + this.profileFamilyCoupleRepository.save(couples), ]); return new HttpSuccess(); @@ -359,76 +536,109 @@ export class ImportDataController extends Controller { * @summary ครอบครัว ลูกจ้างประจำ */ @Post("uploadProfileFamily-Employee") - @UseInterceptors(FileInterceptor("file")) - async UploadFileSQLFamilyEmp( - @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); + async UploadFileSQLFamilyEmp(@Request() request: { user: Record }) { + let rowCount = 0; let fathers: any = []; let mothers: any = []; let couples: any = []; - let null_: any = null - await Promise.all( - getExcel.map(async (item: any) => { - const profileFather = new ProfileFamilyFather(); - const profileMother = new ProfileFamilyMother(); - const profileCouple = new ProfileFamilyCouple(); - const existingProfile = await this.profileEmpRepo.findOne({ - where: { citizenId: item.ID }, - }); - if (!existingProfile) { - return; - } - profileFather.profileEmployeeId = existingProfile.id; - profileFather.fatherPrefix = item.FATHER_RANK_NAME; - profileFather.fatherFirstName = item.FATHER_FNAME; - profileFather.fatherLastName = item.FATHER_LNAME; - profileFather.createdUserId = request.user.sub; - profileFather.createdFullName = request.user.name; - profileFather.lastUpdateUserId = request.user.sub; - profileFather.lastUpdateFullName = request.user.name + const [_profiles, total] = await AppDataSource.getRepository(ProfileEmployee) + .createQueryBuilder("profile") + .select(["profile.id"]) + .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(); - profileMother.profileEmployeeId = existingProfile.id; - profileMother.motherPrefix = item.MOTHER_RANK_NAME; - profileMother.motherFirstName = item.MOTHER_FNAME; - profileMother.motherLastName = item.MOTHER_LNAME; - profileMother.createdUserId = request.user.sub; - profileMother.createdFullName = request.user.name; - profileMother.lastUpdateUserId = request.user.sub; - profileMother.lastUpdateFullName = request.user.name + 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; + } - profileCouple.profileEmployeeId = existingProfile.id; - profileCouple.couplePrefix = item.SPOUSE_RANK_NAME; - profileCouple.coupleFirstName = item.SPOUSE_FNAME; - profileCouple.coupleLastName = item.SPOUSE_LNAME; - profileCouple.coupleCitizenId = item.SPOUSE_ID; - profileCouple.relationship = item.MARRIAGE_STATE; - profileCouple.coupleLive = item.LIFE_SPOUSE; - profileCouple.createdUserId = request.user.sub; - profileCouple.createdFullName = request.user.name; - profileCouple.lastUpdateUserId = request.user.sub; - profileCouple.lastUpdateFullName = request.user.name + rowCount++; + const profileFather = new ProfileFamilyFather(); + const profileMother = new ProfileFamilyMother(); + const profileCouple = new ProfileFamilyCouple(); - fathers.push(profileFather); - mothers.push(profileMother); - couples.push(profileCouple); - }), - ); - const result = { - fathers: fathers, - mothers: mothers, - couples: couples, - }; + 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(fathers); + fathers = await []; + } + if (mothers.length === BATCH_SIZE) { + await this.profileFamilyMotherRepository.save(mothers); + mothers = await []; + } + if (couples.length === BATCH_SIZE) { + await this.profileFamilyCoupleRepository.save(couples); + couples = await []; + } + }), + ); + } + console.log(rowCount); await Promise.all([ - this.profileFamilyFatherRepository.save(fathers), - this.profileFamilyMotherRepository.save(mothers), - this.profileFamilyCoupleRepository.save(couples) + this.profileFamilyFatherRepository.save(fathers), + this.profileFamilyMotherRepository.save(mothers), + this.profileFamilyCoupleRepository.save(couples), ]); return new HttpSuccess(); @@ -448,7 +658,7 @@ export class ImportDataController extends Controller { // 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(); diff --git a/src/controllers/ProfileEditEmployeeController.ts b/src/controllers/ProfileEditEmployeeController.ts index 01d2fc98..40901a9a 100644 --- a/src/controllers/ProfileEditEmployeeController.ts +++ b/src/controllers/ProfileEditEmployeeController.ts @@ -33,7 +33,7 @@ export class ProfileEditEmployeeController extends Controller { private profileEditRepository = AppDataSource.getRepository(ProfileEdit); @Get("user") - public async detailProfileEditUser( + public async detailProfileEditUserEmp( @Request() request: { user: Record }, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @@ -94,7 +94,7 @@ export class ProfileEditEmployeeController extends Controller { } @Get("admin") - public async detailProfileEditAdmin( + public async detailProfileEditAdminEmp( @Request() request: { user: Record }, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @@ -159,7 +159,7 @@ export class ProfileEditEmployeeController extends Controller { } @Get("{profileEmployeeId}") - public async detailProfileEdit(@Path() profileEmployeeId: string) { + public async detailProfileEditEmp(@Path() profileEmployeeId: string) { const getProfileEdit = await this.profileEditRepository.findOne({ where: { profileEmployeeId: profileEmployeeId }, relations: ["profileEmployee"], diff --git a/src/entities/HR_PERSONAL_OFFICER_FAMILY.ts b/src/entities/HR_PERSONAL_OFFICER_FAMILY.ts new file mode 100644 index 00000000..c772ed39 --- /dev/null +++ b/src/entities/HR_PERSONAL_OFFICER_FAMILY.ts @@ -0,0 +1,90 @@ +import { Entity, Column, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm"; + +@Entity("HR_PERSONAL_OFFICER_FAMILY") +export class HR_PERSONAL_OFFICER_FAMILY { + @Column({ + nullable: true, + type: "text", + default: null, + }) + CIT: string; + @PrimaryGeneratedColumn() + id!: number; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + FATHER_RANK_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + FATHER_FNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + FATHER_LNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MOTHER_RANK_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MOTHER_FNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MOTHER_LNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SPOUSE_RANK_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SPOUSE_FNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SPOUSE_LNAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SPOUSE_ID: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MARRIAGE_STATE: string; +} diff --git a/src/entities/HR_POSITION_OFFICER.ts b/src/entities/HR_POSITION_OFFICER.ts new file mode 100644 index 00000000..622145a3 --- /dev/null +++ b/src/entities/HR_POSITION_OFFICER.ts @@ -0,0 +1,90 @@ +import { Entity, Column, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm"; + +@Entity("HR_POSITION_OFFICER") +export class HR_POSITION_OFFICER { + @Column({ + nullable: true, + type: "text", + default: null, + }) + CIT: string; + @PrimaryGeneratedColumn() + id!: number; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MP_POS_DATE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SALARY: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MP_COMMAND_NUM: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + POS_NUM_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + POS_NUM_CODE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + FLAG_TO_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + WORK_LINE_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SPECIALIST_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + ADMIN_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + REMARK: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + ORDER_MOVE_POSITION: string; +}