wlzboy
2025-10-18 b46065a201c09ce69f111806f2bda4a5f476bc4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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<DepartmentSyncDTO> branchDepts)
    {
        try
        {
            if (branchDepts == null || branchDepts.isEmpty())
            {
                return AjaxResult.warn("传入的部门数据为空");
            }
            
            log.info("开始同步 {} 条分公司数据到 MySQL 数据库...", branchDepts.size());
            
            // 使用Map来跟踪已创建的分公司,key为分公司名称,value为部门ID
            Map<String, Long> 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<String, Object> 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<DepartmentSyncDTO> 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<String, Object> 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());
        }
    }
}