OfficialAccountSubStat.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Console\Commands\Temp;
  3. use Log;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. class OfficialAccountSubStat extends Command
  7. {
  8. /**
  9. * 执行命令 php artisan temp:official_account_sub_stat
  10. *
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'temp:official_account_sub_stat';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '服务号订阅数据';
  22. /**
  23. * Execute the console command.
  24. *
  25. * @return mixed
  26. */
  27. public function handle()
  28. {
  29. /**
  30. * CREATE TABLE `official_account_day_sub_stats` (
  31. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  32. `appid` varchar(255) DEFAULT NULL,
  33. `official_account_name` varchar(255) DEFAULT NULL,
  34. `date` date DEFAULT NULL,
  35. `reward_balance` int(11) DEFAULT NULL,
  36. `charge_balance` int(11) DEFAULT NULL,
  37. `fee` int(11) DEFAULT NULL,
  38. PRIMARY KEY (`id`)
  39. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;*
  40. *
  41. */
  42. print_r("======服务号订阅数据 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  43. Log::info("======服务号订阅数据 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  44. $_start = new \DateTime('2017-12-12');
  45. $_end = new \DateTime('2018-06-01');
  46. foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
  47. {
  48. $date = $d->format('Y-m-d');
  49. $start = $date;
  50. $end = date('Y-m-d', strtotime($date) + 86400);
  51. $offset = 0;
  52. $limit = 1000;
  53. $data = [];
  54. for ($i=0;$i<512;$i++)
  55. {
  56. //章节订购
  57. $chapter_table = 'zsy_sub.chapter_orders'.$i;
  58. $force_user_table = 'yueduyun.force_subscribe_users';
  59. $users = DB::connection('chapter_order_mysql')->select("select appid,{$chapter_table}.distribution_channel_id,{$chapter_table}.uid,sum(fee) sum_fee,sum(charge_balance) sum_charge_balance,sum(reward_balance) sum_reward_balance from {$chapter_table} left join {$force_user_table} on {$chapter_table}.uid = {$force_user_table}.uid where {$chapter_table}.created_at >= '{$date}' and {$chapter_table}.created_at < '{$end}' group by {$chapter_table}.uid");
  60. print_r("select appid,{$chapter_table}.distribution_channel_id,{$chapter_table}.uid,sum(fee) sum_fee,sum(charge_balance) sum_charge_balance,sum(reward_balance) sum_reward_balance from {$chapter_table} left join {$force_user_table} on {$chapter_table}.uid = {$force_user_table}.uid where {$chapter_table}.created_at >= '{$date}' and {$chapter_table}.created_at < '{$end}' group by {$chapter_table}.uid" . "\n");
  61. foreach ($users as $user) {
  62. if(!$user->appid)
  63. {
  64. //获取第一个appid
  65. $official_account = DB::connection('chapter_order_mysql')->table('yueduyun.official_accounts')->where('distribution_channel_id',$user->distribution_channel_id)->first();
  66. if($official_account)
  67. {
  68. $user->appid = $official_account->appid;
  69. }else{
  70. $user->appid = 'no';
  71. }
  72. }
  73. if($user->appid)
  74. {
  75. @$data[$user->appid]['fee'] += $user->sum_fee;
  76. @$data[$user->appid]['charge_balance'] += $user->sum_charge_balance;
  77. @$data[$user->appid]['reward_balance'] += $user->sum_reward_balance;
  78. }
  79. }
  80. }
  81. //按本订购
  82. $chapter_table = 'yueduyun.book_orders';
  83. $force_user_table = 'yueduyun.force_subscribe_users';
  84. $users = DB::connection('chapter_order_mysql')->select("select appid,{$chapter_table}.distribution_channel_id,{$chapter_table}.uid,sum(fee) sum_fee,sum(charge_balance) sum_charge_balance,sum(reward_balance) sum_reward_balance from {$chapter_table} left join {$force_user_table} on {$chapter_table}.uid = {$force_user_table}.uid where {$chapter_table}.created_at >= '{$date}' and {$chapter_table}.created_at < '{$end}' group by {$chapter_table}.uid");
  85. print_r("select appid,{$chapter_table}.distribution_channel_id,{$chapter_table}.uid,sum(fee) sum_fee,sum(charge_balance) sum_charge_balance,sum(reward_balance) sum_reward_balance from {$chapter_table} left join {$force_user_table} on {$chapter_table}.uid = {$force_user_table}.uid where {$chapter_table}.created_at >= '{$date}' and {$chapter_table}.created_at < '{$end}' group by {$chapter_table}.uid" . "\n");
  86. foreach ($users as $user) {
  87. if(!$user->appid)
  88. {
  89. //获取第一个appid
  90. $official_account = DB::connection('chapter_order_mysql')->table('yueduyun.official_accounts')->where('distribution_channel_id',$user->distribution_channel_id)->first();
  91. if($official_account)
  92. {
  93. $user->appid = $official_account->appid;
  94. }else{
  95. $user->appid = 'no';
  96. }
  97. }
  98. if($user->appid)
  99. {
  100. @$data[$user->appid]['fee'] += $user->sum_fee;
  101. @$data[$user->appid]['charge_balance'] += $user->sum_charge_balance;
  102. @$data[$user->appid]['reward_balance'] += $user->sum_reward_balance;
  103. }
  104. }
  105. foreach ($data as $appid=>$_item)
  106. {
  107. $_data = [];
  108. $_data['appid'] = $appid;
  109. $_data['fee'] = $_item['fee'];
  110. $_data['date'] = $date;
  111. $_data['charge_balance'] = $_item['charge_balance'];
  112. $_data['reward_balance'] = $_item['reward_balance'];
  113. $official_account = DB::connection('chapter_order_mysql')->table('yueduyun.official_accounts')->where('appid',$appid)->first();
  114. $_data['official_account_name'] = $official_account ? $official_account->nickname : '';
  115. DB::connection('chapter_order_mysql')->table('yueduyun.official_account_day_sub_stats')->insert($_data);
  116. }
  117. Log::info("======服务号订阅数据 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  118. print_r("======服务号订阅数据 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  119. }
  120. }
  121. }