LogicHandler.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using UAS_PLCDataReader.DataOperate;
  9. using UAS_PLCDataReader.Entity;
  10. namespace UAS_PLCDataReader.PublicMethod
  11. {
  12. class LogicHandler
  13. {
  14. public LogicHandler() { }
  15. static DataHelper dh = SystemInf.dh;
  16. //用于存放批量执行的SQL
  17. static List<string> sqls = new List<string>();
  18. /// <summary>
  19. /// 验证用户身份信息
  20. /// </summary>
  21. /// <param name="iUserCode"></param>
  22. /// <param name="oErrorMessage"></param>
  23. /// <returns></returns>
  24. public static bool CheckUserLogin(string iUserCode, string iPassWord, out string oErrorMessage)
  25. {
  26. oErrorMessage = "";
  27. string SQL = "select em_code from employee where upper(em_code)=:UserName and em_password =:PassWord";
  28. DataTable dt;
  29. dt = (DataTable)dh.ExecuteSql(SQL, "select", iUserCode.ToUpper(), iPassWord);
  30. if (dt.Rows.Count > 0)
  31. return true;
  32. else
  33. {
  34. oErrorMessage = "用户名或者密码不正确!";
  35. return false;
  36. }
  37. }
  38. public static bool CheckUserLogin(string UserName, string PassWord, string Master, out string oMsg)
  39. {
  40. oMsg = "";
  41. try
  42. {
  43. string url = DataHelper.ERPAddesss + "mobile/login.action";//html调用的地址
  44. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
  45. if (webrequest == null)
  46. {
  47. return false;
  48. }
  49. webrequest.Method = "POST";
  50. webrequest.Timeout = 1000;
  51. webrequest.ContentType = "application/x-www-form-urlencoded";
  52. System.Collections.Hashtable pars = new System.Collections.Hashtable();
  53. pars.Add("username", UserName);
  54. pars.Add("password", PassWord);
  55. pars.Add("master", Master);
  56. string buffer = "";
  57. //发送POST数据
  58. if (!(pars == null || pars.Count == 0))
  59. {
  60. foreach (string key in pars.Keys)
  61. {
  62. buffer = buffer + "&" + key + "=" + pars[key].ToString();
  63. }
  64. byte[] data = Encoding.UTF8.GetBytes(buffer);
  65. using (Stream stream = webrequest.GetRequestStream())
  66. {
  67. stream.Write(data, 0, data.Length);
  68. }
  69. }
  70. string[] values = webrequest.Headers.GetValues("Content-Type");
  71. WebResponse myResponse = webrequest.GetResponse();
  72. using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
  73. {
  74. StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  75. string Content = newReader.ReadToEnd();
  76. Dictionary<string, object> dic = new Dictionary<string, object>();
  77. dic = BaseUtil.ToDictionary(Content);
  78. if (!dic.ContainsKey("erpaccount"))
  79. {
  80. oMsg = dic["reason"].ToString();
  81. return false;
  82. }
  83. newReader.Close();
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. LogManager.DoLog(ex.Message.ToString());
  89. }
  90. return true;
  91. }
  92. /// <summary>
  93. /// 记录轮询日志,记录时间
  94. /// </summary>
  95. /// <param name="iDeCode">设备编号</param>
  96. /// <param name="iSendDataSize">发送数据大小</param>
  97. /// <param name="iReceiveDataSize">接收数据大小</param>
  98. /// <param name="iOnLine">上线或者离线</param>
  99. public static void DoDevicePollingLog(string iDeCode, bool iOnLine)
  100. {
  101. StringBuilder sql = new StringBuilder();
  102. sql.Clear();
  103. sql.Append("update DEVICEPOLLINGLOG set ");
  104. if (iOnLine)
  105. {
  106. sql.Append("dpg_onlinetime = sysdate,dpg_status='" + DeviceStatus.Running + "' where dpg_id=(select max(dpg_id)");
  107. sql.Append(" from DEVICEPOLLINGLOG where dpg_decode='" + iDeCode + "') and nvl(dpg_status,'disconnect')='disconnect'");
  108. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  109. sql.Clear();
  110. sql.Append("update DeviceRunTime set drt_onlinetime=sysdate where drt_id=(select min(drt_id) from DeviceRunTime ");
  111. sql.Append("where drt_decode='" + iDeCode + "' and drt_onlinetime is null)");
  112. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  113. //删除无用的重复数据
  114. sql.Clear();
  115. sql.Append("delete from DeviceRunTime where drt_decode='" + iDeCode + "' and drt_onlinetime is null");
  116. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  117. }
  118. else
  119. {
  120. sql.Append("dpg_offlinetime = sysdate,dpg_status='" + DeviceStatus.Disconnect + "' where dpg_id=(select max(dpg_id)");
  121. sql.Append(" from DEVICEPOLLINGLOG where dpg_decode='" + iDeCode + "') and dpg_status='running'");
  122. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  123. sql.Clear();
  124. sql.Append("insert into DeviceRunTime (drt_id,drt_decode,drt_offlinetime,drt_badcount) values(DeviceRunTime_seq.nextval,");
  125. sql.Append("'" + iDeCode + "',sysdate,(select count(1)+1 from DeviceRunTime where drt_decode='" + iDeCode + "' and drt_onlinetime is not null))");
  126. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  127. }
  128. }
  129. /// <summary>
  130. /// 记录轮询日志,记录时间
  131. /// </summary>
  132. /// <param name="iDeCode">设备编号</param>
  133. /// <param name="iSendDataSize">发送数据大小</param>
  134. /// <param name="iReceiveDataSize">接收数据大小</param>
  135. /// <param name="iOnLine">上线或者离线</param>
  136. public static void DoDevicePollingLog(string iDeCode, string iRemark)
  137. {
  138. StringBuilder sql = new StringBuilder();
  139. sql.Clear();
  140. sql.Append("update DEVICEPOLLINGLOG set dpg_remark='" + iRemark + "' where dpg_id=(select max(dpg_id) ");
  141. sql.Append("from DEVICEPOLLINGLOG where dpg_decode='" + iDeCode + "')");
  142. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  143. }
  144. /// <summary>
  145. /// 记录轮询日志,不记录时间
  146. /// </summary>
  147. /// <param name="iDeCode">设备编号</param>
  148. /// <param name="iSendDataSize">发送数据大小</param>
  149. /// <param name="iReceiveDataSize">接收数据大小</param>
  150. public static void DoDevicePollingLog(string iDeCode, string iSendDataSize, string iReceiveDataSize)
  151. {
  152. StringBuilder sql = new StringBuilder();
  153. sql.Clear();
  154. sql.Append("update DEVICEPOLLINGLOG set dpg_senddatasize=nvl(dpg_senddatasize,0)+" + iSendDataSize);
  155. sql.Append(",dpg_receivedatasize=nvl(dpg_receivedatasize,0)+" + iReceiveDataSize + ",dpg_count=nvl(dpg_count,0)+1 ");
  156. sql.Append(" where dpg_id=(select max(dpg_id) from DEVICEPOLLINGLOG where dpg_decode='" + iDeCode + "')");
  157. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  158. }
  159. /// <summary>
  160. /// 更新设备数据投入,产出等数据
  161. /// </summary>
  162. /// <param name="Decode"></param>
  163. /// <param name="ItemData"></param>
  164. public static void UpdateDeviceData(string Decode, Dictionary<string, string> ItemData)
  165. {
  166. if (ItemData == null)
  167. return;
  168. StringBuilder sql = new StringBuilder();
  169. StringBuilder sql1 = new StringBuilder();
  170. StringBuilder sql2 = new StringBuilder();
  171. //更新设备运行状态
  172. sql.Append("update DeviceRunstatus set ");
  173. //更新设备工作时间
  174. sql1.Append("merge into deviceworktime a using (select '' decode,sysdate time from dual) b on(trunc(a.dwt_indate)=trunc(b.time) and a.dwt_decode = b.decode) when matched then update set ");
  175. //不良代码索引
  176. int BadIndex = 0;
  177. foreach (var item in ItemData)
  178. {
  179. switch (item.Key)
  180. {
  181. case "INQTY":
  182. if (item.Value != "")
  183. {
  184. sql.Append("dr_qty='" + item.Value + "',");
  185. }
  186. break;
  187. case "OUTQTY":
  188. if (item.Value != "")
  189. {
  190. sql.Append("dr_okqty='" + item.Value + "',");
  191. }
  192. break;
  193. case "NGQTY":
  194. if (item.Value != "")
  195. {
  196. sql.Append("dr_ngqty='" + item.Value + "',");
  197. }
  198. break;
  199. case "TEMPERATURE":
  200. if (item.Value != "")
  201. {
  202. sql.Append("dr_TEMPERATURE='" + item.Value + "',");
  203. }
  204. break;
  205. case "PARAM1":
  206. if (item.Value != "")
  207. {
  208. sql.Append("dr_value1='" + item.Value + "',");
  209. }
  210. break;
  211. case "PARAM2":
  212. if (item.Value != "")
  213. {
  214. sql.Append("dr_value2='" + item.Value + "',");
  215. }
  216. break;
  217. case "PARAM3":
  218. if (item.Value != "")
  219. {
  220. sql.Append("dr_value3='" + item.Value + "',");
  221. }
  222. break;
  223. case "PARAM4":
  224. if (item.Value != "")
  225. {
  226. sql.Append("dr_value4='" + item.Value + "',");
  227. }
  228. break;
  229. case "PARAM5":
  230. if (item.Value != "")
  231. {
  232. sql.Append("dr_value5='" + item.Value + "',");
  233. }
  234. break;
  235. case "PARAM6":
  236. if (item.Value != "")
  237. {
  238. sql.Append("dr_value6='" + item.Value + "',");
  239. }
  240. break;
  241. case "PARAM7":
  242. if (item.Value != "")
  243. {
  244. sql.Append("dr_value7='" + item.Value + "',");
  245. }
  246. break;
  247. case "PARAM8":
  248. if (item.Value != "")
  249. {
  250. sql.Append("dr_value8='" + item.Value + "',");
  251. }
  252. break;
  253. case "PARAM9":
  254. if (item.Value != "")
  255. {
  256. sql.Append("dr_value9='" + item.Value + "',");
  257. }
  258. break;
  259. case "PARAM10":
  260. if (item.Value != "")
  261. {
  262. sql.Append("dr_value10='" + item.Value + "',");
  263. }
  264. break;
  265. case "WAITTIME":
  266. if (item.Value != "")
  267. {
  268. sql1.Append("dwt_waittime='" + item.Value + "',");
  269. }
  270. break;
  271. case "BREAKTIME":
  272. if (item.Value != "")
  273. {
  274. sql1.Append("dwt_breaktime='" + item.Value + "',");
  275. }
  276. break;
  277. case "MANSTOPTIME":
  278. if (item.Value != "")
  279. {
  280. sql1.Append("dwt_manstoptime='" + item.Value + "',");
  281. }
  282. break;
  283. case "WORKTIME":
  284. if (item.Value != "")
  285. {
  286. sql1.Append("dwt_worktime='" + item.Value + "',");
  287. }
  288. break;
  289. case "STARTTIME":
  290. if (item.Value != "")
  291. {
  292. sql1.Append("dwt_starttime='" + item.Value + "',");
  293. }
  294. break;
  295. case "RUNTIME":
  296. if (item.Value != "")
  297. {
  298. sql1.Append("dwt_runtime='" + item.Value + "',");
  299. }
  300. break;
  301. default:
  302. break;
  303. }
  304. if (item.Key.Contains("BADDETAIL"))
  305. {
  306. DataTable dt = (DataTable)dh.ExecuteSql("select bgd_badcode from badgroup left join BADGROUPDETAIL on bg_id=bgd_bgid where bg_code='"+ Decode + "' order by bgd_detno", "select");
  307. if(BadIndex<dt.Rows.Count)
  308. {
  309. string badcode = dt.Rows[BadIndex]["bgd_badcode"].ToString();
  310. sql2.Clear();
  311. sql2.Append("merge into makebad a using (select '"+ Decode + "' decode,sysdate time from dual)b ");
  312. sql2.Append("on(trunc(a.mb_indate)=trunc(b.time) and a.mb_badcode = '"+ badcode + "' and a.mb_decode='"+Decode+"') when matched then update set ");
  313. sql2.Append("mb_value='"+item.Value+ "',mb_updatetime=sysdate when not matched then insert (mb_id,mb_decode,mb_badcode,mb_value,mb_updatetime)values(deviceworktime_seq.nextval,'" + Decode+"', '"+ badcode + "', '" + item.Value+"',sysdate)");
  314. Console.WriteLine(Decode);
  315. Main.QueueUpdateDevice.Enqueue(sql2.ToString());
  316. }
  317. BadIndex = BadIndex + 1;
  318. }
  319. }
  320. //更新设别运行状态
  321. sql.Append("dr_updatedate=sysdate where dr_decode='" + Decode + "'");
  322. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  323. //更新设备工作时长
  324. sql1.Append("dwt_updatetime=sysdate when not matched then insert (dwt_id,dwt_decode,dwt_indate) values(deviceworktime_seq.nextval,'" + Decode + "',sysdate)");
  325. Main.QueueUpdateDevice.Enqueue(sql1.ToString());
  326. }
  327. /// <summary>
  328. /// 更新设备运行状态
  329. /// </summary>
  330. /// <param name="dh"></param>
  331. /// <param name="iDecode"></param>
  332. /// <param name="iDename"></param>
  333. /// <param name="iDestatus"></param>
  334. public static void UpdateDeviceStatus(string iDecode, string iDename, string iErrMessage, string iDestatus, int iType)
  335. {
  336. StringBuilder sql = new StringBuilder();
  337. if (iDestatus == DeviceStatus.Running)
  338. {
  339. sql.Clear();
  340. sql.Append("MERGE INTO DeviceRunstatus alias1 USING (select '" + iDecode + "' dr_decode from dual) alias2 ");
  341. sql.Append("ON (alias1.dr_decode=alias2.dr_decode) WHEN MATCHED THEN UPDATE SET dr_startdate =sysdate,Dr_runstatus='" + iDestatus + "',dr_getdatatime=(sysdate-dr_updatestatusdate)* 24 * 60 * 60*1000 ");
  342. sql.Append("WHEN NOT MATCHED THEN INSERT (Dr_id,dr_decode,dr_dename,Dr_runstatus,dr_startdate,dr_updatestatusdate) VALUES (DeviceRunstatus_seq.nextval,");
  343. sql.Append("'" + iDecode + "','" + iDename + "','" + iDestatus + "',sysdate,sysdate)");
  344. }
  345. else
  346. {
  347. sql.Clear();
  348. sql.Append("MERGE INTO DeviceRunstatus alias1 USING (select '" + iDecode + "' dr_decode from dual) alias2 ");
  349. sql.Append("ON (alias1.dr_decode=alias2.dr_decode) WHEN MATCHED THEN UPDATE SET dr_startdate=sysdate,Dr_runstatus='" + iDestatus + "',dr_updatestatusdate=sysdate ");
  350. sql.Append("WHEN NOT MATCHED THEN INSERT (Dr_id,dr_decode,dr_dename,Dr_runstatus,dr_startdate,dr_updatestatusdate) VALUES (DeviceRunstatus_seq.nextval,");
  351. sql.Append("'" + iDecode + "','" + iDename + "','" + iDestatus + "',sysdate,sysdate)");
  352. }
  353. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  354. if (iType == -1)
  355. {
  356. sql.Clear();
  357. sql.Append("insert into DeviceRunRecord(drr_id,drr_decode,drr_dename,drr_date,drr_doman,drr_remark,drr_logstatus,drr_type)");
  358. sql.Append("values(DeviceRunRecord_seq.nextval,'" + iDecode + "','" + iDename + "',sysdate,'" + User.UserName + "','" + iErrMessage.Replace("'", "") + "','" + iDestatus + "'," + iType + ")");
  359. Main.QueueUpdateDevice.Enqueue(sql.ToString());
  360. }
  361. }
  362. public static void DoCommandLog(string iUserCode, string iPageText, string iContent)
  363. {
  364. StringBuilder sql = new StringBuilder();
  365. sql.Clear();
  366. sql.Append("insert into commandlog(cL_id,cl_date,cl_man,cl_result,cl_caller)values(commandlog_seq.nextval,sysdate,'" + iUserCode + "','" + iContent + "','" + iPageText + "')");
  367. dh.ExecuteSql(sql.ToString(), "insert");
  368. }
  369. }
  370. }