private void UpdateSchedule(DutyCheckPackageTimePlan model, AllInOneContext.AllInOneContext db, DutyCheckPackageTimePlan oldPlan) { if (model.Schedule == null) { return; } Guid scheduleId = oldPlan.ScheduleId; if (oldPlan.ScheduleId != model.ScheduleId) { scheduleId = model.ScheduleId; } //转换一般数据 Schedule shedule = db.Schedule .Include(t => t.ScheduleType) .Include(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods) .Include(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .FirstOrDefault(p => p.ScheduleId.Equals(scheduleId)); if (shedule == null) { return; } shedule.EffectiveTime = model.Schedule.EffectiveTime; shedule.ExpirationTime = model.Schedule.ExpirationTime; shedule.ScheduleTypeId = model.Schedule.ScheduleTypeId; shedule.ScheduleName = model.Schedule.ScheduleName; // ScheduleCycle scheduleCycle = db.Set <ScheduleCycle>() .Include(t => t.DayPeriods) .FirstOrDefault(p => p.ScheduleCycleId.Equals(shedule.ScheduleCycle.ScheduleCycleId)); //转换ScheduleCycle scheduleCycle.CycleTypeId = model.Schedule.ScheduleCycle.CycleTypeId; scheduleCycle.Days = model.Schedule.ScheduleCycle.Days; scheduleCycle.DaysJson = model.Schedule.ScheduleCycle.DaysJson; scheduleCycle.LastExecute = model.Schedule.ScheduleCycle.LastExecute; scheduleCycle.Months = model.Schedule.ScheduleCycle.Months; scheduleCycle.MonthsJson = model.Schedule.ScheduleCycle.MonthsJson; scheduleCycle.NextExecute = model.Schedule.ScheduleCycle.NextExecute; scheduleCycle.WeekDayJson = model.Schedule.ScheduleCycle.WeekDayJson; scheduleCycle.WeekDays = model.Schedule.ScheduleCycle.WeekDays; // UpdateTimePeriod(db, shedule, model); // //scheduleCycle.DayPeriods = model.Schedule.ScheduleCycle.DayPeriods; shedule.ScheduleCycle = scheduleCycle; db.Schedule.Update(shedule); db.SaveChanges(); }
public IActionResult Delete(Guid id) { try { using (var db = new AllInOneContext.AllInOneContext()) { using (var tran = db.Database.BeginTransaction()) { Schedule data = db.Schedule .Include(t => t.ScheduleType) .Include(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods) .Include(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .FirstOrDefault(p => p.ScheduleId.Equals(id)); if (data == null) { return(NoContent()); } //移除LIST RemoveList(db, data); //移除ScheduleCycle ScheduleCycle scheduleCycle = db.Set <ScheduleCycle>() .FirstOrDefault(p => p.ScheduleCycleId.Equals(data.ScheduleCycle.ScheduleCycleId)); if (scheduleCycle != null) { db.Set <ScheduleCycle>().Remove(scheduleCycle); } db.Schedule.Remove(data); db.SaveChanges(); tran.Commit(); return(new NoContentResult()); } } } catch (DbUpdateException dbEx) { _logger.LogError("Delete:Message:{0}\r\n,StackTrace:{1}", dbEx.Message, dbEx.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message })); } catch (System.Exception ex) { _logger.LogError("Delete:Message:{0}\r\n,StackTrace:{1}", ex.Message, ex.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } }
public IActionResult Update([FromBody] Schedule model) { try { if (model == null) { return(BadRequest()); } using (var db = new AllInOneContext.AllInOneContext()) { using (var tran = db.Database.BeginTransaction()) { if (model.ScheduleCycle != null) { //补全数据 SystemOption cycletype = db.Set <SystemOption>() .FirstOrDefault(p => p.SystemOptionId.Equals(model.ScheduleCycle.CycleTypeId)); Schedule Schedule = model; if (cycletype != null) { Schedule.ScheduleCycle.CycleType = cycletype; } DateTime?executeTime = ScheduleUtility.GetExecuteTime(Schedule, DateTime.Now); model.ScheduleCycle.NextExecute = executeTime; } //转换一般数据 Schedule shedule = db.Schedule .Include(t => t.ScheduleType) .Include(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods) .Include(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .FirstOrDefault(p => p.ScheduleId.Equals(model.ScheduleId)); if (shedule == null) { return(BadRequest()); } shedule.EffectiveTime = model.EffectiveTime; shedule.ExpirationTime = model.ExpirationTime; shedule.ScheduleTypeId = model.ScheduleTypeId; shedule.ScheduleName = model.ScheduleName; // ScheduleCycle scheduleCycle = db.Set <ScheduleCycle>() .Include(t => t.DayPeriods) .FirstOrDefault(p => p.ScheduleCycleId.Equals(shedule.ScheduleCycle.ScheduleCycleId)); //转换ScheduleCycle scheduleCycle.CycleTypeId = model.ScheduleCycle.CycleTypeId; scheduleCycle.Days = model.ScheduleCycle.Days; scheduleCycle.DaysJson = model.ScheduleCycle.DaysJson; scheduleCycle.LastExecute = model.ScheduleCycle.LastExecute; scheduleCycle.Months = model.ScheduleCycle.Months; scheduleCycle.MonthsJson = model.ScheduleCycle.MonthsJson; scheduleCycle.NextExecute = model.ScheduleCycle.NextExecute; scheduleCycle.WeekDayJson = model.ScheduleCycle.WeekDayJson; scheduleCycle.WeekDays = model.ScheduleCycle.WeekDays; // RemoveList(db, shedule); // scheduleCycle.DayPeriods = model.ScheduleCycle.DayPeriods; shedule.ScheduleCycle = scheduleCycle; db.Schedule.Update(shedule); db.SaveChanges(); // tran.Commit(); return(new NoContentResult()); } } } catch (DbUpdateException dbEx) { _logger.LogError("Update:Message:{0}\r\n,StackTrace:{1}", dbEx.Message, dbEx.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message })); } catch (System.Exception ex) { _logger.LogError("Update:Message:{0}\r\n,StackTrace:{1}", ex.Message, ex.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } }