package org.catframework.agileworking.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.catframework.agileworking.domain.MeetingRoom;
import org.catframework.agileworking.domain.MeetingRoomRepository;
import org.catframework.agileworking.domain.Schedule;
import org.catframework.agileworking.domain.ScheduleRepository;
import org.catframework.agileworking.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ScheduleServiceImpl implements ScheduleService {

	private static final Log logger = LogFactory.getLog(ScheduleServiceImpl.class);
	
	@Autowired
	private ScheduleRepository scheduleRepository;

	@Autowired
	private MeetingRoomRepository meetingRoomRepository;

	@Override
	public List<Schedule> find(Long meetingRoomId, Date date,Date to) {
		MeetingRoom meetingRoom = meetingRoomRepository.findOne(meetingRoomId);
		List<Schedule> schedules = scheduleRepository.findByMeetingRoomAndDate(meetingRoom, date);
		List<Schedule> weeklySchedules = scheduleRepository.findByRepeatMode(Schedule.REPEAT_MODE_WEEKLY);
		weeklySchedules.stream().forEach((s1) -> {
			if (schedules.stream().noneMatch((s2) -> {
				return s1.getId().equals(s2.getId());
			})) {
				if (s1.getDate().compareTo(date) < 0) {
					Calendar c1 = Calendar.getInstance();
					c1.setTime(s1.getDate());
					Calendar c2 = Calendar.getInstance();
					c2.setTime(date);
					logger.info(c1.getTime());
					logger.info(c2.getTime());
					if (c1.get(Calendar.DAY_OF_WEEK) == c2.get(Calendar.DAY_OF_WEEK)) {
						s1.setDate(date);
						schedules.add(s1);
					}
				}
			}
		});
		return schedules;
	}

	public void setScheduleRepository(ScheduleRepository scheduleRepository) {
		this.scheduleRepository = scheduleRepository;
	}

	public void setMeetingRoomRepository(MeetingRoomRepository meetingRoomRepository) {
		this.meetingRoomRepository = meetingRoomRepository;
	}
}

@@ -123,93 +123,44 @@
 st;%0A
-%0A
 import 
-org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFa
+java.util.stream.Colle
 ctor
-y;
+s;%0A
 %0Aimp
@@ -651,91 +651,8 @@
  %7B%0A%0A
-%09private static final Log logger = LogFactory.getLog(ScheduleServiceImpl.class);%0A%09%0A
 %09@Au
@@ -844,16 +844,17 @@
 te date,
+ 
 Date to)
@@ -1454,72 +1454,8 @@
 e);%0A
-%09%09%09%09%09logger.info(c1.getTime());%0A%09%09%09%09%09logger.info(c2.getTime());%0A
 %09%09%09%09
@@ -1602,24 +1602,162 @@
 ;%0A%09%09
-return schedules
+// %E6%8C%89%E7%85%A7%E5%BC%80%E5%A7%8B%E6%97%B6%E9%97%B4%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F%0A%09%09return schedules.stream().sorted((s1, s2) -%3E %7B%0A%09%09%09return s1.getStartTime().compareTo(s2.getStartTime());%0A%09%09%7D).collect(Collectors.toList())
 ;%0A%09%7D

优化增加了排序，按会议开始日期排序