DataBase.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. void CreditCardTimeSet(char *_time, RecordTransactionInfo* deductInfo)
  283. {
  284. _time[0] = '2';
  285. _time[1] = '0';
  286. _time[2] = (char)deductInfo->pCreditCard.TransDate[0];
  287. _time[3] = (char)deductInfo->pCreditCard.TransDate[1];
  288. _time[4] = '-';
  289. _time[5] = (char)deductInfo->pCreditCard.TransDate[2];
  290. _time[6] = (char)deductInfo->pCreditCard.TransDate[3];
  291. _time[7] = '-';
  292. _time[8] = (char)deductInfo->pCreditCard.TransDate[4];
  293. _time[9] = (char)deductInfo->pCreditCard.TransDate[5];
  294. _time[10] = ' ';
  295. _time[11] = (char)deductInfo->pCreditCard.TransTime[0];
  296. _time[12] = (char)deductInfo->pCreditCard.TransTime[1];
  297. _time[13] = ':';
  298. _time[14] = (char)deductInfo->pCreditCard.TransTime[2];
  299. _time[15] = (char)deductInfo->pCreditCard.TransTime[3];
  300. _time[16] = ':';
  301. _time[17] = (char)deductInfo->pCreditCard.TransTime[4];
  302. _time[18] = (char)deductInfo->pCreditCard.TransTime[5];
  303. _time[19] = '\0';
  304. }
  305. int DeductDB_Open(void)
  306. {
  307. int result = PASS;
  308. char* errMsg = NULL;
  309. char* createDeductInfoSql="CREATE TABLE IF NOT EXISTS report_credit_deduct("
  310. "idx integer primary key AUTOINCREMENT, "
  311. "gun_index text, "
  312. "reportDate text, "
  313. "vemData text, "
  314. "txId text, "
  315. "creditNo text, "
  316. "amount text, "
  317. "storeId text, "
  318. "approvalNo text, "
  319. "RRN text, "
  320. "ROC text, "
  321. "isDonateInvoice text, "
  322. "isDeductResult text, "
  323. "isIntoCharge text, "
  324. "isUpload text, "
  325. "lineSn text"
  326. "Energy text"
  327. ");";
  328. if(sqlite3_open(DEDUCT_FILE, &deductDb))
  329. {
  330. result = FAIL;
  331. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  332. sqlite3_close(deductDb);
  333. }
  334. else
  335. {
  336. log_info("Deduct database open successfully.");
  337. if (sqlite3_exec(deductDb, createDeductInfoSql, 0, 0, &errMsg) != SQLITE_OK)
  338. {
  339. result = FAIL;
  340. log_info("Create deduct info table error message: %s", errMsg);
  341. }
  342. else
  343. {
  344. log_info("Opened deduct info table successfully");
  345. }
  346. sqlite3_close(deductDb);
  347. }
  348. return result;
  349. }
  350. int InsertDeductInfo(int gunIndex, RecordTransactionInfo *deductInfo)
  351. {
  352. int result = PASS;
  353. char *errMsg = NULL;
  354. char sqlStr[1024] = {0};
  355. char approNo[13];
  356. char carNo[21];
  357. char vemData[65];
  358. char storeId[18];
  359. char RRN[12];
  360. char ROC[12];
  361. char lineSn[20];
  362. char _time[20];
  363. memset(approNo, '\0', sizeof(approNo));
  364. memset(carNo, '\0', sizeof(carNo));
  365. memset(vemData, '\0', sizeof(vemData));
  366. memset(storeId, '\0', sizeof(storeId));
  367. memset(RRN, '\0', sizeof(RRN));
  368. memset(ROC, '\0', sizeof(ROC));
  369. memset(lineSn, '\0', sizeof(lineSn));
  370. strncpy(approNo, (char *)&deductInfo->pCreditCard.ApprovalNo,9);
  371. strncpy(carNo, (char *)&deductInfo->pCreditCard.CardNo,20);
  372. strncpy(vemData, (char *)&deductInfo->pCreditCard.VemData,64);
  373. strncpy(storeId, (char *)&deductInfo->pCreditCard.StoreId,18 );
  374. strncpy(RRN,(char *)&deductInfo->pCreditCard.RRN,12);
  375. strncpy(ROC,(char *)&deductInfo->pCreditCard.ROC,12);
  376. CreditCardTimeSet(&_time, deductInfo);
  377. //sprintf(reportDate, "%s%s", deductInfo->pCreditCard.TransDate, deductInfo->pCreditCard.TransTime);
  378. sprintf(sqlStr, "insert into report_credit_deduct(gun_index, reportDate, vemData, txId, creditNo, amount, storeId, approvalNo, RRN, ROC, isDonateInvoice, isDeductResult, isIntoCharge, isUpload, lineSn, Energy) "
  379. "values('%d', '%s', '%s', '%d', '%s', '%.1f', '%s', '%s', '%s', '%s', '%d', '%d', '%d' , '%d', '%s' ,'%.1f');",
  380. gunIndex,
  381. _time,
  382. vemData,
  383. deductInfo->TransactionId,
  384. carNo,
  385. deductInfo->Amount,
  386. storeId,
  387. approNo,
  388. RRN,
  389. ROC,
  390. (int)deductInfo->IsDonateInvoice,
  391. (int)deductInfo->DeductResult,
  392. (int)deductInfo->isIntoCharge,
  393. (int)deductInfo->IsUpload,
  394. lineSn);
  395. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  396. {
  397. result = FAIL;
  398. log_info( "Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  399. sqlite3_close(deductDb);
  400. }
  401. else
  402. {
  403. //log_info( "Local event record database open successfully.");
  404. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  405. {
  406. result = FAIL;
  407. log_info( "Insert deduct record error message: %s", errMsg);
  408. }
  409. else
  410. {
  411. //log_info( "Insert local event record successfully");
  412. }
  413. sprintf(sqlStr, "delete from report_credit_deduct where idx < (select idx from report_credit_deduct order by idx desc limit 1)-10000;");
  414. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  415. {
  416. result = FAIL;
  417. log_info("delete deduct record error message: %s", errMsg);
  418. }
  419. else
  420. {
  421. //log_info("delete local charging record successfully");
  422. }
  423. sqlite3_close(deductDb);
  424. }
  425. return result;
  426. }
  427. int UpdateDeductInfoStatus(int gunIndex, RecordTransactionInfo *deductInfo)
  428. {
  429. int result = PASS;
  430. char *errMsg = NULL;
  431. char sqlStr[1024] = {0};
  432. char vemData[65] = {0};
  433. char _time[20] = { 0 };
  434. memset(vemData, '\0', 65);
  435. strncpy(vemData, (char*)&deductInfo->pCreditCard.VemData,64);
  436. sprintf(sqlStr, "update report_credit_deduct set isUpload = %d , isIntoCharge = %d, amount = %.1f, txId = %d, isDeductResult = %d, Energy = %.1f where vemData = '%s'; ",
  437. (int)deductInfo->IsUpload,
  438. (int)deductInfo->isIntoCharge,
  439. deductInfo->Amount,
  440. deductInfo->TransactionId,
  441. (int)deductInfo->DeductResult,
  442. deductInfo->Energy,
  443. vemData);
  444. log_info("%s",sqlStr);
  445. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  446. {
  447. result = FAIL;
  448. log_info( "Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  449. sqlite3_close(deductDb);
  450. }
  451. else
  452. {
  453. //log_info( "Local event record database open successfully.");
  454. if (sqlite3_exec(deductDb, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  455. {
  456. result = FAIL;
  457. log_info( "update deduct record error message: %s!", errMsg);
  458. }
  459. else
  460. {
  461. //log_info( "update deduct record successfully");
  462. }
  463. sqlite3_close(deductDb);
  464. }
  465. return result;
  466. }
  467. int DB_GetDeductInfo(int deductResult, int uploadState, int *gunIndex, RecordTransactionInfo *deductInfo)
  468. {
  469. int result = PASS;
  470. char *errMsg = NULL;
  471. char sqlStr[1024];
  472. char **rs;
  473. int rows, cols;
  474. if(deductResult >= 0 && uploadState >= 0)
  475. {
  476. sprintf(sqlStr, "select * from report_credit_deduct where isDeductResult = %d and isUpload = %d;", deductResult > 0 ? 1 : 0, uploadState > 0 ? 1 : 0);
  477. }
  478. else if(deductResult >= 0 && uploadState < 0)
  479. {
  480. sprintf(sqlStr, "select * from report_credit_deduct where isDeductResult = 0 OR isDeductResult = 3;");
  481. }
  482. else if(deductResult < 0 && uploadState >= 0)
  483. {
  484. sprintf(sqlStr, "select * from report_credit_deduct where isUpload = %d;", uploadState > 0 ? 1 : 0);
  485. }
  486. else
  487. {
  488. sprintf(sqlStr, "select * from report_credit_deduct;");
  489. }
  490. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  491. {
  492. result = FAIL;
  493. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  494. sqlite3_close(deductDb);
  495. }
  496. else
  497. {
  498. sqlite3_get_table(deductDb, sqlStr, &rs, &rows, &cols, &errMsg);
  499. if (rows > 0)
  500. {
  501. for (int idxRow = 1; idxRow <= rows; idxRow++)
  502. {
  503. *gunIndex = atoi(rs[(idxRow * cols) + 1]);
  504. deductInfo->TransactionId = atoi(rs[(idxRow * cols) + 2]);
  505. deductInfo->DeductResult = atoi(rs[(idxRow * cols) + 3]);
  506. deductInfo->IsDonateInvoice = atoi(rs[(idxRow * cols) + 4]);
  507. strcpy((char *)&deductInfo->pCreditCard.ApprovalNo, rs[(idxRow * cols) + 5]);
  508. strcpy((char *)&deductInfo->pCreditCard.CardNo, rs[(idxRow * cols) + 6]);
  509. strcpy((char *)&deductInfo->pCreditCard.VemData, rs[(idxRow * cols) + 7]);
  510. deductInfo->Amount = atoi(rs[(idxRow * cols) + 8]);
  511. deductInfo->IsUpload = atoi(rs[(idxRow * cols) + 9]);
  512. break;
  513. }
  514. }
  515. else
  516. {
  517. result = FAIL;
  518. }
  519. sqlite3_free_table(rs);
  520. sqlite3_close(deductDb);
  521. }
  522. return result;
  523. }
  524. int DB_GetReDeductInfo(int *gunIndex, RecordTransactionInfo *deductInfo)
  525. {
  526. int result = PASS;
  527. if(DB_GetDeductInfo(0, -1, gunIndex, deductInfo) == PASS)
  528. {
  529. //log_info("GetReDeductInfo OK");
  530. }
  531. else
  532. {
  533. result = FAIL;
  534. //log_info("GetReDeductInfo NG");
  535. }
  536. return result;
  537. }
  538. int DB_GetReUploadDeduct(int *gunIndex, RecordTransactionInfo *deductInfo)
  539. {
  540. int result = PASS;
  541. if(DB_GetDeductInfo(-1, 0, gunIndex, deductInfo) == PASS)
  542. {
  543. //log_info("GetReUploadDeduct OK");
  544. }
  545. else
  546. {
  547. result = FAIL;
  548. //log_info("GetReUploadDeduct NG");
  549. }
  550. return result;
  551. }
  552. void GetDeductTime(char* _time,RecordTransactionInfo* deductInfo)
  553. {
  554. deductInfo->pCreditCard.TransDate[0] = (unsigned char)_time[2];
  555. deductInfo->pCreditCard.TransDate[1] = (unsigned char)_time[3];
  556. deductInfo->pCreditCard.TransDate[2] = (unsigned char)_time[5];
  557. deductInfo->pCreditCard.TransDate[3] = (unsigned char)_time[6];
  558. deductInfo->pCreditCard.TransDate[4] = (unsigned char)_time[8];
  559. deductInfo->pCreditCard.TransDate[5] = (unsigned char)_time[9];
  560. deductInfo->pCreditCard.TransTime[0] = (unsigned char)_time[11];
  561. deductInfo->pCreditCard.TransTime[1] = (unsigned char)_time[12];
  562. deductInfo->pCreditCard.TransTime[2] = (unsigned char)_time[14];
  563. deductInfo->pCreditCard.TransTime[3] = (unsigned char)_time[15];
  564. deductInfo->pCreditCard.TransTime[4] = (unsigned char)_time[17];
  565. deductInfo->pCreditCard.TransTime[5] = (unsigned char)_time[18];
  566. }
  567. // return quantity of DeductInfo
  568. int DB_GetMultiDeductInfo(int deductResult, int uploadState, int *gunIndex, RecordTransactionInfo *deductInfo)
  569. {
  570. char *errMsg = NULL;
  571. char sqlStr[1024];
  572. char **rs;
  573. int rows = 0, cols = 0;
  574. if(deductResult >= 0 && uploadState >= 0)
  575. {
  576. sprintf(sqlStr, "select * from report_credit_deduct where isDeductResult = %d and isUpload = %d;", deductResult > 0 ? 1 : 0, uploadState > 0 ? 1 : 0);
  577. }
  578. else if(deductResult >= 0 && uploadState < 0)
  579. {
  580. sprintf(sqlStr, "select * from report_credit_deduct where isDeductResult = 0 OR isDeductResult = 3;");
  581. }
  582. else if(deductResult < 0 && uploadState >= 0)
  583. {
  584. sprintf(sqlStr, "select * from report_credit_deduct where isUpload = %d;", uploadState > 0 ? 1 : 0);
  585. }
  586. else
  587. {
  588. sprintf(sqlStr, "select * from report_credit_deduct;");
  589. }
  590. if (sqlite3_open(DEDUCT_FILE, &deductDb))
  591. {
  592. log_info("Can't open deduct database: %s", sqlite3_errmsg(deductDb));
  593. sqlite3_close(deductDb);
  594. }
  595. else
  596. {
  597. sqlite3_get_table(deductDb, sqlStr, &rs, &rows, &cols, &errMsg);
  598. if (rows > 0)
  599. {
  600. for (int idxRow = 1; idxRow <= rows; idxRow++)
  601. {
  602. gunIndex[idxRow - 1] = atoi(rs[(idxRow * cols) + 1]);
  603. // GetDeductTime(rs[(idxRow * cols) + 1], deductInfo);
  604. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.VemData, rs[(idxRow * cols) + 3],64);
  605. deductInfo[idxRow - 1].TransactionId = atoi(rs[(idxRow * cols) + 4]);
  606. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.CardNo, rs[(idxRow * cols) + 5],20);
  607. deductInfo[idxRow - 1].Amount = atoi(rs[(idxRow * cols) + 6]);
  608. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.StoreId, rs[(idxRow * cols) + 7],18);
  609. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.ApprovalNo, rs[(idxRow * cols) + 8],9);
  610. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.RRN, rs[(idxRow * cols) + 9],12);
  611. memcpy((char*)&deductInfo[idxRow - 1].pCreditCard.ROC, rs[(idxRow * cols) + 10],12);
  612. deductInfo[idxRow - 1].IsDonateInvoice = atoi(rs[(idxRow * cols) + 11]);
  613. deductInfo[idxRow - 1].DeductResult = atoi(rs[(idxRow * cols) + 12]);
  614. deductInfo[idxRow - 1].isIntoCharge = atoi(rs[(idxRow * cols) + 13]);
  615. deductInfo[idxRow - 1].IsUpload = atoi(rs[(idxRow * cols) + 14]);
  616. if(idxRow >= 128)
  617. {
  618. rows = 128;
  619. break;
  620. }
  621. }
  622. }
  623. sqlite3_free_table(rs);
  624. sqlite3_close(deductDb);
  625. }
  626. return rows;
  627. }
  628. int DB_GetMultiReDeductInfo(int *gunIndex, RecordTransactionInfo *deductInfo)
  629. {
  630. int quantity = 0;
  631. quantity = DB_GetMultiDeductInfo(0, -1, gunIndex, deductInfo);
  632. return quantity;
  633. }
  634. int DB_GetMultiReUploadDeduct(int *gunIndex, RecordTransactionInfo *deductInfo)
  635. {
  636. int quantity = 0;
  637. quantity = DB_GetMultiDeductInfo(-1, 0, gunIndex, deductInfo);
  638. return quantity;
  639. }
  640. void ReDeductTest()
  641. {
  642. int quantity = 0;
  643. int gunList[128];
  644. RecordTransactionInfo deductInfo[128];
  645. quantity = DB_GetMultiReDeductInfo(gunList, deductInfo);
  646. if(quantity > 0)
  647. {
  648. for(int i = 0; i < quantity; i++)
  649. {
  650. printf("Gun %d Rededuct CardNo: %s", gunList[i], deductInfo[i].pCreditCard.VemData);
  651. }
  652. }
  653. }