import { defineStore } from "pinia"; import { reactive, ref } from "vue"; import http from "@/plugins/http"; import config from "@/app.config"; /** importType*/ import type { DataActive, SumPosition, PosMaster, PosMaster2, } from "@/modules/02_organization/interface/response/organizational"; export const useOrganizational = defineStore("organizationalStore", () => { const typeOrganizational = ref("current"); // ประเภทโครงสร้าง const statusView = ref("list"); // การแสดงผล รายการ,map const rootId = ref(""); const isOfficer = ref(null); const isStaff = ref(null); const remark = ref(""); const dataActive = ref(); //ข้อมูลโครงสร้าง const activeId = ref(); // id โครงสร้างปัจจุบัน const draftId = ref(); // id แบบร่างโครงสร้าง const historyId = ref(); // id ประวัติโครงสร้าง const historyDnaOrgId = ref(""); // id ของโครงสร้างสำหรับใช้ในเมนูสืบทอดตำแหน่ง const isPublic = ref(false); // การเผยแพร่ const treeId = ref(); // id โหนด const level = ref(); // ระดับโหนด const orgPublishDate = ref(null); // วันเผยแพร่ const isLock = ref(false); const sumPosition = reactive({ total: 0, use: 0, vacant: 0, totalRoot: 0, useRoot: 0, vacantRoot: 0, }); /** * ฟังก์ชันกำหนดจำนวนข้อมูลตำแหน่ง * @param data ข้อมุลจำนวนตำแหน่ง */ function getSumPosition(data: SumPosition) { sumPosition.total = data.totalPosition; sumPosition.totalRoot = data.totalRootPosition ? data.totalRootPosition : 0; if (typeOrganizational.value == "draft") { sumPosition.use = data.totalPositionNextUse; sumPosition.useRoot = data.totalRootPositionNextUse ? data.totalRootPositionNextUse : 0; sumPosition.vacant = data.totalPositionNextVacant; sumPosition.vacantRoot = data.totalRootPositionNextVacant ? data.totalRootPositionNextVacant : 0; } else { sumPosition.use = data.totalPositionCurrentUse; sumPosition.useRoot = data.totalRootPositionCurrentUse ? data.totalRootPositionCurrentUse : 0; sumPosition.vacant = data.totalPositionCurrentVacant; sumPosition.vacantRoot = data.totalRootPositionCurrentVacant ? data.totalRootPositionCurrentVacant : 0; } } /** * ฟังก์ชันกำหนดข้อมูลโครงสร้าง * @param data ข้อมูลโครงสร้าง */ async function fetchDataActive(data: DataActive) { dataActive.value = data; activeId.value = data.activeId; draftId.value = data.draftId; isPublic.value = data.isPublic; orgPublishDate.value = data.orgPublishDate; } /** * ฟังก์ชันประมวลผลข้อมูลตำแหน่ง * @param data ข้อมูลตำแหน่ง * @returns ข้อมูลตำแหน่งที่ผ่านการประมวลผลแล้ว */ function fetchPosMaster(data: PosMaster[]) { const newPosMaster: PosMaster2[] = data.map((e: PosMaster) => ({ ...e, positionIsSelected: typeOrganizational.value === "draft" && e.fullNameNextHolder !== null ? e.fullNameNextHolder : typeOrganizational.value !== "draft" && e.fullNameCurrentHolder !== null ? e.fullNameCurrentHolder : "ว่าง", posMasterNo: e.orgShortname + (e.posMasterNoPrefix ? e.posMasterNoPrefix : "") + (e.posMasterNo ? ` ${e.posMasterNo}` : "") + (e.posMasterNoSuffix ? e.posMasterNoSuffix : ""), positionName: e.isSit ? e.profilePosition : e.positionName, posTypeName: e.isSit ? e.profilePostype : e.posTypeName, posLevelName: e.isSit ? e.profilePoslevel : e.posLevelName, posExecutiveName: e.posExecutiveName, isSit: e.isSit, orgShortname: e.orgShortname, posMasterNoPrefix: e.posMasterNoPrefix, posMasterNoSuffix: e.posMasterNoSuffix, posMasterNoMain: e.posMasterNo, })); return newPosMaster || []; } /** * ฟังก์ชันตรวจสอบและแปลงค่าประเภทระดับ * @param type ประเภทระดับ * @returns ชื่อประเภทระดับ */ async function checkLevel(type: number) { switch (type) { case 0: return "Root"; case 1: return "Child1"; case 2: return "Child2"; case 3: return "Child3"; default: return "Child4"; } } /** * ฟังก์ชันตรวจสอบและแปลงค่าระดับหน่วยงาน * @param type ระดับหน่วยงาน * @returns ชื่อประระดับหน่วยงาน */ function convertType(type: string) { switch (type) { case "DEPARTMENT": return "ระดับสำนัก"; case "OFFICE": return "ระดับกอง/สำนักงาน/ส่วนราชการ/โรงพยาบาล/เทียบเท่ากอง"; case "DIVISION": return "ระดับส่วน/กลุ่มภารกิจ"; case "SECTION": return "ระดับฝ่าย/กลุ่มงาน"; default: return "-"; } } /** * ฟังก์ชันแปลงค่าสถานะรายละเอียดโครงสร้าง * @param type สถานะ * @returns ชือสถานะ */ function convertStatus(type: string) { switch (type) { case "current": return "ปกติ"; case "draft": return "แบบร่าง"; case "old": return "ยุบเลิก"; default: return "-"; } } const cachedData = ref>({}); /** * ฟังก์ชันดึงข้อมูลโครงสร้าง * @param keyId id โครงสร้าง */ async function fetchDataTree(keyId: string) { // ตรวจสอบว่ามีข้อมูลใน cache แล้วหรือไม่ if (cachedData.value[keyId]) { return cachedData.value[keyId]; } // ถ้าไม่มีข้อมูลใน cache หรือ forceRefresh เป็น true ให้ดึงข้อมูลจาก API try { const res = await http.get(config.API.orgByid(keyId)); const result = res.data.result; // เก็บข้อมูลใน cache cachedData.value[keyId] = result; return result; } catch (error) { console.error("Error fetching data tree:", error); return []; } } return { typeOrganizational, statusView, // fetchDataActive, checkLevel, convertType, draftId, activeId, historyId, treeId, level, isPublic, orgPublishDate, fetchPosMaster, sumPosition, getSumPosition, isOfficer, isStaff, rootId, isLock, remark, convertStatus, fetchDataTree, historyDnaOrgId, }; });