public IActionResult Add([FromBody] DutyGroupSchedule model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest());
                }
                using (var db = new AllInOneContext.AllInOneContext())
                {
                    //判断是否已存在编排
                    if (DutyGroupScheduleHelper.IsExist(model.StartDate, model.EndDate, model.OrganizationId))
                    {
                        return(BadRequest(new ApplicationException {
                            ErrorCode = "DataError", ErrorMessage = "当前时间已进行编排"
                        }));
                    }

                    if (CompletionData(model, db))
                    {
                        return(BadRequest(new ApplicationException {
                            ErrorCode = "DataError", ErrorMessage = "编排哨位节点下的监控点数据不存在"
                        }));
                    }
                    //
                    db.DutyGroupSchedule.Add(model);
                    db.SaveChanges();
                    //生成日数据
                    Task.Factory.StartNew(() =>
                    {
                        DutyGroupScheduleHelper.GetAbsoultedDutyCheckLog(model);
                    });
                    return(Created("", "OK"));
                }
            }
            catch (DbUpdateException dbEx)
            {
                return(BadRequest(new ApplicationException {
                    ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message
                }));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new ApplicationException {
                    ErrorCode = "Unknown", ErrorMessage = ex.Message
                }));
            }
        }
        public IActionResult Delete(Guid id)
        {
            try
            {
                using (var db = new AllInOneContext.AllInOneContext())
                {
                    using (var tran = db.Database.BeginTransaction())
                    {
                        DutyGroupSchedule dutyGroupSchedule = db.DutyGroupSchedule
                                                              .Include(t => t.Lister)
                                                              .Include(t => t.Organization)
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckMan)
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckDutySiteSchedule)/*.ThenInclude(t => t.CheckDutySite).ThenInclude(t => t.Camera)*/
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckDutySiteSchedule).ThenInclude(t => t.CheckMan)
                                                              .Include(t => t.Reservegroup).ThenInclude(t => t.Staff)
                                                              .Include(t => t.EmergencyTeam).ThenInclude(t => t.Staff)
                                                              .FirstOrDefault(p => p.DutyGroupScheduleId.Equals(id));
                        if (dutyGroupSchedule == null)
                        {
                            return(NoContent());
                        }
                        if (DutyGroupScheduleHelper.IsInWork(dutyGroupSchedule.StartDate, dutyGroupSchedule.EndDate, dutyGroupSchedule.OrganizationId) > 0)
                        {
                            dutyGroupSchedule.IsCancel = true;
                            db.DutyGroupSchedule.Update(dutyGroupSchedule);
                            db.SaveChanges();
                        }
                        else
                        {
                            //
                            RemoveLinkage(db, dutyGroupSchedule);
                            //
                            db.DutyGroupSchedule.Remove(dutyGroupSchedule);
                            db.SaveChanges();
                        }
                        tran.Commit();

                        return(new NoContentResult());
                    }
                }
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new ApplicationException {
                    ErrorCode = "Unknown", ErrorMessage = ex.Message
                }));
            }
        }
        /// <summary>
        /// 自动匹配该组织节点下的摄像机数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="db"></param>
        private static bool CompletionData(DutyGroupSchedule model, AllInOneContext.AllInOneContext db)
        {
            bool isCompleted = false;

            foreach (DutyGroupScheduleDetail dgsd in model.DutyGroupScheduleDetails)
            {
                foreach (DutyCheckSiteSchedule dcss in dgsd.CheckDutySiteSchedule)
                {
                    MonitorySite ms = DutyGroupScheduleHelper.GetMonitorySiteByOrganizationId(dcss.SiteOrganizationId.Value, db);
                    if (ms != null)
                    {
                        dcss.CheckDutySiteId = ms.MonitorySiteId;
                    }
                    else
                    {
                        isCompleted = true;
                    }
                }
            }
            return(isCompleted);
        }
        public IActionResult Update([FromBody] DutyGroupSchedule model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest());
                }
                using (var db = new AllInOneContext.AllInOneContext())
                {
                    using (var tran = db.Database.BeginTransaction())
                    {
                        DutyGroupSchedule dutyGroupSchedule = db.DutyGroupSchedule
                                                              .Include(t => t.Lister)
                                                              .Include(t => t.Organization)
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckMan)
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckDutySiteSchedule)/*.ThenInclude(t => t.CheckDutySite).ThenInclude(t => t.Camera)*/
                                                              .Include(t => t.DutyGroupScheduleDetails).ThenInclude(t => t.CheckDutySiteSchedule).ThenInclude(t => t.CheckMan)
                                                              .Include(t => t.Reservegroup).ThenInclude(t => t.Staff)
                                                              .Include(t => t.EmergencyTeam).ThenInclude(t => t.Staff)
                                                              .FirstOrDefault(p => p.DutyGroupScheduleId.Equals(model.DutyGroupScheduleId));
                        if (dutyGroupSchedule == null)
                        {
                            return(BadRequest());
                        }

                        //
                        if (dutyGroupSchedule.StartDate <= model.StartDate && dutyGroupSchedule.EndDate >= model.EndDate)
                        {
                            //判断是否为进行中
                            Delete(dutyGroupSchedule.DutyGroupScheduleId);
                        }
                        //删除未进行的查勤记录
                        var del = db.DutyCheckLog
                                  .Where(p => p.PlanDate >= DateTime.Now &&
                                         p.PlanDate <= dutyGroupSchedule.EndDate &&
                                         p.StatusId != new Guid("361ADFE9-E58A-4C88-B191-B742CC212443"));

                        if (del != null && del.Count() != 0)
                        {
                            db.DutyCheckLog.RemoveRange(del.ToList());
                            db.SaveChanges();
                        }
                        //执行新增流程,重新对实体内属性GUID赋值
                        DutyGroupSchedule newModel = UpdateData(model);
                        //
                        if (CompletionData(newModel, db))
                        {
                            return(BadRequest(new ApplicationException {
                                ErrorCode = "DataError", ErrorMessage = "数据不齐"
                            }));
                        }
                        //
                        db.DutyGroupSchedule.Add(newModel);
                        db.SaveChanges();
                        //生成日数据
                        Task.Factory.StartNew(() =>
                        {
                            DutyGroupScheduleHelper.GetAbsoultedDutyCheckLog(newModel);
                        });
                        tran.Commit();
                        return(new NoContentResult());
                    }
                }
            }
            catch (DbUpdateException dbEx)
            {
                return(BadRequest(new ApplicationException {
                    ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message
                }));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new ApplicationException {
                    ErrorCode = "Unknown", ErrorMessage = ex.Message
                }));
            }
        }