ReceptionHelper rh = new ReceptionHelper(getDao(), dto,
weekIndex.getBegin().getTime(), weekIndex.getEnd().getTime());
List<WeekDay> weekDays = rh.getModel();
int day = dto.getDay();
WeekDay weekDay = weekDays.get(day-1 < 0 ? 0 : (day-1) % 7);
//Создаем таблицу часы минуты
ArrayList<Hour> hours = new ArrayList<Hour>();
List<Ticket> tickets = weekDay.getTickets();
for (int i = 0; i < 23; i++) {
hours.add(new Hour(i));
}
if (tickets != null && tickets.size() > 0) {
//распределяем тикеты по часам
for (int i = 0; i < tickets.size(); i++) {
Ticket ticket = tickets.get(i);
ticket.setHeight(ticket.getDuration());
int mm = ticket.getMinute();
int hh = ticket.getHour();
int duration = ticket.getHeight();
while (duration > 0) {
if (mm + duration <= 60) {
ticket.setHeight(duration);
if (ticket.getHeight() >= 3) {
hours.get(hh).getTickets().add(ticket);
}
duration = 0;
} else {
ticket.setHeight(60-mm);
if (ticket.getHeight() >= 3) {
hours.get(hh).getTickets().add(ticket);
}
boolean buzy = ticket.isBuzy();
duration = duration - (60-mm);
ticket = new Ticket(ticket.getCalendar(), ticket.getDuration());
ticket.setBuzy(buzy);
//переход на след. час
hh += 1;
mm = 0;
}
}
}
//заполняем пустые места в часе
for (Hour hour : hours) {
ArrayList<Ticket> tList = hour.getTickets();
ArrayList<Ticket> newTickets = new ArrayList<Ticket>();
Collections.sort(tList);
int lasttime = 0;
for (Ticket ticket : tList) {
if (ticket.getHour() != hour.getHour()) {
Calendar cal = (Calendar) ticket.getCalendar().clone();
cal.add(Calendar.MINUTE, ticket.getDuration());
lasttime = cal.get(Calendar.MINUTE);
}
else if (ticket.getMinute() > lasttime) {
int height = ticket.getMinute()-lasttime;
Calendar cal = (Calendar) ticket.getCalendar().clone();
cal.add(Calendar.MINUTE, -height);
cal.set(Calendar.HOUR_OF_DAY, hour.getHour());
Ticket newTicket = new Ticket(cal, height);
newTicket.setHeight(height);
newTicket.setBuzy(true);
if (newTicket.getHeight() >= 3) {
newTickets.add(newTicket);
}
}
lasttime = ticket.getMinute()+ ticket.getDuration();
}
if (lasttime < 60) {
Calendar cal = weekDay.getDay().getCalendar();
cal.set(Calendar.HOUR_OF_DAY, hour.getHour());
cal.set(Calendar.MINUTE, lasttime);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Ticket newTicket = new Ticket(cal, 60 - lasttime);
newTicket.setHeight(60 - lasttime);
newTicket.setBuzy(true);
if (newTicket.getHeight() >= 3) {
newTickets.add(newTicket);
}
}
tList.addAll(newTickets);
Collections.sort(tList);
}
}
//Удаляем пустые часы за рамками интервала
for (Iterator<Hour> it = hours.iterator(); it.hasNext();) {
Hour hour = it.next();
if (hour.getHour() < 8 || hour.getHour() > 18) {
boolean empty = true;
for (Ticket ticket : hour.getTickets()) {
if (!ticket.isBuzy()) {
empty = false;
}
}
if (empty) {
it.remove();
}
}
}
//если часов больше 11 то удаляем пустые начиная с конца
for (int i = hours.size() - 1; i >=0; i--) {
Hour hour = hours.get(i);
boolean empty = true;
for (Ticket ticket : hour.getTickets()) {
if (!ticket.isBuzy()) {
empty = false;
}
}
if (empty && hours.size() > 11) {
hours.remove(i);
}
}
model.put("hours", hours);
model.put("day", weekDay.getDay());
model.put("pageBreadcrumb", ReceptionController.initBreadCrumb(dto.getClient(), dto.getType(), dto.getLpu(), dto.getCollaborator()));
model.put("command", command);
return new ModelAndView("reception/receptionday", model);
}