ForceUserActive.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. use App\Modules\Sys\Services\SysConfigService;
  16. class ForceUserActive extends Command
  17. {
  18. /**
  19. * 执行命令 php artisan force_user_active
  20. *
  21. * The name and signature of the console command.
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'force_user_active {start} {end}';
  26. /**
  27. * The console command description.
  28. *
  29. * @var string
  30. */
  31. protected $description = '每日强关用户行为';
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. /**
  40. * 更新渠道、公众号信息
  41. *update force_users_date_actives,distribution_channels,official_accounts,companies,channel_users set force_users_date_actives.distribution_channel_id = distribution_channels.id,force_users_date_actives.distribution_channel_name=companies.name,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 and companies.id = channel_users.company_id and distribution_channels.channel_user_id = channel_users.id
  42. * 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
  43. * 24小时内取关粉丝阅读到免费最后一章和取关时间差
  44. *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
  45. *24小时内取关粉丝关注到取关时间差
  46. *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
  47. *24小时内新关粉丝阅读到免费最后一章和首充时间差
  48. *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
  49. *24小时内新关粉丝关注到首充时间差
  50. *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
  51. *24小时内新关粉丝关注到订阅时间差
  52. *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
  53. *update force_users_date_read set unsub_time = null where unsub_time = 0
  54. */
  55. print_r("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  56. Log::info("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  57. //清空原表数据
  58. DB::table('force_users_date_actives')->truncate();
  59. $_start = new \DateTime($this->argument('start'));
  60. $_end = new \DateTime($this->argument('end'));
  61. if(!$_start || !$_end) dd('param empty!exit');
  62. foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
  63. {
  64. $date = $d->format('Y-m-d');
  65. $start = $date;
  66. $end = date('Y-m-d', strtotime($date) + 86400);
  67. print_r("======执行日期:{$date}=====");
  68. $offset = 0;
  69. $limit = 13000;
  70. $data = [];
  71. $_m_data = [];
  72. while (true) {
  73. 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"));
  74. $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");
  75. print_r("users_count:".count($users));
  76. if (count($users) == 0) break;
  77. foreach ($users as $user) {
  78. $is_unsub_in_one_day = false;
  79. $is_pay_in_one_day = false;
  80. $is_pay_in_two_days = false;
  81. $is_pay_in_three_days = false;
  82. $is_unsub_in_two_days = false;
  83. $is_unsub_in_three_days = false;
  84. $order_sum_in_one_day = 0;
  85. $order_sum_in_two_day = 0;
  86. $order_sum_in_three_day = 0;
  87. $order_sum_in_one_month = 0;
  88. $order_sum_in_two_month = 0;
  89. $order_sum_in_three_month = 0;
  90. $order_sum_in_six_month = 0;
  91. $user_orders = DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->get();
  92. $sub_time = strtotime($user->created_at);//关注时间戳
  93. if (count($user_orders)) {
  94. $first_order = $user_orders ? $user_orders[0] : null;
  95. if (strtotime($first_order->created_at) < $sub_time + 86400)//24小时内
  96. {
  97. @$data[$user->appid]['pay_user_num_in_one_day']++;
  98. $is_pay_in_one_day = true;
  99. }
  100. if (strtotime($first_order->created_at) < $sub_time + 86400 * 2)//48小时内
  101. {
  102. $is_pay_in_two_days = true;
  103. }
  104. if (strtotime($first_order->created_at) < $sub_time + 86400 * 3)//72小时内
  105. {
  106. @$data[$user->appid]['pay_user_num_in_three_days']++;
  107. $is_pay_in_three_days = true;
  108. }
  109. if (strtotime($first_order->created_at) < $sub_time + 86400 * 30)//30天内
  110. {
  111. @$data[$user->appid]['pay_user_num_in_one_month']++;
  112. }
  113. foreach ($user_orders as $_order)
  114. {
  115. $price = $_order->price;
  116. if(strtotime($_order->created_at) - $sub_time <= 86400) $order_sum_in_one_day += $price;
  117. if(strtotime($_order->created_at) - $sub_time <= 86400*3) $order_sum_in_three_day += $price;
  118. if(strtotime($_order->created_at) - $sub_time <= 86400*30) $order_sum_in_one_month += $price;
  119. if(strtotime($_order->created_at) - $sub_time <= 86400*60) $order_sum_in_two_month += $price;
  120. if(strtotime($_order->created_at) - $sub_time <= 86400*90) $order_sum_in_three_month += $price;
  121. if(strtotime($_order->created_at) - $sub_time <= 86400*180) $order_sum_in_six_month += $price;
  122. }
  123. }
  124. @$data[$user->appid]['order_sum_in_one_day'] += $order_sum_in_one_day;
  125. //@$data[$user->appid]['order_sum_in_two_day'] += $order_sum_in_two_day;
  126. @$data[$user->appid]['order_sum_in_three_day'] += $order_sum_in_three_day;
  127. @$data[$user->appid]['order_sum_in_one_month'] += $order_sum_in_one_month;
  128. @$data[$user->appid]['fans_num']++;
  129. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400)//24小时内取注
  130. {
  131. @$data[$user->appid]['unsub_user_num_in_one_day']++;
  132. $is_unsub_in_one_day = true;
  133. }
  134. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*2)//48小时内取注
  135. {
  136. @$data[$user->appid]['unsub_user_num_in_two_days']++;
  137. $is_unsub_in_two_days = true;
  138. }
  139. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*3)//72小时内取注
  140. {
  141. @$data[$user->appid]['unsub_user_num_in_three_days']++;
  142. $is_unsub_in_three_days = true;
  143. }
  144. // if ($user->send_order_id && ($is_unsub_in_one_day || $is_unsub_in_two_days || $is_unsub_in_three_days)) {
  145. // $send_order = DB::table('send_orders')->where('id', $user->send_order_id)->first();
  146. // if ($send_order->book_id) {
  147. // $book = Redis::hget('book_read:' . $user->uid, $send_order->book_id);
  148. //
  149. // if ($book) {
  150. // $book_info = explode('_', $book);
  151. //
  152. // $cid = $book_info[0];
  153. // $book_config = DB::table('book_configs')->select(['force_subscribe_chapter_seq','charge_type'])->where('bid', $send_order->book_id)->first();
  154. //
  155. // $chapter = DB::table('chapters')->select(['sequence'])->where('id', $cid)->first();
  156. //
  157. // if($book_config && $chapter)
  158. // {
  159. // //判断付费章节
  160. // $vip_chapter = DB::table('chapters')->select(['sequence'])->where('bid', $send_order->book_id)->where('is_vip',1)->orderby('sequence')->first();
  161. // if($vip_chapter)
  162. // {
  163. // if ($book_config->charge_type == 'BOOK' && $chapter->sequence >= $vip_chapter->sequence - 1)//按本、看书超过付费前一章
  164. // {
  165. // $is_unsub_in_one_day && @$data[$user->appid]['unsub_in_one_day_read_last_free_user_num']++;
  166. // $is_unsub_in_two_days && @$data[$user->appid]['unsub_in_two_day_read_last_free_user_num']++;
  167. // $is_unsub_in_three_days && @$data[$user->appid]['unsub_in_three_day_read_last_free_user_num']++;
  168. // }
  169. //
  170. // if ($book_config->charge_type == 'CHAPTER')//按章节 有订阅无充值
  171. // {
  172. // if($is_unsub_in_one_day && !$is_pay_in_one_day)
  173. // {
  174. // $_day = 1;
  175. // $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();
  176. // $sub_num && @$data[$user->appid]['unsub_in_one_day_read_last_free_user_num']++;
  177. //
  178. // }elseif($is_unsub_in_two_days && !$is_pay_in_two_days)
  179. // {
  180. // $_day = 2;
  181. // $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();
  182. // $sub_num && @$data[$user->appid]['unsub_in_two_day_read_last_free_user_num']++;
  183. // }elseif($is_unsub_in_three_days && !$is_pay_in_three_days)
  184. // {
  185. // $_day = 3;
  186. // $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();
  187. // $sub_num && @$data[$user->appid]['unsub_in_three_day_read_last_free_user_num']++;
  188. // }
  189. //
  190. // }
  191. // }
  192. // }
  193. //
  194. // }
  195. //
  196. // }
  197. //
  198. // }
  199. }
  200. $offset = $offset + $limit;
  201. }
  202. foreach ($data as $appid => $item) {
  203. $_data = [];
  204. $_data['appid'] = $appid;
  205. $_data['date'] = $date;
  206. $_data['fans_num'] = isset($item['fans_num']) ? $item['fans_num'] : 0;
  207. $_data['pay_user_num_in_one_day'] = isset($item['pay_user_num_in_one_day']) ? $item['pay_user_num_in_one_day'] : 0;
  208. $_data['read_last_free_user_num'] = isset($item['read_last_free_user_num']) ? $item['read_last_free_user_num'] : 0;
  209. $_data['unsub_user_num_in_one_day'] = isset($item['unsub_user_num_in_one_day']) ? $item['unsub_user_num_in_one_day'] : 0;
  210. $_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;
  211. $_data['unsub_user_num_in_two_days'] = isset($item['unsub_user_num_in_two_days']) ? $item['unsub_user_num_in_two_days'] : 0;
  212. $_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;
  213. $_data['unsub_user_num_in_three_days'] = isset($item['unsub_user_num_in_three_days']) ? $item['unsub_user_num_in_three_days'] : 0;
  214. $_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;
  215. $_data['pay_user_num_in_three_days'] = isset($item['pay_user_num_in_three_days']) ? $item['pay_user_num_in_three_days'] : 0;
  216. $_data['pay_user_num_in_one_month'] = isset($item['pay_user_num_in_one_month']) ? $item['pay_user_num_in_one_month'] : 0;
  217. $_data['order_sum_in_one_day'] = $item['order_sum_in_one_day'];
  218. $_data['order_sum_in_three_day'] = $item['order_sum_in_three_day'];
  219. $_data['order_sum_in_one_month'] = $item['order_sum_in_one_month'];
  220. $_data['created_at'] = $_data['updated_at'] = date('Y-m-d H:i:s');
  221. $_m_data[] = $_data;
  222. }
  223. DB::table('force_users_date_actives')->insert($_m_data);
  224. }
  225. //更新渠道名称
  226. DB::update("update force_users_date_actives,distribution_channels,official_accounts,companies,channel_users set force_users_date_actives.distribution_channel_id = distribution_channels.id,force_users_date_actives.distribution_channel_name=companies.name,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 and companies.id = channel_users.company_id and distribution_channels.channel_user_id = channel_users.id");
  227. //修改config表开关
  228. SysConfigService::closeForceActiveSwitch();
  229. Log::info("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  230. print_r("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  231. }
  232. }