ScheduleServiceImpl.java 9.0 KB

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