123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using Check.DataOperate;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using UAS_MES.PublicForm;
- namespace UAS_CheckWork
- {
- public partial class Form1 : Form
- {
- DataHelper dh;
- //Create Standalone SDK class dynamicly.
- public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
- private bool bIsConnected = false;//the boolean value identifies whether the device is connected
- private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.
- //开启线程
- Thread InitGetInfo;
- public Form1()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- CheckForIllegalCrossThreadCalls = false;//可跨线程
- txtIP.Text = Properties.Settings.Default.IP;
- txtPort.Text = Properties.Settings.Default.Port;
- dh = new DataHelper();
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- Properties.Settings.Default.IP = txtIP.Text;
- Properties.Settings.Default.Port = txtPort.Text;
- Properties.Settings.Default.Save();
- if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
- {
- MessageBox.Show("请先输入IP和端口", "Error");
- return;
- }
- int idwErrorCode = 0;
- Cursor = Cursors.WaitCursor;
- if (btnConnect.Text == "断开连接")
- {
- axCZKEM1.Disconnect();
- bIsConnected = false;
- btnConnect.Text = "连接";
- lblState.Text = "状态:未连接";
- Cursor = Cursors.Default;
- return;
- }
- bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
- if (bIsConnected)
- {
- btnConnect.Text = "断开连接";
- btnConnect.Refresh();
- lblState.Text = "状态:已连接";
- iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
- axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
- }
- else
- {
- axCZKEM1.GetLastError(ref idwErrorCode);
- MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
- }
- Cursor = Cursors.Default;
- }
- private void getAllData_Click(object sender, EventArgs e)
- {
- if (!bIsConnected)
- {
- MessageBox.Show("请先连接设备", "Error");
- return;
- }
- InitGetInfo = new Thread(InPrint);
- SetLoadingWindow stw = new SetLoadingWindow(InitGetInfo, "正在同步数据...");
- stw.StartPosition = FormStartPosition.CenterParent;
- stw.ShowDialog();
- }
- private void InPrint()
- {
- int idwErrorCode = 0;
- //获取所有的考勤记录
- if (axCZKEM1.ReadGeneralLogData(iMachineNumber))
- {
- //循环获取
- DataTable dt = new DataTable();
- dt.Columns.Add("cl_cardcode");
- dt.Columns.Add("cl_time");
- showDataGrid.DataSource = dt;
- string SerialNum = "";
- axCZKEM1.GetSerialNumber(axCZKEM1.MachineNumber, out SerialNum);
- string LastDate = dh.getFieldDataByCondition("cardlog", "to_char(max(cl_time),'yyyy-MM-dd HH24:mi:ss')", "cl_address='考勤机数据导入' and cl_code='" + SerialNum + "'").ToString();
- if (LastDate == "")
- {
- LastDate = "1990-01-01 00:00:00";
- }
- string enrollNumber = "";//记录用户ID
- int verifyCode = 0;//记录用户验证方式 0 为密码验证,1 为指纹验证,2 为卡验证
- int inoutMode = 0;//考勤状态,具体含义如下: 默认 0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out
- int year = 0;//记录考勤年份
- int month = 0;//记录考勤月份
- int day = 0;//记录考勤日期
- int hour = 0;//记录考勤小时
- int minute = 0;//记录考勤分钟
- int second = 0;//记录考勤秒
- int workCode = 0; //记录workCode
- List<string> cl_cardcode = new List<string>();
- List<string> cl_time = new List<string>();
- List<string> cl_machinenum = new List<string>();
- StringBuilder sql = new StringBuilder();
- sql.Append("insert into cardlog (cl_id,cl_cardcode,cl_time,cl_address,cl_code) values (cardlog_seq.nextval,:cl_emcode,TO_DATE(:cl_time,'yyyy-MM-dd hh24:mi:ss'),'考勤机数据导入',:cl_machinenum)");
- int count = 0;
- while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out enrollNumber, out verifyCode, out inoutMode, out year, out month, out day, out hour, out minute, out second, ref workCode))
- {
- string logtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
- if (Convert.ToDateTime(logtime) > Convert.ToDateTime(LastDate))
- {
- cl_cardcode.Add(enrollNumber);
- cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
- cl_machinenum.Add(SerialNum);
- if (count % 1000 == 0)
- {
- dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
- cl_cardcode.Clear();
- cl_time.Clear();
- cl_machinenum.Clear();
- }
- count = count + 1;
- //将数据加入DataTable中
- DataRow dr = dt.NewRow();
- dr["cl_cardcode"] = enrollNumber;
- dr["cl_time"] = logtime;
- dt.Rows.Add(dr);
- }
- }
- if (cl_time.Count > 0)
- dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
- showDataGrid.DataSource = dt;
- if (dt.Rows.Count > 0)
- {
- MessageBox.Show("数据同步成功");
- }
- else
- {
- MessageBox.Show("没有需要同步的数据");
- }
- }
- else
- {
- axCZKEM1.GetLastError(ref idwErrorCode);
- MessageBox.Show("Unable to get data,ErrorCode=" + idwErrorCode.ToString(), "Error");
- }
- }
- private void showDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
- {
- }
- private void ExportEmp_Click(object sender, EventArgs e)
- {
- if (!bIsConnected)
- {
- MessageBox.Show("请先连接设备", "Error");
- return;
- }
- folderBrowserDialog1.Description = "选择导出的路径";
- DialogResult result = folderBrowserDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- string FolderPath = folderBrowserDialog1.SelectedPath;
- string EMenrollNumber = "";
- string Name = "";
- string Pwd = "";
- int Privilege = 0;
- bool Enable = false;
- DataTable dt = new DataTable();
- dt.Columns.Add("卡号");
- dt.Columns.Add("姓名");
- dt.Columns.Add("密码");
- dt.Columns.Add("权限");
- dt.Columns.Add("是否可用");
- while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out EMenrollNumber, out Name, out Pwd, out Privilege, out Enable))
- {
- DataRow dr = dt.NewRow();
- dr["卡号"] = EMenrollNumber;
- dr["姓名"] = Name;
- dr["密码"] = Pwd;
- dr["权限"] = Privilege;
- dr["是否可用"] = Enable;
- dt.Rows.Add(dr);
- }
- ExcelHandler ex = new ExcelHandler();
- ex.ExportExcel(dt, FolderPath);
- }
- }
- }
- }
|