import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, SuccessResponse, Response, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan"; import CallAPI from "../interfaces/call-api"; import { KpiPeriod } from "../entities/kpiPeriod"; import { Brackets, IsNull } from "typeorm"; import { KpiPlanHistory } from "../entities/kpiPlanHistory"; import { KpiSpecial } from "../entities/kpiSpecial"; import { KpiRole } from "../entities/kpiRole"; import permission from "../interfaces/permission"; import { RequestWithUser } from "../middlewares/user"; import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/kpi/plan") @Tags("kpiPlan") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiPlanController extends Controller { private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); private kpiRoleRepository = AppDataSource.getRepository(KpiRole); private kpiSpecialRepository = AppDataSource.getRepository(KpiSpecial); private kpiPlanHistoryRepository = AppDataSource.getRepository(KpiPlanHistory); private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); /** * สร้างตัวชี้วัดตามแผนฯ * @param requestBody * @param request */ @Post() async createKpiPlan(@Body() requestBody: createKpiPlan, @Request() request: RequestWithUser) { await new permission().PermissionCreate(request, "SYS_EVA_INDICATOR"); const kpiPlan = Object.assign(new KpiPlan(), requestBody); if (requestBody.year != null && requestBody.period != null) { const kpiPeriod = await this.kpiPeriodRepository .createQueryBuilder("kpiPeriod") .where("kpiPeriod.year = :year", { year: requestBody.year }) .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) .getOne(); if (!kpiPeriod) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินนี้ในปีงบประมาณ " + requestBody.year, ); } } await new CallAPI() .PostData(request, "/org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { kpiPlan.root = x.root; kpiPlan.rootId = x.rootId; kpiPlan.rootShortName = x.rootShortName; kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1; kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id; kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName; kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2; kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id; kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName; kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3; kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id; kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName; kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4; kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id; kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName; }) .catch((x) => {}); await new CallAPI() .PostData(request, "/development/strategy/find/all", { strategy: requestBody.strategy, strategyId: requestBody.strategyId, }) .then((x) => { kpiPlan.strategyChild1 = x.strategyChild1; kpiPlan.strategyChild1Id = x.strategyChild1Id; kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2; kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id; kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3; kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id; kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4; kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id; kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5; kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id; }) .catch((x) => {}); let maxIncludingPlan: any; if (requestBody.node == 0) { maxIncludingPlan = await this.kpiPlanRepository .createQueryBuilder("kpiPlan") .select("MAX(kpiPlan.including)", "maxIncluding") .where("kpiPlan.rootId = :rootId AND kpiPlan.child1Id IS NULL", { rootId: requestBody.nodeId, }) .andWhere("kpiPlan.year = :year", { year: requestBody.year, }) .andWhere("kpiPlan.period = :period", { period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 1) { maxIncludingPlan = await this.kpiPlanRepository .createQueryBuilder("kpiPlan") .select("MAX(kpiPlan.including)", "maxIncluding") .where("kpiPlan.child1Id = :child1Id AND kpiPlan.child2Id IS NULL", { child1Id: requestBody.nodeId, }) .andWhere("kpiPlan.year = :year", { year: requestBody.year, }) .andWhere("kpiPlan.period = :period", { period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 2) { maxIncludingPlan = await this.kpiPlanRepository .createQueryBuilder("kpiPlan") .select("MAX(kpiPlan.including)", "maxIncluding") .where("kpiPlan.child2Id = :child2Id AND kpiPlan.child3Id IS NULL", { child2Id: requestBody.nodeId, }) .andWhere("kpiPlan.year = :year", { year: requestBody.year, }) .andWhere("kpiPlan.period = :period", { period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 3) { maxIncludingPlan = await this.kpiPlanRepository .createQueryBuilder("kpiPlan") .select("MAX(kpiPlan.including)", "maxIncluding") .where("kpiPlan.child3Id = :child3Id AND kpiPlan.child4Id IS NULL", { child3Id: requestBody.nodeId, }) .andWhere("kpiPlan.year = :year", { year: requestBody.year, }) .andWhere("kpiPlan.period = :period", { period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 4) { maxIncludingPlan = await this.kpiPlanRepository .createQueryBuilder("kpiPlan") .select("MAX(kpiPlan.including)", "maxIncluding") .where("kpiPlan.child4Id = :child4Id", { child4Id: requestBody.nodeId, }) .andWhere("kpiPlan.year = :year", { year: requestBody.year, }) .andWhere("kpiPlan.period = :period", { period: requestBody.period, }) .getRawOne(); } const before = null; kpiPlan.including = maxIncludingPlan.maxIncluding + 1; kpiPlan.createdUserId = request.user.sub; kpiPlan.createdFullName = request.user.name; kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; kpiPlan.createdAt = new Date(); kpiPlan.lastUpdatedAt = new Date(); await this.kpiPlanRepository.save(kpiPlan, { data: request }); setLogDataDiff(request, { before, after: kpiPlan }); const history = new KpiPlanHistory(); history.kpiPlanId = kpiPlan.id; history.createdUserId = request.user.sub; history.createdFullName = request.user.name; history.lastUpdateUserId = request.user.sub; history.lastUpdateFullName = request.user.name; history.createdAt = new Date(); history.lastUpdatedAt = new Date(); await this.kpiPlanHistoryRepository.save(history, { data: request }); setLogDataDiff(request, { before: null, after: history }); return new HttpSuccess(kpiPlan.id); } /** * API แก้ไขตัวชี้วัดตามแผนฯ * @param id * @param requestBody * @param request */ @Put("{id}") async updateKpiPlan( @Path() id: string, @Body() requestBody: updateKpiPlan, @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_EVA_INDICATOR"); const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } if (requestBody.year != null) { const kpiPeriod = await this.kpiPeriodRepository .createQueryBuilder("kpiPeriod") .where("kpiPeriod.year = :year", { year: requestBody.year }) .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) .getOne(); if (!kpiPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } } Object.assign(kpiPlan, requestBody); await new CallAPI() .PostData(request, "/org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { kpiPlan.root = x.root; kpiPlan.rootId = x.rootId; kpiPlan.rootShortName = x.rootShortName; kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1; kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id; kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName; kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2; kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id; kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName; kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3; kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id; kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName; kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4; kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id; kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName; }) .catch((x) => {}); await new CallAPI() .PostData(request, "/development/strategy/find/all", { strategy: requestBody.strategy, strategyId: requestBody.strategyId, }) .then((x) => { kpiPlan.strategyChild1 = x.strategyChild1; kpiPlan.strategyChild1Id = x.strategyChild1Id; kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2; kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id; kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3; kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id; kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4; kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id; kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5; kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id; }) .catch((x) => {}); const before = structuredClone(kpiPlan); kpiPlan.createdUserId = request.user.sub; kpiPlan.createdFullName = request.user.name; kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; kpiPlan.createdAt = new Date(); kpiPlan.lastUpdatedAt = new Date(); await this.kpiPlanRepository.save(kpiPlan, { data: request }); setLogDataDiff(request, { before, after: kpiPlan }); const history = new KpiPlanHistory(); history.kpiPlanId = kpiPlan.id; history.createdUserId = request.user.sub; history.createdFullName = request.user.name; history.lastUpdateUserId = request.user.sub; history.lastUpdateFullName = request.user.name; history.createdAt = new Date(); history.lastUpdatedAt = new Date(); await this.kpiPlanHistoryRepository.save(history, { data: request }); setLogDataDiff(request, { before: null, after: history }); return new HttpSuccess(id); } /** * API ตัวชี้วัดตามแผนฯ * @param id Guid, *Id ตัวชี้วัดตามแผนฯ */ @Get("edit/{id}") async GetKpiPlanByIdEdit(@Request() request: RequestWithUser, @Path() id: string) { let _workflow = await new permission().Workflow(request, id, "SYS_EVA_INDICATOR"); if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_INDICATOR"); const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, relations: { kpiPeriod: true }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } let node = null; let nodeId = null; let nodeName = null; if (kpiPlan.child4Id != null) { node = 4; nodeId = kpiPlan.child4Id; nodeName = kpiPlan.child4; } else if (kpiPlan.child3Id != null) { node = 3; nodeId = kpiPlan.child3Id; nodeName = kpiPlan.child3; } else if (kpiPlan.child2Id != null) { node = 2; nodeId = kpiPlan.child2Id; nodeName = kpiPlan.child2; } else if (kpiPlan.child1Id != null) { node = 1; nodeId = kpiPlan.child1Id; nodeName = kpiPlan.child1; } else if (kpiPlan.rootId != null) { node = 0; nodeId = kpiPlan.rootId; nodeName = kpiPlan.root; } let strategy = null; let strategyId = null; let strategyName = null; if (kpiPlan.strategyChild5Id != null) { strategy = 5; strategyId = kpiPlan.strategyChild5Id; } else if (kpiPlan.strategyChild4Id != null) { strategy = 4; strategyId = kpiPlan.strategyChild4Id; strategyName = kpiPlan.strategyChild4; } else if (kpiPlan.strategyChild3Id != null) { strategy = 3; strategyId = kpiPlan.strategyChild3Id; strategyName = kpiPlan.strategyChild3; } else if (kpiPlan.strategyChild2Id != null) { strategy = 2; strategyId = kpiPlan.strategyChild2Id; strategyName = kpiPlan.strategyChild2; } else if (kpiPlan.strategyChild1Id != null) { strategy = 1; strategyId = kpiPlan.strategyChild1Id; strategyName = kpiPlan.strategyChild1; } const formattedData = { id: kpiPlan.id, // year: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.year, // round: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.durationKPI, year: kpiPlan.year == null ? null : kpiPlan.year, round: kpiPlan.period == null ? null : kpiPlan.period, kpiPeriodId: kpiPlan.kpiPeriodId, including: kpiPlan.including, includingName: kpiPlan.includingName, target: kpiPlan.target, unit: kpiPlan.unit, weight: kpiPlan.weight, achievement1: kpiPlan.achievement1, achievement2: kpiPlan.achievement2, achievement3: kpiPlan.achievement3, achievement4: kpiPlan.achievement4, achievement5: kpiPlan.achievement5, meaning: kpiPlan.meaning, formula: kpiPlan.formula, root: kpiPlan.rootId, child1: kpiPlan.child1Id, child2: kpiPlan.child2Id, child3: kpiPlan.child3Id, child4: kpiPlan.child4Id, node: node, nodeId: nodeId, nodeName: nodeName, orgRevisionId: kpiPlan.orgRevisionId, strategy: strategy, strategyId: strategyId, strategyName: strategyName, strategyChild1: kpiPlan.strategyChild1Id, strategyChild2: kpiPlan.strategyChild2Id, strategyChild3: kpiPlan.strategyChild3Id, strategyChild4: kpiPlan.strategyChild4Id, strategyChild5: kpiPlan.strategyChild5Id, documentInfoEvidence: kpiPlan.documentInfoEvidence, }; return new HttpSuccess(formattedData); } /** * API ตัวชี้วัดตามแผนฯ * @param id Guid, *Id ตัวชี้วัดตามแผนฯ */ @Get("{id}") async GetKpiPlanById(@Request() request: RequestWithUser, @Path() id: string) { const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, relations: { kpiPeriod: true }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } let node = null; let nodeId = null; let nodeName = null; if (kpiPlan.child4Id != null) { node = 4; nodeId = kpiPlan.child4Id; nodeName = kpiPlan.child4; } else if (kpiPlan.child3Id != null) { node = 3; nodeId = kpiPlan.child3Id; nodeName = kpiPlan.child3; } else if (kpiPlan.child2Id != null) { node = 2; nodeId = kpiPlan.child2Id; nodeName = kpiPlan.child2; } else if (kpiPlan.child1Id != null) { node = 1; nodeId = kpiPlan.child1Id; nodeName = kpiPlan.child1; } else if (kpiPlan.rootId != null) { node = 0; nodeId = kpiPlan.rootId; nodeName = kpiPlan.root; } let strategy = null; let strategyId = null; let strategyName = null; if (kpiPlan.strategyChild5Id != null) { strategy = 5; strategyId = kpiPlan.strategyChild5Id; } else if (kpiPlan.strategyChild4Id != null) { strategy = 4; strategyId = kpiPlan.strategyChild4Id; strategyName = kpiPlan.strategyChild4; } else if (kpiPlan.strategyChild3Id != null) { strategy = 3; strategyId = kpiPlan.strategyChild3Id; strategyName = kpiPlan.strategyChild3; } else if (kpiPlan.strategyChild2Id != null) { strategy = 2; strategyId = kpiPlan.strategyChild2Id; strategyName = kpiPlan.strategyChild2; } else if (kpiPlan.strategyChild1Id != null) { strategy = 1; strategyId = kpiPlan.strategyChild1Id; strategyName = kpiPlan.strategyChild1; } const formattedData = { id: kpiPlan.id, // year: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.year, // round: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.durationKPI, year: kpiPlan.year == null ? null : kpiPlan.year, round: kpiPlan.period == null ? null : kpiPlan.period, kpiPeriodId: kpiPlan.kpiPeriodId, including: kpiPlan.including, includingName: kpiPlan.includingName, target: kpiPlan.target, unit: kpiPlan.unit, weight: kpiPlan.weight, achievement1: kpiPlan.achievement1, achievement2: kpiPlan.achievement2, achievement3: kpiPlan.achievement3, achievement4: kpiPlan.achievement4, achievement5: kpiPlan.achievement5, meaning: kpiPlan.meaning, formula: kpiPlan.formula, root: kpiPlan.rootId, child1: kpiPlan.child1Id, child2: kpiPlan.child2Id, child3: kpiPlan.child3Id, child4: kpiPlan.child4Id, node: node, nodeId: nodeId, nodeName: nodeName, orgRevisionId: kpiPlan.orgRevisionId, strategy: strategy, strategyId: strategyId, strategyName: strategyName, strategyChild1: kpiPlan.strategyChild1Id, strategyChild2: kpiPlan.strategyChild2Id, strategyChild3: kpiPlan.strategyChild3Id, strategyChild4: kpiPlan.strategyChild4Id, strategyChild5: kpiPlan.strategyChild5Id, documentInfoEvidence: kpiPlan.documentInfoEvidence, }; return new HttpSuccess(formattedData); } /** * API list ตัวชี้วัดตามแผนฯ * @param page * @param pageSize * @param keyword */ @Post("search") async listKpiPlan( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; year?: string | null; period?: string | null; nodeId?: string | null; node?: number | null; keyword?: string | null; isAll?: boolean | false; // isNull?: boolean | false; }, ) { let condition = ""; let parameters: any = {}; if (requestBody.isAll === false) { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId AND kpiPlan.child1Id IS NULL"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId AND kpiPlan.child2Id IS NULL"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId AND kpiPlan.child3Id IS NULL"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId AND kpiPlan.child4Id IS NULL"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } else { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod") .andWhere(condition, parameters) .andWhere(requestBody.year ? "kpiPlan.year LIKE :year" : "1=1", { year: `%${requestBody.year}%`, }) .andWhere(requestBody.period ? "kpiPlan.period LIKE :period" : "1=1", { period: `%${requestBody.period}%`, }) .andWhere( new Brackets((qb) => { qb.orWhere("kpiPlan.including LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.includingName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.year LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.period LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }); }), ) .select([ "kpiPlan.id", "kpiPeriod.year", "kpiPeriod.durationKPI", "kpiPlan.including", "kpiPlan.includingName", "kpiPlan.createdAt", ]) .orderBy("kpiPlan.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiPlan, total }); } /** * API list ตัวชี้วัดตามแผนฯ * @param page * @param pageSize * @param keyword */ @Post("search-kpi-plan") async searchKpiPlan( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; year?: string | null; period?: string | null; nodeId?: string | null; node?: number | null; keyword?: string | null; isAll?: boolean | false; // isNull?: boolean | false; }, ) { let _data = await new permission().PermissionList(request, "SYS_KPI_LIST"); let condition = ""; let parameters: any = {}; if (requestBody.isAll === false) { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId AND kpiPlan.child1Id IS NULL"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId AND kpiPlan.child2Id IS NULL"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId AND kpiPlan.child3Id IS NULL"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId AND kpiPlan.child4Id IS NULL"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } else { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod") .andWhere(condition, parameters) .andWhere(requestBody.year ? "kpiPlan.year LIKE :year" : "1=1", { year: `%${requestBody.year}%`, }) .andWhere(requestBody.period ? "kpiPlan.period LIKE :period" : "1=1", { period: `%${requestBody.period}%`, }) .andWhere( new Brackets((qb) => { qb.orWhere("kpiPlan.including LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.includingName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.year LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.period LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }); }), ) .select([ "kpiPlan.id", "kpiPeriod.year", "kpiPeriod.durationKPI", "kpiPlan.including", "kpiPlan.includingName", "kpiPlan.createdAt", ]) .orderBy("kpiPlan.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiPlan, total }); } /** * API list ตัวชี้วัดตามแผนฯ * @param page * @param pageSize * @param keyword */ @Post("search-edit") async listKpiPlanEdit( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; year?: string | null; period?: string | null; nodeId?: string | null; node?: number | null; keyword?: string | null; isAll?: boolean | false; // isNull?: boolean | false; }, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR"); let condition = ""; let parameters: any = {}; if (requestBody.isAll === false) { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId AND kpiPlan.child1Id IS NULL"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId AND kpiPlan.child2Id IS NULL"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId AND kpiPlan.child3Id IS NULL"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId AND kpiPlan.child4Id IS NULL"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } else { switch (requestBody.node) { case 0: condition = "kpiPlan.rootId LIKE :nodeId"; break; case 1: condition = "kpiPlan.child1Id LIKE :nodeId"; break; case 2: condition = "kpiPlan.child2Id LIKE :nodeId"; break; case 3: condition = "kpiPlan.child3Id LIKE :nodeId"; break; case 4: condition = "kpiPlan.child4Id LIKE :nodeId"; break; default: condition = "1=1"; break; } parameters.nodeId = `%${requestBody.nodeId}%`; } const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod") .andWhere(condition, parameters) .andWhere(requestBody.year ? "kpiPlan.year LIKE :year" : "1=1", { year: `%${requestBody.year}%`, }) .andWhere(requestBody.period ? "kpiPlan.period LIKE :period" : "1=1", { period: `%${requestBody.period}%`, }) .andWhere( new Brackets((qb) => { qb.orWhere("kpiPlan.including LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.includingName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.year LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiPlan.period LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }); }), ) .select([ "kpiPlan.id", "kpiPeriod.year", "kpiPeriod.durationKPI", "kpiPlan.including", "kpiPlan.includingName", "kpiPlan.createdAt", ]) .orderBy("kpiPlan.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiPlan, total }); } /** * API ลบตัวชี้วัดตามแผนฯ * @param id */ @Delete("{id}") async deleteKpiPlan(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionDelete(request, "SYS_EVA_INDICATOR"); const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } let type = 0; if (kpiPlan.child1Id != null) { type = 1; } else if (kpiPlan.child2Id != null) { type = 2; } else if (kpiPlan.child3Id != null) { type = 3; } else if (kpiPlan.child4Id != null) { type = 4; } await this.kpiPlanHistoryRepository.delete({ kpiPlanId: id }); await this.kpiPlanRepository.remove(kpiPlan, { data: request }); if (kpiPlan) { let remainingKpiPlans: any; if (type == 0) { remainingKpiPlans = await this.kpiPlanRepository.find({ where: { rootId: kpiPlan.rootId, child1Id: IsNull(), }, order: { including: "ASC", }, }); } else if (type == 1) { remainingKpiPlans = await this.kpiPlanRepository.find({ where: { child1Id: kpiPlan.child1Id, child2Id: IsNull(), }, order: { including: "ASC", }, }); } else if (type == 2) { remainingKpiPlans = await this.kpiPlanRepository.find({ where: { child2Id: kpiPlan.child2Id, child3Id: IsNull(), }, order: { including: "ASC", }, }); } else if (type == 3) { remainingKpiPlans = await this.kpiPlanRepository.find({ where: { child3Id: kpiPlan.child3Id, child4Id: IsNull(), }, order: { including: "ASC", }, }); } else if (type == 4) { remainingKpiPlans = await this.kpiPlanRepository.find({ where: { child4Id: kpiPlan.child4Id, }, order: { including: "ASC", }, }); } remainingKpiPlans.forEach((kpiPlan: any, index: any) => { kpiPlan.including = index + 1; }); remainingKpiPlans.lastUpdateUserId = request.user.sub; remainingKpiPlans.lastUpdateFullName = request.user.name; remainingKpiPlans.lastUpdatedAt = new Date(); await this.kpiPlanRepository.save(remainingKpiPlans, { data: request }); } return new HttpSuccess(); } /** * API ประวัดิตัวชี้วัดิตามแผน * @param id Guid, *Id ประวัดิตัวชี้วัดตามแผน */ @Get("history/{id}") async GetHistory(@Request() request: RequestWithUser, @Path() id: string) { let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR"); const kpiPlanHistory = await this.kpiPlanHistoryRepository.find({ where: { kpiPlanId: id }, order: { createdAt: "ASC", }, }); if (!kpiPlanHistory) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัดิตัวชี้วัดตามแผนนี้"); } return new HttpSuccess(kpiPlanHistory); } /** * API summary ของตัวชี้วัดในระบบ */ @Post("summary/indicator") async GetSummary() { const kpiPlan = await this.kpiPlanRepository.count(); const kpiRole = await this.kpiRoleRepository.count(); const kpiSpecial = await this.kpiSpecialRepository.count(); return new HttpSuccess({ kpiPlan, kpiRole, kpiSpecial, total: kpiPlan + kpiRole + kpiSpecial, }); } }