BaseModal1Leather.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 || ''" disabled style="width: calc(100% - 65px); border: 1px solid #1f7699" />
  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 || ''" disabled style="width: calc(100% - 65px); border: 1px solid #1f7699" />
  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();
  76. const formSchemas1 = deviceControlColumns();
  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.deviceType) {
  152. message.info('请先选择设备!');
  153. return;
  154. }
  155. console.log(model, '=====model');
  156. // 缓存当前编辑行
  157. tempCurrentModel.value = model;
  158. await getDevicePointList(model.deviceType);
  159. openPointModal(true, { deviceType: model.deviceType });
  160. }
  161. // 选中点位回填到当前行
  162. function setPoint(value: any[]) {
  163. if (!value || !value.length || !tempCurrentModel.value) return;
  164. const data = value[0];
  165. const model = tempCurrentModel.value;
  166. // 赋值当前行
  167. model.monitorId = data.id;
  168. model.pointOptions = [{ value: data.id, label: data.valuename }];
  169. }
  170. const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  171. schemas: props.formSchemas,
  172. showActionButtonGroup: false,
  173. });
  174. const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  175. isUpdate.value = unref(data.isUpdate);
  176. await resetFields();
  177. title.value = data.isUpdate ? '编辑' : '新增';
  178. warnFormRefs.value = [];
  179. controlFormRefs.value = [];
  180. tempCurrentModel.value = null;
  181. if (data.isUpdate && data.record) {
  182. await setFieldsValue(data.record);
  183. const allAlarms = data.record.alarmList || [];
  184. const warnings = allAlarms.filter((item: any) => String(item.monitorType) === '2');
  185. const controls = allAlarms.filter((item: any) => String(item.monitorType) === '1');
  186. warnList.value = warnings.length ? warnings : [getEmptyWarnRow()];
  187. controlList.value = controls.length ? controls : [getEmptyControlRow()];
  188. } else {
  189. warnList.value = [getEmptyWarnRow()];
  190. controlList.value = [getEmptyControlRow()];
  191. }
  192. if (data.type === 'devicePoint') {
  193. title.value = unref(data.title);
  194. if (data.record) {
  195. await getDevicePointList(data.record['deviceType']);
  196. devicePointList.value.forEach((item) => {
  197. if (item.id == data.record.monitorId) {
  198. const model = data.record;
  199. model.monitorId = item.id;
  200. model.pointOptions = [{ value: item.id, label: item.valuename }];
  201. }
  202. });
  203. await setFieldsValue({ ...data.record });
  204. }
  205. }
  206. });
  207. // 提交
  208. // 提交
  209. async function onSubmit() {
  210. setModalProps({ confirmLoading: true });
  211. try {
  212. const baseForm = await getFieldsValue();
  213. // 获取预警数据
  214. const warnValues = [];
  215. for (const form of warnFormRefs.value) {
  216. if (form) {
  217. const val = await form.getFieldsValue();
  218. delete val.pointOptions;
  219. if (val.strtype) {
  220. val.deviceType = val.strtype;
  221. }
  222. delete val.strtype;
  223. const deviceId = Array.isArray(val.deviceId) ? val.deviceId[0] : val.deviceId || '';
  224. if (!deviceId) continue;
  225. warnValues.push({
  226. ...val,
  227. monitorType: 2,
  228. deviceId: deviceId,
  229. });
  230. }
  231. }
  232. // 获取控制数据
  233. const controlValues = [];
  234. for (const form of controlFormRefs.value) {
  235. if (form) {
  236. const val = await form.getFieldsValue();
  237. delete val.pointOptions;
  238. if (val.strtype) {
  239. val.deviceType = val.strtype;
  240. }
  241. delete val.strtype;
  242. const deviceId = Array.isArray(val.deviceId) ? val.deviceId[0] : val.deviceId || '';
  243. if (!deviceId) continue;
  244. controlValues.push({
  245. ...val,
  246. monitorType: 1,
  247. deviceId: deviceId,
  248. });
  249. }
  250. }
  251. const submitData = {
  252. ...baseForm,
  253. alarmList: [...warnValues, ...controlValues],
  254. };
  255. if (!isUpdate.value) {
  256. emit('add', submitData);
  257. } else {
  258. emit('update', submitData);
  259. }
  260. closeModal();
  261. } catch (error) {
  262. console.error('提交异常:', error);
  263. message.error('提交失败,请检查表单');
  264. } finally {
  265. setModalProps({ confirmLoading: false });
  266. }
  267. }
  268. </script>
  269. <style lang="less" scoped>
  270. @import '/@/design/theme.less';
  271. .setting-form {
  272. padding: 0 20px;
  273. }
  274. .setting-form__wrapper {
  275. width: 49%;
  276. color: #fff;
  277. border-top: 3px solid @vent-gas-primary-text;
  278. background-color: @vent-gas-primary-trasparent-bg;
  279. padding: 10px 10px 0 10px;
  280. margin-bottom: 10px;
  281. }
  282. .setting-form__button {
  283. display: block;
  284. width: 40%;
  285. margin: 10px auto;
  286. color: @vent-gas-primary-text;
  287. border-color: @vent-gas-primary-text;
  288. background-color: transparent !important;
  289. }
  290. .double-form__layout {
  291. display: flex;
  292. gap: 16px;
  293. margin-top: 16px;
  294. }
  295. .panel-title {
  296. margin: 0 0 10px;
  297. font-size: 14px;
  298. font-weight: bold;
  299. color: #fafafa;
  300. }
  301. .form-row {
  302. border: 1px solid #2e7695;
  303. display: flex;
  304. align-items: center;
  305. gap: 10px;
  306. padding: 10px;
  307. margin-top: 10px;
  308. }
  309. </style>