gaoluyang
19 小时以前 19aa78a505d468739a61054f07d0840477192b2d
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
<template>
  <div>
    <el-dialog
        v-model="isShow"
        title="编辑工序"
        width="400"
        @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
        <el-form-item
            label="工序名称:"
            prop="name"
            :rules="[
                {
                required: true,
                message: '请输入工序名称',
              },
              {
                max: 100,
                message: '最多100个字符',
              }
            ]">
          <el-input v-model="formState.name" />
        </el-form-item>
        <el-form-item label="工序编号" prop="no">
          <el-input v-model="formState.no"  />
        </el-form-item>
<!--        <el-form-item label="工资定额" prop="salaryQuota">-->
<!--          <el-input v-model="formState.salaryQuota" type="number" :step="0.001" />-->
<!--        </el-form-item>-->
        <el-form-item label="是否质检" prop="isQuality">
          <el-switch v-model="formState.isQuality" :active-value="true" inactive-value="false"/>
        </el-form-item>
        <el-form-item label="报工权限" prop="userPower" :rules="[{ required: true, message: '请选择报工权限', trigger: 'change' }]">
          <el-tree-select
            v-model="formState.userPower"
            :data="staffList"
            :props="treeProps"
            placeholder="请选择人员"
            multiple
            show-checkbox
            collapse-tags
            collapse-tags-tooltip
            style="width: 100%"
            node-key="id"
            :render-after-expand="false"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formState.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确认</el-button>
          <el-button @click="closeModal">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, computed, getCurrentInstance, watch, onMounted } from "vue";
import {update} from "@/api/productionManagement/productionProcess.js";
import { listDeptUserTree } from "@/api/basicData/productProcess.js";
 
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
 
  record: {
    type: Object,
    required: true,
  }
});
 
const emit = defineEmits(['update:visible', 'completed']);
 
// 响应式数据(替代选项式的 data)
const formState = ref({
  id: props.record.id,
  name: props.record.name,
  no: props.record.no,
  remark: props.record.remark,
  salaryQuota: props.record.salaryQuota,
  isQuality: props.record.isQuality,
  userPower: props.record.userPower ? props.record.userPower.split(',') : [],
});
 
const staffList = ref([]);
 
const treeProps = {
  label: 'label',
  children: 'children',
};
 
const isShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emit('update:visible', val);
  },
});
 
// 监听 record 变化,更新表单数据
watch(() => props.record, (newRecord) => {
  if (newRecord && isShow.value) {
    formState.value = {
      id: newRecord.id,
      name: newRecord.name || '',
      no: newRecord.no || '',
      remark: newRecord.remark || '',
      salaryQuota: newRecord.salaryQuota || '',
      isQuality: newRecord.isQuality,
      userPower: [],
    };
    
    // 等待 staffList 加载完成后,再查找用户ID
    if (staffList.value.length > 0) {
      setUserPowerIds(newRecord.userPower);
    } else {
      // staffList 还没加载,等待一下再查找
      const checkStaffList = () => {
        if (staffList.value.length > 0) {
          setUserPowerIds(newRecord.userPower);
        } else {
          setTimeout(checkStaffList, 100);
        }
      };
      checkStaffList();
    }
  }
}, { immediate: true, deep: true });
 
// 根据用户名查找用户ID
const setUserPowerIds = (userPowerStr) => {
  const userPowerNames = userPowerStr ? userPowerStr.split(',') : [];
  const userPowerIds = [];
  const findUserIds = (nodes, targetName) => {
    nodes.forEach(node => {
      if (node.isUser && node.label === targetName) {
        userPowerIds.push(node.id);
      }
      if (node.children && node.children.length > 0) {
        findUserIds(node.children, targetName);
      }
    });
  };
  userPowerNames.forEach(name => {
    findUserIds(staffList.value, name);
  });
  formState.value.userPower = userPowerIds;
};
 
let { proxy } = getCurrentInstance()
 
const closeModal = () => {
  isShow.value = false;
};
 
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      const userPowerNames = getAllUserNamesFromSelection(formState.value.userPower);
      
      const submitData = {
        ...formState.value,
        userPower: userPowerNames.join(',')
      };
      update(submitData).then(res => {
        // 关闭模态框
        isShow.value = false;
        // 告知父组件已完成
        emit('completed');
        proxy.$modal.msgSuccess("提交成功");
      })
    }
  })
};
 
const findUserById = (userId) => {
  const findInTree = (nodes) => {
    for (const node of nodes) {
      if (node.id === userId) {
        return node;
      }
      if (node.children && node.children.length > 0) {
        const found = findInTree(node.children);
        if (found) return found;
      }
    }
    return null;
  };
  return findInTree(staffList.value);
};
 
const getStaffList = () => {
  listDeptUserTree().then(res => {
    const buildTree = (nodes) => {
      return nodes.map(node => {
        const deptNode = {
          id: `dept_${node.deptId}`,
          label: node.deptName,
          isUser: false,
          children: []
        };
        
        if (node.userList && node.userList.length > 0) {
          node.userList.forEach(user => {
            deptNode.children.push({
              id: user.userId,
              label: user.nickName || user.userName,
              isUser: true,
              userName: user.userName,
              nickName: user.nickName
            });
          });
        }
        
        if (node.childrenList && node.childrenList.length > 0) {
          const childNodes = buildTree(node.childrenList);
          deptNode.children = deptNode.children.concat(childNodes);
        }
        
        return deptNode;
      });
    };
    staffList.value = buildTree(res.data || []);
  }).catch(() => {
    staffList.value = [];
  });
};
 
const getAllUserNamesFromSelection = (selectedIds) => {
  const names = [];
  const processNode = (node) => {
    if (selectedIds.includes(node.id)) {
      if (node.isUser) {
        names.push(node.label);
      } else {
        if (node.children && node.children.length > 0) {
          node.children.forEach(child => {
            if (child.isUser) {
              names.push(child.label);
            }
          });
        }
      }
    }
    if (node.children && node.children.length > 0) {
      node.children.forEach(child => processNode(child));
    }
  };
  staffList.value.forEach(node => processNode(node));
  return [...new Set(names)];
};
 
onMounted(() => {
  getStaffList();
});
 
defineExpose({
  closeModal,
  handleSubmit,
  isShow,
});
</script>