| New file |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.*; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.domain.UserSyncDTO; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import com.ruoyi.system.service.IUserSyncService; |
| | | import com.ruoyi.system.service.IUserSyncDataService; |
| | | |
| | | /** |
| | | * 用户同步Service业务层处理 |
| | | * |
| | | * 职责:接收用户数据,写入 MySQL 数据库 |
| | | * 数据来源: |
| | | * 1. 内部查询:调用 IUserSyncDataService 从 SQL Server 查询 |
| | | * 2. 外部传入:接收已查询好的数据 |
| | | * |
| | | * @author ruoyi |
| | | * @date 2025-10-18 |
| | | */ |
| | | @Service |
| | | public class UserSyncServiceImpl implements IUserSyncService |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(UserSyncServiceImpl.class); |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 同步OA用户数据(使用外部传入的数据源) |
| | | * |
| | | * 执行流程: |
| | | * 1. 接收外部传入的 UserSyncDTO 列表 |
| | | * 2. 查询 MySQL 中的部门信息(sysDeptMapper 使用默认 MySQL 数据源) |
| | | * 3. 写入 MySQL 数据库(sysUserMapper 使用默认 MySQL 数据源) |
| | | * |
| | | * @param oaUsers 外部传入的OA用户数据列表 |
| | | * @return 同步结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AjaxResult syncOaUsers(List<UserSyncDTO> oaUsers) |
| | | { |
| | | try |
| | | { |
| | | if (oaUsers == null || oaUsers.isEmpty()) |
| | | { |
| | | return AjaxResult.warn("传入的用户数据为空"); |
| | | } |
| | | |
| | | log.info("开始同步 {} 条OA用户数据到 MySQL 数据库...", oaUsers.size()); |
| | | |
| | | int createdCount = 0; |
| | | int updatedCount = 0; |
| | | int skippedCount = 0; |
| | | int errorCount = 0; |
| | | |
| | | // 处理每一条用户数据 |
| | | for (UserSyncDTO dto : oaUsers) |
| | | { |
| | | try |
| | | { |
| | | // 验证必填字段 |
| | | if (StringUtils.isEmpty(dto.getUserName()) || StringUtils.isEmpty(dto.getNickName())) |
| | | { |
| | | log.warn("用户数据不完整,跳过: oaUserId={}", dto.getOaUserId()); |
| | | skippedCount++; |
| | | continue; |
| | | } |
| | | |
| | | // 3. 根据 department_id 查找对应的 dept_id(从 MySQL 中查询) |
| | | Long deptId = null; |
| | | if (dto.getDepartmentId() != null) |
| | | { |
| | | SysDept dept = sysDeptMapper.selectDeptByDepartmentId(dto.getDepartmentId()); |
| | | if (dept != null) |
| | | { |
| | | deptId = dept.getDeptId(); |
| | | } |
| | | else |
| | | { |
| | | log.warn("未找到对应的部门: departmentId={}, 用户: {}", |
| | | dto.getDepartmentId(), dto.getUserName()); |
| | | } |
| | | } |
| | | |
| | | // 4. 检查用户是否已存在(根据oa_user_id) |
| | | SysUser existingUser = sysUserMapper.selectUserByOaUserId(dto.getOaUserId()); |
| | | |
| | | if (existingUser != null) |
| | | { |
| | | // 用户已存在,更新信息 |
| | | updateExistingUser(existingUser, dto, deptId); |
| | | updatedCount++; |
| | | log.info("更新用户: {} ({}), oaUserId: {}", |
| | | dto.getNickName(), dto.getUserName(), dto.getOaUserId()); |
| | | } |
| | | else |
| | | { |
| | | // 检查用户名是否已被占用 |
| | | SysUser userByName = sysUserMapper.checkUserNameUnique(dto.getUserName()); |
| | | if (userByName != null) |
| | | { |
| | | // 用户名已存在,更新其oa_user_id |
| | | userByName.setOaUserId(dto.getOaUserId()); |
| | | userByName.setNickName(dto.getNickName()); |
| | | if (deptId != null) |
| | | { |
| | | userByName.setDeptId(deptId); |
| | | } |
| | | if (StringUtils.isNotEmpty(dto.getEmail())) |
| | | { |
| | | userByName.setEmail(dto.getEmail()); |
| | | } |
| | | if (StringUtils.isNotEmpty(dto.getPhonenumber())) |
| | | { |
| | | userByName.setPhonenumber(dto.getPhonenumber()); |
| | | } |
| | | if (StringUtils.isNotEmpty(dto.getSex())) |
| | | { |
| | | userByName.setSex(dto.getSex()); |
| | | } |
| | | userByName.setUpdateBy("sync"); |
| | | sysUserMapper.updateUser(userByName); |
| | | updatedCount++; |
| | | log.info("更新已存在用户名的用户: {} ({}), 设置oaUserId: {}", |
| | | dto.getNickName(), dto.getUserName(), dto.getOaUserId()); |
| | | } |
| | | else |
| | | { |
| | | // 创建新用户 |
| | | createNewUser(dto, deptId); |
| | | createdCount++; |
| | | log.info("创建新用户: {} ({}), oaUserId: {}, deptId: {}", |
| | | dto.getNickName(), dto.getUserName(), dto.getOaUserId(), deptId); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | errorCount++; |
| | | log.error("同步用户失败: oaUserId={}, userName={}, error: {}", |
| | | dto.getOaUserId(), dto.getUserName(), e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | String message = String.format("同步完成!创建用户: %d, 更新用户: %d, 跳过: %d, 失败: %d", |
| | | createdCount, updatedCount, skippedCount, errorCount); |
| | | log.info(message); |
| | | |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("created", createdCount); |
| | | result.put("updated", updatedCount); |
| | | result.put("skipped", skippedCount); |
| | | result.put("error", errorCount); |
| | | result.put("totalProcessed", oaUsers.size()); |
| | | |
| | | return AjaxResult.success(message, result); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("同步用户数据失败", e); |
| | | return AjaxResult.error("同步失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新已存在的用户 |
| | | */ |
| | | private void updateExistingUser(SysUser existingUser, UserSyncDTO dto, Long deptId) |
| | | { |
| | | existingUser.setNickName(dto.getNickName()); |
| | | |
| | | if (deptId != null) |
| | | { |
| | | existingUser.setDeptId(deptId); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getEmail())) |
| | | { |
| | | existingUser.setEmail(dto.getEmail()); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getPhonenumber())) |
| | | { |
| | | // 校验手机号是否已被其他用户使用(排除自己) |
| | | SysUser phoneCheck = sysUserMapper.checkPhoneUnique(dto.getPhonenumber()); |
| | | if (StringUtils.isNotNull(phoneCheck) && !phoneCheck.getUserId().equals(existingUser.getUserId())) |
| | | { |
| | | log.warn("更新用户 {} 失败,手机号 {} 已被用户 {} 使用,跳过手机号更新", |
| | | existingUser.getUserName(), dto.getPhonenumber(), phoneCheck.getUserName()); |
| | | } |
| | | else |
| | | { |
| | | existingUser.setPhonenumber(dto.getPhonenumber()); |
| | | } |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getSex())) |
| | | { |
| | | existingUser.setSex(dto.getSex()); |
| | | } |
| | | |
| | | existingUser.setUpdateBy("sync"); |
| | | sysUserMapper.updateUser(existingUser); |
| | | } |
| | | |
| | | /** |
| | | * 创建新用户 |
| | | */ |
| | | private void createNewUser(UserSyncDTO dto, Long deptId) |
| | | { |
| | | SysUser newUser = new SysUser(); |
| | | newUser.setUserName(dto.getUserName()); |
| | | newUser.setNickName(dto.getNickName()); |
| | | newUser.setOaUserId(dto.getOaUserId()); |
| | | |
| | | if (deptId != null) |
| | | { |
| | | newUser.setDeptId(deptId); |
| | | } |
| | | else |
| | | { |
| | | // 如果没有部门,默认设置为顶级部门(若依科技) |
| | | newUser.setDeptId(100L); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getEmail())) |
| | | { |
| | | newUser.setEmail(dto.getEmail()); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getPhonenumber())) |
| | | { |
| | | // 校验手机号是否已被其他用户使用 |
| | | SysUser phoneCheck = sysUserMapper.checkPhoneUnique(dto.getPhonenumber()); |
| | | if (StringUtils.isNotNull(phoneCheck)) |
| | | { |
| | | log.warn("创建用户失败,手机号 {} 已被用户 {} 使用,跳过用户 {}", |
| | | dto.getPhonenumber(), phoneCheck.getUserName(), dto.getUserName()); |
| | | return; // 跳过创建 |
| | | } |
| | | newUser.setPhonenumber(dto.getPhonenumber()); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getSex())) |
| | | { |
| | | newUser.setSex(dto.getSex()); |
| | | } |
| | | else |
| | | { |
| | | newUser.setSex("2"); // 默认未知 |
| | | } |
| | | |
| | | // 设置默认密码(123456) |
| | | newUser.setPassword(SecurityUtils.encryptPassword("123456")); |
| | | |
| | | // 设置默认状态为正常 |
| | | newUser.setStatus("0"); |
| | | |
| | | newUser.setCreateBy("sync"); |
| | | |
| | | sysUserMapper.insertUser(newUser); |
| | | } |
| | | } |