cuilei
5 天以前 dacf37deb3d53345024402016e9c4d19ac82e3aa
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
 
    <a-button :style="{ marginBottom: '10px' }" type="primary" @click="handleAdd">新增</a-button>
    <vxe-table
      ref="table"
      border
      :data="dataSource"
      :edit-config="{trigger: 'click', mode: 'cell'}"
      :edit-rules="editRules"
    >
      <vxe-table-column type="seq" width="40"/>
      <vxe-table-column width="200" title="工单号" field="workOrderCode">
        <template #default="{ row }">
          <span v-if="row.workOrderCode">{{ row.workOrderCode }}</span>
          <span v-else class="placeholder-text">系统自动生成</span>
        </template>
      </vxe-table-column>
      <vxe-table-column
        title="物料编号"
        field="materialNumber"
        :edit-render="{name: '$select', options: materialOptions, events: {change: handleMaterialChange}}">
        <template #default="{ row }">
          <span v-if="row.materialNumber">{{ row.materialNumber }}</span>
          <span v-else class="placeholder-text">请选择物料编号</span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="物料名称" field="materialName">
        <template #default="{ row }">
          <span v-if="row.materialName">{{ row.materialName }}</span>
          <span v-else class="placeholder-text">物料名称自动填充</span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="产线" field="factoryName"></vxe-table-column>
      <vxe-table-column
        title="班组"
        field="groupId"
        :edit-render="{name: '$select', options: groupOptions, events: {change: handleGroupChange}}">
      </vxe-table-column>
      <vxe-table-column title="班次id" :visible="false" field="shiftName"></vxe-table-column>
      <vxe-table-column title="班次" field="shiftName"></vxe-table-column>
      <vxe-table-column
        title="排产日期"
        field="workOrderDate"
        :edit-render="{name: '$select', options: workOrderDateOptions, events: {change: handleWorkOrderDateChange}}">
      </vxe-table-column>
      <vxe-table-column
        title="计划生产数量"
        field="planQuantity"
        width="150"
        :edit-render="{name: '$input'}">
        <template #default="{ row }">
          <span v-if="row.planQuantity">{{ row.planQuantity }}</span>
          <span v-else class="placeholder-text">请填写计划生产数量</span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作">
        <template #default="{ row }">
          <vxe-button size="small" class="error-button" @click="handleRemove(row)">删除</vxe-button>
        </template>
      </vxe-table-column>
    </vxe-table>
  </j-modal>
</template>
 
<script>
 
import MesProductionWorkOrderForm from './MesProductionWorkOrderForm'
import { ajaxGetDictItems } from '@api/api'
import { getAction, postAction } from '@api/manage'
 
export default {
    name: 'MesProductionWorkOrderModal',
    components: {
      MesProductionWorkOrderForm
    },
    data () {
      return {
        title: '排产明细',
        width: 1300,
        visible: false,
        disableSubmit: false,
        loading: false,
        dataSource: [],
        groupOptions: [],
        groupShiftMap: {},
        materialOptions: [],
        materNumberNameMap: {},
        workOrderDateOptions: [],
        url: {
          queryShiftGroupByFactoryId: '/base/shiftGroup/queryShiftGroupByFactoryId',
          addSchedulePlan: '/mesproductionworkorder/mesProductionWorkOrder/addSchedulePlan'
        },
        editRules: {
          materialNumber: [
            { required: true, message: '物料编号必须选择' }
          ],
          groupId: [
            { required: true, message: '班组必须选择' }
          ],
          workOrderDate: [
            { required: true, message: '排产日期必须选择' }
          ],
          planQuantity: [
            { required: true, message: '计划生产数量为必填' }
          ]
        }
      }
    },
    methods: {
      // 生成日期范围数组
      generateDateRangeOptions(startDate, endDate) {
        const dateOptions = [];
        const start = new Date(startDate);
        const end = new Date(endDate);
 
        // 设置时间为每天的0点
        start.setHours(0, 0, 0, 0);
        end.setHours(0, 0, 0, 0);
 
        // 生成日期范围内的所有日期
        for (let date = new Date(start); date <= end; date.setDate(date.getDate() + 1)) {
          const formattedDate = this.formatDate(date);
          dateOptions.push({
            value: formattedDate,
            label: formattedDate
          });
        }
        return dateOptions;
      },
      // 格式化日期为 YYYY-MM-DD
      formatDate(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      },
      // 生成工单号的方法
      generateWorkOrderCode(row) {
        console.log('row:', row)
        // 获取各个字段的值
        const factoryCode = row.factoryCode || '';      // 产线编号
        const materialNumber = row.materialNumber || ''; // 物料编号
        const workDate = row.workOrderDate || '';       // 排产日期
        const shiftCode = row.shiftCode || '';            // 班次编号
 
        // 格式化排产日期(如果需要特定格式,比如只取年月日)
        let formattedDate = workDate;
        if (workDate && workDate.length >= 10) {
          // 如果是完整日期格式,提取 YYYY-MM-DD 部分
          formattedDate = workDate.substring(0, 10).replace(/-/g, '');
        }
        // 拼接工单号
        return `${factoryCode}${materialNumber}${formattedDate}${shiftCode}`;
      },
      // 在适当时机更新工单号
      updateWorkOrderCode(row) {
        row.workOrderCode = this.generateWorkOrderCode(row);
      },
      handleWorkOrderDateChange($event, value) {
        $event.row.workOrderDate = value.value;
        this.updateWorkOrderCode($event.row)
      },
      handleMaterialChange($event, value) {
        const key = value.value
        if (key && this.materNumberNameMap[key]) {
          $event.row.materialName = this.materNumberNameMap[key]
        } else {
          $event.row.materialName = '';
        }
        // 更新物料编号
        $event.row.materialNumber = key;
        // 重新生成工单号
        this.updateWorkOrderCode($event.row);
      },
      handleGroupChange($event, value) {
        const key = value.value
        // 从 groupShiftMap 中获取对应的班次信息
        if (key && this.groupShiftMap[key]) {
          // 更新当前行的班次字段
          $event.row.shiftId = this.groupShiftMap[key].shiftId;
          $event.row.shiftCode = this.groupShiftMap[key].shiftCode;
          $event.row.shiftName = this.groupShiftMap[key].shiftName;
        } else {
          // 如果没有匹配的班次信息,则清空班次字段
          $event.row.shiftId = '';
          $event.row.shiftCode = '';
          $event.row.shiftName = '';
        }
        // 重新生成工单号
        this.updateWorkOrderCode($event.row);
      },
      initDictSelectOptions(record) {
        return new Promise((resolve) => {
          // 并行执行多个异步请求
          const promises = [];
 
          // 获取班组和班次信息
          if (record && record.length > 0) {
            const factoryId = record[0].factoryId
            const shiftGroupPromise = getAction(this.url.queryShiftGroupByFactoryId, { factoryId: factoryId }).then(res => {
              if (res.success) {
                // 修正映射逻辑
                this.groupOptions = res.result.map(item => {
                  return {
                    value: item.id,
                    label: item.groupName
                  }
                })
                // 保存班组和班次的映射关系
                this.groupShiftMap = res.result.reduce((map, item) => {
                  map[item.id] = {
                    shiftId: item.shiftId,
                    shiftCode: item.shiftCode_dictText,
                    shiftName: item.shiftId_dictText
                  }
                  return map
                }, {})
              }
            }).catch(() => {
              // 即使请求失败也不阻塞
            });
            promises.push(shiftGroupPromise);
          }
 
          //获取物料编号下拉选项
          const materialNumberPromise = ajaxGetDictItems("lsw_material,material_name,material_number,del_flag!='1' order by material_number asc", null).then(res => {
            if (res.success) {
              // 假设物料编号字段为 materialNumber,需要根据实际字段调整
              // 将物料选项存储到组件数据中,供表格使用
              this.materialOptions = res.result.map(item => {
                return {
                  value: item.value,
                  label: item.value
                }
              });
              this.materNumberNameMap = res.result.reduce((map, item) => {
                map[item.value] = item.text
                return map
              }, {})
            }
          }).catch(() => {
            // 即使请求失败也不阻塞
          });
          promises.push(materialNumberPromise);
 
          // 等待所有请求完成
          Promise.all(promises).then(() => {
            resolve();
          });
        })
      },
      edit (record, dateRange) {
        // 先加载字典数据,再初始化表单
        this.initDictSelectOptions(record).then(() => {
          this.$nextTick(() => {
            // 根据日期范围生成排产日期选项
            if (dateRange && dateRange.length === 2) {
              this.workOrderDateOptions = this.generateDateRangeOptions(dateRange[0], dateRange[1]);
            }
 
            // 为每条记录生成工单号
            if (record && record.length > 0) {
              record.forEach(row => {
                if (!row.workOrderCode) {
                  this.updateWorkOrderCode(row);
                }
              });
            }
            this.dataSource = record
            this.visible = true
            // 数据加载完成后激活所有行的编辑状态
            this.$nextTick(() => {
              if (this.$refs.table && this.dataSource.length > 0) {
                // 激活第一行进入编辑状态
                this.$refs.table.setActiveRow(this.dataSource[0])
              }
            })
          });
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleAdd() {
        // 创建新行数据
        const newRow = {
          workOrderCode: this.dataSource.length > 0 ? this.dataSource[0].factoryCode : '',
          materialNumber: '',       // 物料编号(需选择)
          materialName: '',         // 物料名称(自动填充)
          factoryId: this.dataSource.length > 0 ? this.dataSource[0].factoryId : '',
          factoryCode: this.dataSource.length > 0 ? this.dataSource[0].factoryCode : '',
          factoryName: this.dataSource.length > 0 ? this.dataSource[0].factoryName : '',
          groupId: '',              // 班组(需选择)
          shiftId: '',              // 班次ID
          shiftCode: '',            // 班次编号
          shiftName: '',            // 班次名称
          workOrderDate: '',        // 排产日期(需选择)
          planQuantity: ''          // 计划生产数量(需填写)
        }
        // 将新行添加到数据源中
        this.dataSource.push(newRow);
        // 激活新增的行进入编辑状态
        this.$nextTick(() => {
          if (this.$refs.table) {
            this.$refs.table.setActiveRow(newRow);
          }
        });
      },
      handleRemove(row) {
        let table = this.$refs.table
        this.$confirm({
          title: '提示',
          content: '确认要删除吗?',
          okText: '确定',
          cancelText: '取消',
          onOk: () => {
            // 删除行
            table.remove(row)
          }
        })
      },
      handleOk () {
        // 表格全表校验
        this.$refs.table.validate((valid) => {
          if (valid) {
            this.$message.error("请完成所有必填信息后再提交!")
          } else {
            let tableData = this.$refs.table.getTableData().fullData
            console.log(tableData)
            postAction(this.url.addSchedulePlan, tableData).then(res=> {
              if (res.success) {
                this.$message.success(res.message)
                this.submitCallback()
              } else {
                this.$message.warning(res.message)
              }
            })
          }
        })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>
 
<style scoped>
 
.placeholder-text {
  color: #999;
  font-style: italic;
}
 
.error-button {
  background-color: #ff4d4f !important;
  border-color: #ff4d4f !important;
  color: #fff !important;
}
 
</style>