public void DeleteCourseRest(string id, string coachid) { int _id = Convert.ToInt32(id); int _coachid = int.Parse(coachid); tCoachRest t = (from p in db.tCoachRest where p.fRestId == _id && p.fCoachId == _coachid select p).FirstOrDefault(); db.tCoachRest.Remove(t); db.SaveChanges(); }
public string UpdatetCoachRest(string STime, string ETime, string eventid, string coachid, string description) { DateTime StartTime = Convert.ToDateTime(STime); DateTime Endtime = Convert.ToDateTime(ETime); int _coachid = int.Parse(coachid); //判斷跟自己排休時間有沒有衝到 bool conflictCoachTime = (from p in db.tCoachRest where p.fCoachId == _coachid && ( (p.fStartTime <= StartTime && p.fEndTime >= Endtime) || (p.fStartTime <= Endtime && p.fEndTime >= Endtime)) select p).Any(); //判斷跟學員預約時間有沒有衝到 bool conflictBookingTime = (from p in db.tOrder where p.fCoachID == _coachid && ( (p.fStartTime <= StartTime && p.fEndTime >= Endtime) || (p.fStartTime <= Endtime && p.fEndTime >= Endtime)) select p).Any(); if (conflictBookingTime) { return("與學員預約時間衝突"); } if (conflictCoachTime && string.IsNullOrWhiteSpace(eventid)) { return("與自己排休時間重複"); } if (string.IsNullOrWhiteSpace(eventid)) { int restid = (from p in db.tCoachRest where p.fCoachId == _coachid select p.fRestId).FirstOrDefault(); if (restid == 0) { bool hasrestid = (from p in db.tCoachRest select p).Any(); if (hasrestid == false) { restid = 1; } else { restid = (from p in db.tCoachRest select p.fRestId).Max() + 1; } } else { restid = (from p in db.tCoachRest select p.fRestId).Max() + 1; } //新增到資料庫 tCoachRest t = new tCoachRest(); t.fRestId = restid; t.fCoachId = _coachid; t.fStartTime = StartTime; t.fEndTime = Endtime; t.Description = description; db.tCoachRest.Add(t); db.SaveChanges(); } else { //修改資料庫 int _eventid = int.Parse(eventid); tCoachRest t = (from p in db.tCoachRest where p.fRestId == _eventid && p.fCoachId == _coachid select p).FirstOrDefault(); if (t != null) { t.fStartTime = StartTime; t.fEndTime = Endtime; t.Description = description; db.Entry(t).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } return(""); }