Sfoglia il codice sorgente

优惠券功能优化

lighter 1 anno fa
parent
commit
378e5b1ec1

+ 1 - 1
pom.xml

@@ -10,7 +10,7 @@
     </parent>
     <groupId>thyyxxk</groupId>
     <artifactId>wxservice-server</artifactId>
-    <version>12.0.4</version>
+    <version>12.0.7</version>
     <name>wxservice-server</name>
     <description>server for wxservice-web</description>
 

+ 10 - 0
src/main/java/thyyxxk/wxservice_server/controller/CouponController.java

@@ -31,6 +31,16 @@ public class CouponController {
         return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
     }
 
+    @GetMapping("/receiveSalesmanCoupon")
+    public ResultVo<String> receiveSalesmanCoupon(@RequestParam("key") String key) {
+        String successResponse = "优惠券领取成功,请前往【个人中心-我的卡券】查看。";
+        String response = service.receiveSalesmanCoupon(key);
+        if (response.equals("SUCCESS")) {
+            return ResultVoUtil.success(successResponse);
+        }
+        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
+    }
+
     @GetMapping("/getMyCoupons")
     public ResultVo<GetCouponResponse> getMyCoupons(@RequestParam("hisOrdNum") String hisOrdNum) {
         GetCouponResponse response = service.getMyCoupons(hisOrdNum);

+ 15 - 0
src/main/java/thyyxxk/wxservice_server/dao/CouponDao.java

@@ -2,6 +2,7 @@ package thyyxxk.wxservice_server.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Select;
+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;
@@ -13,10 +14,19 @@ public interface CouponDao extends BaseMapper<PatientCoupon> {
     @Select("select id,value,effective_days,salesman='SystemGift' from t_zd_coupons where id='assessment_gift'")
     CouponAttribute selectAssessmentGiftCouponValue();
 
+    @Select("select id,value,left_quantity,effective_days from t_zd_coupons where id=#{couponId}")
+    CouponAttribute selectSalesmanOperationCouponValue(String couponId);
+
+    @Update("update t_zd_coupons set left_quantity=left_quantity-1 where id=#{couponId}")
+    int updateLeftQuantity(String couponId);
+
     @Select("select count(1) from t_patient_coupons where openid=#{openid} " +
             "and coupon_id='assessment_gift' and coupon_source=#{source}")
     int selectSameAssessmentGiftCouponCount(String openid, String source);
 
+    @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 " +
             "from t_patient_coupons a, t_zd_coupons b " +
@@ -26,4 +36,9 @@ public interface CouponDao extends BaseMapper<PatientCoupon> {
     @Select("select ((select count(1) from t_si_setlinfo where pat_no=#{patNo} and times=#{times} and revoked=0) + " +
             "(select count(1) from t_si_presetlinfo where pat_no=#{patNo} and times=#{times}) )")
     int selectMedinsCount(String patNo, int times);
+
+    @Update("update t_patient_coupons set coupon_state='EXPIRED' " +
+            "where expire_date<=getdate()+1 and coupon_state='USABLE'")
+    int expireCoupon();
 }
+

+ 1 - 0
src/main/java/thyyxxk/wxservice_server/entity/coupon/CouponAttribute.java

@@ -6,6 +6,7 @@ import lombok.Data;
 public class CouponAttribute {
     private String id;
     private Integer value;
+    private Integer leftQuantity;
     private Integer effectiveDays;
     private String salesman;
 }

+ 25 - 0
src/main/java/thyyxxk/wxservice_server/scheduled/ExpireCoupons.java

@@ -0,0 +1,25 @@
+package thyyxxk.wxservice_server.scheduled;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import thyyxxk.wxservice_server.dao.CouponDao;
+
+@Slf4j
+@Component
+public class ExpireCoupons {
+    private final CouponDao dao;
+
+    @Autowired
+    public ExpireCoupons(CouponDao dao) {
+        this.dao = dao;
+    }
+
+    @Scheduled(cron = "0 0 5 * * ?")
+    public void executeTask() {
+        int expiredCount = dao.expireCoupon();
+        log.info("优惠券过期:{} 张", expiredCount);
+    }
+
+}

+ 31 - 3
src/main/java/thyyxxk/wxservice_server/service/CouponService.java

@@ -1,5 +1,6 @@
 package thyyxxk.wxservice_server.service;
 
+import cn.hutool.core.codec.Base64;
 import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,13 +34,42 @@ public class CouponService {
                 .openid(openid).couponAttribute(attribute)
                 .couponSource(source).build().make();
         int insertResponse = dao.insert(coupon);
-        log.info("发放问卷调查礼品卡券:\n参数:{},结果:{}", JSON.toJSON(coupon), insertResponse);
+        log.info("发放问卷调查礼品卡券:\n参数:{},结果:{}",
+                JSON.toJSONStringWithDateFormat(coupon, "yyyy.MM.dd"), insertResponse);
         if (insertResponse == 1) {
             return "SUCCESS";
         }
         return "问卷调查礼品卡券发放失败,请联系管理员。";
     }
 
+    public synchronized String receiveSalesmanCoupon(String key) {
+        String openid = TokenUtil.getInstance().getUserOpenid();
+        String[] decode = Base64.decodeStr(key).split("-");
+        String couponId = decode[0];
+        CouponAttribute attribute = dao.selectSalesmanOperationCouponValue(couponId);
+        if (null == attribute) {
+            return "无效的优惠券。";
+        }
+        int sameCouponCount = dao.selectSameSalesmanOperationCouponCount(openid, couponId);
+        if (sameCouponCount > 0) {
+            return "您已领取过此优惠券,无法继续领取。";
+        }
+        if (attribute.getLeftQuantity() < 1) {
+            return "优惠券已无余量,领取失败。";
+        }
+        attribute.setSalesman(decode[1]);
+        PatientCoupon coupon = new CouponFactory.Builder()
+                .openid(openid).couponAttribute(attribute)
+                .couponSource("SALESMAN_OPERATION").build().make();
+        int insertResponse = dao.insert(coupon);
+        log.info("患者领取优惠券:\n参数:{},结果:{}",
+                JSON.toJSONStringWithDateFormat(coupon, "yyyy.MM.dd"), insertResponse);
+        if (insertResponse == 1 && dao.updateLeftQuantity(couponId) == 1) {
+            return "SUCCESS";
+        }
+        return "领取优惠券失败,请联系管理员。";
+    }
+
     public GetCouponResponse getMyCoupons(String hisOrdNum) {
         String openid = TokenUtil.getInstance().getUserOpenid();
         boolean isMedins = ifMedinsExist(hisOrdNum);
@@ -56,13 +86,11 @@ public class CouponService {
             response.setUsableCoupons(usableCouponList);
             response.setUnusableCoupons(new ArrayList<>());
         }
-
         List<PatientCoupon> unusableCoupons = dao.selectCouponsByState(openid, CouponState.EXPIRED);
         List<CouponModel> unusableCouponList = new ArrayList<>();
         for (PatientCoupon coupon : unusableCoupons) {
             unusableCouponList.add(new CouponModel.Builder().build(coupon, isMedins));
         }
-
         response.getUnusableCoupons().addAll(unusableCouponList);
         return response;
     }