SQLiteHelper.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. using SXLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.SQLite;
  8. namespace MOKA_Factory_Tools
  9. {
  10. class SQLiteHelper
  11. {
  12. /// <summary>
  13. /// 新建数据库文件
  14. /// </summary>
  15. /// <param name="dbPath">数据库文件路径及名称</param>
  16. /// <returns>新建成功,返回true,否则返回false</returns>
  17. static public Boolean NewDbFile(string dbPath)
  18. {
  19. try
  20. {
  21. SQLiteConnection.CreateFile(dbPath);
  22. return true;
  23. }
  24. catch (Exception ex)
  25. {
  26. throw new Exception("Create DB" + dbPath + "fail:" + ex.Message);
  27. }
  28. }
  29. /// <summary>
  30. /// 创建表
  31. /// </summary>
  32. /// <param name="dbPath">指定数据库文件</param>
  33. /// <param name="tableName">表名称</param>
  34. static public void NewTable(string dbPath, string tableName,string ColumnMessage)
  35. {
  36. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  37. if (sqliteConn.State != System.Data.ConnectionState.Open)
  38. {
  39. sqliteConn.Open();
  40. SQLiteCommand cmd = new SQLiteCommand();
  41. cmd.Connection = sqliteConn;
  42. cmd.CommandText = "CREATE TABLE " + tableName + ColumnMessage;
  43. cmd.ExecuteNonQuery();
  44. }
  45. sqliteConn.Close();
  46. sqliteConn.Dispose();
  47. }
  48. /// <summary>
  49. /// mid表插入一行
  50. /// </summary>
  51. /// <param name="dbPath"></param>
  52. /// <param name="param"></param>
  53. static public void AddOneLine(string dbPath,object[] param)
  54. {
  55. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  56. if (sqliteConn.State != System.Data.ConnectionState.Open)
  57. {
  58. sqliteConn.Open();
  59. SQLiteCommand cmd = new SQLiteCommand();
  60. cmd.Connection = sqliteConn;
  61. cmd.CommandText = string.Format("insert into mid(bid,number,pid,ctype,version,host,purl,psize,pmd5,status) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9]);
  62. cmd.ExecuteNonQuery();
  63. }
  64. sqliteConn.Close();
  65. sqliteConn.Dispose();
  66. }
  67. /// <summary>
  68. /// roku表插入一行
  69. /// </summary>
  70. /// <param name="dbPath"></param>
  71. /// <param name="param"></param>
  72. static public void AddRokuOneLine(string dbPath, object[] param)
  73. {
  74. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  75. if (sqliteConn.State != System.Data.ConnectionState.Open)
  76. {
  77. sqliteConn.Open();
  78. SQLiteCommand cmd = new SQLiteCommand();
  79. cmd.Connection = sqliteConn;
  80. cmd.CommandText = string.Format("insert into rokuCustomer(ordernum,region,brand,oemmodel,supporturl,supportphone,productiondate,remotetype) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7]);
  81. cmd.ExecuteNonQuery();
  82. }
  83. sqliteConn.Close();
  84. sqliteConn.Dispose();
  85. }
  86. /// <summary>
  87. /// whitebalance表插入一行
  88. /// </summary>
  89. /// <param name="dbPath"></param>
  90. /// <param name="param"></param>
  91. static public void AddwbOneLine(string dbPath, object[] param)
  92. {
  93. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  94. if (sqliteConn.State != System.Data.ConnectionState.Open)
  95. {
  96. sqliteConn.Open();
  97. SQLiteCommand cmd = new SQLiteCommand();
  98. cmd.Connection = sqliteConn;
  99. cmd.CommandText = string.Format("insert into whitebalance(ordernum,hdmirgain,hdmiggain,hdmibgain,nrgain,nggain,nbgain,lrgain,lggain,lbgain) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9]);
  100. cmd.ExecuteNonQuery();
  101. }
  102. sqliteConn.Close();
  103. sqliteConn.Dispose();
  104. }
  105. /// <summary>
  106. /// dsn表插入一行
  107. /// </summary>
  108. /// <param name="dbPath"></param>
  109. /// <param name="param"></param>
  110. static public void AdddsnOneLine(string dbPath, object[] param)
  111. {
  112. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  113. if (sqliteConn.State != System.Data.ConnectionState.Open)
  114. {
  115. sqliteConn.Open();
  116. SQLiteCommand cmd = new SQLiteCommand();
  117. cmd.Connection = sqliteConn;
  118. cmd.CommandText = string.Format("insert into dsn(ordernum,dsn) values('{0}','{1}')", param[0], param[1]);
  119. cmd.ExecuteNonQuery();
  120. }
  121. sqliteConn.Close();
  122. sqliteConn.Dispose();
  123. }
  124. static public void UpdateTime(string dbPath, string bid)
  125. {
  126. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  127. if (sqliteConn.State != System.Data.ConnectionState.Open)
  128. {
  129. sqliteConn.Open();
  130. SQLiteCommand cmd = new SQLiteCommand();
  131. cmd.Connection = sqliteConn;
  132. cmd.CommandText = string.Format("update mid set finish_date=datetime(CURRENT_TIMESTAMP,'localtime') where bid='{0}'",bid);
  133. cmd.ExecuteNonQuery();
  134. }
  135. sqliteConn.Close();
  136. sqliteConn.Dispose();
  137. }
  138. static public void UpdateStatus(string dbPath, string str)
  139. {
  140. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  141. if (sqliteConn.State != System.Data.ConnectionState.Open)
  142. {
  143. sqliteConn.Open();
  144. SQLiteCommand cmd = new SQLiteCommand();
  145. cmd.Connection = sqliteConn;
  146. cmd.CommandText = string.Format("update mid set status='{0}'", str);
  147. cmd.ExecuteNonQuery();
  148. }
  149. sqliteConn.Close();
  150. sqliteConn.Dispose();
  151. }
  152. /// <summary>
  153. /// 使用事务批量插入sn,key行
  154. /// </summary>
  155. /// <param name="dbPath"></param>
  156. /// <param name="keys"></param>
  157. /// <returns></returns>
  158. static public bool InsertKeys(string dbPath, List<object[]> keys)
  159. {
  160. DbTransaction trans = null;
  161. try
  162. {
  163. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  164. if (sqliteConn.State != System.Data.ConnectionState.Open)
  165. {
  166. sqliteConn.Open();
  167. SQLiteCommand cmd = new SQLiteCommand();
  168. cmd.Connection = sqliteConn;
  169. trans = sqliteConn.BeginTransaction();
  170. foreach (var key in keys)
  171. {
  172. cmd.CommandText = string.Format("insert into keys(sn,keys) values('{0}','{1}')", key[0], key[1]);
  173. cmd.ExecuteNonQuery();
  174. }
  175. trans.Commit();
  176. }
  177. sqliteConn.Close();
  178. sqliteConn.Dispose();
  179. return true;
  180. }
  181. catch(Exception ex)
  182. {
  183. if (trans != null)
  184. trans.Rollback();
  185. Log.WriteErrorLog("\r\nFail to transfer key to DB:\r\n" + ex.Message);
  186. return false;
  187. }
  188. }
  189. /// <summary>
  190. /// 插入一条sn/key数据
  191. /// </summary>
  192. /// <param name="sqliteConn"></param>
  193. /// <param name="sn"></param>
  194. /// <param name="keydata"></param>
  195. /// <returns></returns>
  196. static public bool Insertonekey(SQLiteConnection sqliteConn,string sn,string keydata)
  197. {
  198. DbTransaction trans = null;
  199. try
  200. {
  201. SQLiteCommand cmd = new SQLiteCommand();
  202. cmd.Connection = sqliteConn;
  203. trans = sqliteConn.BeginTransaction();
  204. cmd.CommandText = string.Format("insert into keys(sn,keys,copy_date) values('{0}','{1}','{2}')", sn, keydata, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  205. cmd.ExecuteNonQuery();
  206. trans.Commit();
  207. return true;
  208. }
  209. catch (Exception ex)
  210. {
  211. if (trans != null)
  212. trans.Rollback();
  213. Log.WriteErrorLog("\r\nFail to transfer key to DB:\r\n" + ex.Message);
  214. return false;
  215. }
  216. }
  217. static public bool CheckDownloadStatus(string dbPath,string bid)
  218. {
  219. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  220. if (sqliteConn.State != System.Data.ConnectionState.Open)
  221. {
  222. sqliteConn.Open();
  223. SQLiteCommand cmd = new SQLiteCommand();
  224. cmd.Connection = sqliteConn;
  225. cmd.CommandText = string.Format("select * from mid where bid='{0}'and status='1'", bid);
  226. using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
  227. {
  228. if(dr.Read())
  229. {
  230. return true;
  231. }
  232. }
  233. }
  234. sqliteConn.Close();
  235. sqliteConn.Dispose();
  236. return false;
  237. }
  238. static public bool WholeCheckDownloadStatus(SQLiteConnection sqliteConn, string bid)
  239. {
  240. SQLiteCommand cmd = new SQLiteCommand();
  241. cmd.Connection = sqliteConn;
  242. cmd.CommandText = string.Format("select * from mid where bid='{0}'and status='1'", bid);
  243. SQLiteDataReader dr = cmd.ExecuteReader();
  244. if (dr.Read())
  245. {
  246. dr.Close();
  247. return true;
  248. }
  249. dr.Close();
  250. return false;
  251. }
  252. /// <summary>
  253. /// 获取DB文件内的key数据(json格式)
  254. /// </summary>
  255. /// <param name="sqliteConn"></param>
  256. /// <param name="SN"></param>
  257. /// <param name="copydone"></param>
  258. /// <returns></returns>
  259. static public string Getkeys(SQLiteConnection sqliteConn, string SN,out bool copydone)
  260. {
  261. copydone = false;
  262. SQLiteCommand cmd = new SQLiteCommand();
  263. cmd.Connection = sqliteConn;
  264. cmd.CommandText = string.Format("select * from keys where sn='{0}'", SN);
  265. SQLiteDataReader dr = cmd.ExecuteReader();
  266. if (dr.Read())
  267. {
  268. NameValueCollection user = dr.GetValues();
  269. string values = user.Get("keys");
  270. if (user.Get("copy_date").Length>0)
  271. copydone = true;
  272. dr.Close();
  273. return values;
  274. }
  275. dr.Close();
  276. return "";
  277. }
  278. /// <summary>
  279. /// 获取DB文件内的roku抄写数据
  280. /// </summary>
  281. /// <param name="sqliteConn"></param>
  282. /// <param name="order"></param>
  283. /// <param name="rokuCustomer"></param>
  284. /// <returns></returns>
  285. static public bool GetrokuCustomer(SQLiteConnection sqliteConn, string order, out RokuCustomer rokuCustomer)
  286. {
  287. rokuCustomer = null;
  288. SQLiteCommand cmd = new SQLiteCommand();
  289. cmd.Connection = sqliteConn;
  290. cmd.CommandText = string.Format("select * from rokuCustomer where ordernum='{0}'", order);
  291. SQLiteDataReader dr = cmd.ExecuteReader();
  292. if (dr.Read())
  293. {
  294. NameValueCollection user = dr.GetValues();
  295. rokuCustomer = new RokuCustomer()
  296. {
  297. ordernum = user.Get("ordernum"),
  298. brand = user.Get("brand"),
  299. region = user.Get("region"),
  300. oemmodel = user.Get("oemmodel"),
  301. supporturl = user.Get("supporturl"),
  302. supportphone = user.Get("supportphone"),
  303. productiondate = user.Get("productiondate"),
  304. remotetype= user.Get("remotetype")
  305. };
  306. dr.Close();
  307. return true;
  308. }
  309. dr.Close();
  310. return false;
  311. }
  312. /// <summary>
  313. /// 获取DB文件内的dsn数据
  314. /// </summary>
  315. /// <param name="sqliteConn"></param>
  316. /// <param name="order"></param>
  317. /// <param name="rokuCustomer"></param>
  318. /// <returns></returns>
  319. static public bool Getdsn(SQLiteConnection sqliteConn, string order, out string dsn)
  320. {
  321. dsn = null;
  322. SQLiteCommand cmd = new SQLiteCommand();
  323. cmd.Connection = sqliteConn;
  324. cmd.CommandText = string.Format("select * from dsn where ordernum='{0}'", order);
  325. SQLiteDataReader dr = cmd.ExecuteReader();
  326. if (dr.Read())
  327. {
  328. NameValueCollection user = dr.GetValues();
  329. dsn = user.Get("dsn");
  330. dr.Close();
  331. return true;
  332. }
  333. dr.Close();
  334. return false;
  335. }
  336. /// <summary>
  337. /// 获取DB文件内的白平衡数据
  338. /// </summary>
  339. /// <param name="sqliteConn"></param>
  340. /// <param name="order"></param>
  341. /// <param name="whiteBalanceInfo"></param>
  342. /// <returns></returns>
  343. static public bool Getwhitebalance(SQLiteConnection sqliteConn, string order, out WhiteBalanceInfo whiteBalanceInfo)
  344. {
  345. whiteBalanceInfo = null;
  346. try
  347. {
  348. SQLiteCommand cmd = new SQLiteCommand();
  349. cmd.Connection = sqliteConn;
  350. cmd.CommandText = string.Format("select * from whitebalance where ordernum='{0}'", order);
  351. SQLiteDataReader dr = cmd.ExecuteReader();
  352. if (dr.Read())
  353. {
  354. NameValueCollection user = dr.GetValues();
  355. whiteBalanceInfo = new WhiteBalanceInfo()
  356. {
  357. ordernum = user.Get("ordernum"),
  358. hdmirgain = user.Get("hdmirgain"),
  359. hdmiggain = user.Get("hdmiggain"),
  360. hdmibgain = user.Get("hdmibgain"),
  361. nrgain = user.Get("nrgain"),
  362. nggain = user.Get("nggain"),
  363. nbgain = user.Get("nbgain"),
  364. lrgain = user.Get("lrgain"),
  365. lggain = user.Get("lggain"),
  366. lbgain = user.Get("lbgain")
  367. };
  368. dr.Close();
  369. return true;
  370. }
  371. dr.Close();
  372. return false;
  373. }
  374. catch(Exception ex)
  375. {
  376. Log.WriteErrorLog(ex.Message);
  377. return false;
  378. }
  379. }
  380. static public bool GetDBMidInfo(SQLiteConnection sqliteConn, string bid,out MidAddress MidAddress1)
  381. {
  382. MidAddress1 = new MidAddress();
  383. SQLiteCommand cmd = new SQLiteCommand();
  384. cmd.Connection = sqliteConn;
  385. cmd.CommandText = string.Format("select * from mid where bid='{0}'", bid);
  386. SQLiteDataReader dr = cmd.ExecuteReader();
  387. if (dr.Read())
  388. {
  389. NameValueCollection user = dr.GetValues();
  390. MidAddress1.order = user.Get("bid");
  391. MidAddress1.pid= user.Get("pid");
  392. MidAddress1.des = user.Get("des");
  393. MidAddress1.number = user.Get("number");
  394. MidAddress1.ctype = user.Get("ctype");
  395. MidAddress1.purl = user.Get("purl");
  396. MidAddress1.psize = user.Get("psize");
  397. MidAddress1.pmd5 = user.Get("pmd5");
  398. MidAddress1.version = user.Get("version");
  399. MidAddress1.host = user.Get("host");
  400. dr.Close();
  401. return true;
  402. }
  403. dr.Close();
  404. return false;
  405. }
  406. static public bool UpdateCopyStatus(SQLiteConnection sqliteConn,string SN,out string copydate)
  407. {
  408. SQLiteCommand cmd = new SQLiteCommand();
  409. cmd.Connection = sqliteConn;
  410. cmd.CommandText = "select datetime(CURRENT_TIMESTAMP,'localtime')";
  411. SQLiteDataReader dr = cmd.ExecuteReader();
  412. copydate = "";
  413. if (dr.Read())
  414. {
  415. NameValueCollection user = dr.GetValues();
  416. copydate = user.Get("datetime(CURRENT_TIMESTAMP,'localtime')");
  417. }
  418. dr.Close();
  419. cmd.CommandText = string.Format("update keys set copy_date='"+ copydate+ "'where sn='{0}'", SN);
  420. int result = cmd.ExecuteNonQuery();
  421. cmd.Dispose();
  422. if (result > 0 && copydate.Length > 0)
  423. return true;
  424. else
  425. return false;
  426. }
  427. static public bool UpdateReportStatus(SQLiteConnection sqliteConn, string SN)
  428. {
  429. SQLiteCommand cmd = new SQLiteCommand();
  430. cmd.Connection = sqliteConn;
  431. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  432. int result = cmd.ExecuteNonQuery();
  433. cmd.Dispose();
  434. if (result > 0)
  435. return true;
  436. else
  437. return false;
  438. }
  439. static public bool UpdateReportListStatus(SQLiteConnection sqliteConn, List<string> SNs)
  440. {
  441. DbTransaction trans = null;
  442. try
  443. {
  444. SQLiteCommand cmd = new SQLiteCommand();
  445. cmd.Connection = sqliteConn;
  446. trans = sqliteConn.BeginTransaction();
  447. foreach (var SN in SNs)
  448. {
  449. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  450. cmd.ExecuteNonQuery();
  451. }
  452. trans.Commit();
  453. cmd.Dispose();
  454. return true;
  455. }
  456. catch(Exception ex)
  457. {
  458. return false;
  459. }
  460. }
  461. static public bool InsertDelayCopyReport(SQLiteConnection sqliteConn, object[] param)
  462. {
  463. SQLiteCommand cmd = new SQLiteCommand();
  464. cmd.Connection = sqliteConn;
  465. cmd.CommandText = string.Format("insert into CopyDelayReport(bid,url,content) values('{0}','{1}','{2}')", param[0], param[1], param[2]);
  466. int result = cmd.ExecuteNonQuery();
  467. cmd.Dispose();
  468. if (result > 0)
  469. return true;
  470. else
  471. return false;
  472. }
  473. static public bool DeleteDelayCopyReport(SQLiteConnection sqliteConn, string ID)
  474. {
  475. SQLiteCommand cmd = new SQLiteCommand();
  476. cmd.Connection = sqliteConn;
  477. cmd.CommandText = string.Format("Delete from CopyDelayReport where ID='{0}'",ID);
  478. int result = cmd.ExecuteNonQuery();
  479. cmd.Dispose();
  480. if (result > 0)
  481. return true;
  482. else
  483. return false;
  484. }
  485. static public bool InsertDelayErrorReport(SQLiteConnection sqliteConn, object[] param)
  486. {
  487. SQLiteCommand cmd = new SQLiteCommand();
  488. cmd.Connection = sqliteConn;
  489. cmd.CommandText = string.Format("insert into ErrorReport(url,content) values('{0}','{1}')", param[0], param[1]);
  490. int result = cmd.ExecuteNonQuery();
  491. cmd.Dispose();
  492. if (result > 0)
  493. return true;
  494. else
  495. return false;
  496. }
  497. public static bool CheckProductionNum(SQLiteConnection sqliteConn, string order)
  498. {
  499. SQLiteCommand cmd = new SQLiteCommand();
  500. cmd.Connection = sqliteConn;
  501. cmd.CommandText = string.Format("select * from ProductionCount where bid='{0}'", order);
  502. SQLiteDataReader dr = cmd.ExecuteReader();
  503. if (dr.Read())
  504. {
  505. dr.Close();
  506. return true;
  507. }
  508. dr.Close();
  509. cmd.CommandText = string.Format("insert into ProductionCount(bid,count) values('{0}','{1}')", order, "0");
  510. int result = cmd.ExecuteNonQuery();
  511. cmd.Dispose();
  512. if (result > 0)
  513. return true;
  514. else
  515. return false;
  516. }
  517. public static bool UpdateProductionNum(SQLiteConnection sqliteConn, string order)
  518. {
  519. try
  520. {
  521. SQLiteCommand cmd = new SQLiteCommand();
  522. cmd.Connection = sqliteConn;
  523. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  524. SQLiteDataReader dr = cmd.ExecuteReader();
  525. if (dr.Read())
  526. {
  527. NameValueCollection user = dr.GetValues();
  528. string values = user.Get("count");
  529. string count = (Convert.ToInt32(values) + 1).ToString();
  530. dr.Close();
  531. cmd.CommandText = string.Format("update ProductionCount set count ='{0}' where bid='{1}'", count, order);
  532. int result = cmd.ExecuteNonQuery();
  533. if (result > 0)
  534. return true;
  535. else
  536. Log.WriteErrorLog("\r\nFail to update ProductionCount from db:\r\n" + cmd.CommandText);
  537. cmd.Dispose();
  538. return false;
  539. }
  540. dr.Close();
  541. Log.WriteErrorLog("\r\nFail to get ProductionCount from db:\r\n" + cmd.CommandText);
  542. cmd.Dispose();
  543. return false;
  544. }
  545. catch(Exception ex)
  546. {
  547. Log.WriteErrorLog("\r\nFail to UpdateProductionNum:" + ex.Message);
  548. return false;
  549. }
  550. }
  551. /// <summary>
  552. /// 获取本地DB对应订单已经抄写的数量(包含重复抄写)
  553. /// </summary>
  554. /// <param name="sqliteConn"></param>
  555. /// <param name="order"></param>
  556. /// <returns></returns>
  557. public static string GetProductionNum(SQLiteConnection sqliteConn, string order)
  558. {
  559. try
  560. {
  561. SQLiteCommand cmd = new SQLiteCommand();
  562. cmd.Connection = sqliteConn;
  563. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  564. SQLiteDataReader dr = cmd.ExecuteReader();
  565. if (dr.Read())
  566. {
  567. NameValueCollection user = dr.GetValues();
  568. string values = user.Get("count");
  569. dr.Close();
  570. cmd.Dispose();
  571. return values;
  572. }
  573. dr.Close();
  574. cmd.Dispose();
  575. return "";
  576. }
  577. catch (Exception ex)
  578. {
  579. Log.WriteErrorLog("\r\nFail to GetProductionNum:" + ex.Message);
  580. return "";
  581. }
  582. }
  583. /// <summary>
  584. /// 删除30天之前已经上报的数据
  585. /// </summary>
  586. /// <param name="sqliteConn"></param>
  587. /// <returns></returns>
  588. public static bool DeleteOldData(SQLiteConnection sqliteConn)
  589. {
  590. try
  591. {
  592. SQLiteCommand cmd = new SQLiteCommand();
  593. cmd.Connection = sqliteConn;
  594. cmd.CommandText = "Delete from ErrorReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  595. cmd.ExecuteNonQuery();
  596. cmd.CommandText = "Delete from CopyDelayReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  597. cmd.ExecuteNonQuery();
  598. cmd.Dispose();
  599. return true;
  600. }
  601. catch(Exception ex)
  602. {
  603. GC.Collect();
  604. Log.WriteErrorLog("\r\nFail to DeleteOldData:" + ex.Message);
  605. return false;
  606. }
  607. }
  608. /// <summary>
  609. /// 获取本地DB记录的未上报异常数据
  610. /// </summary>
  611. /// <param name="sqliteConn"></param>
  612. /// <param name="url"></param>
  613. /// <param name="content"></param>
  614. /// <returns></returns>
  615. public static bool GetErrorReportData(SQLiteConnection sqliteConn,out string url,out string content,out string id)
  616. {
  617. url = "";
  618. content = "";
  619. id = "";
  620. try
  621. {
  622. SQLiteCommand cmd = new SQLiteCommand();
  623. cmd.Connection = sqliteConn;
  624. cmd.CommandText = "select url,content,ID from ErrorReport where report_date is null LIMIT 1";
  625. SQLiteDataReader dr = cmd.ExecuteReader();
  626. if (dr.Read())
  627. {
  628. NameValueCollection user = dr.GetValues();
  629. url = user.Get("url");
  630. content = user.Get("content");
  631. id = user.Get("ID");
  632. dr.Close();
  633. return true;
  634. }
  635. dr.Close();
  636. return false;
  637. }
  638. catch(Exception ex)
  639. {
  640. GC.Collect();
  641. Log.WriteErrorLog("\r\nFail to GetErrorReportData:" + ex.Message);
  642. return false;
  643. }
  644. }
  645. /// <summary>
  646. /// 获取本地DB记录的未上报烧录数据
  647. /// </summary>
  648. /// <param name="sqliteConn"></param>
  649. /// <param name="url"></param>
  650. /// <param name="content"></param>
  651. /// <returns></returns>
  652. public static bool GetDelayReportData(SQLiteConnection sqliteConn, out string url, out string content,out string id)
  653. {
  654. url = "";
  655. content = "";
  656. id = "";
  657. try
  658. {
  659. SQLiteCommand cmd = new SQLiteCommand();
  660. cmd.Connection = sqliteConn;
  661. cmd.CommandText = "select url,content,ID from CopyDelayReport where report_date is null LIMIT 1";
  662. SQLiteDataReader dr = cmd.ExecuteReader();
  663. if (dr.Read())
  664. {
  665. NameValueCollection user = dr.GetValues();
  666. url = user.Get("url");
  667. content = user.Get("content");
  668. id = user.Get("ID");
  669. dr.Close();
  670. return true;
  671. }
  672. dr.Close();
  673. return false;
  674. }
  675. catch (Exception ex)
  676. {
  677. GC.Collect();
  678. Log.WriteErrorLog("\r\nFail to GetDelayReportData:" + ex.Message);
  679. return false;
  680. }
  681. }
  682. public static bool GetEppormDataList(SQLiteConnection sqliteConn, out List<PostDateClass> postList,out List<string> SNs)
  683. {
  684. postList = new List<PostDateClass>();
  685. try
  686. {
  687. SQLiteCommand cmd = new SQLiteCommand();
  688. SNs = new List<string>();
  689. cmd.Connection = sqliteConn;
  690. cmd.CommandText = "select sn from keys where report_date is null LIMIT 200";
  691. SQLiteDataReader dr = cmd.ExecuteReader();
  692. while (dr.Read())
  693. {
  694. NameValueCollection user = dr.GetValues();
  695. string sn = user.Get("sn");
  696. SNs.Add(sn);
  697. postList.Add(new PostDateClass("sn", sn));
  698. postList.Add(new PostDateClass("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  699. }
  700. dr.Close();
  701. return true;
  702. }
  703. catch (Exception ex)
  704. {
  705. GC.Collect();
  706. SNs = null;
  707. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  708. return false;
  709. }
  710. }
  711. public static bool GetEppormDataList2(SQLiteConnection sqliteConn, out List<PostDateClass> postList, out List<string> SNs)
  712. {
  713. postList = new List<PostDateClass>();
  714. try
  715. {
  716. SQLiteCommand cmd = new SQLiteCommand();
  717. SNs = new List<string>();
  718. cmd.Connection = sqliteConn;
  719. cmd.CommandText = "select sn,copy_date from keys where report_date is null and copy_date is not null LIMIT 20";
  720. SQLiteDataReader dr = cmd.ExecuteReader();
  721. while (dr.Read())
  722. {
  723. NameValueCollection user = dr.GetValues();
  724. string sn = user.Get("sn");
  725. string copy_date = user.Get("copy_date");
  726. SNs.Add(sn);
  727. postList.Add(new PostDateClass("sn", sn));
  728. postList.Add(new PostDateClass("date", copy_date));
  729. }
  730. dr.Close();
  731. return true;
  732. }
  733. catch (Exception ex)
  734. {
  735. GC.Collect();
  736. SNs = null;
  737. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  738. return false;
  739. }
  740. }
  741. public static bool GetEppormDataCount(SQLiteConnection sqliteConn, out string count)
  742. {
  743. count = "";
  744. try
  745. {
  746. SQLiteCommand cmd = new SQLiteCommand();
  747. cmd.Connection = sqliteConn;
  748. cmd.CommandText = "select count(*) from keys";
  749. SQLiteDataReader dr = cmd.ExecuteReader();
  750. if (dr.Read())
  751. {
  752. NameValueCollection user = dr.GetValues();
  753. count = user.Get("count(*)");
  754. dr.Close();
  755. return true;
  756. }
  757. dr.Close();
  758. return false;
  759. }
  760. catch (Exception ex)
  761. {
  762. GC.Collect();
  763. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  764. return false;
  765. }
  766. }
  767. public static bool GetEppormUploadedDataCount(SQLiteConnection sqliteConn, out string count)
  768. {
  769. count = "";
  770. try
  771. {
  772. SQLiteCommand cmd = new SQLiteCommand();
  773. cmd.Connection = sqliteConn;
  774. cmd.CommandText = "select count(*) from keys where report_date is not null";
  775. SQLiteDataReader dr = cmd.ExecuteReader();
  776. if (dr.Read())
  777. {
  778. NameValueCollection user = dr.GetValues();
  779. count = user.Get("count(*)");
  780. dr.Close();
  781. return true;
  782. }
  783. dr.Close();
  784. return false;
  785. }
  786. catch (Exception ex)
  787. {
  788. GC.Collect();
  789. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  790. return false;
  791. }
  792. }
  793. /// <summary>
  794. /// 更新本地DB记录的未上报数据
  795. /// </summary>
  796. public static bool UpdateReportData(SQLiteConnection sqliteConn,string table,string column,string data)
  797. {
  798. SQLiteCommand cmd = new SQLiteCommand();
  799. cmd.Connection = sqliteConn;
  800. cmd.CommandText = string.Format("update {0} set report_date = datetime(CURRENT_TIMESTAMP,'localtime') where {1}= '{2}'", table, column, data);
  801. int result = cmd.ExecuteNonQuery();
  802. cmd.Dispose();
  803. if (result > 0)
  804. return true;
  805. else
  806. return false;
  807. }
  808. }
  809. }