ScheduleServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.example.cinema.blImpl;
  2. import com.example.cinema.bl.ScheduleService;
  3. import com.example.cinema.data.HallMapper;
  4. import com.example.cinema.data.MovieMapper;
  5. import com.example.cinema.data.ScheduleMapper;
  6. import com.example.cinema.vo.*;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.*;
  12. /**
  13. * @author fjj
  14. * @date 2019/4/11 4:14 PM
  15. */
  16. @Service
  17. public class ScheduleServiceImpl implements ScheduleService {
  18. private static final String TIME_CONFLICT_ERROR_MESSAGE = "时间段冲突";
  19. private static final String CROSS_DAYS_ERROR_MESSAGE = "起止时间不能跨天";
  20. private static final String DATE_INTERVAL_LESS_THAN_LENGTH_ERROR_MESSAGE = "起止时间段不能少于电影时长或结束时间不能早于开始时间";
  21. private static final String BEFORE_START_DATE_ERROR_MESSAGE = "排片时间不能早于电影上映时间";
  22. private static final String MOVIE_NOT_EXIST_ERROR_MESSAGE = "电影不存在";
  23. private static final String HALL_NOT_EXIST_ERROR_MESSAGE = "影厅不存在";
  24. private static final String VIEW_COUNT_ERROR_MESSAGE = "排片可见限制数错误";
  25. private static final String ID_LIST_NULL_ERROR_MESSAGE = "id列表不可为空";
  26. private static final String VIEW_CONFLICT_ERROR_MESSAGE = "有排片信息已对观众可见,无法删除或修改";
  27. @Autowired
  28. private ScheduleMapper scheduleMapper;
  29. @Autowired
  30. private MovieMapper movieMapper;
  31. @Autowired
  32. private HallMapper hallMapper;
  33. @Override
  34. public ResponseVO addSchedule(ScheduleForm scheduleForm) {
  35. try {
  36. ResponseVO responseVO = preCheck(scheduleForm);
  37. if(!responseVO.getSuccess()){
  38. return responseVO;
  39. }
  40. scheduleMapper.insertOneSchedule(scheduleForm);
  41. return ResponseVO.buildSuccess();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. return ResponseVO.buildFailure("失败");
  45. }
  46. }
  47. @Override
  48. public ResponseVO updateSchedule(ScheduleForm scheduleForm) {
  49. try {
  50. ResponseVO responseVO = preCheck(scheduleForm);
  51. if(!responseVO.getSuccess()){
  52. return responseVO;
  53. }
  54. //在修改时要检查想要修改的排片信息是否已被观众可见,若可见则无法修改
  55. if(isAudienceCanView(Arrays.asList(scheduleForm.getId()))){
  56. return ResponseVO.buildFailure(VIEW_CONFLICT_ERROR_MESSAGE);
  57. }
  58. scheduleMapper.updateScheduleById(scheduleForm);
  59. return ResponseVO.buildSuccess();
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. return ResponseVO.buildFailure("失败");
  63. }
  64. }
  65. @Override
  66. public ResponseVO searchScheduleSevenDays(int hallId, Date startDate) {
  67. try {
  68. // 处理查询表单的起止时间
  69. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  70. startDate = simpleDateFormat.parse(simpleDateFormat.format(startDate));
  71. Date endDate = getNumDayAfterDate(startDate, 7);
  72. // 按照日期整理ScheduleItem
  73. List<ScheduleItem> scheduleItemList = scheduleMapper.selectSchedule(hallId, startDate, endDate);
  74. List<ScheduleVO> scheduleVOList = new ArrayList<>();
  75. for(int i = 0; i < 7; i++){
  76. Date date = getNumDayAfterDate(startDate, i);
  77. ScheduleVO scheduleVO = new ScheduleVO();
  78. scheduleVO.setDate(date);
  79. List<ScheduleItem> scheduleItems = new ArrayList<>();
  80. for(ScheduleItem scheduleItem : scheduleItemList){
  81. Date startTime = simpleDateFormat.parse(simpleDateFormat.format(scheduleItem.getStartTime()));
  82. if(date.equals(startTime)){
  83. scheduleItems.add(scheduleItem);
  84. }
  85. }
  86. scheduleVO.setScheduleItemList(scheduleItems);
  87. scheduleVOList.add(scheduleVO);
  88. }
  89. return ResponseVO.buildSuccess(scheduleVOList);
  90. } catch (ParseException e) {
  91. e.printStackTrace();
  92. return ResponseVO.buildFailure("失败");
  93. }
  94. }
  95. @Override
  96. public ResponseVO setScheduleView(ScheduleViewForm scheduleViewForm) {
  97. try{
  98. if(scheduleViewForm.getDay() < 0){
  99. return ResponseVO.buildFailure(VIEW_COUNT_ERROR_MESSAGE);
  100. }
  101. int num = scheduleMapper.selectViewCount();
  102. if(num == 0){
  103. scheduleMapper.insertOneView(scheduleViewForm);
  104. }
  105. else if(num == 1){
  106. scheduleMapper.updateOneView(scheduleViewForm);
  107. }
  108. else {
  109. return ResponseVO.buildFailure(VIEW_COUNT_ERROR_MESSAGE);
  110. }
  111. return ResponseVO.buildSuccess();
  112. }catch (Exception e){
  113. e.printStackTrace();
  114. return ResponseVO.buildFailure("失败");
  115. }
  116. }
  117. @Override
  118. public ResponseVO deleteBatchOfSchedule(ScheduleBatchDeleteForm scheduleBatchDeleteForm) {
  119. try{
  120. List<Integer> scheduleIdList = scheduleBatchDeleteForm.getScheduleIdList();
  121. if(scheduleIdList.size() == 0){
  122. return ResponseVO.buildFailure(ID_LIST_NULL_ERROR_MESSAGE);
  123. }
  124. if(isAudienceCanView(scheduleIdList)){
  125. return ResponseVO.buildFailure(VIEW_CONFLICT_ERROR_MESSAGE);
  126. }
  127. scheduleMapper.deleteScheduleBatch(scheduleIdList);
  128. return ResponseVO.buildSuccess();
  129. }catch (Exception e){
  130. e.printStackTrace();
  131. return ResponseVO.buildFailure("失败");
  132. }
  133. }
  134. /**
  135. * 获得num天后的日期
  136. * @param oldDate
  137. * @param num
  138. * @return
  139. * @throws ParseException
  140. */
  141. Date getNumDayAfterDate(Date oldDate, int num) throws ParseException {
  142. Calendar calendarTime = Calendar.getInstance();
  143. calendarTime.setTime(oldDate);
  144. calendarTime.add(Calendar.DAY_OF_YEAR, num);
  145. return calendarTime.getTime();
  146. }
  147. /**
  148. * 新增或修改排片信息的公共前置检查
  149. * @param scheduleForm
  150. * @return
  151. */
  152. ResponseVO preCheck(ScheduleForm scheduleForm){
  153. try {
  154. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  155. // 处理排片跨天错误
  156. if(!simpleDateFormat.format(scheduleForm.getStartTime()).equals(simpleDateFormat.format(scheduleForm.getEndTime()))){
  157. return ResponseVO.buildFailure(CROSS_DAYS_ERROR_MESSAGE);
  158. }
  159. //检查影厅是否存在
  160. if(hallMapper.selectHallById(scheduleForm.getHallId()) == null){
  161. return ResponseVO.buildFailure(HALL_NOT_EXIST_ERROR_MESSAGE);
  162. }
  163. // 检查电影是否存在
  164. MovieVO movieVO = movieMapper.selectMovieById(scheduleForm.getMovieId());
  165. if(movieVO == null){
  166. return ResponseVO.buildFailure(MOVIE_NOT_EXIST_ERROR_MESSAGE);
  167. }
  168. // 检查电影的上映时间是否和排片时间匹配
  169. if(scheduleForm.getStartTime().before(movieVO.getStartDate())){
  170. return ResponseVO.buildFailure(BEFORE_START_DATE_ERROR_MESSAGE);
  171. }
  172. // 校验排片时间段合法性
  173. int minutes = movieVO.getLength();
  174. Calendar calendarStartTime = Calendar.getInstance();
  175. calendarStartTime.setTime(scheduleForm.getStartTime());
  176. calendarStartTime.add(Calendar.MINUTE, minutes);
  177. Date endTime = calendarStartTime.getTime();
  178. if(scheduleForm.getEndTime().before(endTime)){
  179. return ResponseVO.buildFailure(DATE_INTERVAL_LESS_THAN_LENGTH_ERROR_MESSAGE);
  180. }
  181. // 检查该排片时间段是否和其他排片信息冲突
  182. int id = scheduleForm.getId() == null? 0 : scheduleForm.getId();
  183. if(0 != scheduleMapper.selectScheduleConflictByHallIdAndTime(scheduleForm.getHallId(), scheduleForm.getStartTime(), scheduleForm.getEndTime(),id).size()){
  184. return ResponseVO.buildFailure(TIME_CONFLICT_ERROR_MESSAGE);
  185. }
  186. }catch (Exception e){
  187. e.printStackTrace();
  188. }
  189. return ResponseVO.buildSuccess();
  190. }
  191. boolean isAudienceCanView(List<Integer> scheduleIdList) throws ParseException {
  192. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  193. Date today = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
  194. Date endDate = getNumDayAfterDate(today, scheduleMapper.selectView());
  195. List<ScheduleItem> scheduleList = scheduleMapper.selectScheduleBatch(scheduleIdList);
  196. for(ScheduleItem s : scheduleList){
  197. if(s.getEndTime().before(endDate) && s.getEndTime().after(today)){
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. }