StatVisitClass.php 3.9 KB

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