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
<template>
  <div>
    <el-dialog
        v-model="dialogFormVisible"
        title="生产报工"
        width="70%"
                draggable
        @close="closeDia"
    >
      <el-form label-width="140px" label-position="top" :rules="rules" ref="formRef">
        <el-table
          :data="reportList"
          border
          style="width: 100%"
        >
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="60"
          />
          <el-table-column label="合同号" prop="salesContractNo" width="150" />
          <el-table-column label="产品大类" prop="productCategory" width="120" />
          <el-table-column label="规格型号" prop="specificationModel" width="150" />
          <el-table-column label="排产数量" prop="schedulingNum" width="100">
            <template #default="scope">
              <span>{{ scope.row.schedulingNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="待生产数量" prop="pendingNum" width="100">
            <template #default="scope">
              <span>{{ scope.row.pendingNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="本次生产数量" prop="finishedNum" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.finishedNum"
                placeholder="请输入"
                :min="0"
                :step="0.1"
                :precision="2"
                clearable
                style="width: 100%"
                @change="(val) => changeNum(scope.row, val)"
              />
            </template>
          </el-table-column>
          <el-table-column label="单价(元)" prop="unitPrice" width="120">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.unitPrice"
                placeholder="请输入"
                :min="0"
                :step="0.01"
                :precision="2"
                clearable
                style="width: 100%"
                @change="() => calculateTotalPrice(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column label="总价(元)" prop="totalPrice" width="120">
            <template #default="scope">
              <span>{{ scope.row.totalPrice || '0.00' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="生产人" prop="schedulingUserId" width="150">
            <template #default="scope">
              <el-select
                v-model="scope.row.schedulingUserId"
                placeholder="选择人员"
                style="width: 100%;"
                filterable
                default-first-option
                :reserve-keyword="false"
              >
                <el-option
                  v-for="user in userList"
                  :key="user.userId"
                  :label="user.nickName"
                  :value="user.userId"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" prop="schedulingDate" width="150">
            <template #default="scope">
              <el-date-picker
                v-model="scope.row.schedulingDate"
                type="date"
                placeholder="请选择日期"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                clearable
                style="width: 100%"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确认</el-button>
          <el-button @click="closeDia">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import {ref, reactive, toRefs, getCurrentInstance} from "vue";
import {getStaffJoinInfo, staffJoinAdd, staffJoinUpdate} from "@/api/personnelManagement/onboarding.js";
import {userListNoPageByTenantId} from "@/api/system/user.js";
import {productionReport, productionReportUpdate} from "@/api/productionManagement/productionReporting.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
 
const userList = ref([])
const dialogFormVisible = ref(false);
const operationType = ref('')
const reportList = ref([])
const data = reactive({
  rules: {
        schedulingNum: [{ required: true, message: "请输入", trigger: "blur" },],
  },
});
const { rules } = toRefs(data);
 
// 打开弹框
const openDialog = (type, rows) => {
  operationType.value = type;
  dialogFormVisible.value = true;
    userListNoPageByTenantId().then((res) => {
        userList.value = res.data;
    });
    
    // 处理多条数据
    const rowsArray = Array.isArray(rows) ? rows : [rows];
    reportList.value = rowsArray.map(row => {
        const total = Number(row?.schedulingNum ?? 0);
        const pendingFinish = Number(row?.pendingFinishNum ?? 0);
        const autoFill = pendingFinish > 0 ? Math.min(pendingFinish, total) : total;
        const unitPrice = row?.unitPrice ? Number(row.unitPrice) : 0.33;
        
        return {
            id: row?.id ?? null,
            salesContractNo: row?.salesContractNo ?? '',
            productCategory: row?.productCategory ?? '',
            specificationModel: row?.specificationModel ?? '',
            schedulingNum: total,
            pendingNum: Math.max(total - autoFill, 0),
            finishedNum: autoFill,
            unitPrice: unitPrice,
            totalPrice: (autoFill * unitPrice).toFixed(2),
            schedulingUserId: row?.schedulingUserId ?? '',
            schedulingDate: row?.schedulingDate ?? '',
        };
    });
}
 
const changeNum = (row, value) => {
    if (value > row.schedulingNum) {
        row.finishedNum = row.schedulingNum;
        proxy.$modal.msgWarning('本次生产数量不可大于排产数量')
    }
    row.pendingNum = row.schedulingNum - row.finishedNum;
    calculateTotalPrice(row);
}
 
// 计算总价
const calculateTotalPrice = (row) => {
    const quantity = Number(row.finishedNum ?? 0);
    const unitPrice = Number(row.unitPrice ?? 0);
    
    if (quantity > 0 && unitPrice > 0) {
        row.totalPrice = (quantity * unitPrice).toFixed(2);
    } else {
        row.totalPrice = '0.00';
    }
}
 
// 提交产品表单
const submitForm = () => {
    // 验证数据
    for (let i = 0; i < reportList.value.length; i++) {
        const item = reportList.value[i];
        if (!item.finishedNum || item.finishedNum <= 0) {
            proxy.$modal.msgError(`第${i + 1}行本次生产数量需大于0`);
            return;
        }
        if (item.finishedNum > item.schedulingNum) {
            proxy.$modal.msgError(`第${i + 1}行本次生产数量不可大于排产数量`);
            return;
        }
        if (!item.schedulingUserId) {
            proxy.$modal.msgError(`第${i + 1}行请选择生产人`);
            return;
        }
        if (!item.schedulingDate) {
            proxy.$modal.msgError(`第${i + 1}行请选择生产日期`);
            return;
        }
    }
 
    // 构建提交数据数组
    const payloadList = reportList.value.map(item => ({
        id: item.id,
        finishedNum: Number(item.finishedNum),
        unitPrice: Number(item.unitPrice || 0.33),
        totalPrice: Number(item.totalPrice || 0),
        schedulingUserId: item.schedulingUserId,
        schedulingDate: item.schedulingDate,
    }));
 
    if (operationType.value === "add") {
        productionReport(payloadList).then(res => {
            proxy.$modal.msgSuccess("提交成功");
            closeDia();
        }).catch(err => {
            console.error('提交失败:', err);
        })
    } else {
        // 编辑模式,逐条更新
        Promise.all(payloadList.map(item => productionReportUpdate(item)))
            .then(() => {
                proxy.$modal.msgSuccess("提交成功");
                closeDia();
            })
            .catch(err => {
                console.error('提交失败:', err);
                proxy.$modal.msgError('提交失败,请重试');
            });
    }
}
 
// 关闭弹框
const closeDia = () => {
  if (proxy.$refs.formRef) {
    proxy.resetForm("formRef");
  }
  dialogFormVisible.value = false;
  reportList.value = [];
  emit('close')
};
defineExpose({
  openDialog,
});
</script>
 
<style scoped>
 
</style>