|
@@ -29,7 +29,7 @@ class EmployeeService extends Service
|
|
|
$model = $model->where('id',$data['id'])->first();
|
|
|
$model->id_card = $data['id_card']??'';
|
|
|
$model->number = $data['number'] ;
|
|
|
- $model->mobile = $data['mobile'];
|
|
|
+ $model->mobile = $data['mobile']??'';
|
|
|
$model->emp_name = $data['emp_name'];
|
|
|
$model->is_admin = $data['is_admin'];
|
|
|
if($model->is_admin == 1){
|
|
@@ -39,6 +39,19 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
}
|
|
|
$model->save();
|
|
|
+
|
|
|
+ $permisson_model = new EmployeeDepartPermission();
|
|
|
+ $permisson_model = $permisson_model->where('employee_id',$data['id'])->first();
|
|
|
+ if($permisson_model){
|
|
|
+ $permisson_model->depart_id = $data['depart_id'];
|
|
|
+ $permisson_model->save();
|
|
|
+ }else{
|
|
|
+ $permisson_model = new EmployeeDepartPermission();
|
|
|
+ $permisson_model->employee_id = $data['id'];
|
|
|
+ $permisson_model->depart_id = $data['depart_id'];
|
|
|
+ $permisson_model->save();
|
|
|
+ }
|
|
|
+
|
|
|
return [true,'保存成功!'];
|
|
|
}
|
|
|
|
|
@@ -49,7 +62,7 @@ class EmployeeService extends Service
|
|
|
$model = new Employee();
|
|
|
$model->id_card = $data['id_card']??'';
|
|
|
$model->number = $data['number'] ;
|
|
|
- $model->mobile = $data['mobile'];
|
|
|
+ $model->mobile = $data['mobile']??'';
|
|
|
$model->emp_name = $data['emp_name'];
|
|
|
$model->state = 1;
|
|
|
$model->crt_id = $user['id'];
|
|
@@ -60,6 +73,11 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
$model->save();
|
|
|
|
|
|
+ $permisson_model = new EmployeeDepartPermission();
|
|
|
+ $permisson_model->employee_id = $model->id;
|
|
|
+ $permisson_model->depart_id = $data['depart_id'];
|
|
|
+ $permisson_model->save();
|
|
|
+
|
|
|
return [true,'保存成功!'];
|
|
|
}
|
|
|
|
|
@@ -74,16 +92,59 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
|
|
|
public function employeeList($data){
|
|
|
- $list = Employee::where('del_time',0)->select('id_card','emp_name','mobile','crt_time','account','is_admin','upd_time','id')->orderBy('id','desc');
|
|
|
+ $model = Employee::where('del_time',0)
|
|
|
+ ->select('id_card','emp_name','mobile','crt_time','account','is_admin','upd_time','id','number')
|
|
|
+ ->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']]);
|
|
|
+ $employee_id = DB::table('employee_depart_permission')
|
|
|
+ ->whereIn("depart_id", $result)
|
|
|
+ ->select("employee_id")
|
|
|
+ ->get()->toArray();
|
|
|
+ $employee_id = array_column($employee_id,'employee_id');
|
|
|
+ $model->whereIn("id", $employee_id);
|
|
|
+ }
|
|
|
+ if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
|
|
|
+ if(! empty($data['emp_name'])) $model->where('emp_name', 'LIKE', '%'.$data['emp_name'].'%');
|
|
|
|
|
|
- $list = $this->limit($list,'',$data);
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+
|
|
|
+ //组织数据
|
|
|
+ $list = $this->organizationEmployeeData($list);
|
|
|
|
|
|
return [200,$list];
|
|
|
}
|
|
|
|
|
|
+ public function organizationEmployeeData($data) {
|
|
|
+ if (empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $res = DB::table('employee_depart_permission as a')
|
|
|
+ ->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'))
|
|
|
+ ->get()->toArray();
|
|
|
+ $map = array_column($res,null,'employee_id');
|
|
|
+
|
|
|
+ $res = DB::table('employee_team_permission as a')
|
|
|
+ ->select('a.employee_id','b.title','b.id')
|
|
|
+ ->join('team as b','a.team_id','=','b.id')
|
|
|
+ ->whereIn("a.employee_id",array_column($data['data'],'id'))
|
|
|
+ ->get()->toArray();
|
|
|
+ $map2 = array_column($res,null,'employee_id');
|
|
|
+
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['depart_id'] = $map[$value['id']]->id ?? '';
|
|
|
+ $data['data'][$key]['depart_title'] = $map[$value['id']]->title ?? '';
|
|
|
+ }
|
|
|
+ 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不能为空!'];
|
|
@@ -164,45 +225,31 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
|
|
|
public function departEdit($data){
|
|
|
- list($status,$msg) = $this->departRule($data);
|
|
|
+ list($status,$msg) = $this->departRule($data,false);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
- $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
+
|
|
|
+ $update = $msg['data'][0];
|
|
|
|
|
|
$model = new Depart();
|
|
|
- $model = $model->where('id',$data['id'])->first();
|
|
|
+ $model->where('id',$data['id'])->update($update);
|
|
|
|
|
|
- $model->title = $data['title'];
|
|
|
- $model->code = $data['code']??'';
|
|
|
- $model->save();
|
|
|
return [true,'保存成功!'];
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- public function departAdd($data,$user){
|
|
|
-
|
|
|
-
|
|
|
-// if($this->isEmpty($data,'title')) return [201,'名称不存在!'];
|
|
|
+ public function departAdd($data){
|
|
|
list($status,$msg) = $this->departRule($data);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
- $first = Depart::where('title',$data['title'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
-
|
|
|
- $model = new Depart();
|
|
|
-
|
|
|
- $model->title = $data['title'] ;
|
|
|
- $model->code = $data['code'] ?? '' ;
|
|
|
|
|
|
- $model->save();
|
|
|
+ Depart::insert($msg['data']);
|
|
|
|
|
|
return [true,'保存成功!'];
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public function departDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
+ list($status,$msg) = $this->checkDepartDel($data);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
|
|
|
- Depart::where('id',$data['id'])->update([
|
|
|
+ Depart::whereIn('id',$data['id'])->update([
|
|
|
'del_time'=>time()
|
|
|
]);
|
|
|
|
|
@@ -210,20 +257,97 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
|
|
|
public function departList($data){
|
|
|
- $list = Depart::where('del_time',0)->select('title','crt_time','id','upd_time','code')->orderBy('id','desc');
|
|
|
-
|
|
|
- $list = $this->limit($list,'',$data);
|
|
|
+ $model = Depart::where('del_time',0)
|
|
|
+ ->select('title','id','code','parent_id','is_use')
|
|
|
+ ->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);
|
|
|
+ }
|
|
|
|
|
|
return [200,$list];
|
|
|
}
|
|
|
|
|
|
- public function departRule($data){
|
|
|
- if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
|
|
|
- if($this->isEmpty($data,'code')) return [false,'编码不存在!'];
|
|
|
+ public function departRule($data, $is_check = true){
|
|
|
+ if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
|
|
|
+
|
|
|
+ $code = array_column($data['data'],'code');
|
|
|
+ $title = array_column($data['data'],'title');
|
|
|
+ $code = array_map(function($val) {
|
|
|
+ return $val !== null ? $val : 0;
|
|
|
+ }, $code);
|
|
|
+ $title = array_map(function($val) {
|
|
|
+ return $val !== null ? $val : 0;
|
|
|
+ }, $title);
|
|
|
+ $code_count = array_count_values($code);
|
|
|
+ $title_count = array_count_values($title);
|
|
|
+ foreach ($code as $value){
|
|
|
+ if(empty($value)) return [false,'编码不能为空!'];
|
|
|
+ if($code_count[$value] > 1) return [false,'编码不能重复'];
|
|
|
+ }
|
|
|
+ foreach ($title as $value){
|
|
|
+ if(empty($value)) return [false,'名称不能为空!'];
|
|
|
+ if($title_count[$value] > 1) return [false,'名称不能重复'];
|
|
|
+ }
|
|
|
|
|
|
- return [true,''];
|
|
|
+ $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;
|
|
|
+
|
|
|
+ $data['data'][$key]['upd_time'] = time();
|
|
|
+ if($is_check){
|
|
|
+ $data['data'][$key]['crt_time'] = time();
|
|
|
+ $bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->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)
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'编码或部门名称不能重复'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $data];
|
|
|
}
|
|
|
|
|
|
+ public function checkDepartDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
+
|
|
|
+ $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
|
|
|
+ if($bool) return [false,'部门下有子部门!'];
|
|
|
+
|
|
|
+ if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ //检测部门下是否存在人员
|
|
|
+ public function checkDepartHasPerson($depart_id = []){
|
|
|
+ if(empty($depart_id)) return false;
|
|
|
+
|
|
|
+ $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
|
|
|
+ ->leftJoin('employee as b','b.id','a.employee_id')
|
|
|
+ ->where('b.del_time',0)
|
|
|
+ ->whereIn('a.depart_id',$depart_id)
|
|
|
+ ->exists();
|
|
|
+
|
|
|
+ return $bool;
|
|
|
+ }
|
|
|
|
|
|
public function teamEdit($data){
|
|
|
list($status,$msg) = $this->teamRule($data,false);
|