|
@@ -12,16 +12,19 @@ import cn.iocoder.yudao.module.attendance.dal.dataobject.leave.AttendanceLeaveDO
|
|
|
import cn.iocoder.yudao.module.attendance.dal.mysql.leave.AttendanceLeaveMapper;
|
|
|
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
|
|
|
import cn.iocoder.yudao.module.employee.api.dto.EmployeeRespDTO;
|
|
|
+import cn.iocoder.yudao.module.employee.api.dto.EmployeeSaveReqDTO;
|
|
|
import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
import java.util.List;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.module.attendance.enums.ErrorCodeConstants.ATTENDANCE_LEAVE_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.attendance.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 请假信息 Service 实现类
|
|
@@ -40,6 +43,7 @@ public class AttendanceLeaveServiceImpl implements AttendanceLeaveService {
|
|
|
private FileApi fileApi;
|
|
|
|
|
|
@Override
|
|
|
+ @DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
public Long createLeave(AttendanceLeaveSaveReqVO createReqVO) {
|
|
|
// 获取用户信息和租户ID
|
|
|
LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
|
@@ -66,6 +70,28 @@ public class AttendanceLeaveServiceImpl implements AttendanceLeaveService {
|
|
|
leaveMapper.insert(leave);
|
|
|
// 保存业务uuid到附件中
|
|
|
fileApi.updateFileBiz(createReqVO.getFileIdList(), infoId);
|
|
|
+ if ("年假".equals(createReqVO.getLeaveType())) {
|
|
|
+ // 获取剩余年假和申请天数
|
|
|
+ BigDecimal remainingAnnualLeave = employee.getRemainingAnnualLeave();
|
|
|
+ String requestDayStr = createReqVO.getDay(); // 更清晰的变量名
|
|
|
+ BigDecimal requestDays = new BigDecimal(requestDayStr); // 假设getDay返回的是可以解析为double的字符串
|
|
|
+
|
|
|
+ // 检查剩余年假是否足够
|
|
|
+ if (remainingAnnualLeave.compareTo(requestDays) < 0) {
|
|
|
+ throw exception(ATTENDANCE_EMAINING_ANNUAL_LEAVE_IS_INSUFFICIENT);
|
|
|
+ }
|
|
|
+ // 如果剩余年假足够,进行扣减操作
|
|
|
+ employee.setRemainingAnnualLeave(remainingAnnualLeave.subtract(requestDays));
|
|
|
+ employee.setUsedAnnualLeave(employee.getUsedAnnualLeave().add(requestDays));
|
|
|
+ // 创建一个DTO来更新员工信息(假设EmployeeSaveReqDTO已经包含了所有必要的字段)
|
|
|
+ EmployeeSaveReqDTO updateObj = BeanUtils.toBean(employee, EmployeeSaveReqDTO.class);
|
|
|
+ // 更新员工信息
|
|
|
+ try {
|
|
|
+ employeeApi.updateEmployee(updateObj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(ATTENDANCE_UPDATING_EMPLOYEE_INFO_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 返回
|
|
|
return leave.getId();
|
|
|
}
|
|
@@ -79,13 +105,86 @@ public class AttendanceLeaveServiceImpl implements AttendanceLeaveService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
public void updateLeave(AttendanceLeaveSaveReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
validateLeaveExists(updateReqVO.getId());
|
|
|
- // 保存业务uuid到附件中
|
|
|
- fileApi.updateFileBiz(updateReqVO.getFileIdList(), updateReqVO.getLeaveId());
|
|
|
+ // 查询原数据
|
|
|
+ AttendanceLeaveDO oldLeave = leaveMapper.selectById(updateReqVO.getId());
|
|
|
// 请假人信息
|
|
|
EmployeeRespDTO employee = employeeApi.getEmployeeById(updateReqVO.getEmployeeId());
|
|
|
+ if (oldLeave.getEmployeeId().equals(employee.getId())) {// 员工ID不变
|
|
|
+ if ("年假".equals(oldLeave.getLeaveType()) && "年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+ // 新旧都是年假,对比天数进行增减
|
|
|
+ BigDecimal oldDays = new BigDecimal(oldLeave.getDay());
|
|
|
+ BigDecimal newDays = new BigDecimal(updateReqVO.getDay());
|
|
|
+ BigDecimal differenceDays = newDays.subtract(oldDays);// 差值
|
|
|
+ // 获取剩余年假和申请天数
|
|
|
+ BigDecimal remainingAnnualLeave = employee.getRemainingAnnualLeave();
|
|
|
+ if (remainingAnnualLeave.compareTo(differenceDays) < 0) {
|
|
|
+ throw exception(ATTENDANCE_EMAINING_ANNUAL_LEAVE_IS_INSUFFICIENT);
|
|
|
+ }
|
|
|
+ // 如果剩余年假足够,进行扣减操作
|
|
|
+ employee.setRemainingAnnualLeave(remainingAnnualLeave.subtract(differenceDays));
|
|
|
+ employee.setUsedAnnualLeave(employee.getUsedAnnualLeave().add(differenceDays));
|
|
|
+ EmployeeSaveReqDTO updateObj = BeanUtils.toBean(employee, EmployeeSaveReqDTO.class);
|
|
|
+ // 更新员工信息
|
|
|
+ try {
|
|
|
+ employeeApi.updateEmployee(updateObj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(ATTENDANCE_UPDATING_EMPLOYEE_INFO_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!"年假".equals(oldLeave.getLeaveType()) && "年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+ // 新的是年假、旧的不是
|
|
|
+ BigDecimal remainingAnnualLeave = employee.getRemainingAnnualLeave();
|
|
|
+ String requestDayStr = updateReqVO.getDay();
|
|
|
+ BigDecimal requestDays = new BigDecimal(requestDayStr);
|
|
|
+ // 检查剩余年假是否足够
|
|
|
+ if (remainingAnnualLeave.compareTo(requestDays) < 0) {
|
|
|
+ throw exception(ATTENDANCE_EMAINING_ANNUAL_LEAVE_IS_INSUFFICIENT);
|
|
|
+ }
|
|
|
+ // 如果剩余年假足够,进行扣减操作
|
|
|
+ employee.setRemainingAnnualLeave(remainingAnnualLeave.subtract(requestDays));
|
|
|
+ employee.setUsedAnnualLeave(employee.getUsedAnnualLeave().add(requestDays));
|
|
|
+ EmployeeSaveReqDTO updateObj = BeanUtils.toBean(employee, EmployeeSaveReqDTO.class);
|
|
|
+ // 更新员工信息
|
|
|
+ try {
|
|
|
+ employeeApi.updateEmployee(updateObj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(ATTENDANCE_UPDATING_EMPLOYEE_INFO_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ("年假".equals(oldLeave.getLeaveType()) && !"年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+ // 旧的是年假,新的不是,归还年假天数
|
|
|
+ BigDecimal remainingAnnualLeave = employee.getRemainingAnnualLeave();
|
|
|
+ BigDecimal oldDays = new BigDecimal(oldLeave.getDay());
|
|
|
+ employee.setRemainingAnnualLeave(remainingAnnualLeave.add(oldDays));
|
|
|
+ employee.setUsedAnnualLeave(employee.getUsedAnnualLeave().subtract(oldDays));
|
|
|
+ // 创建一个DTO来更新员工信息(假设EmployeeSaveReqDTO已经包含了所有必要的字段)
|
|
|
+ EmployeeSaveReqDTO updateObj = BeanUtils.toBean(employee, EmployeeSaveReqDTO.class);
|
|
|
+ // 更新员工信息
|
|
|
+ try {
|
|
|
+ employeeApi.updateEmployee(updateObj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(ATTENDANCE_UPDATING_EMPLOYEE_INFO_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+// else {// 员工ID变化
|
|
|
+// if ("年假".equals(oldLeave.getLeaveType()) && "年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+// // 新旧都是年假
|
|
|
+// }
|
|
|
+// if (!"年假".equals(oldLeave.getLeaveType()) && "年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+// // 新的是年假、旧的不是
|
|
|
+// }
|
|
|
+// if ("年假".equals(oldLeave.getLeaveType()) && !"年假".equals(updateReqVO.getLeaveType())) {
|
|
|
+// // 旧的是年假,新的不是
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ // 保存业务uuid到附件中
|
|
|
+ fileApi.updateFileBiz(updateReqVO.getFileIdList(), updateReqVO.getLeaveId());
|
|
|
updateReqVO.setEmployeeId(employee.getId());
|
|
|
updateReqVO.setEmployeeName(employee.getName());
|
|
|
updateReqVO.setEmployeePhone(employee.getPhone());
|
|
@@ -97,9 +196,27 @@ public class AttendanceLeaveServiceImpl implements AttendanceLeaveService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
public void deleteLeave(Long id) {
|
|
|
// 校验存在
|
|
|
validateLeaveExists(id);
|
|
|
+ // 查询原数据
|
|
|
+ AttendanceLeaveDO oldLeave = leaveMapper.selectById(id);
|
|
|
+ // 请假人信息
|
|
|
+ EmployeeRespDTO employee = employeeApi.getEmployeeById(oldLeave.getEmployeeId());
|
|
|
+ if ("年假".equals(oldLeave.getLeaveType())) {
|
|
|
+ BigDecimal remainingAnnualLeave = employee.getRemainingAnnualLeave();
|
|
|
+ BigDecimal oldDays = new BigDecimal(oldLeave.getDay());
|
|
|
+ employee.setRemainingAnnualLeave(remainingAnnualLeave.add(oldDays));
|
|
|
+ employee.setUsedAnnualLeave(employee.getUsedAnnualLeave().subtract(oldDays));
|
|
|
+ EmployeeSaveReqDTO updateObj = BeanUtils.toBean(employee, EmployeeSaveReqDTO.class);
|
|
|
+ // 更新员工信息
|
|
|
+ try {
|
|
|
+ employeeApi.updateEmployee(updateObj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(ATTENDANCE_UPDATING_EMPLOYEE_INFO_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 删除
|
|
|
leaveMapper.deleteById(id);
|
|
|
}
|