public IActionResult Get(Guid id) { try { using (var db = new AllInOneContext.AllInOneContext()) { DutyCheckPackageTimePlan data = db.DutyCheckPackageTimePlan .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods).ThenInclude(t => t.TimePeriods) .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .Include(t => t.Schedule).ThenInclude(t => t.ScheduleType) .Include(t => t.Organization) .FirstOrDefault(p => p.DutyCheckPackageTimePlanId.Equals(id)); if (data == null) { return(NoContent()); } return(new ObjectResult(data)); } } catch (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 transaction = db.Database.BeginTransaction()) { DutyCheckPackageTimePlan plan = db.DutyCheckPackageTimePlan .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods).ThenInclude(t => t.TimePeriods) .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .Include(t => t.Organization) .FirstOrDefault(p => p.DutyCheckPackageTimePlanId.Equals(id)); if (plan == null) { return(NoContent()); } db.DutyCheckPackageTimePlan.Remove(plan); db.SaveChanges(); transaction.Commit(); return(new NoContentResult()); } } } catch (System.Exception ex) { return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } }
public IActionResult Update([FromBody] DutyCheckPackageTimePlan model) { try { if (model == null) { return(BadRequest()); } using (var db = new AllInOneContext.AllInOneContext()) { using (var transaction = db.Database.BeginTransaction()) { DutyCheckPackageTimePlan plan = db.DutyCheckPackageTimePlan .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods).ThenInclude(t => t.TimePeriods) .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .Include(t => t.Organization) .FirstOrDefault(p => p.DutyCheckPackageTimePlanId.Equals(model.DutyCheckPackageTimePlanId)); if (plan == null) { return(BadRequest()); } //转换普通数据 plan.OrganizationId = model.OrganizationId; plan.RandomRate = model.RandomRate; //更新排程中查勤设置 UpdateSchedule(model, db, plan); // plan.ScheduleId = model.ScheduleId; // DutyCheckPackageHelper.DutyCheckPackageTimePlanOnChange(plan.OrganizationId); // db.DutyCheckPackageTimePlan.Update(plan); db.SaveChanges(); transaction.Commit(); DutyCheckPackageHelper.AllocationDutychekPackage(plan.OrganizationId, DateTime.Now); 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 })); } }
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(); }
/// <summary> /// 根据组织机构ID获取查勤包计划 /// </summary> /// <param name="organizationId"></param> /// <returns></returns> public static DutyCheckPackageTimePlan GetDutyCheckPackageTimePlan(Guid organizationId) { DutyCheckPackageTimePlan plan = null; using (var db = new AllInOneContext.AllInOneContext()) { plan = db.DutyCheckPackageTimePlan .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.DayPeriods).ThenInclude(t => t.TimePeriods) .Include(t => t.Schedule).ThenInclude(t => t.ScheduleCycle).ThenInclude(t => t.CycleType) .FirstOrDefault(p => p.Organization.OrganizationId.Equals(organizationId)); } return(plan); }
public IActionResult Add([FromBody] DutyCheckPackageTimePlan model) { try { if (model == null) { return(BadRequest()); } using (var db = new AllInOneContext.AllInOneContext()) { using (var tran = db.Database.BeginTransaction()) { DutyCheckPackageTimePlan plan = db.DutyCheckPackageTimePlan .FirstOrDefault(p => p.OrganizationId.Equals(model.OrganizationId)); if (plan != null) //每级节点只允许存在一个查勤包计划表 { return(BadRequest(new ApplicationException { ErrorCode = "Exist", ErrorMessage = "已存在查勤计划" })); } db.DutyCheckPackageTimePlan.Add(model); db.SaveChanges(); tran.Commit(); DutyCheckPackageHelper.AllocationDutychekPackage(plan.OrganizationId, DateTime.Now); 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 })); } }
private void Run() { while (true) { try { DutyCheckPackageTimePlan executeTask = m_timerList.OrderBy(t => t.Schedule.ScheduleCycle.NextExecute).FirstOrDefault(t => !t.Running); if (executeTask == null) { m_planExecutorWaiter.WaitOne(); } //休眠 Schedule planSchedule = executeTask.Schedule; DateTime currTime = DateTime.Now; if (planSchedule.ScheduleCycle.NextExecute != null && !executeTask.Running) { int waitTimeout = (int)((planSchedule.ScheduleCycle.NextExecute.Value - currTime).TotalMilliseconds); //误差2分钟内,有效执行 if (waitTimeout < 0 && waitTimeout > -2 * 60 * 1000) { waitTimeout = 0; } if (waitTimeout >= 0) { TimePeriod tp = ScheduleUtility.GetPlanTaskTimePeriod(planSchedule, planSchedule.ScheduleCycle.NextExecute.Value); executeTask.Running = true; bool isGenerate = DutyCheckPackageHelper.CheckDutyPackageExist(executeTask.OrganizationId); if (isGenerate) { DutyCheckPackageHelper.AllocationDutychekPackage(executeTask.OrganizationId, DateTime.Now); } var planTask = Task.Delay(waitTimeout).ContinueWith((runTask) => GetDutyCheckPackage()); } } } catch (Exception ex) { _logger.LogError("执行查勤包异常:Message:{0}\r\nStackTrace:{1}", ex.Message, ex.StackTrace); } } }
/// <summary> /// 手动移除实体中的List属性 /// </summary> /// <param name="db"></param> /// <param name="shedule"></param> private static void UpdateTimePeriod(AllInOneContext.AllInOneContext db, Schedule shedule, DutyCheckPackageTimePlan model) { foreach (DayPeriod dp in model.Schedule.ScheduleCycle.DayPeriods) { foreach (TimePeriod tp in dp.TimePeriods) { TimePeriod timePeriod = db.Set <TimePeriod>().FirstOrDefault(p => p.TimePeriodId.Equals(tp.TimePeriodId)); if (timePeriod != null) { timePeriod.ExtraJson = tp.ExtraJson; db.Set <TimePeriod>().Update(timePeriod); db.SaveChanges(); } } } }
/// <summary> /// 划分查勤包 /// </summary> public static void AllocationDutychekPackage(Guid organizationId, DateTime packStartTime) { #region Bak //DutyCheckPackageTimePlan plan = GetDutyCheckPackageTimePlan(organizationId); //List<MonitorySite> allMonitorySites = GetAllMonitorySite(organizationId); //if (plan == null || allMonitorySites.Count==0) // return; //if (plan.RandomRate != 0) //{ // allMonitorySites.AddRange(GetRandomMonitorySite(plan.RandomRate, allMonitorySites)); //} //int monitorPointNum = allMonitorySites.Count; //int scheduleDay = 1; //switch (plan.Schedule.ScheduleCycle.CycleType.SystemOptionCode) //{ // case "13700001": //每天 // scheduleDay = 1; // break; // case "13700002": //每周 // scheduleDay = 7; // break; //} //int fixMonitorPointSize =GetAbsoultedPackageSize(plan.Schedule.ScheduleCycle.DayPeriods[0].TimePeriods); //int onedayCount = monitorPointNum / scheduleDay; //DateTime endTime = new DateTime(startTime.AddDays(scheduleDay).Year, startTime.AddDays(scheduleDay).Month, startTime.AddDays(scheduleDay).Day, 23, 59, 59); //List<DutyCheckPackage> DutyCheckPackages = new List<DutyCheckPackage>(); //for (int i = 0; i < scheduleDay; i++) //{ // startTime = startTime.AddDays(i); // startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0); // foreach (TimePeriod tp in plan.Schedule.ScheduleCycle.DayPeriods[0].TimePeriods) // { // //组装查勤包 // DutyCheckPackage dDutyCheckPackage = new DutyCheckPackage(); // dDutyCheckPackage.DutyCheckPackageId = Guid.NewGuid(); // dDutyCheckPackage.StartTime = new DateTime(startTime.Year, startTime.Month,startTime.Day,tp.StartTime.Hour,tp.StartTime.Minute,tp.StartTime.Second); // dDutyCheckPackage.EndTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, tp.EndTime.Hour, tp.EndTime.Minute, tp.EndTime.Second); // dDutyCheckPackage.OrganizationId = plan.OrganizationId; // List<DutyCheckPackageLog> logs = new List<DutyCheckPackageLog>(); // int packageSize = 0; // if (tp.TimePeriodExtra != null && tp.TimePeriodExtra.ValueType == 0) // { // packageSize = tp.TimePeriodExtra.AbsoluteValue; // } // else if (tp.TimePeriodExtra != null && tp.TimePeriodExtra.ValueType == 1) // { // packageSize = (int)(onedayCount - fixMonitorPointSize * tp.TimePeriodExtra.PercentValue / 100.0D); // } // else // { // packageSize = onedayCount; // } // Random r = new Random(); // for (int k = 0; k < packageSize; k++) // { // if (monitorPointNum == 0) // { // break; // } // int index = r.Next(monitorPointNum); // MonitorySite monitorPoint = allMonitorySites[index]; // allMonitorySites.RemoveAt(index); // monitorPointNum--; // //DutyCheckLog log = new DutyCheckLog // //{ // // DutyCheckLogId = Guid.NewGuid(), // // OrganizationId = plan.OrganizationId, // // RecordTypeId = new Guid("359A58FA-0BAB-45A3-ACAF-98EB73228B95"), //网络查勤 // // //Schedule=plan.Schedule, // // StatusId = new Guid("361ADFE9-E58A-4C88-B191-B742CC212443"), //未开始 // //}; // if (monitorPoint == null) // { // break; // } // DutyCheckPackageLog dutycheckpackagelog = new DutyCheckPackageLog // { // DutyCheckPackageId = dDutyCheckPackage.DutyCheckPackageId, // DutyCheckLog = GetSiteLog(plan.OrganizationId, tp, monitorPoint, plan.Schedule.ScheduleCycle.DayPeriods[0].DayPeriodId) // }; // logs.Add(dutycheckpackagelog); // } // if (logs.Count == 0) // { // break; // } // dDutyCheckPackage.DutyCheckPackLogs = logs; // DutyCheckPackages.Add(dDutyCheckPackage); // } //} //using (var db = new AllInOneContext.AllInOneContext()) //{ // try // { // db.DutyCheckPackage.AddRange(DutyCheckPackages); // db.SaveChanges(); // } // catch (Exception ex) // { // } //} #endregion //不再预先取下级查勤点,改为按组织机构生成查勤包,查勤再向下级去查勤点数据(1.基于网络风险,可能导致取不到监控点,引起异常) DutyCheckPackageTimePlan plan = GetDutyCheckPackageTimePlan(organizationId); using (var db = new AllInOneContext.AllInOneContext()) { Organization org = db.Organization.FirstOrDefault(p => p.OrganizationId.Equals(organizationId)); if (org == null) { return; } //获取所有组织机构 var orgs = db.Organization.Where(t => t.DutycheckPoints > 0).ToList(); //查勤点 List <Guid> dutycheckSiteOrganizationIdList = new List <Guid>(); orgs.ForEach(t => { for (int i = 0; i < t.DutycheckPoints; i++) { dutycheckSiteOrganizationIdList.Add(t.OrganizationId); } }); dutycheckSiteOrganizationIdList.AddRange(GetRandomOrganizationList(plan.RandomRate, dutycheckSiteOrganizationIdList)); //根据查勤包数量:时段*周期(总队默认一周覆盖一次,支队和中队是每天都要覆盖一次) int scheduleDay = 0; int dutycheckSiteSum = dutycheckSiteOrganizationIdList.Count; if (!org.OrganizationFullName.Contains(".") && org.OrganizationFullName.Contains("总队")) { scheduleDay = plan.Schedule.ScheduleCycle.DayPeriods[0].TimePeriods.Count() * plan.Schedule.ScheduleCycle.Days.Count(); } else { scheduleDay = 1; } // int perDayDutycheckSiteCount = dutycheckSiteSum / scheduleDay; if (perDayDutycheckSiteCount * scheduleDay < dutycheckSiteSum) { perDayDutycheckSiteCount = perDayDutycheckSiteCount + 1; } int fixDutycheckSiteCount = GetAbsoultedPackageSize(plan.Schedule.ScheduleCycle.DayPeriods[0].TimePeriods); //固定查勤数量 DateTime endTime = new DateTime(packStartTime.AddDays(scheduleDay).Year, packStartTime.AddDays(scheduleDay).Month, packStartTime.AddDays(scheduleDay).Day, 23, 59, 59); List <DutyCheckPackage> dutycheckPackageList = new List <DutyCheckPackage>(); for (int i = 0; i < scheduleDay; i++) { DateTime startTime = packStartTime.AddDays(i).Date; foreach (TimePeriod tp in plan.Schedule.ScheduleCycle.DayPeriods[0].TimePeriods) { //组装查勤包 DutyCheckPackage dutyCheckPackage = new DutyCheckPackage(); dutyCheckPackage.DutyCheckPackageId = Guid.NewGuid(); dutyCheckPackage.StartTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, tp.StartTime.Hour, tp.StartTime.Minute, tp.StartTime.Second); dutyCheckPackage.EndTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, tp.EndTime.Hour, tp.EndTime.Minute, tp.EndTime.Second); dutyCheckPackage.OrganizationId = plan.OrganizationId; List <DutyCheckPackageLog> logs = new List <DutyCheckPackageLog>(); int packageSize = perDayDutycheckSiteCount; if (tp.TimePeriodExtra != null && tp.TimePeriodExtra.ValueType == 0) { packageSize = tp.TimePeriodExtra.AbsoluteValue; } else if (tp.TimePeriodExtra != null && tp.TimePeriodExtra.ValueType == 1) { packageSize = (int)(perDayDutycheckSiteCount - fixDutycheckSiteCount * tp.TimePeriodExtra.PercentValue / 100.0D); } Random r = new Random(); for (int k = 0; k < packageSize; k++) { if (dutycheckSiteSum == 0) { break; } int index = r.Next(dutycheckSiteSum); Guid dutycheckPointOrganizationId = dutycheckSiteOrganizationIdList[index]; dutycheckSiteOrganizationIdList.RemoveAt(index); dutycheckSiteSum--; DutyCheckPackageLog dutycheckpackagelog = new DutyCheckPackageLog { DutyCheckPackageId = dutyCheckPackage.DutyCheckPackageId, DutyCheckLog = PreCreateDutycheckLog(plan.OrganizationId, tp, dutycheckPointOrganizationId, plan.Schedule.ScheduleCycle.DayPeriods[0].DayPeriodId) }; logs.Add(dutycheckpackagelog); } if (logs.Count > 0) { dutyCheckPackage.DutyCheckPackLogs = logs; dutyCheckPackage.PackageStatusId = new Guid("361ADFE9-E58A-4C88-B191-B742CC212443"); //未开始 dutycheckPackageList.Add(dutyCheckPackage); } } } db.DutyCheckPackage.AddRange(dutycheckPackageList); db.SaveChanges(); } }