123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package thyyxxk.wxservice_server.service;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
- import thyyxxk.wxservice_server.constant.Constants;
- import thyyxxk.wxservice_server.dao.AppointmentDao;
- import thyyxxk.wxservice_server.entity.PureCodeName;
- import thyyxxk.wxservice_server.entity.ResultVo;
- import thyyxxk.wxservice_server.entity.appointment.*;
- import thyyxxk.wxservice_server.entity.assessment.CovidQuestionnaire;
- import thyyxxk.wxservice_server.entity.hrgresponse.SourcesResponse;
- import thyyxxk.wxservice_server.entity.hrgresponse.HrgCommonResponse;
- import thyyxxk.wxservice_server.entity.hrgresponse.MzClassResponse;
- import thyyxxk.wxservice_server.utils.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * @author dj
- */
- @Slf4j
- @Service
- public class AppointmentService {
- private static List<MzClass> mzClasses;
- private final AppointmentDao dao;
- @Value("${hrgApiUrl}")
- private String hrgApiUrl;
- @Autowired
- public AppointmentService(AppointmentDao dao) {
- this.dao = dao;
- }
- public ResultVo<List<MzClass>> getAllDepartments() {
- if (null != mzClasses && !mzClasses.isEmpty()) {
- return ResultVoUtil.success(mzClasses);
- }
- log.info("重新获取门诊科室并缓存。");
- RestTemplate template = new RestTemplate();
- MzClassResponse response = template.getForObject(hrgApiUrl + "/getMzClass", MzClassResponse.class);
- if (null == response || -1 == response.getResultCode()) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "获取科室分类失败,请联系服务中心。");
- }
- mzClasses = new ArrayList<>();
- JSONObject param = new JSONObject();
- for (Map<String, Object> item : response.getData()) {
- MzClass pojo = new MzClass();
- pojo.setId(item.get("code").toString());
- pojo.setText(item.get("name").toString());
- param.put("mzClass", pojo.getId());
- MzClassResponse response2 = template.postForObject(hrgApiUrl + "/getUnitCodeByMzClass", param, MzClassResponse.class);
- if (null != response2 && response2.getResultCode() == 0) {
- List<MzDept> children = new ArrayList<>();
- for (Map<String, Object> child : response2.getData()) {
- MzDept dept = new MzDept();
- dept.setId(child.get("code").toString());
- dept.setText(child.get("name").toString());
- List<Map<String, Object>> grandChildrenMap = CastUtil.cast(child.get("children"));
- if (null != grandChildrenMap && grandChildrenMap.size() > 0) {
- List<MzDept> grandChildren = new ArrayList<>();
- for (Map<String, Object> temp : grandChildrenMap) {
- MzDept mzDept = new MzDept();
- mzDept.setId(temp.get("code").toString());
- mzDept.setText(temp.get("name").toString());
- grandChildren.add(mzDept);
- }
- dept.setChildren(grandChildren);
- }
- children.add(dept);
- }
- pojo.setChildren(children);
- mzClasses.add(pojo);
- }
- }
- return ResultVoUtil.success(mzClasses);
- }
- public ResultVo<String> refreshMzClasses() {
- log.info("门诊科室缓存已重置。");
- mzClasses = null;
- return ResultVoUtil.success("门诊科室缓存已重置。");
- }
- public ResultVo<Integer[]> getSourcesByDate(GetSourcesByDateParam param) {
- Integer[] retArr = new Integer[7];
- RestTemplate restTemplate = new RestTemplate();
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- for (int i = 0; i < 7; i++) {
- retArr[i] = 0;
- int addNum = i == 0 ? 0 : 1;
- calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + addNum);
- String date = format.format(calendar.getTime());
- String url = String.format(hrgApiUrl + "/getDoctorByDateAndDept?requestDay=%s&unitCode=%s", date, param.getDept());
- SourcesResponse hrgResponse = restTemplate.getForObject(url, SourcesResponse.class);
- if (null == hrgResponse || hrgResponse.getResultCode() == -1) {
- retArr[i] = 0;
- continue;
- }
- for (Map<String, Object> item : hrgResponse.getData()) {
- if ((int) item.get("leftNum") > 0) {
- retArr[i] = 1;
- break;
- }
- }
- }
- return ResultVoUtil.success(retArr);
- }
- public ResultVo<Object> getDoctorSources(GetDoctorSourcesParam param) {
- String url = String.format(hrgApiUrl + "/getDoctorByDateAndDept?requestDay=%s&unitCode=%s", param.getDate(), param.getDeptCode());
- RestTemplate template = new RestTemplate();
- SourcesResponse data = template.getForObject(url, SourcesResponse.class);
- if (null == data || null == data.getResultCode()) {
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
- }
- if (data.getResultCode() == -1) {
- return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR, data.getResultMessage());
- }
- data.getData().removeIf(map -> (int) map.get("leftNum") == 0);
- if (data.getData().size() == 0) {
- return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR);
- }
- for (Map<String, Object> map : data.getData()) {
- PureCodeName info = dao.selectPortraitAndIntroduction(map.get("doctorCode").toString());
- if (null != info) {
- map.put("portrait", info.getCode());
- map.put("introduction", info.getName());
- }
- }
- data.getData().sort((o1, o2) -> {
- Integer i1 = Integer.parseInt(o1.get("empTitCode").toString());
- Integer i2 = Integer.parseInt(o2.get("empTitCode").toString());
- int diff = i1 - i2;
- return Integer.compare(diff, 0);
- });
- return ResultVoUtil.success(data.getData());
- }
- public ResultVo<Object> getDoctorArrangement(GetDoctorSourcesParam param) {
- String url = String.format(hrgApiUrl + "/getRequestByDateAndDeptAndDoctor?requestDay=%s&unitCode=%s&doctorCode=%s",
- param.getDate(), param.getDeptCode(), param.getDoctorCode());
- RestTemplate template = new RestTemplate();
- SourcesResponse data = template.getForObject(url, SourcesResponse.class);
- if (null == data || null == data.getResultCode()) {
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
- }
- if (data.getResultCode() == -1) {
- return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR, data.getResultMessage());
- }
- data.getData().removeIf(map -> (int) map.get("leftNum") == 0);
- if (data.getData().size() == 0) {
- return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR);
- }
- return ResultVoUtil.success(data.getData());
- }
- public ResultVo<Integer[]> getSourcesByDateAndDoctor(GetDoctorSourcesParam param) {
- Integer[] source = new Integer[7];
- source[0] = getDoctorArrangement(param).getCode();
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- for (int i = 1; i < source.length; i++) {
- calendar.add(Calendar.DATE, 1);
- param.setDate(dateFormat.format(calendar.getTime()));
- source[i] = getDoctorArrangement(param).getCode();
- }
- return ResultVoUtil.success(source);
- }
- public ResultVo<Map<String, Object>> getGhFee(GetGhFeeParam param) {
- String url = String.format(hrgApiUrl + "/getMzChargeTypeByRequestIdForHaiCi?mzyRequestId=%d&patientId=%s",
- param.getMzyRequestId(), param.getPatientId());
- RestTemplate template = new RestTemplate();
- HrgCommonResponse data = template.getForObject(url, HrgCommonResponse.class);
- log.info("获取挂号费用:\n参数:{},\n结果:{}", param, data);
- if (null == data) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "网络服务错误!");
- }
- if (-1 == data.getCode()) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, data.getMessage());
- }
- Map<String, Object> map = new HashMap<>(Constants.Capacity.TWO);
- map.put("fee", data.getData());
- map.put("message", data.getMessage());
- return ResultVoUtil.success(map);
- }
- public ResultVo<DoctorInfo> getDoctorInfo(String doctorCode, String openId) {
- DoctorInfo doctor = dao.selectDoctorInfo(doctorCode, openId);
- if (null == doctor) {
- return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "未找到医生信息!");
- }
- return ResultVoUtil.success(doctor);
- }
- public ResultVo<String> hasValidCovidAssessment(String patientId, String deptCode) {
- PureCodeName sexSocial = dao.selectSexAndSocialNo(patientId);
- String mzClass = dao.selectMzClass(deptCode);
- if (Constants.Sex.MALE.equals(sexSocial.getCode()) && Constants.MzClass.GYNAECOLOGY.equals(mzClass)) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "男性无法在妇产科挂号,请选择其他科室。");
- }
- int age = IdCardUtil.calAgeBySocialNo(sexSocial.getName());
- if (age == -1) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在我院绑定的身份证号不是有效的身份证号," +
- "请前往【个人中心 - 我的就诊人 - 就诊人信息】进行修改。");
- }
- if (Constants.MzDept.PEDIATRICS.equals(deptCode) && age >= 18) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "成年人无法在儿科挂号,请选择其他科室。");
- }
- CovidQuestionnaire covid = dao.validCovidAssessment(patientId);
- if (null == covid) {
- return ResultVoUtil.success("no valid assessment");
- } else {
- if (covid.getTemperature() == 2 ||
- covid.getItem1() != 14 ||
- covid.getItem2() != 24 ||
- covid.getItem3() != 32 ||
- covid.getItem4() != 42) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您新型冠状病毒感染流行病学史问卷未通过,请挂发热门诊。");
- } else {
- return ResultVoUtil.success("valid assessment");
- }
- }
- }
- public ResultVo<String> getDoctorQrCode(String doctorCode) {
- final String token = PropertiesUtil.getProperty("qywxToken");
- final String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + token +
- "&userid=" + dao.getCodeRsByCode(doctorCode);
- RestTemplate restTemplate = new RestTemplate();
- final String result = restTemplate.getForObject(url, String.class);
- JSONObject json = JSONObject.parseObject(result);
- return ResultVoUtil.success(json.getString("qr_code"));
- }
- public ResultVo<List<Map<String, String>>> getPaidMzGhList(String patientId) {
- log.info("获取门诊挂号记录列表:{}", patientId);
- RestTemplate template = new RestTemplate();
- JSONObject obj = new JSONObject();
- obj.put("patientId", patientId);
- SourcesResponse hrgResponse = template.postForObject(hrgApiUrl + "/getRegistrationForPaid", obj, SourcesResponse.class);
- return ThmzUtil.getResultVoCompletableFuture(hrgResponse);
- }
- }
|