|
|
@@ -1,11 +1,9 @@
|
|
|
package com.example.cinema.blImpl;
|
|
|
|
|
|
-import com.example.cinema.bl.CouponService;
|
|
|
+import com.example.cinema.bl.HallService;
|
|
|
import com.example.cinema.bl.TicketService;
|
|
|
-import com.example.cinema.data.CouponMapper;
|
|
|
-import com.example.cinema.data.ScheduleMapper;
|
|
|
-import com.example.cinema.data.TicketMapper;
|
|
|
-import com.example.cinema.data.VIPCardMapper;
|
|
|
+import com.example.cinema.data.*;
|
|
|
+import com.example.cinema.po.Activity;
|
|
|
import com.example.cinema.po.Coupon;
|
|
|
import com.example.cinema.po.Ticket;
|
|
|
import com.example.cinema.po.VIPCard;
|
|
|
@@ -14,6 +12,7 @@ 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.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -31,38 +30,70 @@ public class TicketServiceImpl implements TicketService {
|
|
|
ScheduleMapper scheduleMapper;
|
|
|
@Autowired
|
|
|
CouponMapper couponMapper;
|
|
|
+ @Autowired
|
|
|
+ HallMapper hallmapper;
|
|
|
+ @Autowired
|
|
|
+ ActivityMapper activityMapper;
|
|
|
|
|
|
@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);
|
|
|
+ TicketWithCouponVO vo=new TicketWithCouponVO();
|
|
|
+ vo.setTicketVOList(tickets.stream().map(ticket -> ticket.getVO()).collect(Collectors.toList()));
|
|
|
+ ScheduleItem scheduleItem = scheduleMapper.selectScheduleById(tickets.get(0).getScheduleId());
|
|
|
+ double total=scheduleItem.getFare()*tickets.size();
|
|
|
+ vo.setTotal(total);
|
|
|
+ vo.setCoupons(couponMapper.selectCouponByUserAndAmount(tickets.get(0).getUserId(),total));
|
|
|
+ return ResponseVO.buildSuccess(vo);
|
|
|
} 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);
|
|
|
+ int userId=tickets.get(0).getUserId();
|
|
|
+ if (couponId != 0) {
|
|
|
+ couponMapper.deleteCouponUser(couponId, userId);
|
|
|
+ }
|
|
|
+ ScheduleItem schedule=scheduleMapper.selectScheduleById(tickets.get(0).getScheduleId());
|
|
|
+ // 按电影优惠
|
|
|
+ List<ActivityVO> activityVOS=activityMapper.selectActivities();
|
|
|
+ List<Integer> coupons=new ArrayList<>();
|
|
|
+ for (ActivityVO vo:activityVOS ) {
|
|
|
+ coupons.add(vo.getCoupon().getId());
|
|
|
+ }
|
|
|
+ // 按时间优惠
|
|
|
+ List<ActivityVO> timeActivityVOS=activityMapper.selectActivitiesWithoutMovie();
|
|
|
+ for (ActivityVO vo:timeActivityVOS ) {
|
|
|
+ coupons.add(vo.getCoupon().getId());
|
|
|
}
|
|
|
- return ResponseVO.buildSuccess(ticket.getVO());
|
|
|
+ coupons=coupons.stream().distinct().collect(Collectors.toList());
|
|
|
+ for (Integer coupon : coupons) {
|
|
|
+ couponMapper.insertCouponUser(coupon,userId);
|
|
|
+ }
|
|
|
+ return ResponseVO.buildSuccess(tickets.stream().map(ticket -> ticket.getVO()).collect(Collectors.toList()));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|
|
|
@@ -73,14 +104,16 @@ public class TicketServiceImpl implements TicketService {
|
|
|
public ResponseVO getBySchedule(int scheduleId) {
|
|
|
try {
|
|
|
List<Ticket> tickets = ticketMapper.selectTicketsBySchedule(scheduleId);
|
|
|
- List<TicketSeatVO> ticketSeatVOS = tickets.stream().map(ticket -> {
|
|
|
- TicketSeatVO vo = new TicketSeatVO();
|
|
|
- vo.setScheduleId(ticket.getScheduleId());
|
|
|
- vo.setColumnIndex(ticket.getColumnIndex());
|
|
|
- vo.setRowIndex(ticket.getRowIndex());
|
|
|
- return vo;
|
|
|
- }).collect(Collectors.toList());
|
|
|
- return ResponseVO.buildSuccess(ticketSeatVOS);
|
|
|
+ ScheduleItem schedule=scheduleMapper.selectScheduleById(scheduleId);
|
|
|
+ HallVO hall=hallmapper.selectHallById(schedule.getHallId());
|
|
|
+ int[][] seats=new int[hall.getRow()][hall.getColumn()];
|
|
|
+ tickets.stream().forEach(ticket -> {
|
|
|
+ seats[ticket.getRowIndex()][ticket.getColumnIndex()]=1;
|
|
|
+ });
|
|
|
+ ScheduleWithSeatVO scheduleWithSeatVO=new ScheduleWithSeatVO();
|
|
|
+ scheduleWithSeatVO.setScheduleItem(schedule);
|
|
|
+ scheduleWithSeatVO.setSeats(seats);
|
|
|
+ return ResponseVO.buildSuccess(scheduleWithSeatVO);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|
|
|
@@ -90,7 +123,11 @@ public class TicketServiceImpl implements TicketService {
|
|
|
@Override
|
|
|
public ResponseVO getTicketByUser(int userId) {
|
|
|
try {
|
|
|
- List<TicketVO> vos = ticketMapper.selectTicketByUser(userId).stream().map(ticket -> ticket.getVO()).collect(Collectors.toList());
|
|
|
+ List<TicketWithScheduleVO> vos = ticketMapper.selectTicketByUser(userId).stream().map(ticket -> {
|
|
|
+ TicketWithScheduleVO vo=ticket.getWithScheduleVO();
|
|
|
+ vo.setSchedule(scheduleMapper.selectScheduleById(ticket.getScheduleId()));
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
return ResponseVO.buildSuccess(vos);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -100,24 +137,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 +160,27 @@ 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;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|