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.utils.StringUtils; import com.ruoyi.system.domain.DepartmentSyncDTO; import com.ruoyi.system.mapper.SysDeptMapper; import com.ruoyi.system.service.IDepartmentSyncService; import com.ruoyi.system.service.IDepartmentSyncDataService; /** * 部门同步Service业务层处理 * * 职责:接收部门数据,写入 MySQL 数据库 * 数据来源: * 1. 内部查询:调用 IDepartmentSyncDataService 从 SQL Server 查询 * 2. 外部传入:接收已查询好的数据 * * @author ruoyi * @date 2025-10-18 */ @Service public class DepartmentSyncServiceImpl implements IDepartmentSyncService { private static final Logger log = LoggerFactory.getLogger(DepartmentSyncServiceImpl.class); @Autowired private SysDeptMapper sysDeptMapper; /** * 同步分公司和部门数据(使用外部传入的数据源) * * 执行流程: * 1. 接收外部传入的 DepartmentSyncDTO 列表 * 2. 写入 MySQL 数据库(sysDeptMapper 使用默认 MySQL 数据源) * * @param branchDepts 外部传入的分公司数据列表 * @return 同步结果 */ @Override @Transactional public AjaxResult syncBranchDepartments(List branchDepts) { try { if (branchDepts == null || branchDepts.isEmpty()) { return AjaxResult.warn("传入的部门数据为空"); } log.info("开始同步 {} 条分公司数据到 MySQL 数据库...", branchDepts.size()); // 使用Map来跟踪已创建的分公司,key为分公司名称,value为部门ID Map branchMap = new HashMap<>(); int createdBranch = 0; int updatedBranch = 0; int createdDept = 0; int updatedDept = 0; // 处理每一条分公司数据 for (DepartmentSyncDTO dto : branchDepts) { String fullName = dto.getDepartmentName(); if (StringUtils.isEmpty(fullName)) { continue; } // 解析部门名称:湛江--护士 -> 分公司:湛江分公司,部门:护士 String[] parts = fullName.split("--"); if (parts.length != 2) { log.warn("部门名称格式不正确,跳过: {}", fullName); continue; } String branchName = parts[0].trim() + "分公司"; // 湛江 -> 湛江分公司 String deptName = parts[1].trim(); // 护士 // 获取或创建分公司 Long branchDeptId = branchMap.get(branchName); if (branchDeptId == null) { // 检查分公司是否已存在(通过部门名称和parent_id=100来查询) SysDept existingBranch = sysDeptMapper.checkDeptNameUnique(branchName, 100L); if (existingBranch != null) { // 分公司已存在,更新department_id branchDeptId = existingBranch.getDeptId(); branchMap.put(branchName, branchDeptId); log.info("分公司已存在: {}, ID: {}", branchName, branchDeptId); } else { // 创建新的分公司 SysDept newBranch = new SysDept(); newBranch.setParentId(100L); // 父节点为若依科技(默认100) newBranch.setDeptName(branchName); newBranch.setAncestors("0,100"); // 祖级列表 newBranch.setOrderNum(branchMap.size() + 1); // 排序 newBranch.setStatus("0"); // 正常状态 newBranch.setCreateBy("sync"); sysDeptMapper.insertDept(newBranch); branchDeptId = newBranch.getDeptId(); branchMap.put(branchName, branchDeptId); createdBranch++; log.info("创建新分公司: {}, ID: {}", branchName, branchDeptId); } } // 创建或更新子部门 // 先根据departmentId查询是否已存在 SysDept existingDept = sysDeptMapper.selectDeptByDepartmentIdAndParentId( dto.getDepartmentId(), branchDeptId); if (existingDept != null) { // 部门已存在,更新信息 existingDept.setDeptName(deptName); existingDept.setUpdateBy("sync"); sysDeptMapper.updateDept(existingDept); updatedDept++; log.info("更新部门: {} -> {}", branchName, deptName); } else { // 检查是否存在同名部门(可能之前手动创建的) SysDept sameName = sysDeptMapper.checkDeptNameUnique(deptName, branchDeptId); if (sameName != null) { // 同名部门已存在,更新其department_id sameName.setDepartmentId(dto.getDepartmentId()); sameName.setUpdateBy("sync"); sysDeptMapper.updateDept(sameName); updatedDept++; log.info("更新已存在的同名部门: {} -> {}, departmentId: {}", branchName, deptName, dto.getDepartmentId()); } else { // 创建新部门 SysDept newDept = new SysDept(); newDept.setParentId(branchDeptId); newDept.setDeptName(deptName); newDept.setAncestors("0,100," + branchDeptId); // 祖级列表 newDept.setOrderNum(1); // 排序 newDept.setStatus("0"); // 正常状态 newDept.setDepartmentId(dto.getDepartmentId()); // 记录SQL Server中的部门ID newDept.setCreateBy("sync"); sysDeptMapper.insertDept(newDept); createdDept++; log.info("创建新部门: {} -> {}, departmentId: {}", branchName, deptName, dto.getDepartmentId()); } } } String message = String.format("同步完成!创建分公司: %d, 更新分公司: %d, 创建部门: %d, 更新部门: %d", createdBranch, updatedBranch, createdDept, updatedDept); log.info(message); Map result = new HashMap<>(); result.put("createdBranch", createdBranch); result.put("updatedBranch", updatedBranch); result.put("createdDept", createdDept); result.put("updatedDept", updatedDept); result.put("totalProcessed", branchDepts.size()); return AjaxResult.success(message, result); } catch (Exception e) { log.error("同步分公司数据失败", e); return AjaxResult.error("同步失败: " + e.getMessage()); } } /** * 同步转运部和子部门数据(使用外部传入的数据源) * * 执行流程: * 1. 确保总公司(ID=101)下存在"转运部" * 2. 创建转运部的子部门(直接创建,无需解析"--"格式) * 3. 写入 MySQL 数据库(sysDeptMapper 使用默认 MySQL 数据源) * * @param transportDepts 外部传入的转运部子部门数据列表 * @return 同步结果 */ @Override @Transactional public AjaxResult syncTransportDepartments(List transportDepts) { try { if (transportDepts == null || transportDepts.isEmpty()) { return AjaxResult.warn("传入的转运部子部门数据为空"); } log.info("开始同步 {} 条转运部子部门数据到 MySQL 数据库...", transportDepts.size()); // 第一步:确保总公司(ID=101)下存在"转运部" Long transportDeptId = null; // 查询总公司是否存在 SysDept headOffice = sysDeptMapper.selectDeptById(101L); if (headOffice == null) { log.error("总公司(ID=101)不存在,无法同步转运部"); return AjaxResult.error("总公司(ID=101)不存在,请先创建总公司"); } // 查询转运部是否已存在(通过部门名称和parent_id=101来查询) SysDept existingTransport = sysDeptMapper.checkDeptNameUnique("转运部", 101L); if (existingTransport != null) { // 转运部已存在 transportDeptId = existingTransport.getDeptId(); log.info("转运部已存在: ID={}", transportDeptId); } else { // 创建新的转运部 SysDept newTransport = new SysDept(); newTransport.setParentId(101L); // 父节点为总公司 newTransport.setDeptName("转运部"); newTransport.setAncestors("0,101"); // 祖级列表 newTransport.setOrderNum(1); // 排序 newTransport.setStatus("0"); // 正常状态 newTransport.setCreateBy("sync"); newTransport.setDepartmentId(150); sysDeptMapper.insertDept(newTransport); transportDeptId = newTransport.getDeptId(); log.info("创建新转运部: ID={}", transportDeptId); } // 第二步:创建或更新转运部的子部门 int createdDept = 0; int updatedDept = 0; for (DepartmentSyncDTO dto : transportDepts) { String deptName = dto.getDepartmentName(); if (StringUtils.isEmpty(deptName)) { continue; } // 先根据departmentId查询是否已存在 SysDept existingDept = sysDeptMapper.selectDeptByDepartmentIdAndParentId( dto.getDepartmentId(), transportDeptId); if (existingDept != null) { // 部门已存在,更新信息 existingDept.setDeptName(deptName); existingDept.setUpdateBy("sync"); sysDeptMapper.updateDept(existingDept); updatedDept++; log.info("更新转运部子部门: {}", deptName); } else { // 检查是否存在同名部门(可能之前手动创建的) SysDept sameName = sysDeptMapper.checkDeptNameUnique(deptName, transportDeptId); if (sameName != null) { // 同名部门已存在,更新其department_id sameName.setDepartmentId(dto.getDepartmentId()); sameName.setUpdateBy("sync"); sysDeptMapper.updateDept(sameName); updatedDept++; log.info("更新已存在的同名转运部子部门: {}, departmentId: {}", deptName, dto.getDepartmentId()); } else { // 创建新部门 SysDept newDept = new SysDept(); newDept.setParentId(transportDeptId); newDept.setDeptName(deptName); newDept.setAncestors("0,101," + transportDeptId); // 祖级列表 newDept.setOrderNum(1); // 排序 newDept.setStatus("0"); // 正常状态 newDept.setDepartmentId(dto.getDepartmentId()); // 记录SQL Server中的部门ID newDept.setCreateBy("sync"); sysDeptMapper.insertDept(newDept); createdDept++; log.info("创建新转运部子部门: {}, departmentId: {}", deptName, dto.getDepartmentId()); } } } String message = String.format("转运部同步完成!创建子部门: %d, 更新子部门: %d", createdDept, updatedDept); log.info(message); Map result = new HashMap<>(); result.put("transportDeptId", transportDeptId); result.put("createdDept", createdDept); result.put("updatedDept", updatedDept); result.put("totalProcessed", transportDepts.size()); return AjaxResult.success(message, result); } catch (Exception e) { log.error("同步转运部数据失败", e); return AjaxResult.error("同步失败: " + e.getMessage()); } } }