gaoshp
2024-10-05 65972957e56a31778cc1633b1032ac16627665f3
src/views/console/workstation/CollDialog.vue
@@ -1,11 +1,19 @@
<template>
    <el-dialog title="数据点" v-model="visible" :width="'100%'" destroy-on-close @closed="$emit('closed')">
        <el-select v-model="type" placeholder="Select" size="small" style="width: 240px" @change="changeType">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
    <el-dialog title="数据点" v-model="visible" :width="'80%'" destroy-on-close @closed="$emit('closed')">
        <p style="margin-bottom: 14px;">
            <span style="margin-right: 8px;">类型</span>
            <el-select v-model="type" placeholder="Select" size="small" style="width: 240px" @change="changeType">
                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
        </p>
        <scFormTable v-show="type" ref="table1" v-model="tabledata" stripe hideDelete :hideAdd="!type"
            :addTemplate="addTemplate">
            <el-table-column v-for="item in cols" :key="item.prop" :prop="item.prop" :label="item.label">
                <template #header="scope">
                    <span v-show="item.isRequired" style="color: red;">*</span>
                    <span>{{ scope.column.label }}</span>
                </template>
                <template #default="scope">
                    <el-select v-model="scope.row[item.prop]" placeholder="Select" size="small"
                        v-if="scope.row.status && item.type === 'option'">
@@ -19,12 +27,12 @@
                </template>
            </el-table-column>
            <el-table-column label="操作" prop="state" width="120">
            <el-table-column label="操作" prop="state" width="200">
                <template #default="scope">
                    <el-button-group>
                        <el-button text type="primary" size="small">编辑</el-button>
                        <el-button text type="primary">复制</el-button>
                        <el-popconfirm width="220" title="确定将选择的数据删除" @confirm="del(scope.row)">
                        <el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
                        <el-button text type="primary" size="small" @click="copy(scope.row)">复制</el-button>
                        <el-popconfirm width="220" title="确定将选择的数据删除" @confirm="del(scope.$index)">
                            <template #reference>
                                <el-button text type="primary" size="small">删除</el-button>
                            </template>
@@ -42,8 +50,6 @@
</template>
<script>
import deepmerge from 'deepmerge'
import http from "@/utils/request"
export default {
    emits: ['success', 'closed'],
    props: {
@@ -63,9 +69,6 @@
            tabledata: [],
            apiObj: null,
            addTemplate: {
                name: '',
                describe: '',
                deviceAddress: '',
                status: '2' // 不存在已保存 1: 正在编辑 2: 新增
            },
            type: '',
@@ -78,13 +81,19 @@
    },
    methods: {
        changeType(val) {
            console.log(val)
            console.log(JSON.parse(this.typeList.find(v => v.id === val).dpHeadFull))
            try {
                this.cols = JSON.parse(this.typeList.find(v => v.id === val).dpHeadFull)
            } catch (error) {
            this.$confirm(`切换类型将删除所有已配置数据点`, '提示', {
                type: 'warning'
            }).then(() => {
                try {
                    this.cols = JSON.parse(this.typeList.find(v => v.id === val).dpHeadFull)
                    this.table = []
                } catch (error) {
                    this.cols = []
                }
            }).catch(() => {
            }
            })
        },
        //显示
        open(mode = 'add', params) {
@@ -98,34 +107,76 @@
            return this
        },
        getDetailList(params) {
            this.$HTTP.get(`/api/smart-collect/tpl/typelist`, {}, { params }).then(res => {
                this.typeList = res.data || [];
                // return res
            }).finally(() => {
                this.$HTTP.get(`/api/blade-cps/workstation/listDatapointsByWorkstationId`, {}, { params }).then(res => {
                    // res.data.records = res?.data?.records.map(v => {
                    //     return {
                    //         ...v,
                    //         statusName: v.status ? '启用' : '禁用'
                    //     }
                    // })
                    this.type = res.data.type
                    this.tabledata = res.data.dpConfig || [];
                    // return res
                })
                this.getList(params)
            })
        },
        getList(params) {
            this.$HTTP.get(`/api/blade-cps/workstation/listDatapointsByWorkstationId`, {}, { params }).then(res => {
                try {
                    this.cols = JSON.parse(res.data.dpHead)
                    console.log(this.cols, 'cols')
                } catch (error) {
                    this.cols = []
                }
                try {
                    this.tabledata = JSON.parse(res.data.dpConfig).map(v => {
                        v.status = null
                        return v
                    }) || [];
                } catch (error) {
                    this.tabledata = []
                }
                this.type = res.data.type
                // return res
            })
        },
        table_edit(row) {
            row.status = '1'
        },
        del(index) {
            this.tabledata.splice(index, 1)
        },
        copy(row) {
            this.tabledata.push(Object.assign({}, row, { status: '1' }))
        },
        //表单提交方法
        submit() {
            console.log(this.tabledata)
            let isRequiredKey = this.cols.filter(v => v.isRequired).map(v => v.prop)
            let flag = this.tabledata.every(item => {
                return isRequiredKey.every(v => {
                    return item[v] !== undefined && item[v] !== null && item[v] !== ''
                })
            })
            if (!flag) {
                return this.$message.warning('请校验必填项')
            }
            let len = [...new Set(this.tabledata.map(v => v.dpName))].length
            if (len != this.tabledata.length) {
                return this.$message.warning('数据点名称不能重复')
            }
            this.$HTTP.put('/api/blade-cps/workstation/saveDatapoints', {
                dpConfig: JSON.stringify(this.tabledata),
                dpConfig: JSON.stringify(this.tabledata.map(v => {
                    delete v.status
                    return v
                })),
                workstationId: this.params.id,
                type: this.type
            }).then(res => {
                if (res.code === 200) {
                    this.getList({
                        workstationId: this.params.id,
                    })
                }
            }).finally(() => {
                this.visible = false
                this.$emit('success')
            })
        },