Form1.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Check.DataOperate;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using UAS_MES.PublicForm;
  11. namespace UAS_CheckWork
  12. {
  13. public partial class Form1 : Form
  14. {
  15. DataHelper dh;
  16. //Create Standalone SDK class dynamicly.
  17. public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
  18. private bool bIsConnected = false;//the boolean value identifies whether the device is connected
  19. private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.
  20. //开启线程
  21. Thread InitGetInfo;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. StartPosition = FormStartPosition.CenterScreen;
  26. }
  27. private void Form1_Load(object sender, EventArgs e)
  28. {
  29. CheckForIllegalCrossThreadCalls = false;//可跨线程
  30. txtIP.Text = Properties.Settings.Default.IP;
  31. txtPort.Text = Properties.Settings.Default.Port;
  32. dh = new DataHelper();
  33. }
  34. private void btnConnect_Click(object sender, EventArgs e)
  35. {
  36. Properties.Settings.Default.IP = txtIP.Text;
  37. Properties.Settings.Default.Port = txtPort.Text;
  38. Properties.Settings.Default.Save();
  39. if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
  40. {
  41. MessageBox.Show("请先输入IP和端口", "Error");
  42. return;
  43. }
  44. int idwErrorCode = 0;
  45. Cursor = Cursors.WaitCursor;
  46. if (btnConnect.Text == "断开连接")
  47. {
  48. axCZKEM1.Disconnect();
  49. bIsConnected = false;
  50. btnConnect.Text = "连接";
  51. lblState.Text = "状态:未连接";
  52. Cursor = Cursors.Default;
  53. return;
  54. }
  55. bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
  56. if (bIsConnected)
  57. {
  58. btnConnect.Text = "断开连接";
  59. btnConnect.Refresh();
  60. lblState.Text = "状态:已连接";
  61. 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.
  62. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  63. }
  64. else
  65. {
  66. axCZKEM1.GetLastError(ref idwErrorCode);
  67. MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
  68. }
  69. Cursor = Cursors.Default;
  70. }
  71. private void getAllData_Click(object sender, EventArgs e)
  72. {
  73. if (!bIsConnected)
  74. {
  75. MessageBox.Show("请先连接设备", "Error");
  76. return;
  77. }
  78. InitGetInfo = new Thread(InPrint);
  79. SetLoadingWindow stw = new SetLoadingWindow(InitGetInfo, "正在同步数据...");
  80. stw.StartPosition = FormStartPosition.CenterParent;
  81. stw.ShowDialog();
  82. }
  83. private void InPrint()
  84. {
  85. int idwErrorCode = 0;
  86. //获取所有的考勤记录
  87. if (axCZKEM1.ReadGeneralLogData(iMachineNumber))
  88. {
  89. //循环获取
  90. DataTable dt = new DataTable();
  91. dt.Columns.Add("cl_cardcode");
  92. dt.Columns.Add("cl_time");
  93. showDataGrid.DataSource = dt;
  94. string SerialNum = "";
  95. axCZKEM1.GetSerialNumber(axCZKEM1.MachineNumber, out SerialNum);
  96. string LastDate = dh.getFieldDataByCondition("cardlog", "to_char(max(cl_time),'yyyy-MM-dd HH24:mi:ss')", "cl_address='考勤机数据导入' and cl_code='" + SerialNum + "'").ToString();
  97. if (LastDate == "")
  98. {
  99. LastDate = "1990-01-01 00:00:00";
  100. }
  101. string enrollNumber = "";//记录用户ID
  102. int verifyCode = 0;//记录用户验证方式 0 为密码验证,1 为指纹验证,2 为卡验证
  103. int inoutMode = 0;//考勤状态,具体含义如下: 默认 0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out
  104. int year = 0;//记录考勤年份
  105. int month = 0;//记录考勤月份
  106. int day = 0;//记录考勤日期
  107. int hour = 0;//记录考勤小时
  108. int minute = 0;//记录考勤分钟
  109. int second = 0;//记录考勤秒
  110. int workCode = 0; //记录workCode
  111. List<string> cl_cardcode = new List<string>();
  112. List<string> cl_time = new List<string>();
  113. List<string> cl_machinenum = new List<string>();
  114. StringBuilder sql = new StringBuilder();
  115. 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)");
  116. int count = 0;
  117. 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))
  118. {
  119. string logtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  120. if (Convert.ToDateTime(logtime) > Convert.ToDateTime(LastDate))
  121. {
  122. cl_cardcode.Add(enrollNumber);
  123. cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
  124. cl_machinenum.Add(SerialNum);
  125. if (count % 1000 == 0)
  126. {
  127. dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
  128. cl_cardcode.Clear();
  129. cl_time.Clear();
  130. cl_machinenum.Clear();
  131. }
  132. count = count + 1;
  133. //将数据加入DataTable中
  134. DataRow dr = dt.NewRow();
  135. dr["cl_cardcode"] = enrollNumber;
  136. dr["cl_time"] = logtime;
  137. dt.Rows.Add(dr);
  138. }
  139. }
  140. if (cl_time.Count > 0)
  141. dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
  142. showDataGrid.DataSource = dt;
  143. if (dt.Rows.Count > 0)
  144. {
  145. MessageBox.Show("数据同步成功");
  146. }
  147. else
  148. {
  149. MessageBox.Show("没有需要同步的数据");
  150. }
  151. }
  152. else
  153. {
  154. axCZKEM1.GetLastError(ref idwErrorCode);
  155. MessageBox.Show("Unable to get data,ErrorCode=" + idwErrorCode.ToString(), "Error");
  156. }
  157. }
  158. private void showDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
  159. {
  160. }
  161. private void ExportEmp_Click(object sender, EventArgs e)
  162. {
  163. if (!bIsConnected)
  164. {
  165. MessageBox.Show("请先连接设备", "Error");
  166. return;
  167. }
  168. folderBrowserDialog1.Description = "选择导出的路径";
  169. DialogResult result = folderBrowserDialog1.ShowDialog();
  170. if (result == DialogResult.OK)
  171. {
  172. string FolderPath = folderBrowserDialog1.SelectedPath;
  173. string EMenrollNumber = "";
  174. string Name = "";
  175. string Pwd = "";
  176. int Privilege = 0;
  177. bool Enable = false;
  178. DataTable dt = new DataTable();
  179. dt.Columns.Add("卡号");
  180. dt.Columns.Add("姓名");
  181. dt.Columns.Add("密码");
  182. dt.Columns.Add("权限");
  183. dt.Columns.Add("是否可用");
  184. while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out EMenrollNumber, out Name, out Pwd, out Privilege, out Enable))
  185. {
  186. DataRow dr = dt.NewRow();
  187. dr["卡号"] = EMenrollNumber;
  188. dr["姓名"] = Name;
  189. dr["密码"] = Pwd;
  190. dr["权限"] = Privilege;
  191. dr["是否可用"] = Enable;
  192. dt.Rows.Add(dr);
  193. }
  194. ExcelHandler ex = new ExcelHandler();
  195. ex.ExportExcel(dt, FolderPath);
  196. }
  197. }
  198. }
  199. }