|
@ -0,0 +1,234 @@
|
|
|
package com.yihu.wlyy.service.app.prescription;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.wlyy.entity.charge.WlyyCharge;
|
|
|
import com.yihu.wlyy.entity.organization.HospitalMapping;
|
|
|
import com.yihu.wlyy.entity.patient.SignFamily;
|
|
|
import com.yihu.wlyy.entity.security.AccessToken;
|
|
|
import com.yihu.wlyy.repository.charge.ChargeDao;
|
|
|
import com.yihu.wlyy.repository.organization.HospitalMappingDao;
|
|
|
import com.yihu.wlyy.repository.patient.SignFamilyDao;
|
|
|
import com.yihu.wlyy.service.BaseService;
|
|
|
import com.yihu.wlyy.service.common.account.AccessTokenService;
|
|
|
import com.yihu.wlyy.service.weixin.wxpay.common.Configure;
|
|
|
import com.yihu.wlyy.util.SystemConf;
|
|
|
import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
|
|
|
import com.ylzinfo.onepay.sdk.domain.RequestParams;
|
|
|
import com.ylzinfo.onepay.sdk.domain.ResponseParams;
|
|
|
import com.ylzinfo.onepay.sdk.utils.DateUtil;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by Reece on 2017/7/28/028.
|
|
|
* 患者支付
|
|
|
*/
|
|
|
@Service
|
|
|
public class PatientPrescriptionPayService extends BaseService {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(PatientPrescriptionPayService.class);
|
|
|
|
|
|
//公共配置文件
|
|
|
Properties systemConf = SystemConf.getInstance().getSystemProperties();
|
|
|
// 各环境对应配置
|
|
|
@Value("${server.server_url}")
|
|
|
private String serverUrl;
|
|
|
|
|
|
// Java配置文件 后续删除
|
|
|
private String signType = "MD5";
|
|
|
private String encryptType = "AES";
|
|
|
|
|
|
@Value("${wechat.appId}")
|
|
|
private String appId;
|
|
|
@Value("${wechat.appSecret}")
|
|
|
private String appSecret;
|
|
|
@Value("${wechat.accId}")
|
|
|
private String accId;
|
|
|
|
|
|
|
|
|
// 引入实例
|
|
|
@Autowired
|
|
|
private Configure config;
|
|
|
|
|
|
@Autowired
|
|
|
private ChargeDao chargeDao;
|
|
|
|
|
|
@Autowired
|
|
|
private SignFamilyDao signFamilyDao;
|
|
|
|
|
|
@Autowired
|
|
|
private HospitalMappingDao hospitalMappingDao;
|
|
|
|
|
|
@Autowired
|
|
|
private AccessTokenService accessTokenService;
|
|
|
|
|
|
@Autowired
|
|
|
private com.yihu.wlyy.util.HttpUtil HttpUtil;
|
|
|
|
|
|
|
|
|
public String getSignFamilyPayResult(String outChargeNo, String accessToken, String appId, String appSecret) throws Exception {
|
|
|
Boolean isSuccess = true;
|
|
|
String msgBody = "";
|
|
|
String response = "";
|
|
|
String error = "";
|
|
|
String result = null;
|
|
|
|
|
|
// *************************** 测通流程 ***************************************
|
|
|
OnepayDefaultClient onepayClient = new OnepayDefaultClient(config.getOnepayApi(), appId, appSecret, signType, encryptType);
|
|
|
|
|
|
try {
|
|
|
RequestParams requestParams = new RequestParams();
|
|
|
requestParams.setAppId(appId);
|
|
|
requestParams.setTimestamp(DateUtil.getCurrentDateTime());
|
|
|
requestParams.setSignType(signType);
|
|
|
requestParams.setEncryptType(encryptType);
|
|
|
requestParams.setTransType(config.getChargeQueryType());
|
|
|
|
|
|
//业务参数
|
|
|
JSONObject params = new JSONObject();
|
|
|
params.put("outChargeNo", outChargeNo); //接入应用结算业务流水号
|
|
|
params.put("wxToken", accessToken);
|
|
|
|
|
|
requestParams.setParam(params);
|
|
|
msgBody = JSON.toJSONString(requestParams);
|
|
|
|
|
|
// *************************** 测通流程 ***************************************
|
|
|
//执行支付 返回原生返回值
|
|
|
ResponseParams<JSONObject> res = onepayClient.execute(requestParams);
|
|
|
response = JSON.toJSONString(res);
|
|
|
|
|
|
if (OnepayDefaultClient.isSuccessful(res)) {
|
|
|
//业务处理*******************
|
|
|
// 只返回业务出参
|
|
|
result = JSON.toJSONString(res.getParam());
|
|
|
|
|
|
// logger.info("请求成功,返回参数: " + result);
|
|
|
} else {
|
|
|
isSuccess = false;
|
|
|
error = "请求失败,返回结果:" + res.getRespCode() + "," + res.getRespMsg();
|
|
|
}
|
|
|
} catch (Exception ex) {
|
|
|
isSuccess = false;
|
|
|
StringWriter sw = new StringWriter();
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
ex.printStackTrace(pw);
|
|
|
error = sw.toString();
|
|
|
}
|
|
|
|
|
|
//type = 3易联众接口保存http日志
|
|
|
// logger.info("api - " + config.getChargeQueryType());
|
|
|
// logger.info("request - " + msgBody);
|
|
|
// logger.info("responses - " + response);
|
|
|
// logger.info("error - " + error);
|
|
|
|
|
|
if (!isSuccess) {
|
|
|
throw new Exception(error);
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 1.根据时间范围遍历没有医保签约号的家庭签约支付记录
|
|
|
* 2.询家庭签约支付结果,补更新医保签约号
|
|
|
* @param startdate
|
|
|
* @param enddate
|
|
|
*/
|
|
|
public void getSignFamilyPayResultJob(Date startdate, Date enddate) throws Exception {
|
|
|
|
|
|
//根据时间范围遍历没有医保签约号的家庭签约支付记录
|
|
|
List<WlyyCharge> charges = chargeDao.findByTradeStatusAndMiRegisterNoAndUpdateTime(0,enddate,startdate);
|
|
|
|
|
|
if(!charges.isEmpty()){
|
|
|
|
|
|
logger.info("根据时间范围遍历没有医保签约号的家庭签约支付记录:"+charges.size()+"条");
|
|
|
String accessToken = this.getAccessToken();
|
|
|
for (WlyyCharge charge:charges) {
|
|
|
try {
|
|
|
|
|
|
//1.根据签约ID获取家庭签约记录,判断是否有效
|
|
|
SignFamily signFamily = signFamilyDao.findByCode(charge.getChargeRelation());
|
|
|
|
|
|
if(1 == signFamily.getStatus()){
|
|
|
HospitalMapping hospitalMapping = hospitalMappingDao.findByCode(charge.getHospital());
|
|
|
String appid = hospitalMapping.getAppId();
|
|
|
String appsecert = hospitalMapping.getAppSecret();
|
|
|
String signFamilyPayResultJson = this.getSignFamilyPayResult(charge.getCode(),"",appid,appsecert);
|
|
|
|
|
|
if(StringUtils.isNotBlank(signFamilyPayResultJson)){
|
|
|
JSONObject reobj = JSON.parseObject(signFamilyPayResultJson);
|
|
|
if(reobj.containsKey("responseContent")){
|
|
|
JSONObject responseContent = reobj.getJSONObject("responseContent");
|
|
|
|
|
|
if(responseContent.containsKey("miRegisterNo") && StringUtils.isNotBlank(responseContent.getString("miRegisterNo"))){
|
|
|
String miRegisterNo = responseContent.getString("miRegisterNo");
|
|
|
charge.setMiRegisterNo(miRegisterNo);
|
|
|
charge.setNeedUpload("1");
|
|
|
charge.setUpdateTime(new Date());
|
|
|
chargeDao.save(charge);
|
|
|
}else{
|
|
|
logger.info("ERROR======获取家庭签约支付医保签约号JOB,CODE:"+charge.getCode()+",message:miRegisterNo不存在");
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
logger.info("ERROR======获取家庭签约支付医保签约号JOB,CODE:"+charge.getCode()+",message:responseContent不存在");
|
|
|
}
|
|
|
}else{
|
|
|
logger.info("ERROR======获取家庭签约支付医保签约号JOB,CODE:"+charge.getCode()+",message:接口无返回值");
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
logger.info("ERROR======获取家庭签约支付医保签约号JOB,CODE:"+charge.getCode()+",message:对应的签约记录无效");
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
|
|
logger.info("ERROR======获取家庭签约支付医保签约号JOB,CODE:"+charge.getCode()+",message:" +e.getMessage());
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
|
* 获取微信的access_token
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public String getAccessToken() throws Exception{
|
|
|
Iterable<AccessToken> accessTokens = accessTokenService.findAccessToken();
|
|
|
if (accessTokens != null) {
|
|
|
for (AccessToken accessToken : accessTokens) {
|
|
|
if ((System.currentTimeMillis() - accessToken.getAdd_timestamp()) < (accessToken.getExpires_in() * 1000)) {
|
|
|
return accessToken.getAccess_token();
|
|
|
} else {
|
|
|
accessTokenService.delAccessToken(accessToken);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
|
|
|
String result = HttpUtil.sendGet(token_url, params);
|
|
|
org.json.JSONObject json = new org.json.JSONObject(result);
|
|
|
if (json.has("access_token")) {
|
|
|
String token = json.get("access_token").toString();
|
|
|
String expires_in = json.get("expires_in").toString();
|
|
|
AccessToken newaccessToken = new AccessToken();
|
|
|
newaccessToken.setAccess_token(token);
|
|
|
newaccessToken.setExpires_in(Long.parseLong(expires_in));
|
|
|
newaccessToken.setAcc_id(accId);
|
|
|
accessTokenService.addAccessToken(newaccessToken);
|
|
|
return token;
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|