Form1.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. List<string> IPAddress = new List<string>();
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. StartPosition = FormStartPosition.CenterScreen;
  27. }
  28. private void Form1_Load(object sender, EventArgs e)
  29. {
  30. IPAddress.Add("192.168.2.230");
  31. IPAddress.Add("192.168.2.231");
  32. IPAddress.Add("192.168.2.232");
  33. IPAddress.Add("192.168.1.4");
  34. IPAddress.Add("192.168.1.241");
  35. IPAddress.Add("192.168.1.153");
  36. IPAddress.Add("192.168.1.182");
  37. IPAddress.Add("192.168.1.183");
  38. IPAddress.Add("192.168.1.75");
  39. IPAddress.Add("192.168.1.181");
  40. IPAddress.Add("192.168.1.219");
  41. IPAddress.Add("192.168.1.244");
  42. CheckForIllegalCrossThreadCalls = false;//可跨线程
  43. txtPort.Text = Properties.Settings.Default.Port;
  44. dh = new DataHelper();
  45. DataTable dt = (DataTable)dh.ExecuteSql("select ma_user,ma_function from master", "select");
  46. Master.DataSource = dt;
  47. Master.DisplayMember = "ma_function";
  48. Master.ValueMember = "ma_user";
  49. }
  50. private void getAllData_Click(object sender, EventArgs e)
  51. {
  52. string master = Master.SelectedValue.ToString();
  53. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + master + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yitoadt.wicp.io)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  54. dh = new DataHelper();
  55. int idwErrorCode = 0;
  56. for (int i = 0; i < IPAddress.Count; i++)
  57. {
  58. bIsConnected = axCZKEM1.Connect_Net(IPAddress.ToArray()[i], Convert.ToInt32(txtPort.Text));
  59. if (bIsConnected)
  60. {
  61. lblState.Text = "状态:已连接";
  62. 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.
  63. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  64. InitGetInfo = new Thread(InPrint);
  65. SetLoadingWindow stw = new SetLoadingWindow(InitGetInfo, "正在同步数据...");
  66. stw.StartPosition = FormStartPosition.CenterParent;
  67. stw.ShowDialog();
  68. }
  69. else
  70. {
  71. axCZKEM1.GetLastError(ref idwErrorCode);
  72. OperateResult.AppendText("无法连接设备,"+ IPAddress.ToArray()[i] + "ErrorCode=" + idwErrorCode.ToString() + "\n");
  73. }
  74. }
  75. }
  76. private void InPrint()
  77. {
  78. int idwErrorCode = 0;
  79. //获取所有的考勤记录
  80. if (axCZKEM1.ReadGeneralLogData(iMachineNumber))
  81. {
  82. //循环获取
  83. DataTable dt = new DataTable();
  84. dt.Columns.Add("cl_cardcode");
  85. dt.Columns.Add("cl_time");
  86. string SerialNum = "";
  87. axCZKEM1.GetSerialNumber(axCZKEM1.MachineNumber, out SerialNum);
  88. string LastDate = dh.getFieldDataByCondition("cardlog", "to_char(max(cl_time),'yyyy-MM-dd HH24:mi:ss')", "cl_address='考勤机数据导入' and cl_code='" + SerialNum + "'").ToString();
  89. if (LastDate == "")
  90. {
  91. LastDate = "1990-01-01 00:00:00";
  92. }
  93. string enrollNumber = "";//记录用户ID
  94. int verifyCode = 0;//记录用户验证方式 0 为密码验证,1 为指纹验证,2 为卡验证
  95. int inoutMode = 0;//考勤状态,具体含义如下: 默认 0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out
  96. int year = 0;//记录考勤年份
  97. int month = 0;//记录考勤月份
  98. int day = 0;//记录考勤日期
  99. int hour = 0;//记录考勤小时
  100. int minute = 0;//记录考勤分钟
  101. int second = 0;//记录考勤秒
  102. int workCode = 0; //记录workCode
  103. List<string> cl_cardcode = new List<string>();
  104. List<string> cl_time = new List<string>();
  105. List<string> cl_machinenum = new List<string>();
  106. StringBuilder sql = new StringBuilder();
  107. 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)");
  108. int count = 0;
  109. 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))
  110. {
  111. string logtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  112. if (Convert.ToDateTime(logtime) > Convert.ToDateTime(LastDate))
  113. {
  114. cl_cardcode.Add(enrollNumber);
  115. cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
  116. cl_machinenum.Add(SerialNum);
  117. if (count % 1000 == 0)
  118. {
  119. dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
  120. cl_cardcode.Clear();
  121. cl_time.Clear();
  122. cl_machinenum.Clear();
  123. }
  124. count = count + 1;
  125. //将数据加入DataTable中
  126. DataRow dr = dt.NewRow();
  127. dr["cl_cardcode"] = enrollNumber;
  128. dr["cl_time"] = logtime;
  129. dt.Rows.Add(dr);
  130. }
  131. }
  132. if (cl_time.Count > 0)
  133. dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray());
  134. if (dt.Rows.Count > 0)
  135. {
  136. OperateResult.AppendText(SerialNum + "数据同步成功\n");
  137. }
  138. else
  139. {
  140. OperateResult.AppendText(SerialNum + "没有需要同步的数据\n");
  141. }
  142. axCZKEM1.Disconnect();
  143. bIsConnected = false;
  144. }
  145. else
  146. {
  147. axCZKEM1.GetLastError(ref idwErrorCode);
  148. OperateResult.AppendText("Unable to get data,ErrorCode=" + idwErrorCode.ToString() + "\n");
  149. }
  150. }
  151. private void showDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
  152. {
  153. }
  154. private void ExportEmp_Click(object sender, EventArgs e)
  155. {
  156. if (!bIsConnected)
  157. {
  158. OperateResult.AppendText("请先连接设备\n");
  159. return;
  160. }
  161. folderBrowserDialog1.Description = "选择导出的路径";
  162. DialogResult result = folderBrowserDialog1.ShowDialog();
  163. if (result == DialogResult.OK)
  164. {
  165. string FolderPath = folderBrowserDialog1.SelectedPath;
  166. string EMenrollNumber = "";
  167. string Name = "";
  168. string Pwd = "";
  169. int Privilege = 0;
  170. bool Enable = false;
  171. DataTable dt = new DataTable();
  172. dt.Columns.Add("卡号");
  173. dt.Columns.Add("姓名");
  174. dt.Columns.Add("密码");
  175. dt.Columns.Add("权限");
  176. dt.Columns.Add("是否可用");
  177. while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out EMenrollNumber, out Name, out Pwd, out Privilege, out Enable))
  178. {
  179. DataRow dr = dt.NewRow();
  180. dr["卡号"] = EMenrollNumber;
  181. dr["姓名"] = Name;
  182. dr["密码"] = Pwd;
  183. dr["权限"] = Privilege;
  184. dr["是否可用"] = Enable;
  185. dt.Rows.Add(dr);
  186. }
  187. ExcelHandler ex = new ExcelHandler();
  188. ex.ExportExcel(dt, FolderPath);
  189. }
  190. }
  191. }
  192. }