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.
221 lines
12 KiB
221 lines
12 KiB
package com.ynxbd.common.service;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ynxbd.common.action.pay.PEnum;
|
|
import com.ynxbd.common.bean.enums.MerchantEnum;
|
|
import com.ynxbd.common.bean.pay.Order;
|
|
import com.ynxbd.common.bean.pay.Recipe;
|
|
import com.ynxbd.common.dao.peis.PeisDao;
|
|
import com.ynxbd.common.helper.common.JsonHelper;
|
|
import com.ynxbd.common.helper.his.HisHelper;
|
|
import com.ynxbd.common.helper.http.OkHttpHelper;
|
|
import com.ynxbd.common.helper.xbd.HttpHelper;
|
|
import com.ynxbd.common.helper.xbd.XBDHelper;
|
|
import com.ynxbd.common.result.JsonResult;
|
|
import com.ynxbd.common.result.JsonResultEnum;
|
|
import com.ynxbd.common.result.Result;
|
|
import com.ynxbd.common.result.ServiceException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import okhttp3.FormBody;
|
|
import okhttp3.RequestBody;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author 李进才
|
|
* @ClassName PeisService
|
|
* @Description TODO
|
|
* @date 2023/11/16 15:53:00
|
|
*/
|
|
|
|
@Slf4j
|
|
public class PeisService {
|
|
|
|
public Boolean Reserve(String openid, String patientId, String treatNum, String outTradeNo, String totalFee, String recipeId) {
|
|
Recipe recipe = new Recipe();
|
|
recipe.setOpenid(openid);
|
|
recipe.setPatientId(patientId);
|
|
recipe.setTreatNum(treatNum);
|
|
recipe.setOutTradeNo(outTradeNo);
|
|
recipe.setRecipeId(recipeId);
|
|
recipe.setTotalFee(new BigDecimal(totalFee));
|
|
recipe.setPayMoney(new BigDecimal(totalFee));
|
|
recipe.setPeisStatus(-1);
|
|
recipe.setPayStatus(-1);
|
|
if (new PeisDao().isRepeat(recipe.getRecipeId())) {
|
|
log.info("[体检预约]该处方号已经缴费,recipeId-{}", recipe.getRecipeId());
|
|
return false;
|
|
}
|
|
return new PeisDao().insert(recipe);
|
|
}
|
|
|
|
public void payNotify(MerchantEnum merchantEnum, String openid, BigDecimal totalFee, String outTradeNo, String bankTransNo, String payInfo, Date tradeDate) {
|
|
PeisDao peisDao = new PeisDao();
|
|
Recipe recipe = peisDao.selectByOutTradeNo(outTradeNo);
|
|
String tradeNo = HisHelper.getHisTradeNo(bankTransNo, PEnum.PEIS_RESERVE);
|
|
String recipeId = recipe.getRecipeId();
|
|
Integer payStates = recipe.getPayStatus();
|
|
if (payStates == 0) {
|
|
log.error("[体检预约] 拒绝支付-订单已支付 outTradeNo={}, bankTransNo={}, recipeId={}", outTradeNo, bankTransNo, recipeId);
|
|
return;
|
|
}
|
|
if (!peisDao.updatePayStateOk(outTradeNo, bankTransNo, tradeNo)) {
|
|
log.info("[体检预约]修改支付状态失败 outTradeNo={}, bankTransNo={},tradeNo-{}", outTradeNo, bankTransNo, tradeNo);
|
|
RequestBody formBody = new FormBody.Builder().
|
|
add("orderNo", recipeId)
|
|
.add("payStatus", "FAIL")
|
|
.add("payFailReason", "修改支付状态失败")
|
|
.add("bankTransNo", bankTransNo)
|
|
.add("outTradeNo", outTradeNo)
|
|
.build();
|
|
String result = OkHttpHelper.post(XBDHelper.XBD_PEIS + "/wxUse/wxPay", formBody);
|
|
JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
log.info("[体检预约]缴费失败回调体检系统计费 recipeId={}, bankTransNo-{}, 计费状态-{}, 计费信息-{}", recipeId, bankTransNo, jsonResult.get("code"), jsonResult.get("message"));
|
|
String pushMessage = "体检预约失败已申请退款,原因:修改支付状态失败,订单号:" + outTradeNo + "orderNo:" + recipeId;
|
|
;
|
|
peisAutoRefund(recipeId, merchantEnum, outTradeNo, totalFee, pushMessage, tradeDate, openid);
|
|
} else {
|
|
RequestBody formBody = new FormBody.Builder().
|
|
add("orderNo", recipeId)
|
|
.add("payStatus", "OK")
|
|
.add("payFailReason", "")
|
|
.add("bankTransNo", bankTransNo)
|
|
.add("outTradeNo", outTradeNo)
|
|
.build();
|
|
String result = OkHttpHelper.post(XBDHelper.XBD_PEIS + "/wxUse/wxPay", formBody);
|
|
if (result == null) {
|
|
log.info("[体检预约]体检服务无响应,开始退费");
|
|
String pushMessage = "体检预约失败已申请退款,原因:体检服务无响应,订单号:" + outTradeNo + "orderNo:" + recipeId;
|
|
peisAutoRefund(recipeId, merchantEnum, outTradeNo, totalFee, pushMessage, tradeDate, openid);
|
|
}
|
|
JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
log.info("[体检预约]缴费成功回调体检系统计费 recipeId={}, bankTransNo-{}, 计费状态-{}, 计费信息-{}", recipeId, bankTransNo, jsonResult.get("code"), jsonResult.get("message"));
|
|
if ("200".equals(jsonResult.get("code").toString())) {
|
|
if (!peisDao.updatePeisStateOk(outTradeNo, bankTransNo)) {
|
|
log.info("[体检预约]修改体检系统支付状态失败 outTradeNo={}, bankTransNo={}", outTradeNo, bankTransNo);
|
|
}
|
|
} else {
|
|
log.info("[体检预约]回调体检系统计费状态码异常 recipeId={}, bankTransNo-{}, 计费状态-{}, 计费信息-{}", recipeId, bankTransNo, jsonResult.get("code"), jsonResult.get("message"));
|
|
String pushMessage = "体检预约失败已申请退款,原因:体检系统计费失败,订单号:" + outTradeNo + "orderNo:" + recipeId;
|
|
peisAutoRefund(recipeId, merchantEnum, outTradeNo, totalFee, pushMessage, tradeDate, openid);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 体检退费接口
|
|
*
|
|
* @param recipeId 订单号
|
|
* @param merchantEnum 支付方式
|
|
* @param outTradeNo outTradeNo
|
|
* @param totalFee 总金额
|
|
* @param pushInfo 提示信息
|
|
* @param tradeDate 退费时间
|
|
* @param openid openid
|
|
*/
|
|
public Result peisAutoRefund(String recipeId, MerchantEnum merchantEnum, String outTradeNo, BigDecimal totalFee, String pushInfo, Date tradeDate, String openid) {
|
|
PeisDao peisDao = new PeisDao();
|
|
if (peisDao.checkRefund(outTradeNo)) {
|
|
log.info("{} [体检预约][该订单已经退费,请不要重复退费] outTradeNo={}, recipeId={}, totalFee={}", merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
return Result.error("该订单已经退费,请不要重复退费");
|
|
}
|
|
String result = OkHttpHelper.get(XBDHelper.XBD_PEIS + "/wxUse/GetOrder", params -> {
|
|
params.put("orderNo", recipeId);
|
|
});
|
|
if (result == null) {
|
|
log.info("{} [体检预约][退费错误,计费信息查询失败] outTradeNo={}, recipeId={}, totalFee={}", merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
return Result.error("退费错误,计费信息查询失败");
|
|
}
|
|
JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
String fee = jsonResult.getJSONObject("data").get("fee").toString();
|
|
boolean isWxPay = Boolean.parseBoolean(jsonResult.getJSONObject("data").get("isWxPay").toString());
|
|
boolean isCost = Boolean.parseBoolean(jsonResult.getJSONObject("data").get("isCost").toString());
|
|
if (totalFee.compareTo(BigDecimal.ZERO) == 0 || "0".equals(fee)) {
|
|
log.info("{} [体检预约][退费错误,退费金额为0] outTradeNo={}, recipeId={}, totalFee={}", merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
return Result.error("退费错误,退费金额为0");
|
|
}
|
|
if (totalFee.compareTo(new BigDecimal(fee)) != 0) {
|
|
log.info("{} [体检预约][退费错误,退费金额跟订单金额不符] outTradeNo={}, recipeId={}, totalFee={}", merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
return Result.error("退费错误,退费金额跟订单金额不符");
|
|
}
|
|
// if(isWxPay){
|
|
// log.info("{} [体检预约][退费错误,体检显示已计费,不允许退费] recipeId-{}, outTradeNo={}, recipeId={}, totalFee={}", recipeId, merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
// return Result.error("退费错误,体检显示已计费,不允许退费");
|
|
// }
|
|
if (isCost) {
|
|
log.info("{} [体检预约][退费错误,his显示已计费,不允许退费] outTradeNo={}, recipeId={}, totalFee={}", merchantEnum.NAME, outTradeNo, recipeId, totalFee);
|
|
return Result.error("退费错误,体检系统his显示已计费,不允许退费");
|
|
}
|
|
String refundResult;
|
|
Order orderRefund = PayService.refund(merchantEnum, outTradeNo, recipeId, totalFee, totalFee, pushInfo, tradeDate, openid, null, pushInfo);
|
|
if (!orderRefund.isSuccess()) {
|
|
log.info("{} [体检预约][退费错误] outTradeNo={}, totalFee={},recipeId-{}", merchantEnum.NAME, outTradeNo, totalFee, recipeId);
|
|
refundResult = orderRefund.getRefundResult();
|
|
} else {
|
|
refundResult = "OK";
|
|
}
|
|
boolean isUpdate = peisDao.updateRefundResult(outTradeNo, recipeId, refundResult);
|
|
if (!isUpdate) {
|
|
log.info("{} [体检预约][退费错误]退费信息更新失败 recipeId={}", merchantEnum.NAME, recipeId);
|
|
}
|
|
return Result.success();
|
|
|
|
}
|
|
|
|
public boolean peisRefund(String recipeId, String refundStatus, String refundFailReason) {
|
|
RequestBody formBody = new FormBody.Builder().
|
|
add("orderNo", recipeId)
|
|
.add("refundStatus", refundStatus)
|
|
.add("refundFailReason", refundFailReason)
|
|
.build();
|
|
String result = OkHttpHelper.post(XBDHelper.XBD_PEIS + "/wxUse/wxRefund", formBody);
|
|
JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
return "200".equals(jsonResult.get("code"));
|
|
}
|
|
|
|
public JSONArray getPeisByCardNo(String cardNo, String begDate, String endDate) throws ServiceException {
|
|
RequestBody formBody = new FormBody.Builder().
|
|
add("enIdCard", cardNo)
|
|
.add("begDate", begDate)
|
|
.add("endDate", endDate)
|
|
.build();
|
|
String result = OkHttpHelper.post(XBDHelper.XBD_PEIS + "/WxUse/GetPeisOfPacsResult", formBody);
|
|
if (ObjectUtils.isEmpty(result)) {
|
|
throw new ServiceException("请求失败");
|
|
}
|
|
JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
if (jsonResult == null) {
|
|
throw new ServiceException("请求失败");
|
|
}
|
|
String code = jsonResult.getString("code");
|
|
if (!"200".equals(code)) {
|
|
String message = jsonResult.getString("message");
|
|
throw new ServiceException(ObjectUtils.isEmpty(message) ? "请求失败" : message);
|
|
}
|
|
return jsonResult.getJSONArray("data");
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
RequestBody formBody = new FormBody.Builder().
|
|
add("orderNo", "Fee2023112900000001")
|
|
.add("payStatus", "OK")
|
|
.add("payFailReason", "")
|
|
.add("bankTransNo", "4200001809202305012522296970")
|
|
.build();
|
|
String result = OkHttpHelper.post(XBDHelper.XBD_PEIS + "/wxUse/wxPay", formBody);
|
|
System.out.println(result);
|
|
// String result = OkHttpHelper.get(XBDHelper.XBD_PEIS+"/wxUse/GetOrder",params -> {
|
|
// params.put("orderNo","Fee2023112900000001");
|
|
// });
|
|
// JSONObject jsonResult = JsonHelper.parseObject(result);
|
|
// System.out.println(result);
|
|
}
|
|
|
|
}
|
|
|