liding
8 小时以前 50552c590564e51349229d4f2e26a147109adc60
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
<template>
  <el-dialog
    v-model="visible"
    title="领料"
    width="1000px"
    top="3vh"
    :close-on-click-modal="false"
    destroy-on-close
    class="material-requisition-dialog"
  >
    <div class="material-requisition-form">
      <!-- 原材料列表 -->
      <el-table :data="materialList" border style="width: 100%" height="65vh">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="productName" label="产品名称" min-width="150" />
        <el-table-column prop="model" label="型号" min-width="150" />
        <!-- <el-table-column prop="batchNo" label="批号" min-width="150">
          <template #default="{ row }">
            <el-select
              v-model="row.batchNo"
              placeholder="请选择批号"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in row.batchOptions || []"
                :key="item.batchNo"
                :label="item.batchNo"
                :value="item.batchNo"
              />
            </el-select>
          </template>
        </el-table-column> -->
        <el-table-column prop="unit" label="单位" width="80" align="center" />
        <el-table-column prop="qualitity" label="数量" width="100" align="center">
          <template #default="{ row }">
            {{ row.qualitity || 0 }}
          </template>
        </el-table-column>
        <el-table-column prop="requisitionQty" label="领用数量" width="120" align="center">
          <template #default="{ row }">
            <el-input-number
              v-model="row.requisitionQty"
              :min="0"
              :precision="2"
              :controls="false"
              :disabled="!row.qualitity || hasDrawMaterials"
              style="width: 100%"
            />
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="150">
        </el-table-column>
      </el-table>
    </div>
 
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" :loading="saving" @click="handleConfirm">确 认</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </span>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, reactive, computed, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { getByBomId, drawMaterials } from '@/api/productionManagement/productionOrder.js';
 
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  orderData: {
    type: Object,
    default: () => ({})
  }
});
 
const emit = defineEmits(['update:modelValue', 'confirm']);
 
const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
});
 
const loading = ref(false);
const saving = ref(false);
const materialList = ref([]);
const hasDrawMaterials = ref(false);
 
// 监听弹框打开,加载数据
watch(() => props.modelValue, (val) => {
  if (val && props.orderData) {
    loadMaterialList();
  }
});
 
const loadMaterialList = async () => {
  const order = props.orderData;
  const drawMaterialsData = order.drawMaterials;
 
  // 如果已有领料数据,直接使用
  if (drawMaterialsData) {
    hasDrawMaterials.value = true;
    try {
      const list = typeof drawMaterialsData === 'string'
        ? JSON.parse(drawMaterialsData)
        : drawMaterialsData;
      materialList.value = list.map(item => ({
        ...item,
        requisitionQty: item.requisitionQty || 0
      }));
      return;
    } catch (e) {
      console.error('解析领料数据失败:', e);
    }
  }
 
  // 没有领料数据,调用接口查询
  hasDrawMaterials.value = false;
  const bomId = order?.bomId;
  if (!bomId) {
    ElMessage.warning('当前订单缺少BOM信息');
    return;
  }
 
  loading.value = true;
  try {
    const res = await getByBomId({ bomId });
    const data = res.data || [];
    // 处理数据,添加领用数量字段和批号选项
    materialList.value = data.map(item => ({
      ...item,
      requisitionQty: item.qualitity ? 0 : 0,
      batchNo: item.batchNo || '',
      remark: item.remark || '',
      // 批号选项,从库存原材料信息中获取
      batchOptions: item.inventoryList || []
    }));
  } catch (error) {
    console.error('查询原材料列表失败:', error);
    ElMessage.error('查询原材料列表失败');
    materialList.value = [];
  } finally {
    loading.value = false;
  }
};
 
const handleCancel = () => {
  visible.value = false;
  materialList.value = [];
  hasDrawMaterials.value = false;
};
 
const handleConfirm = async () => {
  const orderId = props.orderData?.id;
  if (!orderId) {
    ElMessage.error('订单ID不存在');
    return;
  }
 
  const drawMaterialsList = materialList.value.map(item => ({
    ...item,
    requisitionQty: item.requisitionQty || 0
  }));
 
  const postData = {
    id: orderId,
    drawMaterials: JSON.stringify(drawMaterialsList)
  };
 
  saving.value = true;
  try {
    await drawMaterials(postData);
    ElMessage.success('领料保存成功');
    visible.value = false;
    materialList.value = [];
    hasDrawMaterials.value = false;
  } catch (error) {
    console.error('保存领料失败:', error);
    ElMessage.error('保存领料失败');
  } finally {
    saving.value = false;
  }
};
</script>
 
<style scoped lang="scss">
.material-requisition-form {
  .el-table {
    margin-top: 10px;
  }
}
 
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
 
:deep(.el-dialog) {
  height: 85vh;
  margin-top: 3vh !important;
  display: flex;
  flex-direction: column;
}
 
:deep(.el-dialog__body) {
  flex: 1;
  overflow-y: auto;
  padding: 15px 20px;
}
 
:deep(.el-dialog__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #e4e7ed;
}
 
:deep(.el-dialog__footer) {
  padding: 15px 20px;
  border-top: 1px solid #e4e7ed;
}
</style>