TemporaryTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 TemporaryTask 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 = 'temporaryTask {start_id}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '临时任务';
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $access_token = unserialize(Redis::get('[wechat_op.common.access_token.wxceb2aacdce248393][1]'));
  37. var_dump($access_token);
  38. return;
  39. $this->test1();
  40. return;
  41. print_r('start_temporary:-----------------' . date('Y-m-d H:i:s'));
  42. $total_users = $above_five_thousands_total = $above_five_thousands_male = $above_five_thousands_felman = 0;
  43. $above_three_thousands_total = $above_three_thousands_male = $above_three_thousands_felman = 0;
  44. $min_uid = $this->argument('start_id');
  45. for ($i = 1; $i <= 10; $i++) {
  46. $inside_min_uid = $min_uid + ($i - 1) * 10000;
  47. $users = DB::table('user_pay')
  48. ->select('uid')
  49. ->where([['id', '>=', $inside_min_uid], ['id', '<=', $inside_min_uid + 9999]])
  50. ->orderBy('id')
  51. ->get();
  52. foreach ($users as $user) {
  53. //单本书15天内有无订阅
  54. try {
  55. $book_order = DB::table('book_orders')
  56. ->where('uid', $user->uid)
  57. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  58. ->sum('fee');
  59. $table_seq = ($user->uid) % 512;
  60. $chapter_order = DB::connection('chapter_order_mysql')
  61. ->table('chapter_orders' . $table_seq)
  62. ->where('uid', $user->uid)
  63. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  64. ->sum('fee');
  65. //\Log::info(($user->uid).$chapter_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
  66. //}
  67. $subscribe_fee = $book_order + $chapter_order;
  68. if ($subscribe_fee > 0) {
  69. //\Log::info(($user->uid).':'.$subscribe_fee);
  70. $total_users++;
  71. $gender = 0;
  72. if ($subscribe_fee >= 3000) {
  73. $user_channel = DB::table('force_subscribe_users')
  74. ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
  75. ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
  76. ->where('force_subscribe_users.uid', $user->uid)
  77. ->first();
  78. if ($user_channel) {
  79. $gender = $user_channel->pid;
  80. } else {
  81. \Log::error('user_gender_not_found:uid:' . $user->uid);
  82. }
  83. $above_three_thousands_total++;
  84. if ($gender == 1) {
  85. $above_three_thousands_male++;
  86. }
  87. if ($gender == 2) {
  88. $above_three_thousands_felman++;
  89. }
  90. }
  91. if ($subscribe_fee >= 5000) {
  92. $above_five_thousands_total++;
  93. if ($gender == 1) {
  94. $above_five_thousands_male++;
  95. }
  96. if ($gender == 2) {
  97. $above_five_thousands_felman++;
  98. }
  99. }
  100. }
  101. } catch (\Exception $e) {
  102. \Log::error('temp task error:' . $e->getMessage());
  103. }
  104. print_r('$total_users:' . 'min_uid' . ($min_uid) . 'uid:' . ($user->id) . ':' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n");
  105. print_r('||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male . "\r\n");
  106. \Log::info('actual_total_users:' . 'min_uid' . ($min_uid) . 'uid:' . ($user->id) . ':' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n" . '||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male);
  107. //\Log::info('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
  108. }
  109. }
  110. /*
  111. DB::table('user_pay')
  112. ->orderBy('uid','asc')
  113. ->chunk(1000,function($users) use(&$total_users,&$above_five_thousands_total,&$above_five_thousands_felman,&$above_five_thousands_male,&$above_three_thousands_total,&$above_three_thousands_male,&$above_three_thousands_felman){
  114. foreach ($users as $user) {
  115. //单本书15天内有无订阅
  116. try{
  117. $book_order = DB::table('book_orders')
  118. ->where('uid',$user->uid)
  119. ->where('created_at','>=',date('Y-m-d 00:00:00',strtotime('-15 day')))
  120. ->sum('fee');
  121. //\Log::info(($user->uid).':'.$book_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
  122. //$chapter_order=0;
  123. //if(!$book_order) {
  124. $table_seq = ($user->uid)%512;
  125. $chapter_order = DB::connection('chapter_order_mysql')
  126. ->table('chapter_orders'.$table_seq)
  127. ->where('uid',$user->uid)
  128. ->where('created_at','>=',date('Y-m-d 00:00:00',strtotime('-15 day')))
  129. ->sum('fee');
  130. //\Log::info(($user->uid).$chapter_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
  131. //}
  132. $subscribe_fee = $book_order + $chapter_order;
  133. if($subscribe_fee > 0){
  134. //\Log::info(($user->uid).':'.$subscribe_fee);
  135. $total_users++;
  136. $gender =0;
  137. if($subscribe_fee>=3000){
  138. $user_channel = DB::table('force_subscribe_users')
  139. ->leftjoin('books','books.id','=','force_subscribe_users.bid')
  140. ->leftjoin('book_categories','book_categories.id','=','books.category_id')
  141. ->where('force_subscribe_users.uid',$user->uid)
  142. ->first();
  143. if($user_channel){
  144. $gender=$user_channel->pid;
  145. }else{
  146. \Log::error('user_gender_not_found:uid:'.$user->uid);
  147. }
  148. $above_three_thousands_total++;
  149. if($gender==1){
  150. $above_three_thousands_male++;
  151. }
  152. if($gender==2){
  153. $above_three_thousands_felman++;
  154. }
  155. }
  156. if($subscribe_fee>=5000) {
  157. $above_five_thousands_total++;
  158. if($gender==1){
  159. $above_five_thousands_male++;
  160. }
  161. if($gender==2){
  162. $above_five_thousands_felman++;
  163. }
  164. }
  165. }
  166. }catch (\Exception $e){
  167. \Log::error('temp task error:'.$e->getMessage());
  168. }
  169. print_r('$total_users:'.$total_users.'||$above_five_thousands_total:'.$above_five_thousands_total.'||$above_five_thousands_felman:'.$above_five_thousands_felman.'||$above_five_thousands_male:'.$above_five_thousands_male."\r\n");
  170. print_r('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
  171. \Log::info('$total_users:uid:'.($user->uid).$total_users.'||$above_five_thousands_total:'.$above_five_thousands_total.'||$above_five_thousands_felman:'.$above_five_thousands_felman.'||$above_five_thousands_male:'.$above_five_thousands_male."\r\n");
  172. \Log::info('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
  173. }
  174. });*/
  175. \Log::info('final_stats:min_uid:' . ($min_uid) . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male);
  176. print_r('final_stats:min_uid:' . ($min_uid) . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n");
  177. print_r('final_stats:min_uid:' . ($min_uid) . '||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male . "\r\n");
  178. print_r('final_stats:min_uid:' . ($min_uid) . 'end_temporary:-----------------' . date('Y-m-d H:i:s'));
  179. \Log::info('final_stats:min_uid:' . ($min_uid) . 'end_temporary:-----------------' . date('Y-m-d H:i:s'));
  180. }
  181. private function test1()
  182. {
  183. $array = ['recent_read','subscribe','text_sign','text_search','readerover','readrecord','daily_sign'];//
  184. // $array = ['daily_sign'];
  185. foreach ($array as $v){
  186. $users = Redis::SMEMBERS("temp_from_where:".$v);
  187. foreach ($users as $user){
  188. $sub = DB::connection('chapter_order_mysql')
  189. ->table('chapter_orders'.($user%512))
  190. ->where('uid',$user)
  191. ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
  192. ->first();
  193. if($sub){
  194. $fee = DB::connection('chapter_order_mysql')
  195. ->table($v.'_user')
  196. ->select('uid','fee')
  197. ->where('uid',$user)
  198. ->first();
  199. DB::connection('chapter_order_mysql')
  200. ->table($v.'_user_sub')
  201. ->insert(['uid'=>$user,'fee'=>isset($fee->fee)?$fee->fee:-1]);
  202. }
  203. }
  204. /*DB::connection('chapter_order_mysql')
  205. ->table($v.'_user_sub')
  206. ->select('uid','fee')
  207. ->orderBy('id')
  208. ->chunk(10000,function($users) use($v){
  209. foreach ($users as $user){
  210. $sub = DB::table('chapter_orders'.(($user->uid)%512))
  211. ->where('uid',$user->uid)
  212. ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
  213. ->first();
  214. if($sub){
  215. DB::table($v.'_user_sub')->insert(['uid'=>$user->uid,'fee'=>$user->fee]);
  216. }
  217. }
  218. });*/
  219. //$total_users = $above_five_thousands_total = $above_five_thousands_male = $above_five_thousands_felman = 0;
  220. //$above_three_thousands_total = $above_three_thousands_male = $above_three_thousands_felman = 0;
  221. //$users = Redis::SMEMBERS("temp_from_where:".$v);
  222. /*foreach ($users as $user) {
  223. DB::connection('chapter_order_mysql')->table($v.'_user')->insert(['uid'=>$user]);
  224. /*$book_order = DB::table('book_orders')
  225. ->where('uid', $user)
  226. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  227. ->first();
  228. $table_seq = ($user) % 512;
  229. $chapter_order = DB::connection('chapter_order_mysql')
  230. ->table('chapter_orders' . $table_seq)
  231. ->where('uid', $user)
  232. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
  233. ->first();
  234. if ($book_order || $chapter_order) {
  235. //\Log::info(($user->uid).':'.$subscribe_fee);
  236. $total_users++;
  237. $chapter_order_sum = DB::connection('chapter_order_mysql')
  238. ->table('chapter_orders' . $table_seq)
  239. ->where('uid', $user)
  240. ->sum('fee');
  241. $book_order_sum = DB::table('book_orders')
  242. ->where('uid', $user)
  243. ->sum('fee');
  244. $subscribe_fee = $book_order_sum + $chapter_order_sum;
  245. $gender = 0;
  246. if ($subscribe_fee >= 3000) {
  247. $user_channel = DB::table('force_subscribe_users')
  248. ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
  249. ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
  250. ->where('force_subscribe_users.uid', $user)
  251. ->first();
  252. if ($user_channel) {
  253. $gender = $user_channel->pid;
  254. } else {
  255. \Log::error('user_gender_not_found:uid:' . $user);
  256. }
  257. $above_three_thousands_total++;
  258. if ($gender == 1) {
  259. $above_three_thousands_male++;
  260. }
  261. if ($gender == 2) {
  262. $above_five_thousands_felman++;
  263. }
  264. }
  265. if ($subscribe_fee >= 5000) {
  266. $user_channel = DB::table('force_subscribe_users')
  267. ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
  268. ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
  269. ->where('force_subscribe_users.uid', $user)
  270. ->first();
  271. if ($user_channel) {
  272. $gender = $user_channel->pid;
  273. } else {
  274. \Log::error('user_gender_not_found:uid:' . $user);
  275. }
  276. $above_three_thousands_total++;
  277. if ($gender == 1) {
  278. $above_three_thousands_male++;
  279. }
  280. if ($gender == 2) {
  281. $above_five_thousands_felman++;
  282. }
  283. }
  284. //}
  285. }*/
  286. //return true;
  287. /*myLog('temp_from_where')->info($v.'---------------------------------------------------');
  288. myLog('temp_from_where')->info(compact(
  289. 'v',
  290. 'total_users',
  291. 'above_five_thousands_total',
  292. 'above_five_thousands_male',
  293. 'above_five_thousands_felman',
  294. 'above_three_thousands_total',
  295. 'above_three_thousands_male',
  296. 'above_three_thousands_felman'
  297. ));*/
  298. }
  299. }
  300. private function test2() {
  301. $users = DB::table('daily_sign_user_sub')->where('fee',-1)->get();
  302. foreach ($users as $user){
  303. $fee = DB::table('chapter_orders'.($user%512))
  304. ->where('uid',$user->uid)
  305. ->where('created_at','<=','2018-12-07 23:59:59')
  306. ->sum('fee');
  307. $user->fee = $fee;
  308. $user->save();
  309. }
  310. }
  311. }