张诺
5 小时以前 b9d3432b9994a3a050cb913d6137a8a7f0be9f67
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
<template>
  <view class="invoice-add">
    <!-- 使用通用页面头部组件 -->
    <PageHeader title="生产报工"
                @back="goBack" />
    <!-- 表单内容 -->
    <u-form @submit="submitForm"
            ref="formRef"
            label-width="110"
            input-align="right"
            error-message-align="right">
      <!-- 基本信息 -->
      <view class="form-section">
        <u-form-item label="机台"
                     prop="deviceName"
                     required>
          <u-input v-model="form.deviceName"
          disabled
                   placeholder="请输入机台" />
        </u-form-item>
        <u-form-item label="计划开始时间"
                     prop="planStartTime"
                     required>
          <u-input v-model="form.planStartTime"
                   placeholder="请选择计划开始时间"
                   readonly
                   @click="showStartTimePicker = true"
                   suffix-icon="calendar" />
        </u-form-item>
        <u-form-item label="计划结束时间"
                     prop="planEndTime"
                     required>
          <u-input v-model="form.planEndTime"
                   placeholder="请选择计划结束时间"
                   readonly
                   @click="showEndTimePicker = true"
                   suffix-icon="calendar" />
        </u-form-item>
        <u-form-item label="待生产数量"
                     prop="planQuantity"
                     required>
          <u-input v-model="form.planQuantity"
                   placeholder="自动填充"
                   disabled />
        </u-form-item>
        <u-form-item label="本次生产数量"
                     prop="quantity"
                     required>
          <u-input v-model="form.quantity"
                   placeholder="请输入"
                   type="number" />
        </u-form-item>
        <u-form-item label="报废数量"
                     prop="scrapQty">
          <u-input v-model="form.scrapQty"
                   placeholder="请输入"
                   type="number" />
        </u-form-item>
        <!-- 班组信息和审核人 -->
        <u-form-item v-for="(item, key) in pickerFields"
                     :key="key"
                     :label="item.label"
                     :prop="key + '.name'"
                     required>
          <u-input v-model="form[key].name"
                   :placeholder="form[key].name ? '已选择: ' + form[key].name : '请选择' + item.label"
                   readonly
                   @click="openProducerPicker(key)"
                   suffix-icon="arrow-down" />
        </u-form-item>
      </view>
      <!-- 使用FooterButtons组件 -->
      <FooterButtons @cancel="goBack"
                     @confirm="submitForm"
                     :loading="submitting" />
      <!-- 为底部按钮留出空间 -->
      <view style="height: 80px;"></view>
    </u-form>
    <!-- 生产人选择器 -->
    <up-action-sheet :show="showProducerPicker"
                     :actions="producerList"
                     title="选择生产人"
                     @select="onProducerConfirm"
                     @close="showProducerPicker = false" />
 
    <!-- 开始时间选择器 -->
    <up-datetime-picker :show="showStartTimePicker"
                        v-model="startTimeValue"
                        mode="datetime"
                        @confirm="onStartTimeConfirm"
                        @cancel="showStartTimePicker = false" />
 
    <!-- 结束时间选择器 -->
    <up-datetime-picker :show="showEndTimePicker"
                        v-model="endTimeValue"
                        mode="datetime"
                        @confirm="onEndTimeConfirm"
                        @cancel="showEndTimePicker = false" />
  </view>
</template>
 
<script setup>
  import { ref, nextTick } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
 
  const showToast = message => {
    uni.showToast({
      title: message,
      icon: "none",
    });
  };
  import { addProductMain } from "@/api/productionManagement/productionReporting";
  import { getInfo } from "@/api/login";
  import { userListNoPageByTenantId } from "@/api/system/user";
 
  // 表单引用
  const formRef = ref();
 
  // 表单数据
  const form = ref({
    deviceName: "",
    planStartTime: "",
    planEndTime: "",
    planQuantity: "",
    quantity: "",
    scrapQty: "",
    workOrderId: "",
    productProcessRouteItemId: "",
    userId: { value: "", name: "" }, // 班组信息
    auditUserId: { value: "", name: "" }, // 审核人
  });
 
  // 这里的字段配置用于模版循环
  const pickerFields = {
    userId: { label: "班组信息" },
    auditUserId: { label: "审核人" },
  };
 
  // 生产人选择器状态
  const showProducerPicker = ref(false);
  // 时间选择器状态
  const showStartTimePicker = ref(false);
  const showEndTimePicker = ref(false);
  const startTimeValue = ref(Number(new Date()));
  const endTimeValue = ref(Number(new Date()));
 
  const producerList = ref([]);
  const currentField = ref(""); // 当前选择的字段
 
  // 打开生产人选择器
  const openProducerPicker = async (field) => {
    if (producerList.value.length === 0) {
      // 如果列表为空,先加载用户列表
      try {
        const res = await userListNoPageByTenantId();
        const users = res.data || [];
        // 转换为 action-sheet 需要的格式
        producerList.value = users.map(user => ({
          name: user.nickName || "",
          value: user.userId,
        }));
      } catch (error) {
        console.error("加载用户列表失败:", error);
        showToast("加载用户列表失败");
        return;
      }
    }
    showProducerPicker.value = true;
    currentField.value = field; // 保存当前字段
  };
 
  // 生产人选择确认
  const onProducerConfirm = e => {
    if (currentField.value && form.value[currentField.value]) {
      form.value[currentField.value].value = e.value;
      form.value[currentField.value].name = e.name ;
    }
    showProducerPicker.value = false;
  };
 
  // 格式化日期
  const formatDateTime = (timestamp) => {
    const date = new Date(timestamp);
    const y = date.getFullYear();
    const m = (date.getMonth() + 1).toString().padStart(2, '0');
    const d = date.getDate().toString().padStart(2, '0');
    const h = date.getHours().toString().padStart(2, '0');
    const min = date.getMinutes().toString().padStart(2, '0');
    const s = date.getSeconds().toString().padStart(2, '0');
    return `${y}-${m}-${d} ${h}:${min}:${s}`;
  };
 
  // 开始时间确认
  const onStartTimeConfirm = (e) => {
    form.value.planStartTime = formatDateTime(e.value);
    showStartTimePicker.value = false;
  };
 
  // 结束时间确认
  const onEndTimeConfirm = (e) => {
    form.value.planEndTime = formatDateTime(e.value);
    showEndTimePicker.value = false;
  };
 
  // 提交状态
  const submitting = ref(false);
 
  // 返回上一页
  const goBack = () => {
    uni.navigateBack();
  };
  // 提交表单
  const submitForm = async () => {
    submitting.value = true;
    // 校验表单
    if (!form.value.deviceName) {
      submitting.value = false;
      showToast("请输入机台");
      return;
    }
    if (!form.value.planStartTime) {
      submitting.value = false;
      showToast("请选择计划开始时间");
      return;
    }
    if (!form.value.planEndTime) {
      submitting.value = false;
      showToast("请选择计划结束时间");
      return;
    }
    if (!form.value.quantity) {
      submitting.value = false;
      showToast("请输入本次生产数量");
      return;
    }
    if (!form.value.userId.value) {
      submitting.value = false;
      showToast("请选择班组信息");
      return;
    }
    if (!form.value.auditUserId.value) {
      submitting.value = false;
      showToast("请选择审核人");
      return;
    }
    // 转换为数字进行比较
    const quantity = Number(form.value.quantity) || 0;
    const scrapQty = Number(form.value.scrapQty) || 0;
    const planQuantity = Number(form.value.planQuantity);
    // 验证生产数量和报废数量的和不能超过待生产数量
    if (quantity + scrapQty > planQuantity) {
      submitting.value = false;
      showToast("生产数量和报废数量的和不能超过待生产数量");
      return;
    }
    if (quantity > planQuantity) {
      submitting.value = false;
      showToast("本次生产数量不能大于待生产数量");
      return;
    }
    // 准备提交数据,确保数量字段为数字类型
    const submitData = {
      ...form.value,
      quantity: Number(form.value.quantity),
      scrapQty: Number(form.value.scrapQty) || 0,
      planQuantity: Number(form.value.planQuantity) || 0,
      userId: form.value.userId.value,
      auditUserId: form.value.auditUserId.value,
      auditUserName: form.value.auditUserId.name,
      schedulingUserId: form.value.userId.value, // 兼容旧字段
    };
    console.log(submitData, "submitData");
 
    addProductMain(submitData).then(res => {
      if (res.code === 200) {
        showToast("报工成功");
        submitting.value = false;
        setTimeout(() => {
          goBack();
        }, 1000);
      } else {
        showToast(res.msg || "报工失败");
        submitting.value = false;
      }
    }).catch(err => {
      submitting.value = false;
    })
    ;
  };
 
  // 页面加载时初始化数据
  onLoad(options => {
    try {
      const orderRow = JSON.parse(options.orderRow);
      console.log(orderRow, "orderRow");
      // 确保 planQuantity 转换为字符串,以便在 u-input 中正确显示
      form.value.planQuantity = orderRow.planQuantity != null ? String(orderRow.planQuantity) : "";
      form.value.productProcessRouteItemId = orderRow.productProcessRouteItemId || "";
      form.value.workOrderId = orderRow.id || "";
      form.value.deviceName = orderRow.deviceName || "";
      form.value.planStartTime = orderRow.planStartTime || "";
      form.value.planEndTime = orderRow.planEndTime || "";
      getInfo().then(res => {
        // 默认使用当前登录用户,但允许用户修改
        form.value.userId.value = res.user.userId;
        form.value.userId.name = res.user.nickName;
      });
      // 使用 nextTick 确保 DOM 更新
      nextTick(() => {
        console.log("form.value after assignment:", form.value);
      });
    } catch (error) {
      console.error("订单解析失败:", error);
      showToast("订单解析失败");
      goBack();
      return;
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>