zhaowei
2025-07-02 d3441881742c1397cd02f68c4b0de565456f97b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<template>
  <a-spin :spinning="confirmLoading">
    <vxe-toolbar>
      <template v-slot:buttons>
        <vxe-button status="primary" @click="insertEvent()">新增</vxe-button>
        <vxe-button status="danger" @click="handleBatchRemove()">批量删除</vxe-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="editableDetailTable"
      border
      resizable
      show-overflow
      keep-source
      :height="300"
      :loading="confirmLoading"
      :data="dataSource"
      :edit-rules="validRules"
      :edit-config="{trigger: 'manual', mode: 'row', showStatus: true}"
      style="margin-top: 8px;">
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column title="ID" field="id" :visible="false"></vxe-table-column>
      <vxe-table-column title="equipmentId" field="equipmentId" :visible="false"></vxe-table-column>
      <vxe-table-column title="参数名称" field="parameterId" align="center"
                        :edit-render="{name : '$select', options: processParameterList, optionProps: {label:'parameterName', value:'id'}, props: {clearable:true}, events: {change:handleParamSelectChange}}"></vxe-table-column>
      <vxe-table-column title="参数编码" field="parameterCode" align="center"></vxe-table-column>
      <vxe-table-column title="计量单位" field="parameterUnit" align="center"></vxe-table-column>
      <vxe-table-column title="参数范围" field="parameterPeriod" align="center"
                        :edit-render="{name : '$input'}"></vxe-table-column>
      <vxe-table-column title="操作" width="160">
        <template v-slot="{row}">
          <template v-if="hasEditStatus(row)">
            <vxe-button @click="saveRowEvent(row)">保存</vxe-button>
            <vxe-button @click="cancelRowEvent(row)">取消</vxe-button>
          </template>
          <template v-else>
            <vxe-button @click="editRowEvent(row)">编辑</vxe-button>
            <vxe-button @click="handleRemove(row)">删除</vxe-button>
          </template>
 
        </template>
      </vxe-table-column>
    </vxe-table>
    <a-pagination
      v-bind="ipagination"
      @change="handlePageChange"
      @showSizeChange="handleShowSizeChange"
    />
  </a-spin>
</template>
 
<script>
import { deleteAction, getAction, httpAction } from '@/api/manage'
 
export default {
  name: 'EamEquipmentProcessModal',
  data() {
    return {
      title: '操作',
      confirmLoading: false,
      url: {
        add: '/eam/equipmentProcessParameters/add',
        edit: '/eam/equipmentProcessParameters/edit',
        list: '/eam/equipmentProcessParameters/list',
        delete: '/eam/equipmentProcessParameters/delete',
        deleteBatch: '/eam/equipmentProcessParameters/deleteBatch',
        processParamList: '/eam/processParameters/listAll'
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        size: 'small'
      },
      dataSource: [],
      processParameterList: [],
      queryParam: {
        equipmentId: '-1'
      },
      validRules: {
        parameterId: [
          { required: true, message: '请选择参数!' }
        ],
        parameterPeriod: [
          { required: true, message: '请输入参数范围!' }
        ]
      }
    }
  },
  created() {
    this.loadProcessParameterList()
  },
  methods: {
    handleParamSelectChange($event, value) {
      let parameter = this.processParameterList.find(item => item.id === value.value)
      if (parameter) {
        $event.row.parameterUnit = parameter.parameterUnit
        $event.row.parameterCode = parameter.parameterCode
      } else {
        $event.row.parameterUnit = undefined
        $event.row.parameterCode = undefined
      }
    },
    loadProcessParameterList() {
      getAction(this.url.processParamList).then(res => {
        if (res.success) {
          this.processParameterList = [...res.result]
        }
      })
    },
    onClearSelected() {
      this.dataSource = []
      this.queryParam.equipmentId = '-1'
      this.ipagination.current = 1
      this.ipagination.total = 0
    },
    loadData(args) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      if (args === 1) {
        this.ipagination.current = 1
      }
      // 封装查询条件
      let formData = {
        pageNo: args,
        pageSize: this.ipagination.pageSize,
        equipmentId: this.queryParam.equipmentId
      }
      // 调用查询数据接口
      this.confirmLoading = true
      getAction(this.url.list, formData).then(res => {
        if (res.success) {
          // 后台查询回来的 total,数据总数量
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        // 这里是无论成功或失败都会执行的方法,在这里关闭loading
        this.confirmLoading = false
      })
    },
    async insertEvent() {
      let record = { equipmentId: this.queryParam.equipmentId }
      let { row: newRow } = await this.$refs.editableDetailTable.insert(record)
      await this.$refs.editableDetailTable.setActiveCell(newRow, 'parameterId')
    },
    async saveRowEvent(row) {
      let that = this
      const errMap = await that.$refs.editableDetailTable.validate().catch(errMap => errMap)
      if (errMap) {
        that.$message.warning('校验不通过,请补充必填项!')
        return
      }
      that.confirmLoading = true
      let httpurl = ''
      let method = ''
      if (!row.id) {
        httpurl += that.url.add
        method = 'post'
      } else {
        httpurl += that.url.edit
        method = 'put'
      }
      let res = await httpAction(httpurl, row, method)
      if (res.success) {
        that.$message.success(res.message)
        that.loadData(that.ipagination.current)
        await that.$refs.editableDetailTable.clearActived()
      } else {
        that.$message.warning(res.message)
      }
      that.confirmLoading = false
    },
    handleRemove(row) {
      let xTable = this.$refs.editableDetailTable
      let that = this
      this.$confirm({
        content: `确认要删除吗?`,
        onOk: () => {
          if (row.id) {
            //后端删除
            deleteAction(that.url.delete, { id: row.id }).then((res) => {
              if (res.success) {
                that.reCalculatePage(1)
                that.$message.success(res.message)
                that.loadData(that.ipagination.current)
              } else {
                that.$message.warning(res.message)
              }
            })
          } else {
            //前端删除
            xTable.remove(row)
          }
        },
        onCancel: () => {
          xTable.setActiveCell(row, 'parameterId')
        }
      })
    },
    handleBatchRemove(){
      let xTable = this.$refs.editableDetailTable
      let that = this
      let checkboxRecords = xTable.getCheckboxRecords();
      if(checkboxRecords && checkboxRecords.length > 0) {
        let ids = checkboxRecords.map(record => record.id);
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.confirmLoading = true;
            deleteAction(that.url.deleteBatch, {ids: ids.join(',')}).then((res) => {
              if (res.success) {
                //重新计算分页问题
                that.reCalculatePage(checkboxRecords.length)
                that.$message.success(res.message);
                that.loadData(that.ipagination.current);
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            });
          }
        });
      } else {
        that.$message.warning('请先选中数据行!');
      }
    },
    editRowEvent(row) {
      this.$refs.editableDetailTable.setActiveRow(row)
    },
    cancelRowEvent(row) {
      let xTable = this.$refs.editableDetailTable
      this.$confirm({
        content: `确认要放弃编辑吗?`,
        onOk: () => {
          xTable.revertData()
        },
        onCancel: () => {
          xTable.setActiveCell(row, 'parameterId')
        }
      })
    },
    hasEditStatus(row) {
      const $table = this.$refs.editableDetailTable
      if ($table) {
        return $table.isActiveByRow(row)
      }
      return false
    },
    reCalculatePage(count) {
      //总数量-count
      let total = this.ipagination.total - count
      //获取删除后的分页数
      let currentIndex = Math.ceil(total / this.ipagination.pageSize)
      //删除后的分页数<所在当前页
      if (currentIndex < this.ipagination.current) {
        this.ipagination.current = currentIndex
      }
    },
    handlePageChange(current, pageSize){
      this.$set(this.ipagination, 'current', current)
      this.$emit('change', {current, pageSize})
    },
    handleShowSizeChange(current, pageSize){
      this.$set(this.ipagination, 'pageSize', pageSize)
      this.$emit('change', {current, pageSize})
    },
  }
}
</script>
 
<style lang="less" scoped>
 
</style>