SQLiteHelper.cs 35 KB

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