diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index d244350..aaeac81 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -178,52 +178,73 @@ export class SalaryPeriodController extends Controller { @Query("year") year: number = 2024, ) { //ssss total ผิด - let salaryPeriod: any; - let total: any; - if (year != 0) { - const startOfYear = new Date(year, 0, 1); - const endOfYear = new Date(year, 11, 31); - [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ - skip: (page - 1) * pageSize, - take: pageSize, - where: { - effectiveDate: Between(startOfYear, endOfYear), - }, - }); - } else { - [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ - skip: (page - 1) * pageSize, - take: pageSize, - }); - } + // let salaryPeriod: any; + // let total: any; + // if (year != 0) { + // const startOfYear = new Date(year, 0, 1); + // const endOfYear = new Date(year, 11, 31); + // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ + // skip: (page - 1) * pageSize, + // take: pageSize, + // where: { + // effectiveDate: Between(startOfYear, endOfYear), + // }, + // }); + // } else { + // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ + // skip: (page - 1) * pageSize, + // take: pageSize, + // }); + // } - if (keyword != undefined && keyword !== "") { - const filteredSalaryPeriod = salaryPeriod.filter( - (x: any) => - x.period.toString().includes(keyword) || - x.isActive.toString().includes(keyword) || - x.effectiveDate.getFullYear().toString().includes(keyword), - ); + // if (keyword != undefined && keyword !== "") { + // const filteredSalaryPeriod = salaryPeriod.filter( + // (x: any) => + // x.period.toString().includes(keyword) || + // x.isActive.toString().includes(keyword) || + // x.effectiveDate.getFullYear().toString().includes(keyword), + // ); - const formattedData = filteredSalaryPeriod.map((item: any) => ({ - id: item.id, - period: item.period, - isActive: item.isActive, - effectiveDate: item.effectiveDate, - status: item.status, - })); + // const formattedData = filteredSalaryPeriod.map((item: any) => ({ + // id: item.id, + // period: item.period, + // isActive: item.isActive, + // effectiveDate: item.effectiveDate, + // status: item.status, + // })); + // return new HttpSuccess({ data: formattedData, total: formattedData.length }); + // } - return new HttpSuccess({ data: formattedData, total: formattedData.length }); - } + // const formattedData = salaryPeriod.map((item: any) => ({ + // id: item.id, + // period: item.period, + // isActive: item.isActive, + // effectiveDate: item.effectiveDate, + // status: item.status, + // })); + // return new HttpSuccess({ data: formattedData, total }); + + const [salaryPeriod, total] = await AppDataSource.getRepository(SalaryPeriod) + .createQueryBuilder("SalaryPeriod") + .andWhere(year != 0 ? "SalaryPeriod.year LIKE :year" : "1=1", {year: `%${year}%`,}) + .orWhere("SalaryPeriod.period LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("SalaryPeriod.isActive LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("SalaryPeriod.year LIKE :keyword", { keyword: `%${year}%` }) + .select([ + "SalaryPeriod.id", + "SalaryPeriod.period", + "SalaryPeriod.isActive", + "SalaryPeriod.effectiveDate", + "SalaryPeriod.status", + "SalaryPeriod.year", + ]) + .orderBy("SalaryPeriod.effectiveDate", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: salaryPeriod, total }); - const formattedData = salaryPeriod.map((item: any) => ({ - id: item.id, - period: item.period, - isActive: item.isActive, - effectiveDate: item.effectiveDate, - status: item.status, - })); - return new HttpSuccess({ data: formattedData, total }); } /**