ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ #1689

This commit is contained in:
Bright 2025-07-30 17:03:24 +07:00
parent 0f89aa4a92
commit 900fb8deb5
3 changed files with 342 additions and 528 deletions

View file

@ -4010,243 +4010,127 @@ export class ProfileEmployeeController extends Controller {
*/
@Post("search-personal")
async getProfileBySearchKeyword(
@Request() request: RequestWithUser,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Body()
body: {
fieldName: string;
keyword?: string;
system?: string;
},
) {
let findProfile: any;
let total: any;
let revision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } });
const skip = (page - 1) * pageSize;
const take = pageSize;
let queryLike = `CONCAT(
IFNULL(orgChild4.orgChild4ShortName, ''), " ",
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword OR CONCAT(
IFNULL(orgChild3.orgChild3ShortName, ''), " ",
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword OR CONCAT(
IFNULL(orgChild2.orgChild2ShortName, ''), " ",
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword OR CONCAT(
IFNULL(orgChild1.orgChild1ShortName, ''), " ",
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword OR CONCAT(
IFNULL(orgRoot.orgRootShortName, ''), " ",
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword`;
switch (body.fieldName) {
case "citizenId":
[findProfile, total] = await this.profileRepo.findAndCount({
where: {
citizenId: Like(`%${body.keyword}%`),
current_holders: {
orgRevisionId: revision?.id,
},
},
relations: [
"posType",
"posLevel",
"current_holders",
"profileSalary",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
skip,
take,
});
break;
case "fullName":
[findProfile, total] = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id })
.andWhere(
"CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword",
{ keyword: `%${body.keyword}%` },
)
.skip(skip)
.take(take)
.getManyAndCount();
break;
case "position":
[findProfile, total] = await this.profileRepo.findAndCount({
where: {
position: Like(`%${body.keyword}%`),
current_holders: {
orgRevisionId: revision?.id,
},
},
relations: [
"posType",
"posLevel",
"current_holders",
"profileSalary",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
skip,
take,
});
break;
case "posNo":
[findProfile, total] = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id })
.andWhere(
new Brackets((qb) => {
qb.orWhere(
body.keyword != undefined && body.keyword != null && body.keyword != ""
? queryLike
: "1=1",
{
keyword: `%${body.keyword}%`,
},
);
}),
)
.skip(skip)
.take(take)
.getManyAndCount();
break;
case "posType":
[findProfile, total] = await this.profileRepo.findAndCount({
where: {
posType: {
posTypeName: Like(`%${body.keyword}%`),
},
current_holders: {
orgRevisionId: revision?.id,
},
},
relations: [
"posType",
"posLevel",
"current_holders",
"profileSalary",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
skip,
take,
});
break;
// case "posLevel":
// [findProfile, total] = await this.profileRepo.findAndCount({
// where: {
// posLevel: {
// posLevelName: Like(`%${body.keyword}%`),
// },
// current_holders: {
// orgRevisionId: revision?.id,
// },
// },
// relations: [
// "posType",
// "posLevel",
// "current_holders",
// "profileSalary",
// "current_holders.orgRoot",
// "current_holders.orgChild1",
// "current_holders.orgChild2",
// "current_holders.orgChild3",
// "current_holders.orgChild4",
// ],
// skip,
// take,
// });
// break;
case "organization":
[findProfile, total] = await this.profileRepo.findAndCount({
where: {
current_holders: {
orgRevisionId: revision?.id,
orgRoot: {
orgRootName: Like(`%${body.keyword}%`),
},
},
},
relations: [
"posType",
"posLevel",
"current_holders",
"profileSalary",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
skip,
take,
});
break;
default:
[findProfile, total] = await this.profileRepo.findAndCount({
where: {
current_holders: {
orgRevisionId: revision?.id,
},
},
relations: [
"posType",
"posLevel",
"current_holders",
"profileSalary",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
skip,
take,
});
break;
}
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
// ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ
let _system:string ="SYS_REGISTRY_EMP";
if(body.system)
_system = body.system;
let _data = await new permission().PermissionOrgList(request, _system);
const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } });
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
}
let queryLike = "1=1"
switch (body.fieldName) {
case "citizenId":
queryLike = "profile.citizenId LIKE :keyword";
break;
case "fullName":
queryLike = "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword";
break;
case "position":
queryLike = "profile.position LIKE :keyword";
break;
case "posNo":
queryLike = `
CASE
WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, " ", current_holders.posMasterNo)
WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, " ", current_holders.posMasterNo)
WHEN current_holders.orgChild2Id IS NOT NULL THEN CONCAT(orgChild2.orgChild2ShortName, " ", current_holders.posMasterNo)
WHEN current_holders.orgChild1Id IS NOT NULL THEN CONCAT(orgChild1.orgChild1ShortName, " ", current_holders.posMasterNo)
ELSE CONCAT(orgRoot.orgRootShortName, " ", current_holders.posMasterNo)
END LIKE :keyword
`;
break;
case "posType":
queryLike = "posType.posTypeName LIKE :keyword";
break;
case "posLevel":
queryLike = "posLevel.posLevelName LIKE :keyword";
break;
case "organization":
queryLike = "orgRoot.orgRootName LIKE :keyword";
break;
default:
queryLike = "1=1"
break;
}
const [findProfile, total] = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.where("current_holders.orgRevision = :revisionId", { revisionId: findRevision?.id })
.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(
new Brackets((qb) => {
qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` });
}),
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const mapDataProfile = await Promise.all(
findProfile.map(async (item: ProfileEmployee) => {