微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
10 KiB

package com.ynxbd.common.service;
import com.ynxbd.common.bean.Patient;
import com.ynxbd.common.bean.enums.HCardTypeEnum;
import com.ynxbd.common.dao.PatientDao;
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;
import java.util.Map;
@Slf4j
public class GMCService {
/**
* 子服务同步患者数据
*
* @param request request
* @param wxOpenId 子服务的openId
* @param gmcOpenId 主服务的openId
* @return list
*/
public List<Patient> 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<>();
}
long begTime = System.currentTimeMillis();
JsonResult jsonResult = postFormGMC(request, "/patient/queryPatientList", params -> {
params.put("openId", AesWxHelper.encode(gmcOpenId));
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<Patient> gmcPatients = jsonResult.getDataMapList(Patient.class, "data");
List<Integer> removeIds = new ArrayList<>(); // 需删除用户ids
List<Patient> addList = new ArrayList<>(); // 需添加用户
List<Patient> dbPatients = new PatientDao().selectListByToken(wxOpenId, unionId);
for (Patient item : gmcPatients) {
item.setId(null);
item.setOpenid(wxOpenId);
Patient findDBItem = dbPatients.isEmpty() ? null : dbPatients.stream().filter(o -> (!ObjectUtils.isEmpty(o.getPatientId()) && o.getPatientId().equals(item.getPatientId()))).findFirst().orElse(null);
if (findDBItem == null) { // 需新增
addList.add(item);
} else { // 比对数据
item.setId(findDBItem.getId());
if (HCardTypeEnum.NO_CARD.WX_CODE.equals(item.getCardType())) { // 无证绑定->不做处理
removeIds.add(findDBItem.getId());
} else {
if (!findDBItem.equalsPatient(item)) { // 数据不同->需修改本地数据
removeIds.add(findDBItem.getId());
addList.add(item);
}
}
}
}
for (Patient item : dbPatients) {
Patient findItem = gmcPatients.stream().filter(o -> (!ObjectUtils.isEmpty(o.getPatientId()) && o.getPatientId().equals(item.getPatientId()))).findFirst().orElse(null);
if (findItem == null) { // 本地数据多余->移除
removeIds.add(item.getId());
}
}
int delRows = 0, addRows = 0;
if (!removeIds.isEmpty()) {
delRows = new PatientDao().delByIds(wxOpenId, removeIds);
}
if (!addList.isEmpty()) {
addRows = new PatientDao().insertBatch(addList);
}
long endTime = System.currentTimeMillis();
String takeTime = (endTime - begTime) + "ms"; // 耗时
log.info("[医共体-数据同步][{}]][耗时:{}]-共[{}]条数据, 删除:[{}]条, 同步:[{}]条", wxOpenId, takeTime, gmcPatients.size(), delRows, addRows);
return gmcPatients;
}
/**
* 医共体绑定
*
* @param request request
* @param bindInfo 绑定信息
*/
public Map<String, Object> 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参数异常");
}
JsonResult jsonResult = postFormGMC(request, "/healthCode/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("hospAppId", WeChatConfig.APP_ID);
}, null);
if (!jsonResult.success()) {
throw new ServiceException(jsonResult.getMessage());
}
log.info("[医供体]绑定转发 resp={}", JsonHelper.toJsonString(jsonResult));
return jsonResult.getDataMap();
}
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;
}
}
}