BookOrderByDayCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Console\Subscribe;
  3. use App\Cache\StatisticCache;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class BookOrderByDayCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'Subscribe:BookOrderByDay {--day=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'redis临时保存的订阅记录按天汇总写入数据库';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $day = $this->option('day');
  37. if (!$day) $day = date('Y-m-d', strtotime('-1 day'));
  38. $this->saveDB($day);
  39. }
  40. private function saveDB($day)
  41. {
  42. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~开始统计redis临时保存的订阅记录~~~~~~~~~~~~~~~~~~~~~~~~~~');
  43. $executeStart = microtime(true);
  44. // 获取数据
  45. $data = $this->getRecord($day);
  46. $fields = ['bid', 'day', 'balance', 'charge_balance', 'reward_balance', 'is_chapter', 'count'];
  47. // 组装数据
  48. $insertData = [];
  49. foreach ($data as $item) {
  50. foreach ($item as $v) {
  51. $arr = json_decode($v, true);
  52. if (!is_array($arr) || empty($arr)) {
  53. continue;
  54. }
  55. // 参数不正确就跳过
  56. foreach ($fields as $field) {
  57. if (!isset($arr[$field])) continue(2);
  58. }
  59. if (isset($arr['uid'])) unset($arr['uid']);
  60. $arr['created_at'] = $arr['updated_at'] = date('Y-m-d H:i:s');
  61. // 不是指定日期的数据则跳过
  62. if ($arr['day'] != $day) continue;
  63. // 按书籍id累加
  64. if (isset($insertData[$arr['bid']])) {
  65. $insertData[$arr['bid']]['balance'] += $arr['balance'];
  66. $insertData[$arr['bid']]['charge_balance'] += $arr['charge_balance'];
  67. $insertData[$arr['bid']]['reward_balance'] += $arr['reward_balance'];
  68. $insertData[$arr['bid']]['count'] += $arr['count'];
  69. }else {
  70. $insertData[$arr['bid']] = $arr;
  71. }
  72. }
  73. }
  74. if ($insertData) {
  75. DB::table('book_order_by_day')->where('day', $day)->delete();
  76. DB::table('book_order_by_day')->insert($insertData);
  77. }
  78. $executeEnd = microtime(true);
  79. dLog('command_logs')->info('脚本运行时间: ', ['execute_time'=>round(($executeEnd - $executeStart), 6).'s']);
  80. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~结束统计redis临时保存的订阅记录~~~~~~~~~~~~~~~~~~~~~~~~~~');
  81. }
  82. private function getRecord($day)
  83. {
  84. if ($day) $day = date('Ymd', strtotime($day));
  85. else $day = date('Ymd', strtotime('-1 day'));
  86. $length = StatisticCache::getSubscribeCount($day);
  87. if (!$length) return [];
  88. $count = ceil($length / 1000);
  89. $result = [];
  90. for ($i = 0; $i < $count; $i++) {
  91. $start = $i * 1000;
  92. $end = $start + 999;
  93. $result[] = StatisticCache::getSubscribe($day, $start, $end);
  94. }
  95. return $result;
  96. }
  97. }