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; entity.FailReason += DateTime.Now.ToString(); }); // 所有的过期用户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 )); } }
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"; if (Int32.Parse(_now.ToString("HH")) >= 6 && Int32.Parse(_now.ToString("HH")) <= 9) { SleepSpan = 500; } int erro = 0; JObject StrJson; IEnumerable <JToken> JsonListcode; IEnumerable <JToken> JsonListmsg; var data1 = from r in Db.ClockModels.ToList() where (r.flag == true && (r.ClockStateAM == false || r.ClockStatePM == false)) select new { r.CardId, //工号 r.PassWord, 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, //周日打卡 r.FailReason }; foreach (var item in data1.OrderBy(p => Guid.NewGuid())) { try { //判断当前用户“今天”需不需要打卡 if ((item.NeedSta && isSta) || (item.NeedSun && isSun) || (!isSta && !isSun)) { //判断“现在”是不是该用户打上班卡的时间 if (getTimeSpan(item.OnTimeStart, item.OnTimeEnd) && !item.ClockStateAM) { result = HttpPost(PostURL, "user="******"&pass="******"code"].AsEnumerable(); JsonListmsg = StrJson["msg"].AsEnumerable(); } catch (Exception ex) { logger.Error($"工号id{item.CardId}打卡出错:{ex}"); Cloc.FailReason += Unicode2String(result).ToString() + "JSON解析错误" + DateTime.Now.ToString(); if (Unicode2String(result).Contains("0")) //网站是通过CODE=0 来判断是否成功 { Cloc.LastClockTime = DateTime.Now; Cloc.ClockStateAM = true; } Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); continue; } if (JsonListcode.ToString().Contains("0")) { Cloc.LastClockTime = DateTime.Now; Cloc.ClockStateAM = true; Cloc.FailReason = null; Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; //一次性 生成sql语句到数据库执行 Db.SaveChanges(); Thread.Sleep(SleepSpan); } else { Cloc.FailReason += StrJson.ToString() + DateTime.Now.ToString(); Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); } } //判断“现在”是不是该用户打下班卡的时间 if (getTimeSpan(item.OffTimeStart, item.OffTimeEnd) && !item.ClockStatePM) { result = HttpPost(PostURL, "user="******"&pass="******"code"].AsEnumerable(); JsonListmsg = StrJson["msg"].AsEnumerable(); } catch (Exception ex) { logger.Error($"工号id{item.CardId}打卡出错:{ex}"); Cloc.FailReason += Unicode2String(result).ToString() + "JSON解析错误" + DateTime.Now.ToString(); if (Unicode2String(result).Contains("0")) //网站是通过CODE=0 来判断是否成功 { Cloc.LastClockTime = DateTime.Now; Cloc.ClockStatePM = true; } Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); continue; } if (JsonListcode.ToString().Contains("0")) { Cloc.LastClockTime = DateTime.Now; Cloc.ClockStatePM = true; Cloc.FailReason = null; Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); Thread.Sleep(SleepSpan); } else { Cloc.FailReason += StrJson.ToString() + DateTime.Now.ToString(); Db.Entry <ClockModels>(Cloc).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); } } } } catch (Exception ex) { erro++; logger.Error($"此次错误次数{erro},工号id{item.CardId},打卡出错:{ex}"); continue; } } }
public void ClockBatchGo() { _dicCookie.Clear(); _dicClassID.Clear(); _dicSucClass.Clear(); ClockInEntity Db = new ClockInEntity(); IEnumerable <ClockBatch> allData = (from r in Db.ClockBatch where r.flag == true select r).ToList(); //.Where(r => _now.Subtract(Convert.ToDateTime(r.LastClockTime)).TotalHours > 1); //.Where(r => _now.TimeOfDay.TotalHours- Convert.ToDateTime(Convert.ToDateTime(r.LastClockTime).ToShortTimeString()).TimeOfDay.TotalHours > 1); var ClassName = ""; JObject studentsJson = null; string strResult1 = ""; string strResult2 = ""; IEnumerable <JToken> studentsList; //获取所有班级 var temp = allData.GroupBy(x => x.ClassName).Select(g => new { g.Key }).ToList(); List <string> allClass = new List <string>(); temp.ForEach(x => _dicSucClass.TryAdd(x.Key, 2)); try { foreach (var model in allData.OrderBy(P => Guid.NewGuid())) { try { //该班失败的次数大于成功的次数 会停止该班的打卡 _dicSucClass.TryGetValue(model.ClassName, out int b); if (b <= 0) { continue; } if (model.StartClockTime == null || DateTime.Now.Subtract(Convert.ToDateTime(model.LastClockTime)).TotalHours < 0.5) { continue; } if (DateTime.Parse(Convert.ToDateTime(model.StartClockTime.ToString()).ToShortTimeString()).TimeOfDay > DateTime.Now.TimeOfDay) { continue; } ClassName = System.Text.RegularExpressions.Regex.Replace(model.ClassName, @"[^0-9]+", ""); var response1 = HttpPost(posturl1, "idcard=" + model.CardId); strResult1 = Unicode2String(response1);//正常字符串结果 if (strResult1.Contains("第一步成功")) { try { studentsJson = JObject.Parse(response1); } catch (Exception ex) { logger.Warn($"第一步网站JSON返回解析错误{strResult1}"); model.ClockState = false; model.FailedReason += strResult1 + DateTime.Now; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); _dicSucClass[model.ClassName]--; continue; } studentsList = studentsJson["data"]["classes"].AsEnumerable(); if (!_dicClassID.ContainsKey(model.ClassName)) { foreach (var item in studentsList) { //找到班级名称对应的班级ID 放入班级ID字典 删除“2019”再做数字匹配 if (ClassName.Equals(Regex.Replace(item["tname"].ToString().Replace("2019", ""), @"[^0-9]+", ""))) { _dicClassID.TryAdd(model.ClassName, item["id"].ToString());//key用model.ClassName是因为有:不同老师的班,但是数字相同的情况 break; } ; } } if (!_dicClassID.ContainsKey(model.ClassName)) { model.FailedReason += $"打卡失败,请该工号{model.CardId}是否在该班级"; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); _dicSucClass[model.ClassName]--; continue; } var response2 = HttpPost(posturl2, "class_id=" + _dicClassID[model.ClassName] , new Dictionary <string, string>() { { "cookie", _dicCookie[string.Format("idcard={0}", model.CardId)] } }); try { strResult2 = JObject.Parse(response2).ToString(); } catch (Exception) { logger.Warn($"第二步网站JSON返回解析错误{Unicode2String(response2)}"); _dicSucClass[model.ClassName]--; continue; } if (strResult2.Contains("成功")) { model.LastClockTime = DateTime.Now; model.ClockState = true; model.FailedReason = ""; model.Times++; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); _dicSucClass[model.ClassName]++; //此次操作成功次数 Thread.Sleep(new Random().Next(500, 1001)); } else if ((strResult2.Contains("已签"))) { if (DateTime.Now.Subtract(Convert.ToDateTime(model.LastClockTime)).TotalHours > 0.5) { model.Times++; } model.LastClockTime = DateTime.Now; model.ClockState = true; model.FailedReason += JObject.Parse(response2)["msg"].ToString() + DateTime.Now.ToString(); Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); _dicSucClass[model.ClassName]++; //此次操作成功次数 } else { model.ClockState = false; model.FailedReason += JObject.Parse(response2)["msg"].ToString() + DateTime.Now.ToString(); Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); //_dicSucClass.TryRemove(model.ClassName, out int aa); _dicSucClass[model.ClassName]--; } } else { model.ClockState = false; model.FailedReason += strResult1 + DateTime.Now; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); //_dicSucClass.TryRemove(model.ClassName, out bool bb); _dicSucClass[model.ClassName]--; } } catch (Exception ex) { logger.Fatal($"遍历内部错误,错误工号{model.CardId}错误信息{ex},已continue,继续下一个打卡用户!!!!预期外的错误!!!!{DateTime.Now.ToString()}"); continue; } } } catch (Exception ex) { logger.Fatal(ex); } }