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 getShiftPage(HrmShiftPageReqVO pageReqVO) { return shiftMapper.selectPage(pageReqVO); } @Override public List getShiftList() { return shiftMapper.selectListByStatus(0); } }