org_053 v.queryBuilder ยังไม่เสร็จ

This commit is contained in:
AdisakKanthawilang 2024-02-01 17:28:47 +07:00
parent be71b978bf
commit 009e315378

View file

@ -33,6 +33,7 @@ import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { Position } from "../entities/Position";
import { Brackets } from "typeorm/browser";
@Route("api/v1/org/pos")
@Tags("Position")
@Security("bearerAuth")
@ -733,103 +734,140 @@ export class PositionController extends Controller {
let typeCondition: any = {};
let checkChildConditions: any = {};
if (body.type === 0) {
typeCondition = {
orgRootId: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild1Id: IsNull(),
};
}
} else if (body.type === 1) {
typeCondition = {
orgChild1Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild2Id: IsNull(),
};
}
} else if (body.type === 2) {
typeCondition = {
orgChild2Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild3Id: IsNull(),
};
}
} else if (body.type === 3) {
typeCondition = {
orgChild3Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild4Id: IsNull(),
};
}
} else if (body.type === 4) {
typeCondition = {
orgChild4Id: body.id,
};
}
const keywordCondition = {}
? {
posMasterNoPrefix: Like(`%${body.keyword}%`),
positionName: Like(`%${body.keyword}%`),
posTypeName: Like(`%${body.keyword}%`),
posLevelName: Like(`%${body.keyword}%`),
}
: {};
const posMaster = await this.posMasterRepository.find({
where: {
...typeCondition,
...checkChildConditions,
...keywordCondition,
},
order: { posMasterOrder: "ASC" },
skip: (body.page - 1) * body.pageSize,
take: body.pageSize,
});
const total = posMaster.length;
// if (body.type === 0) {
// data = data
// .andWhere("position.orgRootId = :orgRootId", { orgRootId: body.id });
// if (!posMaster || posMaster.length === 0) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
// if (!body.isAll) {
// checkChildConditions = {
// orgChild1Id: IsNull(),
// };
// }
// } else if (body.type === 1) {
// typeCondition = {
// orgChild1Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild2Id: IsNull(),
// };
// }
// } else if (body.type === 2) {
// typeCondition = {
// orgChild2Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild3Id: IsNull(),
// };
// }
// } else if (body.type === 3) {
// typeCondition = {
// orgChild3Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild4Id: IsNull(),
// };
// }
// } else if (body.type === 4) {
// typeCondition = {
// orgChild4Id: body.id,
// };
// }
const formattedData = await Promise.all(
posMaster.map(async (posMaster) => {
const positions = await this.positionRepository.find({
where: { posMasterId: posMaster.id },
relations: ["posLevel", "posType", "posExecutive"],
});
// const keywordPosMasterCondition = body.keyword
// ? {
// posMasterNoPrefix: Like(`%${body.keyword}%`),
// positionName: Like(`%${body.keyword}%`),
// posTypeName: Like(`%${body.keyword}%`),
// posLevelName: Like(`%${body.keyword}%`),
// }
// : {};
return {
id: posMaster.id,
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
positions: positions.map((position) => ({
id: position.id,
positionName: position.positionName,
positionField: position.positionField,
posTypeId: position.posTypeId,
posTypeName: position.posType == null ? null : position.posType.posTypeName,
posLevelId: position.posLevelId,
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId,
posExecutiveName:
position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
positionExecutiveField: position.positionExecutiveField,
positionArea: position.positionArea,
positionIsSelected: position.positionIsSelected,
})),
};
}),
);
return new HttpSuccess({ data: formattedData, total });
let data = AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoin("posMaster.positions", "position")
.leftJoin("position.posType", "posType")
.leftJoin("position.posLevel", "posLevel")
.andWhere("posMaster.posMasterNoPrefix LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("position.positionName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orderBy("posMaster.posMasterOrder", "ASC")
.skip((body.page - 1) * body.pageSize)
.take(body.pageSize);
if (body.type === 0) {
data = data.andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild1Id IS NULL");
}
} else if (body.type === 1) {
data = data.andWhere("posMaster.orgChild1Id = :orgChild1Id", { orgChild1Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild2Id IS NULL");
}
} else if (body.type === 2) {
data = data.andWhere("posMaster.orgChild2Id = :orgChild2Id", { orgChild2Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild3Id IS NULL");
}
} else if (body.type === 3) {
data = data.andWhere("posMaster.orgChild3Id = :orgChild3Id", { orgChild3Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild4Id IS NULL");
}
} else if (body.type === 4) {
data = data.andWhere("posMaster.orgChild4Id = :orgChild4Id", { orgChild4Id: body.id });
}
const [posMaster, total] = await data.getManyAndCount();
// const posMaster = await this.posMasterRepository.find({
// where: {
// ...typeCondition,
// ...checkChildConditions,
// },
// order: { posMasterOrder: "ASC" },
// skip: (body.page - 1) * body.pageSize,
// take: body.pageSize,
// });
// const total = posMaster.length;
const formattedData = await Promise.all(
posMaster.map(async (posMaster) => {
const positions = await this.positionRepository.find({
where: {
posMasterId: posMaster.id,
},
relations: ["posLevel", "posType", "posExecutive"],
});
return {
id: posMaster.id,
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
positions: positions.map((position) => ({
id: position.id,
positionName: position.positionName,
positionField: position.positionField,
posTypeId: position.posTypeId,
posTypeName: position.posType == null ? null : position.posType.posTypeName,
posLevelId: position.posLevelId,
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId,
posExecutiveName:
position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
positionExecutiveField: position.positionExecutiveField,
positionArea: position.positionArea,
positionIsSelected: position.positionIsSelected,
})),
};
}),
);
return new HttpSuccess({ data: formattedData, total });
} catch (error) {
return error;
}
@ -1000,27 +1038,27 @@ export class PositionController extends Controller {
* @summary ORG_041 - (ADMIN) #44
*
*/
@Post("executive")
async createPosExecutive(
@Post("executive")
async createPosExecutive(
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> }
) {
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try {
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
try {
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
@ -1029,28 +1067,27 @@ export class PositionController extends Controller {
*
* @param {string} id Id
*/
@Put("executive/{id}")
async updatePosExecutive(
@Put("executive/{id}")
async updatePosExecutive(
@Path() id: string,
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> }
) {
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
try {
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
try {
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
@ -1074,5 +1111,4 @@ export class PositionController extends Controller {
return error;
}
}
}