| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | 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业务层处理 |
| | |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(DepartmentSyncServiceImpl.class); |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | @Autowired |
| | | private IOrderClassDataService orderClassDataService; |
| | | |
| | | |
| | | |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AjaxResult syncBranchDepartments(List<DepartmentSyncDTO> branchDepts) |
| | | public AjaxResult syncBranchDepartments(List<DepartmentSyncDTO> branchDepts,List<OrderClassDTO> serviceOrderList,List<OrderClassDTO> dispatchOrderList,List<Map<String, Object>> addressList) |
| | | { |
| | | try |
| | | { |
| | |
| | | } |
| | | |
| | | log.info("开始同步 {} 条分公司数据到 MySQL 数据库...", branchDepts.size()); |
| | | |
| | | // 一次性从SQL Server查询所有编码数据,避免在循环中重复查询 |
| | | |
| | | |
| | | // 使用Map来跟踪已创建的分公司,key为分公司名称,value为部门ID |
| | | Map<String, Long> branchMap = new HashMap<>(); |
| | |
| | | if (parts.length != 2) |
| | | { |
| | | log.warn("部门名称格式不正确,跳过: {}", fullName); |
| | | continue; |
| | | parts= fullName.split("-"); |
| | | if(parts.length != 2) { |
| | | continue; |
| | | } |
| | | } |
| | | String part=""; |
| | | //只要发现有(新)或(新)就在branchName中加个新 字 |
| | | if(fullName.contains("(新)") || fullName.contains("(新)")) { |
| | | part="(新)"; |
| | | } |
| | | |
| | | String branchName = parts[0].trim() + "分公司"; // 湛江 -> 湛江分公司 |
| | | String deptName = parts[1].trim(); // 护士 |
| | | String cityName=parts[0].trim(); |
| | | String namePart=parts[0].trim(); |
| | | if(namePart.contains("(新)") || namePart.contains("(新)")) { |
| | | branchName=namePart+"分公司"; |
| | | cityName=namePart; |
| | | deptName=parts[1].trim(); |
| | | } |
| | | else{ |
| | | branchName=namePart+part+"分公司"; |
| | | cityName=namePart+ part; |
| | | deptName=parts[1].trim(); |
| | | } |
| | | |
| | | |
| | | // 获取或创建分公司 |
| | | Long branchDeptId = branchMap.get(branchName); |
| | |
| | | SysDept existingBranch = sysDeptMapper.checkDeptNameUnique(branchName, 100L); |
| | | if (existingBranch != null) |
| | | { |
| | | // 分公司已存在,更新department_id |
| | | // 分公司已存在,更新编码 |
| | | branchDeptId = existingBranch.getDeptId(); |
| | | branchMap.put(branchName, branchDeptId); |
| | | log.info("分公司已存在: {}, ID: {}", branchName, branchDeptId); |
| | | |
| | | // 检查并更新编码 |
| | | syncOrderClassCodes(existingBranch, cityName, serviceOrderList, dispatchOrderList,addressList); |
| | | // existingBranch.setDepartmentId(dto.getDepartmentId()); |
| | | sysDeptMapper.updateDept(existingBranch); |
| | | log.info("更新分公司编码: {}, 服务单编码: {}, 调度单编码: {}", |
| | | branchName, existingBranch.getServiceOrderClass(), existingBranch.getDispatchOrderClass()); |
| | | } |
| | | else |
| | | { |
| | |
| | | newBranch.setOrderNum(branchMap.size() + 1); // 排序 |
| | | newBranch.setStatus("0"); // 正常状态 |
| | | newBranch.setCreateBy("sync"); |
| | | // newBranch.setDepartmentId(dto.getDepartmentId()); |
| | | |
| | | // 自动匹配并设置服务单和调度单编码 |
| | | syncOrderClassCodes(newBranch, cityName, serviceOrderList, dispatchOrderList,addressList); |
| | | |
| | | sysDeptMapper.insertDept(newBranch); |
| | | branchDeptId = newBranch.getDeptId(); |
| | | branchMap.put(branchName, branchDeptId); |
| | | createdBranch++; |
| | | log.info("创建新分公司: {}, ID: {}", branchName, branchDeptId); |
| | | log.info("创建新分公司: {}, ID: {}, 服务单编码: {}, 调度单编码: {}", |
| | | branchName, branchDeptId, newBranch.getServiceOrderClass(), newBranch.getDispatchOrderClass()); |
| | | } |
| | | } |
| | | |
| | |
| | | // 部门已存在,更新信息 |
| | | existingDept.setDeptName(deptName); |
| | | existingDept.setUpdateBy("sync"); |
| | | // existingDept.setDepartmentId(dto.getDepartmentId()); |
| | | sysDeptMapper.updateDept(existingDept); |
| | | updatedDept++; |
| | | log.info("更新部门: {} -> {}", branchName, deptName); |
| | |
| | | return AjaxResult.error("同步失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 同步分公司的地址数据 |
| | | * @param departAddress |
| | | */ |
| | | public void syncDeptAddress(List<Map<String,Object>> departAddress){ |
| | | List<SysDept> depts =sysDeptMapper.selectDeptListByParentId(100L); |
| | | for(SysDept dept:depts){ |
| | | Map<String,Object> addressInfo = getDeptAddressInfo(departAddress,dept.getServiceOrderClass()); |
| | | if(addressInfo != null){ |
| | | dept.setDepartureAddress(getAddress(addressInfo)); |
| | | dept.setDepartureLongitude(getLongitude(addressInfo)); |
| | | dept.setDepartureLatitude(getLatitude(addressInfo)); |
| | | } |
| | | sysDeptMapper.updateDept(dept); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 同步转运部和子部门数据(使用外部传入的数据源) |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AjaxResult syncTransportDepartments(List<DepartmentSyncDTO> transportDepts) |
| | | public AjaxResult syncTransportDepartments(List<DepartmentSyncDTO> transportDepts, List<Map<String, Object>> addressList) |
| | | { |
| | | try |
| | | { |
| | |
| | | // 部门已存在,更新信息 |
| | | existingDept.setDeptName(deptName); |
| | | existingDept.setUpdateBy("sync"); |
| | | existingDept.setDepartmentId(dto.getDepartmentId()); |
| | | sysDeptMapper.updateDept(existingDept); |
| | | updatedDept++; |
| | | log.info("更新转运部子部门: {}", deptName); |
| | |
| | | return AjaxResult.error("同步失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private Map<String,Object> getDeptAddressInfo(List<Map<String,Object>> departAddress,String ServiceBranch){ |
| | | return departAddress.stream() |
| | | .filter(map -> map.get("ServiceBranch").equals(ServiceBranch)) |
| | | .findFirst() |
| | | .orElse(null); |
| | | } |
| | | |
| | | private String getAddress(Map<String, Object> addressInfo){ |
| | | if(addressInfo == null){ |
| | | return null; |
| | | } |
| | | return addressInfo.get("ServiceAddress").toString(); |
| | | } |
| | | |
| | | private BigDecimal getLongitude(Map<String, Object> addressInfo){ |
| | | if(addressInfo == null){ |
| | | return null; |
| | | } |
| | | return new BigDecimal(addressInfo.get("ServiceAddress_lng").toString()); |
| | | } |
| | | |
| | | private BigDecimal getLatitude(Map<String, Object> addressInfo){ |
| | | if(addressInfo == null){ |
| | | return null; |
| | | } |
| | | return new BigDecimal(addressInfo.get("ServiceAddress_lat").toString()); |
| | | } |
| | | |
| | | /** |
| | | * 同步订单编码(服务单和调度单编码) |
| | | * |
| | | * 根据分公司名称中的地名,自动匹配SQL Server中的编码并更新到部门对象 |
| | | * |
| | | * @param dept 部门对象(分公司) |
| | | * @param cityName 城市名称(如:中山、广州、湛江) |
| | | * @param serviceOrderList 服务单编码列表(从SQL Server查询) |
| | | * @param dispatchOrderList 调度单编码列表(从SQL Server查询) |
| | | */ |
| | | private void syncOrderClassCodes(SysDept dept, String cityName, |
| | | List<OrderClassDTO> serviceOrderList, |
| | | List<OrderClassDTO> dispatchOrderList, List<Map<String, Object>> addressList) |
| | | { |
| | | if (serviceOrderList == null || dispatchOrderList == null) |
| | | { |
| | | log.warn("编码列表为空,跳过编码同步 - 城市: {}", cityName); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | // 匹配服务单编码 |
| | | String serviceOrderClass = matchCityNameToCode(cityName, serviceOrderList); |
| | | if (serviceOrderClass != null) |
| | | { |
| | | Map<String, Object> addressInfo = this.getDeptAddressInfo(addressList, serviceOrderClass); |
| | | String address=this.getAddress(addressInfo); |
| | | if(address!=null){ |
| | | dept.setDepartureAddress(address); |
| | | } |
| | | BigDecimal lon = this.getLongitude(addressInfo); |
| | | if(lon!=null){ |
| | | dept.setDepartureLongitude(lon); |
| | | } |
| | | BigDecimal lat = this.getLatitude(addressInfo); |
| | | if(lat!=null){ |
| | | dept.setDepartureLatitude(lat); |
| | | } |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据城市名称匹配编码 |
| | | * |
| | | * 匹配规则:如果vtext中包含城市名称,则返回对应的vOrder2编码 |
| | | * 例如:cityName="中山", vtext="中山服务单", vOrder2="JA" -> 返回"JA" |
| | | * |
| | | * @param cityName 城市名称(如:中山、广州) |
| | | * @param orderClassList 编码列表 |
| | | * @return 匹配的编码值(vOrder2),未匹配返回null |
| | | */ |
| | | private String matchCityNameToCode(String cityName, List<OrderClassDTO> orderClassList) |
| | | { |
| | | if (StringUtils.isEmpty(cityName) || orderClassList == null || orderClassList.isEmpty()) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | // 遍历编码列表,查找包含城市名称的项 |
| | | for (OrderClassDTO dto : orderClassList) |
| | | { |
| | | //有些加了新 TODO |
| | | if (dto.getVtext() != null && dto.getVtext().contains(cityName)) |
| | | { |
| | | log.debug("城市名称匹配成功 - 城市: {}, vtext: {}, vOrder2: {}", |
| | | cityName, dto.getVtext(), dto.getVOrder2()); |
| | | return dto.getVOrder2(); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | } |