liu
9 小时以前 9697bac35fe2c74811afaf66f9cf28b758f3a386
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
package cn.iocoder.yudao.module.hrm.service.attendance;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmShiftPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmShiftSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmShiftDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmEmployeeScheduleMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmShiftMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * 班次定义 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class HrmShiftServiceImpl implements HrmShiftService {
 
    @Resource
    private HrmShiftMapper shiftMapper;
    @Resource
    private HrmEmployeeScheduleMapper employeeScheduleMapper;
 
    @Override
    public Long createShift(HrmShiftSaveReqVO createReqVO) {
        validateShiftNameUnique(null, createReqVO.getName());
        HrmShiftDO shift = BeanUtils.toBean(createReqVO, HrmShiftDO.class);
        if (shift.getStatus() == null) {
            shift.setStatus(0);
        }
        if (shift.getCrossDay() == null) {
            shift.setCrossDay(false);
        }
        shiftMapper.insert(shift);
        return shift.getId();
    }
 
    @Override
    public void updateShift(HrmShiftSaveReqVO updateReqVO) {
        validateShiftExists(updateReqVO.getId());
        validateShiftNameUnique(updateReqVO.getId(), updateReqVO.getName());
        HrmShiftDO updateObj = BeanUtils.toBean(updateReqVO, HrmShiftDO.class);
        if (updateObj.getCrossDay() == null) {
            updateObj.setCrossDay(false);
        }
        shiftMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteShift(Long id) {
        validateShiftExists(id);
        // 校验班次是否已被排班使用
        Long usedCount = employeeScheduleMapper.selectCountByShiftId(id);
        if (usedCount != null && usedCount > 0) {
            throw exception(SHIFT_IN_USE);
        }
        shiftMapper.deleteById(id);
    }
 
    private void validateShiftExists(Long id) {
        if (shiftMapper.selectById(id) == null) {
            throw exception(SHIFT_NOT_EXISTS);
        }
    }
 
    private void validateShiftNameUnique(Long id, String name) {
        HrmShiftDO shift = shiftMapper.selectOne(HrmShiftDO::getName, name);
        if (shift == null) {
            return;
        }
        if (id == null || !shift.getId().equals(id)) {
            throw exception(SHIFT_NAME_EXISTS);
        }
    }
 
    @Override
    public HrmShiftDO getShift(Long id) {
        return shiftMapper.selectById(id);
    }
 
    @Override
    public PageResult<HrmShiftDO> getShiftPage(HrmShiftPageReqVO pageReqVO) {
        return shiftMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<HrmShiftDO> getShiftList() {
        return shiftMapper.selectListByStatus(0);
    }
 
}