|
@@ -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,42 +29,120 @@ 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['account'];
|
|
|
+ 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,
|
|
|
+ 'crt_time' => time(),
|
|
|
+ 'upd_time' => time(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ 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['account'];
|
|
|
+ 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,
|
|
|
+ 'crt_time' => time(),
|
|
|
+ 'upd_time' => time(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ 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){
|
|
@@ -383,10 +462,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 +473,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)
|
|
@@ -744,15 +814,31 @@ class EmployeeService extends Service
|
|
|
|
|
|
//获取登录账号的部门
|
|
|
public static function getLoginDepart($employee_id){
|
|
|
- if(empty($employee_id)) return [];
|
|
|
+ $is_main = 0;//是否总社
|
|
|
+ if(empty($employee_id)) return ['', $is_main];
|
|
|
|
|
|
//admin账号
|
|
|
- if($employee_id == Employee::SPECIAL_ADMIN) return [Depart::RULE_DEPART];
|
|
|
+ if($employee_id == Employee::SPECIAL_ADMIN) {
|
|
|
+ $is_main = 1;
|
|
|
+ return [Depart::RULE_DEPART, $is_main];
|
|
|
+ }
|
|
|
|
|
|
- $depart = EmployeeDepartPermission::where('employee_id',$employee_id)
|
|
|
- ->select('depart_id')
|
|
|
+ //自己绑定的部门 启用的部门
|
|
|
+ $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 array_unique(array_column($depart,'depart_id'));
|
|
|
+ return [$depart, $is_main];
|
|
|
}
|
|
|
}
|