package com.ruoyi.system.service.impl; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.system.domain.HospDataSyncDTO; import com.ruoyi.system.domain.TbHospData; import com.ruoyi.system.service.IHospDataSyncService; import com.ruoyi.system.service.IHospDataSyncDataService; import com.ruoyi.system.service.ITbHospDataService; import org.apache.commons.lang3.StringUtils; 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 java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 医院数据同步Service业务层处理 * * @author ruoyi */ @Service public class HospDataSyncServiceImpl implements IHospDataSyncService { private static final Logger log = LoggerFactory.getLogger(HospDataSyncServiceImpl.class); @Autowired private IHospDataSyncDataService hospDataSyncDataService; @Autowired private ITbHospDataService tbHospDataService; /** * 从SQL Server同步医院数据到MySQL * * @return 同步结果 */ @Override public AjaxResult syncHospData() { try { log.info("开始同步医院数据..."); // 从SQL Server查询所有医院数据 List sqlServerHospList = hospDataSyncDataService.selectAllHospDataFromSqlServer(); if (sqlServerHospList == null || sqlServerHospList.isEmpty()) { return AjaxResult.warn("SQL Server中没有医院数据"); } log.info("从SQL Server查询到 {} 条医院数据", sqlServerHospList.size()); int insertCount = 0; int updateCount = 0; int skipCount = 0; Map errorMessages = new HashMap<>(); // 遍历SQL Server数据,同步到MySQL for (HospDataSyncDTO dto : sqlServerHospList) { try { // 检查必填字段 if (dto.getHospId() == null || StringUtils.isBlank(dto.getHospName())) { log.warn("医院数据不完整,跳过同步: HospID={}, HospName={}", dto.getHospId(), dto.getHospName()); skipCount++; continue; } // 根据旧系统ID查询MySQL中是否已存在 TbHospData existingHosp = tbHospDataService.selectTbHospDataByLegacyId(dto.getHospId()); if (existingHosp != null) { // 更新已有数据 updateHospData(existingHosp, dto); tbHospDataService.updateTbHospData(existingHosp); updateCount++; log.debug("更新医院: {} (HospID={})", dto.getHospName(), dto.getHospId()); } else { // 插入新数据 TbHospData newHosp = convertToTbHospData(dto); tbHospDataService.insertTbHospData(newHosp); insertCount++; log.debug("新增医院: {} (HospID={})", dto.getHospName(), dto.getHospId()); } } catch (Exception e) { String errorMsg = String.format("同步医院 HospID=%d 失败: %s", dto.getHospId(), e.getMessage()); log.error(errorMsg, e); errorMessages.put("HospID_" + dto.getHospId(), errorMsg); } } String resultMsg = String.format( "医院数据同步完成 - 新增: %d, 更新: %d, 跳过: %d, 失败: %d", insertCount, updateCount, skipCount, errorMessages.size()); log.info(resultMsg); Map result = new HashMap<>(); result.put("insertCount", insertCount); result.put("updateCount", updateCount); result.put("skipCount", skipCount); result.put("errorCount", errorMessages.size()); result.put("errors", errorMessages); result.put("totalProcessed", sqlServerHospList.size()); if (errorMessages.isEmpty()) { return AjaxResult.success(resultMsg, result); } else { return AjaxResult.warn(resultMsg, result); } } catch (Exception e) { log.error("同步医院数据失败", e); return AjaxResult.error("同步失败: " + e.getMessage()); } } /** * 将DTO转换为实体对象(新增) * * @param dto SQL Server数据DTO * @return MySQL实体对象 */ private TbHospData convertToTbHospData(HospDataSyncDTO dto) { TbHospData hosp = new TbHospData(); hosp.setLegacyHospId(dto.getHospId()); hosp.setHospName(dto.getHospName()); hosp.setHospCityId(dto.getHospCityId()); hosp.setHospShort(dto.getHospShort()); hosp.setHopsProvince(dto.getHopsProvince()); hosp.setHopsCity(dto.getHopsCity()); hosp.setHopsArea(dto.getHopsArea()); hosp.setHospAddress(dto.getHospAddress()); hosp.setHospTel(dto.getHospTel()); hosp.setHospUnitId(dto.getHospUnitId()); hosp.setHospState(dto.getHospState()); hosp.setHospOaId(dto.getHospOaId()); hosp.setHospIntroducerId(dto.getHospIntroducerId()); hosp.setHospIntroducerDate(parseDate(dto.getHospIntroducerDate())); hosp.setHospLevel(dto.getHospLevel()); hosp.setStatus("0"); // 默认正常状态 hosp.setCreateBy("sync"); hosp.setRemark("从旧系统同步"); return hosp; } /** * 更新现有数据 * * @param target MySQL实体对象 * @param source SQL Server数据DTO */ private void updateHospData(TbHospData target, HospDataSyncDTO source) { target.setHospName(source.getHospName()); target.setHospCityId(source.getHospCityId()); target.setHospShort(source.getHospShort()); target.setHopsProvince(source.getHopsProvince()); target.setHopsCity(source.getHopsCity()); target.setHopsArea(source.getHopsArea()); target.setHospAddress(source.getHospAddress()); target.setHospTel(source.getHospTel()); target.setHospUnitId(source.getHospUnitId()); target.setHospState(source.getHospState()); target.setHospOaId(source.getHospOaId()); target.setHospIntroducerId(source.getHospIntroducerId()); target.setHospIntroducerDate(parseDate(source.getHospIntroducerDate())); target.setHospLevel(source.getHospLevel()); target.setUpdateBy("sync"); } /** * 解析日期字符串 * * @param dateStr 日期字符串 * @return Date对象 */ private Date parseDate(String dateStr) { if (StringUtils.isBlank(dateStr)) { return null; } try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(dateStr); } catch (ParseException e) { log.warn("日期解析失败: {}", dateStr); return null; } } }