model = new Personnel(); } /** * 获取人力资源-人员列表 * @param array $where * @return array */ public function getPage(array $where = []) { $field = 'id,name,head_img,gender,birthday,phone,address,native_place,education,profile,emergency_contact_phone,id_card_front,id_card_back,employee_number,status,is_sys_user,sys_user_id,create_time,update_time,delete_time'; $order = 'create_time desc'; $search_model = $this->model->withSearch(["name", "gender", "phone", "address", "education", "employee_number", "status", "create_time"], $where)->field($field)->order($order); $list = $this->pageQuery($search_model, function ($item) { $CampusPersonRole = new CampusPersonRole(); $item['is_role'] = 1; $role = $CampusPersonRole->where(['person_id' => $item['id']])->find(); if($role){ $item['role_data'] = [ 'id' => $role['id'], 'campus_id' => $role['campus_id'], 'person_id' => $item['id'], 'role_id' => $role['role_id'], 'dept_id' => 0 ]; $item['is_role'] = $role['dept_id'] == 0 ? 1 : 0; }else{ $item['role_data'] = [ 'id' => '', 'campus_id' => '', 'person_id' => $item['id'], 'role_id' => '', 'dept_id' => '' ]; } }); return $list; } /** * 获取人力资源-人员信息 * @param int $id * @return array */ public function getInfo(int $id) { $field = 'id,name,gender,head_img,birthday,phone,address,native_place,education,profile,emergency_contact_phone,id_card_front,id_card_back,employee_number,status,is_sys_user,sys_user_id,create_time,update_time,delete_time'; $info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray(); $info['gender'] = strval($info['gender']); $info['status'] = strval($info['status']); $info['is_sys_user'] = strval($info['is_sys_user']); return $info; } /** * 添加人力资源-人员 * @param array $data * @return mixed */ public function add(array $data) { $status = $this->model->where('phone', $data['phone'])->value('id'); if ($status) { throw new \Exception('手机号已存在'); } try { Db::startTrans(); if ($data['is_sys_user'] === '1') { $uid = (new UserService())->addUser([ 'username' => $data['phone'], 'password' => $data['phone'], 'real_name' => $data['name'], 'head_img' => $data['head_img'], 'status' => UserDict::ON, 'role_ids' => [] ]); $data['sys_user_id'] = $uid; } // 员工编号 $data['employee_number'] = getEmployeeNumber(); $res = $this->model->create($data); Db::commit(); return $res->id; } catch (\Exception $e) { Db::rollback(); throw new \Exception($e->getMessage()); } } /** * 人力资源-人员编辑 * @param int $id * @param array $data * @return bool */ public function edit(int $id, array $data) { $status = $this->model->where('phone', $data['phone'])->value('id'); if ($status && $status != $id) { throw new \Exception('手机号已存在'); } try { Db::startTrans(); if ($data['is_sys_user'] === '1') { $uid = (new SysUser())->where(['username' => $data['phone']])->value('uid'); if (!$uid) { $uid = (new UserService())->addUser([ 'username' => $data['phone'], 'password' => $data['phone'], 'real_name' => $data['name'], 'head_img' => $data['head_img'], 'status' => UserDict::ON, 'role_ids' => [] ]); $data['sys_user_id'] = $uid; } } $this->model->where([['id', '=', $id]])->update($data); Db::commit(); } catch (\Exception $e) { Db::rollback(); throw new \Exception($e->getMessage()); } return true; } /** * 删除人力资源-人员 * @param int $id * @return bool */ public function del(int $id) { $model = $this->model->where([['id', '=', $id]])->find(); $res = $model->delete(); return $res; } }