stgfeecharge2mysql.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. if ($_GET['action'] == 'stgfeecharge2mysql') {
  3. global $ubillingConfig;
  4. $ubCache = new UbillingCache();
  5. $feeChargeData = array();
  6. $stglog = $ubillingConfig->getAlterParam('STG_LOG_PATH');
  7. $billingConf = $ubillingConfig->getBilling();
  8. $sudo = $billingConf['SUDO'];
  9. $cat = $billingConf['CAT'];
  10. $grep = $billingConf['GREP'];
  11. $exportsPath = str_ireplace('modules/remoteapi', '', dirname(__FILE__)) . 'exports/tmp_fee_charge';
  12. $command = $sudo . ' ' . $cat . ' ' . $stglog . ' | ' . $grep . ' "fee charge" > ' . $exportsPath;
  13. shell_exec($command);
  14. $tQuery = "DROP TABLE IF EXISTS `stg_fee_charge_tab`";
  15. nr_query($tQuery);
  16. $tQuery = "CREATE TABLE IF NOT EXISTS `stg_fee_charge_tab` (
  17. `date` varchar(10),
  18. `time` varchar(10),
  19. `f3` varchar(5),
  20. `f4` varchar(20),
  21. `f5` varchar(20),
  22. `ip` varchar(20),
  23. `f7` varchar(20),
  24. `login` varchar(50),
  25. `f9` varchar(20),
  26. `f10` varchar(20),
  27. `f11` varchar(20),
  28. `f12` varchar(20),
  29. `ffrom` varchar(20),
  30. `f14` varchar(5),
  31. `fto` varchar(20),
  32. `f16` varchar(20),
  33. `f17` varchar(20),
  34. `f18` varchar(20),
  35. KEY `date` (`date`),
  36. KEY `time` (`time`),
  37. KEY dt (date,time),
  38. KEY login(login)
  39. ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
  40. nr_query($tQuery);
  41. $tQuery = "LOAD DATA LOCAL INFILE '" . $exportsPath ."' INTO TABLE `stg_fee_charge_tab` FIELDS TERMINATED BY ' '";
  42. nr_query($tQuery);
  43. $tQuery = "SELECT `max_date`, `user_login`, REPLACE(ffrom, \"'\", \"\") `balance_from`, REPLACE(fto, \"'\", \"\") `balance_to`
  44. FROM
  45. (SELECT CONCAT(`date`, ' ', `time`) AS `dat`, REPLACE(REPLACE(`login`, \"'\", \"\"), \":\", \"\") AS `user_login`, `login`, `ffrom`, `fto`
  46. FROM `stg_fee_charge_tab`) AS `ttb`
  47. INNER JOIN
  48. (SELECT MAX(CONCAT(`date`, ' ', `time`)) AS `max_date`, `login`
  49. FROM `stg_fee_charge_tab` GROUP BY `login`) AS `ttb2`
  50. ON `ttb`.`login` = `ttb2`.`login` and `ttb`.`dat` = `ttb2`.`max_date`";
  51. /*$tQuery = "SELECT `ttb2`.`max_date`, REPLACE(REPLACE(`ttb`.`login`, \"'\", \"\"), \":\", \"\") AS `user_login`, REPLACE(ffrom, \"'\", \"\") `balance_from`, REPLACE(fto, \"'\", \"\") `balance_to`
  52. FROM `stg_fee_charge_tab` AS `ttb`
  53. INNER JOIN
  54. (SELECT MAX(CONCAT(`date`, ' ', `time`)) AS `max_date`, `login` FROM `stg_fee_charge_tab` GROUP BY `login`) AS `ttb2`
  55. ON `ttb`.`login` = `ttb2`.`login` and CONCAT(`ttb`.`date`, ' ', `ttb`.`time`) = `ttb2`.`max_date`";
  56. */
  57. $result = simple_queryall($tQuery);
  58. if (!empty($result)) {
  59. foreach ($result as $item) {
  60. $feeChargeData[$item['user_login']]['max_date'] = $item['max_date'];
  61. $feeChargeData[$item['user_login']]['balance_from'] = trim($item['balance_from'], ".:'");
  62. $feeChargeData[$item['user_login']]['balance_to'] = trim($item['balance_to'], ".:'");
  63. }
  64. }
  65. $ubCache->set('STG_FEE_CHARGE', $feeChargeData);
  66. $tQuery = "DROP TABLE IF EXISTS `stg_fee_charge_tab`";
  67. nr_query($tQuery);
  68. unlink($exportsPath);
  69. }