public ActionResult Save(Models.ModelSchedule model, string mainid) { model.Id = mainid; //for security reason string guid = Guid.NewGuid().ToString(); SchMainService mainService = new SchMainService(guid); SchStylesService stylesService = new SchStylesService(guid); SchStylesRepository style = stylesService.GetSpecifiedStyleByMainId("td", model.Id); SchEventsService eventsService = new SchEventsService(guid); SchEventsRepository newEvent; SchWeekDayRespository newWeekDay; IEnumerable <SchEventsRepository> events; MvcApplicationIPS2.Service.SAM.UserService userService = new Service.SAM.UserService(); string pernr = UserService.Pernr; try { if (style != null) { style.STYLE = model.CellStyle; stylesService.Update(style); } else { style = new SchStylesRepository(); style.TYPE = "td"; style.MAIN_ID = model.Id; style.STYLE = model.CellStyle; style.CREATE_EMPNO = pernr; stylesService.Create(style); } if (model.WeekDays != null) { //暫時不分月份 foreach (SchWeekDayRespository weekday in mainService.SchWeekDayService.GetWeekDaysByMainId(model.Id, model.CurrentMonth)) { foreach (Models.ModelWeekDays wd in model.WeekDays) { if (weekday.YYYYMM == wd.YYYYMM && weekday.WEEKDAY == wd.WeekDay) { mainService.SchWeekDayService.Delete(weekday); } } } foreach (Models.ModelWeekDays wd in model.WeekDays) { if (string.IsNullOrEmpty(wd.Description) || string.IsNullOrEmpty(wd.YYYYMM)) { continue; } newWeekDay = new SchWeekDayRespository(); newWeekDay.DESCRIPTION = wd.Description; newWeekDay.MAIN_ID = model.Id; newWeekDay.WEEKDAY = wd.WeekDay; newWeekDay.YYYYMM = wd.YYYYMM; mainService.SchWeekDayService.Create(newWeekDay); } } //delete 與 create需要分開做 因為不同的event可能是相同的日期 若同在一個loop做 可能會把該次新增的刪除掉 if (model.Events != null) { foreach (Models.ModelEvents e in model.Events) { events = eventsService.GetEventsByMainIdInDate(model.Id, e.YYYYMMDD); foreach (SchEventsRepository e2 in events) { eventsService.Delete(e2); } } foreach (Models.ModelEvents e in model.Events) { if (string.IsNullOrEmpty(e.YYYYMMDD)) { continue; } if (string.IsNullOrEmpty(e.Title) && string.IsNullOrEmpty(e.Style)) { continue; } newEvent = new SchEventsRepository(); newEvent.CREATED_BY = pernr; newEvent.YYYYMMDD = e.YYYYMMDD; newEvent.HHMM = e.HHMM; newEvent.TITLE = e.Title; newEvent.MAIN_ID = model.Id; newEvent.DETAIL = string.Empty; newEvent.STYLE = e.Style; eventsService.Create(newEvent); } } model.CurrentMonth = string.IsNullOrEmpty(model.CurrentMonth) ? DateTime.Now.ToString("yyyyMM") : model.CurrentMonth; eventsService.SaveChange(); model = getModel(model.Id, model.CurrentMonth); } catch (Exception ex) { mainService.RollBack(); throw ex; } model.Authority = Models.enumAuthority.Adm; string jsonObj = Newtonsoft.Json.JsonConvert.SerializeObject(model); return(Content(jsonObj)); }
public ActionResult Save(Models.ModelSchedule model, string mainid) { model.Id = mainid; //for security reason string guid = Guid.NewGuid().ToString(); SchMainService mainService = new SchMainService(guid); SchStylesService stylesService = new SchStylesService(guid); SchStylesRepository style = stylesService.GetSpecifiedStyleByMainId("td", model.Id); SchEventsService eventsService = new SchEventsService(guid); SchEventsRepository newEvent; SchWeekDayRespository newWeekDay; IEnumerable<SchEventsRepository> events; MvcApplicationIPS2.Service.SAM.UserService userService = new Service.SAM.UserService(); string pernr = UserService.Pernr; try { if (style != null) { style.STYLE = model.CellStyle; stylesService.Update(style); } else { style = new SchStylesRepository(); style.TYPE = "td"; style.MAIN_ID = model.Id; style.STYLE = model.CellStyle; style.CREATE_EMPNO = pernr; stylesService.Create(style); } if (model.WeekDays != null) { //暫時不分月份 foreach (SchWeekDayRespository weekday in mainService.SchWeekDayService.GetWeekDaysByMainId(model.Id, model.CurrentMonth)) { foreach (Models.ModelWeekDays wd in model.WeekDays) { if (weekday.YYYYMM == wd.YYYYMM && weekday.WEEKDAY == wd.WeekDay) mainService.SchWeekDayService.Delete(weekday); } } foreach (Models.ModelWeekDays wd in model.WeekDays) { if (string.IsNullOrEmpty(wd.Description) || string.IsNullOrEmpty(wd.YYYYMM)) continue; newWeekDay = new SchWeekDayRespository(); newWeekDay.DESCRIPTION = wd.Description; newWeekDay.MAIN_ID = model.Id; newWeekDay.WEEKDAY = wd.WeekDay; newWeekDay.YYYYMM = wd.YYYYMM; mainService.SchWeekDayService.Create(newWeekDay); } } //delete 與 create需要分開做 因為不同的event可能是相同的日期 若同在一個loop做 可能會把該次新增的刪除掉 if (model.Events != null) { foreach (Models.ModelEvents e in model.Events) { events = eventsService.GetEventsByMainIdInDate(model.Id, e.YYYYMMDD); foreach (SchEventsRepository e2 in events) { eventsService.Delete(e2); } } foreach (Models.ModelEvents e in model.Events) { if (string.IsNullOrEmpty(e.YYYYMMDD)) continue; if (string.IsNullOrEmpty(e.Title) && string.IsNullOrEmpty(e.Style)) continue; newEvent = new SchEventsRepository(); newEvent.CREATED_BY = pernr; newEvent.YYYYMMDD = e.YYYYMMDD; newEvent.HHMM = e.HHMM; newEvent.TITLE = e.Title; newEvent.MAIN_ID = model.Id; newEvent.DETAIL = string.Empty; newEvent.STYLE = e.Style; eventsService.Create(newEvent); } } model.CurrentMonth = string.IsNullOrEmpty(model.CurrentMonth) ? DateTime.Now.ToString("yyyyMM") : model.CurrentMonth; eventsService.SaveChange(); model = getModel(model.Id, model.CurrentMonth); } catch (Exception ex) { mainService.RollBack(); throw ex; } model.Authority = Models.enumAuthority.Adm; string jsonObj = Newtonsoft.Json.JsonConvert.SerializeObject(model); return Content(jsonObj); }