UserPayUpdateTask.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 DB;
  10. use Illuminate\Console\Command;
  11. use Log;
  12. use Redis;
  13. class UserPayUpdateTask extends Command
  14. {
  15. /**
  16. * 执行命令 php artisan force_user_active
  17. *
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'userPayUpdateTask';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '更新user_pay表字段';
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $min_id = DB::table('global_configs')->where('config','pay_user_orders_max_id')->first();
  37. $min_id = $min_id->config_value;
  38. $max_id = DB::table('orders')->max('id');
  39. //插入user_pay
  40. while ($min_id+9999 <= $max_id){
  41. DB::update("insert into user_pay (uid,distribution_channel_id,first_recharge_time,first_recharge_amount)
  42. select uid,distribution_channel_id,min(created_at),price from orders
  43. where status ='PAID' and orders.id >=".$min_id." and orders.id<=".($min_id+9999)." and not EXISTS
  44. (select 1 from user_pay where uid = orders.uid) GROUP BY uid");
  45. $min_id = $min_id+9999;
  46. }
  47. DB::table('global_configs')->where('config','pay_user_orders_max_id')->update(['config_value'=>$max_id]);
  48. DB::update("update user_pay set user_pay.sec_recharge_time = (SELECT orders.created_at FROM orders
  49. where user_pay.uid = orders.uid and orders.created_at > user_pay.first_recharge_time and status = 'PAID' LIMIT 1),user_pay.sec_recharge_amount=(SELECT orders.price FROM orders
  50. where user_pay.uid = orders.uid and orders.created_at > user_pay.first_recharge_time and status = 'PAID' LIMIT 1) WHERE user_pay.sec_recharge_time is null");
  51. //三充时间、金额更新
  52. DB::update("update user_pay set user_pay.third_recharge_time = (SELECT orders.created_at FROM orders where user_pay.uid = orders.uid and orders.created_at > user_pay.sec_recharge_time AND status = 'PAID' LIMIT 1),
  53. user_pay.third_recharge_amount=(SELECT orders.price FROM orders where user_pay.uid = orders.uid and orders.created_at > user_pay.sec_recharge_time AND status='PAID' LIMIT 1) WHERE user_pay.third_recharge_amount is null");
  54. //余额更新
  55. DB::update("update user_pay set user_pay.balance = (select users.balance from users where users.id = user_pay.uid)");
  56. }
  57. private function test1()
  58. {
  59. $array = ['recent_read','subscribe','text_sign','text_search','readerover','readrecord','daily_sign'];//
  60. // $array = ['daily_sign'];
  61. foreach ($array as $v){
  62. $users = Redis::SMEMBERS("temp_from_where:".$v);
  63. foreach ($users as $user){
  64. $sub = DB::connection('chapter_order_mysql')
  65. ->table('chapter_orders'.($user%512))
  66. ->where('uid',$user)
  67. ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
  68. ->first();
  69. if($sub){
  70. $fee = DB::connection('chapter_order_mysql')
  71. ->table($v.'_user')
  72. ->select('uid','fee')
  73. ->where('uid',$user)
  74. ->first();
  75. DB::connection('chapter_order_mysql')
  76. ->table($v.'_user_sub')
  77. ->insert(['uid'=>$user,'fee'=>isset($fee->fee)?$fee->fee:-1]);
  78. }
  79. }
  80. /*DB::connection('chapter_order_mysql')
  81. ->table($v.'_user_sub')
  82. ->select('uid','fee')
  83. ->orderBy('id')
  84. ->chunk(10000,function($users) use($v){
  85. foreach ($users as $user){
  86. $sub = DB::table('chapter_orders'.(($user->uid)%512))
  87. ->where('uid',$user->uid)
  88. ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
  89. ->first();
  90. if($sub){
  91. DB::table($v.'_user_sub')->insert(['uid'=>$user->uid,'fee'=>$user->fee]);
  92. }
  93. }
  94. });*/
  95. //$total_users = $above_five_thousands_total = $above_five_thousands_male = $above_five_thousands_felman = 0;
  96. //$above_three_thousands_total = $above_three_thousands_male = $above_three_thousands_felman = 0;
  97. //$users = Redis::SMEMBERS("temp_from_where:".$v);
  98. /*foreach ($users as $user) {
  99. DB::connection('chapter_order_mysql')->table($v.'_user')->insert(['uid'=>$user]);
  100. /*$book_order = DB::table('book_orders')
  101. ->where('uid', $user)
  102. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  103. ->first();
  104. $table_seq = ($user) % 512;
  105. $chapter_order = DB::connection('chapter_order_mysql')
  106. ->table('chapter_orders' . $table_seq)
  107. ->where('uid', $user)
  108. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  109. ->first();
  110. if ($book_order || $chapter_order) {
  111. //\Log::info(($user->uid).':'.$subscribe_fee);
  112. $total_users++;
  113. $chapter_order_sum = DB::connection('chapter_order_mysql')
  114. ->table('chapter_orders' . $table_seq)
  115. ->where('uid', $user)
  116. ->sum('fee');
  117. $book_order_sum = DB::table('book_orders')
  118. ->where('uid', $user)
  119. ->sum('fee');
  120. $subscribe_fee = $book_order_sum + $chapter_order_sum;
  121. $gender = 0;
  122. if ($subscribe_fee >= 3000) {
  123. $user_channel = DB::table('force_subscribe_users')
  124. ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
  125. ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
  126. ->where('force_subscribe_users.uid', $user)
  127. ->first();
  128. if ($user_channel) {
  129. $gender = $user_channel->pid;
  130. } else {
  131. \Log::error('user_gender_not_found:uid:' . $user);
  132. }
  133. $above_three_thousands_total++;
  134. if ($gender == 1) {
  135. $above_three_thousands_male++;
  136. }
  137. if ($gender == 2) {
  138. $above_five_thousands_felman++;
  139. }
  140. }
  141. if ($subscribe_fee >= 5000) {
  142. $user_channel = DB::table('force_subscribe_users')
  143. ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
  144. ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
  145. ->where('force_subscribe_users.uid', $user)
  146. ->first();
  147. if ($user_channel) {
  148. $gender = $user_channel->pid;
  149. } else {
  150. \Log::error('user_gender_not_found:uid:' . $user);
  151. }
  152. $above_three_thousands_total++;
  153. if ($gender == 1) {
  154. $above_three_thousands_male++;
  155. }
  156. if ($gender == 2) {
  157. $above_five_thousands_felman++;
  158. }
  159. }
  160. //}
  161. }*/
  162. //return true;
  163. /*myLog('temp_from_where')->info($v.'---------------------------------------------------');
  164. myLog('temp_from_where')->info(compact(
  165. 'v',
  166. 'total_users',
  167. 'above_five_thousands_total',
  168. 'above_five_thousands_male',
  169. 'above_five_thousands_felman',
  170. 'above_three_thousands_total',
  171. 'above_three_thousands_male',
  172. 'above_three_thousands_felman'
  173. ));*/
  174. }
  175. }
  176. private function test2() {
  177. $users = DB::table('daily_sign_user_sub')->where('fee',-1)->get();
  178. foreach ($users as $user){
  179. $fee = DB::table('chapter_orders'.($user%512))
  180. ->where('uid',$user->uid)
  181. ->where('created_at','<=','2018-12-07 23:59:59')
  182. ->sum('fee');
  183. $user->fee = $fee;
  184. $user->save();
  185. }
  186. }
  187. }