Ejemplo n.º 1
0
 private void btnOk_Click(object sender, EventArgs e)
 {
     if (CheckInput())
     {
         List<AttendanceLog> logs = GetLogs();
         if (logs != null && logs.Count > 0)
         {
             FrmProcessing frm = new FrmProcessing();
             Action action = delegate()
             {
                 AttendanceLogBLL bll = new AttendanceLogBLL(AppSettings.CurrentSetting.ConnectUri);
                 int count = logs.Count;
                 int added = 0;
                 foreach (AttendanceLog log in logs)
                 {
                     bll.Add(log);
                     added++;
                     frm.ShowProgress(string.Empty, (decimal)added / count);
                 }
             };
             Thread t = new Thread(new ThreadStart(action));
             t.IsBackground = true;
             t.Start();
             if (frm.ShowDialog() != DialogResult.OK)
             {
                 t.Abort();
             }
         }
         this.departmentTreeview1.SelectedStaff = null;
         MessageBox.Show("补签完成,可以继续补签");
     }
 }
  private void GetAttendanceLog(DeviceInfo device, bool onlyLatest)
  {
      ZKFingerKeeper keeper = new ZKFingerKeeper(device);
      FrmProcessing frm = new FrmProcessing();
      Action action = delegate()
      {
          frm.ShowProgress("正在连接考勤机...", 0);
          keeper.Connect();
          if (keeper.IsConnected)
          {
              try
              {
                  //清空控制器
                  frm.ShowProgress("正在获取考勤记录...", 0);
                  DatetimeRange dr = null;
                  if (onlyLatest && device.LastEventDt != null)
                  {
                      dr = new DatetimeRange(device.LastEventDt.Value.AddSeconds(1), new DateTime(2099, 12, 31)); //从最后那个时间开始获取
                  }
                  List<AttendanceLog> logs = keeper.GetAttendanceLogs(dr);
                  if (logs == null || logs.Count == 0)
                  {
                      frm.ShowProgress(string.Empty , 1);
                  }
                  else
                  {
                      frm.ShowProgress(string.Format("获取到 {0} 条考勤记录,正在努力保存考勤记录...", logs.Count), 0.99m);
                      AttendanceLogBLL bll = new AttendanceLogBLL(AppSettings.CurrentSetting.ConnectUri);
                      int count = 0;
                      foreach (AttendanceLog log in logs)
                      {
                          count++;
                          frm.ShowProgress(string.Format("正在保存第 {0} 条记录", count), (decimal)count / logs.Count);
                          CommandResult ret = bll.Add(log);
                      }
                      frm.ShowProgress(string.Empty , 1);
                      device.LastEventDt = logs.Max(it => it.ReadDateTime);
                      (new DeviceInfoBLL(AppSettings.CurrentSetting.ConnectUri)).Update(device);
                  }
              }
              catch (ThreadAbortException)
              {
              }
              catch (Exception ex)
              {
                  LJH.GeneralLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex);
              }
              finally
              {
                  keeper.Disconnect();
              }
          }
          else
          {
              frm.ShowProgress("连接设备失败", 1);
          }
      };
      Thread t = new Thread(new ThreadStart(action));
      t.Start();
      frm.ShowDialog();
 }