package com.example.cinema.blImpl.sales;
import com.example.cinema.bl.sales.TicketService;
import com.example.cinema.blImpl.management.hall.HallServiceForBl;
import com.example.cinema.blImpl.management.schedule.MovieServiceForBl;
import com.example.cinema.blImpl.management.schedule.ScheduleServiceForBl;
import com.example.cinema.blImpl.promotion.ActivityServiceForBl;
import com.example.cinema.blImpl.promotion.CouponServiceForBl;
import com.example.cinema.blImpl.promotion.VIPServiceForBl;
import com.example.cinema.data.management.MovieMapper;
import com.example.cinema.data.promotion.CouponMapper;
import com.example.cinema.data.promotion.VIPCardMapper;
import com.example.cinema.data.sales.TicketMapper;
import com.example.cinema.po.*;
import com.example.cinema.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
* Created by liying on 2019/4/16.
* Updated by zhihao li on 2019/5/18.
* Updated by jiayi chen on 2019/5/19.
*/
@Service
public class TicketServiceImpl implements TicketService, TicketServiceForBL {
@Autowired
TicketMapper ticketMapper;
@Autowired
MovieServiceForBl movieService;
@Autowired
CouponServiceForBl couponServiceForBl;
@Autowired
VIPCardMapper vipCardMapper;
@Autowired
ScheduleServiceForBl scheduleService;
@Autowired
HallServiceForBl hallService;
@Autowired
CouponServiceForBl couponService;
@Autowired
ActivityServiceForBl activityService;
@Autowired
VIPServiceForBl vipService;
@Autowired
ConsumptionServiceForBL consumptionService;
@Override
@Transactional
public ResponseVO addTicket(TicketForm ticketForm) {
try {
TicketWithCouponVO ticketVO = new TicketWithCouponVO();
List<TicketVO> ticketList = new ArrayList<>();
double total = 0.0;
List<CouponVO> couponList;
List<ActivityVO> activityList;
int scheduleId = ticketForm.getScheduleId();
double price = scheduleService.getScheduleItemById(scheduleId).getFare();
int userId = ticketForm.getUserId();
for (SeatForm seat : ticketForm.getSeats()) {
Ticket ticket = new Ticket();
ticket.setUserId(userId);
ticket.setScheduleId(scheduleId);
ticket.setColumnIndex(seat.getColumnIndex());
ticket.setRowIndex(seat.getRowIndex());
ticket.setState(0);
ticket.setCouponId(0);
ticketMapper.insertTicket(ticket);
ticketList.add(ticket.getVO());
total += price;
}
couponList = couponService.getCouponByUserAndAmount(userId, total);
activityList = activityService.selectActivitiesByMovie(scheduleService.getScheduleItemById(scheduleId).getMovieId());
VIPCard vipCard = vipService.getVIPCardByUserId(userId);
double discount;
try {
String cardType = vipCard.getType();
discount = vipCardMapper.getDiscountByCardType(cardType);
} catch (Exception e) {
discount = 1;
}
ticketVO.setTotal(total * discount);
ticketVO.setTicketVOList(ticketList);
ticketVO.setCoupons(couponList);
ticketVO.setActivities(activityList);
return ResponseVO.buildSuccess(ticketVO);
} catch (Exception e) {
e.printStackTrace();
return ResponseVO.buildFailure("失败");
}
}
@Override
@Transactional
public ResponseVO completeTicket(OrderForm orderForm) {
try {
List<Integer> id = orderForm.getId();
double total = orderForm.getTotal();
int couponId = orderForm.getCouponId();
int userId = orderForm.getUserId();
for (int i : id) ticketMapper.updateTicketState(i, 1);
int movieId = scheduleService.getScheduleItemById(ticketMapper.selectTicketById(id.get(0)).getScheduleId()).getMovieId();
List<ActivityVO> activities = activityService.selectActivitiesByMovie(movieId);
for (ActivityVO activity : activities) {
Coupon coupon = activity.getCoupon();
couponService.insertCouponUser(coupon.getId(), userId);
}
double discountAmount = 0.0;
if (couponId != 0)
discountAmount = couponService.getCouponById(couponId).getDiscountAmount();
if (couponId != 0)
couponService.deleteCouponUser(couponId, userId);
//插入消费记录
consumptionService.insertConsumptionItem(userId, total, discountAmount, 1, id, couponId);
if (couponId != 0) {
for (int i = 0; i < id.size(); i++) {
ticketMapper.addCouponId(id.get(i), couponId);
}
couponService.deleteCouponUser(couponId, userId);
}
return ResponseVO.buildSuccess();
} catch (Exception e) {
e.printStackTrace();
return ResponseVO.buildFailure("失败");
}
}
@Override
public ResponseVO getByScheduleAndUserId(int scheduleId, int userId) {
try {
List<Ticket> tickets = ticketMapper.selectTicketsBySchedule(scheduleId);
ScheduleItem schedule = scheduleService.getScheduleItemById(scheduleId);
Hall hall = hallService.getHallById(schedule.getHallId());
int[][] seats = hall.getSeatsLayout();
tickets.stream().forEach(ticket -> {
//2表明座位被占,3代表座位被自己占
if(ticket.getUserId()==userId&&ticket.getState()==0){
//表示被自己选中,未完成的座位,顺便删除,因为会再点一遍
ticketMapper.deleteTicket(ticket.getId());
seats[ticket.getRowIndex()][ticket.getColumnIndex()] = 3;//表示被自己选中,未完成的座位
}
else{
seats[ticket.getRowIndex()][ticket.getColumnIndex()] = 2;//被其他人占据
}
});
ScheduleWithSeatVO scheduleWithSeatVO = new ScheduleWithSeatVO();
scheduleWithSeatVO.setScheduleItem(schedule);
scheduleWithSeatVO.setSeats(seats);
return ResponseVO.buildSuccess(scheduleWithSeatVO);
} catch (Exception e) {
e.printStackTrace();
return ResponseVO.buildFailure("失败");
}
}
@Override
public ResponseVO getTicketByUser(int userId) {
try {
List<Ticket> tickets = ticketMapper.selectTicketByUser(userId);
for (int i = 0; i < tickets.size(); i++) {
Movie movie = movieService.getMovieById(scheduleService.getScheduleItemById(tickets.get(i).getScheduleId()).getMovieId());
tickets.get(i).setMovie(movie);
}
List<TicketWithScheduleVO> ticketVOList = new ArrayList<>();
List<TicketsInOnePaymentVO> paymentsVOList = new ArrayList<>();
ArrayList timeList = new ArrayList<>();
tickets.stream().forEach(ticket -> {
if (ticket.getState() == 1) {
TicketWithScheduleVO ticketWithScheduleVO = new TicketWithScheduleVO(ticket, scheduleService.getScheduleItemById(ticket.getScheduleId()));
ticketWithScheduleVO.setTime(ticket.getTime());
ticketWithScheduleVO.setMovie(ticket.getMovie());
ticketVOList.add(ticketWithScheduleVO);
}
});
for (int i = 0; i < ticketVOList.size(); i++) {
String currentSeat