แก้ query total

This commit is contained in:
Kittapath 2024-04-26 12:40:23 +07:00
parent 07f5b3d570
commit 5b818556ca
4 changed files with 99 additions and 167 deletions

View file

@ -14,7 +14,7 @@ import {
Get,
Query,
} from "tsoa";
import { Not, Like } from "typeorm";
import { Not, Like, Brackets } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
@ -143,37 +143,26 @@ export class SalaryRankEmployeeController extends Controller {
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
let whereClause: any = {};
if (keyword != undefined && keyword !== "") {
whereClause = {
where: [
{ step: Like(`%${keyword}%`) },
{ salaryMonth: Like(`%${keyword}%`) },
{ salaryDay: Like(`%${keyword}%`) },
],
};
}
const [salaryRankEmployee, total] = await this.salaryRankEmployeeRepository.findAndCount({
...whereClause,
where: {
salaryEmployeeId: id,
},
select: ["id", "step", "salaryMonth", "salaryDay"],
order: {
step: "ASC",
},
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
});
// if (keyword != undefined && keyword !== "") {
// const filteredSalaryRankEmployee = salaryRankEmployee.filter(
// (x) =>
// x.step?.toString().includes(keyword) ||
// x.salaryMonth?.toString().includes(keyword) ||
// x.salaryDay?.toString().includes(keyword),
// );
// const slicedData = filteredSalaryRankEmployee.slice((page - 1) * pageSize, page * pageSize);
// return new HttpSuccess({ data: slicedData, total: filteredSalaryRankEmployee.length });
// }
const [salaryRankEmployee, total] = await AppDataSource.getRepository(SalaryRankEmployee)
.createQueryBuilder("salaryRankEmployee")
.andWhere(
new Brackets((qb) => {
qb.andWhere(`salaryRankEmployee.salaryEmployeeId LIKE :id`, { id: id }).andWhere(
new Brackets((qb) => {
qb.orWhere("salaryRankEmployee.step LIKE :keyword", { keyword: `%${keyword}%` })
.orWhere("salaryRankEmployee.salaryMonth LIKE :keyword", {
keyword: `%${keyword}%`,
})
.orWhere("salaryRankEmployee.salaryDay LIKE :keyword", { keyword: `%${keyword}%` });
}),
);
}),
)
.orderBy("salaryRankEmployee.salary", "DESC")
.addOrderBy("salaryRankEmployee.salaryHalf", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: salaryRankEmployee, total });
}