Browse Source

[增加]新增充值会员卡接口

liying 7 years ago
parent
commit
a6b8c7a4ee

File diff suppressed because it is too large
+ 893 - 314
.idea/workspace.xml


+ 8 - 3
src/main/java/com/example/cinema/bl/VIPService.java

@@ -1,18 +1,23 @@
 package com.example.cinema.bl;
 
-import com.example.cinema.form.VIPCardForm;
-import com.example.cinema.strategy.ChargeStrategy;
+import com.example.cinema.vo.VIPCardForm;
 import com.example.cinema.vo.ResponseVO;
 
+
+
 /**
  * Created by liying on 2019/4/14.
  */
 
 public interface VIPService {
 
-    ResponseVO addVIPCard(VIPCardForm vipCardForm, ChargeStrategy chargeStrategy);
+    ResponseVO addVIPCard(int userId);
 
     ResponseVO getCardById(int id);
 
+    ResponseVO getVIPInfo();
+
+    ResponseVO charge(VIPCardForm vipCardForm);
+
 
 }

+ 56 - 11
src/main/java/com/example/cinema/bl/VIPServiceImpl.java

@@ -1,13 +1,19 @@
 package com.example.cinema.bl;
 
 import com.example.cinema.data.VIPCardMapper;
-import com.example.cinema.form.VIPCardForm;
+import com.example.cinema.vo.VIPCardForm;
 import com.example.cinema.po.VIPCard;
 import com.example.cinema.strategy.ChargeStrategy;
+import com.example.cinema.strategy.ReturnStrategy;
 import com.example.cinema.vo.ResponseVO;
+import com.example.cinema.vo.VIPInfoVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Created by liying on 2019/4/14.
  */
@@ -17,27 +23,66 @@ public class VIPServiceImpl implements VIPService {
     VIPCardMapper vipCardMapper;
 
     @Override
-    public ResponseVO addVIPCard(VIPCardForm vipCardForm, ChargeStrategy chargeStrategy) {
-        VIPCard vipCard=new VIPCard();
-        vipCard.setUserId(vipCardForm.getUserId());
-        if(vipCardForm.getAmount()<VIPCard.price){
-            return ResponseVO.buildFailure("充值金额不足,无法完成");
-        }
-        vipCard.setBalance(chargeStrategy.calculate(vipCardForm.getAmount())-VIPCard.price);
+    public ResponseVO addVIPCard(int userId) {
+        VIPCard vipCard = new VIPCard();
+        vipCard.setUserId(userId);
+        vipCard.setBalance(0);
         try {
             return ResponseVO.buildSuccess(vipCardMapper.insertOneCard(vipCard));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");
         }
-
     }
 
     @Override
     public ResponseVO getCardById(int id) {
-        VIPCard vipCard=vipCardMapper.selectCardById(id);
         try {
-        return ResponseVO.buildSuccess(vipCardMapper.selectCardById(id));
+            return ResponseVO.buildSuccess(vipCardMapper.selectCardById(id));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResponseVO.buildFailure("失败");
+        }
+    }
+
+    @Override
+    public ResponseVO getVIPInfo() {
+        List<ChargeStrategy> strategies = new ArrayList<>();
+        strategies.add(new ReturnStrategy());
+        VIPInfoVO vipInfoVO = new VIPInfoVO();
+        vipInfoVO.setStrategies(strategies);
+        vipInfoVO.setPrice(VIPCard.price);
+        return ResponseVO.buildSuccess(vipInfoVO);
+    }
+
+    @Override
+    public ResponseVO charge(VIPCardForm vipCardForm) {
+        if (vipCardForm.getStrategyName() == null) {
+            return ResponseVO.buildFailure("充值优惠策略不能为空");
+        }
+        ChargeStrategy chargeStrategy;
+        try {
+            Class c = Class.forName(vipCardForm.getStrategyName());
+            //如果是抽象类或接口
+            if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
+                return ResponseVO.buildFailure("充值优惠策略不存在");
+            }
+            chargeStrategy = (ChargeStrategy) c.newInstance();
+        } catch (ClassNotFoundException e) {
+            return ResponseVO.buildFailure("充值优惠策略不存在");
+        } catch (IllegalAccessException e) {
+            chargeStrategy = new ReturnStrategy();
+        } catch (InstantiationException e) {
+            chargeStrategy = new ReturnStrategy();
+        }
+        VIPCard vipCard=vipCardMapper.selectCardById(vipCardForm.getVipId());
+        if(vipCard==null){
+            return ResponseVO.buildFailure("会员卡不存在");
+        }
+        double balance = chargeStrategy.calculate(vipCardForm.getAmount());
+        vipCard.setBalance(vipCard.getBalance()+balance);
+        try {
+            return ResponseVO.buildSuccess(vipCardMapper.updateCardBalance(vipCardForm.getVipId(),balance));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");

+ 19 - 7
src/main/java/com/example/cinema/controller/VIPCardController.java

@@ -1,8 +1,7 @@
 package com.example.cinema.controller;
 
 import com.example.cinema.bl.VIPService;
-import com.example.cinema.form.VIPCardForm;
-import com.example.cinema.strategy.ReturnStrategy;
+import com.example.cinema.vo.VIPCardForm;
 import com.example.cinema.vo.ResponseVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
@@ -10,19 +9,32 @@ import org.springframework.web.bind.annotation.*;
 /**
  * Created by liying on 2019/4/14.
  */
-@RestController("/vip")
+@RestController()
+@RequestMapping("/vip")
 public class VIPCardController {
     @Autowired
     VIPService vipService;
 
     @PostMapping("/add")
-    public ResponseVO addVIP(@RequestBody VIPCardForm vipCardForm){
-        return vipService.addVIPCard(vipCardForm,new ReturnStrategy());
+    public ResponseVO addVIP(@RequestParam int userId){
+        return vipService.addVIPCard(userId);
     }
-
     @GetMapping("{id}/get")
-    public ResponseVO addVIP(@PathVariable int id){
+    public ResponseVO getVIP(@PathVariable int id){
         return vipService.getCardById(id);
     }
 
+    @GetMapping("/getVIPInfo")
+    public ResponseVO getVIPInfo(){
+        return vipService.getVIPInfo();
+    }
+
+    @PostMapping("/charge")
+    public ResponseVO charge(@RequestBody VIPCardForm vipCardForm){
+        return vipService.charge(vipCardForm);
+    }
+
+
+
+
 }

+ 3 - 0
src/main/java/com/example/cinema/data/VIPCardMapper.java

@@ -2,6 +2,7 @@ package com.example.cinema.data;
 
 import com.example.cinema.po.VIPCard;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * Created by liying on 2019/4/14.
@@ -13,6 +14,8 @@ public interface VIPCardMapper {
 
     VIPCard selectCardById(int id);
 
+    VIPCard updateCardBalance(@Param("id") int id,@Param("balance") double balance);
+
 
 
 }

+ 0 - 33
src/main/java/com/example/cinema/form/VIPCardForm.java

@@ -1,33 +0,0 @@
-package com.example.cinema.form;
-
-/**
- * Created by liying on 2019/4/14.
- */
-public class VIPCardForm {
-
-    /**
-     * 用户id
-     */
-    private int userId;
-
-    /**
-     * 付款金额
-     */
-    private int amount;
-
-    public int getUserId() {
-        return userId;
-    }
-
-    public void setUserId(int userId) {
-        this.userId = userId;
-    }
-
-    public int getAmount() {
-        return amount;
-    }
-
-    public void setAmount(int amount) {
-        this.amount = amount;
-    }
-}

+ 6 - 0
src/main/java/com/example/cinema/strategy/ChargeStrategy.java

@@ -16,4 +16,10 @@ public interface ChargeStrategy {
      * @return
      */
     double calculate(double amount);
+
+    /**
+     * 获得类名
+     * @return
+     */
+    String getName();
 }

+ 8 - 0
src/main/java/com/example/cinema/strategy/ReturnStrategy.java

@@ -6,6 +6,8 @@ package com.example.cinema.strategy;
 public class ReturnStrategy implements ChargeStrategy {
     private String description="满200送30";
 
+    private String className="ReturnStrategy";
+
 
     @Override
     public String getDescription() {
@@ -17,4 +19,10 @@ public class ReturnStrategy implements ChargeStrategy {
         return (int)(amount/200)*30+amount;
 
     }
+
+    @Override
+    public String getName() {
+        return className;
+    }
+
 }

+ 43 - 0
src/main/java/com/example/cinema/vo/VIPCardForm.java

@@ -0,0 +1,43 @@
+package com.example.cinema.vo;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+public class VIPCardForm {
+
+    /**
+     * vip卡id
+     */
+    private int vipId;
+
+    /**
+     * 付款金额
+     */
+    private int amount;
+
+    private String strategyName;
+
+    public int getVipId() {
+        return vipId;
+    }
+
+    public void setVipId(int vipId) {
+        this.vipId = vipId;
+    }
+
+    public int getAmount() {
+        return amount;
+    }
+
+    public void setAmount(int amount) {
+        this.amount = amount;
+    }
+
+    public String getStrategyName() {
+        return strategyName;
+    }
+
+    public void setStrategyName(String strategyName) {
+        strategyName = strategyName;
+    }
+}

+ 31 - 0
src/main/java/com/example/cinema/vo/VIPInfoVO.java

@@ -0,0 +1,31 @@
+package com.example.cinema.vo;
+
+import com.example.cinema.strategy.ChargeStrategy;
+
+import java.util.List;
+
+/**
+ * Created by liying on 2019/4/15.
+ */
+public class VIPInfoVO {
+
+    List<ChargeStrategy> strategies;
+
+    double price;
+
+    public List<ChargeStrategy> getStrategies() {
+        return strategies;
+    }
+
+    public void setStrategies(List<ChargeStrategy> strategies) {
+        this.strategies = strategies;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+}

+ 1 - 4
src/test/java/com/example/cinema/bl/VIPServiceImplTest.java

@@ -1,8 +1,6 @@
 package com.example.cinema.bl;
 
-import com.example.cinema.form.VIPCardForm;
-import com.example.cinema.po.VIPCard;
-import com.example.cinema.strategy.ReturnStrategy;
+import com.example.cinema.vo.VIPCardForm;
 import com.example.cinema.vo.ResponseVO;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -25,7 +23,6 @@ public class VIPServiceImplTest {
         VIPCardForm vipCardForm=new VIPCardForm();
         vipCardForm.setUserId(13);
         vipCardForm.setAmount(300);
-        vipService.addVIPCard(vipCardForm,new ReturnStrategy());
     }
 
     @Test

BIN
target/classes/com/example/cinema/bl/ScheduleService.class


BIN
target/classes/com/example/cinema/bl/ScheduleServiceImpl.class


BIN
target/classes/com/example/cinema/controller/ScheduleController.class


BIN
target/classes/com/example/cinema/data/ScheduleMapper.class


BIN
target/classes/com/example/cinema/vo/ScheduleForm.class


Some files were not shown because too many files changed in this diff