public ActionResult Add(ClockModels model) { try { model.CardId = model.CardId.Trim(); model.PassWord = model.PassWord.Trim(); model.CreatTime = DateTime.Now; model.flag = true; model.ClockStateAM = false; model.ClockStatePM = false; model.LastClockTime = Convert.ToDateTime("2000-1-1 00:00:00"); Db.ClockModels.Add(model); Db.Entry <ClockModels>(model).State = EntityState.Added; if (Db.SaveChanges() > 0) { return(Json("OK")); } else { return(Json("添加失败")); } } catch (Exception ex) { return(Json($"添加失败,请检查数据格式以及工号是否重复!异常信息:{ex}")); } }
public void ClockBatchGo111111111111() { DateTime _now = DateTime.Now; 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 strResult = ""; 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, true)); try { foreach (var model in allData.OrderBy(P => Guid.NewGuid())) { _dicSucClass.TryGetValue(model.ClassName, out bool b); if (!b) { continue; } if (model.StartClockTime == null || _now.Subtract(Convert.ToDateTime(model.LastClockTime)).TotalHours < 1) { continue; } if (DateTime.Parse(Convert.ToDateTime(model.StartClockTime.ToString()).ToShortTimeString()).TimeOfDay > _now.TimeOfDay) { continue; } ClassName = System.Text.RegularExpressions.Regex.Replace(model.ClassName, @"[^0-9]+", ""); var response1 = HttpPost(posturl1, "idcard=" + model.CardId); strResult = Unicode2String(response1);//正常字符串结果 if (strResult.Contains("第一步成功")) { try { studentsJson = JObject.Parse(response1); } catch (Exception) { model.ClockState = false; model.FailedReason = strResult; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); continue; } studentsList = studentsJson["data"]["classes"].AsEnumerable(); foreach (var item in studentsList) { //找到班级名称对应的班级ID 放入班级ID字典 从右边取10个字符,然后正则取纯数字。OS:我是被逼的 if (ClassName.Equals(System.Text.RegularExpressions.Regex.Replace(item["tname"].ToString().Remove(0, item["tname"].ToString().Length - 10), @"[^0-9]+", ""))) { if (!_dicClassID.ContainsKey(model.ClassName)) { _dicClassID.TryAdd(ClassName, item["id"].ToString()); break; } } ; } if (!_dicClassID.ContainsKey(ClassName)) { //logger.Warn($"打卡失败,请该工号{model.CardId}是否在该班级"); continue; } var response2 = HttpPost(posturl2, "class_id=" + _dicClassID[ClassName] , new Dictionary <string, string>() { { "cookie", _dicCookie[string.Format("idcard={0}", model.CardId)] } }); if (JObject.Parse(response2).ToString().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(); allClass.Remove(model.ClassName); Thread.Sleep(new Random().Next(3000, 6000)); } else if ((JObject.Parse(response2).ToString().Contains("已签到"))) { 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(); } 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 bool aa); } } else { model.ClockState = false; model.FailedReason = strResult; Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); _dicSucClass.TryRemove(model.ClassName, out bool bb); } } } catch (Exception ex) { } }
public void ClockBatchGo() { DateTime _now = DateTime.Now; 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; IEnumerable <JToken> studentsList; foreach (var model in allData.OrderBy(P => Guid.NewGuid())) { if (model.StartClockTime == null || _now.Subtract(Convert.ToDateTime(model.LastClockTime)).TotalHours < 1) { continue; } if (DateTime.Parse(Convert.ToDateTime(model.StartClockTime.ToString()).ToShortTimeString()).TimeOfDay > _now.TimeOfDay) { continue; } ClassName = System.Text.RegularExpressions.Regex.Replace(model.ClassName, @"[^0-9]+", ""); var response1 = HttpPost(posturl1, "idcard=" + model.CardId); studentsJson = JObject.Parse(response1); if (studentsJson.ToString().Contains("第一步成功")) { studentsList = studentsJson["data"]["classes"].AsEnumerable(); foreach (var item in studentsList) { //找到班级名称对应的班级ID 放入班级ID字典 从右边取10个字符,然后正则取纯数字。OS:我是被逼的 if (ClassName.Equals(System.Text.RegularExpressions.Regex.Replace(item["tname"].ToString().Remove(0, item["tname"].ToString().Length - 10), @"[^0-9]+", ""))) { if (!_dicClassID.ContainsKey(model.ClassName)) { _dicClassID.Add(ClassName, item["id"].ToString()); break; } } ; } if (!_dicClassID.ContainsKey(ClassName)) { //logger.Error($"打卡失败,请该工号{model.CardId}是否在该班级"); continue; } var response2 = HttpPost(posturl2, "class_id=" + _dicClassID[ClassName] , new Dictionary <string, string>() { { "cookie", _dicCookie[string.Format("idcard={0}", model.CardId)] } }); if (JObject.Parse(response2).ToString().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(); Thread.Sleep(new Random().Next(3000, 6000)); } else if ((JObject.Parse(response2).ToString().Contains("已签到"))) { 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(); } 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(); //if (allData.IndexOf(model) == 0) // break; } } else { model.ClockState = false; model.FailedReason = JObject.Parse(response1)["msg"].ToString() + DateTime.Now.ToString(); Db.ClockBatch.Attach(model); Db.Entry <ClockBatch>(model).State = EntityState.Modified; Db.SaveChanges(); //if (allData.IndexOf(model) == 0) // break; } } }