|
|
@@ -0,0 +1,616 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\Depart;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\EmployeeDepartPermission;
|
|
|
+use App\Model\EmployeeRole;
|
|
|
+use App\Model\Role;
|
|
|
+use App\Model\RoleMenu;
|
|
|
+use App\Model\RoleMenuButton;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Hash;
|
|
|
+use Mockery\Exception;
|
|
|
+
|
|
|
+class EmployeeService extends Service
|
|
|
+{
|
|
|
+ public function employeeEditOther($data,$user){
|
|
|
+ list($status,$msg) = $this->employeeOtherRule($data,$user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $model = new Employee();
|
|
|
+ $model = $model->where('id',$user['id'])->first();
|
|
|
+ $model->password = Hash::make($data['new_password']);
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeOtherRule($data,$user){
|
|
|
+ if(! isset($data['old_password'])) return [false,'请输入原密码'];
|
|
|
+ if($data['old_password'] == "") return [false,'原密码不能为空'];
|
|
|
+ if(! isset($data['new_password'])) return [false,'请输入新密码'];
|
|
|
+ if($data['new_password'] == "") return [false,'新密码不能为空'];
|
|
|
+ if(! isset($data['re_password'])) return [false,'请输入确认密码'];
|
|
|
+ if($data['re_password'] == "") return [false,'确认密码不能为空'];
|
|
|
+ if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
|
|
|
+ if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
|
|
|
+ if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeEdit($data,$user){
|
|
|
+ list($status,$msg) = $this->employeeRule($data,$user,false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $model = new Employee();
|
|
|
+ $model = $model->where('id',$data['id'])->first();
|
|
|
+ $model->number = $data['number'];
|
|
|
+ $model->title = $data['title'];
|
|
|
+ $model->mobile = $data['mobile'] ?? '';
|
|
|
+ $model->is_admin = $data['is_admin'];
|
|
|
+ if($model->is_admin && $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()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->employeeRule($data, $user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ $model = new Employee();
|
|
|
+
|
|
|
+ $model->number = $data['number'];
|
|
|
+ $model->title = $data['title'];
|
|
|
+ $model->mobile = $data['mobile'] ?? '';
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->is_admin = $data['is_admin'];
|
|
|
+ $model->account = $data['account'] ?? "";
|
|
|
+ if($model->is_admin && $data['password']) $model->password = Hash::make($data['password']);
|
|
|
+ $model->top_depart_id = $data['top_depart_id'];
|
|
|
+ $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()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
|
|
|
+
|
|
|
+ Employee::whereIn('id',$data['id'])->update([
|
|
|
+ 'del_time'=>time()
|
|
|
+ ]);
|
|
|
+ EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
|
|
|
+ 'del_time'=>time()
|
|
|
+ ]);
|
|
|
+ EmployeeDepartPermission::whereIn('employee_id',$data['id'])->delete();
|
|
|
+
|
|
|
+ return [true,'删除成功'];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeList($data,$user){
|
|
|
+ $model = Employee::TopClear($user,$data);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select('number','mobile','title','id','is_admin', 'account', 'crt_time')
|
|
|
+ ->orderBy('id','desc');
|
|
|
+
|
|
|
+ if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+ if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
|
|
|
+ if(! empty($data['role'])) {
|
|
|
+ $emp = EmployeeRole::where('role_id',$data['role'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('employee_id')->get()->toArray();
|
|
|
+ $model->whereIn('id',array_column($emp,'employee_id'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+
|
|
|
+ //组织数据
|
|
|
+ $list = $this->organizationEmployeeData($list);
|
|
|
+
|
|
|
+ return [true , $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function organizationEmployeeData($data)
|
|
|
+ {
|
|
|
+ if (empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ // 获取员工ID并查询扩展数据
|
|
|
+ $employee_ids = array_column($data['data'], 'id');
|
|
|
+ list($status, $extraMap) = $this->getEmployee($employee_ids);
|
|
|
+
|
|
|
+ foreach ($data['data'] as &$item) {
|
|
|
+ $id = $item['id'];
|
|
|
+ $extra = $extraMap[$id] ?? null;
|
|
|
+
|
|
|
+ $item['role'] = $extra['role_ids'] ?? [];
|
|
|
+ $item['role_name'] = isset($extra['role_names']) ? implode(',', $extra['role_names']) : '';
|
|
|
+ $item['depart'] = $extra['depart_ids'] ?? [];
|
|
|
+ $item['depart_title'] = isset($extra['depart_names']) ? implode(',', $extra['depart_names']) : '';
|
|
|
+
|
|
|
+ // 业务状态字段
|
|
|
+ $item['is_admin_title'] = Employee::IS_ADMIN_TITLE[$item['is_admin']] ?? "";
|
|
|
+ $item['crt_time'] = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : "";
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getEmployee(array $employee_ids)
|
|
|
+ {
|
|
|
+ if (empty($employee_ids)) return [false, []];
|
|
|
+
|
|
|
+ // 1. 一次性获取所有角色
|
|
|
+ $roles = DB::table('employee_role as a')
|
|
|
+ ->join('role as b', 'a.role_id', '=', 'b.id')
|
|
|
+ ->where('a.del_time', 0)
|
|
|
+ ->where('b.del_time', 0)
|
|
|
+ ->whereIn("a.employee_id", $employee_ids)
|
|
|
+ ->select('a.employee_id', 'b.title', 'b.id')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ // 2. 一次性获取所有部门
|
|
|
+ $departs = DB::table('employee_depart_permission as a')
|
|
|
+ ->join('depart as b', 'a.depart_id', '=', 'b.id')
|
|
|
+ ->whereIn("a.employee_id", $employee_ids)
|
|
|
+ ->select('a.employee_id', 'b.title', 'b.id')
|
|
|
+ ->orderBy('b.id')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ // 3. 结果按员工ID分组归纳
|
|
|
+ $resultMap = [];
|
|
|
+ foreach ($employee_ids as $id) {
|
|
|
+ $resultMap[$id] = [
|
|
|
+ 'role_ids' => [],
|
|
|
+ 'role_names' => [],
|
|
|
+ 'depart_ids' => [],
|
|
|
+ 'depart_names' => []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($roles as $r) {
|
|
|
+ $resultMap[$r->employee_id]['role_ids'][] = $r->id;
|
|
|
+ $resultMap[$r->employee_id]['role_names'][] = $r->title;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($departs as $d) {
|
|
|
+ $resultMap[$d->employee_id]['depart_ids'][] = $d->id;
|
|
|
+ $resultMap[$d->employee_id]['depart_names'][] = $d->title;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $resultMap];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function employeeRule(&$data, $user,$is_add = true){
|
|
|
+ if(empty($data['number'])) return [false,'工号不存在'];
|
|
|
+ if(empty($data['title'])) return [false,'姓名不存在'];
|
|
|
+ if(empty($data['depart'])) return [false,'部门不能为空'];
|
|
|
+ if(! empty($data['is_admin']) && empty($data['password'])) return [false, '密码不能为空'];
|
|
|
+ $data['top_depart_id'] = $user['top_depart_id'];
|
|
|
+
|
|
|
+ $mobile = $data['mobile'] ?? "";
|
|
|
+ $number = $data['number'] ?? "";
|
|
|
+ if(! $is_add){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
|
|
|
+ $bool = Employee::where('del_time',0)
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where(function ($query) use ($mobile, $number){
|
|
|
+ $query->where('number', $number);
|
|
|
+ $query->when(! empty($mobile), function ($query) use ($mobile) {
|
|
|
+ return $query->orWhere('mobile', $mobile);
|
|
|
+ });
|
|
|
+ })->exists();
|
|
|
+ }else{
|
|
|
+ if(! empty($data['is_admin'])){
|
|
|
+ $code = Depart::where('id', $user['top_depart_id'])->value('code');
|
|
|
+ $data['account'] = $code . "_" . $data['number'];
|
|
|
+ }
|
|
|
+ $bool = Employee::where('del_time',0)
|
|
|
+ ->where(function ($query) use ($mobile, $number){
|
|
|
+ $query->where('number', $number);
|
|
|
+ $query->when(! empty($mobile), function ($query) use ($mobile) {
|
|
|
+ return $query->orWhere('mobile', $mobile);
|
|
|
+ });
|
|
|
+ })->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'工号或手机号码已存在'];
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleEdit($data,$user){
|
|
|
+ list($status,$msg) = $this->roleRule($data,$user, false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ $model = new Role();
|
|
|
+ $model = $model->where('id',$data['id'])->first();
|
|
|
+ $model->title = $data['title'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->roleRule($data,$user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ $model = new Role();
|
|
|
+ $model->title = $data['title'] ;
|
|
|
+ $model->top_depart_id = $data['top_depart_id'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
+
|
|
|
+ $bool = EmployeeRole::where('del_time',0)
|
|
|
+ ->whereIn('role_id',$data['id'])
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色已绑定人员'];
|
|
|
+
|
|
|
+ Role::where('id',$data['id'])->update([
|
|
|
+ 'del_time' => time()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
|
|
|
+ 'del_time' => time()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
|
|
|
+ 'del_time' => time()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleList($data,$user){
|
|
|
+ $model = Role::TopClear($user,$data);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select('title','crt_time','id','upd_time')
|
|
|
+ ->orderBy('id','desc');
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleRule(&$data,$user, $is_check = true){
|
|
|
+ if($this->isEmpty($data,'title')) return [false,'名称不能为空'];
|
|
|
+
|
|
|
+ $data['top_depart_id'] = $user['top_depart_id'];
|
|
|
+
|
|
|
+ if($is_check){
|
|
|
+ $bool = Role::where('title',$data['title'])
|
|
|
+ ->where('top_depart_id', $data['top_depart_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色名称已存在'];
|
|
|
+ }else{
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
|
|
|
+ $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
|
|
|
+ $bool = Role::where('title',$data['title'])
|
|
|
+ ->where('top_depart_id',$top_depart_id)
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色名称已存在'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleMenu($data){
|
|
|
+ if(empty($data['role_id'])) return [false,'角色不能为空!'];
|
|
|
+ if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
|
|
|
+ RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
|
|
|
+
|
|
|
+ $insert = $insert2 = [];
|
|
|
+ foreach ($data['menu'] as $t){
|
|
|
+ $insert[] = [
|
|
|
+ 'role_id' => $data['role_id'],
|
|
|
+ 'menu_id' => $t['menu_id'],
|
|
|
+ 'type' => $t['type'],
|
|
|
+ 'crt_time' => time()
|
|
|
+ ];
|
|
|
+ if(! empty($t['button'])){
|
|
|
+ foreach ($t['button'] as $b){
|
|
|
+ $insert2[] = [
|
|
|
+ 'role_id' => $data['role_id'],
|
|
|
+ 'menu_id' => $t['menu_id'],
|
|
|
+ 'button_id' => $b,
|
|
|
+ 'crt_time' => time()
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ RoleMenuButton::insert($insert2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ RoleMenu::insert($insert);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function roleDetail($data){
|
|
|
+ if(empty($data['role_id'])) return [false,'请选择角色'];
|
|
|
+
|
|
|
+ $role = Role::where('id',$data['role_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('id','title')
|
|
|
+ ->first();
|
|
|
+ if(empty($role)) return [false,'角色不存在或已被删除'];
|
|
|
+ $role = $role->toArray();
|
|
|
+
|
|
|
+ $menu = RoleMenu::where('role_id',$data['role_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('menu_id','type')
|
|
|
+ ->get()->toArray();
|
|
|
+ $button = $this->fillRoleButton([$data['role_id']]);
|
|
|
+ foreach ($menu as $key => $value){
|
|
|
+ $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
|
|
|
+ }
|
|
|
+ $role['menu'] = $menu;
|
|
|
+
|
|
|
+ return [true, $role];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departEdit($data, $user){
|
|
|
+ list($status,$msg) = $this->departRule($data,$user,false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ $update = $msg['data'][0];
|
|
|
+ $model = new Depart();
|
|
|
+ $model->where('id',$data['id'])->update($update);
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->departRule($data,$user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ foreach ($msg['data'] as $value){
|
|
|
+ $model = new Depart();
|
|
|
+ $model->parent_id = $value['parent_id'];
|
|
|
+ $model->title = $value['title'];
|
|
|
+ $model->code = $value['code'];
|
|
|
+ $model->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departDel($data){
|
|
|
+ list($status,$msg) = $this->checkDepartDel($data);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ Depart::whereIn('id',$data['id'])->update([
|
|
|
+ 'del_time'=>time()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 departList($data, $user){
|
|
|
+ $model = Depart::TopClear($user,$data);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select('title','id','code','parent_id','is_use')
|
|
|
+ ->orderby('id', 'asc');
|
|
|
+
|
|
|
+ if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+ if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
|
|
|
+
|
|
|
+ $list = $model->get()->toArray();
|
|
|
+ $list = $this->fillDepartList($list, $user);
|
|
|
+ $list_tree = $list;
|
|
|
+ if(! empty($list_tree)) {
|
|
|
+ $minParentId = min(array_column($list_tree, 'parent_id'));
|
|
|
+ $list_tree = $this->makeTree($minParentId,$list_tree);
|
|
|
+ $list_tree = $this->set_sort_circle($list_tree);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,['data' => $list,'tree' => $list_tree]];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillDepartList($list,$user){
|
|
|
+ if(empty($list)) return $list;
|
|
|
+
|
|
|
+// foreach ($list as $key => $value){
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departRule($data,$user, $is_check = true){
|
|
|
+ if(empty($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,'名称不能重复'];
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $top_depart_id = $value['parent_id'];
|
|
|
+ if(empty($value['parent_id'])) {
|
|
|
+ $data['data'][$key]['parent_id'] = $user['top_depart_id'];
|
|
|
+ $top_depart_id = $user['top_depart_id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $data['data'][$key]['top_depart_id'] = $top_depart_id;
|
|
|
+ $data['data'][$key]['upd_time'] = time();
|
|
|
+
|
|
|
+ if($is_check){
|
|
|
+ $data['data'][$key]['crt_time'] = time();
|
|
|
+ $bool = Depart::whereRaw("binary code = '{$value['code']}'")
|
|
|
+ ->where('parent_id', $top_depart_id)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }else{
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
|
|
|
+ $bool = Depart::whereRaw("binary code = '{$value['code']}'")
|
|
|
+ ->where('parent_id', $top_depart_id)
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'部门编码不能重复'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $data];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 fillRoleButton($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){
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|