| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace bizGhs\stat\classes;
- use common\components\util;
- use bizGhs\base\classes\BaseClass;
- use Yii;
- /**
- * 批发端 -- 浏览记录与浏览统计
- * Class StatVisitClass
- * @package bizGhs\stat\classes
- */
- class StatVisitClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\stat\models\StatVisit';
- //$time 日期
- public static function getKeyName($sjId, $class, $time = null)
- {
- //全站每天的访问量 //全站每天的访问ip量
- if (in_array($class, ['whole_site_view', 'whole_site_visit',]) == false) {
- util::fail('无效键名');
- }
- $time = isset($time) == false ? date("Ymd") : $time;
- return $class . '_' . $sjId . '_' . $time;
- }
- //增加供货商(批发)的访客
- public static function addGhsCustomVisit($ghs, $custom, $date = null)
- {
- $date = isset($date) == false ? date("Ymd") : $date;
- $customId = $custom->id ?? 0;
- $ghsMainId = $ghs->mainId ?? 0;
- $cacheKey = 'ghs_mall_visit_' . $ghsMainId . '_' . $date;
- $exists = Yii::$app->redis->executeCommand('SISMEMBER', [$cacheKey, $customId]);
- if (empty($exists)) {
- Yii::$app->redis->executeCommand('SADD', [$cacheKey, $customId]);
- $custom->visitTime = date("Y-m-d H:i:s");
- $custom->save();
- }
- }
- //批发保存访客数
- public static function saveVisit($shop, $visitNum, $date = null)
- {
- $sjId = $shop->sjId ?? 0;
- $shopId = $shop->id ?? 0;
- $totalNum = $shop->totalVisit ?? 0;
- $date = isset($date) == false ? date("Ymd") : $date;
- $resource = self::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'time' => $date], true);
- //已经存过
- if (!empty($resource)) {
- return false;
- }
- $data = [
- 'time' => $date,
- 'riseNum' => 0,
- 'totalNum' => 0,
- 'shopId' => $shopId,
- 'sjId' => $sjId,
- 'createTime' => date("Y-m-d H:i:s")
- ];
- $resource = self::add($data, true);
- $nowNum = bcadd($resource->riseNum, $visitNum, 2);
- $resource->riseNum = $nowNum;
- $resource->totalNum = $totalNum;
- $resource->save();
- //每月
- if ($date == null) {
- $time = date('Ym');
- $year = date('Y');
- $month = date('m');
- } else {
- $timestamp = strtotime($date);
- $time = date('Ym', $timestamp);
- $year = date('Y', $timestamp);
- $month = date('m', $timestamp);
- }
- $stat = StatVisitMonthClass::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'time' => $time], true);
- if (empty($stat)) {
- $stat = StatVisitMonthClass::add(['shopId' => $shopId, 'sjId' => $sjId, 'time' => $time, 'year' => $year, 'month' => $month, 'createTime' => date("Y-m-d H:i:s")], true);
- }
- $currentNum = bcadd($stat->riseNum, $visitNum);
- $stat->riseNum = $currentNum;
- $stat->totalNum = $totalNum;
- $stat->save();
- self::clearVisit($shop, $date);
- }
- //批发清空访客
- public static function clearVisit($shop, $date = null)
- {
- $mainId = $shop->mainId ?? 0;
- $date = isset($date) == false ? date("Ymd") : $date;
- $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- }
- //批发端获取访客数 ssh 2020.2.2
- public static function getVisitNum($mainId, $date = null)
- {
- $date = isset($date) == false ? date("Ymd") : $date;
- $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
- $num = Yii::$app->redis->executeCommand('SCARD', [$cacheKey]);
- return !empty($num) ? $num : 0;
- }
- //批发端获取访客ID
- public static function getVisitCustomIds($shop, $date = null)
- {
- $mainId = $shop->mainId ?? 0;
- $date = isset($date) == false ? date("Ymd") : $date;
- $cacheKey = 'ghs_mall_visit_' . $mainId . '_' . $date;
- $ids = Yii::$app->redis->executeCommand('SMEMBERS', [$cacheKey]);
- return $ids;
- }
- }
|