package com.smartor.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysUserDept;
import com.ruoyi.common.core.domain.entity.SysUserRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.service.dto.InhospPositionRecord;
import com.smartor.common.enums.EmergencyLevelEnum;
import com.smartor.common.enums.ImportFlagEnum;
import com.ruoyi.common.utils.http.HttpUtils;
import com.smartor.common.DistinctByProperty;
import com.smartor.domain.*;
import com.ruoyi.outpatientvisit.domain.PatMedOuthospError;
import com.ruoyi.outpatientvisit.domain.PatMedOuthosp;
import com.ruoyi.inhospvisit.api.InhospVisitFacade;
import com.ruoyi.inhospvisit.domain.PatMedInhosp;
import com.smartor.mapper.*;
import com.ruoyi.outpatientvisit.mapper.PatMedOuthospErrorMapper;
import com.smartor.service.IPatArchiveService;
import com.smartor.service.IPatMedInhospService;
import com.smartor.service.IPatMedOuthospService;
import com.smartor.service.IServiceSLTDHealthcareRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.ruoyi.common.core.domain.entity.PatArchive;
import com.ruoyi.task.domain.ServiceSubtask;
import com.ruoyi.task.mapper.ServiceSubtaskMapper;
import com.ruoyi.task.service.impl.ServiceSubtaskServiceImpl;
@Slf4j
@Service
public class ServiceSLTDHealthcareRecordServiceImpl implements IServiceSLTDHealthcareRecordService {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 在院患者当前位置查询地址(网关根路径 {@code sltd_pub_path} + 固定后缀)。
*
* 与"入院采集"是同一个接口({@code queryHealthcareRecordList} 用的也是它),
* 区别只是本方法按 {@code healthcareRecordNo} 单查、只取位置字段、不落库。
*/
private static final String INHOSP_POSITION_PATH =
"/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList";
/**
* 当前位置查询的连接/读超时:一轮要按患者调用几千次,
* 不能让一次无响应把采集线程无限期挂住({@code HttpUtils} 老方法没有超时)
*/
private static final int INHOSP_POSITION_CONNECT_TIMEOUT_MS = 5000;
private static final int INHOSP_POSITION_READ_TIMEOUT_MS = 15000;
@Autowired
private IPatArchiveService patArchiveService;
@Autowired
private IPatMedInhospService patMedInhospService;
@Autowired
private IPatMedOuthospService patMedOuthospService;
@Autowired
private SysUser2Mapper sysUser2Mapper;
@Autowired
private SysDept2Mapper sysDeptMapper;
@Autowired
private SysUserDeptMapper sysUserDeptMapper;
@Autowired
private SysUserRole2Mapper sysUserRoleMapper;
@Autowired
private MedicationItemMapper medicationItemMapper;
@Autowired
private MedicationDoseInfoMapper medicationDoseInfoMapper;
@Autowired
private RedisTemplate redisTemplate;
@Value("${sltd_pub_path}")
private String sltdPubPath;
@Value("${userPwd}")
private String userPwd;
@Value("${lwl_app_key}")
private String APP_KEY;
@Value("${spring.profiles.active}")
private String active;
@Autowired
private ServiceSubtaskServiceImpl serviceSubtaskServiceImpl;
@Autowired
private ServiceSubtaskMapper serviceSubtaskMapper;
@Autowired
private InhospVisitFacade inhospVisitFacade;
@Autowired
private PatMedOuthospErrorMapper patMedOuthospErrorMapper;
@Override
public List queryHealthcareRecordList(ServiceSLTDInhospReqVO reqVO) {
try {
Map requestParams = buildRequestParams(reqVO);
Map headers = buildRequestHeaders();
log.info("请求参数出院数据地址:{},appKey:{}", sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList", APP_KEY);
String result = null;
if (reqVO.getSize() == null && reqVO.getCurrent() == null)
result = HttpUtils.sendPostByHeader(sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList", new Gson().toJson(requestParams), headers);
else
result = HttpUtils.sendPostByHeader(sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcPageHealthcareRecordList", new Gson().toJson(requestParams), headers);
log.info("queryHealthcareRecordList--result返回的结果值:{}", result == null ? "空值" : "有值");
String cry = determineCry(reqVO);
log.info("cry的值为:{}", cry);
List serviceSLTDInhospResDTOS = parseResponseData(result, CollectionUtils.isNotEmpty(reqVO.getStatusList()) ? reqVO.getStatusList() : reqVO.getHealthcareRecordTypeList(), cry, reqVO.getCampusId() != null ? reqVO.getCampusId().toString() : null);
log.info("serviceSLTDInhospResDTOS是否返回的有值:{}", CollectionUtils.isNotEmpty(serviceSLTDInhospResDTOS) ? "有" : "没有");
return serviceSLTDInhospResDTOS;
} catch (Exception e) {
log.error("【queryHealthcareRecordList】调用省立同德健康记录查询接口异常,请求参数:{}", reqVO, e);
throw new RuntimeException("调用省立同德健康记录查询接口失败:" + e.getMessage());
}
}
@Override
/**
* 查询药品信息列表
* @return 返回实际获取到的药品数据条数,如果出错则返回 null
*/ public Integer queryMedicationItemList(ServiceSLTDInhospReqVO reqVO) {
try {
Map requestParams = buildRequestParams(reqVO);
Map headers = buildRequestHeaders();
log.info("请求参药品信息地址:{},appKey:{}", sltdPubPath + "/hbos-thirdparty-integration/standard/base/mc/service/medication/queryMedicationInfoPage", APP_KEY);
String result = HttpUtils.sendPostByHeader(sltdPubPath + "/hbos-thirdparty-integration/standard/base/mc/service/medication/queryMedicationInfoPage", new Gson().toJson(requestParams), headers);
List