|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\API\His;
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
use Illuminate\Http\Client\RequestException;
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
|
|
class HisController extends Controller
|
|
|
{
|
|
|
public static $request;
|
|
|
public static $baseUrl = 'http://192.168.80.39:7801';
|
|
|
public static function Api($url_code)
|
|
|
{
|
|
|
$api['查询检查申请单'] = "/roc/order-service/api/v1/apply/pacs/apply/create/query";
|
|
|
$api['查询科室列表'] = "/roc/curr-web/api/v1/common/dept/query";
|
|
|
$api['查询人员列表'] = "/roc/curr-web/api/v1/common/employee/query";
|
|
|
$api['查询非药品列表'] = "/roc/order-service/api/v1/order/order-term/undrug/query";
|
|
|
$api['查询所有检查项目分类'] = "/roc/order-service/api/v1/order/order-term/dawn-code/query?consTypeId=EXAMSHEETTYPE";
|
|
|
$api['查询医生有权限的科室'] = "/roc/curr-web/api/v1/common/dept/doctdept/query";
|
|
|
$api['查询药品列表'] = "/roc/order-service/api/v1/order/order-term/drug/query";
|
|
|
return $api["{$url_code}"] ?? $url_code;
|
|
|
}
|
|
|
|
|
|
public static function Get($url_code, $data)
|
|
|
{
|
|
|
|
|
|
$url_address = self::Api($url_code);
|
|
|
self::RequestLog(self::$baseUrl . $url_address, $data, $url_code, 'Roc接口');
|
|
|
|
|
|
|
|
|
$response = Http::withHeaders([
|
|
|
'Domain' => 'QhdLK_YYGL',
|
|
|
'Key' => 'f7ed8036-b7a1-41ec-80f6-0e180efba1f4'
|
|
|
])->get(self::$baseUrl . $url_address, $data);
|
|
|
|
|
|
// dd(self::BuildAuthorization($nonce, $timestamp, $base64Signature));
|
|
|
if ($response->successful()) {
|
|
|
// 处理成功的响应
|
|
|
$res_string = json_encode($response->json(), JSON_UNESCAPED_UNICODE);
|
|
|
// dd($res_string);
|
|
|
$str_len = mb_strlen($res_string, 'utf-8');
|
|
|
$str_size = $str_len / 1024;
|
|
|
$save_res = $res_string;
|
|
|
if ($str_size > 10) $save_res = '{"data":"Row size too large"}';
|
|
|
self::$request->response_data = $save_res;
|
|
|
self::$request->save();
|
|
|
|
|
|
$res = json_decode($res_string, true);
|
|
|
if ($res['code']<>200) {
|
|
|
throw new HttpResponseException(\Yz::JsonError("Roc接口提示:" . $res['msg']));
|
|
|
}
|
|
|
return [
|
|
|
'code' => $res['code'],
|
|
|
'message' => $res['msg'],
|
|
|
'data' => $res['data']
|
|
|
];
|
|
|
} else {
|
|
|
// 处理失败的响应
|
|
|
self::$request->response_data = "请求Roc接口失败";
|
|
|
self::$request->save();
|
|
|
throw new HttpResponseException(\Yz::JsonError("请求Roc接口失败"));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* POST 请求,带日志记录
|
|
|
*/
|
|
|
public static function Post($url_path, $data)
|
|
|
{
|
|
|
$fullUrl = self::$baseUrl . $url_path;
|
|
|
self::RequestLog($fullUrl, $data, 'HIS回写', 'Roc接口');
|
|
|
|
|
|
$response = Http::withHeaders([
|
|
|
'Domain' => 'QhdLK_YYGL',
|
|
|
'Key' => 'f7ed8036-b7a1-41ec-80f6-0e180efba1f4',
|
|
|
'Content-Type' => 'application/json',
|
|
|
])->post($fullUrl, $data);
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
$res_string = json_encode($response->json(), JSON_UNESCAPED_UNICODE);
|
|
|
$str_len = mb_strlen($res_string, 'utf-8');
|
|
|
$str_size = $str_len / 1024;
|
|
|
$save_res = $res_string;
|
|
|
if ($str_size > 10) $save_res = '{"data":"Row size too large"}';
|
|
|
self::$request->response_data = $save_res;
|
|
|
self::$request->save();
|
|
|
|
|
|
$res = json_decode($res_string, true);
|
|
|
if ($res['code']<>200) {
|
|
|
throw new HttpResponseException(\Yz::JsonError("Roc接口提示:" . $res['msg']));
|
|
|
}
|
|
|
return [
|
|
|
'code' => $res['code'],
|
|
|
'message' => $res['msg'],
|
|
|
'data' => $res['data']
|
|
|
];
|
|
|
} else {
|
|
|
self::$request->response_data = "请求Roc接口失败";
|
|
|
self::$request->save();
|
|
|
throw new HttpResponseException(\Yz::JsonError("请求Roc接口失败"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取申请单详情(带缓存)
|
|
|
* @param string $visitSqNo 就诊流水号
|
|
|
* @param string $requestNo 申请单号
|
|
|
* @param string|int $visitTypeCode 就诊类型代码
|
|
|
* @param string $type 入参类型:本地|HIS
|
|
|
* @return array {"code":200,"msg":"操作成功!","data":[...]}
|
|
|
*/
|
|
|
public static function GetApplyDetail($visitSqNo, $requestNo, $visitTypeCode, $type = 'HIS')
|
|
|
{
|
|
|
// 1. 参数归一:本地 patient_type -> HIS visitTypeCode
|
|
|
if ($type === '本地') {
|
|
|
$patientHisTypeMap = [0 => '03', 1 => '01', 2 => '02', 3 => '04'];
|
|
|
$visitTypeCode = $patientHisTypeMap[$visitTypeCode] ?? '09';
|
|
|
}
|
|
|
|
|
|
// ROC 查询用 visitTypeCode 归一:门诊/急诊 -> 01,住院 -> 03,体检 -> 04,其他 -> 03
|
|
|
$rocVisitTypeCode = in_array($visitTypeCode, ['01', '02']) ? '01' : (in_array($visitTypeCode, ['03', '04']) ? $visitTypeCode : '03');
|
|
|
|
|
|
// 2. 查缓存表
|
|
|
$cached = DB::table('s_apply_detail_cache')
|
|
|
->where('visit_sq_no', $visitSqNo)
|
|
|
->where('request_no', $requestNo)
|
|
|
->where('visit_type_code', $rocVisitTypeCode)
|
|
|
->first();
|
|
|
|
|
|
if ($cached) {
|
|
|
return [
|
|
|
'code' => 200,
|
|
|
'msg' => '操作成功!',
|
|
|
'data' => json_decode($cached->raw_data, true),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
// 3. 请求 ROC(3 轮,每轮 try-catch)
|
|
|
$queries = [
|
|
|
['feeFlag', '0'],
|
|
|
['feeFlag', '1'],
|
|
|
['orderStatus', '1'],
|
|
|
];
|
|
|
|
|
|
foreach ($queries as [$paramKey, $paramValue]) {
|
|
|
$queryData = [
|
|
|
'visitSqNo' => $visitSqNo,
|
|
|
'requestNo' => $requestNo,
|
|
|
'visitTypeCode' => $rocVisitTypeCode,
|
|
|
$paramKey => $paramValue,
|
|
|
];
|
|
|
|
|
|
try {
|
|
|
$res = self::Get('查询检查申请单', $queryData);
|
|
|
} catch (\Exception $e) {
|
|
|
// 接口异常,继续下一轮
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if ($res['code'] == 200 && !empty($res['data'])) {
|
|
|
// 4. 写缓存
|
|
|
$queryFeeFlag = $paramKey === 'feeFlag' ? $paramValue : 'orderStatus=1';
|
|
|
DB::table('s_apply_detail_cache')->insert([
|
|
|
'visit_sq_no' => $visitSqNo,
|
|
|
'request_no' => $requestNo,
|
|
|
'visit_type_code' => $rocVisitTypeCode,
|
|
|
'raw_data' => json_encode($res['data'], JSON_UNESCAPED_UNICODE),
|
|
|
'query_fee_flag' => $queryFeeFlag,
|
|
|
]);
|
|
|
|
|
|
return [
|
|
|
'code' => 200,
|
|
|
'msg' => '操作成功!',
|
|
|
'data' => $res['data'],
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 5. 三轮均无数据
|
|
|
return [
|
|
|
'code' => 200,
|
|
|
'msg' => '操作成功!',
|
|
|
'data' => [],
|
|
|
];
|
|
|
}
|
|
|
|
|
|
public static function RequestLog($url, $post_data, $mark, $code = 0)
|
|
|
{
|
|
|
self::CheckTableName();
|
|
|
foreach ($post_data as $key => $post_datum) {
|
|
|
$str_len = mb_strlen(json_encode($post_datum, JSON_UNESCAPED_UNICODE), 'utf-8');
|
|
|
$str_size = $str_len / 1024;
|
|
|
if ($str_size > 10) {
|
|
|
$post_data["$key"] = 'Row size too large';
|
|
|
}
|
|
|
}
|
|
|
$post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE);
|
|
|
self::$request->code = $code;
|
|
|
self::$request->mark = $mark;
|
|
|
self::$request->post_data = $post_data == '[]' ? '{}' : $post_data;
|
|
|
self::$request->request_url = $url;
|
|
|
self::$request->save();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据 termCode 更新缓存中对应检查项目的执行科室
|
|
|
* 静默执行,异常不影响调用方
|
|
|
*/
|
|
|
public static function UpdateApplyDetailCache($visitSqNo, $requestNo, $visitTypeCode, $type, $termCode, $execDeptCode, $execDeptName)
|
|
|
{
|
|
|
try {
|
|
|
// 1. 归一化 visitTypeCode(与 GetApplyDetail 一致)
|
|
|
if ($type === '本地') {
|
|
|
$patientHisTypeMap = [0 => '03', 1 => '01', 2 => '02', 3 => '04'];
|
|
|
$visitTypeCode = $patientHisTypeMap[$visitTypeCode] ?? '09';
|
|
|
}
|
|
|
$rocVisitTypeCode = in_array($visitTypeCode, ['01', '02']) ? '01' : (in_array($visitTypeCode, ['03', '04']) ? $visitTypeCode : '03');
|
|
|
|
|
|
// 2. 查缓存记录
|
|
|
$cached = DB::table('s_apply_detail_cache')
|
|
|
->where('visit_sq_no', $visitSqNo)
|
|
|
->where('request_no', $requestNo)
|
|
|
->where('visit_type_code', $rocVisitTypeCode)
|
|
|
->first();
|
|
|
|
|
|
if (!$cached) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 3. 解码 raw_data
|
|
|
$data = json_decode($cached->raw_data, true);
|
|
|
if (empty($data)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 4. 遍历所有 data[] 的 itemList,按 termCode 匹配并更新
|
|
|
$updated = false;
|
|
|
foreach ($data as &$applyDetail) {
|
|
|
if (!empty($applyDetail['itemList']) && is_array($applyDetail['itemList'])) {
|
|
|
foreach ($applyDetail['itemList'] as &$item) {
|
|
|
if (($item['termCode'] ?? '') == $termCode) {
|
|
|
$item['execDeptCode'] = $execDeptCode;
|
|
|
$item['execDeptName'] = $execDeptName;
|
|
|
$updated = true;
|
|
|
}
|
|
|
}
|
|
|
unset($item);
|
|
|
}
|
|
|
}
|
|
|
unset($applyDetail);
|
|
|
|
|
|
// 5. 写回缓存
|
|
|
if ($updated) {
|
|
|
DB::table('s_apply_detail_cache')
|
|
|
->where('id', $cached->id)
|
|
|
->update(['raw_data' => json_encode($data, JSON_UNESCAPED_UNICODE)]);
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
// 静默兜底,不影响调用方
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function CheckTableName()
|
|
|
{
|
|
|
$table_name = 'zz_peis_log_' . date('ym');
|
|
|
$table_count = DB::select('select count(1) as c from information_schema.TABLES where table_schema = ? and table_name = ?', [env('DB_DATABASE'), $table_name])[0];
|
|
|
if ($table_count->c === 0) {
|
|
|
Schema::create($table_name, function (Blueprint $table) {
|
|
|
$table->id();
|
|
|
$table->string('code', 50)->index();
|
|
|
$table->string('mark', 50)->index();
|
|
|
$table->text('post_data');
|
|
|
$table->text('response_data')->nullable();
|
|
|
$table->string('request_url', 2000);
|
|
|
$table->timestamps();
|
|
|
});
|
|
|
}
|
|
|
self::$request = new \App\Models\PEISLog();
|
|
|
self::$request->setTable($table_name);
|
|
|
}
|
|
|
}
|