ค้นหา special

This commit is contained in:
Kittapath 2024-05-09 10:54:19 +07:00
parent d2cb987049
commit fecbce6f88

View file

@ -45,12 +45,12 @@ export class kpiSpecialController extends Controller {
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
where: { where: {
including: String(requestBody.including), including: String(requestBody.including),
includingName: String(requestBody.includingName), includingName: String(requestBody.includingName),
}, },
}); });
if(chk_kpiSpecial){ if (chk_kpiSpecial) {
throw new HttpError( throw new HttpError(
HttpStatusCode.CONFLICT, HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
@ -88,13 +88,13 @@ export class kpiSpecialController extends Controller {
} }
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
where: { where: {
id: Not(id), id: Not(id),
including: String(requestBody.including), including: String(requestBody.including),
includingName: String(requestBody.includingName), includingName: String(requestBody.includingName),
}, },
}); });
if(chk_kpiSpecial){ if (chk_kpiSpecial) {
throw new HttpError( throw new HttpError(
HttpStatusCode.CONFLICT, HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
@ -114,7 +114,7 @@ export class kpiSpecialController extends Controller {
@Get("{id}") @Get("{id}")
async GetKpiSpecialById(@Path() id: string) { async GetKpiSpecialById(@Path() id: string) {
const KpiSpecial = await this.kpiSpecialRepository.findOne({ const KpiSpecial = await this.kpiSpecialRepository.findOne({
where: { id: id } where: { id: id },
}); });
if (!KpiSpecial) { if (!KpiSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้");
@ -151,50 +151,61 @@ export class kpiSpecialController extends Controller {
* @param pageSize * @param pageSize
* @param keyword * @param keyword
*/ */
@Get() @Post("search")
async listKpiSpecial( async listKpiSpecial(
@Query("page") page: number = 1, @Body()
@Query("pageSize") pageSize: number = 10, requestBody: {
@Query("keyword") keyword?: string, page: number;
pageSize: number;
year?: string | null;
period?: string | null;
keyword?: string | null;
},
) { ) {
let whereClause: any = {}; let condition: any = {};
if (keyword !== undefined && keyword !== "") { if (requestBody.keyword !== undefined && requestBody.keyword !== "") {
whereClause = { condition = {
where: [{ where: [
including: Like(`%${keyword}%`), {
includingName: Like(`%${keyword}%`), including: Like(`%${requestBody.keyword}%`),
}], includingName: Like(`%${requestBody.keyword}%`),
},
],
}; };
} }
const [kpiSpecial, total] = await this.kpiSpecialRepository.findAndCount({ const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial)
...whereClause, .createQueryBuilder("kpiSpecial")
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), .andWhere(condition)
order: { createdAt: "ASC" }, .andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", {
}); year: `%${requestBody.year}%`,
const mapData = kpiSpecial.map((KpiSpecial) => ({ })
id: KpiSpecial.id, .andWhere(requestBody.period ? "kpiSpecial.period LIKE :period" : "1=1", {
period: KpiSpecial.period, period: `%${requestBody.period}%`,
year: KpiSpecial.year, })
including: KpiSpecial.including, .andWhere(
includingName: KpiSpecial.includingName, new Brackets((qb) => {
target: KpiSpecial.target, qb.orWhere("kpiSpecial.including LIKE :keyword", {
unit: KpiSpecial.unit, keyword: `%${requestBody.keyword}%`,
weight: KpiSpecial.weight, }).orWhere("kpiSpecial.includingName LIKE :keyword", {
point: KpiSpecial.point, keyword: `%${requestBody.keyword}%`,
summary: KpiSpecial.summary, });
documentInfoEvidence: KpiSpecial.documentInfoEvidence, }),
startDate: KpiSpecial.startDate, )
endDate: KpiSpecial.endDate, .select([
achievement1: KpiSpecial.achievement1, "kpiSpecial.id",
achievement2: KpiSpecial.achievement2, "kpiSpecial.year",
achievement3: KpiSpecial.achievement3, "kpiSpecial.period",
achievement4: KpiSpecial.achievement4, "kpiSpecial.including",
achievement5: KpiSpecial.achievement5, "kpiSpecial.includingName",
meaning: KpiSpecial.meaning, "kpiSpecial.createdAt",
formula: KpiSpecial.formula, ])
})); .orderBy("kpiSpecial.createdAt", "DESC")
return new HttpSuccess({ data: mapData, total }); .skip((requestBody.page - 1) * requestBody.pageSize)
.take(requestBody.pageSize)
.getManyAndCount();
return new HttpSuccess({ data: kpiSpecial, total });
} }
/** /**