/// <summary> /// 根據WeekTable資料重新計算展開的TimeTable資料 /// </summary> /// <param name="requestData"></param> /// <returns></returns> public List <TimeTable> GetTimeTableByWeekTable(WeekTablePostRequest requestData) { var tokenService = new TokenService(); var checkToken = tokenService.GetTokenInfo(requestData.Token).Result; if (checkToken == null) { return(null); } var datas = Getdata(requestData.WeekTableData.StartDate, requestData.WeekTableData.EndDate, requestData.CircleKey.ToLower(), 0, requestData.WeekTableData.WeekTable); foreach (var data in datas) { if (data.StartDate.HasValue) { DateTime?_startDate = data.StartDate.Value.ToLocalTime(); data.StartDate = _startDate; } if (data.EndDate.HasValue) { DateTime?_endDate = data.EndDate.Value.ToLocalTime(); data.EndDate = _endDate; } } return(datas.OrderBy(t => t.StartDate).ToList()); }
public IHttpActionResult Post(WeekTablePostRequest requestData) { var requestService = new Service.Utility.RequestDataHelper <WeekTablePostRequest>(); var checkColumnKeys = new string[3] { "token", "circlekey", "weektabledata" }; var checkEmpty = requestService.CheckColumnEmpty(requestData, checkColumnKeys); var response = new Infrastructure.ViewModel.Base.BaseResponse <string>(); response.Success = false; if (checkEmpty == false) { response.Message = "遺漏參數"; response.State = Infrastructure.ViewModel.Base.LogState.RequestDataError; return(Content(HttpStatusCode.BadRequest, response)); } var weekTableService = new WeekTableService(); var responseData = weekTableService.WeekTableDataProxy(requestData); if (responseData) { response.Success = true; response.Message = "處理成功"; return(Ok(response)); } response.Message = "處理失敗"; response.State = Infrastructure.ViewModel.Base.LogState.Error; return(Ok(response)); }
/// <summary> /// 處理資料 /// </summary> /// <param name="requestData"></param> /// <returns></returns> public bool WeekTableDataProxy(WeekTablePostRequest requestData) { var tokenService = new TokenService(); var tokenInfo = tokenService.GetTokenInfo(requestData.Token).Result; if (tokenInfo == null) { return(false); } if (requestData.WeekTableData == null) { return(false); } var insertDatas = requestData.WeekTableData.WeekTable.Where(t => t.Id == 0); var learningCircleService = new LearningCircleService(); var learningCircleInfo = learningCircleService.GetDetailByOuterKey(requestData.CircleKey.ToLower()); if (learningCircleInfo == null) { return(false); } var startDateTime = requestData.WeekTableData.StartDate; var endDateTime = requestData.WeekTableData.EndDate; var dayOfWeekTools = new Utility.DayOfWeekTools(); var DateTimeTools = new Utility.DateTimeTools(); //處理新增資料 foreach (var insertData in insertDatas) { insertData.LearningCircleId = learningCircleInfo.Id; insertData.ClassWeekType = 0; insertData.Creator = tokenInfo.MemberId; insertData.CreateUtcDate = DateTime.UtcNow; var insertStartPeriod = DateTimeTools.DatetimeToSectionint(insertData.StartTime, true); var insertEndPeriod = DateTimeTools.DatetimeToSectionint(insertData.EndTime, false); insertData.StartPeriod = insertStartPeriod; insertData.EndPeriod = insertEndPeriod; /*判斷week字串與startTime的時與分,再組成對的時間*/ //取出時與分 var insertStartHour = insertData.StartTime.ToUniversalTime().Hour; var insertStartMinute = insertData.StartTime.ToUniversalTime().Minute; var insertEndHour = insertData.EndTime.ToUniversalTime().Hour; var insertEndMinute = insertData.EndTime.ToUniversalTime().Minute; var insertDayOfWeek = dayOfWeekTools.ChangeToDayOfWeek(insertData.Week); if (insertDayOfWeek == null) { return(false); } var startDayOfWeek = startDateTime.DayOfWeek; if (insertDayOfWeek.Value != startDayOfWeek) { var interval = (int)startDayOfWeek - (int)insertDayOfWeek.Value; if (interval > 0) { startDateTime = startDateTime.AddDays(interval); } else { startDateTime = startDateTime.AddDays(7 + interval); } } startDateTime = startDateTime.AddHours(insertStartHour); startDateTime = startDateTime.AddMinutes(insertStartMinute); endDateTime = endDateTime.AddHours(insertEndHour); endDateTime = endDateTime.AddMinutes(insertEndMinute); insertData.StartTime = startDateTime.ToUniversalTime(); insertData.EndTime = endDateTime.ToUniversalTime(); } try { var db = _uow.DbContext; var updateLearningCircleInfo = db.LearningCircle.FirstOrDefault(t => t.LearningOuterKey.ToLower() == requestData.CircleKey.ToLower()); if (updateLearningCircleInfo != null) { updateLearningCircleInfo.StartDate = requestData.WeekTableData.StartDate.ToUniversalTime(); updateLearningCircleInfo.EndDate = requestData.WeekTableData.EndDate.ToUniversalTime(); } db.WeekTable.AddRange(insertDatas); var dbData = GetByCirclekey(requestData.CircleKey.ToLower()); //聯集資料 var unionDataIds = requestData.WeekTableData.WeekTable.Select(t => t.Id).Union(dbData.WeekTable.Select(t => t.Id)); //找出欲刪除的資料 var deleteDataIds = unionDataIds.Except(requestData.WeekTableData.WeekTable.Select(t => t.Id)); foreach (var deleteDataId in deleteDataIds) { var deleteTimeTable = db.WeekTable.Find(deleteDataId); if (deleteTimeTable == null) { continue; } db.WeekTable.Remove(deleteTimeTable); } //處理編輯資料 var updateDatas = requestData.WeekTableData.WeekTable.Where(t => t.Id > 0).ToList(); startDateTime = requestData.WeekTableData.StartDate; endDateTime = requestData.WeekTableData.EndDate; foreach (var updateData in updateDatas) { var updateDataInfo = db.WeekTable.Find(updateData.Id); var updateStartPeriod = DateTimeTools.DatetimeToSectionint(updateData.StartTime, true); var updateEndPeriod = DateTimeTools.DatetimeToSectionint(updateData.EndTime, false); updateDataInfo.StartPeriod = updateStartPeriod; updateDataInfo.EndPeriod = updateEndPeriod; updateDataInfo.UpdateUtcDate = DateTime.UtcNow; updateDataInfo.Updater = tokenInfo.MemberId; /*判斷week字串與startTime的時與分,再組成對的時間*/ //取出時與分 var updateStartHour = updateData.StartTime.ToUniversalTime().Hour; var updateStartMinute = updateData.StartTime.ToUniversalTime().Minute; var updateEndHour = updateData.EndTime.ToUniversalTime().Hour; var updateEndMinute = updateData.EndTime.ToUniversalTime().Minute; var updateDayOfWeek = dayOfWeekTools.ChangeToDayOfWeek(updateData.Week); if (updateDayOfWeek == null) { return(false); } var startDayOfWeek = startDateTime.DayOfWeek; if (updateDayOfWeek.Value != startDayOfWeek) { var interval = (int)startDayOfWeek - (int)updateDayOfWeek.Value; if (interval > 0) { startDateTime = startDateTime.AddDays(interval); } else { startDateTime = startDateTime.AddDays(7 + interval); } } startDateTime = startDateTime.AddHours(updateStartHour); startDateTime = startDateTime.AddMinutes(updateStartMinute); endDateTime = endDateTime.AddHours(updateEndHour); endDateTime = endDateTime.AddMinutes(updateEndMinute); updateDataInfo.StartTime = updateData.StartTime.ToUniversalTime(); updateDataInfo.EndTime = updateData.EndTime.ToUniversalTime(); updateDataInfo.Week = updateData.Week; updateDataInfo.Place = updateData.Place; } db.SaveChanges(); return(true); } catch (Exception ex) { return(false); } }