SQLiteHelper.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. Log.WriteErrorLog("\r\nFail to Get keys to DB:\r\n" + cmd.CommandText);
  277. return "";
  278. }
  279. /// <summary>
  280. /// 获取DB文件内的roku抄写数据
  281. /// </summary>
  282. /// <param name="sqliteConn"></param>
  283. /// <param name="order"></param>
  284. /// <param name="rokuCustomer"></param>
  285. /// <returns></returns>
  286. static public bool GetrokuCustomer(SQLiteConnection sqliteConn, string order, out RokuCustomer rokuCustomer)
  287. {
  288. rokuCustomer = null;
  289. SQLiteCommand cmd = new SQLiteCommand();
  290. cmd.Connection = sqliteConn;
  291. cmd.CommandText = string.Format("select * from rokuCustomer where ordernum='{0}'", order);
  292. SQLiteDataReader dr = cmd.ExecuteReader();
  293. if (dr.Read())
  294. {
  295. NameValueCollection user = dr.GetValues();
  296. rokuCustomer = new RokuCustomer()
  297. {
  298. ordernum = user.Get("ordernum"),
  299. brand = user.Get("brand"),
  300. region = user.Get("region"),
  301. oemmodel = user.Get("oemmodel"),
  302. supporturl = user.Get("supporturl"),
  303. supportphone = user.Get("supportphone"),
  304. productiondate = user.Get("productiondate"),
  305. remotetype= user.Get("remotetype")
  306. };
  307. dr.Close();
  308. return true;
  309. }
  310. dr.Close();
  311. return false;
  312. }
  313. /// <summary>
  314. /// 获取DB文件内的dsn数据
  315. /// </summary>
  316. /// <param name="sqliteConn"></param>
  317. /// <param name="order"></param>
  318. /// <param name="rokuCustomer"></param>
  319. /// <returns></returns>
  320. static public bool Getdsn(SQLiteConnection sqliteConn, string order, out string dsn)
  321. {
  322. dsn = null;
  323. SQLiteCommand cmd = new SQLiteCommand();
  324. cmd.Connection = sqliteConn;
  325. cmd.CommandText = string.Format("select * from dsn where ordernum='{0}'", order);
  326. SQLiteDataReader dr = cmd.ExecuteReader();
  327. if (dr.Read())
  328. {
  329. NameValueCollection user = dr.GetValues();
  330. dsn = user.Get("dsn");
  331. dr.Close();
  332. return true;
  333. }
  334. dr.Close();
  335. return false;
  336. }
  337. /// <summary>
  338. /// 获取DB文件内的白平衡数据
  339. /// </summary>
  340. /// <param name="sqliteConn"></param>
  341. /// <param name="order"></param>
  342. /// <param name="whiteBalanceInfo"></param>
  343. /// <returns></returns>
  344. static public bool Getwhitebalance(SQLiteConnection sqliteConn, string order, out WhiteBalanceInfo whiteBalanceInfo)
  345. {
  346. whiteBalanceInfo = null;
  347. try
  348. {
  349. SQLiteCommand cmd = new SQLiteCommand();
  350. cmd.Connection = sqliteConn;
  351. cmd.CommandText = string.Format("select * from whitebalance where ordernum='{0}'", order);
  352. SQLiteDataReader dr = cmd.ExecuteReader();
  353. if (dr.Read())
  354. {
  355. NameValueCollection user = dr.GetValues();
  356. whiteBalanceInfo = new WhiteBalanceInfo()
  357. {
  358. ordernum = user.Get("ordernum"),
  359. hdmirgain = user.Get("hdmirgain"),
  360. hdmiggain = user.Get("hdmiggain"),
  361. hdmibgain = user.Get("hdmibgain"),
  362. nrgain = user.Get("nrgain"),
  363. nggain = user.Get("nggain"),
  364. nbgain = user.Get("nbgain"),
  365. lrgain = user.Get("lrgain"),
  366. lggain = user.Get("lggain"),
  367. lbgain = user.Get("lbgain")
  368. };
  369. dr.Close();
  370. return true;
  371. }
  372. dr.Close();
  373. return false;
  374. }
  375. catch(Exception ex)
  376. {
  377. Log.WriteErrorLog(ex.Message);
  378. return false;
  379. }
  380. }
  381. static public bool GetDBMidInfo(SQLiteConnection sqliteConn, string bid,out MidAddress MidAddress1)
  382. {
  383. MidAddress1 = new MidAddress();
  384. SQLiteCommand cmd = new SQLiteCommand();
  385. cmd.Connection = sqliteConn;
  386. cmd.CommandText = string.Format("select * from mid where bid='{0}'", bid);
  387. SQLiteDataReader dr = cmd.ExecuteReader();
  388. if (dr.Read())
  389. {
  390. NameValueCollection user = dr.GetValues();
  391. MidAddress1.order = user.Get("bid");
  392. MidAddress1.pid= user.Get("pid");
  393. MidAddress1.des = user.Get("des");
  394. MidAddress1.number = user.Get("number");
  395. MidAddress1.ctype = user.Get("ctype");
  396. MidAddress1.purl = user.Get("purl");
  397. MidAddress1.psize = user.Get("psize");
  398. MidAddress1.pmd5 = user.Get("pmd5");
  399. MidAddress1.version = user.Get("version");
  400. MidAddress1.host = user.Get("host");
  401. dr.Close();
  402. return true;
  403. }
  404. dr.Close();
  405. return false;
  406. }
  407. static public bool UpdateCopyStatus(SQLiteConnection sqliteConn,string SN,out string copydate)
  408. {
  409. SQLiteCommand cmd = new SQLiteCommand();
  410. cmd.Connection = sqliteConn;
  411. cmd.CommandText = "select datetime(CURRENT_TIMESTAMP,'localtime')";
  412. SQLiteDataReader dr = cmd.ExecuteReader();
  413. copydate = "";
  414. if (dr.Read())
  415. {
  416. NameValueCollection user = dr.GetValues();
  417. copydate = user.Get("datetime(CURRENT_TIMESTAMP,'localtime')");
  418. }
  419. dr.Close();
  420. cmd.CommandText = string.Format("update keys set copy_date='"+ copydate+ "'where sn='{0}'", SN);
  421. int result = cmd.ExecuteNonQuery();
  422. cmd.Dispose();
  423. if (result > 0 && copydate.Length > 0)
  424. return true;
  425. else
  426. return false;
  427. }
  428. static public bool UpdateReportStatus(SQLiteConnection sqliteConn, string SN)
  429. {
  430. SQLiteCommand cmd = new SQLiteCommand();
  431. cmd.Connection = sqliteConn;
  432. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  433. int result = cmd.ExecuteNonQuery();
  434. cmd.Dispose();
  435. if (result > 0)
  436. return true;
  437. else
  438. return false;
  439. }
  440. static public bool UpdateReportListStatus(SQLiteConnection sqliteConn, List<string> SNs)
  441. {
  442. DbTransaction trans = null;
  443. try
  444. {
  445. SQLiteCommand cmd = new SQLiteCommand();
  446. cmd.Connection = sqliteConn;
  447. trans = sqliteConn.BeginTransaction();
  448. foreach (var SN in SNs)
  449. {
  450. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  451. cmd.ExecuteNonQuery();
  452. }
  453. trans.Commit();
  454. cmd.Dispose();
  455. return true;
  456. }
  457. catch(Exception ex)
  458. {
  459. return false;
  460. }
  461. }
  462. static public bool InsertDelayCopyReport(SQLiteConnection sqliteConn, object[] param)
  463. {
  464. SQLiteCommand cmd = new SQLiteCommand();
  465. cmd.Connection = sqliteConn;
  466. cmd.CommandText = string.Format("insert into CopyDelayReport(bid,url,content) values('{0}','{1}','{2}')", param[0], param[1], param[2]);
  467. int result = cmd.ExecuteNonQuery();
  468. cmd.Dispose();
  469. if (result > 0)
  470. return true;
  471. else
  472. return false;
  473. }
  474. static public bool DeleteDelayCopyReport(SQLiteConnection sqliteConn, string ID)
  475. {
  476. SQLiteCommand cmd = new SQLiteCommand();
  477. cmd.Connection = sqliteConn;
  478. cmd.CommandText = string.Format("Delete from CopyDelayReport where ID='{0}'",ID);
  479. int result = cmd.ExecuteNonQuery();
  480. cmd.Dispose();
  481. if (result > 0)
  482. return true;
  483. else
  484. return false;
  485. }
  486. static public bool InsertDelayErrorReport(SQLiteConnection sqliteConn, object[] param)
  487. {
  488. SQLiteCommand cmd = new SQLiteCommand();
  489. cmd.Connection = sqliteConn;
  490. cmd.CommandText = string.Format("insert into ErrorReport(url,content) values('{0}','{1}')", param[0], param[1]);
  491. int result = cmd.ExecuteNonQuery();
  492. cmd.Dispose();
  493. if (result > 0)
  494. return true;
  495. else
  496. return false;
  497. }
  498. public static bool CheckProductionNum(SQLiteConnection sqliteConn, string order)
  499. {
  500. SQLiteCommand cmd = new SQLiteCommand();
  501. cmd.Connection = sqliteConn;
  502. cmd.CommandText = string.Format("select * from ProductionCount where bid='{0}'", order);
  503. SQLiteDataReader dr = cmd.ExecuteReader();
  504. if (dr.Read())
  505. {
  506. dr.Close();
  507. return true;
  508. }
  509. dr.Close();
  510. cmd.CommandText = string.Format("insert into ProductionCount(bid,count) values('{0}','{1}')", order, "0");
  511. int result = cmd.ExecuteNonQuery();
  512. cmd.Dispose();
  513. if (result > 0)
  514. return true;
  515. else
  516. return false;
  517. }
  518. public static bool UpdateProductionNum(SQLiteConnection sqliteConn, string order)
  519. {
  520. try
  521. {
  522. SQLiteCommand cmd = new SQLiteCommand();
  523. cmd.Connection = sqliteConn;
  524. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  525. SQLiteDataReader dr = cmd.ExecuteReader();
  526. if (dr.Read())
  527. {
  528. NameValueCollection user = dr.GetValues();
  529. string values = user.Get("count");
  530. string count = (Convert.ToInt32(values) + 1).ToString();
  531. dr.Close();
  532. cmd.CommandText = string.Format("update ProductionCount set count ='{0}' where bid='{1}'", count, order);
  533. int result = cmd.ExecuteNonQuery();
  534. if (result > 0)
  535. return true;
  536. else
  537. Log.WriteErrorLog("\r\nFail to update ProductionCount from db:\r\n" + cmd.CommandText);
  538. cmd.Dispose();
  539. return false;
  540. }
  541. dr.Close();
  542. Log.WriteErrorLog("\r\nFail to get ProductionCount from db:\r\n" + cmd.CommandText);
  543. cmd.Dispose();
  544. return false;
  545. }
  546. catch(Exception ex)
  547. {
  548. Log.WriteErrorLog("\r\nFail to UpdateProductionNum:" + ex.Message);
  549. return false;
  550. }
  551. }
  552. /// <summary>
  553. /// 获取本地DB对应订单已经抄写的数量(包含重复抄写)
  554. /// </summary>
  555. /// <param name="sqliteConn"></param>
  556. /// <param name="order"></param>
  557. /// <returns></returns>
  558. public static string GetProductionNum(SQLiteConnection sqliteConn, string order)
  559. {
  560. try
  561. {
  562. SQLiteCommand cmd = new SQLiteCommand();
  563. cmd.Connection = sqliteConn;
  564. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  565. SQLiteDataReader dr = cmd.ExecuteReader();
  566. if (dr.Read())
  567. {
  568. NameValueCollection user = dr.GetValues();
  569. string values = user.Get("count");
  570. dr.Close();
  571. cmd.Dispose();
  572. return values;
  573. }
  574. dr.Close();
  575. cmd.Dispose();
  576. return "";
  577. }
  578. catch (Exception ex)
  579. {
  580. Log.WriteErrorLog("\r\nFail to GetProductionNum:" + ex.Message);
  581. return "";
  582. }
  583. }
  584. /// <summary>
  585. /// 删除30天之前已经上报的数据
  586. /// </summary>
  587. /// <param name="sqliteConn"></param>
  588. /// <returns></returns>
  589. public static bool DeleteOldData(SQLiteConnection sqliteConn)
  590. {
  591. try
  592. {
  593. SQLiteCommand cmd = new SQLiteCommand();
  594. cmd.Connection = sqliteConn;
  595. cmd.CommandText = "Delete from ErrorReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  596. cmd.ExecuteNonQuery();
  597. cmd.CommandText = "Delete from CopyDelayReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  598. cmd.ExecuteNonQuery();
  599. cmd.Dispose();
  600. return true;
  601. }
  602. catch(Exception ex)
  603. {
  604. GC.Collect();
  605. Log.WriteErrorLog("\r\nFail to DeleteOldData:" + ex.Message);
  606. return false;
  607. }
  608. }
  609. /// <summary>
  610. /// 获取本地DB记录的未上报异常数据
  611. /// </summary>
  612. /// <param name="sqliteConn"></param>
  613. /// <param name="url"></param>
  614. /// <param name="content"></param>
  615. /// <returns></returns>
  616. public static bool GetErrorReportData(SQLiteConnection sqliteConn,out string url,out string content,out string id)
  617. {
  618. url = "";
  619. content = "";
  620. id = "";
  621. try
  622. {
  623. SQLiteCommand cmd = new SQLiteCommand();
  624. cmd.Connection = sqliteConn;
  625. cmd.CommandText = "select url,content,ID from ErrorReport where report_date is null LIMIT 1";
  626. SQLiteDataReader dr = cmd.ExecuteReader();
  627. if (dr.Read())
  628. {
  629. NameValueCollection user = dr.GetValues();
  630. url = user.Get("url");
  631. content = user.Get("content");
  632. id = user.Get("ID");
  633. dr.Close();
  634. return true;
  635. }
  636. dr.Close();
  637. return false;
  638. }
  639. catch(Exception ex)
  640. {
  641. GC.Collect();
  642. Log.WriteErrorLog("\r\nFail to GetErrorReportData:" + ex.Message);
  643. return false;
  644. }
  645. }
  646. /// <summary>
  647. /// 获取本地DB记录的未上报烧录数据
  648. /// </summary>
  649. /// <param name="sqliteConn"></param>
  650. /// <param name="url"></param>
  651. /// <param name="content"></param>
  652. /// <returns></returns>
  653. public static bool GetDelayReportData(SQLiteConnection sqliteConn, out string url, out string content,out string id)
  654. {
  655. url = "";
  656. content = "";
  657. id = "";
  658. try
  659. {
  660. SQLiteCommand cmd = new SQLiteCommand();
  661. cmd.Connection = sqliteConn;
  662. cmd.CommandText = "select url,content,ID from CopyDelayReport where report_date is null LIMIT 1";
  663. SQLiteDataReader dr = cmd.ExecuteReader();
  664. if (dr.Read())
  665. {
  666. NameValueCollection user = dr.GetValues();
  667. url = user.Get("url");
  668. content = user.Get("content");
  669. id = user.Get("ID");
  670. dr.Close();
  671. return true;
  672. }
  673. dr.Close();
  674. return false;
  675. }
  676. catch (Exception ex)
  677. {
  678. GC.Collect();
  679. Log.WriteErrorLog("\r\nFail to GetDelayReportData:" + ex.Message);
  680. return false;
  681. }
  682. }
  683. public static bool GetEppormDataList(SQLiteConnection sqliteConn, out List<PostDateClass> postList,out List<string> SNs)
  684. {
  685. postList = new List<PostDateClass>();
  686. try
  687. {
  688. SQLiteCommand cmd = new SQLiteCommand();
  689. SNs = new List<string>();
  690. cmd.Connection = sqliteConn;
  691. cmd.CommandText = "select sn from keys where report_date is null LIMIT 200";
  692. SQLiteDataReader dr = cmd.ExecuteReader();
  693. while (dr.Read())
  694. {
  695. NameValueCollection user = dr.GetValues();
  696. string sn = user.Get("sn");
  697. SNs.Add(sn);
  698. postList.Add(new PostDateClass("sn", sn));
  699. postList.Add(new PostDateClass("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  700. }
  701. dr.Close();
  702. return true;
  703. }
  704. catch (Exception ex)
  705. {
  706. GC.Collect();
  707. SNs = null;
  708. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  709. return false;
  710. }
  711. }
  712. public static bool GetEppormDataList2(SQLiteConnection sqliteConn, out List<PostDateClass> postList, out List<string> SNs)
  713. {
  714. postList = new List<PostDateClass>();
  715. try
  716. {
  717. SQLiteCommand cmd = new SQLiteCommand();
  718. SNs = new List<string>();
  719. cmd.Connection = sqliteConn;
  720. cmd.CommandText = "select sn,copy_date from keys where report_date is null and copy_date is not null LIMIT 20";
  721. SQLiteDataReader dr = cmd.ExecuteReader();
  722. while (dr.Read())
  723. {
  724. NameValueCollection user = dr.GetValues();
  725. string sn = user.Get("sn");
  726. string copy_date = user.Get("copy_date");
  727. SNs.Add(sn);
  728. postList.Add(new PostDateClass("sn", sn));
  729. postList.Add(new PostDateClass("date", copy_date));
  730. }
  731. dr.Close();
  732. return true;
  733. }
  734. catch (Exception ex)
  735. {
  736. GC.Collect();
  737. SNs = null;
  738. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  739. return false;
  740. }
  741. }
  742. public static bool GetEppormDataCount(SQLiteConnection sqliteConn, out string count)
  743. {
  744. count = "";
  745. try
  746. {
  747. SQLiteCommand cmd = new SQLiteCommand();
  748. cmd.Connection = sqliteConn;
  749. cmd.CommandText = "select count(*) from keys";
  750. SQLiteDataReader dr = cmd.ExecuteReader();
  751. if (dr.Read())
  752. {
  753. NameValueCollection user = dr.GetValues();
  754. count = user.Get("count(*)");
  755. dr.Close();
  756. return true;
  757. }
  758. dr.Close();
  759. return false;
  760. }
  761. catch (Exception ex)
  762. {
  763. GC.Collect();
  764. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  765. return false;
  766. }
  767. }
  768. public static bool GetEppormUploadedDataCount(SQLiteConnection sqliteConn, out string count)
  769. {
  770. count = "";
  771. try
  772. {
  773. SQLiteCommand cmd = new SQLiteCommand();
  774. cmd.Connection = sqliteConn;
  775. cmd.CommandText = "select count(*) from keys where report_date is not null";
  776. SQLiteDataReader dr = cmd.ExecuteReader();
  777. if (dr.Read())
  778. {
  779. NameValueCollection user = dr.GetValues();
  780. count = user.Get("count(*)");
  781. dr.Close();
  782. return true;
  783. }
  784. dr.Close();
  785. return false;
  786. }
  787. catch (Exception ex)
  788. {
  789. GC.Collect();
  790. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  791. return false;
  792. }
  793. }
  794. /// <summary>
  795. /// 更新本地DB记录的未上报数据
  796. /// </summary>
  797. public static bool UpdateReportData(SQLiteConnection sqliteConn,string table,string column,string data)
  798. {
  799. SQLiteCommand cmd = new SQLiteCommand();
  800. cmd.Connection = sqliteConn;
  801. cmd.CommandText = string.Format("update {0} set report_date = datetime(CURRENT_TIMESTAMP,'localtime') where {1}= '{2}'", table, column, data);
  802. int result = cmd.ExecuteNonQuery();
  803. cmd.Dispose();
  804. if (result > 0)
  805. return true;
  806. else
  807. return false;
  808. }
  809. }
  810. }