SupervisorsService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\SupervisorsModel;
  13. use App\Services\BaseService;
  14. use App\Services\ConfigService;
  15. use App\Services\RedisService;
  16. /**
  17. * 导师管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class SupervisorsService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new SupervisorsModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * @param $params
  46. * @param int $pageSize
  47. * @return array
  48. */
  49. public function getDataList($params, $pageSize = 15)
  50. {
  51. $where = ['supervisors.mark' => 1];
  52. $status = isset($params['status']) ? $params['status'] : 0;
  53. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  54. if ($status > 0) {
  55. $where['supervisors.status'] = $status;
  56. }
  57. if ($isRecommend > 0) {
  58. $where['supervisors.is_recommend'] = $isRecommend;
  59. }
  60. $list = $this->model->with(['member'])
  61. ->from('supervisors')
  62. ->where($where)
  63. ->where(function ($query) use ($params) {
  64. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  65. if ($keyword) {
  66. $query->where('supervisors.name', 'like', "%{$keyword}%")
  67. ->orWhere('supervisors.mobile','like',"%{$keyword}%");
  68. }
  69. })
  70. ->select(['supervisors.*'])
  71. ->withCount(['consults'])
  72. ->orderBy('supervisors.sort', 'desc')
  73. ->orderBy('supervisors.id', 'desc')
  74. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  75. $list = $list ? $list->toArray() : [];
  76. if ($list) {
  77. foreach ($list['data'] as &$item) {
  78. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  79. }
  80. }
  81. return [
  82. 'pageSize' => $pageSize,
  83. 'total' => isset($list['total']) ? $list['total'] : 0,
  84. 'list' => isset($list['data']) ? $list['data'] : []
  85. ];
  86. }
  87. /**
  88. * 详情
  89. * @param $id
  90. * @param $userId
  91. * @return array|mixed
  92. */
  93. public function getInfo($id,$userId=0)
  94. {
  95. $cacheKey = "caches:supervisors:info_{$id}_{$userId}";
  96. $info = RedisService::get($cacheKey);
  97. if($info){
  98. return $info;
  99. }
  100. $where = ['id'=> $id,'mark'=>1];
  101. $info = $this->model->with(['member'])->where($where)->withCount(['consults'])->first();
  102. $info = $info? $info->toArray() : [];
  103. if($info){
  104. RedisService::set($cacheKey, $info, rand(5, 10));
  105. }
  106. return $info;
  107. }
  108. /**
  109. * 获取推荐数据
  110. * @return array|mixed
  111. */
  112. public function getRecommendList($limit=0)
  113. {
  114. $limit = $limit?$limit:ConfigService::make()->getConfigByCode('supervisors_recommend_num',6);
  115. $cacheKey = "caches:supervisors:recommendList";
  116. $datas = RedisService::get($cacheKey);
  117. if($datas){
  118. return $datas;
  119. }
  120. $datas = $this->model->with(['member'])
  121. ->where(['is_recommend'=> 1,'status'=>1,'mark'=>1])
  122. ->withCount(['consults'])
  123. ->limit($limit)
  124. ->get();
  125. $datas = $datas? $datas->toArray() : [];
  126. if($datas){
  127. RedisService::set($cacheKey, $datas, rand(300,600));
  128. }
  129. return $datas;
  130. }
  131. }