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.
		
		
		
		
			
				
					
					
						
							820 lines
						
					
					
						
							32 KiB
						
					
					
				
			
		
		
	
	
							820 lines
						
					
					
						
							32 KiB
						
					
					
				package com.ynxbd.common.service;
 | 
						|
 | 
						|
import com.alibaba.fastjson.JSONArray;
 | 
						|
import com.alibaba.fastjson.JSONObject;
 | 
						|
import com.tencent.healthcard.impl.HealthCardServerImpl;
 | 
						|
import com.tencent.healthcard.model.CommonIn;
 | 
						|
import com.tencent.healthcard.model.HealthCardInfo;
 | 
						|
import com.tencent.healthcard.model.ReportHISData;
 | 
						|
import com.ynxbd.common.bean.enums.HCardTypeEnum;
 | 
						|
import com.ynxbd.common.bean.enums.HealthCardEnum;
 | 
						|
import com.ynxbd.common.bean.Patient;
 | 
						|
import com.ynxbd.common.bean.enums.HealthCardRespCodeEnum;
 | 
						|
import com.ynxbd.common.helper.ProperHelper;
 | 
						|
import com.ynxbd.wx.config.WeChatConfig;
 | 
						|
import com.ynxbd.common.dao.PatientDao;
 | 
						|
import com.ynxbd.common.config.EhCacheConfig;
 | 
						|
import com.ynxbd.common.helper.common.ErrorHelper;
 | 
						|
import lombok.extern.slf4j.Slf4j;
 | 
						|
import org.apache.commons.lang3.ObjectUtils;
 | 
						|
import org.apache.commons.lang3.StringUtils;
 | 
						|
import org.ehcache.Cache;
 | 
						|
 | 
						|
import java.text.SimpleDateFormat;
 | 
						|
import java.util.*;
 | 
						|
 | 
						|
@Slf4j
 | 
						|
public class HCodeService {
 | 
						|
 | 
						|
    final private static boolean IS_ENABLE; // 是否启用电子健康看(true:启用, false:禁用)
 | 
						|
 | 
						|
    // 健康码
 | 
						|
    final private static String H_APP_ID;
 | 
						|
    final private static String H_APP_SECRET;
 | 
						|
    final private static String H_HOSPITAL_ID; // 医院ID(每家医院不一样)
 | 
						|
    // 万达
 | 
						|
    final private static String CARD_URL;
 | 
						|
    final private static String CARD_APP_ID;
 | 
						|
    final private static String CARD_PUBLIC_KEY;
 | 
						|
    final private static String CARD_PRIVATE_KEY;
 | 
						|
 | 
						|
    // 缓存
 | 
						|
    private static Cache<String, String> CACHE;
 | 
						|
 | 
						|
    // 离线WeChartCode 批量领取健康卡用
 | 
						|
    final private static String WE_CHART_CODE = "73EFA6796D3869FF82FAE7E81E9814B7";
 | 
						|
 | 
						|
    static {
 | 
						|
        ProperHelper config = new ProperHelper().read("hcode.properties");
 | 
						|
        IS_ENABLE = config.getBoolean("is_enable", false);
 | 
						|
        config.setIsEnable(IS_ENABLE);
 | 
						|
 | 
						|
        H_APP_ID = config.getString("h.app_id");
 | 
						|
        H_APP_SECRET = config.getString("h.app_secret");
 | 
						|
        CARD_APP_ID = config.getString("h.card_app_id");
 | 
						|
        CARD_PUBLIC_KEY = config.getString("h.card_public_key");
 | 
						|
        CARD_PRIVATE_KEY = config.getString("h.card_private_key");
 | 
						|
        CARD_URL = config.getString("h.card_url");
 | 
						|
 | 
						|
        H_HOSPITAL_ID = config.getString("h.hospital_id");
 | 
						|
        if ("0".equals(H_HOSPITAL_ID)) {
 | 
						|
            log.info("[电子健康卡]医院id异常");
 | 
						|
        }
 | 
						|
        initCache();
 | 
						|
    }
 | 
						|
 | 
						|
    public static synchronized void initCache() {
 | 
						|
        if (CACHE == null) {
 | 
						|
            // 创建一个缓存实例(7000s最大存活时间)
 | 
						|
            CACHE = EhCacheConfig.createCache(String.class, String.class, "health_card_cache", 1L, 3L, 10L, false, 7000L, null);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 判断是否使用电子健康卡
 | 
						|
     *
 | 
						|
     * @return 是否使用
 | 
						|
     */
 | 
						|
    public static boolean isEnableHealthCard() {
 | 
						|
        if (!IS_ENABLE) {
 | 
						|
            log.info("hcode.properties中禁用电子健康卡isEnable={}", IS_ENABLE);
 | 
						|
        }
 | 
						|
        return IS_ENABLE;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取token
 | 
						|
     *
 | 
						|
     * @return token
 | 
						|
     */
 | 
						|
    public static String getAppToken() {
 | 
						|
        if (!isEnableHealthCard()) {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        String appToken = null;
 | 
						|
        if (CACHE == null) {
 | 
						|
            initCache();
 | 
						|
        }
 | 
						|
        if (CACHE != null) {
 | 
						|
            appToken = CACHE.get("appToken");
 | 
						|
            if (appToken != null) {
 | 
						|
                return appToken;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        try {
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET, 5, 10);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            int channelNum = 0;
 | 
						|
            CommonIn commonIn = new CommonIn("", requestId, H_HOSPITAL_ID, channelNum);
 | 
						|
            //调用接口
 | 
						|
            JSONObject appTokenObj = healthCard.getAppToken(commonIn, H_APP_ID);
 | 
						|
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.getAppToken(commonIn, H_APP_ID);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]获取appToken失败-" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            JSONObject respJson = appTokenObj.getJSONObject("rsp");
 | 
						|
            appToken = respJson.getString("appToken");
 | 
						|
 | 
						|
            if (CACHE != null && appToken != null && !"".equals(appToken)) {
 | 
						|
                CACHE.put("appToken", appToken);
 | 
						|
            }
 | 
						|
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
 | 
						|
        if (StringUtils.isEmpty(appToken)) {
 | 
						|
            log.info("[电子健康卡]获取appToken为空");
 | 
						|
        }
 | 
						|
        return appToken;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static Patient appBindHealthCard(String healthCode){
 | 
						|
        if(healthCode==null){
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        String appToken = getAppToken();
 | 
						|
        if (appToken == null) {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
        String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
        CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
        JSONObject  resultObj = healthCard.getHealthCardByHealthCode(commonIn,healthCode);
 | 
						|
        JSONObject rspObj = resultObj.getJSONObject("rsp");
 | 
						|
        JSONObject cardJson = rspObj.getJSONObject("card");
 | 
						|
        Patient patient = new Patient();
 | 
						|
        patient.setSex(cardJson.getString("gender")); // 性别
 | 
						|
        patient.setName(cardJson.getString("name"));
 | 
						|
        patient.setNation(cardJson.getString("nation"));
 | 
						|
        patient.setCardType(cardJson.getString("idType"));
 | 
						|
        patient.setIdCardNo(cardJson.getString("idNumber"));
 | 
						|
        patient.setBirthday(cardJson.getString("birthday"));
 | 
						|
        patient.setAddress(cardJson.getString("address"));
 | 
						|
        String phone1 = cardJson.getString("phone1");
 | 
						|
        String phone2 = cardJson.getString("phone2");
 | 
						|
        patient.setTel(ObjectUtils.isEmpty(phone1) ? phone2 : phone1);
 | 
						|
        return  patient;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 注册健康卡
 | 
						|
     *
 | 
						|
     * @param weChatCode   微信身份码
 | 
						|
     * @param birthday     出生年月日
 | 
						|
     * @param cardTypeEnum 证件类型(01-居民身份证,其他参考证件类型表)
 | 
						|
     * @param address      地址
 | 
						|
     * @param sex          性别
 | 
						|
     * @param nation       民族
 | 
						|
     * @param name         姓名
 | 
						|
     * @param idCardNo     证件号码
 | 
						|
     * @param phone1       联系方式1
 | 
						|
     */
 | 
						|
    public static JSONObject registerHealthCard(String patientId, String weChatCode,
 | 
						|
                                                String birthday, HCardTypeEnum cardTypeEnum, String address,
 | 
						|
                                                String sex, String nation, String name, String idCardNo, String phone1) {
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            if (cardTypeEnum == null) {
 | 
						|
                cardTypeEnum = HCardTypeEnum._01; // 居民身份证
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
 | 
						|
            // 构造公共输入参数commonIn
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            HealthCardInfo healthCardInfoReq = new HealthCardInfo();
 | 
						|
            healthCardInfoReq.setAddress(address);
 | 
						|
            healthCardInfoReq.setBirthday(birthday);
 | 
						|
            healthCardInfoReq.setGender(sex);
 | 
						|
            healthCardInfoReq.setIdNumber(idCardNo);
 | 
						|
            healthCardInfoReq.setIdType(cardTypeEnum.WX_CODE);
 | 
						|
            healthCardInfoReq.setNation(nation);
 | 
						|
            healthCardInfoReq.setName(name);
 | 
						|
            healthCardInfoReq.setPhone1(phone1);
 | 
						|
            healthCardInfoReq.setWechatCode(weChatCode);
 | 
						|
            healthCardInfoReq.setPatid(patientId);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.registerHealthCard(commonIn, healthCardInfoReq);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]注册失败-" + resultObj);
 | 
						|
                return commonOut;
 | 
						|
            }
 | 
						|
            return resultObj.getJSONObject("rsp");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 授权码获取健康卡
 | 
						|
     *
 | 
						|
     * @param healthCode 健康卡授权码
 | 
						|
     */
 | 
						|
    public static Patient getInfoByHealthCode(String healthCode) {
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.getHealthCardByHealthCode(commonIn, healthCode);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            JSONObject rspObj = resultObj.getJSONObject("rsp");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode")) || rspObj == null) {
 | 
						|
                log.info("[电子健康卡]授权码获取健康卡失败:" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject card = rspObj.getJSONObject("card");
 | 
						|
            if (card == null) {
 | 
						|
                log.info("[电子健康卡]一键绑定 解析json失败");
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            Patient patient = new Patient();
 | 
						|
            String phone1 = card.getString("phone1");
 | 
						|
            if (phone1 == null || "".equals(phone1)) {
 | 
						|
                patient.setTel(card.getString("phone2"));
 | 
						|
            } else {
 | 
						|
                patient.setTel(phone1);
 | 
						|
            }
 | 
						|
 | 
						|
            patient.setName(card.getString("name"));
 | 
						|
            patient.setIdCardNo(card.getString("idNumber"));
 | 
						|
            patient.setSex(card.getString("gender"));
 | 
						|
            patient.setNation(card.getString("nation"));
 | 
						|
            patient.setAddress(card.getString("address"));
 | 
						|
            patient.setBirthday(card.getString("birthday"));
 | 
						|
            patient.setHealthCardId(card.getString("healthCardId"));
 | 
						|
            patient.setCardType(card.getString("idType"));
 | 
						|
            log.info("[一键绑定]获取用户信息 patient={}", patient);
 | 
						|
            return patient;
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * [电子健康卡]通过二维码获取健康卡数据
 | 
						|
     *
 | 
						|
     * @param qrCode 二维码
 | 
						|
     */
 | 
						|
    public static Patient getHealthCardByQrCode(String qrCode) {
 | 
						|
        try {
 | 
						|
            log.info("[电子健康卡]查询健康卡信息 qrCode={}", qrCode);
 | 
						|
            if (ObjectUtils.isEmpty(qrCode)) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.getHealthCardByQRCode(commonIn, qrCode);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]二维码获取健康卡失败:" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject respJson = resultObj.getJSONObject("rsp");
 | 
						|
            if (respJson == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject cardJson = respJson.getJSONObject("card");
 | 
						|
            if (cardJson == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            Patient patient = new Patient();
 | 
						|
            patient.setSex(cardJson.getString("gender")); // 性别
 | 
						|
            patient.setName(cardJson.getString("name"));
 | 
						|
            patient.setNation(cardJson.getString("nation"));
 | 
						|
            patient.setCardType(cardJson.getString("idType"));
 | 
						|
            patient.setIdCardNo(cardJson.getString("idNumber"));
 | 
						|
            patient.setBirthday(cardJson.getString("birthday"));
 | 
						|
            patient.setAddress(cardJson.getString("address"));
 | 
						|
            String phone1 = cardJson.getString("phone1");
 | 
						|
            String phone2 = cardJson.getString("phone2");
 | 
						|
            patient.setTel(ObjectUtils.isEmpty(phone1) ? phone2 : phone1);
 | 
						|
            return patient;
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 实人认证生成orderId接口
 | 
						|
     */
 | 
						|
    public static String registerUniformVerifyOrder(String idCardNo, String name, String wechatCode) {
 | 
						|
        try {
 | 
						|
            log.info("[电子健康卡]实人认证生成orderId idCardNo={}, name={}", idCardNo, name);
 | 
						|
            if (ObjectUtils.isEmpty(idCardNo) || ObjectUtils.isEmpty(name)) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultJson = healthCard.registerUniformVerifyOrder(commonIn, idCardNo, "01", name, wechatCode);
 | 
						|
            JSONObject commonOut = resultJson.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]实人认证生成orderId接口失败:" + resultJson);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject respJson = resultJson.getJSONObject("rsp");
 | 
						|
            if (respJson == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            return respJson.getString("verifyOrderId");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 实人认证结果查询接口
 | 
						|
     */
 | 
						|
    public static boolean checkUniformVerifyResult(String verifyOrderId, String registerOrderId) {
 | 
						|
        try {
 | 
						|
            log.info("[电子健康卡]实人认证结果查询 verifyOrderId={}, registerOrderId={}", verifyOrderId, registerOrderId);
 | 
						|
            if (ObjectUtils.isEmpty(verifyOrderId) || ObjectUtils.isEmpty(registerOrderId)) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultJson = healthCard.checkUniformVerifyResult(commonIn, verifyOrderId, registerOrderId);
 | 
						|
            JSONObject commonOut = resultJson.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]实人认证结果查询失败:" + resultJson);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
            JSONObject respJson = resultJson.getJSONObject("rsp");
 | 
						|
            if (respJson == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
            return respJson.getBoolean("suc");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取卡包订单ID
 | 
						|
     *
 | 
						|
     * @param qrCodeText 二维码编码
 | 
						|
     */
 | 
						|
    public static JSONObject getCardOrderId(String qrCodeText) {
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                log.info("获取卡包订单ID-appToken为空");
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.getOrderIdByOutAppId(commonIn, WeChatConfig.APP_ID, qrCodeText);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]获取卡包订单ID 失败:" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            return resultObj.getJSONObject("rsp");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 电子健康卡用卡数据监测接口
 | 
						|
     *
 | 
						|
     * @param qrCodeText 动态二维码
 | 
						|
     * @param deptName   科室名
 | 
						|
     * @param scene      上报类型 scene
 | 
						|
     * @param cardType   卡类型
 | 
						|
     * @return JSONObject
 | 
						|
     */
 | 
						|
    public static JSONObject reportHISData(String qrCodeText, String deptName, String scene, String cardType, String cardCostType) {
 | 
						|
        if ("".equals(qrCodeText) || scene == null || cardType == null) {
 | 
						|
            log.info("[电子健康卡]用卡数据监测接口,参数为空");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //构造请求参数req
 | 
						|
            ReportHISData reportHISData = new ReportHISData();
 | 
						|
            reportHISData.setQrCodeText(qrCodeText);
 | 
						|
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
						|
            reportHISData.setTime(format.format(new Date()));
 | 
						|
 | 
						|
            reportHISData.setHospitalCode(H_HOSPITAL_ID);
 | 
						|
            // 010101 挂号
 | 
						|
            reportHISData.setScene(scene);
 | 
						|
            reportHISData.setDepartment(deptName); // 科室代码
 | 
						|
            reportHISData.setCardChannel(HealthCardEnum.CARD_CHANNEL_WX_CHANNEL.STATUS); // 微信渠道
 | 
						|
 | 
						|
            reportHISData.setCardType(cardType);
 | 
						|
 | 
						|
            // 自费:0100,医保:0200,公费:0300,其他:0000
 | 
						|
            reportHISData.setCardCostTypes(cardCostType);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.reportHISData(commonIn, reportHISData);
 | 
						|
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("用卡数据监测接口-" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            return resultObj.getJSONObject("rsp");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取健康卡二维码
 | 
						|
     *
 | 
						|
     * @param healthCardId 健康卡ID
 | 
						|
     * @param idCardNo     证件号码
 | 
						|
     * @param codeType     传0或者1,0返回动态码,1返回静态码
 | 
						|
     */
 | 
						|
    public static JSONObject getDynamicQRCode(String healthCardId, String idCardNo, String codeType) {
 | 
						|
        if (healthCardId == null || idCardNo == null || codeType == null) {
 | 
						|
            log.info("[电子健康卡]获取健康卡二维码参数缺失 healthCardId={}, idCardNo={}, codeType={}", healthCardId, idCardNo, codeType);
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.getDynamicQRCode(commonIn, healthCardId, "01", idCardNo, codeType);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("获取健康卡二维码失败, resp-{}",resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            return resultObj.getJSONObject("rsp");
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 一键绑定,绑定健康卡和医院关系
 | 
						|
     *
 | 
						|
     * @param patientId  患者ID
 | 
						|
     * @param qrCodeText 标识ID(healthCardId = qrCodeText)
 | 
						|
     */
 | 
						|
    public static JSONObject bindCardRelation(String patientId, String qrCodeText) {
 | 
						|
        if (patientId == null || "".equals(qrCodeText)) {
 | 
						|
            log.info("[电子健康卡]一键绑定绑定健康卡和医院关系参数缺失 patientId={}, qrCodeText={}", patientId, qrCodeText);
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        try {
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            //构造公共输入参数commonIn
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, 0);
 | 
						|
            JSONObject resultObj = healthCard.bindCardRelation(commonIn, patientId, qrCodeText);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]绑定健康卡和医院关系-" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            return resultObj.getJSONObject("rsp");
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 根据健康卡ID获取动态二维码
 | 
						|
     *
 | 
						|
     * @param healthCardId 健康卡ID
 | 
						|
     * @param idCardNo     身份证号
 | 
						|
     * @return 动态二维码
 | 
						|
     */
 | 
						|
    public static String getQRCodeText(String healthCardId, String idCardNo) {
 | 
						|
        JSONObject QRResult = getDynamicQRCode(healthCardId, idCardNo, "0");
 | 
						|
        if (QRResult == null) {
 | 
						|
            log.info("用卡数据监测接口,获取二维码失败");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        String qrCodeText = QRResult.getString("qrCodeText");
 | 
						|
        if ("".equals(qrCodeText)) {
 | 
						|
            log.info("用卡数据监测接口,获取qrCodeText失败");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        return qrCodeText;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 健康卡数据上报-->挂号支付
 | 
						|
     *
 | 
						|
     * @param openid    openid
 | 
						|
     * @param patientId 患者ID
 | 
						|
     * @param deptName  科室名
 | 
						|
     * @param regDate   挂号日期
 | 
						|
     * @return 是否上报成功
 | 
						|
     */
 | 
						|
    public static boolean regPayReportHISData(String openid, String patientId, String deptName, String regDate) {
 | 
						|
        try {
 | 
						|
            if (!HCodeService.isEnableHealthCard()) { // 判断是否禁用电子健康卡
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            if (deptName == null || openid == null || patientId == null || regDate == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 | 
						|
            String today = format.format(new Date());
 | 
						|
            String scene = regDate.equals(today) ? HealthCardEnum.SCENE_REGISTER_SITE.STATUS : HealthCardEnum.SCENE_REGISTER_RESERVE.STATUS;
 | 
						|
 | 
						|
            Patient patient = new PatientDao().selectByOpenidAndPatientId(openid, patientId);
 | 
						|
            if (patient == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            String healthCardId = patient.getHealthCardId();
 | 
						|
            String idCardNo = patient.getIdCardNo();
 | 
						|
            if (healthCardId == null || idCardNo == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            String qrCodeText = getQRCodeText(healthCardId, idCardNo);
 | 
						|
            if (qrCodeText == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            // 挂号,自费
 | 
						|
            JSONObject jsonObject = reportHISData(qrCodeText, deptName, scene, HealthCardEnum.CARD_TYPE_HEALTH_CARD.STATUS, HealthCardEnum.CARD_COST_TYPE_OWN_EXPENSE.STATUS);
 | 
						|
            if (jsonObject == null) {
 | 
						|
                log.info("挂号数据上报失败");
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 健康卡数据上报-->挂号支付
 | 
						|
     *
 | 
						|
     * @param imageContent 身份证正面照片的base64编码数据,头部信息需要删除,如image/png;base64、image/jpeg/png;base64等,并且数据量建议压缩到百K级别上传。
 | 
						|
     * @return 身份证信息
 | 
						|
     */
 | 
						|
    public static Patient orcInfo(String imageContent) {
 | 
						|
        if (imageContent == null) {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        int index = imageContent.indexOf(",");
 | 
						|
        if (index != -1) {
 | 
						|
            imageContent = imageContent.substring(index + 1);
 | 
						|
        }
 | 
						|
 | 
						|
        Patient patient = new Patient();
 | 
						|
        try {
 | 
						|
            if (!HCodeService.isEnableHealthCard()) { // 判断是否禁用电子健康卡
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            String appToken = getAppToken();
 | 
						|
            if (appToken == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
 | 
						|
            HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
            //构造公共输入参数commonIn
 | 
						|
            String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
            int channelNum = 0;
 | 
						|
            CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, channelNum);
 | 
						|
            //调用接口
 | 
						|
            JSONObject resultObj = healthCard.ocrInfo(commonIn, imageContent);
 | 
						|
            JSONObject commonOut = resultObj.getJSONObject("commonOut");
 | 
						|
            if (!"0".equals(commonOut.getString("resultCode"))) {
 | 
						|
                log.info("[电子健康卡]身份证识别失败-" + resultObj);
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject rsp = resultObj.getJSONObject("rsp");
 | 
						|
            if (rsp == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            JSONObject cardInfo = rsp.getJSONObject("cardInfo");
 | 
						|
            if (cardInfo == null) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
            // 读取信息
 | 
						|
            String idNumber = cardInfo.getString("id");
 | 
						|
 | 
						|
            patient.setIdCardNo(idNumber);
 | 
						|
            patient.setName(cardInfo.getString("name"));
 | 
						|
            patient.setSex(cardInfo.getString("gender"));
 | 
						|
            patient.setAddress(cardInfo.getString("address"));
 | 
						|
            patient.setNation(cardInfo.getString("nation"));
 | 
						|
            patient.setBirthday(cardInfo.getString("birth"));
 | 
						|
 | 
						|
//          if (!"".equals(idNumber)) {
 | 
						|
//                FileHelper.saveBase64Image("data:image/png;base64," + imageContent, "idCard", (idNumber + ".png"), true, false);
 | 
						|
//          }
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        return patient;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 健康卡数据上报-->处方缴费
 | 
						|
     *
 | 
						|
     * @param openid    openid
 | 
						|
     * @param patientId patientId
 | 
						|
     * @return 是否成功
 | 
						|
     */
 | 
						|
    public static boolean payNotifyReportHISData(String openid, String patientId) {
 | 
						|
        try {
 | 
						|
            if (!HCodeService.isEnableHealthCard()) { // 判断是否禁用电子健康卡
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            if (patientId == null || openid == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            Patient patient = new PatientDao().selectByOpenidAndPatientId(openid, patientId);
 | 
						|
            if (patient == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            String healthCardId = patient.getHealthCardId();
 | 
						|
            String idCardNo = patient.getIdCardNo();
 | 
						|
            if (healthCardId == null || idCardNo == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            String qrCodeText = getQRCodeText(healthCardId, idCardNo);
 | 
						|
            if (qrCodeText == null) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            // 门诊缴费
 | 
						|
            JSONObject jsonObject = HCodeService.reportHISData(qrCodeText, null, HealthCardEnum.SCENE_TOLL_OUTPATIENT_PAYMENT.STATUS, HealthCardEnum.CARD_TYPE_HEALTH_CARD.STATUS, HealthCardEnum.CARD_COST_TYPE_OWN_EXPENSE.STATUS);
 | 
						|
            if (jsonObject == null) {
 | 
						|
                log.info("挂号数据上报失败");
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 批量领卡
 | 
						|
     *
 | 
						|
     * @return json
 | 
						|
     */
 | 
						|
    public static JSONArray batchUpdate(List<Patient> lstPatient) {
 | 
						|
        HealthCardServerImpl healthCard = new HealthCardServerImpl(H_APP_SECRET);
 | 
						|
        //构造公共输入参数commonIn
 | 
						|
        String appToken = getAppToken();
 | 
						|
        String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
 | 
						|
        int channelNum = 0;
 | 
						|
        CommonIn commonIn = new CommonIn(appToken, requestId, H_HOSPITAL_ID, channelNum);
 | 
						|
        List<HealthCardInfo> lstHealthCard = new ArrayList<>();
 | 
						|
        for (Patient patient : lstPatient) {
 | 
						|
            HealthCardInfo healthCardInfo = new HealthCardInfo();
 | 
						|
            healthCardInfo.setAddress(patient.getAddress());
 | 
						|
            healthCardInfo.setBirthday(patient.getBirthday());
 | 
						|
            healthCardInfo.setGender(patient.getSex());
 | 
						|
            healthCardInfo.setIdNumber(patient.getIdCardNo());
 | 
						|
            healthCardInfo.setIdType("01");
 | 
						|
            healthCardInfo.setNation(patient.getNation());
 | 
						|
            healthCardInfo.setName(patient.getName());
 | 
						|
            healthCardInfo.setPhone1(patient.getTel());
 | 
						|
            healthCardInfo.setWechatCode(WE_CHART_CODE);
 | 
						|
            healthCardInfo.setPatid(patient.getPatientId());
 | 
						|
            healthCardInfo.setOpenId(patient.getOpenid());
 | 
						|
            healthCardInfo.setWechatUrl("http://www.ynxbdkj.cn");
 | 
						|
            lstHealthCard.add(healthCardInfo);
 | 
						|
        }
 | 
						|
 | 
						|
        JSONObject healthCardInfosRsp = healthCard.registerBatchHealthCard(commonIn, lstHealthCard);
 | 
						|
        JSONObject commonOut = healthCardInfosRsp.getJSONObject("commonOut");
 | 
						|
        JSONObject rspObj = healthCardInfosRsp.getJSONObject("rsp");
 | 
						|
        if (!"0".equals(commonOut.getString("resultCode")) || rspObj == null) {
 | 
						|
            log.info("批量领取健康卡-" + healthCardInfosRsp);
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        JSONArray rspItems = rspObj.getJSONArray("rspItems");
 | 
						|
        if (rspItems == null) {
 | 
						|
            log.info("[批量领取健康卡]解析json失败");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        return rspItems;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 响应码处理
 | 
						|
     *
 | 
						|
     * @param resultCode 响应码
 | 
						|
     * @return 返回响应码对应的消息
 | 
						|
     */
 | 
						|
    public static HealthCardRespCodeEnum resultCodeHandle(String resultCode) {
 | 
						|
        HealthCardRespCodeEnum healthCardRespCodeEnum = HealthCardRespCodeEnum.CONTINUE;
 | 
						|
 | 
						|
        for (HealthCardRespCodeEnum e : HealthCardRespCodeEnum.values()) {
 | 
						|
            if (e.STATUS.equals(resultCode)) {
 | 
						|
                if (e.IS_RESULT) { // 是否返回枚举中的信息
 | 
						|
                    healthCardRespCodeEnum = e;
 | 
						|
                } else {
 | 
						|
                    log.info("[电子健康卡]异常信息:{}", e.MESSAGE);
 | 
						|
                    healthCardRespCodeEnum = e.IS_CONTINUE ? HealthCardRespCodeEnum.CONTINUE : e;
 | 
						|
                }
 | 
						|
                break;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return healthCardRespCodeEnum;
 | 
						|
    }
 | 
						|
 | 
						|
}
 | 
						|
 |