DepartmentMemberController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\admin\controllers;
  11. use app\backend\modules\member\models\Member;
  12. use business\common\models\Department;
  13. use business\common\models\DepartmentStaff;
  14. use business\common\models\Staff;
  15. use Exception;
  16. use Illuminate\Support\Facades\DB;
  17. use Yunshop\WorkWechat\common\models\Employee;
  18. use business\common\services\SettingService;
  19. class DepartmentMemberController extends \business\common\controllers\components\BaseController
  20. {
  21. public function getDepartmentMember(Department $department)
  22. {
  23. $data = $department->getDepartmentMember();
  24. $res = $this->recursion($data);
  25. return $this->successJson('ok',$res);
  26. }
  27. private function recursion($data, $parent_id = 0)
  28. {
  29. $res = [];
  30. foreach ($data as $item) {
  31. if ($item['parent_id'] == $parent_id) {
  32. $item['children'] = $this->recursion($data, $item['id']);
  33. if (empty($item['children'])) {
  34. unset($item['children']);
  35. }
  36. array_push($res, $item);
  37. }
  38. }
  39. return $res;
  40. }
  41. //弃用
  42. public function t()
  43. {
  44. $data = Department::uniacid()->where('business_id', SettingService::getBusinessId())
  45. ->with(['hasMayDepartmentStaff.hasOneStaff'])->get()->groupBy('level')->toArray();
  46. $data = $this->tt($data[1], 1, $data);
  47. return $this->successJson('ok', $data);
  48. }
  49. // 临时写成以数组方式,集合有点bug,只有一个部门返回给前端不是数组而是对象
  50. public function tt(array $list, $level = 1, $lists)
  51. {
  52. foreach ($list as &$item) {
  53. $item['type'] = "department";
  54. // 获取该部门的员工
  55. $staff_ids = DepartmentStaff::uniacid()
  56. ->where('business_id', SettingService::getBusinessId())
  57. ->where('department_id', $item['id'])->pluck('staff_id');
  58. $staff_data = Staff::uniacid()->whereIn('id', $staff_ids)->get()->toArray();
  59. $staff = [];
  60. foreach ($staff_data as $key => $items) {
  61. $staff[$key] = $items;
  62. $staff[$key]['type'] = "staff";
  63. }
  64. if ($staff) {
  65. $item['staff'] = $staff;//array_values(array_unique($staff));
  66. } else {
  67. $item['staff'] = [];
  68. }
  69. // 获取子部门
  70. if ($c = $lists[$level + 1]) {
  71. $c = collect($c);
  72. $children = $c->where('parent_id', $item['id'])->all();
  73. if (!$children) {
  74. $item['children'] = [];
  75. } else {
  76. $children = $this->recursion($children, $level + 1, $lists);
  77. $item['children'] = $children[0] ? $children : array_merge([],$children);
  78. }
  79. } else {
  80. $item['children'] = [];
  81. }
  82. }
  83. return $list;
  84. }
  85. }