gaoluyang
2025-11-14 7efe917a161f7c6965cfa75ac5ad7e664dcc1eb4
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<template>
  <div class="app-container scheduling-container">
    <!-- 筛选区域 -->
    <div class="filter-section">
      <el-form :inline="true" :model="filterForm" class="filter-form">
        <el-form-item label="员工姓名:">
          <el-input
              v-model="filterForm.staffName"
              placeholder="请输入员工姓名"
              clearable
              style="width: 150px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">
            搜索
          </el-button>
          <el-button @click="resetFilter">
            重置
          </el-button>
          <el-button @click="handleExport">
            导出
          </el-button>
          <el-button type="primary" @click="openScheduleDialog('add')">
          <el-icon><Plus/></el-icon>
          新增排班
        </el-button>
        </el-form-item>
      </el-form>
    </div>
 
    <!-- 排班表格 -->
    <div class="table-section">
      <el-table
          :data="scheduleList"
          border
          :loading="tableLoading"
          stripe
          style="width: 100%"
          height="calc(100vh - 18.5em)"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="staffName" label="员工姓名"/>
        <!-- <el-table-column prop="staffNo" label="员工工号" width="100"/> -->
        <!-- <el-table-column prop="department" label="部门" width="120">
          <template #default="scope">
              {{ (department_type.find(i => i.value === String(scope.row.department)) || {}).label }}
          </template>
        </el-table-column> -->
        <el-table-column prop="shiftType" label="班次类型" width="120">
          <template #default="scope">
            <el-tag :type="getShiftTagType(scope.row.shiftType)">
              {{ (shift_type.find(i => i.value === String(scope.row.shiftType)) || {}).label || '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="workDate" label="工作日期" width="120"/> -->
        <el-table-column prop="workStartTime" label="开始时间"/>
        <el-table-column prop="workEndTime" label="结束时间"/>
        <el-table-column prop="lunchTime" label="午休时间(h)">
          <template #default="scope">
            {{ scope.row.lunchTime }}小时
          </template>
        </el-table-column>
        <!-- <el-table-column prop="workHours" label="工作时长" width="100">
          <template #default="scope">
            {{ scope.row.workHours }}小时
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusTagType(scope.row.status)">
              {{ (schedule_status.find(i => i.value === String(scope.row.status)) || {}).label }}
            </el-tag>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="remark" label="备注" min-width="150"/> -->
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="scope">
            <el-button
                            link
                            type="primary"
                @click="openScheduleDialog('edit', scope.row)"
            >
              编辑
            </el-button>
            <el-button
                            link
                            type="danger"
                @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
        <pagination
            v-if="tableCount > 0"
            :total="tableCount"
            :page="filterForm.current"
            :limit="filterForm.size"
            @pagination="paginationChange"
        />
    </div>
 
    <!-- 批量操作 -->
    <div class="batch-actions" v-if="selectedRows.length > 0">
      <el-button
          type="danger"
          @click="handleBatchDelete"
          :disabled="selectedRows.length === 0"
      >
        批量删除 ({{ selectedRows.length }})
      </el-button>
    </div>
 
    <!-- 排班新增/编辑对话框 -->
    <el-dialog
        v-model="scheduleDialog"
        :title="dialogType === 'add' ? '新增排班' : '编辑排班'"
        width="700px"
        @close="closeScheduleDialog"
    >
      <el-form
          :model="scheduleForm"
          :rules="scheduleRules"
          ref="scheduleFormRef"
          label-width="120px"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="员工姓名:" prop="staffIds">
          <el-select v-model="scheduleForm.staffIds" placeholder="请选择员工姓名" style="width: 100%"
                         multiple filterable collapse-tags-tooltip
                         @change="handleSelectStaff">
            <el-option v-for="item in personList" :label="item.nickName" :value="item.userId" :key="item.userId"/>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
 
        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="员工工号:" prop="staffNo">
              <el-input :disabled="true" v-model="scheduleForm.staffNo" placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row> -->
 
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="班次类型:" prop="shiftType">
              <el-select v-model="scheduleForm.shiftType" placeholder="请选择班次类型" style="width: 100%">
                <el-option v-for="item in shift_type" :label="item.label" :value="item.value" :key="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
 
        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="部门:" prop="department">
              <el-select v-model="scheduleForm.department" placeholder="请选择部门" style="width: 100%">
                <el-option v-for="item in department_type" :label="item.label" :value="item.value" :key="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次类型:" prop="shiftType">
              <el-select v-model="scheduleForm.shiftType" placeholder="请选择班次" style="width: 100%">
                <el-option v-for="item in shift_type" :label="item.label" :value="item.value" :key="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
 
        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工作日期:" prop="workDate">
              <el-date-picker
                  v-model="scheduleForm.workDate"
                  type="date"
                  placeholder="选择工作日期"
                  style="width: 100%"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态:" prop="status">
              <el-select v-model="scheduleForm.status" placeholder="请选择状态" style="width: 100%">
                <el-option v-for="item in schedule_status" :label="item.label" :value="item.value" :key="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
 
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始时间:" prop="workStartTime">
              <el-time-picker
                  v-model="scheduleForm.workStartTime"
                  placeholder="选择开始时间"
                  style="width: 100%"
                  format="HH:mm"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间:" prop="workEndTime">
              <el-time-picker
                  v-model="scheduleForm.workEndTime"
                  placeholder="选择结束时间"
                  style="width: 100%"
                  format="HH:mm"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
 
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="午休时间(h):" prop="lunchTime">
              <el-input-number
                  v-model="scheduleForm.lunchTime"
                  :min="0"
                  :max="8"
                  :step="0.5"
                  placeholder="请输入午休时间"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
 
        <!-- <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注:" prop="remark">
              <el-input
                  v-model="scheduleForm.remark"
                  type="textarea"
                  :rows="3"
                  placeholder="请输入备注信息"
              />
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
 
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitScheduleForm">确认</el-button>
          <el-button @click="closeScheduleDialog">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import {ref, reactive, computed, onMounted, getCurrentInstance, watch} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {useDict} from "@/utils/dict.js"
import {Plus, Download, Search, Refresh} from '@element-plus/icons-vue'
import {save, del, delByIds, listPage} from "@/api/personnelManagement/scheduling.js"
import {getStaffOnJob} from "@/api/personnelManagement/onboarding.js";
import dayjs from "dayjs";
import pagination from "@/components/PIMTable/Pagination.vue";
import {listUser} from "@/api/system/user.js";
 
const { proxy } = getCurrentInstance();
 
const tableCount = ref(0)
// 响应式数据
const scheduleDialog = ref(false)
const dialogType = ref('add')
const selectedRows = ref([])
const scheduleFormRef = ref()
 
// 筛选表单
const filterForm = reactive({
  staffName: '',
  current:1,
  size: 10
})
 
// 排班表单
const scheduleForm = reactive({
  id: '',
  staffIds: [],
  // staffNo: '',
  // department: '',
  shiftType: '',
  // workDate: '',
  workStartTime: '',
  workEndTime: '',
  lunchTime: 3,
  // workStartTime: '',
  // workEndTime: '',
  // workHours: 0,
  // status: '',
  // remark: ''
})
 
// 表单验证规则
const scheduleRules = reactive({
  staffIds: [{required: true, message: '请选择员工', trigger: 'change'}],
  // department: [{required: true, message: '请选择部门', trigger: 'change'}],
  shiftType: [{required: true, message: '请选择班次类型', trigger: 'change'}],
  // workDate: [{required: true, message: '请选择工作日期', trigger: 'change'}],
  workStartTime: [{required: true, message: '请选择开始时间', trigger: 'change'}],
  workEndTime: [{required: true, message: '请选择结束时间', trigger: 'change'}],
  lunchTime: [{required: true, message: '请输入午休时间', trigger: 'blur'}],
  // status: [{required: true, message: '请选择状态', trigger: 'change'}]
})
const tableLoading = ref(false)
 
//字典
const {department_type, schedule_status, shift_type} = useDict("department_type", "schedule_status", "shift_type")
// 人员列表
const personList = ref([]);
 
// 模拟排班数据
const scheduleList = ref([])
 
 
/**
 * 获取当前在职人员列表
 */
const getPersonList = () => {
    listUser().then(res => {
        personList.value = res.rows
    })
};
const paginationChange = (obj) => {
  filterForm.current = obj.page;
  filterForm.size = obj.limit;
  handleFilter();
};
 
const handleSelectStaff = (val) => {
  // 多选员工,不再自动设置员工工号
  // let obj = personList.value.find(item => item.id === val)
  // scheduleForm.staffNo = obj.staffNo
}
 
// 获取班次标签类型
const getShiftTagType = (shiftType) => {
  const typeMap = Object.fromEntries(shift_type.value.map(i => [i.value, i.elTagType]))
  return typeMap[shiftType] || 'info'
}
 
// 获取状态标签类型
const getStatusTagType = (status) => {
  const typeMap = Object.fromEntries(schedule_status.value.map(i => [i.value, i.elTagType]))
  return typeMap[status] || 'info'
}
 
// 筛选
const handleFilter = async () => {
  tableLoading.value = true
  let searchForm = {
    ...filterForm
  }
  let resp = await listPage(searchForm)
  tableCount.value = resp.data.total
  scheduleList.value = resp.data.records.map(it => {
    return {
      ...it,
      // 保存原始时间格式用于编辑
      'originalWorkStartTime': it.workStartTime,
      'originalWorkEndTime': it.workEndTime,
      // 格式化时间用于表格显示
      'workStartTime': dayjs(it.workStartTime).format('HH:mm'),
      'workEndTime': dayjs(it.workEndTime).format('HH:mm'),
    }
  })
  tableLoading.value = false
 
}
 
// 重置筛选
const resetFilter = () => {
  filterForm.staffName = ''
}
 
// 打开排班对话框
const openScheduleDialog = (type, data) => {
  dialogType.value = type
  scheduleDialog.value = true
  getPersonList()
  if (type === 'edit' && data) {
    // 编辑模式,复制数据,将员工ID字符串转换为数组格式,并处理时间字段
    Object.assign(scheduleForm, {
      ...data,
        lunchTime: Number(data.lunchTime),
      staffIds: data.staffId ? data.staffId.split(',').map(id => parseInt(id)) : [],
      // 使用原始时间字符串,因为表格中显示的是格式化后的HH:mm
      workStartTime: data.originalWorkStartTime || '',
      workEndTime: data.originalWorkEndTime || ''
    })
  } else {
      // 新增模式,重置表单
      Object.keys(scheduleForm).forEach(key => {
        if (key === 'staffIds') {
          scheduleForm[key] = []
        } else if (key === 'lunchTime') {
          scheduleForm[key] = 3
        } else {
          scheduleForm[key] = ''
        }
      })
      // scheduleForm.status = '已安排'
      // scheduleForm.workDate = new Date().toISOString().split('T')[0]
    }
}
 
// 关闭排班对话框
const closeScheduleDialog = () => {
  scheduleFormRef.value?.resetFields()
  scheduleDialog.value = false
}
 
// 计算工作时长
const calculateWorkHours = () => {
  if (!scheduleForm.workStartTime || !scheduleForm.workEndTime) {
    return;
  }
 
  try {
    // 使用dayjs正确解析时间
    const startDayjs = dayjs(scheduleForm.workStartTime);
    const endDayjs = dayjs(scheduleForm.workEndTime);
 
    if (!startDayjs.isValid() || !endDayjs.isValid()) {
      return;
    }
 
    const startDateTime = startDayjs.toDate();
    const endDateTime = endDayjs.toDate();
 
    // 处理跨天情况(结束时间早于开始时间)
    if (endDateTime < startDateTime) {
      // 跨天,将结束日期加一天
      endDateTime.setDate(endDateTime.getDate() + 1);
    }
 
    // 计算工作时长(小时)
    const diffMs = endDateTime - startDateTime;
    const diffHours = diffMs / (1000 * 60 * 60);
    scheduleForm.workHours = Math.round(diffHours * 100) / 100;
  } catch (error) {
    console.error('时间计算错误:', error);
  }
}
 
// 监听时间字段变化,自动计算工作时长
watch(
  () => [scheduleForm.workStartTime, scheduleForm.workEndTime],
  () => {
    calculateWorkHours()
  },
  { deep: true }
)
 
// 提交排班表单
const submitScheduleForm = async () => {
  const valid = await scheduleFormRef.value.validate()
  if (!valid) return
 
  // 由于员工是多选,需要为每个选中的员工创建排班记录
  const selectedStaffIds = scheduleForm.staffIds || []
  
  if (selectedStaffIds.length === 0) {
    ElMessage.warning('请至少选择一个员工')
    return
  }
 
  try {
    // 获取选中的员工姓名列表
    const selectedStaffNames = selectedStaffIds.map(staffId => {
      const staff = personList.value.find(item => item.userId === staffId)
      return staff ? staff.nickName : ''
    }).filter(name => name !== '')
    
    // 将员工姓名组装成逗号分隔的字符串
    const staffNameString = selectedStaffNames.join(',')
    
    // 创建排班记录,将员工姓名保存为字符串格式
    const newSchedule = {
      ...scheduleForm,
      staffName: staffNameString,
      staffId: selectedStaffIds.join(','), // 将员工ID也保存为逗号分隔的字符串
      // 设置其他必要字段的默认值
      staffNo: '', // 可以根据需要从personList中获取
      department: '',
      shiftType: scheduleForm.shiftType,
      workDate: '',
      status: '',
      remark: ''
    }
    
    await save(newSchedule)
    
    ElMessage.success(`成功为 ${selectedStaffNames.length} 个员工创建排班`)
 
    handleFilter()
    closeScheduleDialog()
  } catch (err) {
    ElMessage.error('保存失败')
  }
}
 
// 删除排班
const handleDelete = (row) => {
  ElMessageBox.confirm(
      `确定要删除 ${row.staffName} 的排班记录吗?`,
      '删除提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    del(row.id)
    ElMessage.success('删除成功')
    handleFilter()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}
 
// 批量删除
const handleBatchDelete = () => {
  if (selectedRows.value.length === 0) {
    ElMessage.warning('请选择要删除的记录')
    return
  }
 
  ElMessageBox.confirm(
      `确定要删除选中的 ${selectedRows.value.length} 条排班记录吗?`,
      '批量删除提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    delByIds(selectedRows.value.map(item => item.id))
    handleFilter()
    ElMessage.success('批量删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}
 
// 选择变化事件
const handleSelectionChange = (selection) => {
  selectedRows.value = selection
}
 
// 导出
const handleExport = () => {
  let searchForm = {
    ...filterForm
  }
  proxy.download('/staff/staffScheduling/export', {}, '人员排班.xlsx')
}
 
// 生命周期
onMounted(() => {
  // 页面初始化
  handleFilter()
})
</script>
 
<style scoped>
.scheduling-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}
 
.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
}
 
.page-header h2 {
  color: white;
  margin-bottom: 10px;
  font-size: 28px;
  font-weight: 600;
}
 
.page-header p {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  margin: 0 0 15px 0;
}
 
.header-controls {
  display: flex;
  justify-content: center;
  gap: 15px;
}
 
.filter-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
 
.filter-form {
  margin: 0;
}
 
.table-section {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}
 
.batch-actions {
  background: white;
  padding: 15px 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
 
.dialog-footer {
  text-align: right;
}
 
:deep(.el-form-item__label) {
  font-weight: 500;
  color: #303133;
}
 
:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}
 
:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}
 
:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset;
}
 
/* 响应式设计 */
@media (max-width: 768px) {
  .scheduling-container {
    padding: 10px;
  }
 
  .page-header {
    padding: 15px;
  }
 
  .page-header h2 {
    font-size: 24px;
  }
 
  .header-controls {
    flex-direction: column;
    gap: 10px;
  }
}
 
@media (max-width: 768px) {
  .filter-form .el-form-item {
    margin-bottom: 10px;
  }
}
</style>