IotInspectRoute.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <ContentWrap v-loading="formLoading">
  3. <ContentWrap>
  4. <el-form
  5. ref="formRef"
  6. :model="formData"
  7. :rules="formRules"
  8. style="margin-right: 4em; margin-left: 0.5em; margin-top: 1em"
  9. label-width="130px"
  10. >
  11. <div class="base-expandable-content">
  12. <el-row>
  13. <el-col :span="8">
  14. <el-form-item label="路线名称" prop="routeName">
  15. <el-input v-model="formData.routeName" placeholder="路线名称" />
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="8">
  19. <el-form-item label="设备类别" prop="deviceClassify">
  20. <el-tree-select
  21. filterable
  22. clearable
  23. v-model="formData.deviceClassify"
  24. :data="productClassifyList"
  25. :props="defaultProps"
  26. check-strictly
  27. node-key="id"
  28. placeholder="请选择设备类别"
  29. />
  30. </el-form-item>
  31. </el-col>
  32. <el-col :span="8">
  33. <el-form-item label="设备" prop="deviceName">
  34. <el-input
  35. v-model="formData.deviceName"
  36. @clear="handleClear"
  37. placeholder="请选择设备"
  38. @click="openDevice"
  39. clearable
  40. />
  41. </el-form-item>
  42. </el-col>
  43. </el-row>
  44. </div>
  45. </el-form>
  46. </ContentWrap>
  47. <ContentWrap>
  48. <!-- 列表 -->
  49. <ContentWrap>
  50. <ContentWrap>
  51. <el-form
  52. class="-mb-15px"
  53. :model="queryParams"
  54. ref="queryFormRef"
  55. :inline="true"
  56. label-width="68px"
  57. >
  58. <el-form-item>
  59. <el-button @click="openForm" type="primary"
  60. ><Icon icon="ep:plus" class="mr-5px" /> 选择巡检项</el-button
  61. >
  62. </el-form-item>
  63. </el-form>
  64. </ContentWrap>
  65. <draggable
  66. v-model="items"
  67. item-key="id"
  68. tag="div"
  69. class="sortable-container"
  70. handle=".sortable-item"
  71. :animation="150"
  72. @start="dragStart"
  73. @end="dragEnd"
  74. >
  75. <template #item="{ element, index }">
  76. <div class="sortable-item">
  77. <!-- 序号显示 -->
  78. <div class="order-number">{{ index + 1 }}</div>
  79. <!-- 拖动手柄 -->
  80. <span class="drag-handle">≡</span>
  81. <!-- 组件内容 -->
  82. <div class="component-content">
  83. <span style="font-weight: bold">巡检项:</span
  84. ><span style="font-size: 14px">{{ element.item }}</span
  85. >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  86. <span style="font-weight: bold">巡检标准:</span
  87. ><span style="font-size: 14px">{{ element.standard }}</span>
  88. </div>
  89. <div>
  90. <el-button type="warning" @click="deleteDraggable(index)">删除</el-button>
  91. </div>
  92. </div>
  93. </template>
  94. </draggable>
  95. </ContentWrap>
  96. </ContentWrap>
  97. <ContentWrap>
  98. <el-form>
  99. <el-form-item style="float: right">
  100. <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>
  101. <el-button @click="close">取 消</el-button>
  102. </el-form-item>
  103. </el-form>
  104. </ContentWrap>
  105. <InspectItemList
  106. ref="inspectItemFormRef"
  107. @choose="inspectItemChoose"
  108. />
  109. <DeviceList ref="deviceFormRef" @choose="deviceChoose" />
  110. </ContentWrap>
  111. </template>
  112. <script setup lang="ts">
  113. import * as UserApi from '@/api/system/user'
  114. import { useUserStore } from '@/store/modules/user'
  115. import { ref } from 'vue'
  116. import { IotMaintainMaterialVO } from '@/api/pms/maintain/material'
  117. import { useTagsViewStore } from '@/store/modules/tagsView'
  118. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  119. import { defaultProps, handleTree } from '@/utils/tree'
  120. import * as ProductClassifyApi from '@/api/pms/productclassify'
  121. import draggable from 'vuedraggable'
  122. import InspectItemList from '@/views/pms/inspect/route/InspectItemList.vue'
  123. import DeviceList from '@/views/pms/failure/DeviceList.vue'
  124. import { IotInspectRouteApi, IotInspectRouteVO } from '@/api/pms/inspect/route'
  125. /** 维修工单 表单 */
  126. defineOptions({ name: 'RouteAdd' })
  127. const { t } = useI18n() // 国际化
  128. const message = useMessage() // 消息弹窗
  129. const { delView } = useTagsViewStore() // 视图操作
  130. const { currentRoute, push } = useRouter()
  131. const deptUsers = ref<UserApi.UserVO[]>([]) // 用户列表
  132. const dialogTitle = ref('') // 弹窗的标题
  133. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  134. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  135. const deviceLabel = ref('') // 表单的类型:create - 新增;update - 修改
  136. const drawerVisible = ref<boolean>(false)
  137. const showDrawer = ref()
  138. const list = ref<IotMaintainMaterialVO[]>([]) // 列表的数据
  139. const { params, name } = useRoute() // 查询参数
  140. const id = params.id
  141. const productClassifyList = ref<Tree[]>([]) // 树形结构
  142. const formData = ref({
  143. id: undefined,
  144. routeName: undefined,
  145. deviceClassify: undefined,
  146. deviceId: undefined,
  147. deviceClassifyName: undefined,
  148. deviceName: undefined,
  149. itemJson: undefined,
  150. remark: undefined,
  151. deptId: undefined,
  152. deviceCode: undefined
  153. })
  154. const handleClear = () => {
  155. formData.value.deviceId = undefined
  156. formData.value.deviceName = undefined
  157. formData.value.deptId = undefined
  158. formData.value.deviceCode = undefined
  159. }
  160. // 拖动状态管理
  161. const dragStart = () => {
  162. document.body.style.cursor = 'grabbing'
  163. }
  164. const deleteDraggable = async (index) => {
  165. await message.delConfirm()
  166. items.value.splice(index, 1)
  167. }
  168. const dragEnd = (event) => {
  169. document.body.style.cursor = ''
  170. console.log(
  171. '新顺序:',
  172. items.value.map((c) => c.id)
  173. )
  174. console.log('拖拽后的新位置:', event.newIndex + 1)
  175. }
  176. const items = ref([])
  177. const deviceChoose = (row) => {
  178. formData.value.deviceId = row.id
  179. formData.value.deviceName = row.deviceName
  180. formData.value.deptId = row.deptId
  181. formData.value.deviceCode = row.deviceCode
  182. //deviceLabel.value = row.deviceName
  183. }
  184. const formRules = reactive({
  185. routeName: [{ required: true, message: '路线名称不能为空', trigger: 'blur' }],
  186. deviceClassify: [{ required: true, message: '设备类别不能为空', trigger: 'blur' }]
  187. })
  188. const formRef = ref() // 表单 Ref
  189. const inspectItemChoose = (rows) => {
  190. rows.forEach((it) => {
  191. const ifExist = items.value.find((item) => item.id === it.id)
  192. if (!ifExist) {
  193. items.value.push(it)
  194. console.log(JSON.stringify(it))
  195. }
  196. })
  197. console.log(JSON.stringify(items.value))
  198. // items.value = rows
  199. }
  200. const inspectItemFormRef = ref()
  201. const openForm = () => {
  202. if (formData.value.deviceClassify === undefined) {
  203. message.error('请选择设备类别')
  204. return
  205. }
  206. inspectItemFormRef.value.open(formData.value.deviceClassify, formData.value.deviceId)
  207. }
  208. const deviceFormRef = ref()
  209. const openDevice = () => {
  210. deviceFormRef.value.open(formData.value.deviceClassify)
  211. }
  212. const close = () => {
  213. delView(unref(currentRoute))
  214. push({
  215. name: 'IotInspectRoute',
  216. query: {
  217. date: new Date().getTime()
  218. }
  219. })
  220. }
  221. const itemsWithIndex = computed(() => {
  222. return items.value.map((item, index) => ({
  223. itemId: item.id,
  224. item: item.item,
  225. standard: item.standard,
  226. index: index + 1 // 序号从1开始
  227. }))
  228. })
  229. const itemsWithIndexUpdate = computed(() => {
  230. return items.value.map((item, index) => ({
  231. itemId: item.itemId,
  232. item: item.item,
  233. standard: item.standard,
  234. index: index + 1 // 序号从1开始
  235. }))
  236. })
  237. const { wsCache } = useCache()
  238. /** 提交表单 */
  239. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  240. const submitForm = async () => {
  241. // 校验表单
  242. await formRef.value.validate()
  243. // 提交请求
  244. formLoading.value = true
  245. try {
  246. // const user = wsCache.get(CACHE_KEY.USER)
  247. // formData.value.deptId = user.user.deptId
  248. const data = formData.value as unknown as IotInspectRouteVO
  249. if (formType.value === 'create') {
  250. const newitems = itemsWithIndex
  251. formData.value.itemJson = JSON.stringify(newitems.value)
  252. await IotInspectRouteApi.createIotInspectRoute(data)
  253. message.success(t('common.createSuccess'))
  254. close()
  255. } else {
  256. const newitems = itemsWithIndexUpdate
  257. formData.value.itemJson = JSON.stringify(newitems.value)
  258. await IotInspectRouteApi.updateIotInspectRoute(data)
  259. message.success(t('common.updateSuccess'))
  260. close()
  261. }
  262. // 发送操作成功的事件
  263. emit('success')
  264. } finally {
  265. formLoading.value = false
  266. }
  267. }
  268. /** 重置表单 */
  269. onMounted(async () => {
  270. // const deptId = useUserStore().getUser.deptId
  271. // deptUsers.value = await UserApi.getDeptUsersByDeptId(deptId)
  272. if (id) {
  273. formType.value = 'update'
  274. const iotInspectRoute = await IotInspectRouteApi.getIotInspectRoute(id)
  275. deviceLabel.value = iotInspectRoute.deviceName
  276. formData.value = iotInspectRoute
  277. items.value = JSON.parse(iotInspectRoute.itemJson)
  278. } else {
  279. formData.value.type = 'in'
  280. formType.value = 'create'
  281. const { wsCache } = useCache()
  282. const userInfo = wsCache.get(CACHE_KEY.USER)
  283. formData.value.maintainPerson = userInfo.user.id
  284. }
  285. productClassifyList.value = handleTree(
  286. await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  287. )
  288. })
  289. const handleDelete = async (id: number) => {
  290. try {
  291. const index = list.value.findIndex((item) => item.code === id)
  292. if (index !== -1) {
  293. // 通过 splice 删除元素
  294. list.value.splice(index, 1)
  295. }
  296. } catch {}
  297. }
  298. </script>
  299. <style scoped>
  300. .base-expandable-content {
  301. overflow: hidden; /* 隐藏溢出的内容 */
  302. transition: max-height 0.3s ease; /* 平滑过渡效果 */
  303. }
  304. /* 横向布局容器 */
  305. .horizontal-list {
  306. display: flex;
  307. gap: 16px;
  308. padding: 20px;
  309. overflow-x: auto;
  310. flex-wrap: nowrap; /* 禁止换行:ml-citation{ref="5" data="citationList"} */
  311. }
  312. /* 拖拽项样式 */
  313. .horizontal-item {
  314. flex: 0 0 auto;
  315. min-width: 150px;
  316. padding: 20px;
  317. background: #d3a137;
  318. border: 2px solid #e0e0e0;
  319. border-radius: 8px;
  320. cursor: move;
  321. user-select: none; /* 防止文字选中:ml-citation{ref="7" data="citationList"} */
  322. transition:
  323. transform 0.3s,
  324. box-shadow 0.3s;
  325. display: flex;
  326. align-items: center;
  327. gap: 10px;
  328. }
  329. /* 拖拽手柄样式 */
  330. .drag-handle {
  331. opacity: 0.5;
  332. cursor: move;
  333. transition: opacity 0.3s;
  334. }
  335. .drag-handle:hover {
  336. opacity: 1;
  337. }
  338. /* 拖拽时的悬停效果 */
  339. .horizontal-item:hover {
  340. transform: translateY(-2px);
  341. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  342. }
  343. /* 滚动条样式 */
  344. .horizontal-list::-webkit-scrollbar {
  345. height: 8px;
  346. }
  347. .horizontal-list::-webkit-scrollbar-thumb {
  348. background: #888;
  349. border-radius: 4px;
  350. }
  351. .sortable-container {
  352. cursor: move;
  353. display: flex;
  354. flex-direction: column;
  355. gap: 9px;
  356. //max-height: 80vh;
  357. overflow-y: auto;
  358. padding: 7px;
  359. }
  360. .sortable-item {
  361. display: flex;
  362. align-items: center;
  363. padding: 8px;
  364. background: #fff;
  365. border: 1px solid #ebeef5;
  366. border-radius: 8px;
  367. transition:
  368. transform 0.3s,
  369. box-shadow 0.3s;
  370. user-select: none;
  371. height: 50%;
  372. }
  373. .sortable-item:hover {
  374. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  375. }
  376. .order-number {
  377. width: 24px;
  378. height: 24px;
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. background: #409eff;
  383. color: white;
  384. border-radius: 50%;
  385. margin-right: 7px;
  386. font-weight: bold;
  387. }
  388. .drag-handle {
  389. padding: 0 12px;
  390. opacity: 0.4;
  391. transition: opacity 0.3s;
  392. }
  393. .drag-handle:hover {
  394. opacity: 1;
  395. }
  396. .component-content {
  397. flex: 1;
  398. min-width: 0;
  399. }
  400. /* 优化滚动条 */
  401. .sortable-container::-webkit-scrollbar {
  402. width: 8px;
  403. }
  404. .sortable-container::-webkit-scrollbar-thumb {
  405. background: #c0c4cc;
  406. border-radius: 4px;
  407. }
  408. </style>