UserHistoryRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 think\facade\Log;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\dao\user\UserHistoryDao;
  15. use app\common\repositories\store\product\SpuRepository;
  16. class UserHistoryRepository extends BaseRepository
  17. {
  18. protected $dao;
  19. /**
  20. * UserHistoryRepository constructor.
  21. * @param UserHistoryDao $dao
  22. */
  23. public function __construct(UserHistoryDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * 获取API列表
  29. * 根据用户请求的页面、每页数量、用户ID和类型,查询特定用户的API调用历史。
  30. * 如果类型为1,还会额外加载SPU信息,并计算该用户的总历史记录数。
  31. *
  32. * @param int $page 当前页码
  33. * @param int $limit 每页数量
  34. * @param int $uid 用户ID
  35. * @param int $type 查询类型,1表示包含SPU信息
  36. * @return array 返回包含总数和列表数据的数组
  37. */
  38. public function getApiList($page,$limit,$uid,$type)
  39. {
  40. // 初始化包含关系数组
  41. $with = [];
  42. // 如果类型为1,查询时包含SPU信息,并计算用户的历史记录总数
  43. if($type == 1){
  44. $with = ['spu'];
  45. $count = app()->make(UserHistoryRepository::class)->userTotalHistory($uid);
  46. }
  47. // 根据用户ID和类型进行查询
  48. $query = $this->dao->search($uid,$type);
  49. // 设置查询的包含关系和排序方式
  50. $query->with($with)->order('update_time DESC');
  51. // 如果类型不为1,则计算查询结果的总数
  52. $count = $count ?? $query->count();
  53. // 分页查询数据
  54. $data = $query->page($page,$limit)->select();
  55. // 初始化最终返回的结果数组和列表数组
  56. $res = $list = [];
  57. // 遍历查询结果,按更新时间分组
  58. foreach ($data as $item) {
  59. if ($item['spu']) {
  60. $time = date('m月d日',strtotime($item['update_time']));
  61. $res[$time][] = $item;
  62. }
  63. }
  64. // 将分组后的数据转换为日期-列表的形式
  65. foreach ($res as $k => $v) {
  66. $list[] = ['date' => $k, 'list' => $v];
  67. }
  68. // 返回总数和列表数据
  69. return compact('count','list');
  70. }
  71. /**
  72. * 获取列表数据
  73. * 根据给定的页码和每页数量,以及用户ID和类型,获取对应的数据列表。
  74. * 如果类型为1,则同时获取SPU数据。
  75. *
  76. * @param int $page 当前页码
  77. * @param int $limit 每页数据数量
  78. * @param int $uid 用户ID
  79. * @param int $type 数据类型
  80. * @return array 包含数据总数和数据列表的数组
  81. */
  82. public function getList($page,$limit,$uid,$type)
  83. {
  84. // 初始化with数组,用于指定关联查询
  85. $with = [];
  86. // 如果类型为1,加载SPU关联数据
  87. if($type == 1)$with = ['spu'];
  88. // 初始化查询对象
  89. $query = $this->dao->search($uid,$type);
  90. // 设置查询条件:关联查询和按更新时间降序排序
  91. $query->with($with)->order('update_time DESC');
  92. // 计算总数据量
  93. $count = $query->count();
  94. // 分页查询数据
  95. $list = $query->page($page,$limit)->select();
  96. // 返回包含总数和列表的数据数组
  97. return compact('count','list');
  98. }
  99. /**
  100. * 根据传入的数据创建或更新SPU。
  101. *
  102. * 此方法用于处理产品类型为0、1或其它情况下的SPU创建或更新逻辑。
  103. * 它首先根据产品类型组装查询条件,然后尝试从数据库中检索相应的SPU信息。
  104. * 如果找到了SPU,并且SPU有有效的ID,则将相关信息存储到浏览记录表中。
  105. * 如果捕获到异常,则记录日志信息。
  106. *
  107. * @param array $data 包含产品类型、ID和其它必要信息的数据数组。
  108. * @return mixed 返回数据库查询结果,如果未找到则返回null。
  109. */
  110. public function createOrUpdate(array $data)
  111. {
  112. // 实例化SPURepository类,用于后续的SPU数据操作
  113. $make = app()->make(SpuRepository::class);
  114. // 初始化查询条件中的产品类型
  115. $where['product_type'] = $data['product_type'];
  116. // 根据产品类型组装查询条件
  117. switch ($data['product_type']) {
  118. case 0:
  119. case 1:
  120. // 产品类型为0或1时,使用产品ID作为查询条件
  121. $where['product_id'] = $data['id'];
  122. break;
  123. default:
  124. // 其他产品类型使用活动ID作为查询条件
  125. $where['activity_id'] = $data['id'];
  126. break;
  127. }
  128. try {
  129. // 根据组装的查询条件尝试获取SPU信息
  130. $ret = $make->getSearch($where)->find();
  131. // 如果找到了SPU,并且SPU有有效的ID,则更新浏览记录
  132. if ($ret && $ret['spu_id']) {
  133. $arr = [
  134. 'res_type' => $data['res_type'],
  135. 'res_id' => $ret['spu_id'],
  136. 'uid' => $data['uid']
  137. ];
  138. $this->dao->createOrUpdate($arr);
  139. }
  140. // 返回查询结果
  141. return $ret;
  142. } catch (\Exception $exception) {
  143. // 捕获异常并记录日志
  144. Log::info('浏览记录添加失败,ID:' . $data['id'] . '类型:' . $data['product_type']);
  145. }
  146. }
  147. /**
  148. * 商品推荐列表
  149. * @param int|null $uid
  150. * @return array
  151. * @author Qinii
  152. * @day 4/9/21
  153. */
  154. public function getRecommend(?int $uid)
  155. {
  156. $ret = $this->dao->search($uid,1)->with(['spu.product'])->limit(10)->select();
  157. if(!$ret) return [];
  158. $i = [];
  159. foreach ($ret as $item){
  160. if(isset($item['spu']['product']['cate_id'])) $i[] = $item['spu']['product']['cate_id'];
  161. }
  162. if($i) $i = array_unique($i);
  163. return $i;
  164. }
  165. /**
  166. * 获取历史记录列表
  167. *
  168. * 本函数用于根据给定的条件查询SPU相关的历史记录,并分页返回结果。
  169. * 主要包括查询条件、分页参数,以及返回数据的格式化。
  170. *
  171. * @param string|array $where 查询条件,可以是字符串或者数组形式的SQL条件。
  172. * @param int $page 当前页码,用于分页查询。
  173. * @param int $limit 每页显示的记录数,用于分页查询。
  174. * @return array 返回包含记录总数和历史记录列表的数组。
  175. */
  176. public function historyLst($where,$page,$limit)
  177. {
  178. // 根据查询条件进行SPU的连接查询
  179. $query = $this->dao->joinSpu($where);
  180. // 声明要包含的关联数据,并按更新时间降序排序
  181. $query->with([
  182. 'spu'
  183. ])->order('update_time DESC');
  184. // 计算满足条件的总记录数
  185. $count = $query->count();
  186. // 进行分页查询,并指定查询字段,返回历史记录列表
  187. $list = $query->page($page, $limit)
  188. ->setOption('field',[])->field('uid,product_id,product_type,spu_id,image,store_name,price')
  189. ->select();
  190. // 返回包含记录总数和历史记录列表的数组
  191. return compact('count','list');
  192. }
  193. }