From 9bec4dcae002f36aa23231da11cb03a156b40110 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周三, 30 4月 2025 16:24:16 +0800
Subject: [PATCH] 222

---
 PipeLineLems/web/src/libs/Store/Store.ts |  385 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 378 insertions(+), 7 deletions(-)

diff --git a/PipeLineLems/web/src/libs/Store/Store.ts b/PipeLineLems/web/src/libs/Store/Store.ts
index db8cb73..c503e99 100644
--- a/PipeLineLems/web/src/libs/Store/Store.ts
+++ b/PipeLineLems/web/src/libs/Store/Store.ts
@@ -1,27 +1,398 @@
-import { reactive, onMounted, ref, Ref } from 'vue'
+import { reactive, onMounted, ref, Ref, computed } from 'vue'
+import {
+  getEnumList,
+  getWorkSectionApi,
+  getWorkStationApi,
+  getFormDataBySettings,
+  getFlowData,
+  getMaterialTypes,
+  getMaterialTypesEnum,
+  getFeatureListData,
+} from '@/api/common-enum'
 
 import { createGlobalState } from '@vueuse/core'
+import { FeatureItemType, FeatureType } from './Store.d'
+import sdk from 'sdk'
 
+interface BaseState {
+  systemConfig: Record<string, any>
+  workSectionList: Record<string, any>
+  featureList: Record<string, any>
+  featureMap: Record<string, any>
+}
+
+enum BaseKeyEnum {
+  systemConfig = 'systemConfig',
+  workSectionList = 'workSectionList',
+  featureList = 'featureList',
+  featureMap = 'featureMap',
+}
 declare global {
   interface Window {
     __globalState: any // 瀹氫箟浣犺鏆撮湶缁� window 鐨勫彉閲忕殑绫诲瀷
-    __BaseState__: {
-      workSectionList: Record<string, any>
-    }
+    __BaseState__: BaseState
   }
 }
+interface StateType<T> {
+  state: Ref<T>
+}
 
+const baseDataGenerate = (key?: BaseKeyEnum) => {
+  const data: BaseState = {
+    systemConfig: {
+      state: ref({}),
+    },
+    workSectionList: {
+      state: ref([]),
+    },
+    featureList: {
+      state: ref([]),
+    },
+    featureMap: {
+      state: ref({}),
+    },
+  }
+  if (key) {
+    return data[key]
+  }
+  return data
+}
+
+const getDataValue = (key: BaseKeyEnum) => {
+  const { systemConfig, workSectionList, featureList, featureMap } =
+    window.__BaseState__
+  return {
+    systemConfig: systemConfig || baseDataGenerate(BaseKeyEnum.systemConfig),
+    workSectionList:
+      workSectionList || baseDataGenerate(BaseKeyEnum.workSectionList),
+    featureList: featureList || baseDataGenerate(BaseKeyEnum.featureList),
+    featureMap: featureMap || baseDataGenerate(BaseKeyEnum.featureMap),
+  }[key]
+}
 /**
  * 鍏ㄥ眬鐘舵��
  */
-export const globalState = reactive<Record<string, any>>({})
+export const globalState = reactive({})
 
+if (window) {
+  // @ts-ignore
+  window.__BaseState__ = window.__BaseState__ || baseDataGenerate()
+}
+interface StateRef {
+  state: Ref
+}
 /**
  * 鍏ㄥ眬hook
  * @returns
  */
-export const useGlobalState = createGlobalState(() => {
-  const state = {}
+export const useGlobalState = createGlobalState<{
+  systemConfig: StateRef
+  workSectionList: StateRef
+  featureList: StateRef
+  featureMap: StateRef
+  productionLineList: StateRef
+  ProductModelList: StateRef
+  materialTypeList: StateRef
+  workStationList: StateRef
+  hasProductionLine: Ref<boolean>
+  getSystemConfig: () => Promise<any>
+  getWorkSectionList: (segmentId?: string) => Promise<any>
+  getProductModelList: () => Promise<any>
+  getFlowList: () => Promise<any>
+  getMaterialTypeList: () => Promise<any>
+  getWorkStationList: () => Promise<any>
+  getProductList: () => Promise<any>
+  // @ts-ignore
+}>(() => {
+  // if(!sessionStorage.getItem('Token')) return {}
+  /**
+   * 宸ュ簭鍒楄〃
+   */
+  const workSectionList = getDataValue(
+    BaseKeyEnum.workSectionList
+  ) as StateType<Array<any>>
+  /**
+   * 绯荤粺閰嶇疆
+   */
+  const systemConfig = getDataValue(BaseKeyEnum.systemConfig) as StateType<
+    Record<string, any>
+  >
 
+  /**
+   * 鐗堟湰鎺у埗
+   */
+  const featureList = getDataValue(BaseKeyEnum.featureList) as StateType<
+    Array<any>
+  >
+
+  /**
+   * 鐗堟湰鎺у埗Map
+   */
+  const featureMap = getDataValue(BaseKeyEnum.featureMap) as StateType<
+    Record<string, any>
+  >
+
+  /**
+   * 浜х嚎鍒楄〃
+   */
+  const productionLineList: StateType<Array<any>> = {
+    state: ref([]),
+  }
+  /**
+   * 浜у搧鍨嬪彿鍒楄〃
+   */
+  const ProductModelList: StateType<Array<any>> = {
+    state: ref([]),
+  }
+
+  /**
+   * 鐗╂枡鍒楄〃
+   */
+  const materialTypeList: StateType<Array<any>> = {
+    state: ref([]),
+  }
+
+  /**
+   * 宸ヤ綅鍒楄〃
+   */
+  const workStationList: StateType<Array<any>> = {
+    state: ref([]),
+  }
+
+  /**
+   * (寮傛)浜х嚎娈垫暟鎹�
+   */
+  const getProductList = async () => {
+    const res = await getEnumList('ProductionLineSegment')
+    const data = res?.map(
+      (item: { name: string; description: string; value: string }) => {
+        return {
+          ...item,
+          product: item.description,
+        }
+      }
+    )
+    productionLineList.state.value = data || []
+    return data || []
+  }
+
+  /**
+   * (寮傛)浜у搧鍨嬪彿鏁版嵁
+   */
+  const getProductModelList = async () => {
+    const res = await getEnumList('ProductModel')
+    ProductModelList.state.value = res || []
+    return res
+  }
+
+  /**
+   * (寮傛)鑾峰彇宸ュ簭
+   */
+  const getWorkSectionList = async (segmentId?: string) => {
+    const params: Record<string, any> = {
+      MaxResultCount: 999,
+      SkipCount: 0,
+    }
+    if (segmentId) {
+      params.Segment = segmentId
+    }
+    const res = await getWorkSectionApi(params)
+    const items = res.items.map((v) => {
+      return {
+        label: v.name,
+        value: v.id,
+        workSectionCode: v.code,
+        source: { ...v },
+      }
+    })
+    if (!segmentId) {
+      workSectionList.state.value = items || []
+    }
+    return items
+  }
+
+  /**
+   * (寮傛)鑾峰彇宸ヤ綅
+   */
+  const getWorkStationList = async () => {
+    const res = await getWorkStationApi({
+      MaxResultCount: 999,
+      SkipCount: 0,
+    })
+    const items = res.items.map((v) => {
+      return {
+        label: v.name,
+        value: v.id,
+      }
+    })
+    workStationList.state.value = items || []
+    return items
+  }
+
+  const config = {}
+  let systemSetTimeT: any
+  let oldSystemSetTimeT: any
+  const getSystemConfig = async (checked: boolean = false) => {
+    const fn = async () => {
+      const data = await getFormDataBySettings()
+      systemConfig.state.value = data || config
+    }
+    async function handleCheckedState() {
+      await fn()
+      await getFeatureList()
+    }
+
+    async function awaitInitLoading() {
+      return new Promise<void>((resolve) => {
+        const t = setInterval(() => {
+          if (
+            Object.keys(systemConfig.state.value).length &&
+            Object.keys(featureList.state.value).length
+          ) {
+            resolve()
+            clearInterval(t)
+          }
+        }, 100)
+      })
+    }
+    return new Promise<void>((resolve) => {
+      const getConfig = async () => {
+        const condition = systemSetTimeT !== oldSystemSetTimeT && !checked
+        oldSystemSetTimeT = systemSetTimeT
+        if (condition) {
+          await handleCheckedState()
+          resolve()
+        } else {
+          await awaitInitLoading()
+          resolve()
+        }
+
+        systemSetTimeT && clearTimeout(systemSetTimeT)
+      }
+      systemSetTimeT = setTimeout(getConfig, 100)
+    })
+  }
+  /**
+   * 鑾峰彇娴佺▼鏁版嵁
+   * @returns
+   */
+  const getFlowList = async () => {
+    const data = await getFlowData()
+    return data.items.map((item: Record<string, any>) => {
+      return {
+        label: item.name,
+        value: item.type,
+        id: item.id,
+      }
+    })
+  }
+
+  const getMaterialTypeList = async () => {
+    const res: Array<{
+      value: number
+      description: string
+    }> = await getMaterialTypesEnum()
+    const types = res.map((item) => {
+      return {
+        value: Number(item.value),
+        label: item.description,
+      }
+    })
+    materialTypeList.state.value = types
+    return types
+    // const types = []
+    // const res = await getMaterialTypes()
+    // // const
+    // await getMaterialTypesEnum()
+    // for (let key in res) {
+    //   types.push({
+    //     label: res[key],
+    //     value: Number(key),
+    //   })
+    // }
+    // materialTypeList.state.value = types
+    // return types
+  }
+
+  const getFeatureList = async () => {
+    if (
+      !featureList.state.value.length &&
+      systemConfig.state.value?.PluginEdition
+    ) {
+      const res = await getFeatureListData(
+        systemConfig.state.value?.PluginEdition
+      )
+      featureList.state.value = res.groups || []
+      res.groups.forEach((item: FeatureItemType) => {
+        item.features.forEach((feature: FeatureType) => {
+          let value: string | boolean | number = feature.value
+          const key: string = feature.name
+          if (feature.valueType.validator.name === 'BOOLEAN') {
+            value = feature.value === 'true'
+          }
+          featureMap.state.value[key] = {
+            ...feature,
+            value,
+          }
+        })
+      })
+      return res
+    }
+    return
+  }
+
+  const getVersion = (version: string) =>
+    Number(version?.split?.('.')?.join?.(''))
+  const sdkVersion = sdk.version.includes('-beta')
+    ? sdk.version.replace('-beta', '')
+    : sdk.version
+  const currentVersion = getVersion(sdkVersion)
+  const initFn = () => {
+    getProductList()
+    getProductModelList()
+    getWorkSectionList()
+    getWorkStationList()
+    getMaterialTypeList()
+    getSystemConfig()
+  }
+  /**
+   * 鏄惁鍚湁浜х嚎缁撴瀯
+   * true - 鍚湁浜х嚎缁撴瀯
+   * false - 涓嶅惈鏈変骇绾跨粨鏋�
+   */
+  const hasProductionLine = computed(() => {
+    const state: Ref<any> = systemConfig.state
+    const structure = state.value.ProductionLineStructure
+    return structure == 1
+  })
+
+  // if (import.meta.env.PROD && currentVersion >= getVersion('2.13.0')) {
+  //   initFn()
+  // }
+
+  // if (import.meta.env.DEV) {
+  //   initFn()
+  // }
+
+  const state = {
+    productionLineList,
+    ProductModelList,
+    workStationList,
+    materialTypeList,
+    workSectionList,
+    systemConfig,
+    featureList,
+    featureMap,
+    hasProductionLine,
+    getSystemConfig,
+    getWorkSectionList,
+    getProductModelList,
+    getFlowList,
+    getMaterialTypeList,
+    getWorkStationList,
+    getProductList,
+  }
+
+  window.__globalState = state
   return state
 })

--
Gitblit v1.9.3