UserRelationDao.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\UserRelation;
  14. use app\common\model\user\UserRelation as model;
  15. /**
  16. * Class UserVisitDao
  17. * @package app\common\dao\user
  18. * @author xaboy
  19. * @day 2020/5/27
  20. */
  21. class UserRelationDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/5/27
  27. */
  28. protected function getModel(): string
  29. {
  30. return model::class;
  31. }
  32. /**
  33. * 检查指定字段是否存在
  34. *
  35. * 该方法通过查询数据库来确定给定字段的值是否在指定的条件下存在。
  36. * 它支持通过可选参数来进一步限定查询的条件,如类型(type)和用户ID(uid)。
  37. * 主要用于API中对数据存在性的验证。
  38. *
  39. * @param string $field 要查询的字段名
  40. * @param mixed $value 字段对应的值
  41. * @param string|null $type 可选参数,限定查询的数据类型
  42. * @param int|null $uid 可选参数,限定查询的用户ID
  43. * @return bool 查询结果,存在返回true,不存在返回false
  44. */
  45. public function apiFieldExists($field, $value, $type = null, $uid = null)
  46. {
  47. // 调用getModel方法获取模型实例,并链式调用getDB方法获取数据库实例
  48. return $this->getModel()::getDB()
  49. // 当$uid不为空时,添加where条件查询指定用户ID的数据
  50. ->when($uid, function ($query) use ($uid) {
  51. $query->where('uid', $uid);
  52. })
  53. // 当$type不为空时,添加where条件查询指定类型的数据
  54. ->when(!is_null($type), function ($query) use ($type) {
  55. $query->where('type', $type);
  56. })
  57. // 最终添加查询字段和值的条件
  58. ->where($field, $value);
  59. }
  60. /**
  61. * 根据条件搜索数据。
  62. *
  63. * 本函数用于根据提供的$where参数条件来搜索数据库中的记录。它支持两种条件:'type' 和 'uid'。
  64. * 'type' 条件的处理逻辑是,如果类型值是在预定义的数组[1,2,3,4]中,则使用whereIn查询;
  65. * 否则,直接使用where查询。'uid' 条件直接使用where查询。
  66. *
  67. * @param array $where 搜索条件数组,包含'type'和'uid'等键值对。
  68. * @return \Illuminate\Database\Query\Builder|static 返回一个查询构建器实例,已应用搜索条件和排序。
  69. */
  70. public function search($where)
  71. {
  72. // 从模型中获取数据库实例,并应用条件查询
  73. $query = ($this->getModel()::getDB())
  74. // 当'type'条件存在且不为空时,应用相应的查询条件
  75. ->when((isset($where['type']) && $where['type'] !== ''), function ($query) use ($where) {
  76. // 如果类型值在预定义数组中,使用whereIn查询;否则,直接使用where查询
  77. if(in_array($where['type'],[1,2,3,4])){
  78. $query->whereIn('type',[1,2,3,4]);
  79. }else{
  80. $query->where('type',$where['type']);
  81. }
  82. })
  83. // 当'uid'条件存在时,应用相应的查询条件
  84. ->when((isset($where['uid']) && $where['uid']), function ($query) use ($where) {
  85. $query->where('uid', $where['uid']);
  86. });
  87. // 返回应用了排序的查询构建器
  88. return $query->order('create_time DESC');
  89. }
  90. /**
  91. * 删除数据
  92. *
  93. * 本函数用于根据指定的条件删除数据库中的记录。
  94. * 通过传入一个包含条件的数组,利用这些条件来定位特定的记录,并将它们从数据库中删除。
  95. *
  96. * @param array $where 删除条件数组,包含一个或多个用于定位记录的条件。
  97. */
  98. public function destory(array $where)
  99. {
  100. // 获取模型对应的数据库实例,并构建删除语句
  101. ($this->getModel()::getDB())->where($where)->delete();
  102. }
  103. /**
  104. * 计算指定日期内喜欢商家的用户数量
  105. *
  106. * 本函数用于查询在指定日期内,对特定商家喜欢的用户数量。
  107. * 如果未指定商家ID,则返回所有商家的喜欢用户数量。
  108. *
  109. * @param string $day 指定的日期,用于查询该日期内的喜欢情况。
  110. * @param int|null $merId 商家ID,可选参数,用于指定查询特定商家的喜欢用户数量。
  111. * @return int 返回指定日期内喜欢商家的用户数量。
  112. */
  113. public function dayLikeStore($day, $merId = null)
  114. {
  115. // 使用UserRelation模型查询数据库,条件为类型为10(表示喜欢操作),
  116. // 如果提供了商家ID,则进一步筛选出喜欢该商家的用户。
  117. // 最后,通过getModelTime处理查询结果,聚焦在指定日期,并计算用户数量。
  118. return getModelTime(UserRelation::getDB()->where('type', 10)->when($merId, function ($query, $merId) {
  119. $query->where('type_id', $merId);
  120. }), $day)->count();
  121. }
  122. /**
  123. * 统计指定日期内,指定商户的访问次数。
  124. *
  125. * 本函数用于查询在特定日期内,特定商户的用户访问次数。
  126. * 如果未指定日期,则返回所有时间的访问次数;如果未指定商户ID,则返回所有商户的访问次数。
  127. *
  128. * @param string $date 查询的日期,格式为YYYY-MM-DD。
  129. * @param int $merId 商户ID,可选参数,用于指定查询特定商户的访问次数。
  130. * @return int 返回符合条件的访问次数。
  131. */
  132. public function dateVisitStore($date, $merId = null)
  133. {
  134. // 从UserRelation类的getDB方法获取数据库查询对象,并开始构建查询条件
  135. return UserRelation::getDB()->where('type', 11)->when($merId, function ($query, $merId) {
  136. // 如果指定了商户ID,则添加where条件,查询特定商户的访问次数
  137. $query->where('type_id', $merId);
  138. })->when($date, function ($query, $date) {
  139. // 如果指定了日期,则调用getModelTime函数,添加where条件,查询特定日期的访问次数
  140. getModelTime($query, $date, 'create_time');
  141. })->count();
  142. // 计算符合条件的记录数,即访问次数,并返回结果
  143. }
  144. /**
  145. * 获取用户ID与指定类型ID交集的函数
  146. *
  147. * 本函数用于查询与给定用户ID相关的特定类型ID集合,
  148. * 其中类型ID是在预定义范围内的一组特定值。
  149. * 通过比较数据库中记录的类型ID,确定与用户相关联的ID集合。
  150. *
  151. * @param int $uid 用户的唯一标识ID
  152. * @param array $ids 指定的类型ID集合
  153. * @return array 返回与用户ID相关的指定类型ID的交集
  154. */
  155. public function intersectionPayer($uid, array $ids): array
  156. {
  157. // 通过UserRelation类的getDB方法获取数据库操作对象
  158. // 然后根据$uid查询满足条件的记录,其中"type"为12且"type_id"在$ids数组内的记录
  159. // 最后返回这些记录的"type_id"字段组成的数组
  160. return UserRelation::getDB()->where('uid', $uid)->whereIn('type', 12)->whereIn('type_id', $ids)->column('type_id');
  161. }
  162. /**
  163. * 获取用户关联的商品到社区的信息
  164. *
  165. * 本函数用于构建查询用户关联的商品(SPU)到社区的条件。支持通过关键词过滤,并限定关联状态为有效。
  166. * 主要用于在用户社区功能中展示用户关联的商品。
  167. *
  168. * @param string|null $keyword 关键词,用于过滤商品名称。如果为null,则不进行关键词过滤。
  169. * @param int $uid 用户ID,指定查询哪个用户的关联商品信息。
  170. * @return \Illuminate\Database\Eloquent\Builder 返回构建好的查询构建器对象,用于进一步的查询操作或数据获取。
  171. */
  172. public function getUserProductToCommunity(?string $keyword, int $uid)
  173. {
  174. // 根据$keyword条件构建商品(SPU)的查询条件,支持模糊搜索,并限定关联状态为1(有效)
  175. $query = UserRelation::hasWhere('spu', function ($query) use($keyword) {
  176. $query->when($keyword, function ($query) use($keyword) {
  177. // 当关键词存在时,进行模糊搜索
  178. $query->whereLike('store_name',"%{$keyword}%");
  179. });
  180. // 限定关联状态为有效
  181. $query->where('status',1);
  182. });
  183. // 指定查询用户ID为$uid的关联信息
  184. $query->where('uid',$uid);
  185. return $query;
  186. }
  187. }