WapVisitStat.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Redis;
  5. use DB;
  6. use App\Modules\User\Services\ReadRecordService;
  7. class WapVisitStat extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'wvs {type} {--extra=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'redis total';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $options = $this->option('extra');
  38. $param = $this->argument('type');
  39. if(empty($param)){
  40. echo 'wvs {type:1,2,3,4,5...} {--extra}'.PHP_EOL;
  41. return false;
  42. }
  43. if(!is_numeric($param)){
  44. return false;
  45. }
  46. if($options && !is_numeric($options)){
  47. return false;
  48. }
  49. switch ($param){
  50. case 1:
  51. $this->sendorderstats();
  52. break;
  53. case 2:
  54. $this->sendOrderStatsEveryDay();
  55. break;
  56. case 3:
  57. $this->allstats();
  58. break;
  59. case 4:
  60. $this->allStatsEveryDay();
  61. break;
  62. case 5:
  63. $this->activitystats();
  64. break;
  65. case 6:
  66. $this->activityStatsEveryDay($options);
  67. break;
  68. case 7:
  69. $this->beforeForceSubAndBeforeVipStats();
  70. break;
  71. case 8:
  72. $this->beforeForceSubAndBeforeVipStatsEveryDay();
  73. break;
  74. case 9:
  75. $this->start();
  76. break;
  77. case 10:
  78. $this->subscribeKeyDel();
  79. break;
  80. case 11:
  81. $this->substituteStats();
  82. break;
  83. }
  84. return false;
  85. }
  86. public function start()
  87. {
  88. $this->allStatsEveryDay();
  89. $this->sendOrderStatsEveryDay();
  90. $this->nowActivitys();
  91. $this->beforeForceSubAndBeforeVipStatsEveryDay();
  92. $this->browserSendOrderStatsEveryDay();
  93. $this->substituteStats();
  94. }
  95. private function sendorderstats()
  96. {
  97. $day = date('Y-m-d',time()-86400);
  98. // Redis::hincrby('send_order_uv_'.$param,$key,1);
  99. //Redis::hincrby('send_order_pv_'.$param,$key,1);//每天
  100. DB::table('send_orders')->select('id')->orderBy('id')->where('created_at', '<', date('Y-m-d H:i:s'))->chunk(1000, function ($res) use ($day){
  101. $now = date('Y-m-d H:i:s');
  102. foreach ($res as $v) {
  103. $temp = Redis::hgetall('send_order_pv_' . $v->id);
  104. if ($temp) {
  105. $data = [];
  106. foreach ($temp as $d => $u) {
  107. if ($d == 'total') {
  108. continue;
  109. }
  110. if($d == date('Y-m-d')){
  111. continue;
  112. }
  113. //$temp_uv = (int)Redis::hget('send_order_uv_' . $v->id, $d);
  114. //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => 'send_orders', 'pv' => $u, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];
  115. Redis::hdel('send_order_pv_' . $v->id,$d);
  116. Redis::hdel('send_order_uv_' . $v->id,$d);
  117. }
  118. //DB::table('wap_visit_stats')->insert($data);
  119. if(!Redis::hget('send_order_pv_' . $v->id,date('Y-m-d'))){
  120. Redis::del('send_order_pv_' . $v->id);
  121. }
  122. if(!Redis::hget('send_order_uv_' . $v->id,date('Y-m-d'))){
  123. Redis::del('send_order_uv_' . $v->id);
  124. }
  125. }
  126. }
  127. });
  128. }
  129. private function sendOrderStatsEveryDay()
  130. {
  131. $day = date('Y-m-d', time() - 86400);
  132. $now = date('Y-m-d H:i:s');
  133. $key_like = 'send_order_pv_';
  134. //$keys = Redis::keys($key_like . '*');
  135. $data = [];
  136. $i = 0;
  137. $all_send_order_id = Redis::SMEMBERS('send_order'.$day);
  138. foreach ($all_send_order_id as $send_order_id) {
  139. $key = $key_like.$send_order_id;
  140. $temp = Redis::hget($key, $day);
  141. $temp_uv = (int)Redis::hget('send_order_uv_' . $send_order_id, $day);
  142. $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];
  143. if ($i && $i % 100 == 0) {
  144. DB::table('wap_visit_stats')->insert($data);
  145. $data = [];
  146. }
  147. $i++;
  148. Redis::hdel($key,$day);
  149. if(!Redis::hget($key,date('Y-m-d'))){
  150. Redis::del($key);
  151. }
  152. Redis::hdel('send_order_uv_' . $send_order_id,$day);
  153. if(!Redis::hget('send_order_uv_' . $send_order_id,date('Y-m-d'))){
  154. Redis::del('send_order_uv_' . $send_order_id);
  155. }
  156. }
  157. if($data){
  158. DB::table('wap_visit_stats')->insert($data);
  159. }
  160. Redis::del('send_order'.$day);
  161. }
  162. private function browserSendOrderStatsEveryDay()
  163. {
  164. $day = date('Y-m-d', time() - 86400);
  165. $now = date('Y-m-d H:i:s');
  166. $key_like = 'browser_send_order_pv_';
  167. //$keys = Redis::keys($key_like . '*');
  168. $data = [];
  169. $i = 0;
  170. $all_send_order_id = Redis::SMEMBERS('browser_send_order'.$day);
  171. foreach ($all_send_order_id as $send_order_id) {
  172. $key = $key_like.$send_order_id;
  173. $temp = Redis::hget($key, $day);
  174. $temp_uv = (int)Redis::hget('browser_send_order_uv_' . $send_order_id, $day);
  175. $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'browser_send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];
  176. if ($i && $i % 100 == 0) {
  177. DB::table('wap_visit_stats')->insert($data);
  178. $data = [];
  179. }
  180. $i++;
  181. Redis::hdel($key,$day);
  182. if(!Redis::hget($key,date('Y-m-d'))){
  183. Redis::del($key);
  184. }
  185. Redis::hdel('browser_send_order_uv_' . $send_order_id,$day);
  186. if(!Redis::hget('browser_send_order_uv_' . $send_order_id,date('Y-m-d'))){
  187. Redis::del('browser_send_order_uv_' . $send_order_id);
  188. }
  189. }
  190. if($data){
  191. DB::table('wap_visit_stats')->insert($data);
  192. }
  193. Redis::del('browser_send_order'.$day);
  194. }
  195. private function allstats()
  196. {
  197. //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);
  198. //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
  199. $day = date('Y-m-d');
  200. $now = date('Y-m-d H:i:s');
  201. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  202. //$all_channel_id = [14];
  203. foreach ($all_channel_id as $v) {
  204. if($v->id == 2){
  205. continue;
  206. }
  207. $like_pv_key = 'customer:push:click:distribution_channel_id:' . $v->id . 'from:';
  208. $froms = $this->getFrom($v->id);
  209. foreach ($froms as $from) {
  210. $true_key = $like_pv_key.$from;
  211. //echo '$true_key: '.$true_key.PHP_EOL;
  212. $temp = Redis::hgetall($true_key);
  213. $data = [];
  214. $i = 0;
  215. foreach ($temp as $d => $u) {
  216. if($d == $day){
  217. continue;
  218. }
  219. $uv_key = 'push:distribution_channel_id:' . $v->id . 'from:' . $from . ':date:' . $d;
  220. $temp_uv = (int)Redis::scard($uv_key);
  221. $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $from, 'pv' => $u, 'uv' => $temp_uv, 'type' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  222. Redis::del($uv_key);
  223. Redis::hdel($true_key,$d);
  224. if($i && $i%100 == 0){
  225. DB::table('wap_visit_stats')->insert($data);
  226. $data = [];
  227. }
  228. $i++;
  229. }
  230. $temp = Redis::hget($true_key,date('Y-m-d'));
  231. if(!$temp){
  232. Redis::del($true_key);
  233. }
  234. DB::table('wap_visit_stats')->insert($data);
  235. }
  236. }
  237. }
  238. private function allStatsEveryDay()
  239. {
  240. //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);
  241. //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
  242. $day = date('Y-m-d', time() - 86400);
  243. $now = date('Y-m-d H:i:s');
  244. //$all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  245. $alls = Redis::SMEMBERS('wap_from_stats'.$day);
  246. $i = 0;
  247. $data = [];
  248. foreach ($alls as $v){
  249. $arr = explode('-',$v);
  250. $distribution_channel_id = $arr[0];
  251. $from = $arr[1];
  252. if(count($arr) == 3){
  253. $from = $arr[1].$arr[2];
  254. }
  255. $pv_key = 'customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from;
  256. $pv = (int)Redis::hget($pv_key, $day);
  257. $uv_key = 'push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $day;
  258. //echo '$pv_key: '.$pv_key.',$uv_key: $uv_key'.$uv_key.PHP_EOL;
  259. $temp_uv = (int)Redis::scard($uv_key);
  260. //echo $pv.'--'.$temp_uv.PHP_EOL;
  261. $data[] = ['day' => $day, 'key' => $distribution_channel_id, 'from_type' => $from, 'pv' => $pv, 'uv' => $temp_uv, 'type' => 1, 'created_at' => $now, 'updated_at' => $now];
  262. if($i && $i%100 == 0){
  263. DB::table('wap_visit_stats')->insert($data);
  264. $data = [];
  265. }
  266. $i++;
  267. Redis::del($uv_key);
  268. Redis::hdel($pv_key, $day);
  269. //$temp = Redis::hget($pv_key,date('Y-m-d'));
  270. $temp = Redis::hget($pv_key,date('Y-m-d'));
  271. if(!$temp){
  272. Redis::del($pv_key);
  273. }
  274. }
  275. Redis::del('wap_from_stats'.$day);
  276. if($data){
  277. DB::table('wap_visit_stats')->insert($data);
  278. }
  279. }
  280. private function activitystats()
  281. {
  282. $day = date('Y-m-d');
  283. $now = date('Y-m-d H:i:s');
  284. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  285. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  286. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  287. foreach ($all_channel_id as $v) {
  288. for ($i = 1; $i <= 140; $i++) {
  289. $pv_key = sprintf($pv_key_format, $i, $v->id);
  290. $pv_temp = Redis::hgetall($pv_key);
  291. if ($pv_temp) {
  292. $data = [];
  293. foreach ($pv_temp as $d => $u) {
  294. if($d == $day){
  295. continue;
  296. }
  297. $uv_key = sprintf($uv_key_format, $i, $v->id, $d);
  298. //$temp_uv = (int)Redis::scard($uv_key);
  299. //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];
  300. Redis::del($uv_key);
  301. }
  302. //DB::table('wap_visit_stats')->insert($data);
  303. }
  304. Redis::del($pv_key);
  305. }
  306. }
  307. }
  308. private function activityStatsEveryDay($activity_id)
  309. {
  310. $day = date('Y-m-d',time()-86400);
  311. $now = date('Y-m-d H:i:s');
  312. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  313. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  314. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  315. foreach ($all_channel_id as $v) {
  316. $i = $activity_id;
  317. $pv_key = sprintf($pv_key_format, $i, $v->id);
  318. $pv_temp = Redis::hgetall($pv_key);
  319. if ($pv_temp) {
  320. $data = [];
  321. foreach ($pv_temp as $d => $u) {
  322. if($d != $day){
  323. continue;
  324. }
  325. $uv_key = sprintf($uv_key_format, $i, $v->id, $d);
  326. $temp_uv = (int)Redis::scard($uv_key);
  327. $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];
  328. Redis::del($uv_key);
  329. Redis::hdel($pv_key,$d);
  330. }
  331. DB::table('wap_visit_stats')->insert($data);
  332. if( !Redis::hget($pv_key,date('Y-m-d'))){
  333. Redis::del($pv_key);
  334. }
  335. }
  336. }
  337. }
  338. private function nowActivitys(){
  339. $day = date('Y-m-d',time()-86400);
  340. $activty_ids = DB::table('activity')->where('start_time','<=',$day.' 23:00:00')->where('end_time','>=',$day)->select('id')->get();
  341. foreach ($activty_ids as $v){
  342. //echo $v->id.PHP_EOL;
  343. $this->activityStatsEveryDay($v->id);
  344. }
  345. }
  346. private function beforeForceSubAndBeforeVipStats()
  347. {
  348. //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;
  349. //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;
  350. $send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();
  351. $now = date('Y-m-d H:i:s');
  352. for ($i = 1; $i < 100; $i++) {
  353. $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';
  354. //$all_key = Redis::keys($like_key . '*');
  355. if ($send_order_ids) {
  356. foreach ($send_order_ids as $ids) {
  357. $data = [];
  358. $send_order_id = $ids->id;
  359. $kkk = $like_key.$send_order_id;
  360. $temp_pv = Redis::hgetall($kkk);
  361. foreach ($temp_pv as $d => $u) {
  362. if($d == 'total'){
  363. continue;
  364. }
  365. if($d == date('Y-m-d')){
  366. continue;
  367. }
  368. $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $d . ':send_order_id:' . $send_order_id;
  369. //$temp_uv = (int)Redis::scard($uv_key);
  370. //$data[] = ['day' => $d, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $u, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];
  371. Redis::del($uv_key);
  372. Redis::hdel($kkk,$d);
  373. }
  374. if(!Redis::hget($kkk,date('Y-m-d'))){
  375. Redis::del($kkk);
  376. }
  377. //DB::table('wap_visit_stats')->insert($data);
  378. }
  379. }
  380. }
  381. //$uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  382. //$pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  383. for ($i = 1; $i < 100; $i++){
  384. $like_key = 'book:' . $i . ':beforeforcesub::pvsend_order_id:';
  385. //$send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();
  386. //$all_key = Redis::keys($like_key . '*');
  387. if($send_order_ids){
  388. foreach ($send_order_ids as $vvvv){
  389. $send_order_id = $vvvv->id;
  390. $ks = $like_key.$send_order_id;
  391. $data = [];
  392. $t_pv = Redis::hgetall($ks);
  393. foreach ($t_pv as $day => $v){
  394. if($day == 'total'){
  395. continue;
  396. }
  397. if($day == date('Y-m-d')){
  398. continue;
  399. }
  400. $uv_key = 'book:' . $i . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  401. //$t_uv = (int)Redis::scard($uv_key);
  402. //$data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $v, 'uv' => $t_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];
  403. Redis::del($uv_key);
  404. Redis::hdel($ks,$day);
  405. }
  406. if(!Redis::hget($ks,date('Y-m-d'))){
  407. Redis::del($ks);
  408. }
  409. //DB::table('wap_visit_stats')->insert($data);
  410. }
  411. }
  412. }
  413. }
  414. private function beforeForceSubAndBeforeVipStatsEveryDay()
  415. {
  416. //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;
  417. //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;
  418. $now = date('Y-m-d H:i:s');
  419. $day = date('Y-m-d',time()-86400);
  420. //Redis::SISMEMBER('beforevip'.$date,$book->bid.'-'.$send_order_id);
  421. $alls = Redis::SMEMBERS('beforevip'.$day);
  422. $j = 0;
  423. $data = [];
  424. foreach ($alls as $v){
  425. $temp = explode('-',$v);
  426. $i = $temp[0];
  427. $send_order_id = $temp[1];
  428. $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';
  429. $kkk = $like_key.$send_order_id;
  430. $temp_pv = Redis::hget($kkk,$day);
  431. $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  432. $temp_uv = (int)Redis::scard($uv_key);
  433. $data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];
  434. Redis::del($uv_key);
  435. Redis::hdel($kkk,$day);
  436. if($j && $j%100 == 0){
  437. DB::table('wap_visit_stats')->insert($data);
  438. $data = [];
  439. }
  440. if(!Redis::hget($kkk,date('Y-m-d'))){
  441. Redis::del($kkk);
  442. }
  443. $j++;
  444. }
  445. if($data){
  446. DB::table('wap_visit_stats')->insert($data);
  447. }
  448. Redis::del('beforevip'.$day);
  449. $data = [];
  450. // Redis::sadd('beforeforcesub'.$date,$book->bid.'-'.$send_order_id);
  451. $alls = Redis::SMEMBERS('beforeforcesub'.$day);
  452. if($alls){
  453. $i = 0;
  454. foreach ($alls as $vs){
  455. $temp = explode('-',$vs);
  456. $bid = $temp[0];
  457. $send_order_id = $temp[1];
  458. $like_key = 'book:' . $bid . ':beforeforcesub::pvsend_order_id:';
  459. $kkk = $like_key.$send_order_id;
  460. $temp_pv = Redis::hget($kkk,$day);
  461. $uv_key = 'book:' . $bid . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  462. $temp_uv = (int)Redis::scard($uv_key);
  463. $data[] = ['day' => $day, 'key' => $bid, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];
  464. Redis::del($uv_key);
  465. Redis::hdel($kkk,$day);
  466. if($i && $i%100 == 0){
  467. DB::table('wap_visit_stats')->insert($data);
  468. $data = [];
  469. }
  470. if(!Redis::hget($kkk,date('Y-m-d'))){
  471. Redis::del($kkk);
  472. }
  473. $i++;
  474. }
  475. if($data){
  476. DB::table('wap_visit_stats')->insert($data);
  477. }
  478. }
  479. Redis::del('beforeforcesub'.$day);
  480. }
  481. private function getFrom(int $channel_id):array {
  482. $from = ['point_push','not_pay','hot_push','unpaid_send_activity','pay_daily_push',
  483. 'auto_custom_trusteeship','subscribe_push','sign_push','reader','signcallback','reader-year','recovery_push'];
  484. $custom_ids = DB::table('custom_send_msgs')->select('id','trusteeship')->where('distribution_channel_id',$channel_id)->get();
  485. if($custom_ids){
  486. foreach ($custom_ids as $item){
  487. if($item->trusteeship){
  488. for ($i=1;$i<=4;$i++){
  489. $from[] = 'custom_'.$item->id.'_'.$i;
  490. }
  491. $from[] = 'custom_'.$item->id;
  492. }else{
  493. $from[] = 'custom_'.$item->id;
  494. }
  495. }
  496. }
  497. $template = DB::table('wechat_template_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();
  498. if($template) {
  499. foreach ($template as $val){
  500. $from[] = 'template_'.$val->id;
  501. }
  502. }
  503. $keyword = DB::table('wechat_keyword_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();
  504. if($keyword){
  505. foreach ($keyword as $value){
  506. $from[] = 'keyword_'.$value->id;
  507. }
  508. }
  509. return $from;
  510. }
  511. private function subscribeKeyDel(){
  512. $account = $this->officialAccount();
  513. $key_format = 'www.zhuishuyun.comforce_subscribe:scene:%s:%s';
  514. //print_r($account);return;
  515. /*for ($i=1;$i<10000;$i++){
  516. foreach ($account as $appid){
  517. Redis::del(sprintf($key_format,$i,$appid));
  518. }
  519. }*/
  520. for ($i = 10;$i<100;$i++){
  521. $skip = $i*10000;
  522. $res = DB::table('users')->select('id','distribution_channel_id')->orderBy('id')->where('id','>=',$skip)->limit(10000)->get();
  523. foreach ($res as $v){
  524. if(isset($account[$v->distribution_channel_id]) && $account[$v->distribution_channel_id]){
  525. foreach ($account[$v->distribution_channel_id] as $appid){
  526. Redis::del(sprintf($key_format,$i,$appid));
  527. }
  528. }
  529. }
  530. $res = null;
  531. }
  532. }
  533. private function officialAccount():array{
  534. $res = DB::table('official_accounts')->select('distribution_channel_id','appid')->get();
  535. $data = [];
  536. foreach ($res as $v){
  537. $data[$v->distribution_channel_id][] = $v->appid;
  538. //$data[] = $v->appid;
  539. }
  540. return $data;
  541. }
  542. private function substituteStats(){
  543. $day = date('Y-m-d',time()-86400);
  544. //代充值按钮
  545. $substituteButtonUvPv = Redis::SMEMBERS('substitutebuttonpvuv'.$day);
  546. if($substituteButtonUvPv){
  547. $i = 0;
  548. $data = [];
  549. foreach ($substituteButtonUvPv as $channel_id){
  550. $temp_pv = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);
  551. $temp_uv_key = sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);
  552. $temp_uv = Redis::scard($temp_uv_key);
  553. $data[] = [
  554. 'day' => $day,
  555. 'key' => $channel_id,
  556. 'from_type' => 'substitutebuttonpvuv',
  557. 'pv' => $temp_pv,
  558. 'uv' => $temp_uv,
  559. 'type' => 6,
  560. 'created_at' => date('Y-m-d H:i:s'),
  561. 'updated_at' => date('Y-m-d H:i:s')
  562. ];
  563. if($i && $i%100 == 0){
  564. DB::table('wap_visit_stats')->insert($data);
  565. $data = [];
  566. }
  567. $i++;
  568. Redis::del($temp_uv_key);
  569. Redis::hdel('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);
  570. $temp = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));
  571. if(!$temp){
  572. Redis::del('substitutebuttonpvuv:distribution_channel_id:' . $channel_id);
  573. }
  574. }
  575. if($data){
  576. DB::table('wap_visit_stats')->insert($data);
  577. }
  578. }
  579. Redis::del('substitutebuttonpvuv'.$day);
  580. //代充值页面
  581. $substituteButtonUvPv = Redis::SMEMBERS('substitutepagepvuv'.$day);
  582. if($substituteButtonUvPv){
  583. $i = 0;
  584. $data = [];
  585. foreach ($substituteButtonUvPv as $channel_id){
  586. $temp_pv = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);
  587. $temp_uv_key = sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);
  588. $temp_uv = Redis::scard($temp_uv_key);
  589. $data[] = [
  590. 'day' => $day,
  591. 'key' => $channel_id,
  592. 'from_type' => 'substitutepagepvuv',
  593. 'pv' => $temp_pv,
  594. 'uv' => $temp_uv,
  595. 'type' => 7,
  596. 'created_at' => date('Y-m-d H:i:s'),
  597. 'updated_at' => date('Y-m-d H:i:s')
  598. ];
  599. if($i && $i%100 == 0){
  600. DB::table('wap_visit_stats')->insert($data);
  601. $data = [];
  602. }
  603. $i++;
  604. Redis::del($temp_uv_key);
  605. Redis::hdel('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);
  606. $temp = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));
  607. if(!$temp){
  608. Redis::del('substitutepagepvuv:distribution_channel_id:' . $channel_id);
  609. }
  610. }
  611. if($data){
  612. DB::table('wap_visit_stats')->insert($data);
  613. }
  614. }
  615. Redis::del('substitutepagepvuv'.$day);
  616. }
  617. }