UserVisitDao.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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\UserVisit;
  14. use think\facade\Db;
  15. use think\Model;
  16. /**
  17. * Class UserVisitDao
  18. * @package app\common\dao\user
  19. * @author xaboy
  20. * @day 2020/5/27
  21. */
  22. class UserVisitDao extends BaseDao
  23. {
  24. /**
  25. * @return string
  26. * @author xaboy
  27. * @day 2020/5/27
  28. */
  29. protected function getModel(): string
  30. {
  31. return UserVisit::class;
  32. }
  33. /**
  34. * 添加访问记录
  35. *
  36. * 该方法用于创建并添加一个访问记录到数据存储中。它通过接收用户ID、访问类型、类型ID以及可选的内容描述,
  37. * 来综合形成一个访问记录,并将其存储。此方法的设计旨在简化访问记录的添加流程,通过一次调用即可完成。
  38. *
  39. * @param int $uid 用户ID。标识访问记录的用户。
  40. * @param string $type 访问类型。描述访问的具体类型,例如页面访问、资源下载等。
  41. * @param int $type_id 类型ID。与访问类型相关联的ID,用于更具体地标识访问的对象。
  42. * @param string $content 可选的内容描述。对访问记录的额外描述信息,可为空。
  43. * @return mixed 返回创建的访问记录的结果。具体类型取决于create方法的实现。
  44. */
  45. public function addVisit(int $uid, string $type, int $type_id, ?string $content = '')
  46. {
  47. // 使用compact函数收集参数并创建访问记录
  48. return $this->create(compact('uid', 'type', 'type_id', 'content'));
  49. }
  50. /**
  51. * 记录用户访问产品的行为
  52. *
  53. * 本函数用于在系统中记录某个用户访问特定产品的行为。通过调用内部的addVisit方法,
  54. * 实现对用户访问行为的统计和记录。这有助于分析用户兴趣和产品受欢迎程度,为后续的
  55. * 产品优化和推荐提供数据支持。
  56. *
  57. * @param int $uid 用户ID。标识访问产品的用户。
  58. * @param int $productId 产品ID。标识被访问的产品。
  59. * @return mixed 返回addVisit方法的执行结果。具体类型取决于addVisit方法的实现。
  60. */
  61. public function visitProduct(int $uid, int $productId)
  62. {
  63. // 调用addVisit方法记录用户的访问行为
  64. return $this->addVisit($uid, 'product', $productId);
  65. }
  66. /**
  67. * 访问页面的方法
  68. *
  69. * 本方法用于记录用户访问特定页面的行为。它通过调用addVisit方法来实现,
  70. * 将用户的ID、访问类型、无用的整数0(此处可能是为了固定参数格式)、以及被访问的页面URL作为参数。
  71. * 这样做是为了统计用户访问页面的次数,帮助分析用户行为和页面受欢迎程度。
  72. *
  73. * @param int $uid 用户ID。用于唯一标识访问页面的用户。
  74. * @param string $page 被访问的页面URL。用于标识用户访问的具体页面。
  75. * @return mixed 返回addVisit方法的执行结果。具体类型取决于addVisit方法的实现。
  76. */
  77. public function visitPage(int $uid, string $page)
  78. {
  79. // 调用addVisit方法来记录页面访问
  80. return $this->addVisit($uid, 'page', 0, $page);
  81. }
  82. /**
  83. * 访问小程序页面的方法
  84. *
  85. * 该方法用于记录用户访问小程序页面的行为。通过调用addVisit方法,将访问信息添加到数据库或其他记录介质中。
  86. * 参数$uid表示用户的唯一标识,$page表示被访问的页面名称或路径。此方法特别之处在于它指定访问类型为'smallProgram',
  87. * 并且不考虑访问时长(设置为0),这符合小程序访问的特点,即通常不需要记录详细的访问时长。
  88. *
  89. * @param int $uid 用户的唯一标识符。用于识别访问行为的发起者。
  90. * @param string $page 被访问的小程序页面名称或路径。用于记录用户访问的具体内容。
  91. * @return mixed 返回addVisit方法的执行结果。具体类型取决于addVisit方法的实现。
  92. */
  93. public function visitSmallProgram(int $uid, string $page)
  94. {
  95. return $this->addVisit($uid, 'smallProgram', 0, $page);
  96. }
  97. /**
  98. * 搜索产品
  99. *
  100. * 本函数用于根据用户ID、关键字和商家ID进行产品搜索。
  101. * 它主要通过调用添加访问记录的函数来实现,同时记录用户的搜索行为。
  102. *
  103. * @param int $uid 用户ID,用于识别进行搜索的用户。
  104. * @param string $keyword 搜索关键字,用户输入的用于搜索产品的内容。
  105. * @param int $merId 商家ID,可选参数,默认为0。用于指定搜索范围,如果为0则表示全局搜索。
  106. * @return mixed 返回添加访问记录的结果,具体类型取决于addVisit函数的实现。
  107. */
  108. public function searchProduct(int $uid, string $keyword, int $merId = 0)
  109. {
  110. // 调用addVisit函数记录用户的搜索行为并返回结果
  111. return $this->addVisit($uid, 'searchProduct', $merId, $keyword);
  112. }
  113. /**
  114. * 搜索商家
  115. *
  116. * 本函数用于执行对商家的搜索操作。它通过用户的ID和搜索关键字来执行搜索,并记录用户的访问行为。
  117. * 搜索不涉及具体的商家数据检索逻辑,而是作为一个触发访问记录添加的操作。
  118. *
  119. * @param int $uid 用户ID,用于标识进行搜索操作的用户。
  120. * @param string $keyword 搜索关键字,用户搜索商家时输入的关键词。
  121. * @return mixed 返回添加访问记录的结果,具体类型取决于addVisit方法的实现。
  122. */
  123. public function searchMerchant(int $uid, string $keyword)
  124. {
  125. // 添加访问记录,记录用户ID,访问类型为'searchMerchant',无特定访问ID,以及搜索关键字
  126. return $this->addVisit($uid, 'searchMerchant', 0, $keyword);
  127. }
  128. /**
  129. * 计算指定用户的商品访问总数
  130. *
  131. * 本函数通过查询用户访问记录表,统计指定用户对商品的访问次数。
  132. * 这里的“商品”是指类型为“product”的访问记录。
  133. *
  134. * @param int $uid 用户ID
  135. * @return int 用户访问商品的总次数
  136. */
  137. public function userTotalVisit($uid)
  138. {
  139. // 使用UserVisit类的静态方法getDB来获取数据库实例,并构造查询条件,统计指定用户uid且访问类型为product的记录总数
  140. return UserVisit::getDB()->where('uid', $uid)->where('type', 'product')->count();
  141. }
  142. /**
  143. * 用户访问搜索
  144. * @param array $where
  145. * @return \think\db\BaseQuery
  146. * @author wuhaotian
  147. * @email 442384644@qq.com
  148. * @date 2024/7/18
  149. */
  150. public function search(array $where)
  151. {
  152. $query = UserVisit::hasWhere('user', function ($query) use ($where) {
  153. $query = $query->where(true);
  154. $query->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  155. $query->whereLike('User.nickname', "%{$where['nickname']}%");
  156. })->when(isset($where['user_type']) && $where['user_type'] !== '', function ($query) use ($where) {
  157. $query->where('User.user_type', $where['user_type']);
  158. })->when(isset($where['phone']) && $where['phone'] !== '', function ($query) use ($where) {
  159. $query->whereLike('User.phone', "%{$where['phone']}%");
  160. });
  161. });
  162. $query = $query->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  163. $query->where('UserVisit.uid', $where['uid']);
  164. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  165. $query->where('UserVisit.type_id', $where['mer_id']);
  166. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  167. if(is_array($where['type'])){
  168. $query->where('UserVisit.type','in', $where['type']);
  169. }else{
  170. $query->where('UserVisit.type', $where['type']);
  171. }
  172. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  173. getModelTime($query, $where['date'], 'UserVisit.create_time');
  174. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  175. $query->whereLike('UserVisit.content', "%{$where['keyword']}%");
  176. });
  177. return $query->order('UserVisit.create_time DESC');
  178. }
  179. /**
  180. * 计算指定日期内,每个用户的访问次数。
  181. *
  182. * 本函数用于统计在给定日期内,每个用户的访问产品数量。
  183. * 如果提供了商家ID,则只统计该商家的产品访问情况。
  184. *
  185. * @param string $date 统计日期,格式为YYYY-MM-DD。
  186. * @param int|null $merId 商家ID,可选参数,用于过滤特定商家的数据。
  187. * @return int 返回访问用户数量。
  188. */
  189. public function dateVisitUserNum($date, $merId = null)
  190. {
  191. // 从UserVisit模型中获取数据库实例,并设置别名为A
  192. return UserVisit::getDB()->alias('A')
  193. // 加入StoreProduct表,以获取产品相关信息
  194. ->join('StoreProduct B', 'A.type_id = B.product_id')
  195. // 当$date提供时,根据$date条件查询
  196. ->when($date, function ($query, $date) {
  197. // 设置查询条件,根据$date筛选创建时间
  198. getModelTime($query, $date, 'A.create_time');
  199. })
  200. // 当$merId提供时,添加条件查询特定商家的产品
  201. ->when($merId, function ($query, $merId) {
  202. // 筛选商家ID为$merId的记录
  203. $query->where('B.mer_id', $merId);
  204. })
  205. // 筛选类型为产品的记录
  206. ->where('A.type', 'product')
  207. // 按用户ID分组,以统计每个用户的访问次数
  208. ->group('uid')
  209. // 统计记录总数,即访问的用户数量
  210. ->count();
  211. }
  212. /**
  213. * 获取指定日期范围内访问商家的数量
  214. *
  215. * 本函数通过查询用户访问记录,结合商家产品信息,统计在指定日期范围内每个商家被访问的次数。
  216. * 默认查询最近7天的数据,可自定义查询日期范围。返回结果按访问次数降序排列。
  217. *
  218. * @param string $date 查询日期范围,格式为'YYYY-MM-DD'。如果不提供,则查询最近7天的数据。
  219. * @param int $limit 返回结果的数量限制,默认为7。
  220. * @return array 返回一个包含每个商家访问次数的数组,每个元素包含商家ID(mer_id)、商家名称(mer_name)和总访问次数(total)。
  221. */
  222. public function dateVisitMerchantNum($date, $limit = 7)
  223. {
  224. // 初始化查询,设置别名为A,以便在后续的JOIN操作中引用
  225. return UserVisit::getDB()->alias('A')
  226. // 加入StoreProduct表,通过type_id与A表关联,别名为B
  227. ->join('StoreProduct B', 'A.type_id = B.product_id')
  228. // 加入Merchant表,通过mer_id与B表关联,别名为C
  229. ->join('Merchant C', 'C.mer_id = B.mer_id')
  230. // 定义要返回的字段,包括访问次数、商家ID和商家名称
  231. ->field(Db::raw('count(A.type) as total,B.mer_id,C.mer_name,C.care_ficti, C.care_count'))
  232. // 当传入$date时,根据$date调整查询的创建时间范围
  233. ->when($date, function ($query, $date) {
  234. getModelTime($query, $date, 'A.create_time');
  235. })
  236. // 限制查询的类型为'product'
  237. ->where('A.type', 'product')
  238. // 限制返回的结果数量
  239. ->limit($limit)
  240. // 按商家ID分组,统计每个商家的访问次数
  241. ->group('B.mer_id')
  242. // 按总访问次数降序排列
  243. ->order('total DESC')
  244. // 执行查询并返回结果
  245. ->select();
  246. }
  247. /**
  248. * 根据指定日期和商家ID,获取该商家产品访问量排名前$limit位的产品信息。
  249. *
  250. * 本函数通过查询用户访问记录和产品信息,统计指定日期内每个产品的访问量,并返回访问量排名前$limit的产品列表。
  251. * 这里的访问量是指用户点击产品的次数。
  252. *
  253. * @param string $date 查询的日期,格式为YYYY-MM-DD。如果未指定日期,则查询最近$limit天的数据。
  254. * @param int $merId 商家ID,用于限定查询商家下的产品。
  255. * @param int $limit 返回结果的数量限制,默认为7,即返回访问量排名前7的产品。
  256. * @return array 返回一个包含产品访问量排名前$limit的产品列表,每个元素包含产品访问量、产品图片、产品名称。
  257. */
  258. public function dateVisitProductNum($date, $merId, $limit = 7)
  259. {
  260. // 从用户访问记录表中查询数据,关联产品信息表和商家信息表
  261. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  262. ->join('Merchant C', 'C.mer_id = B.mer_id')
  263. // 选择查询的字段,包括产品访问量、产品图片和产品名称
  264. ->field(Db::raw('count(A.type_id) as total,B.image,B.store_name'))
  265. // 根据传入的日期条件进行查询,如果指定了日期,则只查询该日期的数据
  266. ->when($date, function ($query, $date) {
  267. getModelTime($query, $date, 'A.create_time');
  268. })
  269. // 限定只查询访问类型为产品的记录,并且产品所属商家为传入的商家ID
  270. ->where('A.type', 'product')->where('B.mer_id', $merId)
  271. ->where('B.is_del', 0)->where('B.status', '1')
  272. // 按产品ID分组,统计每个产品的访问量
  273. ->group('A.type_id')
  274. // 按产品访问量降序排序
  275. ->order('total DESC')
  276. // 限制返回的结果数量
  277. ->limit($limit)
  278. // 执行查询并返回结果
  279. ->select();
  280. }
  281. /**
  282. * 计算指定日期内访问商家的总用户数
  283. *
  284. * 本函数通过查询用户访问记录,统计在指定日期内所有访问类型为产品的用户的数量。
  285. * 这里的访问记录是指用户与商家交互的行为记录,类型为产品表示用户访问了商家的产品。
  286. *
  287. * @param string $date 需要查询的日期,格式为YYYY-MM-DD
  288. * @return int 指定日期内访问商家的总用户数
  289. */
  290. public function dateVisitMerchantTotal($date)
  291. {
  292. // 使用UserVisit类中的getDB方法获取数据库连接
  293. // 当$date有值时,执行闭包函数进行查询条件的添加
  294. return UserVisit::getDB()->when($date, function ($query, $date) {
  295. // 添加查询条件,根据$date查询创建时间在该日期内的记录
  296. getModelTime($query, $date, 'create_time');
  297. })->whereIn('type', 'product')->count();
  298. // 筛选类型为产品的记录,然后统计满足条件的记录总数
  299. }
  300. /**
  301. * 计算指定日期的访问数量。
  302. *
  303. * 本函数用于统计在给定日期内,用户访问页面和小程序的总次数。
  304. * 它通过查询UserVisit模型的数据库记录,筛选出类型为'page'或'smallProgram'的访问记录,
  305. * 并统计这些记录的数量。
  306. *
  307. * @param string $date 需要统计访问数量的日期。
  308. * @return int 指定日期的访问数量。
  309. */
  310. public function dateVisitNum($date)
  311. {
  312. // 使用when方法条件性地加入日期查询条件,如果$date不为空,则根据$date查询创建时间
  313. return UserVisit::getDB()->when($date, function ($query, $date) {
  314. // 这里将查询条件应用到创建时间字段,用于筛选指定日期的记录
  315. getModelTime($query, $date, 'create_time');
  316. })->whereIn('type', ['page', 'smallProgram'])->count();
  317. // 筛选类型为'page'或'smallProgram'的记录,统计它们的数量
  318. }
  319. /**
  320. * 根据指定日期分组统计每日访问用户数。
  321. *
  322. * 本函数用于查询在指定日期范围内的每日独立访问用户数。
  323. * 通过分组查询,将创建时间转换为每天的格式,并统计每天的独立访问用户数。
  324. * 如果指定了日期,则查询该日期的访问数据;如果没有指定日期,则查询所有数据。
  325. *
  326. * @param string $date 指定的日期,格式为YYYY-MM-DD。如果为空,则查询所有日期的数据。
  327. * @return array 返回一个数组,每个元素包含每天的日期和独立访问用户数。
  328. */
  329. public function dateVisitNumGroup($date)
  330. {
  331. // 使用UserVisit模型的getDB方法获取数据库对象
  332. return UserVisit::getDB()->when($date, function ($query, $date) {
  333. // 如果指定了日期,则在查询中添加时间范围条件
  334. getModelTime($query, $date, 'create_time');
  335. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(DISTINCT uid) as total'))
  336. // 按日期分组
  337. ->group('time')
  338. // 按日期升序排序
  339. ->order('time ASC')->select();
  340. }
  341. /**
  342. * 批量删除用户访问记录。
  343. *
  344. * 本函数提供两种方式批量删除用户访问记录:一种是根据访问ID数组进行删除,另一种是根据用户ID删除指定类型的访问记录。
  345. * - 当提供$ids参数时,会删除指定访问ID的记录。
  346. * - 当提供$uid参数时,会删除指定用户ID的所有产品访问记录。
  347. *
  348. * @param array|null $ids 访问ID数组,用于指定删除哪些访问记录。
  349. * @param int|null $uid 用户ID,用于指定删除属于哪个用户的访问记录。
  350. * @return int 删除的记录数。
  351. */
  352. public function batchDelete(? array $ids,?int $uid)
  353. {
  354. // 如果提供了访问ID数组,则根据这些ID删除对应的访问记录。
  355. if($ids) return UserVisit::getDB()->where($this->getPk(),'in',$ids)->delete();
  356. // 如果提供了用户ID但没有提供访问ID数组,则删除该用户的所有产品访问记录。
  357. if($uid) return UserVisit::getDB()->where('uid',$uid)->where('type','product')->delete();
  358. }
  359. /**
  360. * 获取搜索日志
  361. *
  362. * @param array $where
  363. * @return void
  364. */
  365. public function clearSearchLog(array $where)
  366. {
  367. $query = $this->getModel()::when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  368. $query->where('type_id', $where['mer_id']);
  369. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  370. if(is_array($where['type'])){
  371. $query->where('type','in', $where['type']);
  372. }else{
  373. $query->where('type', $where['type']);
  374. }
  375. });
  376. return $query->delete();
  377. }
  378. }