Bladeren bron

添加购买优惠券的功能

lighter 11 maanden geleden
bovenliggende
commit
31c6ca6a26

+ 3 - 1
src/main/java/thyyxxk/wxservice_server/config/exception/ExceptionEnum.java

@@ -27,9 +27,10 @@ public enum ExceptionEnum {
     BAD_REQUEST(2004, "无法找到请求的接口!"),
 
     /**
-     * 以下是需要弹窗提示并且重定向到登录页面
+     * 以下是需要弹窗并且关闭微信浏览器的错误
      * */
     TOKEN_NOT_EXIST(3001, "没有找到鉴权凭据,请关注公众号【长沙泰和医院服务号】,重新进入服务。"),
+    COUPON_NOT_EXIST(3001, "此优惠券已停用,敬请谅解。"),
     TOKEN_ERROR(3002, "鉴权失败,无法访问。"),
 
     /**
@@ -37,6 +38,7 @@ public enum ExceptionEnum {
      * */
     SLIGHTLY_ERROR(4001, "不需要提示的错误。"),
     CONTINUE_NEXT_STEP(4002, ""),
+
     /**
      * 以下是内部使用的错误
      * */

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

@@ -13,6 +13,8 @@ public enum OrderType {
 
     INSPECTIONS(5, "体检缴费"),
 
+    COUPON_PURCHASE(6, "购买优惠券"),
+
     UNDEFINED(99, "未说明的费用类型");
 
     private final int code;

+ 14 - 2
src/main/java/thyyxxk/wxservice_server/controller/CouponController.java

@@ -1,11 +1,11 @@
 package thyyxxk.wxservice_server.controller;
 
+import cn.hutool.core.codec.Base64;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
 import thyyxxk.wxservice_server.entity.ResultVo;
-import thyyxxk.wxservice_server.entity.coupon.CouponInquiry;
-import thyyxxk.wxservice_server.entity.coupon.GetCouponResponse;
+import thyyxxk.wxservice_server.entity.coupon.*;
 import thyyxxk.wxservice_server.service.CouponService;
 import thyyxxk.wxservice_server.utils.ResultVoUtil;
 
@@ -44,4 +44,16 @@ public class CouponController {
         GetCouponResponse response = service.getMyCoupons(inquiry);
         return ResultVoUtil.success(response);
     }
+
+    @GetMapping("/getCouponInfo")
+    public ResultVo<ZdCoupon> getCouponInfo(@RequestParam("key") String key) {
+        String[] decode = Base64.decodeStr(key).split("-");
+        String couponId = decode[0];
+        ZdCoupon zdCoupon = service.getCouponInfo(couponId);
+        if (null == zdCoupon || zdCoupon.getState() == ActiveState.DEACTIVATED) {
+            return ResultVoUtil.fail(ExceptionEnum.COUPON_NOT_EXIST);
+        }
+        zdCoupon.setSalesman(decode[1]);
+        return ResultVoUtil.success(zdCoupon);
+    }
 }

+ 4 - 6
src/main/java/thyyxxk/wxservice_server/dao/CouponDao.java

@@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Update;
 import thyyxxk.wxservice_server.entity.coupon.CouponAttribute;
 import thyyxxk.wxservice_server.entity.coupon.CouponState;
 import thyyxxk.wxservice_server.entity.coupon.PatientCoupon;
+import thyyxxk.wxservice_server.entity.coupon.ZdCoupon;
 
 import java.util.List;
 
@@ -29,12 +30,6 @@ public interface CouponDao extends BaseMapper<PatientCoupon> {
     @Select("select count(1) from t_patient_coupons where openid=#{openid} and coupon_id=#{couponId} ")
     int selectSameSalesmanOperationCouponCount(String openid, String couponId);
 
-    @Select("select a.id,a.coupon_id,a.coupon_value,a.create_date,a.expire_date, " +
-            "a.coupon_state,b.name as couponName,b.description as couponDescription, " +
-            "b.available_charge from t_patient_coupons a, t_zd_coupons b " +
-            "where a.openid=#{openid} and a.coupon_state=#{state} and a.coupon_id=b.id ")
-    List<PatientCoupon> selectCouponsByState(String openid, CouponState state);
-
     @Select("select a.id,a.coupon_id,a.coupon_value,a.create_date,a.expire_date, " +
             "a.coupon_state,b.name as couponName,b.description as couponDescription, " +
             "b.available_charge from t_patient_coupons a, t_zd_coupons b " +
@@ -48,5 +43,8 @@ public interface CouponDao extends BaseMapper<PatientCoupon> {
     @Update("update t_patient_coupons set coupon_state='EXPIRED' " +
             "where expire_date<=getdate()+1 and coupon_state='USABLE'")
     int expireCoupon();
+
+    @Select("select * from t_zd_coupons where id=#{id} and state='ACTIVATED' and left_quantity>0")
+    ZdCoupon getCouponInfo(String id);
 }
 

+ 6 - 0
src/main/java/thyyxxk/wxservice_server/entity/coupon/ActiveState.java

@@ -0,0 +1,6 @@
+package thyyxxk.wxservice_server.entity.coupon;
+
+public enum ActiveState {
+    ACTIVATED,
+    DEACTIVATED,
+}

+ 6 - 0
src/main/java/thyyxxk/wxservice_server/entity/coupon/CouponGainWay.java

@@ -0,0 +1,6 @@
+package thyyxxk.wxservice_server.entity.coupon;
+
+public enum CouponGainWay {
+    FREE,
+    PURCHASE,
+}

+ 6 - 0
src/main/java/thyyxxk/wxservice_server/entity/coupon/CouponType.java

@@ -0,0 +1,6 @@
+package thyyxxk.wxservice_server.entity.coupon;
+
+public enum CouponType {
+    SYSTEM_PROVIDE,
+    SALESMAN_OPERATION
+}

+ 25 - 0
src/main/java/thyyxxk/wxservice_server/entity/coupon/ZdCoupon.java

@@ -0,0 +1,25 @@
+package thyyxxk.wxservice_server.entity.coupon;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class ZdCoupon {
+    private String id;
+    private String name;
+    private Integer value;
+    private Integer totalQuantity;
+    private Integer leftQuantity;
+    private Integer effectiveDays;
+    private Date createTime;
+    private String createStaff;
+    private ActiveState state;
+    private CouponType type;
+    private CouponGainWay gainWay;
+    private Integer price;
+    private String description;
+    private String availableCharge;
+    private String availableChargeName;
+    private String salesman;
+}

+ 4 - 0
src/main/java/thyyxxk/wxservice_server/service/CouponService.java

@@ -119,4 +119,8 @@ public class CouponService {
         int count = dao.selectMedinsCount(arr[0], Integer.parseInt(arr[1]));
         return count > 0;
     }
+
+    public ZdCoupon getCouponInfo(String couponId) {
+        return dao.getCouponInfo(couponId);
+    }
 }

+ 34 - 1
src/main/java/thyyxxk/wxservice_server/service/SavePayResultService.java

@@ -1,5 +1,6 @@
 package thyyxxk.wxservice_server.service;
 
+import cn.hutool.core.codec.Base64;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.wechat.pay.java.service.payments.model.Transaction;
@@ -10,10 +11,14 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
 import thyyxxk.wxservice_server.constant.CardCostTypes;
+import thyyxxk.wxservice_server.dao.CouponDao;
 import thyyxxk.wxservice_server.dao.InpatientDao;
 import thyyxxk.wxservice_server.dao.WxApiDao;
 import thyyxxk.wxservice_server.entity.PureCodeName;
 import thyyxxk.wxservice_server.entity.ResultVo;
+import thyyxxk.wxservice_server.entity.coupon.CouponAttribute;
+import thyyxxk.wxservice_server.entity.coupon.CouponFactory;
+import thyyxxk.wxservice_server.entity.coupon.PatientCoupon;
 import thyyxxk.wxservice_server.entity.electronichealthcard.HisRegister;
 import thyyxxk.wxservice_server.entity.inpatient.GetZyFeeParam;
 import thyyxxk.wxservice_server.entity.paymzfee.MedinsPresettle;
@@ -39,6 +44,7 @@ import java.util.concurrent.TimeUnit;
 public class SavePayResultService {
     private final WxApiDao dao;
     private final InpatientDao yjjDao;
+    private final CouponDao couponDao;
     private final ThmzService thmzService;
     private final WxRefundService refundService;
     private final PushWxMessageService pushWxMessageService;
@@ -47,10 +53,11 @@ public class SavePayResultService {
     private String siMzApiUrl;
 
     @Autowired
-    public SavePayResultService(WxApiDao dao, InpatientDao yjjDao, ThmzService thmzService, WxRefundService refundService,
+    public SavePayResultService(WxApiDao dao, InpatientDao yjjDao, CouponDao couponDao, ThmzService thmzService, WxRefundService refundService,
                                 PushWxMessageService pushWxMessageService, ElectronicHealthCardService healthCardService) {
         this.dao = dao;
         this.yjjDao = yjjDao;
+        this.couponDao = couponDao;
         this.thmzService = thmzService;
         this.refundService = refundService;
         this.pushWxMessageService = pushWxMessageService;
@@ -240,4 +247,30 @@ public class SavePayResultService {
         return payTime;
     }
 
+    public String saveCouponPurchase(WxPayOrder order) {
+        String[] decode = Base64.decodeStr(order.getCouponId()).split("-");
+        String couponId = decode[0];
+        CouponAttribute attribute = couponDao.selectSalesmanOperationCouponValue(couponId);
+        if (null == attribute) {
+            String tradeNo = order.getTradeNo();
+            String refund = refundService.autoRefund(tradeNo, "无效的优惠券,领取失败。");
+            if (!refund.startsWith("ERROR:")) {
+                dao.refundOrder(tradeNo, "无效的优惠券,领取失败。");
+                return "ERROR:【无效的优惠券,领取失败。】已为您自动退款,请留意到账信息。";
+            }
+            return "ERROR:【无效的优惠券,领取失败。】自动退款失败,请联系服务中心进行退款。";
+        }
+        attribute.setSalesman(decode[1]);
+        PatientCoupon coupon = new CouponFactory.Builder()
+                .openid(order.getOpenid()).couponAttribute(attribute)
+                .couponSource("SALESMAN_OPERATION").build().make();
+        int insertResponse = couponDao.insert(coupon);
+        log.info("患者领取优惠券:\n参数:{},结果:{}",
+                JSON.toJSONStringWithDateFormat(coupon, "yyyy.MM.dd"), insertResponse);
+        if (insertResponse == 1) {
+            couponDao.updateLeftQuantity(order.getCouponId());
+            return "SUCCESS";
+        }
+        return "领取优惠券失败,请联系管理员。";
+    }
 }

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

@@ -191,6 +191,8 @@ public class WxApiService {
                     case SELF_HELP_MACHINE:
                     case INSPECTIONS:
                         return "订单已支付。";
+                    case COUPON_PURCHASE:
+                        return savePayResultService.saveCouponPurchase(order);
                     default:
                         return "ERROR:未识别到的订单类型,请联系服务中心。";
                 }
@@ -224,7 +226,6 @@ public class WxApiService {
         order.setSerialNo(SnowFlakeId.instance().nextId());
         order.setOrderType(OrderType.OUTPATIENT.getCode());
         order.setHisOrdNum(param.getHisOrdNum());
-//        MedinsPresettle presettle = getFundPayAmt(param.getHisOrdNum());
         order.setFundpayAmt(BigDecimal.ZERO);
         order.setAcctpayAmt(BigDecimal.ZERO);
         order.setCashpayAmt(DecimalTool.moneyFenToYuan(param.getTotalAmt()));

+ 2 - 2
src/main/resources/application.yml

@@ -8,8 +8,8 @@ spring:
   thymeleaf:
     cache: false
   datasource:
-    url: "jdbc:sqlserver://172.16.32.179:1433;databaseName=thxyhisdb"
-#    url: "jdbc:sqlserver://172.16.32.168:1433;databaseName=thxyhisdb"
+#    url: "jdbc:sqlserver://172.16.32.179:1433;databaseName=thxyhisdb"
+    url: "jdbc:sqlserver://172.16.32.168:1433;databaseName=thxyhisdb"
     username: "sa"
     password:
     driver-class-name: "com.microsoft.sqlserver.jdbc.SQLServerDriver"