edit query

This commit is contained in:
mamoss 2025-07-24 13:08:18 +07:00
parent 0f5afcd0c7
commit 665ab5de4e

View file

@ -6059,6 +6059,7 @@ export class ProfileController extends Controller {
total: 1, total: 1,
}, },
}) })
// ...existing code...
async listProfile( async listProfile(
@Request() request: RequestWithUser, @Request() request: RequestWithUser,
@Query("page") page: number = 1, @Query("page") page: number = 1,
@ -6114,16 +6115,20 @@ export class ProfileController extends Controller {
nodeCondition = "current_holders.orgChild4Id = :nodeId"; nodeCondition = "current_holders.orgChild4Id = :nodeId";
} }
nodeCondition = nodeCondition + nodeAll; nodeCondition = nodeCondition + nodeAll;
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
if (!findRevision) {
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")
.leftJoin("profile.posLevel", "posLevel")
.leftJoin("profile.posType", "posType")
.leftJoin("profile.current_holders", "current_holders")
.leftJoin("current_holders.positions", "positions")
.leftJoin("positions.posExecutive", "posExecutive")
.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")
.select([ .select([
"profile.id", "profile.id",
"profile.prefix", "profile.prefix",
@ -6139,58 +6144,89 @@ export class ProfileController extends Controller {
"posLevel.posLevelName", "posLevel.posLevelName",
"posType.id", "posType.id",
"posType.posTypeName", "posType.posTypeName",
"current_holders.id",
"current_holders.posMasterNo",
"current_holders.orgRevisionId", "current_holders.orgRevisionId",
"current_holders.orgRootId", "current_holders.posMasterNo",
"current_holders.orgChild1Id",
"current_holders.orgChild2Id",
"current_holders.orgChild3Id",
"current_holders.orgChild4Id",
"orgRoot.id", "orgRoot.id",
"orgRoot.orgRootName", "orgRoot.orgRootName",
"orgRoot.orgRootShortName", "orgRoot.orgRootShortName",
"orgRoot.orgRootOrder", "orgRoot.orgRootOrder",
"orgChild1.id",
"orgChild1.orgChild1Name", "orgChild1.orgChild1Name",
"orgChild1.orgChild1ShortName",
"orgChild1.orgChild1Order", "orgChild1.orgChild1Order",
"orgChild2.id",
"orgChild2.orgChild2Name", "orgChild2.orgChild2Name",
"orgChild2.orgChild2ShortName",
"orgChild2.orgChild2Order", "orgChild2.orgChild2Order",
"orgChild3.id",
"orgChild3.orgChild3Name", "orgChild3.orgChild3Name",
"orgChild3.orgChild3ShortName",
"orgChild3.orgChild3Order", "orgChild3.orgChild3Order",
"orgChild4.id",
"orgChild4.orgChild4Name", "orgChild4.orgChild4Name",
"orgChild4.orgChild4ShortName",
"orgChild4.orgChild4Order", "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
? (await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }))?.id
: undefined,
}) })
// ...existing .andWhere เงื่อนไขเดิม... .andWhere(
_data.root != undefined && _data.root != null
? _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 ? "posType.posTypeName LIKE :keyword1" : "1=1", { keyword1: `${posType}` })
.andWhere(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.isLeave IS FALSE`
: isRetire == true && retireType
? `profile.isLeave IS TRUE AND profile.leaveType = '${retireType}'`
: `profile.isLeave IS TRUE`
: "1=1",
)
.andWhere(nodeCondition, { nodeId: nodeId }) .andWhere(nodeCondition, { nodeId: nodeId })
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
qb.orWhere( qb.orWhere(searchKeyword ? queryLike : "1=1", { keyword: `%${searchKeyword}%` });
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
? queryLike
: "1=1",
{
keyword: `%${searchKeyword}%`,
},
);
}), }),
) )
.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")
@ -6205,9 +6241,19 @@ export class ProfileController extends Controller {
.take(pageSize) .take(pageSize)
.getManyAndCount(); .getManyAndCount();
// 2. map ข้อมูลแบบ flat ไม่ต้องวนซ้ำซ้อ // map ข้อมูลแบบเร็วขึ้
const data = record.map((_data) => { const data = record.map((_data) => {
// ...map เฉพาะ field ที่จำเป็น... const holder = _data.current_holders?.[0];
const org = [
holder?.orgChild4?.orgChild4Name,
holder?.orgChild3?.orgChild3Name,
holder?.orgChild2?.orgChild2Name,
holder?.orgChild1?.orgChild1Name,
holder?.orgRoot?.orgRootName,
]
.filter(Boolean)
.join("\n");
return { return {
id: _data.id, id: _data.id,
avatar: _data.avatar, avatar: _data.avatar,
@ -6223,24 +6269,16 @@ export class ProfileController extends Controller {
posLevelId: _data.posLevel?.id ?? null, posLevelId: _data.posLevel?.id ?? null,
posTypeId: _data.posType?.id ?? null, posTypeId: _data.posType?.id ?? null,
position: _data.position, position: _data.position,
posNo: _data.current_holders?.[0]?.posMasterNo ?? null, posNo: holder?.posMasterNo ?? null,
rootId: _data.current_holders?.[0]?.orgRootId ?? null, rootId: holder?.orgRoot?.id ?? null,
root: _data.current_holders?.[0]?.orgRoot?.orgRootName ?? null, root: holder?.orgRoot?.orgRootName ?? null,
orgRootShortName: _data.current_holders?.[0]?.orgRoot?.orgRootShortName ?? null, orgRootShortName: holder?.orgRoot?.orgRootShortName ?? null,
orgRevisionId: _data.current_holders?.[0]?.orgRevisionId ?? null, orgRevisionId: holder?.orgRoot?.orgRevisionId ?? null,
org: [ org,
_data.current_holders?.[0]?.orgChild4?.orgChild4Name,
_data.current_holders?.[0]?.orgChild3?.orgChild3Name,
_data.current_holders?.[0]?.orgChild2?.orgChild2Name,
_data.current_holders?.[0]?.orgChild1?.orgChild1Name,
_data.current_holders?.[0]?.orgRoot?.orgRootName ?? _data.current_holders?.[0]?.orgRoot,
]
.filter((name) => !!name)
.join("\n"),
}; };
}); });
return new HttpSuccess({ data: data, total }); return new HttpSuccess({ data, total });
} }
/** /**