This commit is contained in:
kittapath 2025-02-14 09:58:51 +07:00
parent 408a3a7644
commit b94d4e1b93
3 changed files with 324 additions and 29 deletions

View file

@ -45,6 +45,8 @@ import { OrgChild4 } from "../entities/OrgChild4";
import { IMPORT_ORG } from "../entities/IMPORT_ORG";
import { OrgRevision } from "../entities/OrgRevision";
import { OFFICER } from "../entities/OFFICER";
import { Position } from "../entities/Position";
import { PosMaster } from "../entities/PosMaster";
@Route("api/v1/org/upload")
@Tags("UPLOAD")
@ -85,23 +87,143 @@ export class ImportDataController extends Controller {
private orgChild4Repo = AppDataSource.getRepository(OrgChild4);
private IMPORT_ORGRepo = AppDataSource.getRepository(IMPORT_ORG);
private OFFICERRepo = AppDataSource.getRepository(OFFICER);
private positionRepo = AppDataSource.getRepository(Position);
private posMasterRepo = AppDataSource.getRepository(PosMaster);
/**
* @summary
*/
@Post("uploadProfile-Officer")
async UploadFileSqlOfficer(@Request() request: { user: Record<string, any> }) {
const OFFICER = await this.OFFICERRepo.find();
// let users = [];
let rowCount = 0;
// const filePath = path.resolve(__dirname, "OFFICER.csv"); // Corrected file path
// const readStream = fs.createReadStream(filePath).pipe(csvParser());
// let profiles: any = [];
let null_: any = null;
let profile: any;
// await Promise.all(
// OFFICER.map(async (item) => {
for await (const item of OFFICER) {
let existingProfile = await this.profileRepo.findOne({
// readStream.map(async (item: any) => {
rowCount++;
let type_: any = null;
let level_: any = null;
profile = null;
profile = new Profile();
// if (item["FLAG_RETIRE_STATUSxxxx != "" && item["FLAG_RETIRE_STATUSxxxx != null) {
// continue;
// }
// if (item["FLAG_PERSON_TYPExxxx != "1") {
// continue;
// }
// if (new Date(item.RET_YEAR).getFullYear() >= 2567) {
// return;
// }
const existingProfile = await this.profileRepo.findOne({
where: { citizenId: item.ID },
});
if (existingProfile == null) {
} else {
existingProfile.position = item["WORK_LINE_NAME"] == "" ? null_ : item["WORK_LINE_NAME"];
await this.profileRepo.save(existingProfile);
if (existingProfile) {
profile.id = existingProfile.id;
// continue;
}
if (item.MP_CATEGORY) {
type_ = await this.posTypeRepo.findOne({
where: { posTypeName: item.MP_CATEGORY },
});
}
if (item.MP_LEVEL) {
if (type_ == null) {
level_ = await this.posLevelRepo.findOne({
where: {
posLevelName: item.MP_LEVEL,
},
});
} else {
level_ = await this.posLevelRepo.findOne({
where: {
posLevelName: item.MP_LEVEL,
posTypeId: type_.id,
},
});
}
}
let dateRetire = new Date(item.BORN);
profile.citizenId = item.ID == "" ? "" : item.ID;
profile.rank =
item.RANK_NAME == "" ||
item.RANK_NAME == "นาย" ||
item.RANK_NAME == "นาง" ||
item.RANK_NAME == "นางสาว"
? null
: item.RANK_NAME;
profile.prefix = item.RANK_NAME == "" ? null : item.RANK_NAME;
profile.prefixMain =
item.RANK_NAME == "" ||
(item.RANK_NAME != "นาย" && item.RANK_NAME != "นาง" && item.RANK_NAME != "นางสาว")
? null
: item.RANK_NAME;
profile.firstName = item.FNAME == "" ? null : item.FNAME;
profile.lastName = item.LNAME == "" ? null : item.LNAME;
profile.gender = item.SEX == "1" ? "ชาย" : item.SEX == "2" ? "หญิง" : null_;
profile.birthDate = item.BORN == "" ? null_ : new Date(item.BORN);
profile.dateAppoint = item.BEGIN_ENTRY_DATE == "" ? null_ : new Date(item.BEGIN_ENTRY_DATE);
profile.dateStart = item.BEGIN_ENTRY_DATE == "" ? null_ : new Date(item.BEGIN_ENTRY_DATE);
profile.dateRetire = dateRetire == null ? null_ : calculateRetireDate(dateRetire);
profile.dateRetireLaw = dateRetire == null ? null_ : calculateRetireLaw(dateRetire);
profile.position = item.WORK_LINE_NAME == "" ? null : item.WORK_LINE_NAME;
profile.posTypeId =
type_ != null && type_.posTypeName == item.MP_CATEGORY && type_ ? type_.id : null;
profile.posLevelId =
level_ != null && level_.posLevelName == item.MP_LEVEL && level_ ? level_.id : null;
// profile.relationship =
// item.สถานภาพ == "" ? "" : Extension.CheckRelationship(item.สถานภาพ);
// profile.position =
// item.WORK_LINE_NAME == "" ? "" : Extension.CheckRelationship(item.WORK_LINE_NAME);
profile.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"];
// const level = await this.posLevelRepo.findOne({
// where: {
// posLevelName: item.MP_LEVEL,
// posType: {
// posTypeName: item.MP_Type,
// },
// },
// });
// profile.posLevelId = level?.id ?? null_;
// const type = await this.posTypeRepo.findOne({
// where: { posTypeName: item.MP_Type },
// });
// profile.posTypeId = type?.id ?? null_;
profile.amount = item.SALARY == "" ? 0 : Number(Extension.CheckRelationship(item.SALARY));
// profile.isLeave =
// item.FLAG_RETIRE_STATUSxxxx == "" || item.FLAG_RETIRE_STATUSxxxx == null ? false : true;
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
profile.createdAt = new Date();
profile.lastUpdatedAt = new Date();
// profiles.push(profile);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
// if (profiles.length === BATCH_SIZE) {
await this.profileRepo.save(profile);
// console.log(profile);
// profiles = await [];
// if (global.gc) {
// global.gc();
// }
// }
}
// }),
// );
// )
// );
// console.log(rowCount);
// await this.profileRepo.save(profiles);
return new HttpSuccess();
}
@ -116,19 +238,21 @@ export class ImportDataController extends Controller {
const readStream = fs.createReadStream(filePath).pipe(csvParser());
let profiles: any = [];
let null_: any = null;
let profileEmp: any;
for await (const item of readStream) {
rowCount++;
const profileEmp = new ProfileEmployee();
profileEmp = null;
profileEmp = new ProfileEmployee();
// if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) {
// continue;
// }
if (item["FLAG_PERSON_TYPE"] != "6") {
continue;
}
if (new Date(item["RET_YEAR"]).getFullYear() >= 2567) {
continue;
}
// if (new Date(item["RET_YEAR"]).getFullYear() >= 2567) {
// continue;
// }
const existingProfile = await this.profileEmpRepo.findOne({
where: { citizenId: item["ID"] },
});
@ -168,6 +292,8 @@ export class ImportDataController extends Controller {
profileEmp.salaryLevel = item["SALARY_LEVEL_CODE"] == "" ? null : item["SALARY_LEVEL_CODE"];
profileEmp.relationship =
item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]);
profileEmp.amount =
item["SALARY"] == "" ? 0 : Number(Extension.CheckRelationship(item.SALARY));
profileEmp.createdUserId = request.user.sub;
profileEmp.createdFullName = request.user.name;
profileEmp.lastUpdateUserId = request.user.sub;
@ -1669,4 +1795,131 @@ export class ImportDataController extends Controller {
);
return new HttpSuccess();
}
/**
* @summary
*/
@Post("mapposition-Officer")
async MapPositionOfficer(@Request() request: { user: Record<string, any> }) {
const [officer, total] = await AppDataSource.getRepository(OFFICER)
.createQueryBuilder("OFFICER")
// .skip(0)
// .take(20)
.getManyAndCount();
let rowCount = 0;
let null_: any = null;
let type_: any = null;
let level_: any = null;
const orgRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (orgRevision == null) return new HttpSuccess();
for await (const item of officer) {
rowCount++;
const existingProfile = await this.profileRepo.findOne({
where: { citizenId: item.ID },
});
if (existingProfile == null) {
continue;
}
const orgRoot = await this.orgRootRepo.findOne({
where: { orgRootName: item.DEPARTMENT_NAME, orgRevisionId: orgRevision.id },
});
if (orgRoot == null) {
continue;
}
const orgChild1 = await this.orgChild1Repo.findOne({
where: {
orgChild1Name: item.DIVISION_NAME,
orgRoot: { orgRootName: item.DEPARTMENT_NAME },
orgRevisionId: orgRevision.id,
},
});
const orgChild2 = await this.orgChild2Repo.findOne({
where: {
orgChild2Name: item.SECTION_NAME,
orgChild1: {
orgChild1Name: item.DIVISION_NAME,
orgRoot: { orgRootName: item.DEPARTMENT_NAME },
},
orgRevisionId: orgRevision.id,
},
});
const orgChild3 = await this.orgChild3Repo.findOne({
where: {
orgChild3Name: item.JOB_NAME,
orgChild2: {
orgChild2Name: item.SECTION_NAME,
orgChild1: {
orgChild1Name: item.DIVISION_NAME,
orgRoot: { orgRootName: item.DEPARTMENT_NAME },
},
},
orgRevisionId: orgRevision.id,
},
});
let posMaster = new PosMaster();
posMaster.orgRootId = orgRoot?.id ?? null_;
posMaster.orgChild1Id = orgChild1?.id ?? null_;
posMaster.orgChild2Id = orgChild2?.id ?? null_;
posMaster.orgChild3Id = orgChild3?.id ?? null_;
posMaster.statusReport = "PENDING";
posMaster.isCondition = false;
posMaster.isStaff = false;
posMaster.isDirector = false;
posMaster.isSit = false;
posMaster.current_holderId = existingProfile.id;
posMaster.posMasterNo = item.SALARY == "" ? null_ : Number(item.POS_NUM_CODE);
posMaster.orgRevisionId = orgRevision.id;
posMaster.posMasterCreatedAt = new Date();
posMaster.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepo.save(posMaster);
posMaster.ancestorDNA = posMaster.id;
await this.posMasterRepo.save(posMaster);
let position = new Position();
if (item.MP_CATEGORY) {
type_ = await this.posTypeRepo.findOne({
where: { posTypeName: item.MP_CATEGORY },
});
}
if (item.MP_LEVEL) {
if (type_ == null) {
level_ = await this.posLevelRepo.findOne({
where: {
posLevelName: item.MP_LEVEL,
},
});
} else {
level_ = await this.posLevelRepo.findOne({
where: {
posLevelName: item.MP_LEVEL,
posTypeId: type_.id,
},
});
}
}
position.posMasterId = posMaster.id;
position.isSpecial = false;
position.positionIsSelected = true;
position.createdUserId = request.user.sub;
position.createdFullName = request.user.name;
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdatedAt = new Date();
position.positionName = item.WORK_LINE_NAME == "" ? null_ : item.WORK_LINE_NAME;
position.posTypeId =
type_ != null && type_.posTypeName == item.MP_CATEGORY && type_ ? type_.id : null;
position.posLevelId =
level_ != null && level_.posLevelName == item.MP_LEVEL && level_ ? level_.id : null;
await this.positionRepo.save(position);
}
return new HttpSuccess();
}
}

View file

@ -1213,31 +1213,31 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgRoot.ancestorDNA ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild3.ancestorDNA ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild2.ancestorDNA ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild3.ancestorDNA ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild4.ancestorDNA ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
@ -1510,31 +1510,31 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgRoot.ancestorDNA ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild1.ancestorDNA ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild2.ancestorDNA ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild3.ancestorDNA ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild4.ancestorDNA ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
@ -2276,7 +2276,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgRoot.ancestorDNA ?? null,
child1:
profile?.current_holders?.find(
(x) =>
@ -2294,7 +2294,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild1.ancestorDNA ?? null,
child2:
profile?.current_holders?.find(
(x) =>
@ -2312,7 +2312,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild2.ancestorDNA ?? null,
child3:
profile?.current_holders?.find(
(x) =>
@ -2330,7 +2330,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild3.ancestorDNA ?? null,
child4:
profile?.current_holders?.find(
(x) =>
@ -2348,7 +2348,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild4.ancestorDNA ?? null,
posNo: shortName ?? "",
};
});
@ -2643,7 +2643,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgRoot.ancestorDNA ?? null,
child1:
profile?.current_holders?.find(
(x) =>
@ -2661,7 +2661,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild1.ancestorDNA ?? null,
child2:
profile?.current_holders?.find(
(x) =>
@ -2679,7 +2679,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild2.ancestorDNA ?? null,
child3:
profile?.current_holders?.find(
(x) =>
@ -2697,7 +2697,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild3.ancestorDNA ?? null,
child4:
profile?.current_holders?.find(
(x) =>
@ -2715,7 +2715,7 @@ export class OrganizationDotnetController extends Controller {
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.ancestorDNA ?? null,
)?.orgChild4.ancestorDNA ?? null,
posNo: shortName ?? "",
};
});

View file

@ -88,4 +88,46 @@ export class OFFICER {
default: null,
})
SALARY: string;
@Column({
nullable: true,
type: "text",
default: null,
})
DEPARTMENT_NAME: string;
@Column({
nullable: true,
type: "text",
default: null,
})
DIVISION_NAME: string;
@Column({
nullable: true,
type: "text",
default: null,
})
SECTION_NAME: string;
@Column({
nullable: true,
type: "text",
default: null,
})
JOB_NAME: string;
@Column({
nullable: true,
type: "text",
default: null,
})
POS_NUM_CODE: string;
@Column({
nullable: true,
type: "text",
default: null,
})
POS_NUM_NAME: string;
}