DepartmentService.php
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace App\Http\Services;
use App\Model\BrandModel;
use App\Model\DepartmentModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\UserInfoModel;
use Illuminate\Support\Facades\DB;
//部门数据服务
class DepartmentService
{
public function getSubordinateUserIds($adminId)
{
$departmentId = DB::table('user_info')->where('userId', $adminId)->value('department_id');
$users = $this->getUserByDepartmentId($departmentId);
return array_column($users, 'userId');
}
public function getSubordinateUserNames($adminId)
{
$departmentId = DB::table('user_info')->where('userId', $adminId)->value('department_id');
$users = $this->getUserByDepartmentId($departmentId);
return array_column($users, 'name');
}
// 获取部门人员
public function getUserByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
$this->getSubDepartmentId($departmentId, $departmentIds);
return DB::table('user_info')->whereIn('department_id', $departmentIds)
->where(function ($query) use ($status) {
if ($status !== '') {
$query->where('status', '=', $status);
}
})
->where(function ($query) use ($filter) {
if ($filter) {
$query->whereRaw($filter);
}
})
->select('userId', 'name', 'status')
->get();
}
// 获取下级部门ID
public function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = DB::table('user_department')->where('parent_id', $departmentId)->select('department_id',
'department_name')->get();
if ($sub_department) {
foreach ($sub_department as $v) {
$this->getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
//根据用户id获取上一层部门信息(专门为共用申请设置的方法)
/*
针对共用申请:
不同部门的情况还不同,因为线上的数据错乱了,不同部门的分级有问题
比如运营老大被分在了顶级部门(运营部),联营采购老大和自营采购老大被分在了二级部门
但是联营采购老大的部下,在三级部门,而且自营采购老大的部下,在二级部门,和自己同一个部门
但是目前用这个供应商系统的,只会有这三个部门,所以要找出一个合理的处理方法
所以这边的方法如下 :
运营和联营采购无脑找上一级.
因为运营老大在顶级,找不到上一级,那就取顶级即可
联营采购本来向上找一级就是对的(除了被查找的人是联营采购的老大....那就不要往上找了)
自营采购比较特殊,因为领导和部下都在这一个层级,所以不能往上找,往上找就是顶级的采购部了,顶级采购部是找不到人的(没有用户的部门id是顶级采购部id)
以上所说的情况,都是建立在部门架构不变以及部门老大维持层级不变的前提下进行的
*/
public function getUpperDepartmentByUserIdForShareApply($userId)
{
$userModel = new UserInfoModel();
$departmentId = $userModel->where('userId', $userId)->value('department_id');
if (empty($departmentId)) {
return [];
}
$departmentModel = new DepartmentModel();
//先判断是不是自营采购部门id
if ($departmentId == config('field.ZiYingDepartmentId')) {
return $departmentModel->where('department_id', $departmentId)->first()->toArray();
} else {
$department = $departmentModel->where('department_id', $departmentId)->first();
//如果当前的部门id已经是联营采购部门Id(二级,目前有联营一部和联营二部)
if (in_array($departmentId, config('field.LiangYingDepartmentIds'))) {
return $department->toArray();
} else {
if (!empty($department)) {
$department = $department->toArray();
//找出自己的parent_id,如果是0代表自己就是顶级部门信息了
if ($department['parent_id'] == 0) {
return $department;
}
$department = $departmentModel->where('department_id',
$department['parent_id'])->first()->toArray();
return $department;
}
}
}
return [];
}
//根据用户名获取部门名称
public function getDepartmentNameByUserName($userName)
{
$departmentId = UserInfoModel::where('name', $userName)->value('department_id');
$departmentName = DepartmentModel::where('department_id', $departmentId)->value('department_name');
return $departmentName;
}
}