public void Reset() { IEnumerable <ClockModels> toUpdates = new List <ClockModels>(); // 获取所有数据 using (var context = new ClockInEntity()) { toUpdates = context.ClockModels.ToList(); } // 所有的打卡状态值都改为false Parallel.ForEach(toUpdates, (entity, state) => { entity.ClockStateAM = false; entity.ClockStatePM = false; }); // 所有的过期用户flag改为False Parallel.ForEach(toUpdates.Where( r => r.CreatTime.AddDays(r.TotalDays).Subtract(DateTime.Now).Days <= 0 ), (entity, state) => entity.flag = false); using (var context = new ClockInEntity()) { EFBatchOperation.For(context, context.ClockModels).UpdateAll(toUpdates, x => x.ColumnsToUpdate( c => c.ClockStateAM, c2 => c2.ClockStatePM, c3 => c3.flag )); } }
//private string str = HttpPost(@"http://203.160.52.218:810/log.php", "gh=320821611303161"); public void Main() { ClockInEntity Db = new ClockInEntity(); DateTime _now = DateTime.Now; string result = string.Empty; List <ClockModels> models = new List <ClockModels>(); bool isSta = _now.DayOfWeek.ToString() == "Saturday"; bool isSun = _now.DayOfWeek.ToString() == "Sunday"; var data1 = from r in Db.ClockModels.ToList() where (r.flag == true && (r.ClockStateAM == false || r.ClockStatePM == false)) select new { r.CardId, //工号 r.LastClockTime, r.ClockStateAM, //打卡状态 r.ClockStatePM, OnTimeStart = r.OnTimeStart.ToShortTimeString(), //上班打卡开始时间 OnTimeEnd = r.OnTimeEnd.ToShortTimeString(), //上班打卡结束时间 OffTimeStart = r.OffTimeStart.ToShortTimeString(), //下班打卡开始时间 OffTimeEnd = r.OffTimeEnd.ToShortTimeString(), //下班打卡结束时间 r.NeedSta, //周六打卡 r.NeedSun //周日打卡 }; foreach (var item in data1.ToList().OrderBy(p => Guid.NewGuid())) { //判断当前用户“今天”需不需要打卡 if ((item.NeedSta && isSta) || (item.NeedSun && isSun) || (!item.NeedSta && !item.NeedSun && !isSta && !isSun)) { //判断“现在”是不是该用户打上班卡的时间 if (getTimeSpan(item.OnTimeStart, item.OnTimeEnd) && !item.ClockStateAM) { result = HttpPost(PostURL, "gh=" + item.CardId); if (result.Contains("true")) { var data = from r in Db.ClockModels where r.CardId == item.CardId select r; ClockModels Cloc = data.FirstOrDefault <ClockModels>(); Cloc.LastClockTime = DateTime.Now; Cloc.ClockStateAM = true; Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; //一次性 生成sql语句到数据库执行 Db.SaveChanges(); Thread.Sleep(1000); } } //判断“现在”是不是该用户打下班卡的时间 if (getTimeSpan(item.OffTimeStart, item.OffTimeEnd) && !item.ClockStatePM) { result = HttpPost(PostURL, "gh=" + item.CardId); if (result.Contains("true")) { var data = from r in Db.ClockModels where r.CardId == item.CardId select r; ClockModels Cloc = data.FirstOrDefault <ClockModels>(); Cloc.LastClockTime = DateTime.Now; Cloc.ClockStatePM = true; Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; //一次性 生成sql语句到数据库执行 Db.SaveChanges(); Thread.Sleep(1000); } } } } }