Department.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\common\models;
  11. use app\common\models\BaseModel;
  12. use business\common\services\SettingService;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Database\Eloquent\SoftDeletes;
  15. class Department extends BaseModel
  16. {
  17. public $table = 'yz_business_department';
  18. public $timestamps = true;
  19. protected $guarded = [];
  20. protected $appends = [];
  21. protected $casts = [];
  22. use SoftDeletes;
  23. public function business($business_id = 0)
  24. {
  25. $business_id = $business_id ?: SettingService::getBusinessId();
  26. return self::uniacid()->where('business_id', $business_id);
  27. }
  28. /*
  29. * 获取当前部门的所有上级部门ID
  30. */
  31. public function getAllDepartmentParentId($department_id_arr, $department_list = [], $business_id = 0)
  32. {
  33. $business_id = $business_id ?: SettingService::getBusinessId();
  34. $department_list = $department_list ?: self::where('business_id', $business_id)->get();
  35. $this_parent_id_arr = $department_list->whereIn('id', $department_id_arr)->pluck('parent_id')->toArray();
  36. $parent_id_arr = $this_parent_id_arr = array_values(array_unique(array_filter($this_parent_id_arr)));
  37. if (!$parent_id_arr) {
  38. return [];
  39. }
  40. do {
  41. $this_parent_id_arr = $department_list->whereIn('id', $this_parent_id_arr)->pluck('parent_id')->toArray();
  42. $this_parent_id_arr = array_values(array_unique(array_filter($this_parent_id_arr)));
  43. $parent_id_arr = array_merge($parent_id_arr, $this_parent_id_arr);
  44. } while ($this_parent_id_arr);
  45. return array_values(array_unique(array_filter($parent_id_arr)));
  46. }
  47. /*
  48. * 获取当前部门的所有下级部门ID
  49. */
  50. public function getAllDepartmentSubId($department_id_arr, $department_list = [], $business_id = 0)
  51. {
  52. $business_id = $business_id ?: SettingService::getBusinessId();
  53. $department_list = $department_list ?: self::where('business_id', $business_id)->get();
  54. $this_sub_id_arr = $department_list->whereIn('parent_id', $department_id_arr)->pluck('id')->toArray();
  55. $sub_id_arr = $this_sub_id_arr = array_values(array_unique(array_filter($this_sub_id_arr)));
  56. if (!$sub_id_arr) {
  57. return [];
  58. }
  59. do {
  60. if ($this_sub_id_arr = $department_list->whereIn('parent_id', $this_sub_id_arr)->pluck('id')->toArray()) {
  61. $sub_id_arr = array_merge($sub_id_arr, $this_sub_id_arr);
  62. }
  63. } while ($this_sub_id_arr);
  64. return array_values(array_unique(array_filter($sub_id_arr)));
  65. }
  66. /**
  67. * 获取部门员工
  68. * @return void
  69. */
  70. public function getDepartmentMember()
  71. {
  72. /**
  73. * @var Department $department_mo
  74. */
  75. $department_mo = $this->business()->select(['id','name','parent_id']);
  76. $department_mo->with(['hasMayDepartmentStaff' => function($q){
  77. $q->select(['id','department_id','staff_id'])->with(['hasOneStaff' => function($q){
  78. $q->select(['id','name','user_id','uid', 'avatar'])->where('disabled', 0);
  79. }
  80. ])->groupBy('department_id')->groupBy('staff_id');
  81. }
  82. ]);
  83. return $department_mo->get()->toArray();
  84. }
  85. public function getWechatBusinessDepartment($business_id)
  86. {
  87. return self::where('business_id', $business_id)->where('wechat_department_id', '<>', 0)->get();
  88. }
  89. public function hasOneParentDepartment()
  90. {
  91. return $this->hasOne(self::class, 'id', 'parent_id');
  92. }
  93. public function hasManySubDepartment()
  94. {
  95. return $this->hasMany(self::class, 'parent_id', 'id');
  96. }
  97. public function hasMayDepartmentStaff()
  98. {
  99. return $this->hasMany(DepartmentStaff::class, 'department_id', 'id');
  100. }
  101. }