DataQualityModal.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="registerModal"
  5. @ok="handleSubmit"
  6. :title="getTitle"
  7. width="900px"
  8. :min-height="600"
  9. :max-height="1000"
  10. scrollable
  11. centered
  12. destroyOnClose
  13. :bodyStyle="{ padding: '20px' }"
  14. >
  15. <a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
  16. <!-- 查看模式 -->
  17. <div class="que-container" v-if="mode === 'view'">
  18. <div class="que-status">
  19. <div>矿井状态:</div>
  20. <div>
  21. <span :class="getStatusClass(currentRecord?.mineLinkStatus)" class="status-dot">
  22. {{ getStatusText(currentRecord?.mineLinkStatus) }}
  23. </span>
  24. </div>
  25. </div>
  26. <div class="que-item" v-for="(item, index) in queList" :key="index">
  27. <div class="que-details">
  28. <div class="que-field">
  29. <span class="que-value que-goafName">{{ item.goafName || '-' }}</span>
  30. </div>
  31. <div class="que-field time-field">
  32. <span class="que-label">时间:</span>
  33. <span class="que-value">{{ formatDate(item.startTime) || '-' }}</span>
  34. <span class="time-separator">-----</span>
  35. <span class="que-value">{{ formatDate(item.endTime) || '-' }}</span>
  36. </div>
  37. <div class="que-field">
  38. <span class="que-label">问题描述:</span>
  39. <span class="que-value">{{ item.queCon || '-' }}</span>
  40. </div>
  41. <!-- <div class="que-field">
  42. <span class="que-label">参数:</span>
  43. <span class="que-value">{{ (item.param || '-').replace(/,/g, ' ') }}</span>
  44. </div> -->
  45. </div>
  46. </div>
  47. </div>
  48. <!-- 编辑/新增模式 -->
  49. <div v-else class="edit-container">
  50. <!-- 动态渲染topFormSchema字段(编辑/新增模式) -->
  51. <div class="mine-base-info" v-if="mode === 'add'">
  52. <a-form-item v-for="schema in topFormSchema" :key="schema.field" :name="schema.field" :label="schema.label" :rules="schema.rules">
  53. <component
  54. :is="getComponent(schema.component)"
  55. v-model:value="formData[schema.field]"
  56. v-bind="schema.componentProps"
  57. :placeholder="`请输入${schema.label}`"
  58. style="width: 100%"
  59. />
  60. </a-form-item>
  61. </div>
  62. <!-- 问题项编辑区 -->
  63. <div class="que-item" v-for="(item, index) in queList" :key="item.orderNum || index">
  64. <div class="que-index">问题 {{ index + 1 }}</div>
  65. <div class="edit-fields">
  66. <a-form-item
  67. v-for="schema in formSchema"
  68. :key="`${schema.field}-${index}`"
  69. :name="[`queList`, index, schema.field]"
  70. :label="schema.label"
  71. :rules="schema.rules"
  72. >
  73. <component
  74. :is="getComponent(schema.component)"
  75. v-model:value="item[schema.field]"
  76. v-bind="schema.componentProps"
  77. :placeholder="`请输入${schema.label}`"
  78. style="width: 100%"
  79. />
  80. </a-form-item>
  81. <div class="form-actions">
  82. <a-button type="text" danger @click="removeItem(index)" :disabled="queList.length <= 1"> 删除 </a-button>
  83. </div>
  84. </div>
  85. </div>
  86. <a-button type="dashed" class="add-btn" @click="addNewItem"> <plus-outlined /> 新增问题 </a-button>
  87. </div>
  88. </a-form>
  89. </BasicModal>
  90. </template>
  91. <script setup lang="ts">
  92. import { ref, computed, reactive, watch } from 'vue';
  93. import { BasicModal, useModalInner } from '/@/components/Modal';
  94. import { formSchema, topFormSchema } from '../dataQuality.data';
  95. import { Select, Input, DatePicker, message } from 'ant-design-vue';
  96. import { PlusOutlined } from '@ant-design/icons-vue';
  97. import dayjs, { Dayjs } from 'dayjs';
  98. import MineCascader from '/@/components/Form/src/jeecg/components/MineCascader/MineCascader.vue';
  99. import type { FormInstance, RuleObject } from 'ant-design-vue/es/form';
  100. // 组件映射表
  101. const componentMap = {
  102. Input,
  103. DatePicker,
  104. Select,
  105. InputTextArea: Input.TextArea,
  106. MineCascader,
  107. };
  108. // 定义事件发射
  109. const emit = defineEmits(['success']);
  110. // 模态框模式:查看/编辑/新增
  111. const mode = ref<'view' | 'edit' | 'add'>('view');
  112. // 当前记录数据
  113. const currentRecord = ref<any>({
  114. mineCode: '',
  115. mineLinkStatus: '0',
  116. queJson: [],
  117. isOk: false,
  118. updateTime: '',
  119. createTime: '',
  120. });
  121. // 问题列表
  122. const queList = ref<any[]>([]);
  123. // 表单实例(用于校验)
  124. const formRef = ref<FormInstance>();
  125. // 表单数据聚合(适配Form组件的model)
  126. const formData = reactive({
  127. mineCode: '',
  128. queList: queList.value,
  129. });
  130. // 合并表单规则(从schema中提取)
  131. const formRules = reactive({
  132. mineCode: topFormSchema.find((item) => item.field === 'mineCode')?.rules || [],
  133. queList: {
  134. type: 'array',
  135. required: true,
  136. validator: (rule: RuleObject, value: any[], callback: Function) => {
  137. // 校验每个问题项
  138. if (value.length === 0) {
  139. callback(new Error('至少需要填写一条问题信息'));
  140. return;
  141. }
  142. // 逐个校验问题项的必填字段
  143. for (let i = 0; i < value.length; i++) {
  144. const item = value[i];
  145. // 校验工作面名称
  146. if (!item.goafName) {
  147. callback(new Error(`第${i + 1}条问题:工作面名称不能为空`));
  148. return;
  149. }
  150. // 校验问题描述
  151. if (!item.queCon) {
  152. callback(new Error(`第${i + 1}条问题:问题描述不能为空`));
  153. return;
  154. }
  155. // 校验开始时间
  156. if (!item.startTime) {
  157. callback(new Error(`第${i + 1}条问题:开始时间不能为空`));
  158. return;
  159. }
  160. // 校验结束时间
  161. if (!item.endTime) {
  162. callback(new Error(`第${i + 1}条问题:结束时间不能为空`));
  163. return;
  164. }
  165. // 校验结束时间不能早于开始时间
  166. if (dayjs(item.endTime).isBefore(dayjs(item.startTime))) {
  167. callback(new Error(`第${i + 1}条问题:结束时间不能早于开始时间`));
  168. return;
  169. }
  170. // 校验参数
  171. if (!item.param) {
  172. callback(new Error(`第${i + 1}条问题:参数不能为空`));
  173. return;
  174. }
  175. }
  176. callback();
  177. },
  178. },
  179. });
  180. // 监听queList变化,同步到formData
  181. watch(
  182. queList,
  183. (newVal) => {
  184. formData.queList = JSON.parse(JSON.stringify(newVal));
  185. },
  186. { deep: true, immediate: true }
  187. );
  188. // 监听currentRecord变化,同步mineCode到formData
  189. watch(
  190. () => currentRecord.value.mineCode,
  191. (newVal) => {
  192. formData.mineCode = newVal;
  193. },
  194. { immediate: true }
  195. );
  196. // 根据组件名获取对应组件
  197. const getComponent = (componentName: string) => {
  198. return componentMap[componentName as keyof typeof componentMap];
  199. };
  200. // 日期格式化方法
  201. const formatDate = (date: string | Dayjs | null) => {
  202. if (!date) return '';
  203. return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
  204. };
  205. // 根据状态值获取显示文本(在线/离线)
  206. const getStatusText = (status: string | number) => {
  207. return status === '1' || status === 1 ? '在线' : '离线';
  208. };
  209. // 根据状态值获取样式类
  210. const getStatusClass = (status: string | number) => {
  211. return status === '1' || status === 1 ? 'status-online' : 'status-offline';
  212. };
  213. // 注册模态框并初始化数据
  214. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  215. setModalProps({ confirmLoading: false });
  216. // 接收模式参数
  217. mode.value = data?.mode || 'view';
  218. // 初始化当前记录
  219. currentRecord.value = data?.record
  220. ? JSON.parse(JSON.stringify(data.record))
  221. : {
  222. mineCode: '',
  223. mineLinkStatus: '0',
  224. queJson: [],
  225. isOk: false,
  226. updateTime: '',
  227. createTime: '',
  228. };
  229. // 同步mineCode到formData
  230. formData.mineCode = currentRecord.value.mineCode;
  231. // 初始化问题列表
  232. if (mode.value === 'add') {
  233. // 新增模式:初始化一条空数据
  234. queList.value = [
  235. {
  236. orderNum: '1',
  237. goafName: '',
  238. queCon: '',
  239. startTime: null,
  240. endTime: null,
  241. param: '',
  242. },
  243. ];
  244. formData.queList = JSON.parse(JSON.stringify(queList.value));
  245. if (formRef.value) {
  246. formRef.value.resetFields();
  247. }
  248. } else {
  249. // 编辑/查看模式:解析已有问题数据
  250. try {
  251. const rawData = currentRecord.value?.queJson
  252. ? typeof currentRecord.value.queJson === 'string'
  253. ? currentRecord.value.queJson.trim()
  254. ? JSON.parse(currentRecord.value.queJson)
  255. : [] // 兼容空字符串
  256. : currentRecord.value.queJson
  257. : [];
  258. // 兼容日期格式,避免转换失败
  259. queList.value = rawData.map((item: any) => ({
  260. ...item,
  261. startTime: item.startTime ? dayjs(item.startTime, ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD']) : null,
  262. endTime: item.endTime ? dayjs(item.endTime, ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD']) : null,
  263. }));
  264. // 深拷贝同步到formData,切断引用
  265. formData.queList = JSON.parse(JSON.stringify(queList.value));
  266. } catch (error) {
  267. console.error('解析问题数据失败:', error);
  268. message.warning('问题数据格式异常,将显示原始数据');
  269. // 保留原始queJson,而非重置为空数据
  270. queList.value = currentRecord.value?.queJson ? (typeof currentRecord.value.queJson === 'string' ? [] : currentRecord.value.queJson) : [];
  271. formData.queList = JSON.parse(JSON.stringify(queList.value));
  272. }
  273. }
  274. // 重置表单校验状态
  275. if (formRef.value) {
  276. formRef.value.resetFields();
  277. }
  278. });
  279. // 模态框标题计算属性
  280. const getTitle = computed(() => {
  281. const titleMap = {
  282. view: '质量问题详情',
  283. edit: '编辑质量问题',
  284. add: '新增质量问题',
  285. };
  286. return titleMap[mode.value];
  287. });
  288. // 新增问题项
  289. const addNewItem = () => {
  290. const newItem = {
  291. orderNum: (queList.value.length + 1).toString(),
  292. goafName: '',
  293. queCon: '',
  294. startTime: null,
  295. endTime: null,
  296. param: '',
  297. };
  298. queList.value.push(newItem);
  299. };
  300. // 删除问题项
  301. const removeItem = (index: number) => {
  302. queList.value.splice(index, 1);
  303. queList.value.forEach((item, i) => {
  304. item.orderNum = (i + 1).toString();
  305. });
  306. };
  307. // 提交表单处理
  308. async function handleSubmit() {
  309. try {
  310. // 查看模式直接提交
  311. if (mode.value === 'view') {
  312. closeModal();
  313. return;
  314. }
  315. // 1. 表单校验
  316. if (!formRef.value) return;
  317. const validateResult = await formRef.value.validate();
  318. if (!validateResult) return;
  319. setModalProps({ confirmLoading: true });
  320. // 2. 处理问题列表数据(空日期字段置空,避免空字符串)
  321. const submitQueList = queList.value.map((item) => ({
  322. ...item,
  323. startTime: item.startTime ? item.startTime.format('YYYY-MM-DD HH:mm:ss') : null,
  324. endTime: item.endTime ? item.endTime.format('YYYY-MM-DD HH:mm:ss') : null,
  325. }));
  326. // 3. 构造完整提交数据
  327. const now = dayjs().format('YYYY-MM-DD HH:mm:ss');
  328. const result = {
  329. ...currentRecord.value,
  330. mineCode: formData.mineCode,
  331. queJson: JSON.stringify(submitQueList),
  332. createTime: mode.value === 'add' ? now : currentRecord.value.createTime || now,
  333. updateTime: now,
  334. isOk: mode.value === 'add' ? false : currentRecord.value.isOk,
  335. };
  336. console.log('最终提交数据:', result);
  337. emit('success', result);
  338. closeModal();
  339. } catch (error: any) {
  340. console.error('提交失败:', error);
  341. // 显示校验错误提示
  342. message.error(error.message || '表单校验失败,请检查必填项');
  343. } finally {
  344. setModalProps({ confirmLoading: false });
  345. }
  346. }
  347. </script>
  348. <style scoped>
  349. .que-container {
  350. display: flex;
  351. flex-direction: column;
  352. gap: 16px;
  353. }
  354. .que-status {
  355. display: flex;
  356. width: 100%;
  357. background-color: #f8f9fc;
  358. padding: 8px 16px;
  359. align-items: center;
  360. border: 1px solid #cad2e0;
  361. border-radius: 5px;
  362. }
  363. .mine-info {
  364. padding: 12px 16px;
  365. border: 1px solid #cad2e0;
  366. border-radius: 5px;
  367. background-color: #f8f9fc;
  368. }
  369. .mine-field {
  370. display: flex;
  371. align-items: center;
  372. gap: 8px;
  373. margin-bottom: 8px;
  374. }
  375. .status-dot {
  376. position: relative;
  377. padding-left: 12px;
  378. font-weight: 500;
  379. }
  380. .status-dot::before {
  381. content: '';
  382. position: absolute;
  383. left: 0;
  384. top: 50%;
  385. transform: translateY(-50%);
  386. width: 6px;
  387. height: 6px;
  388. border-radius: 50%;
  389. background-color: inherit;
  390. }
  391. .status-online {
  392. color: #10952c;
  393. font-weight: 500;
  394. }
  395. .status-offline {
  396. color: #f5222d;
  397. font-weight: 500;
  398. }
  399. .status-online.status-dot::before {
  400. background-color: #10952c;
  401. }
  402. .status-offline.status-dot::before {
  403. background-color: #f5222d;
  404. }
  405. .que-item {
  406. padding: 8px 16px;
  407. border: 1px solid #cad2e0;
  408. border-radius: 8px;
  409. background-color: #f8f9fc;
  410. }
  411. .que-index {
  412. font-size: 16px;
  413. font-weight: 600;
  414. color: #1890ff;
  415. margin-bottom: 12px;
  416. padding-bottom: 8px;
  417. border-bottom: 1px dashed #e8e8e8;
  418. }
  419. .que-details {
  420. width: 100%;
  421. }
  422. .que-field {
  423. display: flex;
  424. align-items: center;
  425. gap: 8px;
  426. min-width: 200px;
  427. margin-bottom: 10px;
  428. }
  429. .que-label {
  430. font-size: 16px;
  431. color: #868789;
  432. white-space: nowrap;
  433. }
  434. .que-value {
  435. font-size: 16px;
  436. color: #838486;
  437. word-break: break-all;
  438. }
  439. .que-goafName {
  440. color: #4c4c4e;
  441. font-size: 20px;
  442. }
  443. .time-field {
  444. flex: 1;
  445. min-width: 420px;
  446. }
  447. .time-separator {
  448. color: #999;
  449. margin: 0 8px;
  450. }
  451. .edit-container {
  452. display: flex;
  453. flex-direction: column;
  454. gap: 16px;
  455. }
  456. .mine-base-info {
  457. padding: 12px 16px;
  458. border: 1px solid #cad2e0;
  459. border-radius: 5px;
  460. background-color: #f8f9fc;
  461. }
  462. .edit-fields {
  463. width: 100%;
  464. }
  465. .form-item {
  466. display: flex;
  467. margin-bottom: 10px;
  468. align-items: center;
  469. }
  470. .form-label {
  471. width: 20%;
  472. color: #666;
  473. font-size: 14px;
  474. }
  475. .add-btn {
  476. margin-top: 8px;
  477. width: 100%;
  478. }
  479. .form-actions {
  480. display: flex;
  481. justify-content: end;
  482. margin-bottom: 8px;
  483. }
  484. </style>