From 2c86a8bd60deed0dd0e044bad6fb83f75d19a332 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 26 十月 2025 15:05:50 +0800
Subject: [PATCH] Merge branch 'feature-task'
---
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DepartmentSyncServiceImpl.java | 432 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 432 insertions(+), 0 deletions(-)
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DepartmentSyncServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DepartmentSyncServiceImpl.java
new file mode 100644
index 0000000..3af352b
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DepartmentSyncServiceImpl.java
@@ -0,0 +1,432 @@
+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.domain.OrderClassDTO;
+import com.ruoyi.system.mapper.SysDeptMapper;
+import com.ruoyi.system.service.IDepartmentSyncService;
+import com.ruoyi.system.service.IDepartmentSyncDataService;
+import com.ruoyi.system.service.IOrderClassDataService;
+
+/**
+ * 閮ㄩ棬鍚屾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;
+
+ @Autowired
+ private IOrderClassDataService orderClassDataService;
+
+
+
+ /**
+ * 鍚屾鍒嗗叕鍙稿拰閮ㄩ棬鏁版嵁锛堜娇鐢ㄥ閮ㄤ紶鍏ョ殑鏁版嵁婧愶級
+ *
+ * 鎵ц娴佺▼锛�
+ * 1. 鎺ユ敹澶栭儴浼犲叆鐨� DepartmentSyncDTO 鍒楄〃
+ * 2. 鍐欏叆 MySQL 鏁版嵁搴擄紙sysDeptMapper 浣跨敤榛樿 MySQL 鏁版嵁婧愶級
+ *
+ * @param branchDepts 澶栭儴浼犲叆鐨勫垎鍏徃鏁版嵁鍒楄〃
+ * @return 鍚屾缁撴灉
+ */
+ @Override
+ @Transactional
+ public AjaxResult syncBranchDepartments(List<DepartmentSyncDTO> branchDepts,List<OrderClassDTO> serviceOrderList,List<OrderClassDTO> dispatchOrderList)
+ {
+ try
+ {
+ if (branchDepts == null || branchDepts.isEmpty())
+ {
+ return AjaxResult.warn("浼犲叆鐨勯儴闂ㄦ暟鎹负绌�");
+ }
+
+ log.info("寮�濮嬪悓姝� {} 鏉″垎鍏徃鏁版嵁鍒� MySQL 鏁版嵁搴�...", branchDepts.size());
+
+ // 涓�娆℃�т粠SQL Server鏌ヨ鎵�鏈夌紪鐮佹暟鎹紝閬垮厤鍦ㄥ惊鐜腑閲嶅鏌ヨ
+
+
+ // 浣跨敤Map鏉ヨ窡韪凡鍒涘缓鐨勫垎鍏徃锛宬ey涓哄垎鍏徃鍚嶇О锛寁alue涓洪儴闂↖D
+ 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;
+ }
+
+ // 瑙f瀽閮ㄩ棬鍚嶇О锛氭箾姹�--鎶ゅ+ -> 鍒嗗叕鍙革細婀涙睙鍒嗗叕鍙革紝閮ㄩ棬锛氭姢澹�
+ 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)
+ {
+ // 妫�鏌ュ垎鍏徃鏄惁宸插瓨鍦紙閫氳繃閮ㄩ棬鍚嶇О鍜宲arent_id=100鏉ユ煡璇級
+ SysDept existingBranch = sysDeptMapper.checkDeptNameUnique(branchName, 100L);
+ if (existingBranch != null)
+ {
+ // 鍒嗗叕鍙稿凡瀛樺湪锛屾洿鏂扮紪鐮�
+ branchDeptId = existingBranch.getDeptId();
+ branchMap.put(branchName, branchDeptId);
+
+ // 妫�鏌ュ苟鏇存柊缂栫爜
+ syncOrderClassCodes(existingBranch, parts[0].trim(), serviceOrderList, dispatchOrderList);
+// existingBranch.setDepartmentId(dto.getDepartmentId());
+ sysDeptMapper.updateDept(existingBranch);
+ log.info("鏇存柊鍒嗗叕鍙哥紪鐮�: {}, 鏈嶅姟鍗曠紪鐮�: {}, 璋冨害鍗曠紪鐮�: {}",
+ branchName, existingBranch.getServiceOrderClass(), existingBranch.getDispatchOrderClass());
+ }
+ else
+ {
+ // 鍒涘缓鏂扮殑鍒嗗叕鍙�
+ SysDept newBranch = new SysDept();
+ newBranch.setParentId(100L); // 鐖惰妭鐐逛负鑻ヤ緷绉戞妧锛堥粯璁�100锛�
+ newBranch.setDeptName(branchName);
+ newBranch.setAncestors("0,100"); // 绁栫骇鍒楄〃
+ newBranch.setOrderNum(branchMap.size() + 1); // 鎺掑簭
+ newBranch.setStatus("0"); // 姝e父鐘舵��
+ newBranch.setCreateBy("sync");
+// newBranch.setDepartmentId(dto.getDepartmentId());
+
+ // 鑷姩鍖归厤骞惰缃湇鍔″崟鍜岃皟搴﹀崟缂栫爜
+ syncOrderClassCodes(newBranch, parts[0].trim(), serviceOrderList, dispatchOrderList);
+
+ sysDeptMapper.insertDept(newBranch);
+ branchDeptId = newBranch.getDeptId();
+ branchMap.put(branchName, branchDeptId);
+ createdBranch++;
+ log.info("鍒涘缓鏂板垎鍏徃: {}, ID: {}, 鏈嶅姟鍗曠紪鐮�: {}, 璋冨害鍗曠紪鐮�: {}",
+ branchName, branchDeptId, newBranch.getServiceOrderClass(), newBranch.getDispatchOrderClass());
+ }
+ }
+
+ // 鍒涘缓鎴栨洿鏂板瓙閮ㄩ棬
+ // 鍏堟牴鎹甦epartmentId鏌ヨ鏄惁宸插瓨鍦�
+ SysDept existingDept = sysDeptMapper.selectDeptByDepartmentIdAndParentId(
+ dto.getDepartmentId(), branchDeptId);
+
+ if (existingDept != null)
+ {
+ // 閮ㄩ棬宸插瓨鍦紝鏇存柊淇℃伅
+ existingDept.setDeptName(deptName);
+ existingDept.setUpdateBy("sync");
+// existingDept.setDepartmentId(dto.getDepartmentId());
+ sysDeptMapper.updateDept(existingDept);
+ updatedDept++;
+ log.info("鏇存柊閮ㄩ棬: {} -> {}", branchName, deptName);
+ }
+ else
+ {
+ // 妫�鏌ユ槸鍚﹀瓨鍦ㄥ悓鍚嶉儴闂紙鍙兘涔嬪墠鎵嬪姩鍒涘缓鐨勶級
+ SysDept sameName = sysDeptMapper.checkDeptNameUnique(deptName, branchDeptId);
+ if (sameName != null)
+ {
+ // 鍚屽悕閮ㄩ棬宸插瓨鍦紝鏇存柊鍏禿epartment_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"); // 姝e父鐘舵��
+ 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. 鍒涘缓杞繍閮ㄧ殑瀛愰儴闂紙鐩存帴鍒涘缓锛屾棤闇�瑙f瀽"--"鏍煎紡锛�
+ * 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锛変笉瀛樺湪锛岃鍏堝垱寤烘�诲叕鍙�");
+ }
+
+ // 鏌ヨ杞繍閮ㄦ槸鍚﹀凡瀛樺湪锛堥�氳繃閮ㄩ棬鍚嶇О鍜宲arent_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"); // 姝e父鐘舵��
+ 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;
+ }
+
+ // 鍏堟牴鎹甦epartmentId鏌ヨ鏄惁宸插瓨鍦�
+ SysDept existingDept = sysDeptMapper.selectDeptByDepartmentIdAndParentId(
+ dto.getDepartmentId(), transportDeptId);
+
+ if (existingDept != null)
+ {
+ // 閮ㄩ棬宸插瓨鍦紝鏇存柊淇℃伅
+ existingDept.setDeptName(deptName);
+ existingDept.setUpdateBy("sync");
+ existingDept.setDepartmentId(dto.getDepartmentId());
+ sysDeptMapper.updateDept(existingDept);
+ updatedDept++;
+ log.info("鏇存柊杞繍閮ㄥ瓙閮ㄩ棬: {}", deptName);
+ }
+ else
+ {
+ // 妫�鏌ユ槸鍚﹀瓨鍦ㄥ悓鍚嶉儴闂紙鍙兘涔嬪墠鎵嬪姩鍒涘缓鐨勶級
+ SysDept sameName = sysDeptMapper.checkDeptNameUnique(deptName, transportDeptId);
+ if (sameName != null)
+ {
+ // 鍚屽悕閮ㄩ棬宸插瓨鍦紝鏇存柊鍏禿epartment_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"); // 姝e父鐘舵��
+
+ 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());
+ }
+ }
+
+ /**
+ * 鍚屾璁㈠崟缂栫爜锛堟湇鍔″崟鍜岃皟搴﹀崟缂栫爜锛�
+ *
+ * 鏍规嵁鍒嗗叕鍙稿悕绉颁腑鐨勫湴鍚嶏紝鑷姩鍖归厤SQL Server涓殑缂栫爜骞舵洿鏂板埌閮ㄩ棬瀵硅薄
+ *
+ * @param dept 閮ㄩ棬瀵硅薄锛堝垎鍏徃锛�
+ * @param cityName 鍩庡競鍚嶇О锛堝锛氫腑灞便�佸箍宸炪�佹箾姹燂級
+ * @param serviceOrderList 鏈嶅姟鍗曠紪鐮佸垪琛紙浠嶴QL Server鏌ヨ锛�
+ * @param dispatchOrderList 璋冨害鍗曠紪鐮佸垪琛紙浠嶴QL Server鏌ヨ锛�
+ */
+ private void syncOrderClassCodes(SysDept dept, String cityName,
+ List<OrderClassDTO> serviceOrderList,
+ List<OrderClassDTO> dispatchOrderList)
+ {
+ if (serviceOrderList == null || dispatchOrderList == null)
+ {
+ log.warn("缂栫爜鍒楄〃涓虹┖锛岃烦杩囩紪鐮佸悓姝� - 鍩庡競: {}", cityName);
+ return;
+ }
+
+ try
+ {
+ // 鍖归厤鏈嶅姟鍗曠紪鐮�
+ String serviceOrderClass = matchCityNameToCode(cityName, serviceOrderList);
+ if (serviceOrderClass != null)
+ {
+ dept.setServiceOrderClass(serviceOrderClass);
+ log.info("鍖归厤鍒版湇鍔″崟缂栫爜 - 鍩庡競: {}, 缂栫爜: {}", cityName, serviceOrderClass);
+ }
+ else
+ {
+ log.warn("鏈壘鍒板尮閰嶇殑鏈嶅姟鍗曠紪鐮� - 鍩庡競: {}", cityName);
+ }
+
+ // 鍖归厤璋冨害鍗曠紪鐮�
+ String dispatchOrderClass = matchCityNameToCode(cityName, dispatchOrderList);
+ if (dispatchOrderClass != null)
+ {
+ dept.setDispatchOrderClass(dispatchOrderClass);
+ log.info("鍖归厤鍒拌皟搴﹀崟缂栫爜 - 鍩庡競: {}, 缂栫爜: {}", cityName, dispatchOrderClass);
+ }
+ else
+ {
+ log.warn("鏈壘鍒板尮閰嶇殑璋冨害鍗曠紪鐮� - 鍩庡競: {}", cityName);
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("鍚屾璁㈠崟缂栫爜澶辫触 - 鍩庡競: {}", cityName, e);
+ }
+ }
+
+ /**
+ * 鏍规嵁鍩庡競鍚嶇О鍖归厤缂栫爜
+ *
+ * 鍖归厤瑙勫垯锛氬鏋渧text涓寘鍚煄甯傚悕绉帮紝鍒欒繑鍥炲搴旂殑vOrder2缂栫爜
+ * 渚嬪锛歝ityName="涓北", vtext="涓北鏈嶅姟鍗�", vOrder2="JA" -> 杩斿洖"JA"
+ *
+ * @param cityName 鍩庡競鍚嶇О锛堝锛氫腑灞便�佸箍宸烇級
+ * @param orderClassList 缂栫爜鍒楄〃
+ * @return 鍖归厤鐨勭紪鐮佸�硷紙vOrder2锛夛紝鏈尮閰嶈繑鍥瀗ull
+ */
+ private String matchCityNameToCode(String cityName, List<OrderClassDTO> orderClassList)
+ {
+ if (StringUtils.isEmpty(cityName) || orderClassList == null || orderClassList.isEmpty())
+ {
+ return null;
+ }
+
+ // 閬嶅巻缂栫爜鍒楄〃锛屾煡鎵惧寘鍚煄甯傚悕绉扮殑椤�
+ for (OrderClassDTO dto : orderClassList)
+ {
+ if (dto.getVtext() != null && dto.getVtext().contains(cityName))
+ {
+ log.debug("鍩庡競鍚嶇О鍖归厤鎴愬姛 - 鍩庡競: {}, vtext: {}, vOrder2: {}",
+ cityName, dto.getVtext(), dto.getVOrder2());
+ return dto.getVOrder2();
+ }
+ }
+
+ return null;
+ }
+}
--
Gitblit v1.9.1