ForceUserActive.php.bak 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Finance\Services\FinanceService;
  10. use Log;
  11. use Illuminate\Console\Command;
  12. use DB;
  13. use Redis;
  14. use App\Modules\Book\Services\BookSubscribleChapterService;
  15. class ForceUserActive extends Command
  16. {
  17. /**
  18. * 执行命令 php artisan force_user_active
  19. *
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'force_user_active';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '每日强关用户行为';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. /**
  39. * 更新渠道、公众号信息
  40. * update force_users_date_actives,distribution_channels,official_accounts set force_users_date_actives.distribution_channel_id = distribution_channels.id,force_users_date_actives.distribution_channel_name=distribution_channels.nickname,force_users_date_actives.gzh_name = official_accounts.nickname where force_users_date_actives.appid = official_accounts.appid and official_accounts.distribution_channel_id = distribution_channels.id
  41. * select date,distribution_channel_name,gzh_name,distribution_channel_id,fans_num,pay_user_num_in_one_day,unsub_user_num_in_one_day,unsub_in_one_day_read_last_free_user_num,pay_user_num_in_three_days,unsub_user_num_in_two_days,unsub_in_two_day_read_last_free_user_num,unsub_user_num_in_three_days,unsub_in_three_day_read_last_free_user_num,pay_user_num_in_one_month,order_sum_in_one_day,order_sum_in_three_day,order_sum_in_one_month from force_users_date_actives
  42. * 24小时内取关粉丝阅读到免费最后一章和取关时间差
  43. *select count(1) from force_users_date_read where is_unsub_in_one_day = 1 and last_free_read_time is not null and unsub_time - last_free_read_time >10800 and unsub_time - last_free_read_time <=10800
  44. *24小时内取关粉丝关注到取关时间差
  45. *select count(1) from force_users_date_read where is_unsub_in_one_day = 1 and unsub_time - sub_time >43200 and unsub_time - sub_time <=86400
  46. *24小时内新关粉丝阅读到免费最后一章和首充时间差
  47. *select count(1) from force_users_date_read where last_free_read_time is not null and recharge_time_in_one_day is not null and recharge_time_in_one_day - last_free_read_time >10800 and recharge_time_in_one_day - last_free_read_time <=10800
  48. *24小时内新关粉丝关注到首充时间差
  49. *select count(1) from force_users_date_read where recharge_time_in_one_day is not null and recharge_time_in_one_day - sub_time >21600 and recharge_time_in_one_day - sub_time <=43200
  50. *24小时内新关粉丝关注到订阅时间差
  51. *select count(1) from force_users_date_read where first_sub_time is not null and first_sub_time - sub_time >43200 and first_sub_time - sub_time <=86400
  52. *update force_users_date_read set unsub_time = null where unsub_time = 0
  53. */
  54. print_r("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  55. Log::info("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  56. $_start = new \DateTime('2018-01-05');
  57. $_end = new \DateTime('2018-03-05');
  58. foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
  59. {
  60. $date = $d->format('Y-m-d');
  61. $start = $date;
  62. $end = date('Y-m-d', strtotime($date) + 86400);
  63. $offset = 0;
  64. $limit = 13000;
  65. $data = [];
  66. $_m_data = [];
  67. while (true) {
  68. print_r("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit" . date("y-m-d H:i:s" . "\n"));
  69. $users = DB::select("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit");
  70. print_r("users_count:".count($users));
  71. if (count($users) == 0) break;
  72. foreach ($users as $user) {
  73. $is_unsub_in_one_day = false;
  74. $is_pay_in_one_day = false;
  75. $is_pay_in_two_days = false;
  76. $is_pay_in_three_days = false;
  77. $is_unsub_in_two_days = false;
  78. $is_unsub_in_three_days = false;
  79. $order_sum_in_one_day = 0;
  80. $order_sum_in_two_day = 0;
  81. $order_sum_in_three_day = 0;
  82. $order_sum_in_one_month = 0;
  83. $order = DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->first();
  84. if ($order) {
  85. if (strtotime($order->created_at) < strtotime($user->created_at) + 86400)//24小时内
  86. {
  87. @$data[$user->appid]['pay_user_num_in_one_day']++;
  88. $is_pay_in_one_day = true;
  89. }
  90. if (strtotime($order->created_at) < strtotime($user->created_at) + 86400 * 2)//48小时内
  91. {
  92. $is_pay_in_two_days = true;
  93. }
  94. if (strtotime($order->created_at) < strtotime($user->created_at) + 86400 * 3)//72小时内
  95. {
  96. @$data[$user->appid]['pay_user_num_in_three_days']++;
  97. $is_pay_in_three_days = true;
  98. }
  99. if (strtotime($order->created_at) < strtotime($user->created_at) + 86400 * 30)//30天内
  100. {
  101. @$data[$user->appid]['pay_user_num_in_one_month']++;
  102. }
  103. $order_sum_in_one_day = (float)DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at)+86400))->sum('price');
  104. //$order_sum_in_two_day = (float)DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at)+86400*2))->sum('price');
  105. $order_sum_in_three_day = (float)DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at)+86400*3))->sum('price');
  106. $order_sum_in_one_month = (float)DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at)+86400*30))->sum('price');
  107. }
  108. @$data[$user->appid]['order_sum_in_one_day'] += $order_sum_in_one_day;
  109. //@$data[$user->appid]['order_sum_in_two_day'] += $order_sum_in_two_day;
  110. @$data[$user->appid]['order_sum_in_three_day'] += $order_sum_in_three_day;
  111. @$data[$user->appid]['order_sum_in_one_month'] += $order_sum_in_one_month;
  112. @$data[$user->appid]['fans_num']++;
  113. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400)//24小时内取注
  114. {
  115. @$data[$user->appid]['unsub_user_num_in_one_day']++;
  116. $is_unsub_in_one_day = true;
  117. }
  118. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*2)//48小时内取注
  119. {
  120. @$data[$user->appid]['unsub_user_num_in_two_days']++;
  121. $is_unsub_in_two_days = true;
  122. }
  123. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*3)//72小时内取注
  124. {
  125. @$data[$user->appid]['unsub_user_num_in_three_days']++;
  126. $is_unsub_in_three_days = true;
  127. }
  128. if ($user->send_order_id && ($is_unsub_in_one_day || $is_unsub_in_two_days || $is_unsub_in_three_days)) {
  129. $send_order = DB::table('send_orders')->where('id', $user->send_order_id)->first();
  130. if ($send_order->book_id) {
  131. $book = Redis::hget('book_read:' . $user->uid, $send_order->book_id);
  132. if ($book) {
  133. $book_info = explode('_', $book);
  134. $cid = $book_info[0];
  135. $book_config = DB::table('book_configs')->select(['force_subscribe_chapter_seq','charge_type'])->where('bid', $send_order->book_id)->first();
  136. $chapter = DB::table('chapters')->select(['sequence'])->where('id', $cid)->first();
  137. //判断付费章节
  138. $vip_chapter = DB::table('chapters')->select(['sequence'])->where('bid', $send_order->book_id)->where('is_vip',1)->orderby('sequence')->first();
  139. if ($book_config->charge_type == 'BOOK' && $chapter->sequence >= $vip_chapter->sequence - 1)//按本、看书超过付费前一章
  140. {
  141. $is_unsub_in_one_day && @$data[$user->appid]['unsub_in_one_day_read_last_free_user_num']++;
  142. $is_unsub_in_two_days && @$data[$user->appid]['unsub_in_two_day_read_last_free_user_num']++;
  143. $is_unsub_in_three_days && @$data[$user->appid]['unsub_in_three_day_read_last_free_user_num']++;
  144. }
  145. if ($book_config->charge_type == 'CHAPTER')//按章节 有订阅无充值
  146. {
  147. if($is_unsub_in_one_day && !$is_pay_in_one_day)
  148. {
  149. $_day = 1;
  150. $sub_num = DB::connection('chapter_order_mysql')->table('chapter_orders'.$user->uid%512)->where('uid',$user->uid)->where('bid',$send_order->book_id)->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at) + 86400*$_day))->count();
  151. $sub_num && @$data[$user->appid]['unsub_in_one_day_read_last_free_user_num']++;
  152. }elseif($is_unsub_in_two_days && !$is_pay_in_two_days)
  153. {
  154. $_day = 2;
  155. $sub_num = DB::connection('chapter_order_mysql')->table('chapter_orders'.$user->uid%512)->where('uid',$user->uid)->where('bid',$send_order->book_id)->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at) + 86400*$_day))->count();
  156. $sub_num && @$data[$user->appid]['unsub_in_two_day_read_last_free_user_num']++;
  157. }elseif($is_unsub_in_three_days && !$is_pay_in_three_days)
  158. {
  159. $_day = 3;
  160. $sub_num = DB::connection('chapter_order_mysql')->table('chapter_orders'.$user->uid%512)->where('uid',$user->uid)->where('bid',$send_order->book_id)->where('created_at','<=',date('Y-m-d H:i:s',strtotime($user->created_at) + 86400*$_day))->count();
  161. $sub_num && @$data[$user->appid]['unsub_in_three_day_read_last_free_user_num']++;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. $offset = $offset + $limit;
  169. }
  170. foreach ($data as $appid => $item) {
  171. $_data = [];
  172. $_data['appid'] = $appid;
  173. $_data['date'] = $date;
  174. $_data['fans_num'] = isset($item['fans_num']) ? $item['fans_num'] : 0;
  175. $_data['pay_user_num_in_one_day'] = isset($item['pay_user_num_in_one_day']) ? $item['pay_user_num_in_one_day'] : 0;
  176. $_data['read_last_free_user_num'] = isset($item['read_last_free_user_num']) ? $item['read_last_free_user_num'] : 0;
  177. $_data['unsub_user_num_in_one_day'] = isset($item['unsub_user_num_in_one_day']) ? $item['unsub_user_num_in_one_day'] : 0;
  178. $_data['unsub_in_one_day_read_last_free_user_num'] = isset($item['unsub_in_one_day_read_last_free_user_num']) ? $item['unsub_in_one_day_read_last_free_user_num'] : 0;
  179. $_data['unsub_user_num_in_two_days'] = isset($item['unsub_user_num_in_two_days']) ? $item['unsub_user_num_in_two_days'] : 0;
  180. $_data['unsub_in_two_day_read_last_free_user_num'] = isset($item['unsub_in_two_day_read_last_free_user_num']) ? $item['unsub_in_two_day_read_last_free_user_num'] : 0;
  181. $_data['unsub_user_num_in_three_days'] = isset($item['unsub_user_num_in_three_days']) ? $item['unsub_user_num_in_three_days'] : 0;
  182. $_data['unsub_in_three_day_read_last_free_user_num'] = isset($item['unsub_in_three_day_read_last_free_user_num']) ? $item['unsub_in_three_day_read_last_free_user_num'] : 0;
  183. $_data['pay_user_num_in_three_days'] = isset($item['pay_user_num_in_three_days']) ? $item['pay_user_num_in_three_days'] : 0;
  184. $_data['pay_user_num_in_one_month'] = isset($item['pay_user_num_in_one_month']) ? $item['pay_user_num_in_one_month'] : 0;
  185. $_data['order_sum_in_one_day'] = $item['order_sum_in_one_day'];
  186. $_data['order_sum_in_three_day'] = $item['order_sum_in_three_day'];
  187. $_data['order_sum_in_one_month'] = $item['order_sum_in_one_month'];
  188. $_data['created_at'] = $_data['updated_at'] = date('Y-m-d H:i:s');
  189. $_m_data[] = $_data;
  190. }
  191. DB::table('force_users_date_actives')->insert($_m_data);
  192. Log::info("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  193. print_r("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  194. }
  195. }
  196. }