DepartmentService.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/3/31
  6. * Time: 18:51
  7. */
  8. namespace App\Services;
  9. use App\Models\Department;
  10. use App\Models\User;
  11. use App\Traits\Singleton;
  12. use Illuminate\Support\Facades\Redis;
  13. class DepartmentService
  14. {
  15. use Singleton;
  16. const DEPARTMENTS = "bs_departments";
  17. /**
  18. * 获得所有部门信息
  19. * @return mixed
  20. */
  21. public function all($activityId){
  22. $departments = Department::where("activity_id",$activityId)->where("status",1)->pluck("department_name","department_id");
  23. //$departments = collect($departments)->toArray();
  24. return $departments;
  25. }
  26. /**
  27. * 获得所有部门的总人数
  28. * @return mixed
  29. */
  30. public function allPcount($activityId){
  31. $departments = Department::where("activity_id",$activityId)->where("status",1)->pluck("user_num","department_id");
  32. return collect($departments)->toArray();
  33. }
  34. /**
  35. * 获取部门人员Id
  36. * @param $departmentId
  37. * @return mixed
  38. */
  39. public function getUserIdsByDepartmentId($departmentId){
  40. $userIds = User::where("department_id",$departmentId)->pluck("user_id");
  41. return $userIds;
  42. }
  43. /**
  44. * 获得部门名称
  45. * @param $departmentId
  46. * @return string
  47. */
  48. public function getDepartmentName($departmentId){
  49. $departmentName = "";
  50. if(!empty($departmentId)){
  51. $departmentName = Redis::get(self::DEPARTMENTS.":".$departmentId);
  52. if(empty($departmentName)){
  53. $department = Department::find($departmentId);
  54. if($department){
  55. $departmentName = $department->department_name;
  56. Redis::set(self::DEPARTMENTS.":".$departmentId,$departmentName);
  57. Redis::expire(self::DEPARTMENTS.":".$departmentId,600);
  58. }
  59. }
  60. }
  61. return $departmentName;
  62. }
  63. }