UserDynamicComment.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\Paginator;
  16. /**
  17. * 用户动态评论模型类
  18. * Class UserDynamicComment
  19. * @package app\common\model
  20. */
  21. class UserDynamicComment extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'user_dynamic_comment';
  25. // 定义主键
  26. protected $pk = 'id';
  27. /**
  28. * 获取列表
  29. * @param array $param 查询条件
  30. * @param int $listRows 分页数量
  31. * @return mixed
  32. * @throws \think\db\exception\DbException
  33. */
  34. public function getList(array $param = [], int $listRows = 15)
  35. {
  36. // 筛选条件
  37. $query = $this->getQueryFilter($param);
  38. // 排序条件
  39. $sort = $this->setQuerySort($param);
  40. // 执行查询
  41. $list = $query->alias($this->name)
  42. ->order($sort)
  43. ->paginate($listRows);
  44. // 整理列表数据并返回
  45. return $list;
  46. }
  47. /**
  48. * 设置商品展示的数据
  49. * @param Collection|Paginator $list 商品列表
  50. * @param callable|null $callback 回调函数
  51. * @return mixed
  52. */
  53. protected function setListData($list, callable $callback = null)
  54. {
  55. if ($list->isEmpty()) return $list;
  56. // 遍历商品列表整理数据
  57. foreach ($list as &$item) {
  58. $data = $this->setData($item, $callback);
  59. }
  60. return $list;
  61. }
  62. /**
  63. * 整理数据
  64. * @param Collection|static $info
  65. * @param callable|null $callback
  66. * @return mixed
  67. */
  68. protected function setData($info, callable $callback = null)
  69. {
  70. // 回调函数
  71. is_callable($callback) && call_user_func($callback, $info);
  72. return $info->hidden(array_merge($this->hidden, ['status']));
  73. }
  74. /**
  75. * 检索查询条件
  76. * @param array $params
  77. * @return \think\db\BaseQuery
  78. */
  79. private function getQueryFilter(array $params)
  80. {
  81. $filter = [];
  82. // 实例化新查询对象
  83. $query = $this->getNewQuery();
  84. // 浏览类型
  85. !empty($params['user_id']) && $filter[] = ['user_id', '=', "{$params['user_id']}"];
  86. // 浏览类型
  87. !empty($params['reply_user_id']) && $filter[] = ['reply_user_id', '=', "{$params['reply_user_id']}"];
  88. // 类型
  89. !empty($params['type']) && $filter[] = ['type', '=', "{$params['type']}"];
  90. // 实例化新查询对象
  91. return $query->where($filter)->where(function($query) use ($params){
  92. });
  93. }
  94. /**
  95. * 检索排序条件
  96. * @param array $param
  97. * @return array|string[]
  98. */
  99. private function setQuerySort(array $param = [])
  100. {
  101. $params = $this->setQueryDefaultValue($param, [
  102. 'sortType' => 'all', // 排序类型
  103. $this->name.'.create_time' => false, // 热门排序 (true高到低 false低到高)
  104. ]);
  105. // 排序规则
  106. $sort = [];
  107. if ($params['sortType'] === 'all') {
  108. $sort = [$this->name.'.create_time' => 'desc'];
  109. } elseif ($params['sortType'] === 'view') {
  110. $sort = [$this->name.'.id' => 'desc'];
  111. }
  112. return array_merge($sort, [$this->getPk() => 'desc']);
  113. }
  114. }