WapVisitStat.php 28 KB

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