package com.ruoyi.system.service.impl;
|
|
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.system.domain.SysTask;
|
import com.ruoyi.system.domain.SysTaskEmergency;
|
import com.ruoyi.system.domain.enums.TaskStatus;
|
import com.ruoyi.system.domain.vo.TaskCreateVO;
|
import com.ruoyi.system.domain.vo.TaskUpdateVO;
|
import com.ruoyi.system.mapper.SysTaskEmergencyMapper;
|
import com.ruoyi.system.mapper.SysTaskMapper;
|
import com.ruoyi.system.service.ISysEmergencyTaskService;
|
import com.ruoyi.system.service.ISysTaskEmergencyService;
|
import com.ruoyi.system.service.IMapService;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.HashSet;
|
import java.util.Map;
|
|
@Service
|
public class SysEmergencyTaskServiceImpl implements ISysEmergencyTaskService {
|
|
private static final Logger log = LoggerFactory.getLogger(SysEmergencyTaskServiceImpl.class);
|
|
@Autowired
|
private SysTaskEmergencyMapper sysTaskEmergencyMapper;
|
|
@Autowired
|
private SysTaskMapper sysTaskMapper;
|
|
@Autowired(required = false)
|
private ISysTaskEmergencyService sysTaskEmergencyService;
|
|
@Autowired(required = false)
|
private IMapService mapService;
|
|
@Override
|
public void saveEmergencyInfo(Long taskId, String createUserName, TaskCreateVO createVO,
|
String serviceOrderId, String dispatchOrderId, String serviceOrdNo) {
|
SysTaskEmergency emergencyInfo = new SysTaskEmergency();
|
emergencyInfo.setTaskId(taskId);
|
|
if (createVO.getPatient() != null) {
|
emergencyInfo.setPatientContact(createVO.getPatient().getContact());
|
emergencyInfo.setPatientPhone(createVO.getPatient().getPhone());
|
emergencyInfo.setPatientName(createVO.getPatient().getName());
|
emergencyInfo.setPatientGender(createVO.getPatient().getGender());
|
emergencyInfo.setPatientIdCard(createVO.getPatient().getIdCard());
|
emergencyInfo.setPatientCondition(createVO.getPatient().getCondition());
|
}
|
|
if (createVO.getHospitalOut() != null) {
|
emergencyInfo.setHospitalOutId(createVO.getHospitalOut().getId());
|
emergencyInfo.setHospitalOutName(createVO.getHospitalOut().getName());
|
emergencyInfo.setHospitalOutDepartment(createVO.getHospitalOut().getDepartment());
|
emergencyInfo.setHospitalOutDepartmentId(createVO.getHospitalOut().getDepartmentId());
|
emergencyInfo.setHospitalOutBedNumber(createVO.getHospitalOut().getBedNumber());
|
emergencyInfo.setHospitalOutAddress(createVO.getHospitalOut().getAddress());
|
|
if (createVO.getHospitalOut().getLongitude() != null && createVO.getHospitalOut().getLatitude() != null) {
|
emergencyInfo.setHospitalOutLongitude(createVO.getHospitalOut().getLongitude());
|
emergencyInfo.setHospitalOutLatitude(createVO.getHospitalOut().getLatitude());
|
} else if (mapService != null && createVO.getHospitalOut().getAddress() != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
createVO.getHospitalOut().getAddress(),
|
extractCityFromAddress(createVO.getHospitalOut().getAddress())
|
);
|
if (coords != null) {
|
emergencyInfo.setHospitalOutLongitude(BigDecimal.valueOf(coords.get("lng")));
|
emergencyInfo.setHospitalOutLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转出医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转出医院GPS坐标失败", e);
|
}
|
}
|
}
|
|
if (createVO.getHospitalIn() != null) {
|
emergencyInfo.setHospitalInId(createVO.getHospitalIn().getId());
|
emergencyInfo.setHospitalInName(createVO.getHospitalIn().getName());
|
emergencyInfo.setHospitalInDepartment(createVO.getHospitalIn().getDepartment());
|
emergencyInfo.setHospitalInDepartmentId(createVO.getHospitalIn().getDepartmentId());
|
emergencyInfo.setHospitalInBedNumber(createVO.getHospitalIn().getBedNumber());
|
emergencyInfo.setHospitalInAddress(createVO.getHospitalIn().getAddress());
|
|
if (createVO.getHospitalIn().getLongitude() != null && createVO.getHospitalIn().getLatitude() != null) {
|
emergencyInfo.setHospitalInLongitude(createVO.getHospitalIn().getLongitude());
|
emergencyInfo.setHospitalInLatitude(createVO.getHospitalIn().getLatitude());
|
} else if (mapService != null && createVO.getHospitalIn().getAddress() != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
createVO.getHospitalIn().getAddress(),
|
extractCityFromAddress(createVO.getHospitalIn().getAddress())
|
);
|
if (coords != null) {
|
emergencyInfo.setHospitalInLongitude(BigDecimal.valueOf(coords.get("lng")));
|
emergencyInfo.setHospitalInLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转入医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转入医院GPS坐标失败", e);
|
}
|
}
|
}
|
|
emergencyInfo.setTransferDistance(createVO.getTransferDistance());
|
emergencyInfo.setTransferPrice(createVO.getPrice());
|
emergencyInfo.setDocumentTypeId(createVO.getDocumentTypeId());
|
emergencyInfo.setTaskTypeId(createVO.getTaskTypeId());
|
|
if (createVO.getDiseaseIds() != null && !createVO.getDiseaseIds().isEmpty()) {
|
String diseaseIdsStr = createVO.getDiseaseIds().stream()
|
.map(String::valueOf)
|
.collect(java.util.stream.Collectors.joining(","));
|
emergencyInfo.setDiseaseIds(diseaseIdsStr);
|
}
|
|
if (serviceOrderId != null) {
|
emergencyInfo.setLegacyServiceOrdId(Long.parseLong(serviceOrderId));
|
emergencyInfo.setSyncStatus(2);
|
emergencyInfo.setSyncTime(DateUtils.getNowDate());
|
emergencyInfo.setSyncErrorMsg("旧系统同步过来");
|
}
|
if (dispatchOrderId != null) {
|
emergencyInfo.setLegacyDispatchOrdId(Long.parseLong(dispatchOrderId));
|
emergencyInfo.setDispatchSyncStatus(2);
|
emergencyInfo.setDispatchSyncTime(DateUtils.getNowDate());
|
emergencyInfo.setDispatchSyncErrorMsg("旧系统同步过来");
|
}
|
if (serviceOrdNo != null) {
|
emergencyInfo.setLegacyServiceOrdNo(serviceOrdNo);
|
}
|
|
emergencyInfo.setCreateTime(DateUtils.getNowDate());
|
emergencyInfo.setUpdateTime(DateUtils.getNowDate());
|
emergencyInfo.setCreateBy(createUserName);
|
emergencyInfo.setUpdateBy(createUserName);
|
|
sysTaskEmergencyMapper.insertSysTaskEmergency(emergencyInfo);
|
}
|
|
@Override
|
public void updateEmergencyInfoFromUpdateVO(SysTaskEmergency oldEmergency, TaskUpdateVO updateVO, String userName) {
|
if (oldEmergency == null || updateVO == null) {
|
return;
|
}
|
|
if (updateVO.getPatient() != null) {
|
if (updateVO.getPatient().getContact() != null) {
|
oldEmergency.setPatientContact(updateVO.getPatient().getContact());
|
}
|
if (updateVO.getPatient().getPhone() != null) {
|
oldEmergency.setPatientPhone(updateVO.getPatient().getPhone());
|
}
|
if (updateVO.getPatient().getName() != null) {
|
oldEmergency.setPatientName(updateVO.getPatient().getName());
|
}
|
if (updateVO.getPatient().getGender() != null) {
|
oldEmergency.setPatientGender(updateVO.getPatient().getGender());
|
}
|
if (updateVO.getPatient().getIdCard() != null) {
|
oldEmergency.setPatientIdCard(updateVO.getPatient().getIdCard());
|
}
|
if (updateVO.getPatient().getCondition() != null) {
|
oldEmergency.setPatientCondition(updateVO.getPatient().getCondition());
|
}
|
}
|
|
if (updateVO.getHospitalOut() != null) {
|
if (updateVO.getHospitalOut().getId() != null) {
|
oldEmergency.setHospitalOutId(updateVO.getHospitalOut().getId());
|
}
|
if (updateVO.getHospitalOut().getName() != null) {
|
oldEmergency.setHospitalOutName(updateVO.getHospitalOut().getName());
|
}
|
if (updateVO.getHospitalOut().getDepartment() != null) {
|
oldEmergency.setHospitalOutDepartment(updateVO.getHospitalOut().getDepartment());
|
}
|
if (updateVO.getHospitalOut().getDepartmentId() != null) {
|
oldEmergency.setHospitalOutDepartmentId(updateVO.getHospitalOut().getDepartmentId());
|
}
|
if (updateVO.getHospitalOut().getBedNumber() != null) {
|
oldEmergency.setHospitalOutBedNumber(updateVO.getHospitalOut().getBedNumber());
|
}
|
if (updateVO.getHospitalOut().getAddress() != null
|
&& !updateVO.getHospitalOut().getAddress().equals(oldEmergency.getHospitalOutAddress())) {
|
oldEmergency.setHospitalOutAddress(updateVO.getHospitalOut().getAddress());
|
|
if (updateVO.getHospitalOut().getLongitude() == null
|
&& updateVO.getHospitalOut().getLatitude() == null && mapService != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
updateVO.getHospitalOut().getAddress(),
|
extractCityFromAddress(updateVO.getHospitalOut().getAddress())
|
);
|
if (coords != null) {
|
oldEmergency.setHospitalOutLongitude(BigDecimal.valueOf(coords.get("lng")));
|
oldEmergency.setHospitalOutLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转出医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转出医院GPS坐标失败", e);
|
}
|
}
|
}
|
}
|
|
if (updateVO.getHospitalIn() != null) {
|
if (updateVO.getHospitalIn().getId() != null) {
|
oldEmergency.setHospitalInId(updateVO.getHospitalIn().getId());
|
}
|
if (updateVO.getHospitalIn().getName() != null) {
|
oldEmergency.setHospitalInName(updateVO.getHospitalIn().getName());
|
}
|
if (updateVO.getHospitalIn().getDepartment() != null) {
|
oldEmergency.setHospitalInDepartment(updateVO.getHospitalIn().getDepartment());
|
}
|
if (updateVO.getHospitalIn().getDepartmentId() != null) {
|
oldEmergency.setHospitalInDepartmentId(updateVO.getHospitalIn().getDepartmentId());
|
}
|
if (updateVO.getHospitalIn().getBedNumber() != null) {
|
oldEmergency.setHospitalInBedNumber(updateVO.getHospitalIn().getBedNumber());
|
}
|
if (updateVO.getHospitalIn().getAddress() != null
|
&& !updateVO.getHospitalIn().getAddress().equals(oldEmergency.getHospitalInAddress())) {
|
oldEmergency.setHospitalInAddress(updateVO.getHospitalIn().getAddress());
|
|
if (updateVO.getHospitalIn().getLongitude() == null
|
&& updateVO.getHospitalIn().getLatitude() == null && mapService != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
updateVO.getHospitalIn().getAddress(),
|
extractCityFromAddress(updateVO.getHospitalIn().getAddress())
|
);
|
if (coords != null) {
|
oldEmergency.setHospitalInLongitude(BigDecimal.valueOf(coords.get("lng")));
|
oldEmergency.setHospitalInLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转入医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转入医院GPS坐标失败", e);
|
}
|
}
|
}
|
}
|
|
if (updateVO.getTransferDistance() != null) {
|
oldEmergency.setTransferDistance(updateVO.getTransferDistance());
|
}
|
if (updateVO.getPrice() != null) {
|
oldEmergency.setTransferPrice(updateVO.getPrice());
|
}
|
|
if (updateVO.getDocumentTypeId() != null) {
|
oldEmergency.setDocumentTypeId(updateVO.getDocumentTypeId());
|
}
|
|
if (updateVO.getTaskTypeId() != null) {
|
oldEmergency.setTaskTypeId(updateVO.getTaskTypeId());
|
}
|
|
if (updateVO.getDiseaseIds() != null && !updateVO.getDiseaseIds().isEmpty()) {
|
String diseaseIdsStr = updateVO.getDiseaseIds().stream()
|
.map(String::valueOf)
|
.collect(java.util.stream.Collectors.joining(","));
|
oldEmergency.setDiseaseIds(diseaseIdsStr);
|
}
|
|
oldEmergency.setUpdateTime(DateUtils.getNowDate());
|
oldEmergency.setUpdateBy(userName);
|
|
sysTaskEmergencyMapper.updateSysTaskEmergency(oldEmergency);
|
}
|
|
@Override
|
public void updateEmergencyInfoFromCreateVO(SysTaskEmergency existingInfo, TaskCreateVO createVO, String userName) {
|
if (existingInfo == null || createVO == null) {
|
return;
|
}
|
|
log.info("更新转运任务 taskId:{}", existingInfo.getTaskId());
|
|
// 更新患者信息
|
if (createVO.getPatient() != null) {
|
if (createVO.getPatient().getContact() != null) {
|
existingInfo.setPatientContact(createVO.getPatient().getContact());
|
}
|
if (createVO.getPatient().getPhone() != null) {
|
existingInfo.setPatientPhone(createVO.getPatient().getPhone());
|
}
|
if (createVO.getPatient().getName() != null) {
|
existingInfo.setPatientName(createVO.getPatient().getName());
|
}
|
if (createVO.getPatient().getGender() != null) {
|
existingInfo.setPatientGender(createVO.getPatient().getGender());
|
}
|
if (createVO.getPatient().getIdCard() != null) {
|
existingInfo.setPatientIdCard(createVO.getPatient().getIdCard());
|
}
|
if (createVO.getPatient().getCondition() != null) {
|
existingInfo.setPatientCondition(createVO.getPatient().getCondition());
|
}
|
}
|
|
// 更新转出医院信息
|
if (createVO.getHospitalOut() != null) {
|
if (createVO.getHospitalOut().getId() != null) {
|
existingInfo.setHospitalOutId(createVO.getHospitalOut().getId());
|
}
|
if (createVO.getHospitalOut().getName() != null) {
|
existingInfo.setHospitalOutName(createVO.getHospitalOut().getName());
|
}
|
if (createVO.getHospitalOut().getDepartment() != null) {
|
existingInfo.setHospitalOutDepartment(createVO.getHospitalOut().getDepartment());
|
}
|
if (createVO.getHospitalOut().getDepartmentId() != null) {
|
existingInfo.setHospitalOutDepartmentId(createVO.getHospitalOut().getDepartmentId());
|
}
|
if (createVO.getHospitalOut().getBedNumber() != null) {
|
existingInfo.setHospitalOutBedNumber(createVO.getHospitalOut().getBedNumber());
|
}
|
if (createVO.getHospitalOut().getAddress() != null
|
&& !createVO.getHospitalOut().getAddress().equals(existingInfo.getHospitalOutAddress())) {
|
existingInfo.setHospitalOutAddress(createVO.getHospitalOut().getAddress());
|
|
// 如果更新了地址但没有GPS坐标,后端自动获取
|
if (createVO.getHospitalOut().getLongitude() == null
|
&& createVO.getHospitalOut().getLatitude() == null && mapService != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
createVO.getHospitalOut().getAddress(),
|
extractCityFromAddress(createVO.getHospitalOut().getAddress())
|
);
|
if (coords != null) {
|
existingInfo.setHospitalOutLongitude(BigDecimal.valueOf(coords.get("lng")));
|
existingInfo.setHospitalOutLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转出医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转出医院GPS坐标失败", e);
|
}
|
}
|
}
|
}
|
|
// 更新转入医院信息
|
if (createVO.getHospitalIn() != null) {
|
if (createVO.getHospitalIn().getId() != null) {
|
existingInfo.setHospitalInId(createVO.getHospitalIn().getId());
|
}
|
if (createVO.getHospitalIn().getName() != null) {
|
existingInfo.setHospitalInName(createVO.getHospitalIn().getName());
|
}
|
if (createVO.getHospitalIn().getDepartment() != null) {
|
existingInfo.setHospitalInDepartment(createVO.getHospitalIn().getDepartment());
|
}
|
if (createVO.getHospitalIn().getDepartmentId() != null) {
|
existingInfo.setHospitalInDepartmentId(createVO.getHospitalIn().getDepartmentId());
|
}
|
if (createVO.getHospitalIn().getBedNumber() != null) {
|
existingInfo.setHospitalInBedNumber(createVO.getHospitalIn().getBedNumber());
|
}
|
if (createVO.getHospitalIn().getAddress() != null
|
&& !createVO.getHospitalIn().getAddress().equals(existingInfo.getHospitalInAddress())) {
|
existingInfo.setHospitalInAddress(createVO.getHospitalIn().getAddress());
|
|
// 如果更新了地址但没有GPS坐标,后端自动获取
|
if (createVO.getHospitalIn().getLongitude() == null
|
&& createVO.getHospitalIn().getLatitude() == null && mapService != null) {
|
try {
|
Map<String, Double> coords = mapService.geocoding(
|
createVO.getHospitalIn().getAddress(),
|
extractCityFromAddress(createVO.getHospitalIn().getAddress())
|
);
|
if (coords != null) {
|
existingInfo.setHospitalInLongitude(BigDecimal.valueOf(coords.get("lng")));
|
existingInfo.setHospitalInLatitude(BigDecimal.valueOf(coords.get("lat")));
|
log.info("转入医院GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
|
}
|
} catch (Exception e) {
|
log.error("自动获取转入医院GPS坐标失败", e);
|
}
|
}
|
}
|
}
|
|
// 更新费用信息
|
if (createVO.getTransferDistance() != null) {
|
existingInfo.setTransferDistance(createVO.getTransferDistance());
|
}
|
if (createVO.getPrice() != null) {
|
existingInfo.setTransferPrice(createVO.getPrice());
|
}
|
|
// 更新单据类型ID
|
if (createVO.getDocumentTypeId() != null) {
|
existingInfo.setDocumentTypeId(createVO.getDocumentTypeId());
|
}
|
|
// 更新任务类型ID
|
if (createVO.getTaskTypeId() != null) {
|
existingInfo.setTaskTypeId(createVO.getTaskTypeId());
|
}
|
|
// 更新病情ID列表
|
if (createVO.getDiseaseIds() != null && !createVO.getDiseaseIds().isEmpty()) {
|
String diseaseIdsStr = createVO.getDiseaseIds().stream()
|
.map(String::valueOf)
|
.collect(java.util.stream.Collectors.joining(","));
|
existingInfo.setDiseaseIds(diseaseIdsStr);
|
}
|
|
// 系统字段
|
existingInfo.setUpdateTime(DateUtils.getNowDate());
|
existingInfo.setUpdateBy(userName);
|
|
// 执行更新
|
sysTaskEmergencyMapper.updateSysTaskEmergency(existingInfo);
|
}
|
|
@Override
|
public void markNeedResyncIfNecessary(Long taskId, SysTask oldTask, TaskUpdateVO updateVO, Boolean updateFromLegacy) {
|
if (!"EMERGENCY_TRANSFER".equals(oldTask.getTaskType()) || Boolean.TRUE.equals(updateFromLegacy)) {
|
return;
|
}
|
try {
|
sysTaskEmergencyService.markNeedResync(taskId);
|
} catch (Exception e) {
|
// 标记失败不影响主流程
|
}
|
}
|
|
@Override
|
public boolean hasLegacyServiceOrdId(Long taskId) {
|
SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
|
if (task != null && "EMERGENCY_TRANSFER".equals(task.getTaskType())) {
|
SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
|
return emergencyInfo != null && emergencyInfo.getLegacyServiceOrdId() != null;
|
}
|
return false;
|
}
|
|
@Override
|
public boolean hasLegacyDispatchOrdId(Long taskId) {
|
SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
|
if (task != null && "EMERGENCY_TRANSFER".equals(task.getTaskType())) {
|
SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
|
return emergencyInfo != null && emergencyInfo.getLegacyDispatchOrdId() != null;
|
}
|
return false;
|
}
|
|
@Override
|
public boolean existsByLegacyServiceOrdId(Long legacyServiceOrdId) {
|
if (legacyServiceOrdId == null) {
|
return false;
|
}
|
SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectByLegacyServiceOrdId(legacyServiceOrdId);
|
return emergencyInfo != null;
|
}
|
|
@Override
|
public boolean existsByLegacyDispatchOrdId(Long legacyDispatchOrdId) {
|
if (legacyDispatchOrdId == null) {
|
return false;
|
}
|
SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectByLegacyDispatchOrdId(legacyDispatchOrdId);
|
return emergencyInfo != null;
|
}
|
|
private String extractCityFromAddress(String address) {
|
if (address == null || address.trim().isEmpty()) {
|
return null;
|
}
|
String[] cities = {"广州", "深圳", "东莞", "佛山", "珠海", "惠州", "中山", "江门", "湛江", "肇庆", "清远", "韶关", "梅州", "河源", "潮州", "揭阳", "汕头", "汕尾", "云浮", "阳江","北京","上海","天津"};
|
for (String city : cities) {
|
if (address.contains(city)) {
|
return city;
|
}
|
}
|
return null;
|
}
|
}
|