StoreVisit.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\models\store;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use app\models\user\User;
  10. /**
  11. * 商品浏览分析
  12. * Class StoreVisit
  13. * @package app\models\store
  14. */
  15. class StoreVisit extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'store_visit';
  27. use ModelTrait;
  28. public static function merSet($mer_id)
  29. {
  30. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  31. }
  32. /**
  33. * @param $date
  34. * @param array $class
  35. * @param string $mer_id
  36. * @return array
  37. */
  38. public static function getVisit($date, $class = [], $mer_id = '')
  39. {
  40. $model = new self();
  41. if ($mer_id) {
  42. $model = $model->merSet($mer_id);
  43. }
  44. switch ($date) {
  45. case null:
  46. case 'today':
  47. case 'week':
  48. case 'year':
  49. if ($date == null) $date = 'month';
  50. $model = $model->whereTime('add_time', $date);
  51. break;
  52. case 'quarter':
  53. list($startTime, $endTime) = User::getMonth('n');
  54. $model = $model->where('add_time', '>', $startTime);
  55. $model = $model->where('add_time', '<', $endTime);
  56. break;
  57. default:
  58. list($startTime, $endTime) = explode('-', $date);
  59. $model = $model->where('add_time', '>', strtotime($startTime));
  60. $model = $model->where('add_time', '<', strtotime($endTime));
  61. break;
  62. }
  63. $list = $model->group('type')->field('sum(count) as sum,product_id,cate_id,type,content')->order('sum desc')->limit(0, 10)->select()->toArray();
  64. $view = [];
  65. foreach ($list as $key => $val) {
  66. $now_list['name'] = $val['type'] == 'viwe' ? '浏览量' : '搜索';
  67. $now_list['value'] = $val['sum'];
  68. $now_list['class'] = isset($class[$key]) ? $class[$key] : '';
  69. $view[] = $now_list;
  70. }
  71. if (empty($list)) {
  72. $view = [['name' => '暂无数据', 'value' => 100, 'class' => '']];
  73. }
  74. return $view;
  75. }
  76. /**
  77. * 获取查看拼团商品人数
  78. * @param int $combinationId
  79. * @param string $productType
  80. * @return mixed
  81. */
  82. public static function getVisitPeople($combinationId = 0, $productType = 'combination')
  83. {
  84. $model = new self();
  85. $model = $model->where('product_id', $combinationId);
  86. $model = $model->where('product_type', $productType);
  87. return $model->count();
  88. }
  89. /**
  90. * 设置浏览信息
  91. * @param $uid
  92. * @param int $product_id
  93. * @param int $cate
  94. * @param string $type
  95. * @param string $content
  96. * @param int $min
  97. */
  98. public static function setView($uid, $product_id = 0, $cate = 0, $type = '', $content = '', $min = 20)
  99. {
  100. $model = new self();
  101. $view = $model->where('uid', $uid)->where('product_id', $product_id)->field('count,add_time,id')->find();
  102. if ($view && $type != 'search') {
  103. $time = time();
  104. if (($view['add_time'] + $min) < $time) {
  105. $model->where(['id' => $view['id']])->update(['count' => $view['count'] + 1, 'add_time' => time()]);
  106. }
  107. } else {
  108. $cate = explode(',', $cate)[0];
  109. $model->insert([
  110. 'add_time' => time(),
  111. 'count' => 1,
  112. 'product_id' => $product_id,
  113. 'cate_id' => $cate,
  114. 'type' => $type,
  115. 'uid' => $uid,
  116. 'content' => $content
  117. ]);
  118. }
  119. }
  120. }