WapVisitStat.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. <?php
  2. namespace App\Console\Commands;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Console\Command;
  5. use Redis;
  6. use DB;
  7. use App\Modules\User\Services\ReadRecordService;
  8. class WapVisitStat extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'wvs {type} {--extra=}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'redis total';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $options = $this->option('extra');
  39. $param = $this->argument('type');
  40. if(empty($param)){
  41. echo 'wvs {type:1,2,3,4,5...} {--extra}'.PHP_EOL;
  42. return false;
  43. }
  44. if(!is_numeric($param)){
  45. return false;
  46. }
  47. if($options && !is_numeric($options)){
  48. return false;
  49. }
  50. switch ($param){
  51. case 1:
  52. $this->sendorderstats();
  53. break;
  54. case 2:
  55. $this->sendOrderStatsEveryDay();
  56. break;
  57. case 3:
  58. $this->allstats();
  59. break;
  60. case 4:
  61. $this->allStatsEveryDay();
  62. break;
  63. case 5:
  64. $this->activitystats();
  65. break;
  66. case 6:
  67. $this->activityStatsEveryDay($options);
  68. break;
  69. case 7:
  70. $this->beforeForceSubAndBeforeVipStats();
  71. break;
  72. case 8:
  73. $this->beforeForceSubAndBeforeVipStatsEveryDay();
  74. break;
  75. case 9:
  76. $this->start();
  77. break;
  78. case 10:
  79. $this->subscribeKeyDel();
  80. break;
  81. case 11:
  82. $this->substituteStats();
  83. break;
  84. case 12:
  85. $this->StylePushStats();
  86. break;
  87. case 13:
  88. $this->crm();
  89. break;
  90. }
  91. return false;
  92. }
  93. public function start()
  94. {
  95. $this->allStatsEveryDay();
  96. $this->sendOrderStatsEveryDay();
  97. $this->nowActivitys();
  98. $this->beforeForceSubAndBeforeVipStatsEveryDay();
  99. $this->browserSendOrderStatsEveryDay();
  100. $this->substituteStats();
  101. $this->StylePushStats();
  102. $this->crm();
  103. $this->InnerOuterWeixinStats();
  104. }
  105. private function sendorderstats()
  106. {
  107. $day = date('Y-m-d',time()-86400);
  108. // Redis::hincrby('send_order_uv_'.$param,$key,1);
  109. //Redis::hincrby('send_order_pv_'.$param,$key,1);//每天
  110. DB::table('send_orders')->select('id')->orderBy('id')->where('created_at', '<', date('Y-m-d H:i:s'))->chunk(1000, function ($res) use ($day){
  111. $now = date('Y-m-d H:i:s');
  112. foreach ($res as $v) {
  113. $temp = Redis::hgetall('send_order_pv_' . $v->id);
  114. if ($temp) {
  115. $data = [];
  116. foreach ($temp as $d => $u) {
  117. if ($d == 'total') {
  118. continue;
  119. }
  120. if($d == date('Y-m-d')){
  121. continue;
  122. }
  123. //$temp_uv = (int)Redis::hget('send_order_uv_' . $v->id, $d);
  124. //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => 'send_orders', 'pv' => $u, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];
  125. Redis::hdel('send_order_pv_' . $v->id,$d);
  126. Redis::hdel('send_order_uv_' . $v->id,$d);
  127. }
  128. //DB::table('wap_visit_stats')->insert($data);
  129. if(!Redis::hget('send_order_pv_' . $v->id,date('Y-m-d'))){
  130. Redis::del('send_order_pv_' . $v->id);
  131. }
  132. if(!Redis::hget('send_order_uv_' . $v->id,date('Y-m-d'))){
  133. Redis::del('send_order_uv_' . $v->id);
  134. }
  135. }
  136. }
  137. });
  138. }
  139. private function sendOrderStatsEveryDay()
  140. {
  141. $day = date('Y-m-d', time() - 86400);
  142. $now = date('Y-m-d H:i:s');
  143. $key_like = 'send_order_pv_';
  144. //$keys = Redis::keys($key_like . '*');
  145. $data = [];
  146. $i = 0;
  147. $all_send_order_id = Redis::SMEMBERS('send_order'.$day);
  148. Redis::EXPIRE('send_order'.$day,86400);
  149. foreach ($all_send_order_id as $send_order_id) {
  150. $key = $key_like.$send_order_id;
  151. $temp = Redis::hget($key, $day);
  152. $temp_uv = (int)Redis::hget('send_order_uv_' . $send_order_id, $day);
  153. $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];
  154. if ($i && $i % 100 == 0) {
  155. DB::table('wap_visit_stats')->insert($data);
  156. $data = [];
  157. }
  158. $i++;
  159. Redis::hdel($key,$day);
  160. if(!Redis::hget($key,date('Y-m-d'))){
  161. Redis::del($key);
  162. }
  163. Redis::hdel('send_order_uv_' . $send_order_id,$day);
  164. if(!Redis::hget('send_order_uv_' . $send_order_id,date('Y-m-d'))){
  165. Redis::del('send_order_uv_' . $send_order_id);
  166. }
  167. }
  168. if($data){
  169. DB::table('wap_visit_stats')->insert($data);
  170. }
  171. //Redis::del('send_order'.$day);
  172. }
  173. private function browserSendOrderStatsEveryDay()
  174. {
  175. $day = date('Y-m-d', time() - 86400);
  176. $now = date('Y-m-d H:i:s');
  177. $key_like = 'browser_send_order_pv_';
  178. //$keys = Redis::keys($key_like . '*');
  179. $data = [];
  180. $i = 0;
  181. $all_send_order_id = Redis::SMEMBERS('browser_send_order'.$day);
  182. foreach ($all_send_order_id as $send_order_id) {
  183. $key = $key_like.$send_order_id;
  184. $temp = Redis::hget($key, $day);
  185. $temp_uv = (int)Redis::hget('browser_send_order_uv_' . $send_order_id, $day);
  186. $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];
  187. if ($i && $i % 100 == 0) {
  188. DB::table('wap_visit_stats')->insert($data);
  189. $data = [];
  190. }
  191. $i++;
  192. Redis::hdel($key,$day);
  193. if(!Redis::hget($key,date('Y-m-d'))){
  194. Redis::del($key);
  195. }
  196. Redis::hdel('browser_send_order_uv_' . $send_order_id,$day);
  197. if(!Redis::hget('browser_send_order_uv_' . $send_order_id,date('Y-m-d'))){
  198. Redis::del('browser_send_order_uv_' . $send_order_id);
  199. }
  200. }
  201. if($data){
  202. DB::table('wap_visit_stats')->insert($data);
  203. }
  204. Redis::del('browser_send_order'.$day);
  205. }
  206. private function allstats()
  207. {
  208. //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);
  209. //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
  210. $day = date('Y-m-d');
  211. $now = date('Y-m-d H:i:s');
  212. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  213. //$all_channel_id = [14];
  214. foreach ($all_channel_id as $v) {
  215. if($v->id == 2){
  216. continue;
  217. }
  218. $like_pv_key = 'customer:push:click:distribution_channel_id:' . $v->id . 'from:';
  219. $froms = $this->getFrom($v->id);
  220. foreach ($froms as $from) {
  221. $true_key = $like_pv_key.$from;
  222. //echo '$true_key: '.$true_key.PHP_EOL;
  223. $temp = Redis::hgetall($true_key);
  224. $data = [];
  225. $i = 0;
  226. foreach ($temp as $d => $u) {
  227. if($d == $day){
  228. continue;
  229. }
  230. $uv_key = 'push:distribution_channel_id:' . $v->id . 'from:' . $from . ':date:' . $d;
  231. $temp_uv = (int)Redis::scard($uv_key);
  232. $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')];
  233. Redis::del($uv_key);
  234. Redis::hdel($true_key,$d);
  235. if($i && $i%100 == 0){
  236. DB::table('wap_visit_stats')->insert($data);
  237. $data = [];
  238. }
  239. $i++;
  240. }
  241. $temp = Redis::hget($true_key,date('Y-m-d'));
  242. if(!$temp){
  243. Redis::del($true_key);
  244. }
  245. DB::table('wap_visit_stats')->insert($data);
  246. }
  247. }
  248. }
  249. private function allStatsEveryDay()
  250. {
  251. //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);
  252. //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
  253. $day = date('Y-m-d', time() - 86400);
  254. $now = date('Y-m-d H:i:s');
  255. //$all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  256. $alls = Redis::SMEMBERS('wap_from_stats'.$day);
  257. $i = 0;
  258. $data = [];
  259. $wechatKeyData = [];
  260. foreach ($alls as $v){
  261. $arr = explode('-',$v);
  262. $distribution_channel_id = $arr[0];
  263. $from = $arr[1];
  264. if(count($arr) == 3){
  265. $from = $arr[1].$arr[2];
  266. }
  267. $pv_key = 'customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from;
  268. $pv = (int)Redis::hget($pv_key, $day);
  269. $uv_key = 'push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $day;
  270. //echo '$pv_key: '.$pv_key.',$uv_key: $uv_key'.$uv_key.PHP_EOL;
  271. $temp_uv = (int)Redis::scard($uv_key);
  272. //echo $pv.'--'.$temp_uv.PHP_EOL;
  273. if(starts_with($from, 'keyword_')){
  274. $wechatKeyData[] = ['from'=>$from,'distribution_channel_id'=>$distribution_channel_id,'pv'=>$pv,'uv'=>$temp_uv];
  275. }
  276. $data[] = ['day' => $day, 'key' => $distribution_channel_id, 'from_type' => $from, 'pv' => $pv, 'uv' => $temp_uv, 'type' => 1, 'created_at' => $now, 'updated_at' => $now];
  277. if($i && $i%100 == 0){
  278. DB::table('wap_visit_stats')->insert($data);
  279. $data = [];
  280. }
  281. $i++;
  282. Redis::del($uv_key);
  283. Redis::hdel($pv_key, $day);
  284. //$temp = Redis::hget($pv_key,date('Y-m-d'));
  285. $temp = Redis::hget($pv_key,date('Y-m-d'));
  286. if(!$temp){
  287. Redis::del($pv_key);
  288. }
  289. }
  290. Redis::del('wap_from_stats'.$day);
  291. if($data){
  292. DB::table('wap_visit_stats')->insert($data);
  293. }
  294. if($wechatKeyData){
  295. foreach ($wechatKeyData as $value) {
  296. list($tmep,$id) = explode('_',$value['from']);
  297. if(!$id) continue;
  298. $we_info = DB::table('wechat_keyword_stats')->where('wechat_keyword_id',$id)->where('date',$day)->first();
  299. if($we_info){
  300. DB::table('wechat_keyword_stats')->where('wechat_keyword_id',$id)->where('date',$day)->update(
  301. ['pv'=>$value['pv'],'uv'=>$value['uv'],'visist_updated_at'=>date('Y-m-d H:i:s')]
  302. );
  303. }else{
  304. try{
  305. DB::table('wechat_keyword_stats')->insert([
  306. 'wechat_keyword_id'=>$id,
  307. 'date'=>$day,
  308. 'pv'=>$value['pv'],
  309. 'uv'=>$value['uv'],
  310. 'charge_amount'=>0,
  311. 'success_order_num'=>0,
  312. 'created_at'=>date('Y-m-d H:i:s'),
  313. 'updated_at'=>date('Y-m-d H:i:s'),
  314. 'visist_updated_at'=>date('Y-m-d H:i:s'),
  315. 'charge_updated_at'=>date('Y-m-d H:i:s'),
  316. ]);
  317. }catch (\Exception $e){}
  318. }
  319. }
  320. }
  321. }
  322. private function activitystats()
  323. {
  324. $day = date('Y-m-d');
  325. $now = date('Y-m-d H:i:s');
  326. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  327. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  328. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  329. foreach ($all_channel_id as $v) {
  330. for ($i = 1; $i <= 140; $i++) {
  331. $pv_key = sprintf($pv_key_format, $i, $v->id);
  332. $pv_temp = Redis::hgetall($pv_key);
  333. if ($pv_temp) {
  334. $data = [];
  335. foreach ($pv_temp as $d => $u) {
  336. if($d == $day){
  337. continue;
  338. }
  339. $uv_key = sprintf($uv_key_format, $i, $v->id, $d);
  340. //$temp_uv = (int)Redis::scard($uv_key);
  341. //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];
  342. Redis::del($uv_key);
  343. }
  344. //DB::table('wap_visit_stats')->insert($data);
  345. }
  346. Redis::del($pv_key);
  347. }
  348. }
  349. }
  350. private function activityStatsEveryDay($activity_id)
  351. {
  352. $day = date('Y-m-d',time()-86400);
  353. $now = date('Y-m-d H:i:s');
  354. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  355. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  356. $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();
  357. foreach ($all_channel_id as $v) {
  358. $i = $activity_id;
  359. $pv_key = sprintf($pv_key_format, $i, $v->id);
  360. $pv_temp = Redis::hgetall($pv_key);
  361. if ($pv_temp) {
  362. $data = [];
  363. foreach ($pv_temp as $d => $u) {
  364. if($d != $day){
  365. continue;
  366. }
  367. $uv_key = sprintf($uv_key_format, $i, $v->id, $d);
  368. $temp_uv = (int)Redis::scard($uv_key);
  369. $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];
  370. Redis::del($uv_key);
  371. Redis::hdel($pv_key,$d);
  372. try{
  373. DB::table('activity_statistic_all')
  374. ->where('activity_id',$activity_id)
  375. ->where('day',$d)
  376. ->where('distribution_channel_id',$v->id)->update(['pv'=>$u,'uv'=>$temp_uv]);
  377. }catch (\Exception $e){}
  378. }
  379. DB::table('wap_visit_stats')->insert($data);
  380. if( !Redis::hget($pv_key,date('Y-m-d'))){
  381. Redis::del($pv_key);
  382. }
  383. }
  384. }
  385. }
  386. private function nowActivitys(){
  387. $day = date('Y-m-d',time()-86400);
  388. $activty_ids = DB::table('activity')->where('start_time','<=',$day.' 23:00:00')->where('end_time','>=',$day)->select('id')->get();
  389. foreach ($activty_ids as $v){
  390. //echo $v->id.PHP_EOL;
  391. $this->activityStatsEveryDay($v->id);
  392. }
  393. }
  394. private function beforeForceSubAndBeforeVipStats()
  395. {
  396. //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;
  397. //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;
  398. $send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();
  399. $now = date('Y-m-d H:i:s');
  400. for ($i = 1; $i < 100; $i++) {
  401. $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';
  402. //$all_key = Redis::keys($like_key . '*');
  403. if ($send_order_ids) {
  404. foreach ($send_order_ids as $ids) {
  405. $data = [];
  406. $send_order_id = $ids->id;
  407. $kkk = $like_key.$send_order_id;
  408. $temp_pv = Redis::hgetall($kkk);
  409. foreach ($temp_pv as $d => $u) {
  410. if($d == 'total'){
  411. continue;
  412. }
  413. if($d == date('Y-m-d')){
  414. continue;
  415. }
  416. $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $d . ':send_order_id:' . $send_order_id;
  417. //$temp_uv = (int)Redis::scard($uv_key);
  418. //$data[] = ['day' => $d, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $u, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];
  419. Redis::del($uv_key);
  420. Redis::hdel($kkk,$d);
  421. }
  422. if(!Redis::hget($kkk,date('Y-m-d'))){
  423. Redis::del($kkk);
  424. }
  425. //DB::table('wap_visit_stats')->insert($data);
  426. }
  427. }
  428. }
  429. //$uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  430. //$pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  431. for ($i = 1; $i < 100; $i++){
  432. $like_key = 'book:' . $i . ':beforeforcesub::pvsend_order_id:';
  433. //$send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();
  434. //$all_key = Redis::keys($like_key . '*');
  435. if($send_order_ids){
  436. foreach ($send_order_ids as $vvvv){
  437. $send_order_id = $vvvv->id;
  438. $ks = $like_key.$send_order_id;
  439. $data = [];
  440. $t_pv = Redis::hgetall($ks);
  441. foreach ($t_pv as $day => $v){
  442. if($day == 'total'){
  443. continue;
  444. }
  445. if($day == date('Y-m-d')){
  446. continue;
  447. }
  448. $uv_key = 'book:' . $i . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  449. //$t_uv = (int)Redis::scard($uv_key);
  450. //$data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $v, 'uv' => $t_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];
  451. Redis::del($uv_key);
  452. Redis::hdel($ks,$day);
  453. }
  454. if(!Redis::hget($ks,date('Y-m-d'))){
  455. Redis::del($ks);
  456. }
  457. //DB::table('wap_visit_stats')->insert($data);
  458. }
  459. }
  460. }
  461. }
  462. private function beforeForceSubAndBeforeVipStatsEveryDay()
  463. {
  464. //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;
  465. //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;
  466. $now = date('Y-m-d H:i:s');
  467. $day = date('Y-m-d',time()-86400);
  468. //Redis::SISMEMBER('beforevip'.$date,$book->bid.'-'.$send_order_id);
  469. $alls = Redis::SMEMBERS('beforevip'.$day);
  470. $j = 0;
  471. $data = [];
  472. foreach ($alls as $v){
  473. $temp = explode('-',$v);
  474. $i = $temp[0];
  475. $send_order_id = $temp[1];
  476. $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';
  477. $kkk = $like_key.$send_order_id;
  478. $temp_pv = Redis::hget($kkk,$day);
  479. $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  480. $temp_uv = (int)Redis::scard($uv_key);
  481. $data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];
  482. Redis::del($uv_key);
  483. Redis::hdel($kkk,$day);
  484. if($j && $j%100 == 0){
  485. DB::table('wap_visit_stats')->insert($data);
  486. $data = [];
  487. }
  488. if(!Redis::hget($kkk,date('Y-m-d'))){
  489. Redis::del($kkk);
  490. }
  491. $j++;
  492. }
  493. if($data){
  494. DB::table('wap_visit_stats')->insert($data);
  495. }
  496. Redis::del('beforevip'.$day);
  497. $data = [];
  498. // Redis::sadd('beforeforcesub'.$date,$book->bid.'-'.$send_order_id);
  499. $alls = Redis::SMEMBERS('beforeforcesub'.$day);
  500. if($alls){
  501. $i = 0;
  502. foreach ($alls as $vs){
  503. $temp = explode('-',$vs);
  504. $bid = $temp[0];
  505. $send_order_id = $temp[1];
  506. $like_key = 'book:' . $bid . ':beforeforcesub::pvsend_order_id:';
  507. $kkk = $like_key.$send_order_id;
  508. $temp_pv = Redis::hget($kkk,$day);
  509. $uv_key = 'book:' . $bid . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;
  510. $temp_uv = (int)Redis::scard($uv_key);
  511. $data[] = ['day' => $day, 'key' => $bid, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];
  512. Redis::del($uv_key);
  513. Redis::hdel($kkk,$day);
  514. if($i && $i%100 == 0){
  515. DB::table('wap_visit_stats')->insert($data);
  516. $data = [];
  517. }
  518. if(!Redis::hget($kkk,date('Y-m-d'))){
  519. Redis::del($kkk);
  520. }
  521. $i++;
  522. }
  523. if($data){
  524. DB::table('wap_visit_stats')->insert($data);
  525. }
  526. }
  527. Redis::del('beforeforcesub'.$day);
  528. }
  529. private function getFrom(int $channel_id):array {
  530. $from = ['point_push','not_pay','hot_push','unpaid_send_activity','pay_daily_push',
  531. 'auto_custom_trusteeship','subscribe_push','sign_push','reader','signcallback','reader-year','recovery_push'];
  532. $custom_ids = DB::table('custom_send_msgs')->select('id','trusteeship')->where('distribution_channel_id',$channel_id)->get();
  533. if($custom_ids){
  534. foreach ($custom_ids as $item){
  535. if($item->trusteeship){
  536. for ($i=1;$i<=4;$i++){
  537. $from[] = 'custom_'.$item->id.'_'.$i;
  538. }
  539. $from[] = 'custom_'.$item->id;
  540. }else{
  541. $from[] = 'custom_'.$item->id;
  542. }
  543. }
  544. }
  545. $template = DB::table('wechat_template_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();
  546. if($template) {
  547. foreach ($template as $val){
  548. $from[] = 'template_'.$val->id;
  549. }
  550. }
  551. $keyword = DB::table('wechat_keyword_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();
  552. if($keyword){
  553. foreach ($keyword as $value){
  554. $from[] = 'keyword_'.$value->id;
  555. }
  556. }
  557. return $from;
  558. }
  559. private function subscribeKeyDel(){
  560. $account = $this->officialAccount();
  561. $key_format = 'www.zhuishuyun.comforce_subscribe:scene:%s:%s';
  562. //print_r($account);return;
  563. /*for ($i=1;$i<10000;$i++){
  564. foreach ($account as $appid){
  565. Redis::del(sprintf($key_format,$i,$appid));
  566. }
  567. }*/
  568. for ($i = 10;$i<100;$i++){
  569. $skip = $i*10000;
  570. $res = DB::table('users')->select('id','distribution_channel_id')->orderBy('id')->where('id','>=',$skip)->limit(10000)->get();
  571. foreach ($res as $v){
  572. if(isset($account[$v->distribution_channel_id]) && $account[$v->distribution_channel_id]){
  573. foreach ($account[$v->distribution_channel_id] as $appid){
  574. Redis::del(sprintf($key_format,$i,$appid));
  575. }
  576. }
  577. }
  578. $res = null;
  579. }
  580. }
  581. private function officialAccount():array{
  582. $res = DB::table('official_accounts')->select('distribution_channel_id','appid')->get();
  583. $data = [];
  584. foreach ($res as $v){
  585. $data[$v->distribution_channel_id][] = $v->appid;
  586. //$data[] = $v->appid;
  587. }
  588. return $data;
  589. }
  590. private function substituteStats(){
  591. $day = date('Y-m-d',time()-86400);
  592. //代充值按钮
  593. $substituteButtonUvPv = Redis::SMEMBERS('substitutebuttonpvuv'.$day);
  594. if($substituteButtonUvPv){
  595. $i = 0;
  596. $data = [];
  597. foreach ($substituteButtonUvPv as $channel_id){
  598. $temp_pv = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);
  599. $temp_uv_key = sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);
  600. $temp_uv = Redis::scard($temp_uv_key);
  601. $data[] = [
  602. 'day' => $day,
  603. 'key' => $channel_id,
  604. 'from_type' => 'substitutebuttonpvuv',
  605. 'pv' => $temp_pv,
  606. 'uv' => $temp_uv,
  607. 'type' => 6,
  608. 'created_at' => date('Y-m-d H:i:s'),
  609. 'updated_at' => date('Y-m-d H:i:s')
  610. ];
  611. if($i && $i%100 == 0){
  612. DB::table('wap_visit_stats')->insert($data);
  613. $data = [];
  614. }
  615. $i++;
  616. Redis::del($temp_uv_key);
  617. Redis::hdel('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);
  618. $temp = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));
  619. if(!$temp){
  620. Redis::del('substitutebuttonpvuv:distribution_channel_id:' . $channel_id);
  621. }
  622. }
  623. if($data){
  624. DB::table('wap_visit_stats')->insert($data);
  625. }
  626. }
  627. Redis::del('substitutebuttonpvuv'.$day);
  628. //代充值页面
  629. $substituteButtonUvPv = Redis::SMEMBERS('substitutepagepvuv'.$day);
  630. if($substituteButtonUvPv){
  631. $i = 0;
  632. $data = [];
  633. foreach ($substituteButtonUvPv as $channel_id){
  634. $temp_pv = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);
  635. $temp_uv_key = sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);
  636. $temp_uv = Redis::scard($temp_uv_key);
  637. $data[] = [
  638. 'day' => $day,
  639. 'key' => $channel_id,
  640. 'from_type' => 'substitutepagepvuv',
  641. 'pv' => $temp_pv,
  642. 'uv' => $temp_uv,
  643. 'type' => 7,
  644. 'created_at' => date('Y-m-d H:i:s'),
  645. 'updated_at' => date('Y-m-d H:i:s')
  646. ];
  647. if($i && $i%100 == 0){
  648. DB::table('wap_visit_stats')->insert($data);
  649. $data = [];
  650. }
  651. $i++;
  652. Redis::del($temp_uv_key);
  653. Redis::hdel('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);
  654. $temp = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));
  655. if(!$temp){
  656. Redis::del('substitutepagepvuv:distribution_channel_id:' . $channel_id);
  657. }
  658. }
  659. if($data){
  660. DB::table('wap_visit_stats')->insert($data);
  661. }
  662. }
  663. Redis::del('substitutepagepvuv'.$day);
  664. }
  665. private function StylePushStats(){
  666. $date = date('Y-m-d',time()-86400);
  667. $record = Redis::SMEMBERS(sprintf('push:stylepush:date:%s',$date));
  668. if(!$record)return ;
  669. $i = 0;
  670. $data = [];
  671. foreach ($record as $item){
  672. $item_array = explode('_',$item);
  673. $distribution_channel_id = $item_array[0];
  674. $gxhp = $item_array[1];
  675. $uv_key = sprintf('push:stylepush:uv:%s:gxhp:%s:date:%s',$distribution_channel_id,$gxhp,$date);
  676. $pv_key = sprintf('push:stylepush:pv:%s:gxhp:%s:date:%s',$distribution_channel_id,$gxhp,$date);
  677. $uv = Redis::scard($uv_key);
  678. $pv = Redis::get($pv_key);
  679. $data[] = [
  680. 'day' => $date,
  681. 'key' => $distribution_channel_id,
  682. 'from_type' => $gxhp,
  683. 'pv' => $pv,
  684. 'uv' => $uv,
  685. 'type' => 8,
  686. 'created_at' => date('Y-m-d H:i:s'),
  687. 'updated_at' => date('Y-m-d H:i:s')
  688. ];
  689. Redis::del($uv_key);
  690. Redis::del($pv_key);
  691. if($i && $i%100 == 0){
  692. DB::table('wap_visit_stats')->insert($data);
  693. $data = [];
  694. }
  695. $i++;
  696. }
  697. if($data){
  698. DB::table('wap_visit_stats')->insert($data);
  699. }
  700. Redis::del(sprintf('push:stylepush:date:%s',$date));
  701. }
  702. private function crm(){
  703. $date = date('Y-m-d',time()-86400);
  704. $record = Redis::SMEMBERS('crm_'.$date);
  705. $data = [];
  706. $sql = "select
  707. order_params.data_hour_key,
  708. sum(price) as charge_amount,
  709. count(*) as charge_num
  710. from orders
  711. join order_params on orders.id = order_params.order_id
  712. where orders.created_at BETWEEN '%s' and '%s' and order_params.gxhp='crm' and orders.status = 'PAID'
  713. GROUP by order_params.data_hour_key";
  714. if($record){
  715. foreach ($record as $value){
  716. $pv = (int)Redis::hget(sprintf('crm:pv:%s',$value),$date);
  717. $uv = (int)Redis::hget(sprintf('crm:uv:%s',$value),$date);
  718. $crm_param_info = explode('_',$value);
  719. $crm_id = isset($crm_param_info[1])? $crm_param_info[1]:0;
  720. $data[] =
  721. ['crm_id'=>(int)$crm_id,'day'=>$date,'pv'=>$pv,'crm_param'=>$value,
  722. 'uv'=>$uv,'charge_num'=>0,'charge_amount'=>0,
  723. 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  724. Redis::hdel(sprintf('crm:pv:%s',$value),$date);
  725. Redis::hdel(sprintf('crm:uv:%s',$value),$date);
  726. }
  727. if($data){
  728. DB::table('crm_stats')->insert($data);
  729. }
  730. }
  731. $data = [];
  732. $order_info = DB::select(sprintf($sql,$date,date('Y-m-d 00:00:00')));
  733. if($order_info){
  734. foreach ($order_info as $o){
  735. $crm_param_info = explode('_',$o->data_hour_key);
  736. $crm_id = isset($crm_param_info[1])? $crm_param_info[1]:0;
  737. $update_status = DB::table('crm_stats')->where('day',$date)->where('crm_param',$o->data_hour_key)->update([
  738. 'charge_num'=>$o->charge_num,
  739. 'charge_amount'=>$o->charge_amount
  740. ]);
  741. if(!$update_status){
  742. $data[] =
  743. ['crm_id'=>(int)$crm_id,'day'=>$date,'pv'=>0,'crm_param'=>$o->data_hour_key,
  744. 'uv'=>0,'charge_num'=>$o->charge_num,'charge_amount'=>$o->charge_amount,
  745. 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  746. }
  747. }
  748. if($data){
  749. DB::table('crm_stats')->insert($data);
  750. }
  751. }
  752. Redis::del('crm_'.$date);
  753. }
  754. public function InnerOuterWeixinStats(){
  755. $day = date('Y-m-d',time()-86400);
  756. $keys = Redis::SMEMBERS('InnerOuterWeixinStats:'.$day);
  757. if (!$keys) {
  758. return ;
  759. }
  760. $data = [];
  761. $i = 0;
  762. foreach ($keys as $k){
  763. list($from,$distribution_channel_id) = explode(':',$k);
  764. if(!$from || !$distribution_channel_id) continue;
  765. $pv_key = sprintf('InnerOuterWeixinStats:pv:%s:%s',$from,$distribution_channel_id);
  766. $uv_key = sprintf('InnerOuterWeixinStats:uv:%s:%s:%s',$from,$distribution_channel_id,$day);
  767. $pv = Redis::hget($pv_key,$day);
  768. $uv = Redis::scard($uv_key);
  769. $type = '';
  770. $param = '';
  771. if(is_numeric($from)){
  772. $type = 'send_order';
  773. $param = $from;
  774. }
  775. if(str_contains($from,['template'])){
  776. $type = 'template';
  777. list($temp,$param) = explode('_',$from);
  778. }
  779. if(str_contains($from,['custom'])){
  780. $type = 'custom';
  781. list($temp,$param) = explode('_',$from);
  782. }
  783. Redis::del($uv_key);
  784. Redis::hdel($pv_key,$day);
  785. if(!$type || !$param || !is_numeric($param)) continue;
  786. $data[] = [
  787. 'type'=>$type,
  788. 'param'=>$param,
  789. 'pv'=>$pv,
  790. 'uv'=>$uv,
  791. 'day'=>$day,
  792. 'charge'=>0,
  793. 'updated_at'=>date('Y-m-d H:i:s'),
  794. 'created_at'=>date('Y-m-d H:i:s')
  795. ];
  796. if($i++ % 100 == 0){
  797. DB::table('inner_outer_weixin_stats')->insert($data);
  798. $data = [];
  799. }
  800. }
  801. if($data){DB::table('inner_outer_weixin_stats')->insert($data);}
  802. Redis::del('InnerOuterWeixinStats:'.$day);
  803. }
  804. }