SLDataDialog.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <!-- 模拟数据弹窗 -->
  3. <el-dialog
  4. v-model="visible"
  5. align-center
  6. :append-to-body="true"
  7. width="500"
  8. class="dialog_class"
  9. draggable
  10. >
  11. <template #header="{ titleId, titleClass }">
  12. <div class="my-header">
  13. <h4 :id="titleId" :class="titleClass">模拟数据</h4>
  14. </div>
  15. </template>
  16. <el-table :data="tableSLData" border height="300" style="width: 100%">
  17. <el-table-column type="index" width="40" label="" />
  18. <el-table-column prop="name" label="属性"> </el-table-column>
  19. <el-table-column prop="value" label="值">
  20. <template #default="{ row }">
  21. <el-select
  22. v-if="row.valueType === 1"
  23. v-model="row.value"
  24. placeholder="请选择"
  25. class="full-width-select"
  26. >
  27. <el-option
  28. v-for="option in row.options"
  29. :key="option.val"
  30. :label="option.tag"
  31. :value="option.val"
  32. />
  33. </el-select>
  34. <el-input
  35. v-else-if="row.valueType === 2"
  36. v-model="row.value"
  37. class="full-width-input"
  38. />
  39. <el-button v-else-if="row.valueType === 3" />
  40. <div v-else>{{ row.valueDef }}</div>
  41. </template>
  42. </el-table-column>
  43. <el-table-column prop="unit" label="单位" width="100">
  44. <template #default="{ row }">
  45. <el-select
  46. v-if="row.unitType !== '无'"
  47. v-model="row.unitDef"
  48. placeholder="请选择"
  49. :loading="moreOptionsLoading"
  50. @focus="fetchUnitsForRow(row)"
  51. @change="(newUnit) => handleUnitChange(row, newUnit)"
  52. class="full-width-select"
  53. >
  54. <el-option
  55. v-for="option in row.unitoptions"
  56. :key="option.utId"
  57. :label="option.value"
  58. :value="option.value"
  59. />
  60. </el-select>
  61. <div v-else>{{ row.unitType }}</div>
  62. </template>
  63. </el-table-column>
  64. </el-table>
  65. <template #footer>
  66. <span class="lastbtn">
  67. <el-button @click="closeDialog">{{ $t('dialog.cancel') }}</el-button>
  68. <el-button type="primary" @click="saveSLTabelDialog">
  69. {{ $t('dialog.ok') }}
  70. </el-button>
  71. </span>
  72. </template>
  73. </el-dialog>
  74. </template>
  75. <script setup>
  76. import { request, getImage } from "@/utils/request"
  77. import { ElMessage } from "element-plus"
  78. const visible = ref(false)
  79. const tableSLData = ref([])
  80. const runtype = ref('') // 用于分开不同求解运行
  81. // 单位选项缓存
  82. const unitOptionsCache = ref({});
  83. // 更多选项加载状态
  84. const moreOptionsLoading = ref(false);
  85. let emit = defineEmits(['selectRunType']);
  86. function openDialog(pid) {
  87. getSLData(pid);
  88. visible.value = true
  89. }
  90. function closeDialog() {
  91. visible.value = false
  92. }
  93. // 组件参数点击下拉框调用接口获取单位
  94. const fetchUnitsForRow = async (row) => {
  95. row.prevUnitDef = row.unitDef || row.unit || "无"
  96. if (unitOptionsCache.value[row.unitType]) {
  97. row.unitoptions = unitOptionsCache.value[row.unitType]
  98. if (!row.unitDef && row.unitOptions.length > 0) {
  99. row.unitDef = row.unitOptions[0].value
  100. }
  101. return
  102. }
  103. moreOptionsLoading.value = true;
  104. const params = {
  105. transCode: "ES0019",
  106. gutId: row.unitType
  107. }
  108. try {
  109. const res = await request(params)
  110. row.unitoptions = res.rows || []
  111. console.log("单位选项获取成功", row.unitoptions );
  112. unitOptionsCache.value[row.unitType] = row.unitoptions
  113. if (!row.unitType && row.unitoptions .length > 0) {
  114. row.unitDef = row.unitoptions [0].value
  115. }
  116. moreOptionsLoading.value = false;
  117. } catch (err) {
  118. moreOptionsLoading.value = false;
  119. ElMessage.error(err.returnMsg || t("error.fetchFailed"))
  120. row.unitoptions = []
  121. unitOptionsCache.value[row.unitType] = []
  122. } finally {
  123. moreOptionsLoading.value = false;
  124. }
  125. }
  126. const handleUnitChange = (row, newUnit) => {
  127. console.log("handleUnitChange called with row:", row, "and newUnit:", newUnit);
  128. if (!row || !row.value || isNaN(row.value)) {
  129. ElMessage.warning('请输入有效数值')
  130. return
  131. }
  132. const unitOptions = row.unitoptions
  133. if (!unitOptions) {
  134. console.error('单位选项未定义', col || row)
  135. ElMessage.error('单位选项未定义')
  136. return
  137. }
  138. const currentUnit = unitOptions.find(opt => opt.value === row.prevUnitDef)
  139. if (!currentUnit) {
  140. console.error('未找到当前单位信息', row.prevUnitDef)
  141. ElMessage.error('未找到当前单位信息')
  142. return
  143. }
  144. const targetUnit = unitOptions.find(opt => opt.value === newUnit)
  145. if (!targetUnit) {
  146. console.error('未找到目标单位信息', newUnit)
  147. ElMessage.error('未找到目标单位信息')
  148. return
  149. }
  150. const currentValue = parseFloat(row.value)
  151. const newValue = ((currentValue - currentUnit.utOffset) / currentUnit.factor) * targetUnit.factor + targetUnit.utOffset
  152. row.value = newValue.toFixed(6) // 提高精度
  153. row.unit = newUnit
  154. if (row) {
  155. row.prevUnitDef = newUnit
  156. tableSLData.value = [...tableSLData.value]
  157. } else {
  158. row.prevUnitDef = newUnit
  159. }
  160. }
  161. const getSLData = async ( pid ) => {
  162. const params = {
  163. transCode: "ES0012",
  164. pid: pid
  165. }
  166. try {
  167. const res = await request(params) // 使用 await 等待请求完成
  168. tableSLData.value = res.rows.map((item) => ({
  169. ...item,
  170. isVisible: true, // 默认所有行都可见
  171. unitDef: item.unit || item.unitDef || ""
  172. }))
  173. console.log("tableSLData.value", tableSLData.value)
  174. // 使用 for...of 循环以便使用 await
  175. for (const item of tableSLData.value) {
  176. if (!item.pcaId) continue
  177. // 并行处理 value 和 unit 的初始化
  178. const promises = []
  179. if (item.valueType === 1) {
  180. promises.push(getlistopt(item, "value"))
  181. }
  182. if (item.unitType !== "无") {
  183. // promises.push(getlistopt(item, "unit"))
  184. } else {
  185. item.unit = "无"
  186. }
  187. // 等待当前 item 的所有初始化完成
  188. await Promise.all(promises)
  189. }
  190. console.log("所有数据初始化完成")
  191. } catch (err) {
  192. console.error("初始化失败:", err)
  193. ElMessage.error("初始化失败")
  194. }
  195. }
  196. const saveSLTabelDialog = () => {
  197. console.log("tableSLData:", tableSLData.value)
  198. runtype.value = tableSLData.value.find((item)=>item.code === "SimulationType")?.value || '';
  199. emit('selectRunType',runtype.value);
  200. // dataType 为 -1 的数据
  201. const validItems = tableSLData.value.filter((item) => item.dataType === -1)
  202. const pcavals = validItems
  203. .map((item) => {
  204. const pcaId = item.pcaId ?? ""
  205. const value = item.value ?? ""
  206. const unit = item.unitDef != '' ? item.unitDef : "无"
  207. return `${pcaId},${value},${unit}`
  208. })
  209. .join(";")
  210. const params = {
  211. transCode: "ES0008",
  212. pcavals: pcavals
  213. }
  214. request(params)
  215. .then((res) => {
  216. ElMessage.success("保存成功")
  217. // 执行保存逻辑
  218. closeDialog()
  219. })
  220. .catch((err) => {
  221. console.error("err", err)
  222. ElMessage.error("保存失败")
  223. })
  224. }
  225. const getlistopt = async (item, gettype) => {
  226. let params = {}
  227. if (gettype === "value") {
  228. params = {
  229. transCode: "BES001",
  230. type: item.valueDef
  231. }
  232. } else {
  233. params = {
  234. transCode: "BES001",
  235. type: item.unitType
  236. }
  237. }
  238. try {
  239. const res = await request(params)
  240. // console.log("选项获取成功", res)
  241. if (gettype === "value") {
  242. if (
  243. item.value === undefined ||
  244. item.value === null ||
  245. item.value === ""
  246. ) {
  247. item.value = res.rows?.[0]?.val ?? ""
  248. }
  249. item.options = res.rows || []
  250. } else if (gettype === "unit") {
  251. if (item.unit === undefined || item.unit === null || item.unit === "") {
  252. item.unit = res.rows?.[0]?.val ?? ""
  253. }
  254. item.unitoptions = res.rows || []
  255. }
  256. } catch (err) {
  257. console.error("err", err)
  258. ElMessage.error("选项初始化失败")
  259. }
  260. }
  261. // 暴露给父组件
  262. defineExpose({ openDialog, closeDialog })
  263. </script>