SupervisorsService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\SupervisorsConsultRecordsModel;
  13. use App\Models\SupervisorsModel;
  14. use App\Services\BaseService;
  15. use App\Services\ConfigService;
  16. use App\Services\RedisService;
  17. /**
  18. * 导师管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class SupervisorsService extends BaseService
  24. {
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new SupervisorsModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $where = ['supervisors.mark' => 1];
  54. $status = isset($params['status']) ? $params['status'] : 0;
  55. $type = isset($params['type']) ? $params['type'] : 0;
  56. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  57. if ($status > 0) {
  58. $where['supervisors.status'] = $status;
  59. }
  60. if ($isRecommend > 0) {
  61. $where['supervisors.is_recommend'] = $isRecommend;
  62. }
  63. if ($type > 0) {
  64. $where['supervisors.type'] = $type;
  65. }
  66. $list = $this->model->with(['member'])
  67. ->from('supervisors')
  68. ->where($where)
  69. ->where(function ($query) use ($params) {
  70. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  71. if ($keyword) {
  72. $query->where('supervisors.name', 'like', "%{$keyword}%")
  73. ->orWhere('supervisors.mobile','like',"%{$keyword}%")
  74. ->orWhere('supervisors.occupation','like',"%{$keyword}%");
  75. }
  76. })
  77. ->select(['supervisors.*'])
  78. ->withCount(['consults'])
  79. ->orderBy('supervisors.sort', 'desc')
  80. ->orderBy('supervisors.id', 'desc')
  81. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  82. $list = $list ? $list->toArray() : [];
  83. if ($list) {
  84. foreach ($list['data'] as &$item) {
  85. $item['consults_count']=$item['guidance_count']?$item['guidance_count']:$item['consults_count'];
  86. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  87. }
  88. }
  89. return [
  90. 'pageSize' => $pageSize,
  91. 'total' => isset($list['total']) ? $list['total'] : 0,
  92. 'list' => isset($list['data']) ? $list['data'] : []
  93. ];
  94. }
  95. /**
  96. * 详情
  97. * @param $id
  98. * @param $userId
  99. * @return array|mixed
  100. */
  101. public function getInfo($id,$userId=0)
  102. {
  103. $cacheKey = "caches:supervisors:info_{$id}_{$userId}";
  104. $info = RedisService::get($cacheKey);
  105. if($info){
  106. return $info;
  107. }
  108. $where = ['id'=> $id,'mark'=>1];
  109. $info = $this->model->with(['member'])->where($where)->withCount(['consults'])->first();
  110. $info = $info? $info->toArray() : [];
  111. if($info){
  112. RedisService::set($cacheKey, $info, rand(5, 10));
  113. }
  114. return $info;
  115. }
  116. /**
  117. * 获取推荐数据
  118. * @return array|mixed
  119. */
  120. public function getRecommendList($limit=0)
  121. {
  122. $limit = $limit?$limit:ConfigService::make()->getConfigByCode('supervisors_recommend_num',6);
  123. $cacheKey = "caches:supervisors:recommendList";
  124. $datas = RedisService::get($cacheKey);
  125. if($datas){
  126. return $datas;
  127. }
  128. $datas = $this->model->with(['member'])
  129. ->where(['is_recommend'=> 1,'status'=>1,'mark'=>1])
  130. ->withCount(['consults'])
  131. ->limit($limit)
  132. ->get();
  133. $datas = $datas? $datas->toArray() : [];
  134. if($datas){
  135. RedisService::set($cacheKey, $datas, rand(300,600));
  136. }
  137. return $datas;
  138. }
  139. /**
  140. * 咨询信息提交
  141. * @param $userId
  142. * @param $params
  143. * @return array|false
  144. */
  145. public function consultSubmit($userId, $params)
  146. {
  147. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  148. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  149. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  150. $industry = isset($params['industry']) ? trim($params['industry']) : '';
  151. $position = isset($params['position']) ? trim($params['position']) : '';
  152. if (empty($realname)) {
  153. $this->error = '请填写姓名';
  154. return false;
  155. }
  156. if (empty($mobile)) {
  157. $this->error = '请填写联系方式';
  158. return false;
  159. }
  160. if (empty($industry)) {
  161. $this->error = '请填写所在行业';
  162. return false;
  163. }
  164. if (empty($position)) {
  165. $this->error = '请填写当前角色/职务';
  166. return false;
  167. }
  168. $cacheKey = "caches:supervisors:consult:{$userId}_{$sourceId}";
  169. if (RedisService::get($cacheKey)) {
  170. $this->error = '您近期已经提交过,请30秒后重试';
  171. return false;
  172. }
  173. $sourceInfo = $this->model->where(['id'=>$sourceId,'mark'=>1])->first();
  174. if(empty($sourceInfo)){
  175. RedisService::clear($cacheKey);
  176. $this->error = '提交失败,咨询导师不存在';
  177. return false;
  178. }
  179. $data = [
  180. 'user_id' => $userId,
  181. 'source_id' => $sourceId,
  182. 'realname' => $realname,
  183. 'mobile' => $mobile,
  184. 'industry' => $industry,
  185. 'position' => $position,
  186. 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '',
  187. 'interest' => isset($params['interest'])? trim($params['interest']) : '',
  188. 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '',
  189. 'create_time' => time(),
  190. 'update_time' => time(),
  191. 'status' => 1,
  192. 'mark' => 1,
  193. ];
  194. if (!$id = SupervisorsConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) {
  195. if(!$id = SupervisorsConsultRecordsModel::insertGetId($data)){
  196. RedisService::clear($cacheKey);
  197. $this->error = '提交失败';
  198. return false;
  199. }
  200. }else{
  201. SupervisorsConsultRecordsModel::where(['id'=>$id])->update($data);
  202. }
  203. $this->error = '提交成功';
  204. RedisService::set($cacheKey, $data, 30);
  205. return ['id' => $id];
  206. }
  207. }