ProductAssistSetDao.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\ProductAssistSet;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use think\Exception;
  17. class ProductAssistSetDao extends BaseDao
  18. {
  19. protected function getModel(): string
  20. {
  21. return ProductAssistSet::class;
  22. }
  23. /**
  24. * 增加数值统计
  25. * 该方法用于根据类型增加指定ID的分享数或浏览数。
  26. * @param int $type 类型标识,1代表增加分享数,2代表增加浏览数。
  27. * @param int $id 要操作的数据ID。
  28. * @param int $inc 增加的数值,默认为1。
  29. */
  30. public function incNum(int $type,int $id,int $inc = 1)
  31. {
  32. try{
  33. // 根据ID获取模型实例,并准备更新操作
  34. $query = $this->getModel()::where($this->getPk(),$id);
  35. // 根据$type的值,执行不同的增量更新操作
  36. if($type == 1) {
  37. // 增加分享数
  38. $query->inc('share_num',$inc)->update();
  39. }
  40. if($type == 2) {
  41. // 增加浏览数
  42. $query->inc('view_num',$inc)->update();
  43. }
  44. }catch (Exception $exception){
  45. // 捕获并处理异常,此处为空实现,可根据需要添加日志记录等操作
  46. }
  47. }
  48. /**
  49. * 获取用户数量及最近活跃用户的列表
  50. *
  51. * 本函数用于查询数据库中用户总数,并获取最近活跃的10位用户的信息。
  52. * 活跃用户的信息中包括用户ID和头像URL。
  53. *
  54. * @return array 返回包含用户总数和最近活跃用户列表的数组。
  55. */
  56. public function userCount()
  57. {
  58. // 查询数据库中的用户总数
  59. $count = $this->getModel()::getDB()->count("*");
  60. // 查询最近活跃的10位用户的信息,按创建时间降序排列,并包含用户ID和头像信息
  61. $res = $this->getModel()::getDB()
  62. ->order('create_time DESC')
  63. ->with(['user' => function($query){
  64. // 仅获取用户ID和头像URL
  65. $query->field('uid,avatar avatar_img');
  66. }])
  67. ->limit(10)
  68. ->group('uid')
  69. ->select()
  70. ->toArray();
  71. // 筛选带有有效头像URL的用户信息
  72. $list = [];
  73. foreach ($res as $item){
  74. if(isset($item['user']['avatar_img']) && $item['user']['avatar_img']){
  75. $list[] = $item['user'];
  76. }
  77. }
  78. // 返回用户总数和活跃用户列表
  79. return compact('count','list');
  80. }
  81. /**
  82. * 更新状态
  83. * @param int $id
  84. * @author Qinii
  85. * @day 2020-11-25
  86. */
  87. public function changStatus(int $id)
  88. {
  89. $this->getModel()::getDB()->where($this->getPk(),$id)->update(['status' => 20]);
  90. }
  91. }