/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.reception;
import java.util.Calendar;
import web.rechelper.Ticket;
import web.rechelper.WeekDay;
import web.rechelper.ReceptionHelper;
import domain.Client;
import domain.Collaborator;
import domain.Lpu;
import domain.editors.Editor;
import domain.shedule.WorkType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import utils.WeekIndex;
import web.generic.CH;
import web.generic.GenericCommandController;
import web.rechelper.Hour;
import web.rechelper.IncomingParamsValidator;
/**
* Контроллер более детального выбора времени приема
* Обязательные параметры:
* client - ID пациента на запись
* lpu - ID ЛПУ
* type - ID типа приема
* collaborator - ID врача
* @author axe
*/
public class ReceptionDayController extends GenericCommandController {
public ReceptionDayController() {
setCommandClass(ReceptionDayDTO.class);
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Client.class, new Editor(getDao(), Client.class));
binder.registerCustomEditor(WorkType.class, new Editor(getDao(), WorkType.class));
binder.registerCustomEditor(Lpu.class, new Editor(getDao(), Lpu.class));
binder.registerCustomEditor(Collaborator.class, new Editor(getDao(), Collaborator.class));
}
@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
Client client = CH.getOptional(getDao(), Client.class, request.getParameter("client"));
WorkType type = CH.getOptional(getDao(), WorkType.class, request.getParameter("type"));
Lpu lpu = CH.getOptional(getDao(), Lpu.class, request.getParameter("lpu"));
Collaborator collab = CH.getOptional(getDao(), Collaborator.class, request.getParameter("collaborator"));
if (client == null) {
return IncomingParamsValidator.redirectTo("/reception/index.htm", client, type, lpu, collab);
} else if (collab == null) {
return IncomingParamsValidator.redirectTo("/reception/collaborator.htm", client, type, lpu, collab);
} else if (lpu == null && type == null) {
return IncomingParamsValidator.redirectTo("/reception/work.htm", client, type, lpu, collab);
} else if (lpu == null) {
return IncomingParamsValidator.redirectTo("/reception/lpu.htm", client, type, lpu, collab);
} else if (type == null) {
return IncomingParamsValidator.redirectTo("/reception/work.htm", client, type, lpu, collab);
}
String weekStr = request.getParameter("week");
if (weekStr == null || weekStr.isEmpty()) {
return IncomingParamsValidator.redirectTo("/reception/reception.htm", client, type, lpu, collab);
}
String dayStr = request.getParameter("day");
if (dayStr == null || dayStr.isEmpty()) {
return IncomingParamsValidator.redirectTo("/reception/reception.htm", client, type, lpu, collab);
}
ReceptionDayDTO dto = (ReceptionDayDTO) command;
HashMap model = new HashMap();
WeekIndex weekIndex = new WeekIndex(new WeekIndex(new Date()).getIndex() + dto.getWeek());
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);
}
}