BaseModal1Leather.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <BasicModal @register="register" :title="title" :width="1600" :min-height="500" v-bind="$attrs" @ok="onSubmit">
  3. <BasicForm @register="registerForm" />
  4. <div class="double-form__layout">
  5. <!-- 配置预警设备 -->
  6. <div class="setting-form__wrapper">
  7. <h3 class="panel-title">配置预警设备</h3>
  8. <div class="form-row" v-for="(item, idx) in warnList" :key="`warn-${idx}`">
  9. <BasicForm
  10. :ref="(el) => setWarnFormRef(el, idx)"
  11. :model="item"
  12. :schemas="formSchemas"
  13. layout="horizontal"
  14. label-width="150px"
  15. :show-action-button-group="false"
  16. >
  17. <template #monitor="{ model }">
  18. <div class="vent-flex-row-between">
  19. <a-input :value="model.pointOptions?.[0]?.label || ''" style="width: calc(100% - 65px)" />
  20. <a-button type="primary" @click="selectPoint(model)" style="position: absolute; right: 0; top: 1px">选择</a-button>
  21. </div>
  22. </template>
  23. </BasicForm>
  24. <a-button type="link" normal @click="delWarnRow(idx)" :disabled="warnList.length <= 1">删除</a-button>
  25. </div>
  26. <a-button type="dashed" block @click="addWarnRow" class="setting-form__button">新增预警配置</a-button>
  27. </div>
  28. <!-- 配置控制设备 -->
  29. <div class="setting-form__wrapper">
  30. <h3 class="panel-title">配置控制设备</h3>
  31. <div class="form-row" v-for="(item, idx) in controlList" :key="`control-${idx}`">
  32. <BasicForm
  33. :ref="(el) => setControlFormRef(el, idx)"
  34. :model="item"
  35. :schemas="formSchemas1"
  36. layout="horizontal"
  37. label-width="120px"
  38. :show-action-button-group="false"
  39. >
  40. <template #monitor="{ model }">
  41. <div class="vent-flex-row-between">
  42. <a-input :value="model.pointOptions?.[0]?.label || ''" style="width: calc(100% - 65px)" />
  43. <a-button type="primary" @click="selectPoint(model)" style="position: absolute; right: 0; top: 1px">选择</a-button>
  44. </div>
  45. </template>
  46. </BasicForm>
  47. <a-button type="link" normal @click="delControlRow(idx, item)" :disabled="controlList.length <= 1">删除</a-button>
  48. </div>
  49. <a-button type="dashed" block @click="addControlRow" class="setting-form__button">新增控制配置</a-button>
  50. </div>
  51. </div>
  52. </BasicModal>
  53. <DevicePointTable @register="registerPointModal" :data-source="devicePointList" @reload="setPoint" />
  54. </template>
  55. <script lang="ts" setup>
  56. import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  57. import { ref, unref, nextTick } from 'vue';
  58. import { BasicForm, useForm } from '/@/components/Form';
  59. import { message } from 'ant-design-vue';
  60. import { deviceFormColumns, deviceControlColumns } from './warning.data';
  61. import { workFaceWarningDelete, workFacePointList } from './warning.api';
  62. import DevicePointTable from './DevicePointTable.vue';
  63. import type { FormSchema } from '/@/components/Form/src/types/form';
  64. import type { FormActionType } from '/@/components/Form';
  65. const props = defineProps({
  66. formSchemas: {
  67. type: Array as PropType<FormSchema[]>,
  68. default: () => [],
  69. },
  70. deviceId: String,
  71. monitorType: String,
  72. });
  73. const emit = defineEmits(['add', 'update']);
  74. const [registerPointModal, { openModal: openPointModal }] = useModal();
  75. const formSchemas = deviceFormColumns({ id: String(props.deviceId) });
  76. const formSchemas1 = deviceControlColumns({ id: String(props.deviceId) });
  77. const title = ref('');
  78. const isUpdate = ref(false);
  79. // 表单Ref数组
  80. const warnFormRefs = ref<FormActionType[]>([]);
  81. const controlFormRefs = ref<FormActionType[]>([]);
  82. // 点位弹窗临时保存当前行
  83. const tempCurrentModel = ref<any>(null);
  84. const devicePointList = ref<any[]>([]);
  85. // 预警列表
  86. const warnList = ref([getEmptyWarnRow()]);
  87. function getEmptyWarnRow() {
  88. return {
  89. deviceType: '',
  90. monitorId: '',
  91. pointOptions: [],
  92. fmax: null,
  93. fmin: null,
  94. cxTime: null,
  95. dataTrend: '',
  96. trendCxTime: null,
  97. monitorType: '2',
  98. };
  99. }
  100. // 收集预警表单Ref
  101. function setWarnFormRef(el: any, index: number) {
  102. if (el) warnFormRefs.value[index] = el;
  103. }
  104. function addWarnRow() {
  105. warnList.value.push(getEmptyWarnRow());
  106. warnFormRefs.value.push(null);
  107. }
  108. function delWarnRow(idx: number) {
  109. if (warnList.value.length <= 1) return message.warning('至少保留一行');
  110. warnList.value.splice(idx, 1);
  111. warnFormRefs.value.splice(idx, 1);
  112. }
  113. // 控制列表
  114. const controlList = ref([getEmptyControlRow()]);
  115. function getEmptyControlRow() {
  116. return {
  117. deviceType: '',
  118. monitorId: '',
  119. pointOptions: [],
  120. value: '',
  121. orderNum: null,
  122. remark: '',
  123. monitorType: '1',
  124. };
  125. }
  126. // 收集控制表单Ref
  127. function setControlFormRef(el: any, index: number) {
  128. if (el) controlFormRefs.value[index] = el;
  129. }
  130. function addControlRow() {
  131. controlList.value.push(getEmptyControlRow());
  132. controlFormRefs.value.push(null);
  133. }
  134. async function delControlRow(idx: number, record: any) {
  135. if (controlList.value.length <= 1) return message.warning('至少保留一行');
  136. controlList.value.splice(idx, 1);
  137. controlFormRefs.value.splice(idx, 1);
  138. await workFaceWarningDelete({ id: record.id });
  139. }
  140. // 获取点位列表
  141. async function getDevicePointList(strtype: string) {
  142. try {
  143. const result = await workFacePointList({ deviceType: strtype, valueTypes: props.monitorType });
  144. devicePointList.value = result;
  145. } catch (error) {
  146. devicePointList.value = [];
  147. }
  148. }
  149. // 打开点位选择弹窗
  150. async function selectPoint(model: any) {
  151. if (!model.strtype) {
  152. message.info('请先选择设备!');
  153. return;
  154. }
  155. // 缓存当前编辑行
  156. tempCurrentModel.value = model;
  157. await getDevicePointList(model.strtype);
  158. openPointModal(true, { deviceType: model.strtype });
  159. }
  160. // 选中点位回填到当前行
  161. function setPoint(value: any[]) {
  162. if (!value || !value.length || !tempCurrentModel.value) return;
  163. const data = value[0];
  164. const model = tempCurrentModel.value;
  165. // 赋值当前行
  166. model.monitorId = data.id;
  167. model.pointOptions = [{ value: data.id, label: data.valuename }];
  168. }
  169. const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  170. schemas: props.formSchemas,
  171. showActionButtonGroup: false,
  172. });
  173. const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  174. isUpdate.value = unref(data.isUpdate);
  175. await resetFields();
  176. title.value = data.isUpdate ? '编辑' : '新增';
  177. warnFormRefs.value = [];
  178. controlFormRefs.value = [];
  179. tempCurrentModel.value = null;
  180. if (data.isUpdate && data.record) {
  181. await setFieldsValue(data.record);
  182. const allAlarms = data.record.alarmList || [];
  183. const warnings = allAlarms.filter((item: any) => String(item.monitorType) === '2');
  184. const controls = allAlarms.filter((item: any) => String(item.monitorType) === '1');
  185. warnList.value = warnings.length ? warnings : [getEmptyWarnRow()];
  186. controlList.value = controls.length ? controls : [getEmptyControlRow()];
  187. } else {
  188. warnList.value = [getEmptyWarnRow()];
  189. controlList.value = [getEmptyControlRow()];
  190. }
  191. if (data.type === 'devicePoint') {
  192. title.value = unref(data.title);
  193. if (data.record) {
  194. await getDevicePointList(data.record['deviceType']);
  195. devicePointList.value.forEach((item) => {
  196. if (item.id == data.record.monitorId) {
  197. const model = data.record;
  198. model.monitorId = item.id;
  199. model.pointOptions = [{ value: item.id, label: item.valuename }];
  200. }
  201. });
  202. await setFieldsValue({ ...data.record });
  203. }
  204. }
  205. });
  206. // 提交
  207. // 提交
  208. async function onSubmit() {
  209. setModalProps({ confirmLoading: true });
  210. try {
  211. const baseForm = await getFieldsValue();
  212. // 获取预警数据
  213. const warnValues = [];
  214. for (const form of warnFormRefs.value) {
  215. if (form) {
  216. const val = await form.getFieldsValue();
  217. delete val.pointOptions;
  218. if (val.strtype) {
  219. val.deviceType = val.strtype;
  220. }
  221. delete val.strtype;
  222. const deviceId = Array.isArray(val.deviceId) ? val.deviceId[0] : val.deviceId || '';
  223. if (!deviceId) continue;
  224. warnValues.push({
  225. ...val,
  226. monitorType: 2,
  227. deviceId: deviceId,
  228. });
  229. }
  230. }
  231. // 获取控制数据
  232. const controlValues = [];
  233. for (const form of controlFormRefs.value) {
  234. if (form) {
  235. const val = await form.getFieldsValue();
  236. delete val.pointOptions;
  237. if (val.strtype) {
  238. val.deviceType = val.strtype;
  239. }
  240. delete val.strtype;
  241. const deviceId = Array.isArray(val.deviceId) ? val.deviceId[0] : val.deviceId || '';
  242. if (!deviceId) continue;
  243. controlValues.push({
  244. ...val,
  245. monitorType: 1,
  246. deviceId: deviceId,
  247. });
  248. }
  249. }
  250. const submitData = {
  251. ...baseForm,
  252. alarmList: [...warnValues, ...controlValues],
  253. };
  254. if (!isUpdate.value) {
  255. emit('add', submitData);
  256. } else {
  257. emit('update', submitData);
  258. }
  259. closeModal();
  260. } catch (error) {
  261. console.error('提交异常:', error);
  262. message.error('提交失败,请检查表单');
  263. } finally {
  264. setModalProps({ confirmLoading: false });
  265. }
  266. }
  267. </script>
  268. <style lang="less" scoped>
  269. @import '/@/design/theme.less';
  270. .setting-form {
  271. padding: 0 20px;
  272. }
  273. .setting-form__wrapper {
  274. width: 49%;
  275. color: #fff;
  276. border-top: 3px solid @vent-gas-primary-text;
  277. background-color: @vent-gas-primary-trasparent-bg;
  278. padding: 10px 10px 0 10px;
  279. margin-bottom: 10px;
  280. }
  281. .setting-form__button {
  282. display: block;
  283. width: 40%;
  284. margin: 10px auto;
  285. color: @vent-gas-primary-text;
  286. border-color: @vent-gas-primary-text;
  287. background-color: transparent !important;
  288. }
  289. .double-form__layout {
  290. display: flex;
  291. gap: 16px;
  292. margin-top: 16px;
  293. }
  294. .panel-title {
  295. margin: 0 0 10px;
  296. font-size: 14px;
  297. font-weight: bold;
  298. color: #fafafa;
  299. }
  300. .form-row {
  301. border: 1px solid #2e7695;
  302. display: flex;
  303. align-items: center;
  304. gap: 10px;
  305. padding: 10px;
  306. margin-top: 10px;
  307. }
  308. </style>