diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 6d98905c..50af44f6 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -20,16 +20,16 @@ import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { UseInterceptors } from "@nestjs/common"; import { Profile } from "../entities/Profile"; -import { FileInterceptor } from "@nestjs/platform-express"; -import * as xlsx from "xlsx"; -import { ProfileEducation } from "../entities/ProfileEducation"; +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 { ProfileEmployee } from "../entities/ProfileEmployee"; +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, @@ -48,6 +48,14 @@ import { HR_PERSONAL_OFFICER_FAMILY } from "../entities/HR_PERSONAL_OFFICER_FAMI 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"; @Route("api/v1/org/upload") @Tags("UPLOAD") @@ -65,6 +73,13 @@ export class ImportDataController extends Controller { private HR_POSITION_OFFICERRepo = AppDataSource.getRepository(HR_POSITION_OFFICER); private HR_PERSONAL_OFFICER_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_OFFICER_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 ทะเบียนประวัติ ข้าราชการ */ @@ -195,6 +210,7 @@ export class ImportDataController extends Controller { } 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" : ""; @@ -644,322 +660,515 @@ export class ImportDataController extends Controller { return new HttpSuccess(); } - // /** - // * @summary Import Education Code - // */ - // @Post("uploadEducation-Code") - // @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 = []; + /** + * @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_CODE = item.EDUCATION_CODE; - // educationMis_.push(educationMis); + 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_); + } - // }), - // ); - // await this.educationMisRepo.save(educationMis_); - // return new HttpSuccess(educationMis_); - // } + /** + * @summary ประวัติการศึกษา ข้าราชการ + */ + @Post("uploadProfileEducation-Officer") + @UseInterceptors(FileInterceptor("file")) + async UploadFileSQLEducation( + @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 getEducations = xlsx.utils.sheet_to_json(sheet); + let educations: any = []; + let null_: any = null; + await Promise.all( + getEducations.map(async (item: any) => { + const education = new ProfileEducation(); + const existingProfile = await this.profileRepo.findOne({ + where: { citizenId: item.ID }, + }); + if (!existingProfile) { + return; + } + const educationCode = await this.educationMisRepo.findOne({ + where: { EDUCATION_CODE: item.EDUCATION_CODE }, + }); - // /** - // * API upload EDU - // * - // * @summary DEV_0 - upload EDU # - // * - // */ - // @Post("") - // @UseInterceptors(FileInterceptor("file")) - // async UploadUserDevelopemtById(@UploadedFile() file: Express.Multer.File) { - // const workbook = xlsx.read(file.buffer, { type: "buffer" }); - // const sheetName = workbook.SheetNames[1]; - // const sheet = workbook.Sheets[sheetName]; - // const getEducations = xlsx.utils.sheet_to_json(sheet); + let startDate = item.START_EDUCATION_YEAR + ? Extension.ConvertToDateTime(item.START_EDUCATION_YEAR) + : null_; + startDate = startDate ? new Date(startDate, 0, 1) : null_; - // await Promise.all( - // getEducations.map(async (item: any) => { - // if (item["id"] === undefined) { - // return; - // } - // const education = new ProfileEducation(); + let endDate = item.EDUCATION_YEAR + ? Extension.ConvertToDateTime(item.EDUCATION_YEAR) + : null_; + endDate = endDate ? new Date(endDate, 0, 1) : null_; - // education.id = item["id"]; - // education.createdAt = item["createdAt"]; - // education.createdUserId = item["createdUserId"]; - // education.lastUpdatedAt = item["lastUpdatedAt"]; - // education.lastUpdateUserId = item["lastUpdateUserId"]; - // education.createdFullName = item["createdFullName"]; - // education.lastUpdateFullName = item["lastUpdateFullName"]; - // education.profileId = item["profileId"]; - // education.country = item["country"]; - // education.degree = item["degree"]; - // education.duration = item["duration"]; - // education.durationYear = item["durationYear"]; - // education.field = item["field"]; - // education.finishDate = item["finishDate"]; - // education.fundName = item["fundName"]; - // education.institute = item["institute"]; - // education.other = item["other"]; - // education.startDate = item["startDate"]; - // education.endDate = item["endDate"]; - // education.educationLevel = item["educationLevel"]; - // education.educationLevelId = item["educationLevelId"]; - // education.positionPath = item["positionPath"]; - // education.positionPathId = item["positionPathId"]; - // education.isDate = item["isDate"]; - // education.isEducation = item["isEducation"]; - // education.note = item["note"]; - // education.profileEmployeeId = item["profileEmployeeId"]; + education.profileId = existingProfile.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); + return new HttpSuccess(); + } - // await this.educationRepository.save(education); - // }), - // ); - // } + /** + * @summary ประวัติการศึกษา ลูกจ้างประจำ + */ + @Post("uploadProfileEducation-Employee") + @UseInterceptors(FileInterceptor("file")) + async UploadFileSQLEducationEmp( + @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 getEducations = xlsx.utils.sheet_to_json(sheet); + let educations: any = []; + let null_: any = null; + await Promise.all( + getEducations.map(async (item: any) => { + const education = new ProfileEducation(); + const existingProfile = await this.profileEmpRepo.findOne({ + where: { citizenId: item.ID }, + }); + if (!existingProfile) { + return; + } + const educationCode = await this.educationMisRepo.findOne({ + where: { EDUCATION_CODE: item.EDUCATION_CODE }, + }); - // @Post("uploadSQLEdu") - // @UseInterceptors(FileInterceptor("file")) - // async UploadFileSQLEdu( - // @UploadedFile() file: Express.Multer.File, - // @Request() request: { user: Record }, - // ) { - // const workbook = xlsx.read(file.buffer, { type: "buffer" }); - // const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet - // const sheet = workbook.Sheets[sheetName]; - // const getProFile = xlsx.utils.sheet_to_json(sheet); - // let profiles: any = []; - // await Promise.all( - // getProFile.map(async (item: any) => { - // // Create a new Profile entity and assign fields from the parsed data + let startDate = item.START_EDUCATION_YEAR + ? Extension.ConvertToDateTime(item.START_EDUCATION_YEAR) + : null_; + startDate = startDate ? new Date(startDate, 0, 1) : null_; - // const profile = new ProfileEducation(); - // // Check if the profile already exists - // const existingProfile = await this.educationRepository.findOne({ - // where: { id: item["id"] }, - // }); - // // If profile exists, skip saving - // if (existingProfile) { - // return; - // } - // // Assign fields from the item to the profile entit + let endDate = item.EDUCATION_YEAR + ? Extension.ConvertToDateTime(item.EDUCATION_YEAR) + : null_; + endDate = endDate ? new Date(endDate, 0, 1) : null_; - // profile.id = item["id"] == "NULL" ? null : item["id"]; - // // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]); - // profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"]; - // // profile.lastUpdatedAt = - // // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]); - // profile.lastUpdateUserId = - // item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"]; - // profile.createdFullName = - // item["createdFullName"] == "NULL" ? null : item["createdFullName"]; - // profile.lastUpdateFullName = - // item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"]; - // profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"]; - // profile.country = item["country"] == "NULL" ? null : item["country"]; - // profile.degree = item["degree"] == "NULL" ? null : item["degree"]; - // profile.duration = item["duration"] == "NULL" ? null : item["duration"]; - // profile.durationYear = item["durationYear"] == "NULL" ? null : item["durationYear"]; - // profile.field = item["field"] == "NULL" ? null : item["field"]; - // // profile.finishDate = item["finishDate"] == "NULL" ? null : new Date(item["finishDate"]); - // profile.fundName = item["fundName"] == "NULL" ? null : item["fundName"]; - // profile.gpa = item["gpa"] == "NULL" ? null : item["gpa"]; - // profile.institute = item["institute"] == "NULL" ? null : item["institute"]; - // profile.other = item["other"] == "NULL" ? null : item["other"]; - // // profile.startDate = item["startDate"] == "NULL" ? null : new Date(item["startDate"]); - // // profile.endDate = item["endDate"] == "NULL" ? null : new Date(item["endDate"]); - // profile.educationLevel = item["educationLevel"] == "NULL" ? null : item["educationLevel"]; - // profile.educationLevelId = - // item["educationLevelId"] == "NULL" ? null : item["educationLevelId"]; - // profile.positionPath = item["positionPath"] == "NULL" ? null : item["positionPath"]; - // profile.positionPathId = item["positionPathId"] == "NULL" ? null : item["positionPathId"]; - // profile.isDate = item["isDate"] == "NULL" ? null : item["isDate"]; - // profile.isEducation = item["isEducation"] == "NULL" ? null : item["isEducation"]; - // profile.note = item["note"] == "NULL" ? null : item["note"]; - // profile.profileEmployeeId = - // item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"]; + education.profileEmployeeId = existingProfile.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); + return new HttpSuccess(); + } - // profiles.push(profile); - // }), - // ); - // await this.educationRepository.save(profiles); - // return new HttpSuccess(getProFile); - // } + /** + * @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 = []; - // @Post("uploadSQLFamilly-Father") - // @UseInterceptors(FileInterceptor("file")) - // async UploadFileSQLFamilly( - // @UploadedFile() file: Express.Multer.File, - // // @Request() request: { user: Record }, - // ) { - // const workbook = xlsx.read(file.buffer, { type: "buffer" }); - // const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet - // const sheet = workbook.Sheets[sheetName]; - // const getProFile = xlsx.utils.sheet_to_json(sheet); - // let profiles: any = []; - // await Promise.all( - // getProFile.map(async (item: any) => { - // // Create a new Profile entity and assign fields from the parsed data + 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_); + } - // const profile = new ProfileFamilyFather(); - // // Check if the profile already exists - // const existingProfile = await this.profileFamilyFatherRepository.findOne({ - // where: { id: item["id"] }, - // }); - // // If profile exists, skip saving - // if (existingProfile) { - // return; - // } - // // Assign fields from the item to the profile entit + /** + * @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 = []; - // profile.id = item["id"] == "NULL" ? null : item["id"]; - // // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]); - // profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"]; - // // profile.lastUpdatedAt = - // // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]); - // profile.lastUpdateUserId = - // item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"]; - // profile.createdFullName = - // item["createdFullName"] == "NULL" ? null : item["createdFullName"]; - // profile.lastUpdateFullName = - // item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"]; - // profile.fatherPrefix = item["fatherPrefix"] == "NULL" ? null : item["fatherPrefix"]; - // profile.fatherFirstName = - // item["fatherFirstName"] == "NULL" ? null : item["fatherFirstName"]; - // profile.fatherLastName = item["fatherLastName"] == "NULL" ? null : item["fatherLastName"]; - // profile.fatherCareer = item["fatherCareer"] == "NULL" ? null : item["fatherCareer"]; - // profile.fatherCitizenId = - // item["fatherCitizenId"] == "NULL" ? null : item["fatherCitizenId"]; - // profile.fatherLive = item["fatherLive"] == "NULL" ? null : item["fatherLive"]; - // profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"]; - // profile.profileEmployeeId = - // item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"]; - // profiles.push(profile); - // }), - // ); - // await this.profileFamilyFatherRepository.save(profiles); - // return new HttpSuccess(getProFile); - // } + 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_); + } - // @Post("uploadSQLFamilly-Mother") - // @UseInterceptors(FileInterceptor("file")) - // async UploadFileSQLFamillyMother( - // @UploadedFile() file: Express.Multer.File, - // @Request() request: { user: Record }, - // ) { - // const workbook = xlsx.read(file.buffer, { type: "buffer" }); - // const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet - // const sheet = workbook.Sheets[sheetName]; - // const getProFile = xlsx.utils.sheet_to_json(sheet); - // let profiles: any = []; - // await Promise.all( - // getProFile.map(async (item: any) => { - // // Create a new Profile entity and assign fields from the parsed data + /** + * @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 = []; - // const profile = new ProfileFamilyMother(); - // // Check if the profile already exists - // const existingProfile = await this.profileFamilyMotherRepository.findOne({ - // where: { id: item["id"] }, - // }); - // // If profile exists, skip saving - // if (existingProfile) { - // return; - // } - // // Assign fields from the item to the profile entit + 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_); + } - // profile.id = item["id"] == "NULL" ? null : item["id"]; - // // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]); - // profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"]; - // // profile.lastUpdatedAt = - // // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]); - // profile.lastUpdateUserId = - // item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"]; - // profile.createdFullName = - // item["createdFullName"] == "NULL" ? null : item["createdFullName"]; - // profile.lastUpdateFullName = - // item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"]; - // profile.motherPrefix = item["motherPrefix"] == "NULL" ? null : item["motherPrefix"]; - // profile.motherFirstName = - // item["motherFirstName"] == "NULL" ? null : item["motherFirstName"]; - // profile.motherLastName = item["motherLastName"] == "NULL" ? null : item["motherLastName"]; - // profile.motherCareer = item["motherCareer"] == "NULL" ? null : item["motherCareer"]; - // profile.motherCitizenId = - // item["motherCitizenId"] == "NULL" ? null : item["motherCitizenId"]; - // profile.motherLive = item["motherLive"] == "NULL" ? null : item["motherLive"]; - // profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"]; - // profile.profileEmployeeId = - // item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"]; - // profiles.push(profile); - // }), - // ); - // await this.profileFamilyMotherRepository.save(profiles); - // return new HttpSuccess(getProFile); - // } + /** + * @summary ที่อยู่ตามทะเบียนบ้าน-ปัจจุบัน ข้าราชการ + */ + @Post("uploadProfileAddress-Officer") + @UseInterceptors(FileInterceptor("file")) + async UploadFileSQLAddress( + @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 getAddress = xlsx.utils.sheet_to_json(sheet); + let null_: any = null; + await Promise.all( + getAddress.map(async (item: any) => { + let provinceRegis_: any = null; + let districtRegis_: any = null; + let subDistrictRegis_: any = null; + let provinceCurr_: any = null; + let districtCurr_: any = null; + let subDistrictCurr_: any = null; + const existingProfile = await this.profileRepo.findOne({ + where: { citizenId: item.ID }, + }); + if (!existingProfile) { + return; + } else { + //registration address + if (item["PROVINCE_CODE"]) { + provinceRegis_ = await this.provincsRepo.findOne({ + where: { PROVINCE_CODE: item["PROVINCE_CODE"] }, + }); + if (provinceRegis_) { + let provinceId = await this.provinceIdRepo.findOne({ + where: { + name: provinceRegis_.PROVINCE_NAME, + }, + }); + existingProfile.registrationProvinceId = provinceId ? provinceId.id : null_; + } + } + if (item["AMPHUR_CODE"]) { + districtRegis_ = await this.amphurRepo.findOne({ + where: { + AMPHUR_CODE: item["AMPHUR_CODE"], + PROVINCE_CODE: provinceRegis_.PROVINCE_CODE, + }, + }); + if (districtRegis_) { + let districtId = await this.districtIdRepo.findOne({ + where: { + name: districtRegis_.AMPHUR_NAME, + }, + }); + existingProfile.registrationDistrictId = districtId ? districtId.id : null_; + } + } + if (item["DISTRICT_CODE"]) { + subDistrictRegis_ = await this.subDistrictRepo.findOne({ + where: { + DISTRICT_CODE: item["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, + }, + }); + existingProfile.registrationSubDistrictId = subDistrictId ? subDistrictId.id : null_; + } + } + //current address + if (item["CONTACT_PROVINCE_CODE"]) { + provinceCurr_ = await this.provincsRepo.findOne({ + where: { PROVINCE_CODE: item["CONTACT_PROVINCE_CODE"] }, + }); + if (provinceCurr_) { + let provinceId = await this.provinceIdRepo.findOne({ + where: { + name: provinceCurr_.PROVINCE_NAME, + }, + }); + existingProfile.currentProvinceId = provinceId ? provinceId.id : null_; + } + } + if (item["CONTACT_AMPHUR_CODE"]) { + districtCurr_ = await this.amphurRepo.findOne({ + where: { + AMPHUR_CODE: item["CONTACT_AMPHUR_CODE"], + PROVINCE_CODE: provinceCurr_.PROVINCE_CODE, + }, + }); + if (districtCurr_) { + let districtId = await this.districtIdRepo.findOne({ + where: { + name: districtCurr_.AMPHUR_NAME, + }, + }); + existingProfile.currentDistrictId = districtId ? districtId.id : null_; + } + } + if (item["CONTACT_DISTRICT_CODE"]) { + subDistrictCurr_ = await this.subDistrictRepo.findOne({ + where: { + DISTRICT_CODE: item["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, + }, + }); + existingProfile.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_; + } + } + existingProfile.registrationAddress = item.H_NUMBER; + existingProfile.registrationZipCode = item.ZIPCODE; + existingProfile.currentAddress = item.CONTACT_H_NUMBER; + existingProfile.currentZipCode = item.CONTACT_ZIPCODE; + existingProfile.createdUserId = request.user.sub; + existingProfile.createdFullName = request.user.name; + existingProfile.lastUpdateUserId = request.user.sub; + existingProfile.lastUpdateFullName = request.user.name; + await this.profileRepo.save(existingProfile); + } + }), + ); + return new HttpSuccess(); + } - // @Post("uploadSQLFamilly-Couple") - // @UseInterceptors(FileInterceptor("file")) - // async UploadFileSQLFamillyCouple( - // @UploadedFile() file: Express.Multer.File, - // // @Request() request: { user: Record }, - // ) { - // const workbook = xlsx.read(file.buffer, { type: "buffer" }); - // const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet - // const sheet = workbook.Sheets[sheetName]; - // const getProFile = xlsx.utils.sheet_to_json(sheet); - // let profiles: any = []; - // await Promise.all( - // getProFile.map(async (item: any) => { - // // Create a new Profile entity and assign fields from the parsed data - - // const profile = new ProfileFamilyCouple(); - // // Check if the profile already exists - // const existingProfile = await this.profileFamilyCoupleRepository.findOne({ - // where: { id: item["id"] }, - // }); - // // If profile exists, skip saving - // if (existingProfile) { - // return; - // } - // // Assign fields from the item to the profile entit - - // profile.id = item["id"] == "NULL" ? null : item["id"]; - // // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]); - // profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"]; - // // profile.lastUpdatedAt = - // // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]); - // profile.lastUpdateUserId = - // item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"]; - // profile.createdFullName = - // item["createdFullName"] == "NULL" ? null : item["createdFullName"]; - // profile.lastUpdateFullName = - // item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"]; - // profile.couple = item["couple"] == "NULL" ? null : item["couple"]; - // profile.couplePrefix = item["couplePrefix"] == "NULL" ? null : item["couplePrefix"]; - // profile.coupleFirstName = - // item["coupleFirstName"] == "NULL" ? null : item["coupleFirstName"]; - // profile.coupleLastName = item["coupleLastName"] == "NULL" ? null : item["coupleLastName"]; - // profile.coupleLastNameOld = - // item["coupleLastNameOld"] == "NULL" ? null : item["coupleLastNameOld"]; - // profile.coupleCareer = item["coupleCareer"] == "NULL" ? null : item["coupleCareer"]; - // profile.coupleCitizenId = - // item["coupleCitizenId"] == "NULL" ? null : item["coupleCitizenId"]; - // profile.coupleLive = item["coupleLive"] == "NULL" ? null : item["coupleLive"]; - // profile.relationship = item["relationship"] == "NULL" ? null : item["relationship"]; - // profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"]; - // profile.profileEmployeeId = - // item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"]; - // profiles.push(profile); - // }), - // ); - // await this.profileFamilyCoupleRepository.save(profiles); - // return new HttpSuccess(getProFile); - // } + /** + * @summary ที่อยู่ตามทะเบียนบ้าน-ปัจจุบัน ลูกจ้างประจำ + */ + @Post("uploadProfileAddress-Employee") + @UseInterceptors(FileInterceptor("file")) + async UploadFileSQLAddressEmp( + @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 getAddress = xlsx.utils.sheet_to_json(sheet); + let null_: any = null; + await Promise.all( + getAddress.map(async (item: any) => { + let provinceRegis_: any = null; + let districtRegis_: any = null; + let subDistrictRegis_: any = null; + let provinceCurr_: any = null; + let districtCurr_: any = null; + let subDistrictCurr_: any = null; + const existingProfileEmp = await this.profileEmpRepo.findOne({ + where: { citizenId: item.ID }, + }); + if (!existingProfileEmp) { + return; + } else { + //registration address + if (item["PROVINCE_CODE"]) { + provinceRegis_ = await this.provincsRepo.findOne({ + where: { PROVINCE_CODE: item["PROVINCE_CODE"] }, + }); + if (provinceRegis_) { + let provinceId = await this.provinceIdRepo.findOne({ + where: { + name: provinceRegis_.PROVINCE_NAME, + }, + }); + existingProfileEmp.registrationProvinceId = provinceId ? provinceId.id : null_; + } + } + if (item["AMPHUR_CODE"]) { + districtRegis_ = await this.amphurRepo.findOne({ + where: { + AMPHUR_CODE: item["AMPHUR_CODE"], + PROVINCE_CODE: provinceRegis_.PROVINCE_CODE, + }, + }); + if (districtRegis_) { + let districtId = await this.districtIdRepo.findOne({ + where: { + name: districtRegis_.AMPHUR_NAME, + }, + }); + existingProfileEmp.registrationDistrictId = districtId ? districtId.id : null_; + } + } + if (item["DISTRICT_CODE"]) { + subDistrictRegis_ = await this.subDistrictRepo.findOne({ + where: { + DISTRICT_CODE: item["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, + }, + }); + existingProfileEmp.registrationSubDistrictId = subDistrictId + ? subDistrictId.id + : null_; + } + } + //current address + if (item["CONTACT_PROVINCE_CODE"]) { + provinceCurr_ = await this.provincsRepo.findOne({ + where: { PROVINCE_CODE: item["CONTACT_PROVINCE_CODE"] }, + }); + if (provinceCurr_) { + let provinceId = await this.provinceIdRepo.findOne({ + where: { + name: provinceCurr_.PROVINCE_NAME, + }, + }); + existingProfileEmp.currentProvinceId = provinceId ? provinceId.id : null_; + } + } + if (item["CONTACT_AMPHUR_CODE"]) { + districtCurr_ = await this.amphurRepo.findOne({ + where: { + AMPHUR_CODE: item["CONTACT_AMPHUR_CODE"], + PROVINCE_CODE: provinceCurr_.PROVINCE_CODE, + }, + }); + if (districtCurr_) { + let districtId = await this.districtIdRepo.findOne({ + where: { + name: districtCurr_.AMPHUR_NAME, + }, + }); + existingProfileEmp.currentDistrictId = districtId ? districtId.id : null_; + } + } + if (item["CONTACT_DISTRICT_CODE"]) { + subDistrictCurr_ = await this.subDistrictRepo.findOne({ + where: { + DISTRICT_CODE: item["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, + }, + }); + existingProfileEmp.currentSubDistrictId = subDistrictId ? subDistrictId.id : null_; + } + } + existingProfileEmp.registrationAddress = item.H_NUMBER; + existingProfileEmp.registrationZipCode = item.ZIPCODE; + existingProfileEmp.currentAddress = item.CONTACT_H_NUMBER; + existingProfileEmp.currentZipCode = item.CONTACT_ZIPCODE; + existingProfileEmp.createdUserId = request.user.sub; + existingProfileEmp.createdFullName = request.user.name; + existingProfileEmp.lastUpdateUserId = request.user.sub; + existingProfileEmp.lastUpdateFullName = request.user.name; + await this.profileEmpRepo.save(existingProfileEmp); + } + }), + ); + return new HttpSuccess(); + } } diff --git a/src/entities/AmphurImport.ts b/src/entities/AmphurImport.ts new file mode 100644 index 00000000..62e1ec2d --- /dev/null +++ b/src/entities/AmphurImport.ts @@ -0,0 +1,26 @@ +import { Entity, Column} from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("amphurImport") +export class AmphurImport extends EntityBase{ + @Column({ + nullable: true, + length: 255, + default: null, + }) + PROVINCE_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + AMPHUR_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + AMPHUR_NAME: string; +} diff --git a/src/entities/EducationMis.ts b/src/entities/EducationMis.ts new file mode 100644 index 00000000..6c2864f2 --- /dev/null +++ b/src/entities/EducationMis.ts @@ -0,0 +1,26 @@ +import { Entity, Column} from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("educationMis") +export class EducationMis extends EntityBase{ + @Column({ + nullable: true, + length: 255, + default: null, + }) + EDUCATION_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + EDUCATION_NAME: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + EDUCATION_ABB_NAME: string; +} diff --git a/src/entities/ProvinceImport.ts b/src/entities/ProvinceImport.ts new file mode 100644 index 00000000..978ebf9b --- /dev/null +++ b/src/entities/ProvinceImport.ts @@ -0,0 +1,19 @@ +import { Entity, Column} from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("provinceImport") +export class ProvinceImport extends EntityBase{ + @Column({ + nullable: true, + length: 255, + default: null, + }) + PROVINCE_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + PROVINCE_NAME: string; +} diff --git a/src/entities/SubDistrictImport.ts b/src/entities/SubDistrictImport.ts new file mode 100644 index 00000000..cdf8660d --- /dev/null +++ b/src/entities/SubDistrictImport.ts @@ -0,0 +1,33 @@ +import { Entity, Column} from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("subDistrictImport") +export class SubDistrictImport extends EntityBase{ + @Column({ + nullable: true, + length: 255, + default: null, + }) + PROVINCE_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + AMPHUR_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + DISTRICT_CODE: string; + + @Column({ + nullable: true, + length: 255, + default: null, + }) + DISTRICT_NAME: string; +} diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index e3469e11..938a3f3d 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -1,37 +1,41 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; -import moment from 'moment'; +import moment from "moment"; class Extension { public static ConvertToDateTime(value: any) { - - if (value != "" && value != null ) { - if(isNaN(value)){ - let date = moment(value, 'DD/MM/YYYY').toDate(); + if (value != "" && value != null) { + if (isNaN(value)) { + let date = moment(value, "DD/MM/YYYY").toDate(); let year = date.getFullYear(); if (year < 1800) { - return null; + return null; } else if (year > 2500) { - date.setFullYear(year - 543); + date.setFullYear(year - 543); } return date; + } else { + if (value.toString().length == 4) { + if (value < 1800) { + return null; + } else if (value > 2500) { + return new Date(value - 543, 0, 1); + } + } } - // else { - // return moment.unix(value).format('DD/MM/YYYY'); - // } } return null; } public static CheckRelationship(value: any) { - if(value!= "" && value != null) { - switch(value) { + if (value != "" && value != null) { + switch (value) { case 1: - return "โสด"; + return "โสด"; case 2: - return "สมรส"; + return "สมรส"; case 3: - return "หย่า"; + return "หย่า"; case 4: return "หม้าย"; default: diff --git a/src/migration/1721902034164-add_table_EducationMis.ts b/src/migration/1721902034164-add_table_EducationMis.ts new file mode 100644 index 00000000..114a3c77 --- /dev/null +++ b/src/migration/1721902034164-add_table_EducationMis.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableEducationMis1721902034164 implements MigrationInterface { + name = 'AddTableEducationMis1721902034164' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`educationMis\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`EDUCATION_CODE\` varchar(255) NULL, \`EDUCATION_NAME\` varchar(255) NULL, \`EDUCATION_ABB_NAME\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE \`educationMis\``); + } + +} diff --git a/src/migration/1721964204244-Add_table_AddressImport.ts b/src/migration/1721964204244-Add_table_AddressImport.ts new file mode 100644 index 00000000..a833893d --- /dev/null +++ b/src/migration/1721964204244-Add_table_AddressImport.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableAddressImport1721964204244 implements MigrationInterface { + name = 'AddTableAddressImport1721964204244' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`provinceImport\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`PROVINCE_CODE\` varchar(255) NULL, \`PROVINCE_NAME\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`CREATE TABLE \`subDistrictImport\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`PROVINCE_CODE\` varchar(255) NULL, \`AMPHUR_CODE\` varchar(255) NULL, \`DISTRICT_CODE\` varchar(255) NULL, \`DISTRICT_NAME\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`CREATE TABLE \`amphurImport\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`PROVINCE_CODE\` varchar(255) NULL, \`AMPHUR_CODE\` varchar(255) NULL, \`AMPHUR_NAME\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE \`amphurImport\``); + await queryRunner.query(`DROP TABLE \`subDistrictImport\``); + await queryRunner.query(`DROP TABLE \`provinceImport\``); + } + +}