StatVisitClass.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use common\components\util;
  4. use bizGhs\base\classes\BaseClass;
  5. use Yii;
  6. /**
  7. * 批发端 -- 浏览记录与浏览统计
  8. * Class StatVisitClass
  9. * @package bizGhs\stat\classes
  10. */
  11. class StatVisitClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizGhs\stat\models\StatVisit';
  14. //$time 日期
  15. public static function getKeyName($sjId, $class, $time = null)
  16. {
  17. //全站每天的访问量 //全站每天的访问ip量
  18. if (in_array($class, ['whole_site_view', 'whole_site_visit',]) == false) {
  19. util::fail('无效键名');
  20. }
  21. $time = isset($time) == false ? date("Ymd") : $time;
  22. return $class . '_' . $sjId . '_' . $time;
  23. }
  24. //增加供货商(批发)的访客
  25. public static function addGhsCustomVisit($ghs, $custom, $date = null)
  26. {
  27. $date = isset($date) == false ? date("Ymd") : $date;
  28. $customId = $custom->id ?? 0;
  29. $ghsMainId = $ghs->mainId ?? 0;
  30. $cacheKey = 'ghs_mall_visit_' . $ghsMainId . '_' . $date;
  31. $exists = Yii::$app->redis->executeCommand('SISMEMBER', [$cacheKey, $customId]);
  32. if (empty($exists)) {
  33. Yii::$app->redis->executeCommand('SADD', [$cacheKey, $customId]);
  34. $custom->visitTime = date("Y-m-d H:i:s");
  35. $custom->save();
  36. }
  37. }
  38. //批发保存访客数
  39. public static function saveVisit($shop, $visitNum, $date = null)
  40. {
  41. $sjId = $shop->sjId ?? 0;
  42. $shopId = $shop->id ?? 0;
  43. $totalNum = $shop->totalVisit ?? 0;
  44. $date = isset($date) == false ? date("Ymd") : $date;
  45. $resource = self::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'time' => $date], true);
  46. //已经存过
  47. if (!empty($resource)) {
  48. return false;
  49. }
  50. $data = [
  51. 'time' => $date,
  52. 'riseNum' => 0,
  53. 'totalNum' => 0,
  54. 'shopId' => $shopId,
  55. 'sjId' => $sjId,
  56. 'createTime' => date("Y-m-d H:i:s")
  57. ];
  58. $resource = self::add($data, true);
  59. $nowNum = bcadd($resource->riseNum, $visitNum, 2);
  60. $resource->riseNum = $nowNum;
  61. $resource->totalNum = $totalNum;
  62. $resource->save();
  63. //每月
  64. if ($date == null) {
  65. $time = date('Ym');
  66. $year = date('Y');
  67. $month = date('m');
  68. } else {
  69. $timestamp = strtotime($date);
  70. $time = date('Ym', $timestamp);
  71. $year = date('Y', $timestamp);
  72. $month = date('m', $timestamp);
  73. }
  74. $stat = StatVisitMonthClass::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'time' => $time], true);
  75. if (empty($stat)) {
  76. $stat = StatVisitMonthClass::add(['shopId' => $shopId, 'sjId' => $sjId, 'time' => $time, 'year' => $year, 'month' => $month, 'createTime' => date("Y-m-d H:i:s")], true);
  77. }
  78. $currentNum = bcadd($stat->riseNum, $visitNum);
  79. $stat->riseNum = $currentNum;
  80. $stat->totalNum = $totalNum;
  81. $stat->save();
  82. self::clearVisit($shop, $date);
  83. }
  84. //批发清空访客
  85. public static function clearVisit($shop, $date = null)
  86. {
  87. $mainId = $shop->mainId ?? 0;
  88. $date = isset($date) == false ? date("Ymd") : $date;
  89. $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
  90. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  91. }
  92. //批发端获取访客数 ssh 2020.2.2
  93. public static function getVisitNum($mainId, $date = null)
  94. {
  95. $date = isset($date) == false ? date("Ymd") : $date;
  96. $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
  97. $num = Yii::$app->redis->executeCommand('SCARD', [$cacheKey]);
  98. return !empty($num) ? $num : 0;
  99. }
  100. //批发端获取访客ID
  101. public static function getVisitCustomIds($shop, $date = null)
  102. {
  103. $mainId = $shop->mainId ?? 0;
  104. $date = isset($date) == false ? date("Ymd") : $date;
  105. $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
  106. $ids = Yii::$app->redis->executeCommand('SMEMBERS', [$cacheKey]);
  107. return $ids;
  108. }
  109. }