|
@ -0,0 +1,412 @@
|
|
|
package com.yihu.jw.security.service.healthCare;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.base.patient.BasePatientDO;
|
|
|
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
|
|
|
import com.yihu.jw.entity.hospital.family.WlyyPatientFamilyMemberDO;
|
|
|
import com.yihu.jw.entity.hospital.healthCare.*;
|
|
|
import com.yihu.jw.entity.hospital.httplog.YlzHttpLogDO;
|
|
|
|
|
|
import com.yihu.jw.security.dao.YlzHttpLogDao;
|
|
|
import com.yihu.jw.security.dao.YlzMedicalUserInfoDao;
|
|
|
import com.yihu.jw.security.dao.patient.BasePatientDao;
|
|
|
import com.yihu.jw.security.dao.patient.WlyyHospitalSysDictDao;
|
|
|
import com.yihu.jw.security.dao.patient.WlyyPatientFamilyMemberDao;
|
|
|
import com.yihu.jw.security.service.healthCare.utils.AES;
|
|
|
import com.yihu.jw.security.service.healthCare.utils.SafeUtil;
|
|
|
import com.yihu.jw.security.utils.ByteToInputStream;
|
|
|
import com.yihu.jw.util.common.QrcodeUtil;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import com.yihu.jw.util.encrypt.MD5;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
import com.yihu.jw.util.idcard.IdCardUtil;
|
|
|
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
|
|
import javax.transaction.Transactional;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 新版医保对接
|
|
|
* Created by wangzhinan 20211002
|
|
|
*/
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class HealthCareNewService {
|
|
|
private Logger logger = LoggerFactory.getLogger(HealthCareNewService.class);
|
|
|
@Autowired
|
|
|
private WlyyHospitalSysDictDao hospitalSysDictDao;
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
@Autowired
|
|
|
private YlzHttpLogDao ylzHttpLogDao;
|
|
|
@Autowired
|
|
|
private YlzMedicalUserInfoDao ylzMedicalUserInfoDao;
|
|
|
@Autowired
|
|
|
private BasePatientDao basePatientDao;
|
|
|
@Autowired
|
|
|
private WlyyPatientFamilyMemberDao familyMemberDao;
|
|
|
|
|
|
private String ylzConfigUrl;
|
|
|
private String ylzConfigAppid;
|
|
|
private String ylzConfigAppSecret;
|
|
|
private String ylzConfigEncryptKey;
|
|
|
private String ylzConfigSignKey;
|
|
|
private String ylzConfigOrgCode;
|
|
|
|
|
|
|
|
|
public static String entranceHealthCareUrl = "http://127.0.0.1:10023/healthCare/";
|
|
|
|
|
|
|
|
|
public void initConfig() throws Exception{
|
|
|
logger.info("初始话参数!");
|
|
|
List<WlyyHospitalSysDictDO> hospitalSysDictDOS = hospitalSysDictDao.findByDictName("ylzConfig");
|
|
|
if (hospitalSysDictDOS==null||hospitalSysDictDOS.size()==0){
|
|
|
throw new Exception("尚未添加配置参数");
|
|
|
}else {
|
|
|
for (WlyyHospitalSysDictDO hospitalSysDictDO:hospitalSysDictDOS){
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigUrl")){
|
|
|
ylzConfigUrl = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigAppid")){
|
|
|
ylzConfigAppid = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigAppSecret")){
|
|
|
ylzConfigAppSecret = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigEncryptKey")){
|
|
|
ylzConfigEncryptKey = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigSignKey")){
|
|
|
ylzConfigSignKey = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
if (hospitalSysDictDO.getId().equalsIgnoreCase("ylzConfigOrgCode")){
|
|
|
ylzConfigOrgCode = hospitalSysDictDO.getDictValue();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
public String getYlzToken() throws Exception {
|
|
|
YlzHttpLogDO ylzHttpLogDO = new YlzHttpLogDO();
|
|
|
String token = null;
|
|
|
initConfig();
|
|
|
String funid = "N00.00.00.01";
|
|
|
JSONObject data = new JSONObject();
|
|
|
data.put("appid",ylzConfigAppid);
|
|
|
data.put("appsecret",ylzConfigAppSecret);
|
|
|
data.put("grant_type","client_credentials");
|
|
|
JSONObject object = new JSONObject();
|
|
|
object.put("sign_type","md5");
|
|
|
object.put("appid",ylzConfigAppid);
|
|
|
/* String stringSignTemp ="appid="+ylzConfigAppid+"&appsecret="+ylzConfigAppSecret+"&data="+data+"&funid="+funid+"&sign_type=md5&key="+ylzConfigSignKey;
|
|
|
String sign = MD5.md5(stringSignTemp).toUpperCase();*/
|
|
|
object.put("data",data);
|
|
|
object.put("funid",funid);
|
|
|
String signResult = SafeUtil.sign(object.toJSONString(),ylzConfigSignKey);
|
|
|
String obj = SafeUtil.encrypt(signResult,ylzConfigEncryptKey);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(obj);
|
|
|
logger.info("请求前funid=="+funid+"data="+data);
|
|
|
String response = httpClientUtil.postBody(ylzConfigUrl,jsonObject);
|
|
|
ylzHttpLogDO.setResponseEncrpty(response);
|
|
|
logger.info("请求后response=="+response);
|
|
|
if (response!=null&&response!=""){
|
|
|
JSONObject result = JSONObject.parseObject(response);
|
|
|
if (result.getString("flag").equalsIgnoreCase("1")){
|
|
|
String dataResult = result.getString("encrypt_data");
|
|
|
result.remove("encrypt_data");
|
|
|
if (dataResult!=null&&dataResult!=""){
|
|
|
String decryptResult = AES.aesDecrypt(dataResult,ylzConfigEncryptKey);
|
|
|
JSONObject dataObject = JSONObject.parseObject(decryptResult);
|
|
|
result.put("encrypt_data",dataObject);
|
|
|
response = result.toString();
|
|
|
token = dataObject.getString("access_token");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
ylzHttpLogDO.setCode(funid);
|
|
|
ylzHttpLogDO.setName("获取AccessToken");
|
|
|
ylzHttpLogDO.setRequest(data.toJSONString());
|
|
|
ylzHttpLogDO.setRequestEncrpty(obj);
|
|
|
ylzHttpLogDO.setResponse(response);
|
|
|
ylzHttpLogDO.setCreateTime(new Date());
|
|
|
ylzHttpLogDao.save(ylzHttpLogDO);
|
|
|
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 医保统一调用接口
|
|
|
* @param data
|
|
|
* @param funid
|
|
|
* @param name
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@org.springframework.transaction.annotation.Transactional(propagation= Propagation.NOT_SUPPORTED)
|
|
|
public String requestYlz(JSONObject data,String funid,String name) throws Exception {
|
|
|
String obj="";
|
|
|
try {
|
|
|
YlzHttpLogDO ylzHttpLogDO = new YlzHttpLogDO();
|
|
|
initConfig();
|
|
|
|
|
|
WlyyHospitalSysDictDO systemDict = hospitalSysDictDao.findOneByDictNameAndDictCode("ylzConfig","testFlag");
|
|
|
if (null!=systemDict&&"1".equalsIgnoreCase(systemDict.getDictValue())){//测试环境
|
|
|
//N00.00.00.01接口外其他的接口在服务号前加个T就是测试
|
|
|
funid ="N00.00.00.01".equalsIgnoreCase(funid)?funid:"T"+funid;
|
|
|
System.out.println("funid="+funid);
|
|
|
}
|
|
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
object.put("sign_type","md5");
|
|
|
object.put("appid",ylzConfigAppid);
|
|
|
String accessToken = getYlzToken();
|
|
|
if (accessToken==null){
|
|
|
throw new Exception("token无效");
|
|
|
}
|
|
|
object.put("access_token",accessToken);
|
|
|
object.put("funid",funid);
|
|
|
data.put("appid",ylzConfigAppid);
|
|
|
data.put("org_code",ylzConfigOrgCode);
|
|
|
object.put("data",data);
|
|
|
|
|
|
String signResult = SafeUtil.sign(object.toJSONString(),ylzConfigSignKey);
|
|
|
obj = SafeUtil.encrypt(signResult,ylzConfigEncryptKey);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(obj);
|
|
|
/* obj = YibaoJiaMi.encryptData(object,ylzConfigSignKey,ylzConfigEncryptKey);
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(obj);*/
|
|
|
logger.info("请求前funid=="+funid+"data="+data.toJSONString());
|
|
|
String response = httpClientUtil.postBody(ylzConfigUrl,jsonObject);
|
|
|
ylzHttpLogDO.setResponseEncrpty(response);
|
|
|
if (response!=null&&response!=""){
|
|
|
JSONObject result = JSONObject.parseObject(response);
|
|
|
if (result.getString("flag").equalsIgnoreCase("1")){
|
|
|
String dataResult = result.getString("encrypt_data");
|
|
|
result.remove("encrypt_data");
|
|
|
if (dataResult!=null&&dataResult!=""){
|
|
|
String decryptResult = AES.aesDecrypt(dataResult,ylzConfigEncryptKey);
|
|
|
JSONObject dataObject = JSONObject.parseObject(decryptResult);
|
|
|
/* String decryptResult = YibaoJiaMi.decrypt(dataResult,ylzConfigEncryptKey);
|
|
|
JSONObject dataObject = JSONObject.parseObject(decryptResult);*/
|
|
|
result.put("encrypt_data",dataObject);
|
|
|
response = result.toJSONString();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
ylzHttpLogDO.setCode(funid);
|
|
|
ylzHttpLogDO.setName(name);
|
|
|
ylzHttpLogDO.setRequest(data.toJSONString());
|
|
|
ylzHttpLogDO.setRequestEncrpty(obj);
|
|
|
ylzHttpLogDO.setResponse(response);
|
|
|
ylzHttpLogDO.setCreateTime(new Date());
|
|
|
ylzHttpLogDao.save(ylzHttpLogDO);
|
|
|
return response;
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
StringWriter sw = new StringWriter();
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
e.printStackTrace(pw);
|
|
|
String error = sw.toString();
|
|
|
YlzHttpLogDO ylzHttpLogDO = new YlzHttpLogDO();
|
|
|
ylzHttpLogDO.setCode(funid);
|
|
|
ylzHttpLogDO.setName(name);
|
|
|
ylzHttpLogDO.setRequest(data.toJSONString());
|
|
|
ylzHttpLogDO.setRequestEncrpty(obj);
|
|
|
ylzHttpLogDO.setResponse(error);
|
|
|
ylzHttpLogDO.setCreateTime(new Date());
|
|
|
ylzHttpLogDao.save(ylzHttpLogDO);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public Boolean isHospitalFlag(){
|
|
|
WlyyHospitalSysDictDO hospitalSysDictDO = hospitalSysDictDao.findById("medicalFlag").get();
|
|
|
if (hospitalSysDictDO!=null){
|
|
|
if (hospitalSysDictDO.getDictValue().equalsIgnoreCase("1")){
|
|
|
return true;
|
|
|
}else {
|
|
|
return false;
|
|
|
}
|
|
|
}else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取用户信息(N03.00.05.16)
|
|
|
*
|
|
|
* 返回值
|
|
|
*
|
|
|
* @param code 用户信息临时串
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String getUserInfo(String code) throws Exception{
|
|
|
String funid = "N03.00.05.16";
|
|
|
JSONObject data = new JSONObject();
|
|
|
data.put("code",code);//用户信息临时串
|
|
|
logger.info("data"+data.toJSONString());
|
|
|
String result = requestYlz(data,funid,"获取用户信息");
|
|
|
if (result==null){
|
|
|
throw new Exception("获取用户信息失败");
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取医保二维码
|
|
|
*
|
|
|
* @return
|
|
|
* @throws UnsupportedEncodingException
|
|
|
*/
|
|
|
public Map<String,Object> getYbImage() throws UnsupportedEncodingException {
|
|
|
String no = UUID.randomUUID().toString().substring(0,30);
|
|
|
YlzMedicalUserInfoDO ylzMedicalUserInfoDO = new YlzMedicalUserInfoDO();
|
|
|
ylzMedicalUserInfoDO.setNo(no);
|
|
|
String redirectUrl = "https://hlwyy.xmhcyy.com/hcyy/ims-wx/#/login?no="+no;
|
|
|
String url = "https://service.ylbz.xm.gov.cn/xmyb/web-ixm/app/#/ecAuth?redirectUrl="+ URLEncoder.encode(redirectUrl, "UTF-8");
|
|
|
ylzMedicalUserInfoDO.setUrl(url);
|
|
|
ylzMedicalUserInfoDO.setCreateTime(new Date());
|
|
|
ylzMedicalUserInfoDao.save(ylzMedicalUserInfoDO);
|
|
|
ByteToInputStream byteToInputStream = new ByteToInputStream();
|
|
|
InputStream qrcode = QrcodeUtil.createQrcode(url,300,"jpg");
|
|
|
String QRCodeImg = byteToInputStream.getBase64FromInputStream(qrcode);
|
|
|
Map<String,Object> map = new HashedMap();
|
|
|
map.put("QRCodeImg","data:image/png;base64,"+ QRCodeImg);
|
|
|
map.put("url",url);
|
|
|
map.put("out_authorize_no",no);
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 医保授权保存患者信息
|
|
|
*
|
|
|
* @param no
|
|
|
* @param token
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public BasePatientDO updateUserInfo(String no,String token) throws Exception {
|
|
|
BasePatientDO patientDO = new BasePatientDO();
|
|
|
YlzMedicalUserInfoDO ylzMedicalUserInfoDO = ylzMedicalUserInfoDao.findByNo(no);
|
|
|
if (ylzMedicalUserInfoDO==null){
|
|
|
ylzMedicalUserInfoDO = new YlzMedicalUserInfoDO();
|
|
|
no = UUID.randomUUID().toString().substring(0,30);
|
|
|
ylzMedicalUserInfoDO.setNo(no);
|
|
|
}
|
|
|
String result = getUserInfo(token);
|
|
|
ylzMedicalUserInfoDO.setToken(token);
|
|
|
ylzMedicalUserInfoDO.setJson(result);
|
|
|
ylzMedicalUserInfoDO = ylzMedicalUserInfoDao.save(ylzMedicalUserInfoDO);
|
|
|
JSONObject object = JSONObject.parseObject(result);
|
|
|
logger.info(object.toJSONString());
|
|
|
if (object.getString("flag").equalsIgnoreCase("1")){
|
|
|
JSONObject data = object.getJSONObject("encrypt_data");
|
|
|
String idcard = data.getString("idcard");
|
|
|
String name = data.getString("name");
|
|
|
String cardno = data.getString("cardno");
|
|
|
String sex = data.getString("sex");
|
|
|
ylzMedicalUserInfoDO.setIdcard(idcard);
|
|
|
ylzMedicalUserInfoDO.setName(name);
|
|
|
patientDO = basePatientDao.findByIdcardAndDel(idcard,"1");
|
|
|
if(StringUtils.isNotBlank(idcard)&&patientDO == null){
|
|
|
BasePatientDO patient = new BasePatientDO();
|
|
|
String salt = UUID.randomUUID().toString().substring(0,5);
|
|
|
String pw = null;
|
|
|
pw = idcard.substring(idcard.length()-6);
|
|
|
patient.setIdcard(idcard);
|
|
|
patient.setName(name);
|
|
|
patient.setPassword(MD5.md5Hex(pw + "{" + salt + "}"));
|
|
|
patient.setSalt(salt);
|
|
|
patient.setDel("1");
|
|
|
patient.setEnabled(1);
|
|
|
patient.setLocked(0);
|
|
|
patient.setSex(Integer.parseInt(IdCardUtil.getSexForIdcard_new(idcard)));
|
|
|
patient.setCreateTime(new Date());
|
|
|
patient.setUpdateTime(new Date());
|
|
|
patient.setBirthday(DateUtil.strToDate(IdCardUtil.getBirthdayForIdcardStr(idcard),"yyyyMMdd"));
|
|
|
patientDO = basePatientDao.save(patient);
|
|
|
WlyyPatientFamilyMemberDO basePatientFamilyMemberDO = familyMemberDao.findFamilyMemberByPatientAndRelationCode(patientDO.getId(),"7");
|
|
|
if (basePatientFamilyMemberDO==null){
|
|
|
basePatientFamilyMemberDO = new WlyyPatientFamilyMemberDO();
|
|
|
basePatientFamilyMemberDO.setPatient(patientDO.getId());
|
|
|
basePatientFamilyMemberDO.setFamilyRelation("7");
|
|
|
basePatientFamilyMemberDO.setFamilyRelationName("自己");
|
|
|
basePatientFamilyMemberDO.setCardType("身份证");
|
|
|
basePatientFamilyMemberDO.setCardNo(patientDO.getIdcard());
|
|
|
basePatientFamilyMemberDO.setCreateTime(new Date());
|
|
|
basePatientFamilyMemberDO.setUpdateTime(new Date());
|
|
|
basePatientFamilyMemberDO.setIsAuthorize(1);
|
|
|
basePatientFamilyMemberDO.setIsDel(1);
|
|
|
basePatientFamilyMemberDO.setFamilyMember(patientDO.getId());
|
|
|
familyMemberDao.save(basePatientFamilyMemberDO);
|
|
|
}
|
|
|
}else {
|
|
|
basePatientDao.save(patientDO);
|
|
|
WlyyPatientFamilyMemberDO basePatientFamilyMemberDO = familyMemberDao.findFamilyMemberByPatientAndRelationCode(patientDO.getId(), "7");
|
|
|
if (basePatientFamilyMemberDO == null) {
|
|
|
basePatientFamilyMemberDO = new WlyyPatientFamilyMemberDO();
|
|
|
basePatientFamilyMemberDO.setPatient(patientDO.getId());
|
|
|
basePatientFamilyMemberDO.setFamilyRelation("7");
|
|
|
basePatientFamilyMemberDO.setFamilyRelationName("自己");
|
|
|
basePatientFamilyMemberDO.setCardType("身份证");
|
|
|
basePatientFamilyMemberDO.setCardNo(patientDO.getIdcard());
|
|
|
basePatientFamilyMemberDO.setCreateTime(new Date());
|
|
|
basePatientFamilyMemberDO.setUpdateTime(new Date());
|
|
|
basePatientFamilyMemberDO.setIsAuthorize(1);
|
|
|
basePatientFamilyMemberDO.setIsDel(1);
|
|
|
basePatientFamilyMemberDO.setFamilyMember(patientDO.getId());
|
|
|
familyMemberDao.save(basePatientFamilyMemberDO);
|
|
|
}
|
|
|
}
|
|
|
logger.info("获取用户信息成功");
|
|
|
}
|
|
|
return patientDO;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 验证医保凭证是否授权
|
|
|
* @param authorizeNo
|
|
|
* @return
|
|
|
*/
|
|
|
public Map<String,Object> checkOauthQRCode(String authorizeNo) throws Exception {
|
|
|
YlzMedicalUserInfoDO ylzMedicalUserInfoDO = ylzMedicalUserInfoDao.findByNo(authorizeNo);
|
|
|
Map<String, Object> rsMap = new HashedMap();
|
|
|
if (ylzMedicalUserInfoDO != null) {
|
|
|
if (StringUtils.isNoneBlank(ylzMedicalUserInfoDO.getIdcard())) {
|
|
|
BasePatientDO patientDO = new BasePatientDO();
|
|
|
patientDO = basePatientDao.findByIdcardAndDel(ylzMedicalUserInfoDO.getIdcard(),"1");
|
|
|
logger.info("获取用户信息成功");
|
|
|
rsMap.put("sCode", "succ");
|
|
|
rsMap.put("sMes", "success");
|
|
|
rsMap.put("patient",patientDO);
|
|
|
}else {
|
|
|
rsMap.put("sCode", "ing");
|
|
|
rsMap.put("sMes", "正在授权");
|
|
|
}
|
|
|
} else {
|
|
|
rsMap.put("sCode", "ing");
|
|
|
rsMap.put("sMes", "正在授权");
|
|
|
}
|
|
|
return rsMap;
|
|
|
}
|
|
|
}
|