//全部更新 protected void Button_Quanbugengxin_Click(object sender, EventArgs e) { using (MySqlDBContext db = new MySqlDBContext()) { db.Database.ExecuteSqlCommand("TRUNCATE TABLE yuanshijilus"); //获取所有需要打卡人的数据 getdata字段为是否需要获取打卡数据 var userList = db.user.Where(x => x.getdata == 1).ToList(); //获取yuanshijilu表里面的最晚一条的数据的时间作为更新打卡数据的起始时间 var updateTime = db.yuanshijilu.Any() ? Convert.ToDateTime(db.yuanshijilu.Max(x => x.考勤时间)).Date : new DateTime(2019, 2, 25); List <DateTime> dateTimes = new List <DateTime>(); //当前时间的天数与打卡起始时间的天 差数 因为 钉钉的接口一次只能获取七天之内的打卡数据 所有需要分批 分时间段取数据 int days = (DateTime.Now.Date - updateTime).Days; //按时间天数差 循环 for (int i = 0; i < days; i++) { dateTimes.Add(DateTime.Now.Date.AddDays(-i)); } //七个一批 分成多少批 var index = dateTimes.Count % 7 == 0 ? dateTimes.Count / 7 : dateTimes.Count / 7 + 1; int offset = 0; var datas = new List <yuanshijilu>(); //取出每一个用户的所有数据 var stoptime = new Stopwatch(); stoptime.Start(); for (int i = 0; i < userList.Count; i++) { var user = userList[i]; //每一个用户取多少批 for (int t = 0; t < index; t++) { //当前这一批的所有时间 var times = dateTimes.Skip(t * 7).Take(7).ToList(); //去钉钉取数据 var data = dd.GetAttendance(user.user_id, times.Min(), times.Max().AddDays(1).AddMilliseconds(-1), offset, 50); //处理取出来的所有数据 for (int x = 0; x < data.recordresult.Count; x++) { var result = data.recordresult[x]; var jl = new yuanshijilu() { user_id = result.userId, 打卡时间 = StringToDateTime(result.userCheckTime.ToString()).AddHours(8), 打卡结果 = ToTimeResult(result.timeResult), 考勤时间 = StringToDateTime(result.baseCheckTime.ToString()).AddHours(8), 考勤日期 = StringToDateTime(result.workDate.ToString()) }; //插入数据库 datas.Add(jl); } } } stoptime.Stop(); var chaxuntime = stoptime.ElapsedMilliseconds; stoptime.Restart(); db.BulkInsert(datas); //db.yuanshijilu.AddRange(datas); try { db.SaveChanges(); stoptime.Stop(); var charutime = stoptime.ElapsedMilliseconds; //更新完成 弹出全部更新完成 Response.Write("<script>if(confirm('全部更新完成。查询用时{" + chaxuntime + "}。插入用时{" + charutime + "}')){window.location='/xuanze.aspx'} else{window.location='/xuanze.aspx'}</script>"); } catch (Exception ex) { //弹出暂时没有更新项,是否重新更新 var redirect = "<script>if(confirm('暂时没有更新项,是否重新更新 ' " + $"或者错误信息{ex.Message}" + ")){window.location='/xuanze.aspx'} else " + "{window.location='/shujugengxin.aspx'}</script>"; Response.Write(redirect); }; } }
//部分更新的原理和全部更新的原理一致 只是获取打卡数据的开始时间是自己设定的 protected void Button_Queding_Click(object sender, EventArgs e) { var kaishiTime = Convert.ToDateTime(TextBox_Kaishishijian.Text); var jieshuTime = Convert.ToDateTime(TextBox_Jieshushijian.Text + " 23:59:59"); if ((jieshuTime - kaishiTime).Days > 7) { Response.Write("<script>if(confirm('结束时间和开始时间最大范围不能超过7天')){window.location='/bufengengxin.aspx'} else{window.location='/xuanze.aspx'}</script>"); return; } using (MySqlDBContext db = new MySqlDBContext()) { var userList = db.user.Where(x => x.getdata == 1).ToList(); var maxTime = new DateTime(2019, 2, 25); if (db.yuanshijilu.Any()) { maxTime = Convert.ToDateTime(db.yuanshijilu.Max(x => x.考勤时间)); } if (Convert.ToDateTime(maxTime) > jieshuTime) { Response.Write("<script>if(confirm('当前时间段的数据已经更新存在于数据库中,请往后选择时间段更新')){window.location='/bufengengxin.aspx'} else{window.location='/xuanze.aspx'}</script>"); return; } if (maxTime > kaishiTime) { kaishiTime = maxTime; } List <DateTime> dateTimes = new List <DateTime>(); int days = (jieshuTime - kaishiTime).Days; for (int i = 0; i < days; i++) { dateTimes.Add(jieshuTime.Date.AddDays(-i)); } var index = dateTimes.Count % 7 == 0 ? dateTimes.Count / 7 : dateTimes.Count / 7 + 1; int offset = 0; for (int i = 0; i < userList.Count; i++) { var user = userList[i]; for (int t = 0; t < index; t++) { var times = dateTimes.Skip(t * 7).Take(7).ToList(); var data = dd.GetAttendance(user.user_id, times.Min(), times.Max().AddDays(1).AddMilliseconds(-1), offset, 50); for (int x = 0; x < data.recordresult.Count; x++) { var result = data.recordresult[x]; var jl = new yuanshijilu() { user_id = result.userId, 打卡时间 = StringToDateTime(result.userCheckTime.ToString()).AddHours(8), 打卡结果 = ToTimeResult(result.timeResult), 考勤时间 = StringToDateTime(result.baseCheckTime.ToString()).AddHours(8), 考勤日期 = StringToDateTime(result.workDate.ToString()) }; db.yuanshijilu.Add(jl); } } } if (db.SaveChanges() > 0) { Response.Write("<script>if(confirm('部分更新完成')){window.location='/xuanze.aspx'} else{window.location='/xuanze.aspx'}</script>"); } else { Response.Write("<script>if(confirm('暂时没有更新项,是否重新更新')){window.location='/xuanze.aspx'} else{window.location='/ shujugengxin.aspx'}</script>"); } } }