diff --git a/Laravel/app/Http/Controllers/API/His/UserController.php b/Laravel/app/Http/Controllers/API/His/UserController.php index 563e84c..73b6ed9 100644 --- a/Laravel/app/Http/Controllers/API/His/UserController.php +++ b/Laravel/app/Http/Controllers/API/His/UserController.php @@ -59,6 +59,121 @@ public function GetUserList(){ return \Yz::JsonError("调用His接口失败"); } + //从HIS同步人员 + 返回统计 + public function SyncUser() + { + // 1. 同步前快照 + $beforeSnapshot = $this->getUserSnapshot(); + + // 2. 调用 HIS 接口 + $His = new HisController(); + $res = $His::Get('查询人员列表', []); + + if ($res['code'] != 200) { + return \Yz::JsonError('调用His接口失败'); + } + + $hisData = $res['data']; + $receivedCodes = []; + + foreach ($hisData as $item) { + $emplCode = $item['emplCode']; + if ($emplCode == 'admin') continue; + $receivedCodes[] = $emplCode; + + $user = DB::table('users')->where('cas_code', $emplCode)->first(); + $dept = DB::table('s_department')->where('department_number', $item['deptCode'])->first(); + + $group = null; + if ($item['posiCode'] == '300A') { + $group = 6; + } else { + if (in_array($item['emplType'], ['N', 'T'])) { + $group = 3; + } + if ($item['emplType'] == 'D') { + $group = 7; + } + } + + if (!$user) { + DB::table('users')->insert([ + 'cas_code' => $emplCode, + 'cn_name' => $item['emplName'], + 'username' => $emplCode, + 'pwd' => '$2y$10$HlgBHem3knR9JGcPWWxfhuj/oQzGouQaWQ8BIDbSxliJ39G6pOj/K', + 'department_id' => isset($dept->id) ? $dept->id : null, + 'status' => 1, + 'group' => $group, + ]); + } else { + $updateData = [ + 'cn_name' => $item['emplName'], + 'department_id' => isset($dept->id) ? $dept->id : null, + 'group' => $user->group_locked == 1 ? $user->group : $group, + ]; + // 如果之前是禁用状态,同步时恢复启用 + if ($user->status == 0) { + $updateData['status'] = 1; + } + DB::table('users')->where('id', $user->id)->update($updateData); + } + } + + // 3. 处理移除:HIS未返回的人员标记 status=0 + DB::table('users') + ->where('status', 1) + ->where('cas_code', '!=', 'admin') + ->whereNotIn('cas_code', $receivedCodes) + ->update(['status' => 0]); + + // 4. 同步后快照 + $afterSnapshot = $this->getUserSnapshot(); + + // 5. 对比统计 + $beforeCodes = array_keys($beforeSnapshot); + $afterCodes = array_keys($afterSnapshot); + $beforeSet = array_flip($beforeCodes); + $afterSet = array_flip($afterCodes); + + $addedCodes = array_diff_key($afterSet, $beforeSet); + $removedCodes = array_diff_key($beforeSet, $afterSet); + $commonCodes = array_intersect_key($beforeSet, $afterSet); + + $updatedCount = 0; + foreach ($commonCodes as $code => $v) { + $before = $beforeSnapshot[$code]; + $after = $afterSnapshot[$code]; + if ($before['name'] !== $after['name'] || $before['dept_id'] !== $after['dept_id']) { + $updatedCount++; + } + } + + return \Yz::Return(true, '同步完成', [ + 'added' => count($addedCodes), + 'removed' => count($removedCodes), + 'updated' => $updatedCount, + ]); + } + + //获取所有 status=1 的人员快照 + private function getUserSnapshot() + { + $items = DB::table('users') + ->where('status', 1) + ->select('cas_code', 'cn_name', 'department_id') + ->get(); + + $snapshot = []; + foreach ($items as $item) { + $snapshot[$item->cas_code] = [ + 'name' => $item->cn_name, + 'dept_id' => $item->department_id, + ]; + } + return $snapshot; + } + //用于his 医生直接跳转过来,查看开单记录 public function AutoLogin(){ $usercode = request('usercode'); diff --git a/Laravel/routes/api.php b/Laravel/routes/api.php index 2c8c194..5568788 100644 --- a/Laravel/routes/api.php +++ b/Laravel/routes/api.php @@ -185,6 +185,7 @@ Route::post('admin/HisGetDepartmentList','App\Http\Controllers\API\His\DepartmentController@GetDepartmentList');//获取his科室列表 Route::post('admin/SyncDepartmentFromData','App\Http\Controllers\API\His\DepartmentController@SyncDepartmentFromData');//根据入参数据同步科室 Route::post('admin/HisGetUserList','App\Http\Controllers\API\His\UserController@GetUserList');//获取his用户列表 + Route::post('admin/DeptSyncUser','App\Http\Controllers\API\His\UserController@SyncUser');//从HIS同步人员 Route::post('admin/SyncDrugList','App\Http\Controllers\API\His\DrugController@SyncDrugList');//同步药品列表 Route::post('admin/SyncUndrugList','App\Http\Controllers\API\His\DrugController@SyncUndrugList');//同步非药品列表 Route::post('admin/HisAutoLogin','App\Http\Controllers\API\His\UserController@AutoLogin' ); diff --git a/YiJi-admin/src/api/api.js b/YiJi-admin/src/api/api.js index 35e0712..a5628b4 100644 --- a/YiJi-admin/src/api/api.js +++ b/YiJi-admin/src/api/api.js @@ -51,6 +51,10 @@ export const SaveSystemUserInfo = (data = {}) => { export const GetSystemUserDetail = (data = {}) => { return axios({ url: import.meta.env.VITE_APP_API + 'v1/admin/GetSystemUserDetail', data: data }) } +//从HIS同步人员 +export const DeptSyncUser = (data = {}) => { + return axios({ url: import.meta.env.VITE_APP_API + 'v1/admin/DeptSyncUser', data: data }) +} //admin后台修改密码 export const adminChangePwd = (data = {}) => { return axios({ url: import.meta.env.VITE_APP_API + 'v1/admin/adminChangePwd', data: data }) diff --git a/YiJi-admin/src/views/SystemMngr/User/List.vue b/YiJi-admin/src/views/SystemMngr/User/List.vue index bcc4490..9ff56ee 100644 --- a/YiJi-admin/src/views/SystemMngr/User/List.vue +++ b/YiJi-admin/src/views/SystemMngr/User/List.vue @@ -15,6 +15,7 @@ 查询 新建用户 + 从HIS同步人员 @@ -108,6 +109,45 @@ + + + + + @@ -125,11 +165,13 @@ getAdminUserList, SaveSystemUserInfo, GetSystemUserDetail, - resetPwd + resetPwd, + DeptSyncUser } from '@/api/api.js' import { Edit, - Refresh + Refresh, + Loading } from '@element-plus/icons-vue' import { @@ -267,6 +309,16 @@ ); let resetPwdDialogShow = ref(false); let Password = ref(''); + + //同步人员相关 + let syncDialogVisible = ref(false) + let syncStep = ref(0) + let syncPercent = ref(0) + let syncDone = ref(false) + let syncLoading = ref(false) + let syncLoadingText = ref('') + let syncResult = ref({ added: 0, removed: 0, updated: 0 }) + const resetPwdClick = () => { resetPwdDialogShow.value = true } @@ -294,6 +346,33 @@ } }) } + + //打开同步人员弹窗 + const openSyncDialog = async () => { + syncDialogVisible.value = true + syncStep.value = 0 + syncPercent.value = 0 + syncDone.value = false + syncLoading.value = false + syncResult.value = { added: 0, removed: 0, updated: 0 } + + syncLoadingText.value = '正在同步人员...' + syncLoading.value = true + const res = await DeptSyncUser() + syncLoading.value = false + + if (!res.status) { + ElMessage.error('同步人员失败:' + (res.msg || '未知错误')) + syncDialogVisible.value = false + return + } + + syncResult.value = res.data + syncStep.value = 1 + syncPercent.value = 100 + syncDone.value = true + } + onMounted(() => { })