Browse Source

[修改]修改批量锁座接口以及批量购买接口

liying 7 years ago
parent
commit
b0a1ca7747

+ 5 - 3
src/main/java/com/example/cinema/bl/TicketService.java

@@ -4,6 +4,8 @@ import com.example.cinema.po.Ticket;
 import com.example.cinema.vo.ResponseVO;
 import com.example.cinema.vo.TicketForm;
 
+import java.util.List;
+
 
 /**
  * Created by liying on 2019/4/16.
@@ -11,15 +13,15 @@ import com.example.cinema.vo.TicketForm;
 public interface TicketService {
     ResponseVO addTicket(TicketForm ticketForm);
 
-    ResponseVO completeTicket(int id,int couponId);
+    ResponseVO completeTicket(List<Integer> id, int couponId);
 
     ResponseVO getBySchedule(int scheduleId);
 
     ResponseVO getTicketByUser(int userId);
 
-    ResponseVO completeByVIPCard(int ticketId,int couponId);
+    ResponseVO completeByVIPCard(List<Integer> id,int couponId);
 
-    ResponseVO cancelTicket(int ticketId);
+    ResponseVO cancelTicket(List<Integer> id);
 
 
 }

+ 45 - 29
src/main/java/com/example/cinema/blImpl/TicketServiceImpl.java

@@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -33,36 +35,41 @@ public class TicketServiceImpl implements TicketService {
     CouponMapper couponMapper;
 
     @Override
+    @Transactional
     public ResponseVO addTicket(TicketForm ticketForm) {
-
-        Ticket ticket = new Ticket();
-        ticket.setUserId(ticketForm.getUserId());
-        ticket.setScheduleId(ticketForm.getScheduleId());
-        ticket.setColumnIndex(ticketForm.getColumnIndex());
-        ticket.setRowIndex(ticketForm.getRowIndex());
-        ticket.setState(0);
-        try {
-            if(ticketMapper.selectTicketByScheduleIdAndSeat(ticketForm.getScheduleId(),ticketForm.getColumnIndex(),ticketForm.getRowIndex())!=null){
+        List<SeatForm> seats = ticketForm.getSeats();
+        List<Ticket> tickets=new ArrayList<>();
+        for (SeatForm seat : seats) {
+            Ticket ticket = new Ticket();
+            ticket.setUserId(ticketForm.getUserId());
+            ticket.setScheduleId(ticketForm.getScheduleId());
+            ticket.setColumnIndex(seat.getColumnIndex());
+            ticket.setRowIndex(seat.getRowIndex());
+            ticket.setState(0);
+            if (ticketMapper.selectTicketByScheduleIdAndSeat(ticketForm.getScheduleId(), seat.getColumnIndex(), seat.getRowIndex()) != null) {
                 return ResponseVO.buildFailure("该位置已被占");
             }
-            ticketMapper.insertTicket(ticket);
-            return ResponseVO.buildSuccess(ticket.getVO());
+            tickets.add(ticket);
+        }
+        try {
+            ticketMapper.insertTickets(tickets);
+            return ResponseVO.buildSuccess(tickets.stream().map(ticket -> ticket.getVO()).collect(Collectors.toList()));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");
         }
+
     }
 
     @Override
     @Transactional
-    public ResponseVO completeTicket(int id,int couponId) {
+    public ResponseVO completeTicket(List<Integer> id, int couponId) {
         try {
-            ticketMapper.updateTicketState(id, 1);
-            Ticket ticket=ticketMapper.selectTicketById(id);
-            if(couponId!=0) {
-                couponMapper.deleteCouponUser(couponId, ticket.getUserId());
+            List<Ticket> tickets=updateState(id,1);
+            if (couponId != 0) {
+                couponMapper.deleteCouponUser(couponId, tickets.get(0).getUserId());
             }
-            return ResponseVO.buildSuccess(ticket.getVO());
+            return ResponseVO.buildSuccess(tickets.stream().map(ticket -> ticket.getVO()).collect(Collectors.toList()));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");
@@ -100,24 +107,22 @@ public class TicketServiceImpl implements TicketService {
 
     @Override
     @Transactional
-    public ResponseVO completeByVIPCard(int ticketId,int couponId) {
+    public ResponseVO completeByVIPCard(List<Integer> id, int couponId) {
         try {
-            Ticket ticket=ticketMapper.selectTicketById(ticketId);
+            Ticket ticket = ticketMapper.selectTicketById(id.get(0));
             VIPCard vipCard = vipCardMapper.selectCardByUserId(ticket.getUserId());
             ScheduleItem scheduleItem = scheduleMapper.selectScheduleById(ticket.getScheduleId());
             //todo 优惠券门槛校验
-            double balance = vipCard.getBalance() - scheduleItem.getFare();
-            if(couponId!=0) {
-                Coupon coupon=couponMapper.selectById(couponId);
-                balance+=coupon.getDiscountAmount();
+            double balance = vipCard.getBalance() - scheduleItem.getFare()*id.size();
+            if (couponId != 0) {
+                Coupon coupon = couponMapper.selectById(couponId);
+                balance += coupon.getDiscountAmount();
             }
             if (balance < 0) {
                 return ResponseVO.buildFailure("会员卡余额不足");
             }
             vipCardMapper.updateCardBalance(vipCard.getId(), balance);
-            completeTicket(ticketId,couponId);
-            ticket.setState(1);
-            return ResponseVO.buildSuccess(ticket.getVO());
+            return completeTicket(id, couponId);
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");
@@ -125,15 +130,26 @@ public class TicketServiceImpl implements TicketService {
     }
 
     @Override
-    public ResponseVO cancelTicket(int ticketId) {
+    public ResponseVO cancelTicket(List<Integer> id) {
         try {
-            ticketMapper.updateTicketState(ticketId, 2);
-            return ResponseVO.buildSuccess(ticketMapper.selectTicketById(ticketId).getVO());
+            List<Ticket> tickets=updateState(id,2);
+
+            return ResponseVO.buildSuccess(tickets.stream().map(ticket -> ticket.getVO()).collect(Collectors.toList()));
         } catch (Exception e) {
             e.printStackTrace();
             return ResponseVO.buildFailure("失败");
         }
     }
 
+    private  List<Ticket>  updateState(List<Integer> id,int state){
+        List<Ticket> tickets=new ArrayList<>();
+        for (Integer i:id) {
+            ticketMapper.updateTicketState(i, state);
+            Ticket ticket = ticketMapper.selectTicketById(id.get(0));
+            tickets.add(ticket);
+        }
+        return tickets;
+
+    }
 
 }

+ 5 - 3
src/main/java/com/example/cinema/controller/TicketController.java

@@ -7,6 +7,8 @@ import com.example.cinema.vo.TicketForm;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 /**
  * Created by liying on 2019/4/16.
  */
@@ -17,7 +19,7 @@ public class TicketController {
     TicketService ticketService;
 
     @PostMapping("/vip/buy")
-    public ResponseVO buyTicketByVIPCard(@RequestParam int ticketId,@RequestParam int couponId){
+    public ResponseVO buyTicketByVIPCard(@RequestParam List<Integer> ticketId, @RequestParam int couponId){
         return ticketService.completeByVIPCard(ticketId,couponId);
     }
 
@@ -26,7 +28,7 @@ public class TicketController {
         return ticketService.addTicket(ticketForm);
     }
     @PostMapping("/buy")
-    public ResponseVO buyTicket(@RequestParam int ticketId,@RequestParam int couponId){
+    public ResponseVO buyTicket(@RequestParam List<Integer> ticketId,@RequestParam int couponId){
         return ticketService.completeTicket(ticketId,couponId);
     }
     @GetMapping("/get/{userId}")
@@ -39,7 +41,7 @@ public class TicketController {
         return ticketService.getBySchedule(scheduleId);
     }
     @PostMapping("/cancel")
-    public ResponseVO cancelTicket(@RequestParam int ticketId){
+    public ResponseVO cancelTicket(@RequestParam List<Integer> ticketId){
         return ticketService.cancelTicket(ticketId);
     }
 

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

@@ -14,6 +14,8 @@ public interface TicketMapper {
 
     int insertTicket(Ticket ticket);
 
+    int insertTickets(List<Ticket> tickets);
+
     void deleteTicket(int ticketId);
 
     void updateTicketState(@Param("ticketId") int ticketId, @Param("state") int state);

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

@@ -6,6 +6,16 @@
         insert into ticket(user_id,schedule_id,column_index,row_index,state)
         values(#{userId}, #{scheduleId},#{columnIndex},#{rowIndex},#{state})
     </insert>
+    <insert id="insertTickets" parameterType="java.util.List"
+            useGeneratedKeys="true" keyProperty="id">
+        insert into ticket(user_id,schedule_id,column_index,row_index,state)
+        values
+        <foreach collection="list" item="item" index="index"
+                 separator=",">
+            (#{item.userId}, #{item.scheduleId},#{item.columnIndex},#{item.rowIndex},#{item.state})
+        </foreach>
+
+    </insert>
 
     <select id="selectTicketById" resultMap="Ticket">
         select * from ticket where id=#{id}
@@ -24,7 +34,7 @@
     </select>
 
 
-    <update id="updateTicketState" >
+    <update id="updateTicketState">
         update ticket set state = #{state} where id=#{ticketId}
     </update>
 

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

@@ -0,0 +1,31 @@
+package com.example.cinema.vo;
+
+/**
+ * Created by liying on 2019/4/18.
+ */
+public class SeatForm {
+    /**
+     * 列号
+     */
+    private int columnIndex;
+    /**
+     * 排号
+     */
+    private int rowIndex;
+
+    public int getColumnIndex() {
+        return columnIndex;
+    }
+
+    public void setColumnIndex(int columnIndex) {
+        this.columnIndex = columnIndex;
+    }
+
+    public int getRowIndex() {
+        return rowIndex;
+    }
+
+    public void setRowIndex(int rowIndex) {
+        this.rowIndex = rowIndex;
+    }
+}

+ 10 - 22
src/main/java/com/example/cinema/vo/TicketForm.java

@@ -1,5 +1,7 @@
 package com.example.cinema.vo;
 
+import java.util.List;
+
 /**
  * Created by liying on 2019/4/16.
  */
@@ -13,16 +15,16 @@ public class TicketForm {
      * 排片id
      */
     private int scheduleId;
-    /**
-     * 列号
-     */
-    private int columnIndex;
-    /**
-     * 排号
-     */
-    private int rowIndex;
 
+    private List<SeatForm> seats;
+
+    public List<SeatForm> getSeats() {
+        return seats;
+    }
 
+    public void setSeats(List<SeatForm> seats) {
+        this.seats = seats;
+    }
 
     public TicketForm() {
     }
@@ -43,20 +45,6 @@ public class TicketForm {
         this.scheduleId = scheduleId;
     }
 
-    public int getColumnIndex() {
-        return columnIndex;
-    }
-
-    public void setColumnIndex(int columnIndex) {
-        this.columnIndex = columnIndex;
-    }
 
-    public int getRowIndex() {
-        return rowIndex;
-    }
-
-    public void setRowIndex(int rowIndex) {
-        this.rowIndex = rowIndex;
-    }
 
 }