card_key.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  3. class card_keyControl extends SystemControl
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function statsOp()
  10. {
  11. $condition = ['card_id' => ['gt', 0]];
  12. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  13. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  14. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  15. $condition['assigned_time'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  16. }
  17. elseif ($start_unixtime > 0) {
  18. $condition['assigned_time'] = ['egt', $start_unixtime];
  19. }
  20. elseif ($end_unixtime > 0) {
  21. $condition['assigned_time'] = ['lt', $end_unixtime];
  22. }
  23. if(!empty($_GET['store_id'])) {
  24. $condition['store_id'] = $_GET['store_id'];
  25. }
  26. $items = Model('')->table('card_key')
  27. ->field('card_type, amount, card_state, count(*) as card_count, sum(amount) as card_amounts')
  28. ->where($condition)
  29. ->group('card_type,amount,card_state')
  30. ->order('card_type asc, amount asc, card_state asc')
  31. ->select();
  32. $card_count = 0;
  33. $card_type_texts = [1 => '中石油', 2 => '中石化', 4 => '中国移动', 5 => '中国联通', 6 => '中国电信'];
  34. $stats = [];
  35. foreach ($items as $item) {
  36. $card_count += $item['card_count'];
  37. $card_type = $item['card_type'];
  38. $item['amount'] = intval($item['amount']);
  39. $item['card_amounts'] = intval($item['card_amounts']);
  40. $key = "{$card_type_texts[$card_type]}-{$item['amount']}元";
  41. $stats[$key][] = $item;
  42. }
  43. $card_state_stats = function($stats) {
  44. $data = [];
  45. foreach ($stats as $key => $value) {
  46. foreach ($value as $stat) {
  47. if (empty($data[$key]['UnusedCardCount'])) {
  48. $data[$key]['UnusedCardCount'] = $stat['card_state'] == mtopcard\UnusedCard ? $stat['card_count'] : 0;
  49. $data[$key]['UnusedCardAmounts'] = $stat['card_state'] == mtopcard\UnusedCard ? $stat['card_amounts'] : 0;
  50. }
  51. if (empty($data[$key]['ReserveCardCount'])) {
  52. $data[$key]['ReserveCardCount'] = $stat['card_state'] == mtopcard\ReserveCard ? $stat['card_count'] : 0;
  53. $data[$key]['ReserveCardAmounts'] = $stat['card_state'] == mtopcard\ReserveCard ? $stat['card_amounts'] : 0;
  54. }
  55. if (empty($data[$key]['AssignedCardCount'])) {
  56. $data[$key]['AssignedCardCount'] = $stat['card_state'] == mtopcard\AssignedCard ? $stat['card_count'] : 0;
  57. $data[$key]['AssignedCardAmounts'] = $stat['card_state'] == mtopcard\AssignedCard ? $stat['card_amounts'] : 0;
  58. }
  59. if (empty($data[$key]['FreezedCardCount'])) {
  60. $data[$key]['FreezedCardCount'] = $stat['card_state'] == mtopcard\FreezedCard ? $stat['card_count'] : 0;
  61. $data[$key]['FreezedCardAmounts'] = $stat['card_state'] == mtopcard\FreezedCard ? $stat['card_amounts'] : 0;
  62. }
  63. }
  64. }
  65. return $data;
  66. };
  67. $data = $card_state_stats($stats);
  68. $totals = [];
  69. $items = Model('')->table('card_key')
  70. ->field('card_type, card_state, count(*) as card_count, sum(amount) as card_amounts')
  71. ->where($condition)
  72. ->group('card_type,card_state')
  73. ->order('card_type asc, card_state asc')
  74. ->select();
  75. foreach ($items as $item) {
  76. $card_type = $item['card_type'];
  77. $key = "{$card_type_texts[$card_type]}-总计:";
  78. $totals[$key][] = $item;
  79. }
  80. $total = $card_state_stats($totals);
  81. $provider_list = $this->providers();
  82. Tpl::output('provider_list', $provider_list);
  83. Tpl::output('card_count', $card_count);
  84. Tpl::output('stats', $data);
  85. Tpl::output('totals', $total);
  86. Tpl::showpage('card_key.stats');
  87. }
  88. }