|  | @ -0,0 +1,249 @@
 | 
	
		
			
				|  |  | package com.yihu.wlyy.service.app.prescription;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.message.Message;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.organization.Hospital;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.organization.HospitalMapping;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.patient.Patient;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.patient.prescription.*;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.ylz.PayConfigura;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.zydict.ZyPushLog;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.charge.ChargeDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.doctor.DoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.message.MessageDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.organization.HospitalDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.organization.HospitalMappingDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.patient.PatientDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.patient.SignFamilyDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.prescription.PrescriptionDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.prescription.PrescriptionExpressageDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.prescription.PrescriptionInfoDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.prescription.PrescriptionPayDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.ylz.PayConfiguraDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.service.BaseService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 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.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.util.StreamUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | import javax.transaction.Transactional;
 | 
	
		
			
				|  |  | import java.io.InputStream;
 | 
	
		
			
				|  |  | import java.io.PrintWriter;
 | 
	
		
			
				|  |  | import java.io.StringWriter;
 | 
	
		
			
				|  |  | import java.nio.charset.Charset;
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | 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;
 | 
	
		
			
				|  |  |     @Value("${wechat.accId}")
 | 
	
		
			
				|  |  |     private String accId;
 | 
	
		
			
				|  |  |     //    Java配置文件 后续删除
 | 
	
		
			
				|  |  |     private String signType = "MD5";
 | 
	
		
			
				|  |  |     private String encryptType = "AES";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //    引入实例
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PayConfiguraDao configuraDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PrescriptionPayDao payDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HospitalMappingDao mappingDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PrescriptionDao prescriptionDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 家庭医生签约支付查询
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String chargeQuery(String code, String accessToken) throws Exception {
 | 
	
		
			
				|  |  |         PayConfigura config = configuraDao.findWorkConfigura();
 | 
	
		
			
				|  |  |         int flag = 0;
 | 
	
		
			
				|  |  |         Boolean isSuccess = true;
 | 
	
		
			
				|  |  |         String msgBody = "";
 | 
	
		
			
				|  |  |         String response = "";
 | 
	
		
			
				|  |  |         String error = "";
 | 
	
		
			
				|  |  |         String result = null;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         PrescriptionPay charge = payDao.findByCode(code);
 | 
	
		
			
				|  |  |         String prescriptionCode = charge.getPrescriptionCode();
 | 
	
		
			
				|  |  |         Prescription prescription = prescriptionDao.findByCode(prescriptionCode);
 | 
	
		
			
				|  |  |         HospitalMapping hospitalMapping = mappingDao.findByCode(prescription.getHospital());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //          ***************************  测通流程 ***************************************
 | 
	
		
			
				|  |  |         OnepayDefaultClient onepayClient = new OnepayDefaultClient(config.getOnepayApi(), hospitalMapping.getAppId(), hospitalMapping.getAppSecret(), signType, encryptType);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             RequestParams requestParams = new RequestParams();
 | 
	
		
			
				|  |  | //            requestParams.setVersion();
 | 
	
		
			
				|  |  | //            requestParams.setSign();
 | 
	
		
			
				|  |  | //            requestParams.setEncryptData();
 | 
	
		
			
				|  |  | //            requestParams.setAppId(appId);
 | 
	
		
			
				|  |  |             requestParams.setAppId(hospitalMapping.getAppId());
 | 
	
		
			
				|  |  |             requestParams.setTimestamp(DateUtil.getCurrentDateTime());
 | 
	
		
			
				|  |  |             requestParams.setSignType(signType);
 | 
	
		
			
				|  |  |             requestParams.setEncryptType(encryptType);
 | 
	
		
			
				|  |  |             requestParams.setTransType(config.getChargeQueryType());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //业务参数
 | 
	
		
			
				|  |  |             JSONObject params = new JSONObject();
 | 
	
		
			
				|  |  |             params.put("outChargeNo", code);  //接入应用结算业务流水号
 | 
	
		
			
				|  |  |             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());
 | 
	
		
			
				|  |  | //                新增保存数据
 | 
	
		
			
				|  |  |                 JSONObject request = JSON.parseObject(result);
 | 
	
		
			
				|  |  | //                String tradeStatus = request.getString("tradeStatus");//交易状态
 | 
	
		
			
				|  |  |                 String chargeStatus = request.getString("chargeStatus");//交易状态
 | 
	
		
			
				|  |  |                 String chargeNo = request.getString("chargeNo");//支付平台流水号
 | 
	
		
			
				|  |  |                 String chargeTime = request.getString("chargeTime");//支付平台交易成功时间
 | 
	
		
			
				|  |  |                 String outChargeNo = request.getString("outChargeNo");//接入应用业务流水号
 | 
	
		
			
				|  |  |                 String outChargeTime = request.getString("outChargeTime");//接入应用时间
 | 
	
		
			
				|  |  |                 String medOrgNo = request.getString("medOrgNo");//医疗机构编号
 | 
	
		
			
				|  |  |                 String medOrgName = request.getString("medOrgName");//医疗机构名称
 | 
	
		
			
				|  |  |                 String billNo = request.getString("billNo");//待结算费用单据号
 | 
	
		
			
				|  |  |                 String cardNo = request.getString("cardNo");//医保卡号
 | 
	
		
			
				|  |  |                 String totalAmount = request.getString("totalAmount");//交易总金额
 | 
	
		
			
				|  |  |                 String insuranceAmount = request.getString("insuranceAmount");//医保支付金额
 | 
	
		
			
				|  |  |                 String selfPayAmount = request.getString("selfPayAmount");//自付金额
 | 
	
		
			
				|  |  |                 String userId = request.getString("userId");//用户openID
 | 
	
		
			
				|  |  |                 String userName = request.getString("userName");//用户名字
 | 
	
		
			
				|  |  |                 String idNo = request.getString("idNo");//证件号码
 | 
	
		
			
				|  |  |                 String idType = request.getString("idType");//证件类型
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //                String appId = request.getString("appId");
 | 
	
		
			
				|  |  | //                String channel = request.getString("channel");//支付方式
 | 
	
		
			
				|  |  | //                String sign = request.getString("sign");
 | 
	
		
			
				|  |  | //                String signType = request.getString("signType");
 | 
	
		
			
				|  |  | //                String timestamp = request.getString("timestamp");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 JSONObject responseContents = request.getJSONObject("responseContent");//医保结构体
 | 
	
		
			
				|  |  |                 PrescriptionPay prescriptionPay = payDao.findByCode(outChargeNo);
 | 
	
		
			
				|  |  |                 if (responseContents != null) {
 | 
	
		
			
				|  |  |                     String miSettleNo = responseContents.getString("miSettleNo");//医保收费单据号 (医保签约号)
 | 
	
		
			
				|  |  |                     String miRegisterNo = responseContents.getString("miRegisterNo");//医保挂号
 | 
	
		
			
				|  |  |                     String miCollectDate = responseContents.getString("miCollectDate");//收费日期
 | 
	
		
			
				|  |  |                     String miCollectTime = responseContents.getString("miCollectTime");//收费时间
 | 
	
		
			
				|  |  |                     String miCollectorName = responseContents.getString("miCollectorName");//收费人姓名
 | 
	
		
			
				|  |  |                     String miTotalFee = responseContents.getString("miTotalFee");//本次报销总额
 | 
	
		
			
				|  |  |                     String selfPay = responseContents.getString("selfPay");//个人现金支付
 | 
	
		
			
				|  |  |                     String accountPay = responseContents.getString("accountPay");//个人账户支付
 | 
	
		
			
				|  |  |                     String heathPay = responseContents.getString("heathPay");//健康账户支付
 | 
	
		
			
				|  |  |                     String cadresPay = responseContents.getString("cadresPay");//公务员补助
 | 
	
		
			
				|  |  |                     String fundPay = responseContents.getString("fundPay");//基金支付
 | 
	
		
			
				|  |  |                     String healthCarePay = responseContents.getString("healthCarePay");//保健基金支付
 | 
	
		
			
				|  |  |                     String commercePay = responseContents.getString("commercePay");//商业保险支付
 | 
	
		
			
				|  |  |                     String familyPay = responseContents.getString("familyPay");//共济账户支付
 | 
	
		
			
				|  |  |                     String civilPay = responseContents.getString("civilPay");//民政补助金额
 | 
	
		
			
				|  |  |                     String civilZone = responseContents.getString("civilZone");//民政行政区号
 | 
	
		
			
				|  |  |                     String bdrugPay = responseContents.getString("bdrugPay");//乙类支付金额
 | 
	
		
			
				|  |  |                     String unlessAmount = responseContents.getString("unlessAmount");//非医保费用
 | 
	
		
			
				|  |  |                     String exceedAmount = responseContents.getString("exceedAmount");//超封顶线金额
 | 
	
		
			
				|  |  |                     String accountBalance = responseContents.getString("accountBalance");//个人账户余额
 | 
	
		
			
				|  |  |                     String healthBalance = responseContents.getString("healthBalance");//健康账户余额
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiSettleNo(miSettleNo);//医保收费单据号
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiRegisterNo(miRegisterNo);//医保挂号
 | 
	
		
			
				|  |  |                     SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd");
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiCollectDate(formatDate.parse(miCollectDate));//收费日期
 | 
	
		
			
				|  |  |                     SimpleDateFormat formatTime = new SimpleDateFormat("HHmm");
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiCollectTime(formatTime.parse(miCollectTime));//收费时间
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiCollectorName(miCollectorName);//收费人姓名
 | 
	
		
			
				|  |  |                     prescriptionPay.setMiTotalFee(Integer.parseInt(miTotalFee));//本次报销总额
 | 
	
		
			
				|  |  |                     prescriptionPay.setSelfPay(Integer.parseInt(selfPay));//个人现金支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setAccountPay(Integer.parseInt(accountPay));//个人账户支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setHeathPay(Integer.parseInt(heathPay));//健康账户支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setCadresPay(Integer.parseInt(cadresPay));//公务员补助
 | 
	
		
			
				|  |  |                     prescriptionPay.setFundPay(Integer.parseInt(fundPay));//基金支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setHealthCarePay(Integer.parseInt(healthCarePay));//保健基金支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setCommercePay(Integer.parseInt(commercePay));//商业保险支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setFamilyPay(Integer.parseInt(familyPay));//共济账户支付
 | 
	
		
			
				|  |  |                     prescriptionPay.setCivilPay(Integer.parseInt(civilPay));//民政补助金额
 | 
	
		
			
				|  |  |                     prescriptionPay.setCivilZone(civilZone);//民政行政区号
 | 
	
		
			
				|  |  |                     prescriptionPay.setBdrugPay(Integer.parseInt(bdrugPay));//乙类支付金额
 | 
	
		
			
				|  |  |                     prescriptionPay.setUnlessAmount(Integer.parseInt(unlessAmount));//非医保费用
 | 
	
		
			
				|  |  |                     prescriptionPay.setExceedAmount(Integer.parseInt(exceedAmount));//超封顶线金额
 | 
	
		
			
				|  |  |                     prescriptionPay.setAccountBalance(Integer.parseInt(accountBalance));//个人账户余额
 | 
	
		
			
				|  |  |                     prescriptionPay.setHealthBalance(Integer.parseInt(healthBalance));//健康账户余额
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if ("0".equals(chargeStatus) || "SUCCESS".equals(chargeStatus)) {
 | 
	
		
			
				|  |  |                     prescriptionPay.setTradeStatus("1");//交易状态 成功1 失败0
 | 
	
		
			
				|  |  |                 }else {
 | 
	
		
			
				|  |  |                     prescriptionPay.setTradeStatus("0");//交易状态 成功1 失败0
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 prescriptionPay.setChargeNo(chargeNo);//支付流水号
 | 
	
		
			
				|  |  |                 SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMddHHmmss");
 | 
	
		
			
				|  |  |                 prescriptionPay.setChargeTime(formatDate.parse(chargeTime));//支付平台交易成功时间
 | 
	
		
			
				|  |  |                 prescriptionPay.setOutChargeTime(formatDate.parse(outChargeTime));//接入应用内业务时间
 | 
	
		
			
				|  |  |                 prescriptionPay.setMedOrgNo(medOrgNo);//医疗机构编号
 | 
	
		
			
				|  |  |                 prescriptionPay.setMedOrgName(medOrgName);//医疗机构名称
 | 
	
		
			
				|  |  |                 prescriptionPay.setBillNo(billNo);//待结算费用单据号
 | 
	
		
			
				|  |  |                 prescriptionPay.setCardNo(cardNo);//医保卡号
 | 
	
		
			
				|  |  |                 prescriptionPay.setTotalAmount(Integer.parseInt(totalAmount));//交易总金额
 | 
	
		
			
				|  |  |                 prescriptionPay.setInsuranceAmount(Integer.parseInt(insuranceAmount));//医保支付金额
 | 
	
		
			
				|  |  |                 prescriptionPay.setSelfPayAmount(Integer.parseInt(selfPayAmount));//自付金额
 | 
	
		
			
				|  |  |                 prescriptionPay.setOpenid(userId);
 | 
	
		
			
				|  |  |                 prescriptionPay.setUserName(userName);
 | 
	
		
			
				|  |  |                 prescriptionPay.setIdType(idType);//证件类型
 | 
	
		
			
				|  |  |                 prescriptionPay.setIdNo(idNo);//证件号码
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //        保存数据库更改记录
 | 
	
		
			
				|  |  |                 payDao.save(prescriptionPay);
 | 
	
		
			
				|  |  |                 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();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (!isSuccess) {
 | 
	
		
			
				|  |  |             throw new Exception(error);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 | 
	
		
			
				|  |  | 
 |