package com.ynxbd.common.service; import com.alibaba.fastjson.JSONObject; import com.ynxbd.common.bean.GMCUser; import com.ynxbd.common.bean.Patient; import com.ynxbd.common.bean.enums.HCardTypeEnum; import com.ynxbd.common.dao.GMCUserDao; import com.ynxbd.common.dao.PatientDao; import com.ynxbd.common.dao.his.HisPatientDao; import com.ynxbd.common.helper.common.JsonHelper; import com.ynxbd.common.helper.http.OkHttpHelper; import com.ynxbd.common.result.JsonResult; import com.ynxbd.common.result.JsonResultEnum; import com.ynxbd.common.result.ServiceException; import com.ynxbd.wx.config.WeChatConfig; import com.ynxbd.wx.wxfactory.AesWxHelper; import com.ynxbd.wx.wxfactory.WxAuthHelper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; @Slf4j public class GMCService { /** * 子服务同步患者数据 * * @param request request * @param wxOpenId 子服务的openId * @param gmcOpenId 主服务的openId * @return list */ public List syncPatientData(HttpServletRequest request, String wxOpenId, String gmcOpenId, String unionId) throws ServiceException { log.error("[医共体-数据同步]wxOpenId={}, gmcOpenId={}", wxOpenId, gmcOpenId); if (ObjectUtils.isEmpty(wxOpenId) || ObjectUtils.isEmpty(gmcOpenId)) { return new ArrayList<>(); } if (!WeChatConfig.IS_ENABLE_GMC || WeChatConfig.IS_GMC_SERVER) { // 医共体开关未开启 || 是主服务器 return new ArrayList<>(); } String enGmcOpenId = AesWxHelper.encode(gmcOpenId); long begTime = System.currentTimeMillis(); JsonResult jsonResult = postFormGMC(request, "/patient/queryPatientList", params -> { params.put("openId", enGmcOpenId); params.put("hospAppId", AesWxHelper.encode(WeChatConfig.GMC_APP_ID)); }, null); if (!jsonResult.success()) { String message = jsonResult.getMessage(); log.error("[医共体-数据同步]请求主服务失败 wxOpenId={}, gmcOpenId={}, message={}", wxOpenId, gmcOpenId, message); throw new ServiceException("[医共体-数据同步]请求主服务失败:" + (ObjectUtils.isEmpty(message) ? "" : message)); } List gmcPatients = jsonResult.getDataMapList(Patient.class, "data"); List addList = new ArrayList<>(); // 需添加用户 List removeIds = new ArrayList<>(); // 需删除用户ids List dbPatients = new PatientDao().selectListByToken(wxOpenId, unionId); for (Patient item : gmcPatients) { item.setId(null); item.setOpenid(wxOpenId); if (HCardTypeEnum.NO_CARD.WX_CODE.equals(item.getCardType()) || ObjectUtils.isEmpty(item.getIdCardNo())) { // 无证绑定->不做处理 continue; } Patient findDBItem = dbPatients.isEmpty() ? null : dbPatients.stream().filter(o -> ( !ObjectUtils.isEmpty(o.getEmpiId()) && o.getEmpiId().equals(item.getEmpiId()) && !ObjectUtils.isEmpty(o.getIdCardNo()) && o.getIdCardNo().equals(item.getIdCardNo()) )).findFirst().orElse(null); if (findDBItem == null) { // 需新增 item.setPatientId(null); // 清空本院患者id addList.add(item); } else { // 比对数据 if (!findDBItem.equalsPatient(item)) { // 数据不同->需修改本地数据 item.setId(findDBItem.getId()); item.setPatientId(findDBItem.getPatientId()); removeIds.add(findDBItem.getId()); addList.add(item); } } } for (Patient item : dbPatients) { Patient findItem = gmcPatients.stream().filter(o -> ( !ObjectUtils.isEmpty(o.getEmpiId()) && o.getEmpiId().equals(item.getEmpiId())) && !ObjectUtils.isEmpty(o.getIdCardNo()) && o.getIdCardNo().equals(item.getIdCardNo()) ).findFirst().orElse(null); if (findItem == null) { // 本地数据多余->发送给主体医院=>进行绑定 Integer gmcBindState = item.getGmcBindState(); if (gmcBindState == null || gmcBindState == 0) { // 未与主体医院交互校验过 System.out.println("[医共体]向主体医院同步数据..."); System.out.println(JsonHelper.toJsonString(item)); item.setEnGmcOpenId(enGmcOpenId); Patient patient = new GMCService().bindGmcServer(request, item); if (patient == null) { log.error("[医共体]同步用户数据,返回数据为空 id:{}", item.getId()); continue; } String empiId = patient.getEmpiId(); String gmcUniqueId = patient.getGmcUniqueId(); if (ObjectUtils.isEmpty(gmcUniqueId) || ObjectUtils.isEmpty(empiId)) { log.error("[医共体]同步用户数据,返回唯一键为空 id:{}", item.getId()); continue; } if (!empiId.equals(item.getEmpiId())) { removeIds.add(item.getId()); continue; } if (new PatientDao().updateGmcBindState(gmcUniqueId, item.getId(), item.getEmpiId())) { log.error("[医共体]同步用户数据,修改医共体绑定状态失败 id:{}", item.getId()); item.setGmcUniqueId(gmcUniqueId); gmcPatients.add(item); } } else { // 已校验过=>多余数据=>移除 removeIds.add(item.getId()); } } } // log.info("[医共体-数据同步 本地{}条 移除[{}], add:{}", dbPatients.size(), removeIds, JsonHelper.toJsonString(addList)); int delRows = 0; if (!removeIds.isEmpty()) { delRows = new PatientDao().delByIds(wxOpenId, removeIds); } int addRows = addItems(addList); long endTime = System.currentTimeMillis(); String takeTime = (endTime - begTime) + "ms"; // 耗时 log.info("[医共体-数据同步][{}]][耗时:{}]-共[{}]条数据, 删除:[{}]条, 同步:[{}]条", wxOpenId, takeTime, gmcPatients.size(), delRows, addRows); return gmcPatients; } public int addItems(List patients) { HisPatientDao hisPatientDao = new HisPatientDao(); int addRows = 0; List addList = new ArrayList<>(); if (!patients.isEmpty()) { for (Patient item : patients) { try { if (ObjectUtils.isEmpty(item.getEmpiId())) { log.info("[医共体-数据同步]本院绑定失败"); continue; } Patient bindInfo = hisPatientDao.bind(false, item); if (bindInfo == null) { log.info("[医共体-数据同步]本院绑定失败"); continue; } String patientId = bindInfo.getPatientId(); // 本院患者id if (ObjectUtils.isEmpty(patientId)) { continue; } item.setPatientId(patientId); addList.add(item); } catch (Exception e) { log.error(e.getMessage()); } } addRows = new PatientDao().insertBatch(addList); } return addRows; } /** * 医共体绑定 * * @param request request * @param bindInfo 绑定信息 */ public Patient bindGmcServer(HttpServletRequest request, Patient bindInfo) throws ServiceException { String enGmcOpenId = bindInfo.getEnGmcOpenId(); // 只有子服务器有主服务器的openId String gmcOpenId = AesWxHelper.decode(enGmcOpenId); log.info("[医共体绑定-转发]enGmcOpenId={}, gmcOpenId={}", enGmcOpenId, gmcOpenId); if (ObjectUtils.isEmpty(gmcOpenId)) { throw new ServiceException("医共体非主体公众号ID参数异常"); } String idCardNo = bindInfo.getIdCardNo(); if (ObjectUtils.isEmpty(idCardNo)) { throw new ServiceException("[医共体绑定]证件号缺失"); } String areaCode = bindInfo.getAreaCode(); if (ObjectUtils.isEmpty(areaCode)) { areaCode = idCardNo.substring(0, 6); } String finalAreaCode = areaCode; JsonResult jsonResult = postFormGMC(request, "/healthCode/bind", params -> { params.put("isAreaCode", false); params.put("isFace", false); params.put("isHealthCard", false); params.put("address", bindInfo.getAddress()); params.put("areaCode", finalAreaCode); params.put("areaAddress", bindInfo.getAreaAddress()); params.put("tel", bindInfo.getTel()); params.put("sex", bindInfo.getSex()); params.put("name", bindInfo.getName()); params.put("nation", bindInfo.getNation()); params.put("birthday", bindInfo.getBirthday()); params.put("idCardNo", bindInfo.getIdCardNo()); params.put("enOpenId", enGmcOpenId); // 主服务器的openId params.put("enUnionId", bindInfo.getEnUnionId()); params.put("hospAppId", WeChatConfig.APP_ID); }, null); if (!jsonResult.success()) { throw new ServiceException(jsonResult.getMessage()); } JSONObject resp = jsonResult.dataMapGetNodeToJsonObj(); log.info("[医供体]绑定转发 resp={}", JsonHelper.toJsonString(resp)); if (resp == null) { return null; } String gmcUniqueId = resp.getString("gmcUniqueId"); String enPatientId = resp.getString("enPatientId"); String enEmpiId = resp.getString("enEmpiId"); String enOpenId = resp.getString("enOpenId"); if (ObjectUtils.isEmpty(gmcUniqueId) || ObjectUtils.isEmpty(enPatientId) || ObjectUtils.isEmpty(enOpenId) || ObjectUtils.isEmpty(enEmpiId)) { return null; } String patientId = AesWxHelper.decode(enPatientId); if (ObjectUtils.isEmpty(patientId)) { log.error("[医共体]主体绑定-数据enPatientId解密失败"); return null; } String empiId = AesWxHelper.decode(enEmpiId); if (ObjectUtils.isEmpty(enEmpiId)) { log.error("[医共体]主体绑定-数据enEmpiId解密失败"); return null; } Patient patient = new Patient(); patient.setPatientId(patientId); patient.setEnPatientId(enPatientId); patient.setEnOpenId(enOpenId); patient.setGmcUniqueId(gmcUniqueId); patient.setEnEmpiId(enEmpiId); patient.setEmpiId(empiId); return patient; } /** * 医共体解绑 * * @param request request */ public boolean unBindGmcServer(HttpServletRequest request, String openId, String epId) throws ServiceException { log.info("[医共体解绑-转发]openId={}, epId={}", openId, epId); if (ObjectUtils.isEmpty(openId) || ObjectUtils.isEmpty(epId)) { throw new ServiceException("[医共体解绑-转发]参数缺失"); } GMCUser gmcUser = new GMCUserDao().selectByOpenId(openId); if (gmcUser == null) { throw new ServiceException("[医共体解绑-转发]未匹配到用户id"); } String gmcOpenId = gmcUser.getGmcOpenId(); JsonResult jsonResult = postFormGMC(request, "/patient/unBind", params -> { params.put("openid", gmcOpenId); params.put("epId", epId); params.put("hospAppId", WeChatConfig.APP_ID); }, null); if (!jsonResult.success()) { throw new ServiceException(jsonResult.getMessage()); } return true; } public static JsonResult postForm(String url, OkHttpHelper.MapParams params, OkHttpHelper.Header header) { return OkHttpHelper.postForm(url, params, header, JsonResultEnum.SYS_COMMON); } public static JsonResult postFormGMC(HttpServletRequest request, String api, OkHttpHelper.MapParams params, OkHttpHelper.Header header) { String url = WeChatConfig.getGMCAuthDomain(WxAuthHelper.isHttpsWithProxy(request), true); // 医共体请求服务地址 return OkHttpHelper.postForm((url + api), params, header, JsonResultEnum.SYS_COMMON); } // public Patient queryGmcPatientList(HttpServletRequest request, Patient bindInfo) throws ServiceException { // if (!WeChatConfig.IS_ENABLE_GMC) { // 不为医共体主体 // return bindInfo; // } // String openid = bindInfo.getOpenid(); // String hospAppId = AesWxHelper.decode(bindInfo.getEnHospAppId()); // if (ObjectUtils.isEmpty(hospAppId)) { // throw new ServiceException("医共体子公众号id参数错误"); // } // // String gmcUniqueId; // if (WeChatConfig.IS_GMC_SERVER) { // 是医共体主服务器 // if (!WeChatConfig.APP_ID.equals(hospAppId)) { // 不为自身AppId // bindInfo.setHospAppId(hospAppId); // 需存的数据 // } // bindInfo.setGmcUniqueId(CodeHelper.get32UUID()); // // } else { // String enGmcOpenId = bindInfo.getEnGmcOpenId(); // 只有子公众号有 // String gmcOpenId = AesWxHelper.decode(enGmcOpenId); // // log.info("[转发]enGmcOpenId={}, gmcOpenId={}", enGmcOpenId, gmcOpenId); // if (ObjectUtils.isEmpty(gmcOpenId)) { // throw new ServiceException("医共体非主体公众号ID参数异常"); // } // // JsonResult jsonResult = WxAuthHelper.postFormGMC(request, "/patient/bind", params -> { // params.put("isAreaCode", false); // params.put("isFace", false); // params.put("isHealthCard", false); // // params.put("openid", gmcOpenId); // 主体的openid // params.put("address", bindInfo.getAddress()); // params.put("areaCode", bindInfo.getAreaCode()); // params.put("areaAddress", bindInfo.getAreaAddress()); // params.put("tel", bindInfo.getTel()); // params.put("sex", bindInfo.getSex()); // params.put("name", bindInfo.getName()); // params.put("nation", bindInfo.getNation()); // params.put("birthday", bindInfo.getBirthday()); // params.put("idCardNo", bindInfo.getIdCardNo()); // // // params.put("enOpenId", enGmcOpenId); // params.put("enUnionId", bindInfo.getEnUnionId()); // params.put("enGmcOpenId", enGmcOpenId); // params.put("enHospAppId", AesWxHelper.encode(WeChatConfig.APP_ID)); // }, null); // if (!jsonResult.success()) { // throw new ServiceException(jsonResult.getMessage()); // } // log.info("[绑定转发]resp={}", JsonHelper.toJsonString(jsonResult)); // // JSONObject dataJson = jsonResult.dataMapGetNodeToJsonObj(); // gmcUniqueId = dataJson.getString("gmcUniqueId"); // if (ObjectUtils.isEmpty(gmcUniqueId)) { // throw new ServiceException("医共体返回唯一id为空"); // } // bindInfo.setGmcUniqueId(gmcUniqueId); // if (!enGmcOpenId.equals(dataJson.getString("enOpenId"))) { // throw new ServiceException("医共体主体openId不匹配"); // } // // GMCUser findGMCUser = new GMCUserService().queryInfoByOpenId(openid); // if (findGMCUser == null) { // boolean isOK = new GMCUserService().addInfo(openid, gmcOpenId, bindInfo.getUnionId(), gmcUniqueId); // if (!isOK) { // log.error("[医共体关系]添加失败 openid={}, gmcOpenId={}, uuid={}", openid, gmcOpenId, gmcUniqueId); // } // } // } // return bindInfo; // } public void queryPatientList(HttpServletRequest request, String openId, String unionId, boolean isEnPid) throws ServiceException { if (!WeChatConfig.IS_ENABLE_GMC) { return; } } }