SupervisorsService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new SupervisorsModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['supervisors.mark' => 1];
  53. $status = isset($params['status']) ? $params['status'] : 0;
  54. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  55. if ($status > 0) {
  56. $where['supervisors.status'] = $status;
  57. }
  58. if ($isRecommend > 0) {
  59. $where['supervisors.is_recommend'] = $isRecommend;
  60. }
  61. $list = $this->model->with(['member'])
  62. ->from('supervisors')
  63. ->where($where)
  64. ->where(function ($query) use ($params) {
  65. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  66. if ($keyword) {
  67. $query->where('supervisors.name', 'like', "%{$keyword}%")
  68. ->orWhere('supervisors.mobile','like',"%{$keyword}%")
  69. ->orWhere('supervisors.occupation','like',"%{$keyword}%");
  70. }
  71. })
  72. ->select(['supervisors.*'])
  73. ->withCount(['consults'])
  74. ->orderBy('supervisors.sort', 'desc')
  75. ->orderBy('supervisors.id', 'desc')
  76. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  77. $list = $list ? $list->toArray() : [];
  78. if ($list) {
  79. foreach ($list['data'] as &$item) {
  80. $item['consults_count']=$item['guidance_count']?$item['guidance_count']:$item['consults_count'];
  81. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  82. }
  83. }
  84. return [
  85. 'pageSize' => $pageSize,
  86. 'total' => isset($list['total']) ? $list['total'] : 0,
  87. 'list' => isset($list['data']) ? $list['data'] : []
  88. ];
  89. }
  90. /**
  91. * 详情
  92. * @param $id
  93. * @param $userId
  94. * @return array|mixed
  95. */
  96. public function getInfo($id,$userId=0)
  97. {
  98. $cacheKey = "caches:supervisors:info_{$id}_{$userId}";
  99. $info = RedisService::get($cacheKey);
  100. if($info){
  101. return $info;
  102. }
  103. $where = ['id'=> $id,'mark'=>1];
  104. $info = $this->model->with(['member'])->where($where)->withCount(['consults'])->first();
  105. $info = $info? $info->toArray() : [];
  106. if($info){
  107. RedisService::set($cacheKey, $info, rand(5, 10));
  108. }
  109. return $info;
  110. }
  111. /**
  112. * 获取推荐数据
  113. * @return array|mixed
  114. */
  115. public function getRecommendList($limit=0)
  116. {
  117. $limit = $limit?$limit:ConfigService::make()->getConfigByCode('supervisors_recommend_num',6);
  118. $cacheKey = "caches:supervisors:recommendList";
  119. $datas = RedisService::get($cacheKey);
  120. if($datas){
  121. return $datas;
  122. }
  123. $datas = $this->model->with(['member'])
  124. ->where(['is_recommend'=> 1,'status'=>1,'mark'=>1])
  125. ->withCount(['consults'])
  126. ->limit($limit)
  127. ->get();
  128. $datas = $datas? $datas->toArray() : [];
  129. if($datas){
  130. RedisService::set($cacheKey, $datas, rand(300,600));
  131. }
  132. return $datas;
  133. }
  134. }