CmsUserDepartmentModel.php
4.43 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace App\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserDepartmentModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_department';
public $timestamps = false;
public static function getDepartmentIdsParrentIds($parrent_ids = [])
{
$res = self::whereIn('parent_id', $parrent_ids)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
// 根据父ID获取指定的部门
public static function getDepartmentByParentId($parent_id)
{
if (is_array($parent_id)) {
$list = self::whereIn('parent_id', $parent_id);
} else {
$list = self::where('parent_id', $parent_id);
}
return $list->pluck('department_name', 'department_id')->toArray();
}
public static function getDepartmentInfo($department_id)
{
$res = self::where('department_id', $department_id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentListByDepartmentIdArr($departmentIdArr)
{
$res = self::whereIn('department_id', $departmentIdArr)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentList($departmentName, $departmentIds)
{
$res = self::select('department_name')->where('department_name', 'like',
'%' . $departmentName . '%')->whereIn('department_id', $departmentIds)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentListByName($departmentName)
{
$res = self::select('department_name')->distinct('department_name')->where('department_name', 'like',
"%{$departmentName}%")->orderByDesc('department_id')->pluck('department_name');
return ($res) ? $res->toArray() : [];
}
// 根据部门名称获取部门ID
public static function getDepartmentIdByName($department_name)
{
return self::where('department_name', $department_name)->value('department_id');
}
public static function getDepartmentId($department)
{
$res = self::select('department_id')->distinct('department_name')->where('department_name',
$department)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
// 获取部门人员
public static function getUserByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
self::getSubDepartmentId($departmentId, $departmentIds);
return CmsUserInfoModel::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 static function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = self::where('parent_id', $departmentId)->select('department_id',
'department_name')->get();
if ($sub_department) {
foreach ($sub_department as $v) {
self::getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
/*
* 获取上级部门,只需要往上找一层
* 比如:线上一组=》线上销售部
*/
public static function getParentDepartment($dep_id)
{
$parent_id = self::where("department_id", intval($dep_id))->value("parent_id");
if(!$parent_id){
return [];
}
$parentInfo = self::where("department_id",$parent_id)->select("department_id", "department_name")->first();
return $parentInfo ? $parentInfo->toArray():[];
}
// 获取下级部门
public static function getSubDepartmentById($department_id)
{
return self::where('parent_id', $department_id)->select('department_id', 'department_name')->get()->toArray();
}
// 获取顶级部门名称
public static function getTopDepartmentNameById($department_id)
{
return self::where('department_id', $department_id)->where('parent_id', 0)->value('department_name');
}
}