UserHistoryDao.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\dao\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\user\UserHistory;
  14. class UserHistoryDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return UserHistory::class;
  19. }
  20. /**
  21. * 根据给定的数据创建或更新记录。
  22. * 此方法首先尝试根据数据中的条件查询现有记录,如果存在,则更新记录的更新时间并保存;
  23. * 如果不存在,则将更新时间添加到数据中并创建新记录。
  24. * @param array $data 包含要操作的数据的数组,其中的条件用于查询或插入。
  25. */
  26. public function createOrUpdate(array $data)
  27. {
  28. // 尝试根据$data中的条件查询现有记录
  29. $ret = $this->getModel()::getDB()->where($data)->find();
  30. if ($ret) {
  31. // 如果记录存在,更新更新时间并保存
  32. $ret->update_time = time();
  33. $ret->save();
  34. } else {
  35. // 如果记录不存在,将更新时间添加到数据数组中并创建新记录
  36. $data['update_time'] = time();
  37. $this->create($data);
  38. }
  39. }
  40. /**
  41. * 根据用户ID和资源类型搜索资源。
  42. *
  43. * 此方法提供了一个灵活的方式来查询资源,可以根据用户ID和资源类型进行过滤。
  44. * 查询构建利用了when方法来条件性地添加where子句,确保了只有在相应的参数非空时才应用过滤条件。
  45. * 最后,通过order方法指定了查询结果的排序方式,按照更新时间降序排列。
  46. *
  47. * @param int|null $uid 用户ID。如果提供,将根据用户ID过滤结果。
  48. * @param int $type 资源类型。如果提供,将根据资源类型过滤结果。
  49. * @return 查询对象,已经应用了过滤条件和排序,可用于进一步的操作,如获取结果集。
  50. */
  51. public function search(?int $uid, int $type)
  52. {
  53. // 获取数据库实例,并根据$uid和$type条件性地添加where子句
  54. $query = ($this->getModel()::getDB())->when($uid, function ($query) use ($uid) {
  55. $query->where('uid', $uid);
  56. })->when($type, function ($query) use ($type) {
  57. $query->where('res_type', $type);
  58. });
  59. // 对查询结果按更新时间降序排序
  60. return $query->order('update_time DESC');
  61. }
  62. /**
  63. * 批量删除记录。
  64. * 根据传入的参数,本函数支持两种方式批量删除数据:
  65. * 1. 当$data为数组时,删除主键在$data数组内的所有记录。
  66. * 2. 当$data为1时,删除所有uid为$uid的记录。
  67. *
  68. * @param int $uid 用户ID,当$data为1时,用于指定删除哪个用户的记录。
  69. * @param array|int $data 要删除的记录的主键数组,或者当值为1时,表示删除指定用户的所有记录。
  70. */
  71. public function deleteBatch($uid,$data)
  72. {
  73. // 检查$data是否为数组,如果是,删除主键在数组内的记录。
  74. if(is_array($data)){
  75. $this->getModel()::getDB()->where($this->getPk(),'in',$data)->delete();
  76. }else if($data == 1){
  77. // 如果$data为1,删除所有uid为$uid的记录。
  78. $this->getModel()::getDB()->where('uid',$uid)->delete();
  79. }
  80. }
  81. /**
  82. * 计算用户的历史记录总数
  83. *
  84. * 本函数用于查询指定用户(通过$uid标识)在系统中的历史记录总数。特别地,它只计算
  85. * 类型为1的资源的历史记录,这通常表示用户的购买、浏览或其他有意义的操作次数。
  86. *
  87. * @param int $uid 用户ID,用于指定要查询历史记录的用户。
  88. * @return int 返回用户的历史记录总数。这个数字只包括与资源类型为1相关的记录。
  89. */
  90. public function userTotalHistory($uid)
  91. {
  92. // 使用别名H来引用UserHistory模型,以便在查询中使用
  93. // 加入store_spu表的关联查询,通过S.spu_id = H.res_id条件关联
  94. // 筛选条件:uid为指定用户ID且res_type为1
  95. // 返回满足条件的记录总数
  96. return UserHistory::alias('H')
  97. ->join('store_spu S','S.spu_id = H.res_id')
  98. ->where('uid',$uid)
  99. ->where('res_type', 1)
  100. ->count();
  101. }
  102. /**
  103. * 根据条件查询关联的SPU
  104. *
  105. * 本函数用于构建一个查询用户历史记录中特定SPU的查询语句。它允许通过关键词和其他条件来过滤结果。
  106. * 主要用于在用户历史记录中查找与特定条件匹配的SPU实体。
  107. *
  108. * @param array $where 查询条件数组,包含关键词、用户ID和资源类型。
  109. * @return \Illuminate\Database\Eloquent\Builder 返回构建好的查询构建器对象,用于进一步的查询操作或数据获取。
  110. */
  111. public function joinSpu($where)
  112. {
  113. // 初始化查询,基于是否有关键词来动态构建WHERE子句
  114. $query = UserHistory::hasWhere('spu',function($query) use($where){
  115. // 如果关键词存在且不为空,则添加LIKE条件来筛选store_name
  116. $query->when(isset($where['keyword']) && $where['keyword'] !== '', function($query) use ($where){
  117. // 使用LIKE操作符来匹配关键词
  118. $query->whereLike('store_name',"%{$where['keyword']}%");
  119. });
  120. // 添加一个总是为真的条件,用于确保WHERE子句的存在,以便后续的AND连接
  121. $query->where(true);
  122. });
  123. // 添加用户ID和资源类型作为查询条件
  124. $query->where('uid', $where['uid']);
  125. $query->where('res_type', $where['type']);
  126. // 返回构建好的查询构建器对象
  127. return $query;
  128. }
  129. }