|
@@ -17,6 +17,7 @@ use App\Model\SysMenuButton;
|
|
|
use App\Model\Team;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
+use Mockery\Exception;
|
|
|
|
|
|
/**
|
|
|
* 人员相关
|
|
@@ -28,48 +29,122 @@ class EmployeeService extends Service
|
|
|
list($status,$msg) = $this->employeeRule($data,false);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
|
|
|
- $model = new Employee();
|
|
|
- $model = $model->where('id',$data['id'])->first();
|
|
|
- $model->id_card = $data['id_card']??'';
|
|
|
- $model->number = $data['number'] ;
|
|
|
- $model->mobile = $data['mobile'];
|
|
|
- $model->emp_name = $data['emp_name'];
|
|
|
- $model->is_admin = $data['is_admin'];
|
|
|
- if($model->is_admin == 1){
|
|
|
- $model->account = $data['account'];
|
|
|
- if($data['password'] !== '********'){
|
|
|
- $model->password = Hash::make($data['password']);
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $model = new Employee();
|
|
|
+ $model = $model->where('id',$data['id'])->first();
|
|
|
+ $model->number = $data['number'];
|
|
|
+ $model->emp_name = $data['emp_name'];
|
|
|
+ $model->mobile = $data['mobile'] ?? '';
|
|
|
+ $model->leave_time = $data['leave_time'] ?? '';
|
|
|
+ $model->entry_time = $data['entry_time'] ?? '';
|
|
|
+ $model->is_admin = $data['is_admin'];
|
|
|
+ if($model->is_admin == 1){
|
|
|
+ $model->account = $data['number'];
|
|
|
+ if($data['password'] !== '********'){
|
|
|
+ $model->password = Hash::make($data['password']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
|
|
|
+ if(isset($data['depart'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['depart'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'employee_id' => $model->id,
|
|
|
+ 'depart_id' => $value,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EmployeeDepartPermission::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ EmployeeRole::where('employee_id',$data['id'])->update([
|
|
|
+ 'del_time' => time()
|
|
|
+ ]);
|
|
|
+ if(isset($data['role'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['role'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'employee_id' => $model->id,
|
|
|
+ 'role_id' => $value,
|
|
|
+ 'crt_time' => time(),
|
|
|
+ 'upd_time' => time(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EmployeeRole::insert($insert);
|
|
|
}
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
}
|
|
|
- $model->save();
|
|
|
- return [true,'保存成功!'];
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
}
|
|
|
|
|
|
public function employeeAdd($data,$user){
|
|
|
list($status,$msg) = $this->employeeRule($data);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
|
|
|
- $model = new Employee();
|
|
|
- $model->id_card = $data['id_card']??'';
|
|
|
- $model->number = $data['number'] ;
|
|
|
- $model->mobile = $data['mobile'];
|
|
|
- $model->emp_name = $data['emp_name'];
|
|
|
- $model->state = 1;
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->is_admin = $data['is_admin'];
|
|
|
- if($model->is_admin == 1){
|
|
|
- $model->account = $data['account'];
|
|
|
- $model->password = Hash::make($data['password']);
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ $model = new Employee();
|
|
|
+
|
|
|
+ $model->number = $data['number'];
|
|
|
+ $model->emp_name = $data['emp_name'];
|
|
|
+ $model->mobile = $data['mobile'] ?? '';
|
|
|
+ $model->leave_time = $data['leave_time'] ?? '';
|
|
|
+ $model->entry_time = $data['entry_time'] ?? '';
|
|
|
+ $model->state = 1;
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->is_admin = $data['is_admin'];
|
|
|
+ if($model->is_admin == 1){
|
|
|
+ $model->account = $data['number'];
|
|
|
+ if($data['password'] !== '********'){
|
|
|
+ $model->password = Hash::make($data['password']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ if(isset($data['depart'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['depart'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'employee_id' => $model->id,
|
|
|
+ 'depart_id' => $value,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EmployeeDepartPermission::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($data['role'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['role'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'employee_id' => $model->id,
|
|
|
+ 'role_id' => $value,
|
|
|
+ 'crt_time' => time(),
|
|
|
+ 'upd_time' => time(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EmployeeRole::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (Exception $e){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $e->getMessage()];
|
|
|
}
|
|
|
- $model->save();
|
|
|
|
|
|
- return [true,'保存成功!'];
|
|
|
+ return [true,''];
|
|
|
}
|
|
|
|
|
|
public function employeeDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
|
|
|
|
|
|
- Employee::where('id',$data['id'])->update([
|
|
|
+ Employee::whereIn('id',$data['id'])->update([
|
|
|
'del_time'=>time()
|
|
|
]);
|
|
|
|
|
@@ -78,16 +153,12 @@ class EmployeeService extends Service
|
|
|
|
|
|
public function employeeList($data,$user){
|
|
|
$model = Employee::where('del_time',0)
|
|
|
- ->select('number','mobile','emp_name','id','entry_time','leave_time','is_technical','is_admin','state')
|
|
|
+ ->select('number','mobile','emp_name','id','entry_time','leave_time','is_admin','state')
|
|
|
->orderBy('id','desc');
|
|
|
|
|
|
- if(! empty($data['depart_id'])) {
|
|
|
- $depart = Depart::where('del_time',0)
|
|
|
- ->select('id','parent_id')
|
|
|
- ->get()->toArray();
|
|
|
- $result = array_merge($this->getAllDescendants($depart,$data['depart_id']),[$data['depart_id']]);
|
|
|
+ if(! empty($data['depart'])) {
|
|
|
$employee_id = DB::table('employee_depart_permission')
|
|
|
- ->whereIn("depart_id", $result)
|
|
|
+ ->where("depart_id", $data['depart'])
|
|
|
->select("employee_id")
|
|
|
->get()->toArray();
|
|
|
$employee_id = array_column($employee_id,'employee_id');
|
|
@@ -136,38 +207,41 @@ class EmployeeService extends Service
|
|
|
->select('a.employee_id','b.title','b.id')
|
|
|
->join('depart as b','a.depart_id','=','b.id')
|
|
|
->whereIn("a.employee_id",array_column($data['data'],'id'))
|
|
|
+ ->orderBy('b.id')
|
|
|
->get()->toArray();
|
|
|
- $map = array_column($res,null,'employee_id');
|
|
|
+ $depart_title = $depart_id = [];
|
|
|
+ foreach ($res as $value){
|
|
|
+ if(isset($depart_title[$value->employee_id])){
|
|
|
+ $depart_title[$value->employee_id] .= ',' . $value->title;
|
|
|
+ }else{
|
|
|
+ $depart_title[$value->employee_id] = $value->title;
|
|
|
+ }
|
|
|
+ $depart_id[$value->employee_id][] = $value->id;
|
|
|
+ }
|
|
|
|
|
|
foreach ($data['data'] as $key => $value){
|
|
|
$data['data'][$key]['role'] = $role2[$value['id']] ?? [];
|
|
|
$data['data'][$key]['role_name'] = $role[$value['id']] ?? '';
|
|
|
- $data['data'][$key]['depart_id'] = $map[$value['id']]->id ?? '';
|
|
|
- $data['data'][$key]['depart_title'] = $map[$value['id']]->title ?? '';
|
|
|
+ $data['data'][$key]['depart'] = $depart_id[$value['id']] ?? [];
|
|
|
+ $data['data'][$key]['depart_title'] = $depart_title[$value['id']] ?? '';
|
|
|
}
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
public function employeeRule($data,$is_add = true){
|
|
|
if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
|
|
|
- if($this->isEmpty($data,'mobile')) return [false,'手机号不存在!'];
|
|
|
if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
|
|
|
+
|
|
|
if(! $is_add){
|
|
|
if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
|
|
|
$bool = Employee::where('number',$data['number'])
|
|
|
->where('id','<>',$data['id'])
|
|
|
->where('del_time',0)->exists();
|
|
|
- $bool_account = Employee::where('account',$data['account'])
|
|
|
- ->where('id','<>',$data['id'])
|
|
|
- ->where('del_time',0)->exists();
|
|
|
}else{
|
|
|
$bool = Employee::where('number',$data['number'])
|
|
|
->where('del_time',0)->exists();
|
|
|
- $bool_account = Employee::where('account',$data['account'])
|
|
|
- ->where('del_time',0)->exists();
|
|
|
}
|
|
|
if($bool) return [false,'工号已存在!'];
|
|
|
- if($bool_account) return [false,'账号已存在!'];
|
|
|
|
|
|
return [true,''];
|
|
|
}
|
|
@@ -212,8 +286,10 @@ class EmployeeService extends Service
|
|
|
|
|
|
public function roleList($data){
|
|
|
$list = Role::where('del_time',0)
|
|
|
- ->select('title','title','crt_time','id','upd_time')
|
|
|
+ ->select('title','crt_time','id','upd_time')
|
|
|
->orderBy('id','desc');
|
|
|
+ if(! empty($data['title'])) $list->where('title', 'LIKE', '%' . $data['title'] . '%');
|
|
|
+
|
|
|
$list = $this->limit($list,'',$data);
|
|
|
|
|
|
return [200,$list];
|
|
@@ -293,7 +369,7 @@ class EmployeeService extends Service
|
|
|
->where('del_time',0)
|
|
|
->select('menu_id','type')
|
|
|
->get()->toArray();
|
|
|
- $button = $this->fillRoleButton($data['role_id']);
|
|
|
+ $button = $this->fillRoleButton([$data['role_id']]);
|
|
|
foreach ($menu as $key => $value){
|
|
|
$menu[$key]['button'] = $button[$value['menu_id']] ?? [];
|
|
|
}
|
|
@@ -345,23 +421,24 @@ class EmployeeService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
- public function departList($data){
|
|
|
+ public function departList($data,$user){
|
|
|
$model = Depart::where('del_time',0)
|
|
|
- ->select('title','id','code','parent_id','is_use')
|
|
|
+ ->select('title','id','code','parent_id','is_main')
|
|
|
->orderby('code', 'asc');
|
|
|
if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
|
|
|
|
|
|
$list = $model->get()->toArray();
|
|
|
- if(! empty($list)) {
|
|
|
- $list = $this->makeTree(0,$list);
|
|
|
- $list = $this->set_sort_circle($list);
|
|
|
+ $list_tree = $list;
|
|
|
+ if(! empty($list_tree)) {
|
|
|
+ $list_tree = $this->makeTree(0,$list_tree);
|
|
|
+ $list_tree = $this->set_sort_circle($list_tree);
|
|
|
}
|
|
|
|
|
|
- return [200,$list];
|
|
|
+ return [200,['data' => $list,'tree' => $list_tree]];
|
|
|
}
|
|
|
|
|
|
- public function departRule($data, $is_check){
|
|
|
+ public function departRule($data, $is_check = true){
|
|
|
if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
|
|
|
|
|
|
$code = array_column($data['data'],'code');
|
|
@@ -383,10 +460,6 @@ class EmployeeService extends Service
|
|
|
if($title_count[$value] > 1) return [false,'名称不能重复'];
|
|
|
}
|
|
|
|
|
|
- $depart_id = array_filter(array_column($data['data'],'parent_id'));
|
|
|
- $res = $this->checkDepartHasPerson($depart_id);
|
|
|
- if($res) return [false,'部门下已有人员,不能新建子部门!'];
|
|
|
-
|
|
|
foreach ($data['data'] as $key => $value){
|
|
|
if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
|
|
|
|
|
@@ -398,11 +471,6 @@ class EmployeeService extends Service
|
|
|
->exists();
|
|
|
}else{
|
|
|
if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
|
|
|
-
|
|
|
- if(! $value['is_use']) {
|
|
|
- $bool_is = $this->checkDepartHasPerson([$data['id']]);
|
|
|
- if($bool_is) return [false,'部门下已经有人员,停用失败!'];
|
|
|
- }
|
|
|
$bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
|
|
|
->where('id','<>',$data['id'])
|
|
|
->where('del_time',0)
|
|
@@ -626,7 +694,7 @@ class EmployeeService extends Service
|
|
|
->get()->toArray();
|
|
|
|
|
|
//组织
|
|
|
- $role_id = array_column($role,'role_id');
|
|
|
+ $role_id = array_unique(array_column($role,'role_id'));
|
|
|
asort($role_id);
|
|
|
$role_id = array_values($role_id);
|
|
|
|
|
@@ -662,18 +730,23 @@ class EmployeeService extends Service
|
|
|
->select('menu_id','type')
|
|
|
->get()->toArray();
|
|
|
$button = $this->fillRoleButton($role_id);
|
|
|
+ $tmp = [];
|
|
|
foreach ($search as $value){
|
|
|
- $object[] = [
|
|
|
- 'menu_id' => $value['menu_id'],
|
|
|
- 'type' => $value['type'],
|
|
|
- 'button' => $button[$value['menu_id']] ?? [],
|
|
|
- ];
|
|
|
+ if(! in_array($value['menu_id'],$tmp)){
|
|
|
+ $object[] = [
|
|
|
+ 'menu_id' => $value['menu_id'],
|
|
|
+ 'type' => $value['type'],
|
|
|
+ 'button' => $button[$value['menu_id']] ?? [],
|
|
|
+ ];
|
|
|
+ $tmp[] = $value['menu_id'];
|
|
|
+ }
|
|
|
}
|
|
|
+ unset($tmp);
|
|
|
|
|
|
return $object;
|
|
|
}
|
|
|
|
|
|
- //获取登录账号的权限部门
|
|
|
+ //获取登录账号的权限部门 暂时不用了
|
|
|
public static function getPersonDepart($employee_id){
|
|
|
if(empty($employee_id)) return [];
|
|
|
|
|
@@ -723,16 +796,51 @@ class EmployeeService extends Service
|
|
|
return [true,''];
|
|
|
}
|
|
|
|
|
|
+ //填充角色下的按钮
|
|
|
public function fillRoleButton($role_id){
|
|
|
- $button = RoleMenuButton::where('role_id',$role_id)
|
|
|
+ $button = RoleMenuButton::whereIn('role_id',$role_id)
|
|
|
->where('del_time',0)
|
|
|
->select('menu_id','button_id')
|
|
|
->get()->toArray();
|
|
|
$button_map = [];
|
|
|
foreach ($button as $value){
|
|
|
- $button_map[$value['menu_id']][] = $value['button_id'];
|
|
|
+ if(! isset($button_map[$value['menu_id']])){
|
|
|
+ $button_map[$value['menu_id']][] = $value['button_id'];
|
|
|
+ }else{
|
|
|
+ if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return $button_map;
|
|
|
}
|
|
|
+
|
|
|
+ //获取登录账号的部门
|
|
|
+ public static function getLoginDepart($employee_id){
|
|
|
+ $is_main = 0;//是否总社
|
|
|
+ if(empty($employee_id)) return ['', $is_main];
|
|
|
+
|
|
|
+ //admin账号
|
|
|
+ if($employee_id == Employee::SPECIAL_ADMIN) {
|
|
|
+ $is_main = 1;
|
|
|
+ return [Depart::RULE_DEPART, $is_main];
|
|
|
+ }
|
|
|
+
|
|
|
+ //自己绑定的部门 启用的部门
|
|
|
+ $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
|
|
|
+ ->join('depart as b','b.id','a.depart_id')
|
|
|
+ ->where('a.employee_id',$employee_id)
|
|
|
+ ->where('b.is_use',Depart::IS_UES)
|
|
|
+ ->select('a.depart_id','b.is_main')
|
|
|
+ ->get()->toArray();
|
|
|
+ if(! empty($depart)){
|
|
|
+ foreach ($depart as $value){
|
|
|
+ if($value['is_main'] > 0) {
|
|
|
+ $is_main = 1;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$depart, $is_main];
|
|
|
+ }
|
|
|
}
|