UserVisitRepository.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\UserVisitDao;
  13. use app\common\repositories\BaseRepository;
  14. /**
  15. * Class UserVisitRepository
  16. * @package app\common\repositories\user
  17. * @author xaboy
  18. * @day 2020/5/27
  19. * @mixin UserVisitDao
  20. */
  21. class UserVisitRepository extends BaseRepository
  22. {
  23. /**
  24. * @var UserVisitDao
  25. */
  26. protected $dao;
  27. /**
  28. * UserVisitRepository constructor.
  29. * @param UserVisitDao $dao
  30. */
  31. public function __construct(UserVisitDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取用户推荐产品的类别ID列表
  37. *
  38. * 本函数旨在查询并返回与指定用户相关的产品推荐类别ID列表。通过查询推荐表中与用户UID相关且类型为产品的推荐项,
  39. * 限制返回结果的数量为7条,并进一步获取每项产品的类别ID。此列表可用于显示用户个性化的推荐产品类别。
  40. *
  41. * @param int|null $uid 用户ID。可选参数,如果未指定,则默认查询所有推荐产品的类别ID列表。
  42. * @return array 返回包含产品类别ID的数组。如果查询结果为空或非数组,则返回空数组。
  43. */
  44. public function getRecommend(?int $uid)
  45. {
  46. // 根据用户ID和类型为'product'的条件查询推荐数据,并关联产品信息,限制返回结果数量为7
  47. $data = $this->dao->search(['uid' => $uid, 'type' => 'product'])->with(['product' => function ($query) {
  48. // 选择产品ID和类别ID字段
  49. $query->field('product_id,cate_id');
  50. }])->limit(7)->select();
  51. // 初始化用于存储类别ID的数组
  52. $i = [];
  53. // 如果查询结果是数组,则遍历结果,提取每条推荐产品的类别ID
  54. if (is_array($data)) {
  55. foreach ($data as $item) {
  56. // 将产品类别ID添加到类别ID数组中
  57. $i[] = $item['product']['cate_id'];
  58. }
  59. }
  60. // 返回包含产品类别ID的数组
  61. return $i;
  62. }
  63. /**
  64. * 获取用户产品浏览历史记录
  65. *
  66. * 本函数用于根据用户ID和分页信息,从数据库中检索该用户的产品浏览历史记录。
  67. * 它首先构造一个查询条件为用户ID和类型为产品的查询,然后计算符合条件的记录总数,
  68. * 最后根据指定的页码和每页记录数来获取记录列表。
  69. *
  70. * @param int $uid 用户ID,用于指定要查询哪个用户的浏览历史。
  71. * @param int $page 当前页码,用于分页查询。
  72. * @param int $limit 每页的记录数,用于分页查询。
  73. * @return array 返回一个包含浏览历史记录总数和列表的数组。
  74. */
  75. public function getHistory($uid,$page, $limit)
  76. {
  77. // 根据用户ID和类型为产品搜索浏览历史记录
  78. $query = $this->dao->search(['uid' => $uid, 'type' => 'product']);
  79. // 伴随查询产品信息,只获取指定的字段以减少数据量
  80. $query->with(['product'=>function($query){
  81. $query->field('product_id,image,store_name,slider_image,price,is_show,status,sales');
  82. }]);
  83. // 计算符合条件的浏览历史记录总数
  84. $count = $query->count();
  85. // 根据当前页码和每页记录数获取浏览历史记录列表
  86. $list = $query->page($page,$limit)->select();
  87. // 将记录总数和列表一起返回
  88. return compact('count','list');
  89. }
  90. /**
  91. * 获取搜索日志列表
  92. *
  93. * 根据给定的条件数组 $where,分页获取搜索日志,并包含用户信息。
  94. * 这个方法主要用于支持对搜索行为的数据统计和查询。
  95. *
  96. * @param array $where 搜索条件数组,用于精确匹配搜索日志。
  97. * @param int $page 当前页码,用于分页查询。
  98. * @param int $limit 每页数据条数,用于分页查询。
  99. * @return array 返回包含搜索日志总数和列表的数据数组。
  100. */
  101. public function getSearchLog(array $where, $page, $limit)
  102. {
  103. // 初始化搜索查询
  104. $query = $this->dao->search($where);
  105. // 关联加载用户信息,只获取指定字段以减少数据量
  106. $query->with(['user' => function ($query) {
  107. $query->field('uid,nickname,avatar,user_type');
  108. }]);
  109. // 计算满足条件的搜索日志总数
  110. $count = $query->count();
  111. // 分页查询搜索日志,并按创建时间降序排序
  112. $list = $query->page($page, $limit)->order('create_time DESC')->select();
  113. // 返回搜索日志总数和列表的组合
  114. return compact('count', 'list');
  115. }
  116. /**
  117. * 清除搜索日志
  118. *
  119. * @return void
  120. */
  121. public function clearSearchLog(array $where)
  122. {
  123. return $this->dao->clearSearchLog($where);
  124. }
  125. /**
  126. * 获取热门列表
  127. * 本函数旨在根据用户访问记录,统计前一天访问量最高的前10个产品或内容。
  128. * 如果前一天的访问数据不足,将返回最近访问量排名前100的内容。
  129. *
  130. * @return array 热门内容列表,包含前10个访问量最高的内容。
  131. */
  132. public function getHotList()
  133. {
  134. // 定义查询条件,特定类型为'searchProduct'
  135. $where['type'] = ['searchProduct'];
  136. // 查询前一天访问量最高的内容
  137. $data = app()->make(UserVisitRepository::class)
  138. ->search($where)
  139. ->whereDay('UserVisit.create_time', date('Y-m-d', strtotime("-1 day")))->column('content');
  140. // 如果前一天的数据为空,则查询历史访问量最高的100个内容
  141. if (empty($data)) {
  142. $data = app()->make(UserVisitRepository::class)
  143. ->search($where)->limit(100)->column('content');
  144. }
  145. // 统计每个内容出现的次数,按访问量降序排列
  146. $data = array_count_values($data);
  147. arsort($data);
  148. // 取出前10个访问量最高的内容
  149. $data = array_keys($data);
  150. $data = array_slice($data, 0, 10);
  151. return $data;
  152. }
  153. }