| | |
| | | import com.rongyichuang.employee.dto.response.EmployeeResponse; |
| | | import com.rongyichuang.employee.entity.Employee; |
| | | import com.rongyichuang.employee.repository.EmployeeRepository; |
| | | import com.rongyichuang.user.service.UserService; |
| | | import com.rongyichuang.user.entity.User; |
| | | import com.rongyichuang.common.exception.BusinessException; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | |
| | | @Autowired |
| | | private EmployeeRepository employeeRepository; |
| | | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); |
| | | |
| | |
| | | // 验证输入 |
| | | validateEmployeeInput(input); |
| | | |
| | | // 处理User表逻辑 |
| | | User user; |
| | | if (input.getPassword() != null && !input.getPassword().trim().isEmpty()) { |
| | | // 有密码时,创建或更新用户(包含密码) |
| | | user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), input.getPassword()); |
| | | } else { |
| | | // 无密码时,只更新用户基本信息(不更新密码) |
| | | user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), null); |
| | | } |
| | | |
| | | Employee employee; |
| | | if (input.getId() != null) { |
| | | // 更新员工 |
| | |
| | | throw new BusinessException("PHONE_ALREADY_EXISTS", "手机号已存在"); |
| | | } |
| | | employee = new Employee(); |
| | | employee.setUserId(1L); // 临时设置,实际应该从当前登录用户获取 |
| | | } |
| | | |
| | | // 设置基本信息 |
| | |
| | | employee.setPhone(input.getPhone()); |
| | | employee.setRoleId(input.getRoleId()); |
| | | employee.setDescription(input.getDescription()); |
| | | |
| | | // 处理密码 |
| | | if (input.getPassword() != null && !input.getPassword().trim().isEmpty()) { |
| | | employee.setPassword(passwordEncoder.encode(input.getPassword())); |
| | | } |
| | | employee.setUserId(user.getId()); // 设置关联的用户ID |
| | | |
| | | Employee savedEmployee = employeeRepository.save(employee); |
| | | logger.info("员工保存成功: {}", savedEmployee.getName()); |
| | |
| | | throw new BusinessException("ROLE_REQUIRED", "角色不能为空"); |
| | | } |
| | | |
| | | // 新增员工时密码必填 |
| | | // 密码验证:新增时必填,编辑时可选 |
| | | if (input.getId() == null) { |
| | | // 新增员工时密码必填 |
| | | if (input.getPassword() == null || input.getPassword().trim().isEmpty()) { |
| | | throw new BusinessException("PASSWORD_REQUIRED", "密码不能为空"); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 如果提供了密码,验证密码格式 |
| | | if (input.getPassword() != null && !input.getPassword().trim().isEmpty()) { |
| | | if (!PASSWORD_PATTERN.matcher(input.getPassword()).matches()) { |