|
@@ -1,395 +0,0 @@
|
|
|
-package thyyxxk.wxservice_server.service;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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.ElectronicHealthCardDao;
|
|
|
-import thyyxxk.wxservice_server.entity.ResultVo;
|
|
|
-import thyyxxk.wxservice_server.entity.analyzeidcard.IdCardAnalyzeResult;
|
|
|
-import thyyxxk.wxservice_server.entity.analyzeidcard.UploadIdCardParam;
|
|
|
-import thyyxxk.wxservice_server.entity.electronichealthcard.BatchRegisterParams;
|
|
|
-import thyyxxk.wxservice_server.entity.electronichealthcard.ElectronicHealthCard;
|
|
|
-import thyyxxk.wxservice_server.entity.electronichealthcard.RegisterParams;
|
|
|
-import thyyxxk.wxservice_server.entity.patientcards.WechatPatientBind;
|
|
|
-import thyyxxk.wxservice_server.utils.*;
|
|
|
-
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * 电子健康卡卡面规范及素材 <a href="https://open.tengmed.com/openAccess/docs/access#28">...</a>
|
|
|
- * */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class ElectronicHealthCardService {
|
|
|
- private final ElectronicHealthCardDao dao;
|
|
|
- private final IdCardAnalyzeService idCardAnalyzeService;
|
|
|
- private final RestTemplate template;
|
|
|
- private final RedisLikeService redis;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- public ElectronicHealthCardService(ElectronicHealthCardDao dao, IdCardAnalyzeService idCardAnalyzeService, RestTemplate template, RedisLikeService redis) {
|
|
|
- this.dao = dao;
|
|
|
- this.idCardAnalyzeService = idCardAnalyzeService;
|
|
|
- this.template = template;
|
|
|
- this.redis = redis;
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<List<ElectronicHealthCard>> getRegisteredCards() {
|
|
|
- QueryWrapper<ElectronicHealthCard> wrapper = new QueryWrapper<>();
|
|
|
- String openid = TokenUtil.getInstance().getUserOpenid();
|
|
|
- wrapper.eq("openid", openid);
|
|
|
- List<ElectronicHealthCard> healthCards = dao.selectList(wrapper);
|
|
|
- healthCards.forEach(item -> {
|
|
|
- String lastName = item.getName().substring(0, 1);
|
|
|
- item.setBlurName(item.getName().replaceFirst(lastName, "*"));
|
|
|
- String header = item.getIdNumber().substring(0, 4);
|
|
|
- String tail = "";
|
|
|
- if (item.getIdNumber().length() >= 15) {
|
|
|
- tail = item.getIdNumber().substring(14);
|
|
|
- }
|
|
|
- item.setBlurIdNumber(header + "**********" + tail);
|
|
|
- });
|
|
|
- return ResultVoUtil.success(healthCards);
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<JSONObject> analyseIdCard(UploadIdCardParam param) {
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- req.put("imageContent", param.getContent().split("base64,")[1]);
|
|
|
- req.put("cardType", 0);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/ocrInfo";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("解析身份证信息结果:{}", response);
|
|
|
- if (null == response) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- int resultCode = commonOut.getIntValue("resultCode");
|
|
|
- if (resultCode != 0) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "识别失败,请选择清晰的身份证正面照片或手动输入信息。");
|
|
|
- }
|
|
|
- JSONObject rsp = response.getJSONObject("rsp");
|
|
|
- return ResultVoUtil.success(rsp.getJSONObject("cardInfo"));
|
|
|
- }
|
|
|
-
|
|
|
- private String createHisCard(ElectronicHealthCard card) {
|
|
|
- IdCardAnalyzeResult idCard = new IdCardAnalyzeResult();
|
|
|
- idCard.setOpenid(card.getOpenid());
|
|
|
- idCard.setRelation(Objects.equals(card.getRelation(), "0") ? 1 : 2);
|
|
|
- idCard.setName(card.getName());
|
|
|
- idCard.setSocialNo(card.getIdNumber());
|
|
|
- idCard.setBirthday(card.getBirthday());
|
|
|
- idCard.setAddress(card.getAddress());
|
|
|
- idCard.setSex(DateUtil.calculateSex(card.getIdNumber()));
|
|
|
- idCard.setPhone(card.getPhone1());
|
|
|
- idCard.setCardType(2);
|
|
|
- idCard.setCardNo(card.getIdNumber());
|
|
|
-
|
|
|
- ResultVo<HashMap<String, Object>> response = idCardAnalyzeService.readInput(idCard);
|
|
|
- if (response.getCode() == ExceptionEnum.SUCCESS.getCode()) {
|
|
|
- return "SUCCESS";
|
|
|
- }
|
|
|
- return response.getMessage();
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<ElectronicHealthCard> linkHealthCard(String healthCode, String openid) {
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- req.put("healthCode", healthCode);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/getHealthCardByHealthCode";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("健康卡授权码获取健康卡信息:\n参数:{}\n结果:{}", params, response);
|
|
|
- if (null == response) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- int resultCode = commonOut.getIntValue("resultCode");
|
|
|
- if (resultCode != 0) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, commonOut.getString("errMsg"));
|
|
|
- }
|
|
|
- JSONObject rsp = response.getJSONObject("rsp");
|
|
|
- JSONObject cardObj = rsp.getJSONObject("card");
|
|
|
- ElectronicHealthCard card = JSONObject.parseObject(cardObj.toJSONString(), ElectronicHealthCard.class);
|
|
|
- if (null == openid) {
|
|
|
- openid = TokenUtil.getInstance().getUserOpenid();
|
|
|
- }
|
|
|
- card.setOpenid(openid);
|
|
|
- int existCount = dao.selectBindCount(card.getHealthCardId(), openid);
|
|
|
- if (existCount == 0) {
|
|
|
- dao.insert(card);
|
|
|
- }
|
|
|
- if (StringUtil.notBlank(card.getPatId())) {
|
|
|
- bindPatIdAndOpenid(card);
|
|
|
- return ResultVoUtil.success(card);
|
|
|
- }
|
|
|
- return dealHisCardBind(card);
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<ElectronicHealthCard> linkHealthCard(String healthCode) {
|
|
|
- return linkHealthCard(healthCode, null);
|
|
|
- }
|
|
|
-
|
|
|
- private void bindPatIdAndOpenid(ElectronicHealthCard card) {
|
|
|
- WechatPatientBind bind = dao.selectWechatPatientBind(card.getPatId(), card.getOpenid());
|
|
|
- if (null == bind) {
|
|
|
- int isDefault = Objects.equals(card.getRelation(), "0") ? 1 : 0;
|
|
|
- card.setIsDefault(isDefault);
|
|
|
- dao.insertWechatBind(card);
|
|
|
- } else {
|
|
|
- dao.unfrozenWechatBind(card.getIdNumber(), card.getPhone1(), bind.getId());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<ElectronicHealthCard> register(RegisterParams req) {
|
|
|
- if (!IdCardUtil.isValidatedIdCard(req.getIdNumber())) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请输入正确的身份证号码。");
|
|
|
- }
|
|
|
- if (StringUtil.isBlank(req.getGender())) {
|
|
|
- req.setGender(IdCardUtil.getGenderByIdCard(req.getIdNumber()).getLabel());
|
|
|
- }
|
|
|
- if (StringUtil.isBlank(req.getBirthday())) {
|
|
|
- req.setBirthday(IdCardUtil.getBirthDayByIdCard(req.getIdNumber()));
|
|
|
- }
|
|
|
-// if (StringUtil.isBlank(req.getNation())) {
|
|
|
- req.setNation("未知");
|
|
|
-// }
|
|
|
- String openid = TokenUtil.getInstance().getUserOpenid();
|
|
|
- req.setOpenid(openid);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", JSONObject.toJSON(req));
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/registerHealthCard";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("电子健康卡注册:\n参数:{}\n结果:{}", params, response);
|
|
|
- if (null == response) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- int resultCode = commonOut.getIntValue("resultCode");
|
|
|
- if (resultCode != 0) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, commonOut.getString("errMsg"));
|
|
|
- }
|
|
|
- JSONObject rsp = response.getJSONObject("rsp");
|
|
|
- ElectronicHealthCard card = new ElectronicHealthCard();
|
|
|
- card.setQrCodeText(rsp.getString("qrCodeText"));
|
|
|
- card.setHealthCardId(rsp.getString("healthCardId"));
|
|
|
- card.setPhid(rsp.getString("phid"));
|
|
|
- card.setName(req.getName());
|
|
|
- card.setGender(req.getGender());
|
|
|
- card.setNation(req.getNation());
|
|
|
- card.setBirthday(req.getBirthday());
|
|
|
- card.setIdNumber(req.getIdNumber());
|
|
|
- card.setRelation(req.getRelation());
|
|
|
- card.setAddress(req.getAddress());
|
|
|
- card.setPhone1(req.getPhone1());
|
|
|
- card.setOpenid(openid);
|
|
|
- int existCount = dao.selectBindCount(card.getHealthCardId(), openid);
|
|
|
- if (existCount == 0) {
|
|
|
- dao.insert(card);
|
|
|
- }
|
|
|
- return dealHisCardBind(card);
|
|
|
- }
|
|
|
-
|
|
|
- private ResultVo<ElectronicHealthCard> dealHisCardBind(ElectronicHealthCard card) {
|
|
|
- String patientId = dao.selectLatestBindPatientId(card.getIdNumber());
|
|
|
- if (null == patientId) {
|
|
|
- String crtRes = createHisCard(card);
|
|
|
- if (!crtRes.equals("SUCCESS")) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, crtRes);
|
|
|
- }
|
|
|
- patientId = dao.selectLatestBindPatientId(card.getIdNumber());
|
|
|
- }
|
|
|
- if (null == patientId) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "建卡错误,请联系管理员。");
|
|
|
- }
|
|
|
- card.setPatId(patientId);
|
|
|
- bindPatIdAndOpenid(card);
|
|
|
- dao.updateHealthCardBatchStatus(Constants.HealthCardBatchRegister.SUCCESS, card.getIdNumber());
|
|
|
- bindCardRelation(card.getHealthCardId(), card.getQrCodeText(), patientId);
|
|
|
- return ResultVoUtil.success(card);
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<String> bindCardRelation(String healthCardId, String qrCodeText, String patId) {
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- req.put("patId", patId);
|
|
|
- req.put("qrCodeText", qrCodeText);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/bindCardRelation";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("绑定健康卡和院内ID关系:\n参数:{}\n结果:{}", params, response);
|
|
|
- if (null == response) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- int resultCode = commonOut.getIntValue("resultCode");
|
|
|
- if (resultCode != 0) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, commonOut.getString("errMsg"));
|
|
|
- }
|
|
|
- dao.updatePatId(healthCardId, patId);
|
|
|
- reportHisData(patId, "050100", null, null);
|
|
|
- return ResultVoUtil.success("绑定健康卡和院内ID关系成功。");
|
|
|
- }
|
|
|
-
|
|
|
- public void registerBatchHealthCard() {
|
|
|
- List<BatchRegisterParams> list = dao.selectBatchRegisterParams();
|
|
|
- List<BatchRegisterParams> tempList = new ArrayList<>();
|
|
|
- for (BatchRegisterParams item : list) {
|
|
|
- if (!IdCardUtil.isValidatedIdCard(item.getIdNumber())) {
|
|
|
- dao.updateHealthCardBatchStatus(Constants.HealthCardBatchRegister.INVALID_ID_NUMBER, item.getIdNumber());
|
|
|
- continue;
|
|
|
- }
|
|
|
- item.setGender(IdCardUtil.getGenderByIdCard(item.getIdNumber()).getLabel());
|
|
|
- item.setBirthday(IdCardUtil.getBirthDayByIdCard(item.getIdNumber()));
|
|
|
- tempList.add(item);
|
|
|
- if (tempList.size() == 15) {
|
|
|
- executeRegisterBatchHealthCard(tempList);
|
|
|
- tempList.clear();
|
|
|
- }
|
|
|
- }
|
|
|
- if (!tempList.isEmpty()) {
|
|
|
- executeRegisterBatchHealthCard(tempList);
|
|
|
- tempList.clear();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void executeRegisterBatchHealthCard(List<BatchRegisterParams> userList) {
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- JSONArray healthCardItems = JSONArray.parseArray(JSONArray.toJSONString(userList));
|
|
|
- req.put("healthCardItems", healthCardItems);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/registerBatchHealthCard";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("绑定健康卡和院内ID关系:\n参数:{}\n结果:{}", params, response);
|
|
|
- if (null == response) {
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (commonOut.getIntValue("resultCode") != 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONObject rsp = response.getJSONObject("rsp");
|
|
|
- if (null == rsp) {
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONArray rspItems = rsp.getJSONArray("rspItems");
|
|
|
- if (null == rspItems) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (int i = 0; i < rspItems.size(); i++) {
|
|
|
- JSONObject item = rspItems.getJSONObject(i);
|
|
|
- if (null == item) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String qrCodeText = item.getString("qrCodeText");
|
|
|
- if (StringUtil.isBlank(qrCodeText)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String idNumber = item.getString("idNumber");
|
|
|
- dao.updateHealthCardBatchStatus(Constants.HealthCardBatchRegister.SUCCESS, idNumber);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<String> getOrderIdByOutAppId(String qrCodeText) {
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- req.put("appId", ElectronicHealthCardUtil.PROGRAMMER_APP_ID);
|
|
|
- req.put("qrCodeText", qrCodeText);
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/getOrderIdByOutAppId";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("获取卡包订单ID:\n参数:{}\n结果:{}", params, response);
|
|
|
- if (null == response) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- JSONObject commonOut = response.getJSONObject("commonOut");
|
|
|
- if (null == commonOut) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
- }
|
|
|
- int resultCode = commonOut.getIntValue("resultCode");
|
|
|
- if (resultCode != 0) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, commonOut.getString("errMsg"));
|
|
|
- }
|
|
|
- JSONObject rsp = response.getJSONObject("rsp");
|
|
|
- assert rsp != null;
|
|
|
- return ResultVoUtil.success(rsp.getString("orderId"));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param scene <a href="https://open.tengmed.com/openAccess/docs/develop#73">用卡环节</a>
|
|
|
- * @param dept 该字段可直接上报中文名称。当用卡环节是010101-挂号、0101011-预约挂号、0101012-当日挂号、0201016-在线问诊时,用卡科室数据为必填
|
|
|
- * @param cardCostTypes 用卡费别。当用卡环节代码为010105-收费、0101051-门诊缴费、0101053-住院缴费,则用卡费别数据为必填。
|
|
|
- * */
|
|
|
- public void reportHisData(String patId, String scene, String dept, String cardCostTypes) {
|
|
|
- new Thread(() -> {
|
|
|
- String qrCodeText = dao.selectQrCodeText(patId);
|
|
|
- if (null == qrCodeText) {
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONObject req = new JSONObject();
|
|
|
- req.put("qrCodeText", qrCodeText);
|
|
|
- req.put("time", DateUtil.formatDatetime(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
|
|
- req.put("hospitalCode", ElectronicHealthCardUtil.HOSPITAL_ID);
|
|
|
- req.put("scene", scene);
|
|
|
- req.put("cardType", "10");
|
|
|
- req.put("cardChannel", "0401");
|
|
|
- if (null != dept) {
|
|
|
- req.put("department", redis.getDepartmentName(dept));
|
|
|
- }
|
|
|
- if (null != cardCostTypes) {
|
|
|
- req.put("cardCostTypes", cardCostTypes);
|
|
|
- }
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("commonIn", new JSONObject());
|
|
|
- params.put("req", req);
|
|
|
- ElectronicHealthCardUtil.sign(params);
|
|
|
- String url = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/reportHISData";
|
|
|
- JSONObject response = template.postForObject(url, params, JSONObject.class);
|
|
|
- log.info("用卡数据上报:\n参数:{}\n结果:{}", params, response);
|
|
|
- }).start();
|
|
|
- }
|
|
|
-
|
|
|
- public ResultVo<String> getEncryptOpenid() {
|
|
|
- String openid = TokenUtil.getInstance().getUserOpenid();
|
|
|
- String uuid = UUID.randomUUID().toString().toUpperCase().replace("-","");
|
|
|
- String uoh = uuid + "&" + openid + "&" + ElectronicHealthCardUtil.HOSPITAL_ID;
|
|
|
- String encryptOpenid = ElectronicHealthCardUtil.encryptOpenid(uoh);
|
|
|
- log.info("获取加密openid: \n加密前:{}\n加密后:{}", uoh, encryptOpenid);
|
|
|
- return ResultVoUtil.success(encryptOpenid);
|
|
|
- }
|
|
|
-}
|