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 mzClasses; private final AppointmentDao dao; @Value("${hrgApiUrl}") private String hrgApiUrl; @Autowired public AppointmentService(AppointmentDao dao) { this.dao = dao; } public ResultVo> 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 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 children = new ArrayList<>(); for (Map child : response2.getData()) { MzDept dept = new MzDept(); dept.setId(child.get("code").toString()); dept.setText(child.get("name").toString()); List> grandChildrenMap = CastUtil.cast(child.get("children")); if (null != grandChildrenMap && grandChildrenMap.size() > 0) { List grandChildren = new ArrayList<>(); for (Map 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 refreshMzClasses() { log.info("门诊科室缓存已重置。"); mzClasses = null; return ResultVoUtil.success("门诊科室缓存已重置。"); } public ResultVo 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 item : hrgResponse.getData()) { if ((int) item.get("leftNum") > 0) { retArr[i] = 1; break; } } } return ResultVoUtil.success(retArr); } public ResultVo 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 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 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 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> 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 map = new HashMap<>(Constants.Capacity.TWO); map.put("fee", data.getData()); map.put("message", data.getMessage()); return ResultVoUtil.success(map); } public ResultVo 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 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 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>> 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); } }