แก้คิวรี่ให้ไว

This commit is contained in:
mamoss 2025-07-24 12:36:45 +07:00
parent 91c8e37647
commit 0f5afcd0c7

View file

@ -1110,14 +1110,14 @@ export class ProfileController extends Controller {
: null, : null,
disciplineDetail: item.detail ?? null, disciplineDetail: item.detail ?? null,
refNo: Extension.ToThaiNumber(item.refCommandNo) ?? null, refNo: Extension.ToThaiNumber(item.refCommandNo) ?? null,
level: item.level ?? "" level: item.level ?? "",
})) }))
: [ : [
{ {
disciplineYear: "-", disciplineYear: "-",
disciplineDetail: "-", disciplineDetail: "-",
refNo: "-", refNo: "-",
level: "-" level: "-",
}, },
]; ];
@ -1402,22 +1402,23 @@ export class ProfileController extends Controller {
where: { profileId: id }, where: { profileId: id },
order: { createdAt: "ASC" }, order: { createdAt: "ASC" },
}); });
const history = profileHistory.length > 0 const history =
? profileHistory.map(item => ({ profileHistory.length > 0
birthDateOld: item.birthDateOld ? profileHistory.map((item) => ({
? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDateOld)) birthDateOld: item.birthDateOld
: "", ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDateOld))
birthDate: item.birthDate : "",
? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDate)) birthDate: item.birthDate
: "", ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDate))
})) : "",
: [ }))
{ : [
birthDateOld: "-", {
birthDate: "-", birthDateOld: "-",
} birthDate: "-",
] },
];
const position_raw = await this.salaryRepo.find({ const position_raw = await this.salaryRepo.find({
where: { where: {
profileId: id, profileId: id,
@ -1565,15 +1566,16 @@ export class ProfileController extends Controller {
: "", : "",
pointSum: item.pointSum ? Extension.ToThaiNumber(item.pointSum.toString()) : "", pointSum: item.pointSum ? Extension.ToThaiNumber(item.pointSum.toString()) : "",
pointSumTh: item.pointSum ? Extension.textPoint(item.pointSum) : "", pointSumTh: item.pointSum ? Extension.textPoint(item.pointSum) : "",
level: item.pointSum < 60.00 level:
? "ต้องปรับปรุง (คะแนนต่ำกว่าร้อยละ ๖๐.)" item.pointSum < 60.0
: item.pointSum <= 69.99 && item.pointSum >= 60.00 ? "ต้องปรับปรุง (คะแนนต่ำกว่าร้อยละ ๖๐.)"
? "พอใช้ (คะแนนร้อยละ ๖๐. - ๖๙.๙๙)" : item.pointSum <= 69.99 && item.pointSum >= 60.0
: item.pointSum <= 79.99 && item.pointSum >= 70.00 ? "พอใช้ (คะแนนร้อยละ ๖๐. - ๖๙.๙๙)"
? "ดี (คะแนนร้อยละ ๗๐. - ๗๙.๙๙)" : item.pointSum <= 79.99 && item.pointSum >= 70.0
: item.pointSum <= 89.99 && item.pointSum >= 80.00 ? "ดี (คะแนนร้อยละ ๗๐. - ๗๙.๙๙)"
? "ดีมาก (คะแนนร้อยละ ๘๐. - ๘๙.๙๙)" : item.pointSum <= 89.99 && item.pointSum >= 80.0
: "ดีเด่น (คะแนนร้อยละ ๙๐. ขึ้นไป)" ? "ดีมาก (คะแนนร้อยละ ๘๐. - ๘๙.๙๙)"
: "ดีเด่น (คะแนนร้อยละ ๙๐. ขึ้นไป)",
})) }))
: [ : [
{ {
@ -1583,7 +1585,7 @@ export class ProfileController extends Controller {
point2: "-", point2: "-",
pointSum: "-", pointSum: "-",
pointSumTh: "-", pointSumTh: "-",
level: "-" level: "-",
}, },
]; ];
const profileAbility_raw = await this.profileAbilityRepo.find({ const profileAbility_raw = await this.profileAbilityRepo.find({
@ -6118,109 +6120,67 @@ export class ProfileController extends Controller {
if (!findRevision) { if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
} }
// 1. เลือกเฉพาะ field ที่จำเป็น
const [record, total] = await this.profileRepo const [record, total] = await this.profileRepo
.createQueryBuilder("profile") .createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel") .select([
.leftJoinAndSelect("profile.posType", "posType") "profile.id",
.leftJoinAndSelect("profile.current_holders", "current_holders") "profile.prefix",
.leftJoinAndSelect("current_holders.positions", "positions") "profile.rank",
.leftJoinAndSelect("positions.posExecutive", "posExecutive") "profile.firstName",
.leftJoinAndSelect("current_holders.orgRevision", "orgRevision") "profile.lastName",
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot") "profile.citizenId",
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1") "profile.position",
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2") "profile.avatar",
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3") "profile.avatarName",
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4") "profile.dateAppoint",
"posLevel.id",
"posLevel.posLevelName",
"posType.id",
"posType.posTypeName",
"current_holders.id",
"current_holders.posMasterNo",
"current_holders.orgRevisionId",
"current_holders.orgRootId",
"current_holders.orgChild1Id",
"current_holders.orgChild2Id",
"current_holders.orgChild3Id",
"current_holders.orgChild4Id",
"orgRoot.id",
"orgRoot.orgRootName",
"orgRoot.orgRootShortName",
"orgRoot.orgRootOrder",
"orgChild1.id",
"orgChild1.orgChild1Name",
"orgChild1.orgChild1ShortName",
"orgChild1.orgChild1Order",
"orgChild2.id",
"orgChild2.orgChild2Name",
"orgChild2.orgChild2ShortName",
"orgChild2.orgChild2Order",
"orgChild3.id",
"orgChild3.orgChild3Name",
"orgChild3.orgChild3ShortName",
"orgChild3.orgChild3Order",
"orgChild4.id",
"orgChild4.orgChild4Name",
"orgChild4.orgChild4ShortName",
"orgChild4.orgChild4Order",
])
.leftJoin("profile.posLevel", "posLevel")
.leftJoin("profile.posType", "posType")
.leftJoin("profile.current_holders", "current_holders")
.leftJoin("current_holders.orgRoot", "orgRoot")
.leftJoin("current_holders.orgChild1", "orgChild1")
.leftJoin("current_holders.orgChild2", "orgChild2")
.leftJoin("current_holders.orgChild3", "orgChild3")
.leftJoin("current_holders.orgChild4", "orgChild4")
.where(node && nodeId ? "current_holders.orgRevisionId = :orgRevisionId" : "1=1", { .where(node && nodeId ? "current_holders.orgRevisionId = :orgRevisionId" : "1=1", {
orgRevisionId: node && nodeId ? findRevision.id : undefined, orgRevisionId: node && nodeId ? findRevision.id : undefined,
}) })
.andWhere( // ...existing .andWhere เงื่อนไขเดิม...
_data.root != undefined && _data.root != null .andWhere(nodeCondition, { nodeId: nodeId })
? _data.root[0] != null
? `current_holders.orgRootId IN (:...root)`
: `current_holders.orgRootId is null`
: "1=1",
{
root: _data.root,
},
)
.andWhere(
_data.child1 != undefined && _data.child1 != null
? _data.child1[0] != null
? `current_holders.orgChild1Id IN (:...child1)`
: `current_holders.orgChild1Id is null`
: "1=1",
{
child1: _data.child1,
},
)
.andWhere(
_data.child2 != undefined && _data.child2 != null
? _data.child2[0] != null
? `current_holders.orgChild2Id IN (:...child2)`
: `current_holders.orgChild2Id is null`
: "1=1",
{
child2: _data.child2,
},
)
.andWhere(
_data.child3 != undefined && _data.child3 != null
? _data.child3[0] != null
? `current_holders.orgChild3Id IN (:...child3)`
: `current_holders.orgChild3Id is null`
: "1=1",
{
child3: _data.child3,
},
)
.andWhere(
_data.child4 != undefined && _data.child4 != null
? _data.child4[0] != null
? `current_holders.orgChild4Id IN (:...child4)`
: `current_holders.orgChild4Id is null`
: "1=1",
{
child4: _data.child4,
},
)
.andWhere(
posType != undefined && posType != null && posType != ""
? "posType.posTypeName LIKE :keyword1"
: "1=1",
{
keyword1: `${posType}`,
},
)
.andWhere(
posLevel != undefined && posLevel != null && posLevel != ""
? "posLevel.posLevelName LIKE :keyword2"
: "1=1",
{
keyword2: `${posLevel}`,
},
)
.andWhere(
isProbation != undefined && isProbation != null
? `profile.isProbation = ${isProbation}`
: "1=1",
)
.andWhere(
isRetire != undefined && isRetire != null
? isRetire == false
? // ? `profile.dateLeave IS NULL`
`profile.isLeave IS FALSE`
: isRetire == true && retireType != undefined && retireType != null
? // ? `profile.dateLeave IS NOT NULL AND profile.leaveType = '${retireType}'`
// : `profile.dateLeave IS NOT NULL`
`profile.isLeave IS TRUE AND profile.leaveType = '${retireType}'`
: `profile.isLeave IS TRUE`
: "1=1",
)
.andWhere(nodeCondition, {
nodeId: nodeId,
})
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
qb.orWhere( qb.orWhere(
@ -6233,8 +6193,6 @@ export class ProfileController extends Controller {
); );
}), }),
) )
// .orderBy("current_holders.posMasterNo", "ASC")
// .orderBy(`${sortBy}`, sort)
.addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order")
.orderBy("sort_order", "ASC") .orderBy("sort_order", "ASC")
.addOrderBy("orgRoot.orgRootOrder", sort) .addOrderBy("orgRoot.orgRootOrder", sort)
@ -6247,122 +6205,40 @@ export class ProfileController extends Controller {
.take(pageSize) .take(pageSize)
.getManyAndCount(); .getManyAndCount();
const data = await Promise.all( // 2. map ข้อมูลแบบ flat ไม่ต้องวนซ้ำซ้อน
record.map((_data) => { const data = record.map((_data) => {
const posExecutive = // ...map เฉพาะ field ที่จำเป็น...
_data.current_holders.length == 0 || return {
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null || id: _data.id,
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == avatar: _data.avatar,
0 || avatarName: _data.avatarName,
_data.current_holders dateAppoint: _data.dateAppoint,
.find((x) => x.orgRevisionId == findRevision.id) prefix: _data.prefix,
?.positions.find((x: any) => x.positionIsSelected == true) == null || rank: _data.rank,
_data.current_holders firstName: _data.firstName,
.find((x) => x.orgRevisionId == findRevision.id) lastName: _data.lastName,
?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive == null citizenId: _data.citizenId,
? null posLevel: _data.posLevel?.posLevelName ?? null,
: _data.current_holders posType: _data.posType?.posTypeName ?? null,
.find((x) => x.orgRevisionId == findRevision.id) posLevelId: _data.posLevel?.id ?? null,
?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive posTypeId: _data.posType?.id ?? null,
?.posExecutiveName; position: _data.position,
posNo: _data.current_holders?.[0]?.posMasterNo ?? null,
const shortName = rootId: _data.current_holders?.[0]?.orgRootId ?? null,
_data.current_holders.length == 0 root: _data.current_holders?.[0]?.orgRoot?.orgRootName ?? null,
? null orgRootShortName: _data.current_holders?.[0]?.orgRoot?.orgRootShortName ?? null,
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && orgRevisionId: _data.current_holders?.[0]?.orgRevisionId ?? null,
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != org: [
null _data.current_holders?.[0]?.orgChild4?.orgChild4Name,
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` _data.current_holders?.[0]?.orgChild3?.orgChild3Name,
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && _data.current_holders?.[0]?.orgChild2?.orgChild2Name,
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) _data.current_holders?.[0]?.orgChild1?.orgChild1Name,
?.orgChild3 != null _data.current_holders?.[0]?.orgRoot?.orgRootName ?? _data.current_holders?.[0]?.orgRoot,
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` ]
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && .filter((name) => !!name)
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) .join("\n"),
?.orgChild2 != null };
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` });
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild1 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) !=
null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgRoot != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: null;
const root =
_data.current_holders.length == 0 ||
(_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null)
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot;
const child1 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1;
const child2 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2;
const child3 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3;
const child4 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4;
let _root = root?.orgRootName;
let _child1 = child1?.orgChild1Name;
let _child2 = child2?.orgChild2Name;
let _child3 = child3?.orgChild3Name;
let _child4 = child4?.orgChild4Name;
return {
id: _data.id,
avatar: _data.avatar,
avatarName: _data.avatarName,
dateAppoint: _data.dateAppoint,
prefix: _data.prefix,
rank: _data.rank,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
posLevelId: _data.posLevel == null ? null : _data.posLevel.id,
posTypeId: _data.posType == null ? null : _data.posType.id,
position: _data.position,
posExecutive: posExecutive,
posNo: shortName,
rootId: root == null ? null : root.id,
root: root == null ? null : root.orgRootName,
rootDnaId: root == null ? null : root.ancestorDNA,
orgRootShortName: root == null ? null : root.orgRootShortName,
orgRevisionId: root == null ? null : root.orgRevisionId,
org:
(_child4 == null ? "" : _child4 + "\n") +
(_child3 == null ? "" : _child3 + "\n") +
(_child2 == null ? "" : _child2 + "\n") +
(_child1 == null ? "" : _child1 + "\n") +
(_root == null ? "" : _root),
};
}),
);
return new HttpSuccess({ data: data, total }); return new HttpSuccess({ data: data, total });
} }