diff --git a/src/controllers/PersonalController.ts b/src/controllers/PersonalController.ts index 3b34557..cd9c5e6 100644 --- a/src/controllers/PersonalController.ts +++ b/src/controllers/PersonalController.ts @@ -20,6 +20,7 @@ import { setLogDataDiff } from "../interfaces/utils"; import { Personal, PostPersonal } from "../entities/Personal"; import permission from "../interfaces/permission"; import { Assign } from "../entities/Assign"; +import { Brackets } from "typeorm"; @Route("api/v1/probation/personal") @Tags("Personal") @@ -101,6 +102,7 @@ export class PersonalController extends Controller { @Get("list") async ListPersonal( @Query() status: string = "", + @Query() keyword: string = "", @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Request() request: RequestWithUser, @@ -133,12 +135,39 @@ export class PersonalController extends Controller { conditions.child4 = _data.child4; } - const [lists, total] = await this.personalRepository.findAndCount({ - order: { createdAt: "DESC" }, - where: conditions, - skip: (page - 1) * pageSize, - take: pageSize, - }); + const searchKeyword = await (keyword ? keyword.trim() : null); + + const [lists, total] = await AppDataSource.getRepository(Personal) + .createQueryBuilder("personal") + .where(conditions) + .andWhere( + new Brackets((qb) => { + qb.orWhere( + searchKeyword + ? `CONCAT(prefixName, firstName," ",lastName) like '%${keyword}%'` + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ); + qb.orWhere(searchKeyword ? `positionName like '%${keyword}%'` : "1=1", { + keyword: `%${searchKeyword}%`, + }); + qb.orWhere(searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", { + keyword: `%${searchKeyword}%`, + }); + qb.orWhere(searchKeyword ? `organization like '%${keyword}%'` : "1=1", { + keyword: `%${searchKeyword}%`, + }); + qb.orWhere(searchKeyword ? `order_number like '%${keyword}%'` : "1=1", { + keyword: `%${searchKeyword}%`, + }); + }), + ) + .orderBy("updatedAt", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); if (!lists) { throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถแสดงข้อมูลได้"); @@ -146,32 +175,51 @@ export class PersonalController extends Controller { let result: any = []; - await Promise.all( - lists.map(async (item, index) => { - const probation_no = await this.assignRepository.count({ - where: { - personal_id: item.personal_id, - }, - }); + for (let i = 0; i < lists.length; i++) { + const probation_no = await this.assignRepository.count({ + where: { personal_id: lists[i].personal_id }, + }); - await result.push({ - personal_id: item.personal_id, - ordering: index + 1, - name: item.prefixName + item.firstName + " " + item.lastName, - idcard: item.idcard, - prefixName: item.prefixName, - firstName: item.firstName, - lastName: item.lastName, - position_line: item.positionName, - position_level: item.positionLevelName, - position_type: item.positionTypeName, - organization: item.organization, - probation_no: probation_no, - order_number: item.order_number, - probation_status: item.probation_status, - }); - }), - ); + await result.push({ + personal_id: lists[i].personal_id, + ordering: i + 1, + name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName, + idcard: lists[i].idcard, + position_line: lists[i].positionName, + position_level: lists[i].positionLevelName, + position_type: lists[i].positionTypeName, + organization: lists[i].organization, + probation_no: probation_no, + order_number: lists[i].order_number, + probation_status: lists[i].probation_status, + }); + } + // await Promise.all( + // lists.map(async (item, index) => { + // const probation_no = await this.assignRepository.count({ + // where: { + // personal_id: item.personal_id, + // }, + // }); + + // await result.push({ + // personal_id: item.personal_id, + // ordering: index + 1, + // name: item.prefixName + item.firstName + " " + item.lastName, + // idcard: item.idcard, + // // prefixName: item.prefixName, + // // firstName: item.firstName, + // // lastName: item.lastName, + // position_line: item.positionName, + // position_level: item.positionLevelName, + // position_type: item.positionTypeName, + // organization: item.organization, + // probation_no: probation_no, + // order_number: item.order_number, + // probation_status: item.probation_status, + // }); + // }), + // ); return new HttpSuccess({ data: result, total: total }); }