Browse Source

[增加]新增办理vip卡以及查看vip卡接口

liying 7 years ago
parent
commit
7dcf30c00f

+ 5 - 2
src/main/java/com/example/cinema/bl/MovieServiceImpl.java

@@ -1,5 +1,6 @@
 package com.example.cinema.bl;
 
+import com.example.cinema.data.AccountMapper;
 import com.example.cinema.data.MovieLikeMapper;
 import com.example.cinema.data.MovieMapper;
 
@@ -24,6 +25,8 @@ public class MovieServiceImpl implements MovieService {
     private MovieMapper movieMapper;
     @Autowired
     private MovieLikeMapper movieLikeMapper;
+    @Autowired
+    private AccountMapper accountMapper;
 
     @Override
     public ResponseVO addMovie(MovieForm addMovieForm) {
@@ -59,6 +62,7 @@ public class MovieServiceImpl implements MovieService {
 
     @Override
     public ResponseVO likeMovie(int userId, int movieId) {
+
         //todo: user 判空
         if (userLikeTheMovie(userId, movieId)) {
             return ResponseVO.buildFailure(ALREADY_LIKE_ERROR_MESSAGE);
@@ -81,8 +85,7 @@ public class MovieServiceImpl implements MovieService {
             return ResponseVO.buildFailure(MOVIE_NOT_EXIST_ERROR_MESSAGE);
         }
         try {
-            movieLikeMapper.deleteOneLike(movieId, userId);
-            return ResponseVO.buildSuccess(movieLikeMapper.deleteOneLike(userId, movieId));
+            return ResponseVO.buildSuccess(movieLikeMapper.deleteOneLike(movieId, userId));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");

+ 18 - 0
src/main/java/com/example/cinema/bl/VIPService.java

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

+ 48 - 0
src/main/java/com/example/cinema/bl/VIPServiceImpl.java

@@ -0,0 +1,48 @@
+package com.example.cinema.bl;
+
+import com.example.cinema.data.VIPCardMapper;
+import com.example.cinema.form.VIPCardForm;
+import com.example.cinema.po.VIPCard;
+import com.example.cinema.strategy.ChargeStrategy;
+import com.example.cinema.vo.ResponseVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+@Service
+public class VIPServiceImpl implements VIPService {
+    @Autowired
+    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);
+        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));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResponseVO.buildFailure("失败");
+        }
+    }
+
+
+}

+ 28 - 0
src/main/java/com/example/cinema/controller/VIPCardController.java

@@ -0,0 +1,28 @@
+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.ResponseVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+@RestController("/vip")
+public class VIPCardController {
+    @Autowired
+    VIPService vipService;
+
+    @PostMapping("/add")
+    public ResponseVO addVIP(@RequestBody VIPCardForm vipCardForm){
+        return vipService.addVIPCard(vipCardForm,new ReturnStrategy());
+    }
+
+    @GetMapping("{id}/get")
+    public ResponseVO addVIP(@PathVariable int id){
+        return vipService.getCardById(id);
+    }
+
+}

+ 2 - 2
src/main/java/com/example/cinema/data/MovieLikeMapper.java

@@ -1,6 +1,6 @@
 package com.example.cinema.data;
 
-import com.example.cinema.vo.DateLikeForm;
+import com.example.cinema.po.DateLike;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -49,5 +49,5 @@ public interface MovieLikeMapper {
      * @param movieId
      * @return
      */
-    List<DateLikeForm> getDateLikeNum(@Param("movieId") int movieId);
+    List<DateLike> getDateLikeNum(@Param("movieId") int movieId);
 }

+ 1 - 1
src/main/java/com/example/cinema/data/MovieLikeMapper.xml

@@ -22,7 +22,7 @@
         select count(*) as like_num ,DATE(like_time) as `time` from movie_like ml where movie_id=#{movieId} GROUP BY DATE(ml.like_time)
     </select>
 
-    <resultMap id="MovieLike" type="com.example.cinema.vo.DateLikeForm">
+    <resultMap id="MovieLike" type="com.example.cinema.po.DateLike">
         <result column="time" property="likeTime"></result>
         <result column="like_num" property="likeNum"></result>
     </resultMap>

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

@@ -0,0 +1,18 @@
+package com.example.cinema.data;
+
+import com.example.cinema.po.VIPCard;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+@Mapper
+public interface VIPCardMapper {
+
+    int insertOneCard(VIPCard vipCard);
+
+    VIPCard selectCardById(int id);
+
+
+
+}

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

@@ -0,0 +1,33 @@
+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;
+    }
+}

+ 2 - 3
src/main/java/com/example/cinema/vo/DateLikeForm.java → src/main/java/com/example/cinema/po/DateLike.java

@@ -1,13 +1,12 @@
-package com.example.cinema.vo;
+package com.example.cinema.po;
 
-import lombok.Data;
 
 import java.sql.Date;
 
 /**
  * Created by liying on 2019/3/23.
  */
-public class DateLikeForm {
+public class DateLike {
     /**
      * 喜爱人数
      */

+ 73 - 0
src/main/java/com/example/cinema/po/VIPCard.java

@@ -0,0 +1,73 @@
+package com.example.cinema.po;
+
+
+import com.example.cinema.strategy.ChargeStrategy;
+
+import java.sql.Timestamp;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+
+public class VIPCard {
+
+    public static final double price = 25;
+
+    /**
+     * 用户id
+     */
+    private int userId;
+
+    /**
+     * 会员卡id
+     */
+    private int id;
+
+    /**
+     * 会员卡余额
+     */
+    private double balance;
+
+    /**
+     * 办卡日期
+     */
+    private Timestamp joinDate;
+
+
+    public VIPCard() {
+
+    }
+
+
+    public int getUserId() {
+        return userId;
+    }
+
+    public void setUserId(int userId) {
+        this.userId = userId;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public double getBalance() {
+        return balance;
+    }
+
+    public void setBalance(double balance) {
+        this.balance = balance;
+    }
+
+    public Timestamp getJoinDate() {
+        return joinDate;
+    }
+
+    public void setJoinDate(Timestamp joinDate) {
+        this.joinDate = joinDate;
+    }
+}

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

@@ -0,0 +1,19 @@
+package com.example.cinema.strategy;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+public interface ChargeStrategy {
+    /**
+     * 充值策略描述
+     * @return
+     */
+    String getDescription();
+
+    /**
+     * 计算充值金额
+     * @param amount
+     * @return
+     */
+    double calculate(double amount);
+}

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

@@ -0,0 +1,20 @@
+package com.example.cinema.strategy;
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+public class ReturnStrategy implements ChargeStrategy {
+    private String description="满200送30";
+
+
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+    @Override
+    public double calculate(double amount) {
+        return (int)(amount/200)*30+amount;
+
+    }
+}

+ 37 - 0
src/test/java/com/example/cinema/bl/VIPServiceImplTest.java

@@ -0,0 +1,37 @@
+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.ResponseVO;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+
+/**
+ * Created by liying on 2019/4/14.
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class VIPServiceImplTest {
+    @Autowired
+    VIPService vipService;
+
+    @Test
+    public void addVIPCard() throws Exception {
+        VIPCardForm vipCardForm=new VIPCardForm();
+        vipCardForm.setUserId(13);
+        vipCardForm.setAmount(300);
+        vipService.addVIPCard(vipCardForm,new ReturnStrategy());
+    }
+
+    @Test
+    public void getCardById() throws Exception {
+        ResponseVO vo=vipService.getCardById(2);
+        vo.getContent();
+    }
+
+}