浏览代码

创元体检的扫码支付加入定时任务

lighter 1 年之前
父节点
当前提交
00daf275aa

+ 4 - 0
src/main/java/thyyxxk/wxservice_server/constant/OrderType.java

@@ -15,6 +15,10 @@ public enum OrderType {
 
     COUPON_PURCHASE(6, "购买优惠券"),
 
+    PACS_PRINT(7, "医学影像胶片打印"),
+
+    AESTHETIC_MEDICINE(8, "医学美容"),
+
     UNDEFINED(99, "未说明的费用类型");
 
     private final int code;

+ 0 - 6
src/main/java/thyyxxk/wxservice_server/controller/WxPayNotifyController.java

@@ -37,10 +37,4 @@ public class WxPayNotifyController {
                 .httpServletRequest(httpServletRequest).build();
         new WeChatPayService().decryptNotify(request);
     }
-
-    @PassToken
-    @GetMapping("/testNotify")
-    public void paymentNotify2(@RequestParam("tradeNo") String tradeNo) {
-        service.testNotify(tradeNo);
-    }
 }

+ 40 - 37
src/main/java/thyyxxk/wxservice_server/dao/WxApiDao.java

@@ -18,20 +18,18 @@ import java.util.List;
 @Mapper
 public interface WxApiDao {
     @Select("select rtrim(name) from mz_patient_mi with(nolock) where patient_id=#{patientId}")
-    String selectPatientName(@Param("patientId") String patientId);
+    String selectPatientName(String patientId);
 
     @Select("select * from t_wechat_pay_order with(nolock) where patient_id=#{patientId} " +
             "and mzy_request_id=#{mzyRequestId} and cashpay_amt=#{cost} ")
-    WxPayOrder selectSameGhOrder(@Param("patientId") String patientId,
-                                 @Param("mzyRequestId") Integer mzyRequestId,
-                                 @Param("cost") BigDecimal cost);
+    WxPayOrder selectSameGhOrder(String patientId, Integer mzyRequestId, BigDecimal cost);
 
     @Select("select top 1 * from t_wechat_pay_order with(nolock) where his_ord_num=#{hisOrdNum} " +
             "and cashpay_amt=#{cost} order by create_datetime desc")
-    WxPayOrder selectSameMzPayOrder(@Param("hisOrdNum") String hisOrdNum, @Param("cost") BigDecimal cost);
+    WxPayOrder selectSameMzPayOrder(String hisOrdNum, BigDecimal cost);
 
     @Select("select count(1) from mzy_reqrec WITH(NOLOCK) where psordnum=#{tradeNo} and cancel_mark=0")
-    int alreadyPaidRegisterFee(@Param("tradeNo") String tradeNo);
+    int alreadyPaidRegisterFee(String tradeNo);
 
     @Insert("insert into t_wechat_pay_order (body,openid,total_fee,patient_id," +
             "patient_name,inpatient_no,admiss_times,app_id,mch_id,prepay_id," +
@@ -54,71 +52,73 @@ public interface WxApiDao {
 
     @Update("update t_wechat_pay_order set trade_state=#{tradeState}, pay_datetime=#{successTime}, " +
             "query_state_times=(query_state_times+1),last_query_state=getdate() where trade_no=#{tradeNo}")
-    void updatePayStatusAndPayTime(@Param("tradeNo") String tradeNo,
-                                   @Param("tradeState") Transaction.TradeStateEnum tradeState,
-                                   @Param("successTime") String successTime);
+    void updatePayStatusAndPayTime(String tradeNo,
+                                   Transaction.TradeStateEnum tradeState,
+                                   String successTime);
 
     @Update("update t_wechat_pay_order set trade_state=#{tradeState},pay_datetime=#{successTime},openid=#{openid}, " +
             "query_state_times=(query_state_times+1),last_query_state=getdate() where trade_no=#{tradeNo}")
-    void updatePayStatusAndPayTimeAndOpenid(@Param("tradeNo") String tradeNo,
-                                            @Param("tradeState") Transaction.TradeStateEnum tradeState,
-                                            @Param("successTime") String successTime,
-                                            @Param("openid") String openid);
+    void updatePayStatusAndPayTimeAndOpenid(String tradeNo,
+                                            Transaction.TradeStateEnum tradeState,
+                                            String successTime,
+                                            String openid);
 
     @Update("update t_wechat_pay_order set trade_state=#{tradeState},query_state_times=(query_state_times+1), " +
             "last_query_state=getdate() where trade_no=#{tradeNo}")
-    void updatePayStatusAndQueryTimes(@Param("tradeNo") String tradeNo,
-                                      @Param("tradeState") Transaction.TradeStateEnum tradeState);
+    void updatePayStatusAndQueryTimes(String tradeNo,
+                                      Transaction.TradeStateEnum tradeState);
 
     @Update("update t_wechat_pay_order set trade_state=#{tradeState},query_state_times=(query_state_times+1), " +
             "openid=#{openid},last_query_state=getdate() where trade_no=#{tradeNo}")
-    void updatePayStatusAndQueryTimesAndOpenid(@Param("tradeNo") String tradeNo,
-                                               @Param("tradeState") Transaction.TradeStateEnum tradeState,
-                                               @Param("openid") String openid);
+    void updatePayStatusAndQueryTimesAndOpenid(String tradeNo,
+                                               Transaction.TradeStateEnum tradeState,
+                                               String openid);
 
     @Update("update t_wechat_pay_order set trade_state=#{tradeState} where trade_no=#{tradeNo}")
-    void updatePayStatusOnly(@Param("tradeNo") String tradeNo, @Param("tradeState") Transaction.TradeStateEnum tradeState);
+    void updatePayStatusOnly(String tradeNo, Transaction.TradeStateEnum tradeState);
 
     @Select("select count(1) from mz_deposit_file WITH(NOLOCK) where psordnum=#{tradeNo} and pay_mark=0")
-    int alreadyPayed(@Param("tradeNo") String tradeNo);
+    int alreadyPayed(String tradeNo);
 
     @Select("select trade_state as tradeState,refund_id as name from t_wechat_pay_order where trade_no=#{tradeNo} ")
-    PureCodeName selectPayStatus(@Param("tradeNo") String tradeNo);
+    PureCodeName selectPayStatus(String tradeNo);
 
     @Update("update t_wechat_pay_order set trade_state='REFUND',refund_op_code='99999'," +
             "refund_reason=#{reason},his_status=0,refund_op_datetime=getdate() " +
             "where trade_no=#{tradeNo}")
-    void refundOrder(@Param("tradeNo") String tradeNo, @Param("reason") String reason);
+    void refundOrder(String tradeNo, String reason);
 
     @Update("update t_wechat_pay_order set trade_state='REFUND' where trade_no=#{tradeNo}")
-    void updateRefundStatus(@Param("tradeNo") String tradeNo);
+    void updateRefundStatus(String tradeNo);
 
     @Update("update t_wechat_pay_order set his_status=1 where trade_no=#{tradeNo}")
-    void updateSuccessHisStatus(@Param("tradeNo") String tradeNo);
+    void updateSuccessHisStatus(String tradeNo);
 
     @Update("update t_patient_coupons set coupon_state='USED' where id=#{id}")
     void couponUsed(String id);
 
     @Select("select datediff(day,getdate(),request_day) as dateDiff, " +
             "rtrim(unit_code) as unitCode from mzy_reqrec where psordnum=#{tradeNo}")
-    HisRegister selectRegisterDateDiff(@Param("tradeNo") String tradeNo);
+    HisRegister selectRegisterDateDiff(String tradeNo);
 
     @Select("select count(1) from mz_deposit_file where psordnum=#{tradeNo} and cheque_type='2'")
-    int selectFundPayCount(@Param("tradeNo") String tradeNo);
+    int selectFundPayCount(String tradeNo);
 
     @Select("select * from t_wechat_pay_order with(nolock) where trade_no=#{tradeNo}")
-    WxPayOrder selectOrderByTradeNo(@Param("tradeNo") String tradeNo);
+    WxPayOrder selectOrderByTradeNo(String tradeNo);
 
-    @Select("select trade_no, query_state_times,offset=datediff(minute,create_datetime,last_query_state) " +
-            "from t_wechat_pay_order with(nolock) where his_status=0 and order_type<=3 and openid!='' " +
+    @Select("select trade_no, query_state_times, " +
+            "offset=datediff(minute,create_datetime,last_query_state) " +
+            "from t_wechat_pay_order with(nolock) where his_status=0 " +
+            "and order_type in (1,2,3,5) and openid!='' and sgl_check_notified=0 " +
             "and trade_state in ('NOTPAY','ACCEPT','USERPAYING','PAYERROR') ")
     List<TradeNo> selectTradeNosForScheduleTask();
 
     @Select("select openid from t_wechat_patient_bind with(nolock) where patient_id=#{cardNo} and del_flag=0")
-    List<String> selectOpenidByPatientId(@Param("cardNo") String cardNo);
+    List<String> selectOpenidByPatientId(String cardNo);
 
     @Select("select openid from t_wechat_patient_bind with(nolock) where ic_card_no=#{cardNo} and del_flag=0")
-    List<String> selectOpenidByIcCardNo(@Param("cardNo") String cardNo);
+    List<String> selectOpenidByIcCardNo(String cardNo);
 
     @Select("select rtrim(a.code) doctorCode,rtrim(a.name) doctorName,rtrim(b.name) doctorTitle,rtrim(a.dept_code) deptCode, " +
             "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.dept_code), " +
@@ -126,14 +126,14 @@ public interface WxApiDao {
             "from a_employee_mi a with(nolock), zd_emp_title b with(nolock) " +
             "where a.code=#{doctorCode} and a.emp_tit_code=b.code and " +
             "a.code not in ('00000', '00026') and isnull(a.del_flag,0)<>1")
-    DoctorInfo selectDoctorInfo(@Param("doctorCode") String doctorCode);
+    DoctorInfo selectDoctorInfo(String doctorCode);
 
     @Select("select trade_no from t_wechat_pay_order where his_ord_num=#{hisOrdNum} " +
             "and trade_state not in ('SUCCESS','CLOSED')")
-    List<String> selectTradeNoByHisOrdNum(@Param("hisOrdNum") String hisOrdNum);
+    List<String> selectTradeNoByHisOrdNum(String hisOrdNum);
 
     @Select("select trade_state from t_wechat_pay_order with(nolock) where trade_no=#{tradeNo}")
-    Transaction.TradeStateEnum selectOrderStatus(@Param("tradeNo") String tradeNo);
+    Transaction.TradeStateEnum selectOrderStatus(String tradeNo);
 
     @Update("update t_wechat_pay_order set refund_id=#{refundId}, refund_op_code=#{refundOpCode}, his_status=0, " +
             "refund_op_datetime=#{refundOpDatetime}, refund_reason=#{refundReason}, trade_state='REFUND' where trade_no=#{tradeNo}")
@@ -143,12 +143,15 @@ public interface WxApiDao {
     void deleteOrder(String tradeNo);
 
     @Update("update t_wechat_pay_order set trade_state='REFUND', refund_reason=#{msg}, his_status=0 where trade_no=#{tradeNo}")
-    void alreadyRefund(@Param("tradeNo") String tradeNo, @Param("msg") String msg);
+    void alreadyRefund(String tradeNo, String msg);
 
     @Update("update t_si_setlinfo set mz_saved=1 where pat_no=#{patId} and times=#{times} and revoked=0")
-    void updateMzSavedFlag(@Param("patId") String patId, @Param("times") String times);
+    void updateMzSavedFlag(String patId, String times);
 
     @Select("select medfee_sumamt,fund_pay_sumamt,acct_pay,psn_cash_pay " +
             "from t_si_presetlinfo where pat_no=#{patNo} and times=#{times}")
-    MedinsPresettle selectPreSetlinfo(@Param("patNo") String patNo, @Param("times") String times);
+    MedinsPresettle selectPreSetlinfo(String patNo, String times);
+
+    @Update("update t_wechat_pay_order set sgl_check_notified=1 where trade_no=#{tradeNo}")
+    void updateCytjNotified(String tradeNo);
 }

+ 7 - 0
src/main/java/thyyxxk/wxservice_server/entity/wxapi/WxPayOrder.java

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
 import com.wechat.pay.java.service.payments.model.Transaction;
 import lombok.Data;
 import org.springframework.format.annotation.DateTimeFormat;
+import thyyxxk.wxservice_server.constant.OrderType;
 
 import java.math.BigDecimal;
 import java.util.Date;
@@ -65,4 +66,10 @@ public class WxPayOrder {
     private Integer admissTimes;
     private String apTime;
     private String sglCheckId;
+    private Boolean sglCheckNotified;
+
+    public Boolean sglCheckNotNotified() {
+        return orderType == OrderType.INSPECTIONS.getCode()
+                && (null == sglCheckNotified || !sglCheckNotified);
+    }
 }

+ 16 - 6
src/main/java/thyyxxk/wxservice_server/factory/cytj/CytjService.java

@@ -1,10 +1,11 @@
 package thyyxxk.wxservice_server.factory.cytj;
 
+import com.alibaba.fastjson.JSONObject;
+import com.wechat.pay.java.service.payments.model.Transaction;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.client.RestTemplate;
 import thyyxxk.wxservice_server.entity.wxapi.WxPayOrder;
 import thyyxxk.wxservice_server.factory.cytj.model.NotifyBody;
-import thyyxxk.wxservice_server.utils.DateUtil;
 import thyyxxk.wxservice_server.utils.DecimalTool;
 
 /**
@@ -14,20 +15,29 @@ import thyyxxk.wxservice_server.utils.DecimalTool;
 public class CytjService {
     private final static String URL = "http://172.16.32.100:8088/TaiheWeiXin/TradeStatusNotice";
 
-    public void notifyPaymentSuccess(WxPayOrder params) {
-        NotifyBody body = buildRequestBody(params);
+    public int notifyPaymentSuccess(WxPayOrder params, String payTime) {
+        NotifyBody body = buildRequestBody(params, payTime);
         String response = new RestTemplate().postForObject(URL, body, String.class);
         log.info("[创元体检]用户扫码支付成功通知:\n参数:{}\n结果:{}", body, response);
+        if (null == response) {
+            return -1;
+        }
+        JSONObject jsonObject = JSONObject.parseObject(response);
+        Integer rescode = jsonObject.getInteger("code");
+        if (null == rescode) {
+            return -1;
+        }
+        return rescode;
     }
 
-    private NotifyBody buildRequestBody(WxPayOrder params) {
+    private NotifyBody buildRequestBody(WxPayOrder params, String payTime) {
         NotifyBody notifyBody = new NotifyBody();
         notifyBody.setPatientId(params.getPatientId());
         notifyBody.setTradeNo(params.getTradeNo());
         notifyBody.setTotalAmt(DecimalTool.moneyYuanToFen(params.getTotalFee()));
         notifyBody.setSglcheckid(params.getSglCheckId());
-        notifyBody.setTradeState(params.getTradeState());
-        notifyBody.setPayTime(DateUtil.formatDatetime(params.getPayDatetime()));
+        notifyBody.setTradeState(Transaction.TradeStateEnum.SUCCESS);
+        notifyBody.setPayTime(payTime);
         return notifyBody;
     }
 

+ 9 - 0
src/main/java/thyyxxk/wxservice_server/service/SavePayResultService.java

@@ -24,6 +24,7 @@ import thyyxxk.wxservice_server.entity.inpatient.GetZyFeeParam;
 import thyyxxk.wxservice_server.entity.paymzfee.MedinsPresettle;
 import thyyxxk.wxservice_server.entity.wxapi.PushMessageParam;
 import thyyxxk.wxservice_server.entity.wxapi.WxPayOrder;
+import thyyxxk.wxservice_server.factory.cytj.CytjService;
 import thyyxxk.wxservice_server.factory.thmz.ThmzService;
 import thyyxxk.wxservice_server.factory.thmz.model.SaveAppointmentRequest;
 import thyyxxk.wxservice_server.factory.thmz.model.SaveClinicRegisterRequest;
@@ -273,4 +274,12 @@ public class SavePayResultService {
         }
         return "领取优惠券失败,请联系管理员。";
     }
+
+    public void notifyCytj(WxPayOrder order, String payTime) {
+        CytjService cytjService = new CytjService();
+        int rescode = cytjService.notifyPaymentSuccess(order, payTime);
+        if (rescode == 200) {
+            dao.updateCytjNotified(order.getTradeNo());
+        }
+    }
 }

+ 10 - 2
src/main/java/thyyxxk/wxservice_server/service/WxApiService.java

@@ -114,6 +114,10 @@ public class WxApiService {
         order.setPatientName(dao.selectPatientName(order.getPatientId()));
         order.setCreateDatetime(new Date());
         order.setTradeState(Transaction.TradeStateEnum.NOTPAY);
+        if (order.getOrderType() == OrderType.INSPECTIONS.getCode()
+                && StringUtil.isBlank(order.getOpenid())) {
+            order.setOpenid("inspection-user");
+        }
         dao.insertNewOrder(order);
         return ResultVoUtil.success(order);
     }
@@ -192,7 +196,7 @@ public class WxApiService {
                     case SELF_HELP_MACHINE:
                         return "订单已支付。";
                     case INSPECTIONS:
-                        new CytjService().notifyPaymentSuccess(order);
+                        savePayResultService.notifyCytj(order, successTime);
                         return "订单已支付。";
                     case COUPON_PURCHASE:
                         return savePayResultService.saveCouponPurchase(order, successTime);
@@ -262,8 +266,12 @@ public class WxApiService {
         }
 
         WechatPayGlobalRequest request = new WechatPayGlobalRequest.Builder()
-                .totalAmount(param.getTotalAmt()).description(param.getDescription()).build();
+                .totalAmount(param.getTotalAmt()).description(param.getDescription())
+                .build();
         NativePayResponse response = new WeChatPayService().nativePrepay(request);
+        if (param.getOrderType() == OrderType.INSPECTIONS.getCode()) {
+            response.setTradeNo("TJ_" + response.getTradeNo());
+        }
 
         long timesStamp = System.currentTimeMillis() / 1000;
         String orderTypeName = OrderType.get(param.getOrderType()).getLabel();

+ 19 - 20
src/main/java/thyyxxk/wxservice_server/service/WxPayNotifyService.java

@@ -7,9 +7,9 @@ import org.springframework.stereotype.Service;
 import thyyxxk.wxservice_server.constant.OrderType;
 import thyyxxk.wxservice_server.dao.WxApiDao;
 import thyyxxk.wxservice_server.entity.wxapi.WxPayOrder;
-import thyyxxk.wxservice_server.factory.cytj.CytjService;
 import thyyxxk.wxservice_server.factory.wechatpay.WeChatPayService;
 import thyyxxk.wxservice_server.factory.wechatpay.model.WechatPayGlobalRequest;
+import thyyxxk.wxservice_server.utils.DateUtil;
 import thyyxxk.wxservice_server.utils.TradeVectorUtil;
 
 import javax.servlet.http.HttpServletRequest;
@@ -35,32 +35,34 @@ public class WxPayNotifyService {
         Transaction transaction = new WeChatPayService().decryptNotify(request);
         String tradeNo = transaction.getOutTradeNo();
         WxPayOrder order = dao.selectOrderByTradeNo(tradeNo);
-
-        Transaction.TradeStateEnum tradeState = order.getTradeState();
-        if (order.getOrderType() == OrderType.INSPECTIONS.getCode()
-                && transaction.getTradeState() == Transaction.TradeStateEnum.SUCCESS) {
-            new CytjService().notifyPaymentSuccess(order);
+        Transaction.TradeStateEnum dbTradeState = order.getTradeState();
+        if (dbTradeState == Transaction.TradeStateEnum.SUCCESS) {
+            log.info("订单号【{}】已支付成功,无需继续解密。", tradeNo);
+            if (order.sglCheckNotNotified()) {
+                savePayResultService.notifyCytj(order, DateUtil.formatDatetime(order.getPayDatetime()));
+            }
+            return;
         }
-
-        if (tradeState == Transaction.TradeStateEnum.SUCCESS ||
-                tradeState == Transaction.TradeStateEnum.REFUND) {
-            log.info("订单号【{}】已解密过,无需继续解密。", tradeNo);
+        if (dbTradeState == Transaction.TradeStateEnum.REFUND) {
+            log.info("订单号【{}】已退款,无需继续解密。", tradeNo);
             return;
         }
         if (TradeVectorUtil.tradeNoBeingQuery(tradeNo)) {
             log.info("订单号【{}】正在业务中,无需继续解密。", tradeNo);
             return;
         }
+
+        Transaction.TradeStateEnum wxTradeState = transaction.getTradeState();
         String openid = transaction.getPayer().getOpenid();
-        tradeState = transaction.getTradeState();
-        if (tradeState != Transaction.TradeStateEnum.SUCCESS) {
-            dao.updatePayStatusAndQueryTimesAndOpenid(tradeNo, tradeState, openid);
+        if (wxTradeState != Transaction.TradeStateEnum.SUCCESS) {
+            dao.updatePayStatusAndQueryTimesAndOpenid(tradeNo, wxTradeState, openid);
             return;
         }
-        order.setTradeState(Transaction.TradeStateEnum.SUCCESS);
+
+        order.setTradeState(wxTradeState);
         String successTime = transaction.getSuccessTime()
                 .split("\\+")[0].replace("T", " ");
-        dao.updatePayStatusAndPayTimeAndOpenid(tradeNo, tradeState, successTime, openid);
+        dao.updatePayStatusAndPayTimeAndOpenid(tradeNo, wxTradeState, successTime, openid);
         OrderType orderType = OrderType.get(order.getOrderType());
         switch (orderType) {
             case OUTPATIENT:
@@ -69,11 +71,8 @@ public class WxPayNotifyService {
             case INPATIENT_PRE_PAY:
                 savePayResultService.saveZyYjjInfo(order, successTime);
                 break;
+            case INSPECTIONS:
+                savePayResultService.notifyCytj(order, successTime);
         }
     }
-
-    public void testNotify(String tradeNo) {
-        WxPayOrder order = dao.selectOrderByTradeNo(tradeNo);
-        new CytjService().notifyPaymentSuccess(order);
-    }
 }