Ver Fonte

单位值获取;

lichunyang há 1 mês atrás
pai
commit
1c7f12e1e3

+ 113 - 110
src/components/PersonUnitDialog.vue

@@ -48,28 +48,29 @@
           style="width: 100%"
           :border="false"
           max-height="200px"
+          v-loading="loading"
         >
           <el-table-column
-            prop="number"
+            prop="gutId"
             :label="$t('treeData.number')"
             width="60"
           />
           <el-table-column
-            prop="chineseName"
+            prop="nameZn"
             :label="$t('treeData.chineseName')"
           />
           <el-table-column
-            prop="englishName"
+            prop="nameEn"
             :label="$t('treeData.englishName')"
           />
           <el-table-column :label="$t('treeData.unit')">
             <template #default="{ row }">
-              <el-select v-model="row.value" placeholder="" style="width: 100%">
+              <el-select v-model="row.selectedUnitId" placeholder="" style="width: 100%" @focus="fetchUnitsForRow(row)" @change="saveSelectedUnit(row)">
                 <el-option
-                  v-for="unit in unitOptions"
-                  :key="unit"
-                  :label="unit"
-                  :value="unit"
+                  v-for="unit in row.unitOptions"
+                  :key="unit.gutId"
+                  :label="unit.value"
+                  :value="unit.value"
                 />
               </el-select>
             </template>
@@ -122,12 +123,20 @@ import { request } from "@/utils/request"
 const { t, locale } = useI18n()
 import { ElMessage } from 'element-plus'
 
+const props = defineProps({
+  visible: {
+    type: Boolean,
+    default: false
+  }
+})
+
 const labelWidth = computed(() => {
   return locale.value === "zh-CN" ? "80px" : "100px"
 })
 
 // 表单引用
 const formRef = ref(null)
+
 // 表单校验规则
 const formRules = {
   chineseName: [
@@ -146,67 +155,6 @@ const formRules = {
   ]
 }
 
-// 单位系统下拉框数据
-const fetchUnitGroups = async () => {
-  const params = {
-    transCode: 'ES0024',
-  };
-  try {
-    const res = await request(params);
-    return res.rows || [];
-  } catch (err) {
-    console.error('Fetch unit groups error:', err);
-    ElMessage.error(err.returnMsg || t('error.fetchFailed'));
-    return [];
-  }
-}
-
-// 单位下拉框数据
-const updateUnit = async (row) => {
-  const group = unitGroups.value.find((g) => g.sutId === selectedGroupId.value);
-  const params = {
-    transCode: 'ES0022',
-    value: row.value,
-    gutId: row.number,
-    sutId: group.sutId
-  };
-  try {
-    const res = await request(params);
-    console.log('Update unit responseffffffdxxxxx:', res);
-    
-    return res.rows || [];
-  } catch (err) {
-    console.error('Fetch unit options error:', err);
-    ElMessage.error(err.returnMsg || t('error.fetchFailed'));
-    return [];
-  }
-};
-
-// 初始化表格数据时调用接口
-const initializeUnits = async () => {
-  if (tableData.value.length === 0 || !selectedGroupId.value) return;
-  for (const row of tableData.value) {
-    if (row.value && row.number) { // 确保 unit 和 number 存在
-      const success = await updateUnit(row);
-      if (!success) {
-        row.unit = ''; 
-      }
-    }
-  }
-};
-
-
-const props = defineProps({
-  visible: {
-    type: Boolean,
-    default: false
-  },
-  systemUnitData: {
-    type: Array,
-    default: () => [],
-  }
-})
-
 const emit = defineEmits(["update:visible", "confirm"])
 
 // 下拉框数据
@@ -214,9 +162,12 @@ const unitGroups = ref([])
 const selectedGroupId = ref(null);
 const selectedGroup = ref("")
 const unitOptions = ref([])
+const selectedUnitId = ref(null);
 
 // 表格数据
 const tableData = ref([])
+// 加载状态
+const loading = ref(false);
 
 // 输入框数据
 const formData = ref({
@@ -224,45 +175,28 @@ const formData = ref({
   englishName: ""
 })
 
+// 单位选项缓存
+const unitOptionsCache = ref({})
+
 // 加载下拉框数据并设置默认值
 onMounted(async () => {
   const groups = await fetchUnitGroups()
   unitGroups.value = groups
   if (groups.length > 0) {
     selectedGroupId.value = groups[0].sutId;
+    // 根据下拉框的选项的sutId查询表格数据
+    await fetchUnitTableData(groups[0].sutId);
   }
 })
 
-// 监听 systemUnitData 更新表格
-watch(
-  () => props.systemUnitData,
-  async (newData) => {
-    if (newData.length > 0) {
-      console.log("System Unit Data updated:", newData);
-      
-      tableData.value = newData.map((item) => ({
-        number: item.gutId || 'N/A',
-        chineseName: item.nameZh || '',
-        englishName: item.nameEn || '',
-        value: item.value || '',
-        factor: item.factor || '',
-        utOffset: item.utOffset || '',
-        unit: item.unit || '', // 默认单位
-      }));
-      await initializeUnits();
-    }
-  },
-  { immediate: true }
-);
-
-// 根据下拉框值加载表格数据
+// 单位系统改变重新加载表格数据
 watch(
   selectedGroupId,
   async (newId) => {
     const group = unitGroups.value.find((g) => g.sutId === newId);
     if (group) {
-      console.log("Selected group:", group);
-      const units = await fetchUnits(group);
+      await fetchUnitTableData(selectedGroupId.value);
+      
     } else {
       tableData.value = []
     }
@@ -270,13 +204,97 @@ watch(
   { immediate: true }
 )
 
+// 根据单位系统查看单位
+const fetchUnitsForRow = async (row) => {
+  const params = {
+    transCode: 'ES0019',
+    gutId: row.gutId
+  }
+  try {
+    const res = await request(params)
+    row.unitOptions = res.rows || []
+    // 如果当前行没有选中值,设置为第一个选项
+    if (!row.selectedUnitId && res.rows.length > 0) {
+      row.selectedUnitId = res.rows[0].value
+    }
+  } catch (err) {
+    ElMessage.error(err.returnMsg || t('error.fetchFailed'))
+    row.unitOptions = []
+  }
+}
+
+// 保存选中的单位
+const saveSelectedUnit = async (row) => {
+  try {
+    const selectedUnit = row.unitOptions.find(unit => unit.value === row.selectedUnitId)
+    console.log('Saving selected unitvvvvvvvv:', selectedUnit);
+    console.log('Row datadddddd:', row);
+    
+    
+    const params = {
+      transCode: 'ES0022', // 假设保存接口的 transCode
+      gutId: row.gutId,
+      utId: selectedUnit?.utId || '',
+      sutId: '',
+      gsutId: '',
+      value: row.selectedUnitId
+    }
+    const res = await request(params)
+    ElMessage.success(t('message.saveSuccess'))
+  } catch (err) {
+    ElMessage.error(err.returnMsg || t('message.saveFailed'))
+  }
+}
+
+
+
+// 单位系统下拉框数据
+const fetchUnitGroups = async () => {
+  const params = {
+    transCode: 'ES0024',
+  };
+  try {
+    const res = await request(params);
+    return res.rows || [];
+  } catch (err) {
+    ElMessage.error(err.returnMsg || t('error.fetchFailed'));
+    return [];
+  }
+}
+
+// 单位下拉框获取表格数据
+const fetchUnitTableData = async (sutId) => {
+  loading.value = true
+  const params = {
+    transCode: 'ES0025',
+    sutId: sutId
+  }
+  try {
+    const res = await request(params)
+    // 为每行数据添加 unitOptions 和 selectedUnitId
+    tableData.value = (res.rows || []).map(row => ({
+      ...row,
+      unitOptions: row.value ? [{ utId: row.utId, value: row.value }] : [],
+      selectedUnitId: row.value || null
+    }))
+    loading.value = false
+  } catch (err) {
+    loading.value = false
+    ElMessage.error(err.returnMsg || t('error.fetchFailed'))
+  }
+}
+
+
+
+
 // 保存中英文名称接口
 const submitForm = async () => {
   try {
     await formRef.value.validate()
     await handleSaveName()
+    const groups = await fetchUnitGroups()
+    unitGroups.value = groups
   } catch (error) {
-    console.log("表单校验失败", error)
     return false
   }
 }
@@ -296,21 +314,6 @@ const handleSaveName = async () => {
   }
 }
 
-const fetchUnits = async (group) => {
-  const params = {
-    transCode: 'ES0023',
-    gutId: group.sutId,
-  };
-  try {
-    const res = await request(params);
-    return res.rows || [];
-  } catch (err) {
-    console.error('Fetch units error:', err);
-    ElMessage.error(err.returnMsg || t('error.fetchFailed'));
-    return [];
-  }
-};
-
 // footer 确认按钮
 const handleConfirm = () => {
   emit("confirm", {

+ 3 - 1
src/components/SystemUnitDialog.vue

@@ -123,6 +123,7 @@ const fetchUnitGroups = async () => {
   };
   try {
     const res = await request(params);
+    console.log('Unit groups:', res.rows);
     return res.rows || [];
   } catch (err) {
     console.error('Fetch unit groups error:', err);
@@ -138,6 +139,7 @@ const fetchUnits = async (group) => {
   };
   try {
     const res = await request(params);
+    console.log('Units:', res.rows);
     return res.rows || [];
   } catch (err) {
     console.error('Fetch units error:', err);
@@ -147,7 +149,7 @@ const fetchUnits = async (group) => {
 };
 
 const handleConfirm = () => {
-  emit('confirm', { tableData: tableData.value });
+  emit('confirm', false);
   emit('update:visible', false);
 };
 </script>

+ 1 - 1
src/locales/en.json

@@ -114,7 +114,7 @@
     "factor": "Factor",
     "offset": "Offset",
     "unit": "Unit",
-    "unitSystem": "Unit System",
+    "unitSystem": "Unit Group System",
     "createNewUnitSystem": "This operation will create a new unit system:",
     "chineseNameRequired": "Chinese name is required",
     "englishNameRequired": "English name is required"

+ 1 - 1
src/locales/zh-CN.json

@@ -114,7 +114,7 @@
     "factor": "计算因子",
     "offset": "偏移量",
     "unit": "单位",
-    "unitSystem": "单位系统",
+    "unitSystem": "单位系统",
     "createNewUnitSystem": "该操作将创建一个新的单位系统:",
     "chineseNameRequired": "中文名称不能为空",
     "englishNameRequired": "英文名称不能为空"

+ 4 - 6
src/views/model/index.vue

@@ -219,7 +219,6 @@
     <PersonUnitDialog
       :visible="showPersonUnitDialog"
       @update:visible="showPersonUnitDialog = $event"
-      :systemUnitData="systemUnitData"
       @confirm="handlePersonUnitConfirm"
     />
 
@@ -298,15 +297,14 @@ const handleNodeClick = ({ node, data, event }) => {
 
 
 // 单元系统弹窗确认事件
-const handleSystemUnitConfirm = (data) => {
-  console.log('System Unit Dialog confirmed:', data);
-  systemUnitData.value = data.tableData || [];
+const handleSystemUnitConfirm = () => {
+  console.log('System Unit Dialog confirmed');
   showSystemUnitDialog.value = false;
 };
 
 // 个人单元弹窗确认事件
-const handlePersonUnitConfirm = (data) => {
-  console.log('Person Unit Dialog confirmed:', data);
+const handlePersonUnitConfirm = () => {
+  console.log('Person Unit Dialog confirmed');
   showPersonUnitDialog.value = false;
 };