lixiangyu
18 小时以前 8b5bfdeb201b2653fb648f742b6d4ff5ff05ff6c
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<template>
  <a-card :bordered="false">
    <!-- 新增按钮 -->
    <div class="table-operator" style="margin-bottom: 10px;">
      <a-button
        @click="handleAdd"
        type="primary"
        icon="plus"
        :disabled="!selectedCuttingId || selectedCuttingId === '' || selectedCuttingId === '-1'"
      >
        新增
      </a-button>
    </div>
 
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
 
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
 
        <!-- 可编辑单元格 -->
        <template
          v-for="col in ['workpieceMaterial', 'ratedLife']"
          :slot="col"
          slot-scope="text, record"
        >
          <div :key="col">
            <a-input
              v-if="record.editable"
              style="margin: -5px 0"
              :value="text"
              @change="e => handleChange(e.target.value, record.id, col)"
            />
            <template v-else>
              {{ text }}
            </template>
          </div>
        </template>
 
        <!-- 操作列 -->
        <span slot="action" slot-scope="text, record">
          <div class="editable-row-operations">
            <span v-if="record.editable">
              <a-popconfirm title="确定保存?" @confirm="() => save(record.id)">
                <a>保存</a>
              </a-popconfirm>
              <a style="margin-left: 8px" @click="() => cancel(record.id)">取消</a>
            </span>
            <span v-else>
              <a :disabled="editingKey !== ''" @click="() => edit(record.id)">编辑</a>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </div>
        </span>
 
      </a-table>
    </div>
 
    <rated-life-modal ref="modalForm" @ok="modalFormOk"></rated-life-modal>
  </a-card>
</template>
 
<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { putAction, getAction, postAction } from '@/api/manage'
import RatedLifeModal from './modules/RatedLifeModal'
 
export default {
  name: 'RatedLifeList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    RatedLifeModal
  },
  props: {
    selectedCuttingId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      description: '额定寿命管理页面',
      editingKey: '',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'刀具编码',
          align:"center",
          dataIndex: 'cuttingId_dictText',
          sorter: true
        },
        {
          title:'工件材质',
          align:"center",
          dataIndex: 'workpieceMaterial',
          scopedSlots: { customRender: 'workpieceMaterial' }
        },
        {
          title:'额定寿命',
          align:"center",
          dataIndex: 'ratedLife',
          scopedSlots: { customRender: 'ratedLife' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
          width: 200,
        }
      ],
      url: {
        list: "/cms/ratedLife/list",
        delete: "/cms/ratedLife/delete",
        deleteBatch: "/cms/ratedLife/deleteBatch",
        exportXlsUrl: "/cms/ratedLife/exportXls",
        importExcelUrl: "cms/ratedLife/importExcel",
        edit: "/cms/ratedLife/edit",
        add: "/cms/ratedLife/add"
      },
      dictOptions:{},
      superFieldList:[],
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig(){
    },
    getRatedLife(cuttingId) {
      this.queryParam.cuttingId = cuttingId;
      // this.ipagination.current = 1;
      this.loadData(1);
    },
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'string',value:'cuttingId',text:'刀具ID'})
      fieldList.push({type:'string',value:'workpieceMaterial',text:'工件材质'})
      fieldList.push({type:'number',value:'ratedLife',text:'额定寿命'})
      this.superFieldList = fieldList
    },
 
    // 新增行
    handleAdd() {
      // 如果已经在编辑状态,自动取消当前编辑项
      if (this.editingKey !== '') {
        // 查找正在编辑的记录并取消编辑
        const editingRecord = this.dataSource.find(item => item.id === this.editingKey);
        if (editingRecord) {
          this.cancel(this.editingKey);
        }
      }
 
      // 创建新的空数据项
      const newRow = {
        id: 'new_' + new Date().getTime(), // 临时ID
        cuttingId: this.queryParam.cuttingId,
        workpieceMaterial: '',
        ratedLife: '',
        editable: true,
        isNew: true // 标记为新添加的行
      };
 
      // 将新行插入到数据源开头
      this.dataSource = [newRow, ...this.dataSource];
      this.editingKey = newRow.id;
    },
 
    // 编辑行数据
    edit(key) {
      // 先保存当前数据的副本
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
      this.editingKey = key;
 
      if (target) {
        target.editable = true;
        this.dataSource = newData;
      }
    },
 
    // 保存编辑的数据
    save(key) {
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
 
      if (target) {
        delete target.editable;
        this.dataSource = newData;
 
        // 判断是新增还是编辑
        if (target.isNew) {
          // 新增操作
          const params = { ...target };
          delete params.id;
          delete params.isNew;
          // 删除创建时间字段,让后端自动生成
          delete params.createTime;
          delete params.updateTime;
 
          postAction(this.url.add, params).then(res => {
            if (res.success) {
              this.$message.success('新增成功');
              this.editingKey = '';
              this.loadData(); // 重新加载数据
            } else {
              this.$message.error(res.message);
            }
          }).catch(err => {
            this.$message.error('新增失败');
            console.log(err);
          });
        } else {
          // 编辑操作 - 只发送需要更新的字段
          const params = {
            id: target.id,
            cuttingId: target.cuttingId,
            workpieceMaterial: target.workpieceMaterial,
            ratedLife: target.ratedLife,
          };
 
          putAction(this.url.edit, params).then(res => {
            if (res.success) {
              this.$message.success('保存成功');
              this.editingKey = '';
              this.loadData(); // 重新加载数据
            } else {
              this.$message.error(res.message);
            }
          }).catch(err => {
            this.$message.error('保存失败');
            console.log(err);
          });
        }
      }
    },
 
    // 取消编辑
    cancel(key) {
      const newData = [...this.dataSource];
      const targetIndex = newData.findIndex(item => key === item.id);
      this.editingKey = '';
 
      if (targetIndex >= 0) {
        // 如果是新增的行,直接删除
        if (newData[targetIndex].isNew) {
          newData.splice(targetIndex, 1);
          this.dataSource = newData;
        } else {
          // 如果是编辑的行,从后端重新获取数据以恢复原始值
          const target = newData[targetIndex];
          getAction(this.url.list, { id: key }).then(res => {
            if(res.success && res.result.records.length > 0) {
              const originalData = res.result.records[0];
              Object.keys(target).forEach(key => {
                if(key !== 'editable') {
                  target[key] = originalData[key];
                }
              });
              delete target.editable;
              this.dataSource = newData;
            }
          });
        }
      }
    },
 
    // 处理单元格值变化
    handleChange(value, key, column) {
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
 
      if (target) {
        target[column] = value;
        this.dataSource = newData;
      }
    }
  }
}
</script>
 
<style scoped>
@import '~@assets/less/common.less';
 
.editable-row-operations a {
  margin-right: 8px;
}
</style>