WapVisitStatService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z_yang
  5. * Date: 2018/03/09
  6. * Time: 20:18
  7. */
  8. namespace App\Modules\Statistic\Services;
  9. use App\Modules\Statistic\Models\WapVisitStat;
  10. use App\Modules\SendOrder\Models\SendOrder;
  11. use App\Modules\Subscribe\Services\OrderService;
  12. use DB;
  13. use Redis;
  14. class WapVisitStatService
  15. {
  16. /**
  17. * 获取派单总pv
  18. * @param int $id
  19. */
  20. public static function getSendOrderTotalPv($id)
  21. {
  22. //return (int)Redis::hget('send_order_pv_' . $id, 'total');
  23. $today = (int)Redis::hget('send_order_pv_'.$id,date('Y-m-d'));
  24. $old = (int)WapVisitStat::where('key', $id)->where('type', 3)->where('from_type','send_orders')->sum('pv');
  25. return $today+$old;
  26. }
  27. /**
  28. * 获取浏览器派单总pv uv
  29. * @param $id
  30. * @return array
  31. */
  32. public static function getBrowserSendOrderTotalPvAndUv($id):array
  33. {
  34. $sql_format = "select sum(pv) as pv,sum(uv) as uv from wap_visit_stats WHERE `key`=%s and from_type='%s' and `type`=3 ";
  35. $sql = sprintf($sql_format, $id, 'browser_send_orders');
  36. $today_pv = (int)Redis::hget('browser_send_order_pv_' . $id, date('Y-m-d'));
  37. $today_uv = (int)Redis::hget('browser_send_order_uv_' . $id, date('Y-m-d'));
  38. $res = DB::select($sql);
  39. $uv = (int)$res[0]->uv + $today_uv;
  40. $pv = (int)$res[0]->pv + $today_pv;
  41. return compact('uv', 'pv');
  42. }
  43. /**
  44. * 获取派单总pv
  45. * @param $id int
  46. */
  47. public static function getSendOrderTotalUv($id)
  48. {
  49. //return (int)Redis::hget('send_order_uv_' . $id, 'total');
  50. $today = (int)Redis::hget('send_order_uv_'.$id,date('Y-m-d'));
  51. $old = WapVisitStat::where('key', $id)->where('type', 3)->where('from_type','send_orders')->sum('uv');
  52. return $today+$old;
  53. }
  54. /**
  55. * 获取活动的pv uv
  56. * @param int $channel_id
  57. * @param int $activity_id
  58. * @return array
  59. */
  60. public static function getActivityUvAndPv(int $channel_id, int $activity_id):array
  61. {
  62. $day = date('Y-m-d');
  63. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  64. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  65. $sql_format = "select sum(pv) as pv,sum(uv) as uv from wap_visit_stats WHERE `key`=%s and from_type='%s' and `day` >= '2018-01-10' and type=2 ";
  66. $sql = sprintf($sql_format, $channel_id, $activity_id);
  67. $res = DB::select($sql);
  68. $today_pv = (int)Redis::hget(sprintf($pv_key_format, $activity_id, $channel_id), $day);
  69. $today_uv = (int)Redis::scard(sprintf($uv_key_format, $activity_id, $channel_id, $day));
  70. $uv = $res[0]->uv + $today_uv;
  71. $pv = $res[0]->pv + $today_pv;
  72. return compact('uv', 'pv');
  73. }
  74. /**
  75. * 获取活动的pv uv
  76. * @param int $channel_id
  77. * @param int $activity_id
  78. * @param string $day
  79. * @return array
  80. */
  81. public static function getActivityUvAndPvByDay(int $channel_id, int $activity_id, string $day):array
  82. {
  83. $sql_format = "select sum(pv) as pv,sum(uv) as uv from wap_visit_stats WHERE `key`=%s and from_type='%s' and `day` = '%s' and type=2 ";
  84. $sql = sprintf($sql_format, $channel_id, $activity_id, $day);
  85. $res = DB::select($sql);
  86. $uv = $res[0]->uv;
  87. $pv = $res[0]->pv;
  88. return compact('uv', 'pv');
  89. }
  90. /**
  91. * 获取派单每一天的Uv信息列表
  92. * @param $id int
  93. * @return array UV
  94. */
  95. public static function getSendOrderUv($id)
  96. {
  97. $now = date('Y-m-d');
  98. $res = WapVisitStat::where('key', $id)->where('type', 3)->where('from_type', 'send_orders')->select('day', 'uv')->get();
  99. $data = [];
  100. $total = 0;
  101. if ($res) {
  102. foreach ($res as $v) {
  103. $data[$v->day] = $v->uv;
  104. $total += $v->uv;
  105. }
  106. $data['total'] = $total;
  107. }
  108. $today = (int)Redis::hget('send_order_uv_' . $id, $now);
  109. $data[$now] = $today;
  110. return $data;
  111. }
  112. /**
  113. * 获取派单某一天的UvPV
  114. * @param $id int
  115. * @return array UvPV
  116. */
  117. public static function getSendOrderDayUvPvFromStat($id,$date)
  118. {
  119. $res = WapVisitStat::where('key', $id)->where('type', 3)->where('from_type', 'send_orders')->where('day',$date)->select('uv','pv')->first();
  120. $uv = $res ? $res->uv : 0;
  121. $pv = $res ? $res->pv : 0;
  122. return compact('uv','pv');
  123. }
  124. /**
  125. * 获取每一天的pv信息列表
  126. * @param $id int
  127. * @return array $data
  128. */
  129. public static function getSendOrderPv($id)
  130. {
  131. $now = date('Y-m-d');
  132. $res = WapVisitStat::where('key', $id)->where('type', 3)->where('from_type', 'send_orders')->select('day', 'pv')->get();
  133. $data = [];
  134. $total = 0;
  135. if ($res) {
  136. foreach ($res as $v) {
  137. $data[$v->day] = $v->pv;
  138. $total += $v->pv;
  139. }
  140. $data['total'] = $total;
  141. }
  142. $today = (int)Redis::hget('send_order_pv_' . $id, $now);
  143. $data[$now] = $today;
  144. return $data;
  145. }
  146. /**
  147. *获取site下的uv,pv
  148. */
  149. public static function getSitePvAndUv($distribution_channel_id, $from, $start_day, $end_day)
  150. {
  151. $res = WapVisitStat::where('key', $distribution_channel_id)
  152. ->where('type', 1)
  153. ->where('from_type', $from);
  154. if ($start_day) {
  155. $res = $res->where('day', '>=', $start_day);
  156. }
  157. if ($end_day) {
  158. $res = $res->where('day', '<=', $end_day);
  159. }
  160. $res = $res->select(DB::raw('sum(uv) as uv'), DB::raw('sum(pv) as pv'))
  161. ->get();
  162. $temp = $res->first();
  163. $pv = $temp->pv;
  164. $uv = $temp->uv;
  165. $now = date('Y-m-d');
  166. if ($end_day && $end_day == $now) {
  167. $pv += (int)Redis::hget('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, $now);
  168. $uv += (int)Redis::scard('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $now);
  169. }
  170. if (!$end_day) {
  171. $pv += (int)Redis::hget('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, $now);
  172. $uv += (int)Redis::scard('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $now);
  173. }
  174. return compact('uv', 'pv');
  175. }
  176. /**
  177. * 获取某天的uvAndpv
  178. * @param int $distribution_channel_id
  179. * @param string $from
  180. * @param string $day
  181. * @return array
  182. */
  183. public static function getSitePvAndUvOneDay(int $distribution_channel_id, string $from, string $day):array
  184. {
  185. $res = WapVisitStat::where('key', $distribution_channel_id)
  186. ->where('type', 1)
  187. ->where('from_type', $from)
  188. ->where('day', '=', $day)
  189. ->select('pv', 'uv')
  190. ->first();
  191. if ($res)
  192. return ['pv' => $res->pv, 'uv' => $res->uv];
  193. else {
  194. return ['pv' => 0, 'uv' => 0];
  195. }
  196. }
  197. /**
  198. * 获取强关前一张的uv,pv
  199. */
  200. public static function getBeforeForceSubAndBeforeVipUv($bid, $send_order_id, $date, $one_day)
  201. {
  202. if ($one_day) {
  203. $res = WapVisitStat::where('key', $bid)->where('from_type', (string)$send_order_id)->where('type', 4)->where('day', $date)->select('uv', 'pv')->first();
  204. } else {
  205. $res = WapVisitStat::where('key', $bid)
  206. ->where('from_type', (string)$send_order_id)
  207. ->where('type', 4)
  208. ->where('day', '>=', $date)
  209. ->where('day', '<=', date('Y-m-d'))
  210. ->select(DB::raw('sum(uv) as uv'), DB::raw('sum(pv) as pv'))
  211. ->get();
  212. $res = $res->first();
  213. }
  214. $beforevippv = 0;
  215. $beforevipuv = 0;
  216. if ($res) {
  217. $beforevippv = $res->pv;
  218. $beforevipuv = $res->uv;
  219. }
  220. $res = null;
  221. if ($one_day) {
  222. $res = WapVisitStat::where('key', $bid)->where('from_type', (string)$send_order_id)->where('type', 5)->where('day', $date)->select('uv', 'pv')->first();
  223. } else {
  224. $res = WapVisitStat::where('key', $bid)
  225. ->where('from_type', (string)$send_order_id)
  226. ->where('type', 5)
  227. ->where('day', '>=', $date)
  228. ->where('day', '<=', date('Y-m-d'))
  229. ->select(DB::raw('sum(uv) as uv'), DB::raw('sum(pv) as pv'))
  230. ->get();
  231. $res = $res->first();
  232. }
  233. $beforeforcesubpv = 0;
  234. $beforeforcesubuv = 0;
  235. if ($res) {
  236. $beforeforcesubpv = $res->pv;
  237. $beforeforcesubuv = $res->uv;
  238. }
  239. return compact('beforevipuv', 'beforevippv', 'beforeforcesubpv', 'beforeforcesubuv');
  240. }
  241. public static function getUvAndPVByFromAndDay($distribution_channel_id, $from, $day)
  242. {
  243. $from = array_map(function ($i) {
  244. return sprintf("'%s'", $i);
  245. }, $from);
  246. $from_str = implode(',', $from);
  247. if ($day) {
  248. if (is_string($day)) {
  249. $sql_fromat = "select sum(uv) as uv,sum(pv) as pv from wap_visit_stats where `type`=1 and `key`=%s and from_type in (%s) and `day`='%s'";
  250. $sql = sprintf($sql_fromat, $distribution_channel_id, $from_str, $day);
  251. } else {
  252. $sql_fromat = "select sum(uv) as uv,sum(pv) as pv from wap_visit_stats where `type`=1 and `key`=%s and from_type in (%s) and `day` BETWEEN '%s' and '%s'";
  253. $sql = sprintf($sql_fromat, $distribution_channel_id, $from_str, $day[0], $day[1]);
  254. }
  255. } else {
  256. $sql_fromat = "select sum(uv) as uv,sum(pv) as pv from wap_visit_stats where `type`=1 and `key`=%s and from_type in (%s)";
  257. $sql = sprintf($sql_fromat, $distribution_channel_id, $from_str);
  258. }
  259. $res = DB::select($sql);
  260. $uv = $res[0]->uv;
  261. $pv = $res[0]->pv;
  262. return compact('uv', 'pv');
  263. }
  264. public static function smartPushTestBookStats(int $bid)
  265. {
  266. $data = [
  267. 'uv' => 0,
  268. 'pv' => 0,
  269. 'charge_amount' => 0,
  270. 'charge_user_num' => 0,
  271. 'book_amount' => 0,
  272. 'book_user_num' => 0,
  273. 'real_push_user_num' => 0,
  274. 'second_chapter_uv' => 0
  275. ];
  276. if (empty($bid))
  277. return $data;
  278. $uv_key = sprintf('%s_%s_uv', $bid, 'smart_push');
  279. $uv = (int)(Redis::scard($uv_key));
  280. $pv = (int)Redis::hget('smart_push_test_book', $bid);
  281. $book_user_num = Redis::scard('smart_push_test_book_user_count' . $bid);
  282. $field = sprintf('%s_amount', $bid);
  283. $book_amount = Redis::hget('smart_push_test_book', $field);
  284. $real_push_user_num = (int)Redis::hget('SmartPushBookUserNum', $bid);
  285. $data['uv'] = $uv;
  286. $data['pv'] = $pv;
  287. $data['book_amount'] = $book_amount;
  288. $data['book_user_num'] = $book_user_num;
  289. $data['real_push_user_num'] = $real_push_user_num;
  290. $data['second_chapter_uv'] = (int)Redis::scard('smart_push_test_book_second_uv' . $bid);
  291. return $data;
  292. }
  293. public static function customerAllStats($from)
  294. {
  295. $uv = Redis::scard('push:distribution_channel_id:alluv:from:' . $from);
  296. $pv = Redis::hget('push:distribution_channel_id:allpv', $from);
  297. $pay_user_num = Redis::scard('push:all:paidnum:from:' . $from);
  298. $charge_amount = Redis::hget('push:all:paidamount', $from);
  299. $register_user_num = 0;
  300. return compact('uv', 'pv', 'pay_user_num', 'charge_amount', 'register_user_num');
  301. }
  302. /**
  303. * 内部派单统计
  304. * @param $from
  305. * @return array
  306. */
  307. public static function innerCustomerAllStats($from)
  308. {
  309. $uv = Redis::scard('push:inner_send_order_id:uv:' . $from);
  310. $pv = Redis::hget('push:inner_send_order_id:pv', $from);
  311. $order_info = OrderService::getInnerSendOrderStats($from);
  312. $pay_user_num = 0;
  313. $charge_amount = 0;
  314. if ($order_info) {
  315. $pay_user_num = $order_info->pay_user_num;
  316. $charge_amount = $order_info->charge_amount;
  317. }
  318. $register_user_num = 0;
  319. return compact('uv', 'pv', 'pay_user_num', 'charge_amount', 'register_user_num');
  320. }
  321. /**
  322. * 211站点特殊统计
  323. * @param int $uid
  324. * @param int $bid
  325. * @param int $send_order_id
  326. */
  327. public static function specialChannelIdStatsMarkUser(int $uid,int $bid,int $send_order_id):void
  328. {
  329. if(!$uid || !$bid || !$send_order_id){
  330. return ;
  331. }
  332. try{
  333. if(!Redis::Sismember('specialChannelIdStatsMarkUser:set:'.$uid,$bid)){
  334. Redis::sadd('specialChannelIdStatsMarkUser:set:'.$uid,$bid);
  335. $key = $uid.'-'.$bid;
  336. Redis::hset('specialChannelIdStatsMarkUser:hash:',$key,$send_order_id);
  337. }
  338. }catch (\Exception $e){}
  339. return ;
  340. }
  341. /**
  342. * 211站点特殊统计
  343. * @param int $uid
  344. * @param int $bid
  345. * @return int
  346. */
  347. public static function specialChannelIdStatsGetSendOrder(int $uid,int $bid):int{
  348. if(!$uid || !$bid ){
  349. return 0;
  350. }
  351. try{
  352. $key = $uid.'-'.$bid;
  353. $send_order_id = Redis::hget('specialChannelIdStatsMarkUser:hash:',$key);
  354. if($send_order_id) {
  355. return (int)$send_order_id;
  356. }
  357. }catch (\Exception $e){}
  358. return 0;
  359. }
  360. /**
  361. * 211站点特殊统计
  362. * @param int $uid
  363. * @param $bid
  364. * @param int $fee
  365. */
  366. public static function specialChannelIdStatsSave(int $uid,$bid,int $fee):void{
  367. if(!$uid || !$bid || !$fee){
  368. return ;
  369. }
  370. $send_order_id = self::specialChannelIdStatsGetSendOrder($uid,$bid);
  371. if(!$send_order_id){
  372. return ;
  373. }
  374. try{
  375. $day = date('Y-m-d');
  376. Redis::HINCRBY('specialChannelIdStats:hash',$send_order_id.'_'.$day,$fee);
  377. Redis::HINCRBY('specialChannelIdStats:hash',$send_order_id,$fee);
  378. $set_key = 'specialChannelIdStats:set:sendorder:%s:day:%s';
  379. Redis::sadd(sprintf($set_key,$send_order_id,$day),$uid);
  380. Redis::sadd(sprintf($set_key,$send_order_id,'all'),$uid);
  381. }catch (\Exception $e){}
  382. return ;
  383. }
  384. /**
  385. * 获取 211站点特殊统计
  386. * @param int $send_order_id
  387. * @param string $start_date
  388. * @return array
  389. */
  390. public static function getSpecialChannelIdStats(int $send_order_id,string $start_date):array{
  391. $data = [
  392. 'first_day_subscribe_num'=>0,//首日累计订阅人数
  393. 'first_day_subscribe_amount'=>0,//首日累计订阅总额
  394. 'three_day_subscribe_num'=>0,// 三日累计订阅人数
  395. 'three_day_subscribe_amount'=>0,//三日累计订阅总额
  396. 'total_subscribe_num'=>0,//累计订阅人数
  397. 'total_subscribe_amount'=>0//累计订阅总额
  398. ];
  399. if(!$send_order_id || !$start_date){
  400. return $data;
  401. }
  402. try{
  403. $set_key = 'specialChannelIdStats:set:sendorder:%s:day:%s';
  404. $data['first_day_subscribe_num'] = (int)(Redis::scard(sprintf($set_key,$send_order_id,$start_date)));
  405. $data['first_day_subscribe_amount'] = (int)(Redis::hget('specialChannelIdStats:hash',$send_order_id.'_'.$start_date));
  406. $data['total_subscribe_num'] = (int)(Redis::scard(sprintf($set_key,$send_order_id,'all')));
  407. $data['total_subscribe_amount'] = (int)(Redis::hget('specialChannelIdStats:hash',$send_order_id));
  408. $second_day = date('Y-m-d',strtotime($start_date)+86400);
  409. $three_day = date('Y-m-d',strtotime($second_day)+86400);
  410. //$second_day_subscribe_num = (int)(Redis::scrad(sprintf($set_key,$send_order_id,$second_day)));
  411. $second__day_subscribe_amount = (int)(Redis::hget('specialChannelIdStats:hash',$send_order_id.'_'.$second_day));
  412. //$three_day_subscribe_num = (int)(Redis::scrad(sprintf($set_key,$send_order_id,$three_day)));
  413. $three__day_subscribe_amount = (int)(Redis::hget('specialChannelIdStats:hash',$send_order_id.'_'.$three_day));
  414. Redis::SUNIONSTORE(
  415. 'SpecialChannelIdStatstemp',
  416. sprintf($set_key,$send_order_id,$start_date),
  417. sprintf($set_key,$send_order_id,$second_day),
  418. sprintf($set_key,$send_order_id,$three_day)
  419. );
  420. $data['three_day_subscribe_num'] = (int)( Redis::scard('SpecialChannelIdStatstemp') );
  421. Redis::del('SpecialChannelIdStatstemp');
  422. $data['three_day_subscribe_amount'] = $second__day_subscribe_amount+$three__day_subscribe_amount+$data['first_day_subscribe_amount'];
  423. }catch (\Exception $e){}
  424. return $data;
  425. }
  426. /**
  427. * 阅读器uv pv 统计
  428. * @param $uid
  429. * @param $distribution_channel_id
  430. */
  431. public static function recordReaderUvAndPv($uid,$distribution_channel_id){
  432. if(empty($uid) || empty($distribution_channel_id)){
  433. return ;
  434. }
  435. $day = date('Y-m-d');
  436. $uv_key = sprintf('readeruv:date:%s:channel_id:%s',$day,$distribution_channel_id);
  437. $pv_key = sprintf('readerpv:channel_id:%s',$distribution_channel_id);
  438. $record_key = sprintf('recordReaderUvAndPv:date:%s',$day);
  439. try{
  440. Redis::sadd($uv_key,$uid);
  441. Redis::HINCRBY($pv_key,$day,1);
  442. Redis::sadd($record_key,$distribution_channel_id);
  443. }catch (\Exception $e){
  444. }
  445. }
  446. /**
  447. * 获取阅读器uv pv 统计 ['day'=>'2018-12-25','pv'=>1,'uv'=>3];
  448. * [ ['day'=>'2018-12-25','pv'=>1,'uv'=>3], ['day'=>'2018-12-26','pv'=>2342,'uv'=>342]];
  449. * @param $distribution_channel_id
  450. * @param string $day
  451. * @return array
  452. */
  453. public static function getReaderUvAndPv($distribution_channel_id,$day=''){
  454. $pv_key = sprintf('readerpv:channel_id:%s',$distribution_channel_id);
  455. if($day){
  456. $pv = (int)Redis::hget($pv_key,$day);
  457. $uv_key = sprintf('readeruv:date:%s:channel_id:%s',$day,$distribution_channel_id);
  458. $uv = Redis::scard($uv_key);
  459. return ['day'=>$day,'pv'=>$pv,'uv'=>$uv];
  460. }
  461. $all_pv = Redis::hgetAll($pv_key);
  462. if(!$all_pv){
  463. return [];
  464. }
  465. $data = [];
  466. foreach ($all_pv as $d=>$v){
  467. $uv_key = sprintf('readeruv:date:%s:channel_id:%s',$d,$distribution_channel_id);
  468. $uv = Redis::scard($uv_key);
  469. $data[] = ['day'=>$d,'pv'=>$v,'uv'=>$uv];
  470. }
  471. return $data;
  472. }
  473. /**
  474. * 保存到数据库后,删除数据
  475. * @param $date
  476. */
  477. public static function deleteReaderUvAndPvRedisKey($date){
  478. $id_key = sprintf('recordReaderUvAndPv:date:%s',$date);
  479. $channel_ids = Redis::smembers($id_key );
  480. if($channel_ids){
  481. foreach ($channel_ids as $channel_id){
  482. $pv_key = sprintf('readerpv:channel_id:%s',$channel_id);
  483. Redis::hdel($pv_key,$date);
  484. $uv_key = sprintf('readeruv:date:%s:channel_id:%s',$date,$channel_id);
  485. Redis::del($uv_key);
  486. }
  487. }
  488. Redis::del($id_key);
  489. }
  490. }