DataBase.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6. #include "../Config.h"
  7. #include "../Log/log.h"
  8. #include "../Define/define.h"
  9. #include "../ShareMemory/shmMem.h"
  10. //------------------------------------------------------------------------------
  11. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  12. #define DEDUCT_FILE "/Storage/ChargeLog/localDeductRecord.db"
  13. //------------------------------------------------------------------------------
  14. static sqlite3 *localDb;
  15. static sqlite3 *deductDb;
  16. //------------------------------------------------------------------------------
  17. //===============================================
  18. // SQLite3 related routine
  19. //===============================================
  20. int DB_Open(void)
  21. {
  22. int result = PASS;
  23. char *errMsg = NULL;
  24. char *createRecordSql = "CREATE TABLE IF NOT EXISTS charging_record("
  25. "idx integer primary key AUTOINCREMENT, "
  26. "reservationId text, "
  27. "transactionId text, "
  28. "startMethod text, "
  29. "userId text, "
  30. "dateTimeStart text, "
  31. "dateTimeStop text,"
  32. "socStart text, "
  33. "socStop text, "
  34. "chargeEnergy text, "
  35. "stopReason text"
  36. ");";
  37. /*char *createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  38. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  39. "`IsAvailable` TEXT NOT NULL, "
  40. "`connector` INTEGER NOT NULL, "
  41. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  42. */
  43. //DS60-120 add
  44. char *createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  45. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  46. "`item` TEXT NOT NULL, "
  47. "`connector` INTEGER NOT NULL, "
  48. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  49. char *createrecordSql = "CREATE TABLE IF NOT EXISTS `event_record` ( "
  50. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  51. "`occurDatetime` TEXT NOT NULL, "
  52. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  53. char *createrebootSql = "CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  54. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  55. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  56. if (sqlite3_open(DB_FILE, &localDb)) {
  57. result = FAIL;
  58. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  59. sqlite3_close(localDb);
  60. } else {
  61. log_info( "Local charging record database open successfully.");
  62. if (sqlite3_exec(localDb, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  63. result = FAIL;
  64. log_info( "Create local charging record table error message: %s", errMsg);
  65. } else {
  66. log_info( "Opened local charging record table successfully");
  67. }
  68. if (sqlite3_exec(localDb, createCfgSql, 0, 0, &errMsg) != SQLITE_OK) {
  69. result = FAIL;
  70. log_info( "Create local config table error message: %s", errMsg);
  71. } else {
  72. log_info( "Opened local config table successfully");
  73. }
  74. //DS60-120
  75. if (sqlite3_exec(localDb, createrecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  76. result = FAIL;
  77. log_info( "Create local record table error message: %s", errMsg);
  78. } else {
  79. log_info( "Opened local record table successfully");
  80. }
  81. if (sqlite3_exec(localDb, createrebootSql, 0, 0, &errMsg) != SQLITE_OK) {
  82. result = FAIL;
  83. log_info( "Create reboot record table error message: %s", errMsg);
  84. } else {
  85. log_info( "Opened reboot record table successfully");
  86. }
  87. //-----
  88. sqlite3_close(localDb);
  89. }
  90. return result;
  91. }
  92. int DB_Insert_Record(int gunIndex)
  93. {
  94. int result = PASS;
  95. char *errMsg = NULL;
  96. char insertSql[1024];
  97. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  98. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  99. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  100. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  101. pDcChargingInfo->ReservationId, //DS60-120 add
  102. ShmOCPP16Data->StartTransaction[gunIndex].ResponseTransactionId,
  103. pDcChargingInfo->StartMethod,
  104. pDcChargingInfo->StartUserId,
  105. pDcChargingInfo->StartDateTime,
  106. pDcChargingInfo->StopDateTime,
  107. pDcChargingInfo->EvBatteryStartSoc,
  108. pDcChargingInfo->EvBatterySoc,
  109. pDcChargingInfo->PresentChargedEnergy,
  110. ShmOCPP16Data->StopTransaction[gunIndex].StopReason);
  111. //if (sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db)) {
  112. if (sqlite3_open(DB_FILE, &localDb)) { //DS60-120 add
  113. result = FAIL;
  114. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  115. sqlite3_close(localDb);
  116. } else {
  117. //log_info( "Local charging record database open successfully.");
  118. if (sqlite3_exec(localDb, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  119. result = FAIL;
  120. log_info( "Insert local charging record error message: %s", errMsg);
  121. } else {
  122. //log_info( "Insert local charging record successfully");
  123. }
  124. //DS60-120 add
  125. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  126. if (sqlite3_exec(localDb, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  127. result = FAIL;
  128. log_info( "delete local charging error message: %s", errMsg);
  129. } else {
  130. //log_info( "delete local charging record successfully");
  131. }
  132. sqlite3_close(localDb);
  133. }
  134. return result;
  135. }
  136. int DB_Update_Operactive(uint8_t gunIndex, uint8_t IsAvailable)
  137. {
  138. uint8_t result = false;
  139. char *errMsg = NULL;
  140. char sqlStr[1024];
  141. srand(time(NULL));
  142. if (sqlite3_open(DB_FILE, &localDb)) {
  143. result = FAIL;
  144. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  145. sqlite3_close(localDb);
  146. } else {
  147. log_info( "Local charging record database open successfully (%d).", IsAvailable);
  148. //sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gunIndex, IsAvailable);
  149. sprintf(sqlStr,
  150. "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);",
  151. gunIndex,
  152. IsAvailable); //DS60-120 add
  153. log_info("sqlStr= %s", sqlStr);
  154. if (sqlite3_exec(localDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  155. result = FAIL;
  156. log_info( "update config error message: %s", errMsg);
  157. } else {
  158. log_info("update connector-%d config item isOperactive to %d", gunIndex, IsAvailable);
  159. }
  160. sqlite3_close(localDb);
  161. }
  162. return result;
  163. }
  164. int DB_Get_Operactive(uint8_t gunIndex)
  165. {
  166. uint8_t result = true;
  167. char *errMsg = NULL;
  168. char sqlStr[1024];
  169. char **rs;
  170. int rows, cols;
  171. //sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gunIndex);
  172. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gunIndex); //DS60-120 add
  173. //DEBUG_INFO("sqlStr= %s", sqlStr);
  174. if (sqlite3_open(DB_FILE, &localDb)) {
  175. result = FAIL;
  176. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  177. sqlite3_close(localDb);
  178. } else {
  179. log_info( "Local config query database open successfully.");
  180. sqlite3_get_table(localDb, sqlStr, &rs, &rows, &cols, &errMsg);
  181. if (rows > 0) {
  182. for (int idxRow = 1; idxRow <= rows; idxRow++) {
  183. if (strcmp(rs[(idxRow * cols) + 3], "0") == 0) {
  184. result = false;
  185. }
  186. log_info("Query connector-%d isOperactive: %s", gunIndex, rs[(idxRow * cols) + 3]);
  187. }
  188. } else {
  189. log_info("Query connector-%d fail, set default value to operactive.", gunIndex);
  190. }
  191. sqlite3_free_table(rs);
  192. sqlite3_close(localDb);
  193. }
  194. return result;
  195. }
  196. int DB_Reboot_Record(void)
  197. {
  198. int result = PASS;
  199. char *errMsg = NULL;
  200. char insertSql[256];
  201. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  202. if (sqlite3_open(DB_FILE, &localDb)) {
  203. result = FAIL;
  204. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  205. sqlite3_close(localDb);
  206. } else {
  207. log_info( "Local charging record database open successfully.");
  208. if (sqlite3_exec(localDb, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  209. result = FAIL;
  210. log_info( "Insert reboot record error message: %s", errMsg);
  211. } else {
  212. log_info( "Insert reboot record successfully");
  213. }
  214. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  215. if (sqlite3_exec(localDb, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  216. result = FAIL;
  217. log_info( "delete reboot record error message: %s", errMsg);
  218. } else {
  219. log_info( "delete reboot record successfully");
  220. }
  221. sqlite3_close(localDb);
  222. }
  223. return result;
  224. }
  225. //------------------------------------------------------------------------------
  226. //for Module_EventLog
  227. //------------------------------------------------------------------------------
  228. int CreateEventRecord(void)
  229. {
  230. int result = PASS;
  231. char *errMsg = NULL;
  232. char *createRecordSql = "CREATE TABLE IF NOT EXISTS event_record("
  233. "idx integer primary key AUTOINCREMENT, "
  234. "occurDatetime text NOT NULL, "
  235. "statusCode text NOT NULL"
  236. ");";
  237. if (sqlite3_open(DB_FILE, &localDb)) {
  238. result = FAIL;
  239. log_error( "Can't open database: %s", sqlite3_errmsg(localDb));
  240. sqlite3_close(localDb);
  241. } else {
  242. log_info( "Local event record database open successfully.");
  243. if (sqlite3_exec(localDb, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  244. result = FAIL;
  245. log_error( "Create local event record table error message: %s", errMsg);
  246. } else {
  247. log_info( "Opened local event record table successfully");
  248. }
  249. sqlite3_close(localDb);
  250. }
  251. return result;
  252. }
  253. int InsertEventRecord(uint8_t *statusCode)
  254. {
  255. int result = PASS;
  256. char *errMsg = NULL;
  257. char sqlStr[1024] = {0};
  258. sprintf(sqlStr, "insert into event_record(occurDatetime, statusCode) values(CURRENT_TIMESTAMP, '%s');", statusCode);
  259. if (sqlite3_open(DB_FILE, &localDb)) {
  260. result = FAIL;
  261. log_info( "Can't open database: %s", sqlite3_errmsg(localDb));
  262. sqlite3_close(localDb);
  263. } else {
  264. //log_info( "Local event record database open successfully.");
  265. if (sqlite3_exec(localDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  266. result = FAIL;
  267. log_info( "Insert local event record error message: %s", errMsg);
  268. } else {
  269. //log_info( "Insert local event record successfully");
  270. }
  271. sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
  272. if (sqlite3_exec(localDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  273. result = FAIL;
  274. log_info( "delete local event_record error message: %s", errMsg);
  275. } else {
  276. //log_info( "delete local event record successfully");
  277. }
  278. sqlite3_close(localDb);
  279. }
  280. return result;
  281. }
  282. int DeductDB_Open(void)
  283. {
  284. int result = PASS;
  285. char* errMsg = NULL;
  286. char* createDeductInfoSql="CREATE TABLE IF NOT EXISTS deduct_record("
  287. "idx integer primary key AUTOINCREMENT, "
  288. "gunIndex text, "
  289. "transactionId text, "
  290. "deductResult text, "
  291. "isDonate text, "
  292. "approvalNo text, "
  293. "cardNo text,"
  294. "vemData text, "
  295. "amount text, "
  296. "isUpload text"
  297. ");";
  298. if(sqlite3_open(DEDUCT_FILE, &deductDb))
  299. {
  300. result = FAIL;
  301. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  302. sqlite3_close(deductDb);
  303. }
  304. else
  305. {
  306. log_info("Deduct database open successfully.");
  307. if (sqlite3_exec(deductDb, createDeductInfoSql, 0, 0, &errMsg) != SQLITE_OK)
  308. {
  309. result = FAIL;
  310. log_info("Create deduct info table error message: %s", errMsg);
  311. }
  312. else
  313. {
  314. log_info("Opened deduct info table successfully");
  315. }
  316. sqlite3_close(deductDb);
  317. }
  318. return result;
  319. }
  320. int InsertDeductInfo(int gunIndex, RecordTransactionInfo *deductInfo)
  321. {
  322. int result = PASS;
  323. char *errMsg = NULL;
  324. char sqlStr[1024] = {0};
  325. char approNo[13];
  326. char carNo[21];
  327. char vemData[65];
  328. memset(approNo, 0x00, sizeof(approNo));
  329. memset(carNo, 0x00, sizeof(carNo));
  330. memset(vemData, 0x00, sizeof(vemData));
  331. memcpy(approNo, (char *)&deductInfo->ApprovalNo, sizeof(deductInfo->ApprovalNo));
  332. memcpy(carNo, (char *)&deductInfo->CardNo, sizeof(deductInfo->CardNo));
  333. memcpy(vemData, (char *)&deductInfo->VemData, sizeof(deductInfo->VemData));
  334. sprintf(sqlStr, "insert into deduct_record(gunIndex, transactionId, deductResult, isDonate, approvalNo, cardNo, vemData, amount, isUpload) "
  335. "values('%d', '%d', '%d', '%d', '%s', '%s', '%s', '%f', '%d');",
  336. gunIndex,
  337. deductInfo->TransactionId,
  338. deductInfo->DeductResult,
  339. deductInfo->IsDonateInvoice,
  340. approNo,
  341. carNo,
  342. vemData,
  343. deductInfo->Amount,
  344. deductInfo->IsUpload);
  345. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  346. {
  347. result = FAIL;
  348. log_info( "Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  349. sqlite3_close(deductDb);
  350. }
  351. else
  352. {
  353. //log_info( "Local event record database open successfully.");
  354. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  355. {
  356. result = FAIL;
  357. log_info( "Insert deduct record error message: %s", errMsg);
  358. }
  359. else
  360. {
  361. //log_info( "Insert local event record successfully");
  362. }
  363. sprintf(sqlStr, "delete from deduct_record where idx < (select idx from deduct_record order by idx desc limit 1)-10000;");
  364. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  365. {
  366. result = FAIL;
  367. log_info("delete deduct record error message: %s", errMsg);
  368. }
  369. else
  370. {
  371. //log_info("delete local charging record successfully");
  372. }
  373. sqlite3_close(deductDb);
  374. }
  375. return result;
  376. }
  377. int UpdateDeductInfoStatus(int gunIndex, RecordTransactionInfo *deductInfo)
  378. {
  379. int result = PASS;
  380. char *errMsg = NULL;
  381. char sqlStr[1024] = {0};
  382. sprintf(sqlStr, "update deduct_record set deductResult = %d, isUpload = %d where transactionId = %d;",
  383. deductInfo->DeductResult,
  384. deductInfo->IsUpload,
  385. deductInfo->TransactionId);
  386. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  387. {
  388. result = FAIL;
  389. log_info( "Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  390. sqlite3_close(deductDb);
  391. }
  392. else
  393. {
  394. //log_info( "Local event record database open successfully.");
  395. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  396. {
  397. result = FAIL;
  398. log_info( "update deduct record error message: %s", errMsg);
  399. }
  400. else
  401. {
  402. //log_info( "update deduct record successfully");
  403. }
  404. sqlite3_close(deductDb);
  405. }
  406. return result;
  407. }
  408. int DB_GetDeductInfo(int deductResult, int uploadState, int *gunIndex, RecordTransactionInfo *deductInfo)
  409. {
  410. int result = PASS;
  411. char *errMsg = NULL;
  412. char sqlStr[1024];
  413. char **rs;
  414. int rows, cols;
  415. if(deductResult >= 0 && uploadState >= 0)
  416. {
  417. sprintf(sqlStr, "select * from deduct_record where deductResult = %d and isUpload = %d;", deductResult > 0 ? 1 : 0, uploadState > 0 ? 1 : 0);
  418. }
  419. else if(deductResult >= 0 && uploadState < 0)
  420. {
  421. sprintf(sqlStr, "select * from deduct_record where deductResult = %d;", deductResult > 0 ? 1 : 0);
  422. }
  423. else if(deductResult < 0 && uploadState >= 0)
  424. {
  425. sprintf(sqlStr, "select * from deduct_record where isUpload = %d;", uploadState > 0 ? 1 : 0);
  426. }
  427. else
  428. {
  429. sprintf(sqlStr, "select * from deduct_record;");
  430. }
  431. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  432. {
  433. result = FAIL;
  434. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  435. sqlite3_close(deductDb);
  436. }
  437. else
  438. {
  439. sqlite3_get_table(deductDb, sqlStr, &rs, &rows, &cols, &errMsg);
  440. if (rows > 0)
  441. {
  442. for (int idxRow = 1; idxRow <= rows; idxRow++)
  443. {
  444. *gunIndex = atoi(rs[(idxRow * cols) + 1]);
  445. deductInfo->TransactionId = atoi(rs[(idxRow * cols) + 2]);
  446. deductInfo->DeductResult = atoi(rs[(idxRow * cols) + 3]);
  447. deductInfo->IsDonateInvoice = atoi(rs[(idxRow * cols) + 4]);
  448. strcpy((char *)&deductInfo->ApprovalNo, rs[(idxRow * cols) + 5]);
  449. strcpy((char *)&deductInfo->CardNo, rs[(idxRow * cols) + 6]);
  450. strcpy((char *)&deductInfo->VemData, rs[(idxRow * cols) + 7]);
  451. deductInfo->Amount = atoi(rs[(idxRow * cols) + 8]);
  452. deductInfo->IsUpload = atoi(rs[(idxRow * cols) + 9]);
  453. break;
  454. }
  455. }
  456. else
  457. {
  458. result = FAIL;
  459. }
  460. sqlite3_free_table(rs);
  461. sqlite3_close(deductDb);
  462. }
  463. return result;
  464. }
  465. int DB_GetReDeductInfo(int *gunIndex, RecordTransactionInfo *deductInfo)
  466. {
  467. int result = PASS;
  468. if(DB_GetDeductInfo(0, -1, gunIndex, deductInfo) == PASS)
  469. {
  470. //log_info("GetReDeductInfo OK");
  471. }
  472. else
  473. {
  474. result = FAIL;
  475. //log_info("GetReDeductInfo NG");
  476. }
  477. return result;
  478. }
  479. int DB_GetReUploadDeduct(int *gunIndex, RecordTransactionInfo *deductInfo)
  480. {
  481. int result = PASS;
  482. if(DB_GetDeductInfo(-1, 0, gunIndex, deductInfo) == PASS)
  483. {
  484. //log_info("GetReUploadDeduct OK");
  485. }
  486. else
  487. {
  488. result = FAIL;
  489. //log_info("GetReUploadDeduct NG");
  490. }
  491. return result;
  492. }
  493. // return quantity of DeductInfo
  494. int DB_GetMultiDeductInfo(int deductResult, int uploadState, int *gunIndex, RecordTransactionInfo *deductInfo)
  495. {
  496. char *errMsg = NULL;
  497. char sqlStr[1024];
  498. char **rs;
  499. int rows = 0, cols = 0;
  500. if(deductResult >= 0 && uploadState >= 0)
  501. {
  502. sprintf(sqlStr, "select * from deduct_record where deductResult = %d and isUpload = %d;", deductResult > 0 ? 1 : 0, uploadState > 0 ? 1 : 0);
  503. }
  504. else if(deductResult >= 0 && uploadState < 0)
  505. {
  506. sprintf(sqlStr, "select * from deduct_record where deductResult = %d;", deductResult > 0 ? 1 : 0);
  507. }
  508. else if(deductResult < 0 && uploadState >= 0)
  509. {
  510. sprintf(sqlStr, "select * from deduct_record where isUpload = %d;", uploadState > 0 ? 1 : 0);
  511. }
  512. else
  513. {
  514. sprintf(sqlStr, "select * from deduct_record;");
  515. }
  516. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  517. {
  518. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  519. sqlite3_close(deductDb);
  520. }
  521. else
  522. {
  523. sqlite3_get_table(deductDb, sqlStr, &rs, &rows, &cols, &errMsg);
  524. if (rows > 0)
  525. {
  526. for (int idxRow = 1; idxRow <= rows; idxRow++)
  527. {
  528. gunIndex[idxRow - 1] = atoi(rs[(idxRow * cols) + 1]);
  529. deductInfo[idxRow - 1].TransactionId = atoi(rs[(idxRow * cols) + 2]);
  530. deductInfo[idxRow - 1].DeductResult = atoi(rs[(idxRow * cols) + 3]);
  531. deductInfo[idxRow - 1].IsDonateInvoice = atoi(rs[(idxRow * cols) + 4]);
  532. strcpy((char *)&deductInfo[idxRow - 1].ApprovalNo, rs[(idxRow * cols) + 5]);
  533. strcpy((char *)&deductInfo[idxRow - 1].CardNo, rs[(idxRow * cols) + 6]);
  534. strcpy((char *)&deductInfo[idxRow - 1].VemData, rs[(idxRow * cols) + 7]);
  535. deductInfo[idxRow - 1].Amount = atoi(rs[(idxRow * cols) + 8]);
  536. deductInfo[idxRow - 1].IsUpload = atoi(rs[(idxRow * cols) + 9]);
  537. if(idxRow >= 128)
  538. {
  539. rows = 128;
  540. break;
  541. }
  542. }
  543. }
  544. sqlite3_free_table(rs);
  545. sqlite3_close(deductDb);
  546. }
  547. return rows;
  548. }
  549. int DB_GetMultiReDeductInfo(int *gunIndex, RecordTransactionInfo *deductInfo)
  550. {
  551. int quantity = 0;
  552. quantity = DB_GetMultiDeductInfo(0, -1, gunIndex, deductInfo);
  553. return quantity;
  554. }
  555. int DB_GetMultiReUploadDeduct(int *gunIndex, RecordTransactionInfo *deductInfo)
  556. {
  557. int quantity = 0;
  558. quantity = DB_GetMultiDeductInfo(-1, 0, gunIndex, deductInfo);
  559. return quantity;
  560. }
  561. void ReDeductTest()
  562. {
  563. int quantity = 0;
  564. int gunList[128];
  565. RecordTransactionInfo deductInfo[128];
  566. quantity = DB_GetMultiReDeductInfo(gunList, deductInfo);
  567. if(quantity > 0)
  568. {
  569. for(int i = 0; i < quantity; i++)
  570. {
  571. printf("Gun %d Rededuct CardNo: %s", gunList[i], deductInfo[i].CardNo);
  572. }
  573. }
  574. }