462 lines
17 KiB
PHP
462 lines
17 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/7/12} {11:29}
|
||
*/
|
||
|
||
namespace app\api\controller;
|
||
|
||
|
||
use app\common\model\TabConf;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\gpt\chat\ChatGPT;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
use wanghua\general_utility_tools_php\Validate;
|
||
|
||
/**
|
||
* 疼痛科-数字人-报告
|
||
* Class Reporttt
|
||
* @package app\api\controller
|
||
*/
|
||
class Reporttt extends BaseHttpApi
|
||
{
|
||
|
||
/**
|
||
* desc:疼痛科-数字人-生成听诊报告(同时提交固定问答聊天记录)
|
||
*
|
||
* api/Reporttt/createReport
|
||
* username 用户名
|
||
*
|
||
* name 病人姓名
|
||
gender 病人性别
|
||
age 病人年龄
|
||
edu 教育程度
|
||
career_year 职业及年限
|
||
power 体力要求
|
||
satisfaction 经济满意度
|
||
old_career_year 过往职业及年限
|
||
support 社会支持度
|
||
marriage 婚姻状况
|
||
live 居住情况
|
||
home 家庭关系
|
||
address 地址
|
||
phone 联系方式
|
||
wechat_no 微信号
|
||
ticket 对话票据
|
||
hdrdepartment_id 科室
|
||
* from 来源:num_per=数智人,ty=听译助手,h5=h5网页问诊
|
||
* hdrdepartment 科室名称
|
||
*
|
||
* content 对话内容;格式:
|
||
* $content = [
|
||
["role" => "user", "content" => '医生说:xxxxx'],
|
||
["role" => "user", "content" => '患者说:xxxxx'],
|
||
];
|
||
* author:wh
|
||
*/
|
||
function createReport(){
|
||
//header('Content-Type: text/event-stream');
|
||
//header('Cache-Control: no-cache');
|
||
//header('Connection: keep-alive');
|
||
//header('X-Accel-Buffering: no');
|
||
Tools::log_to_write_txt(['input'=>input()]);
|
||
$report_config = config('ai_num_report_config');
|
||
$h5_login_user_phone = input('h5_login_user_phone');//h5端上传附件登录号码
|
||
//if (empty($h5_login_user_phone)){
|
||
// return json(Tools::set_fail('h5_login_user_phone必须','使用h5端上传附件那个号码'));
|
||
//}
|
||
|
||
$question = '';//input('question','');
|
||
|
||
$chatobj = new ChatGPT();
|
||
$chatobj->url = $report_config['base_url'];
|
||
$chatobj->model = '';
|
||
$chatobj->apiKey = $report_config['APIKey'];
|
||
|
||
$answer_json_arr = [];
|
||
|
||
$ticket = input('ticket');
|
||
if(empty($ticket)){
|
||
return json(Tools::set_fail('对话票据ticket必须(用于隔离用户聊天历史记录),登陆时返回票据,每次诊断结束重新生成票据!'));
|
||
}
|
||
|
||
if(empty(input('from'))){
|
||
return json(Tools::set_fail('from必须'));
|
||
}
|
||
|
||
$from = input('from');
|
||
|
||
$qa_type = input('qa_type');
|
||
if($from=='h5'){
|
||
$qa_type = 'fix';
|
||
}
|
||
if(empty($qa_type)){
|
||
return json(Tools::set_fail('qa_type问答模式必须,可选项:问答模式:free=自由问答,fix=固定问答'));
|
||
}
|
||
$user = Db::table('fa_hdrdoctorusers')->where('ticket',$ticket)->find();
|
||
if(empty($user)){
|
||
return json(Tools::set_res(531,'医生不存在,请登录'));
|
||
}
|
||
$hdrdepartment_id = input('hdrdepartment_id');
|
||
$config = [
|
||
'stream'=>false,
|
||
'chatId'=>Tools::getMillisecond(),
|
||
];
|
||
|
||
//验证参数
|
||
if(empty(input('name'))){
|
||
return json(Tools::set_fail('name必须'));
|
||
}
|
||
if(empty(input('gender'))){
|
||
return json(Tools::set_fail('gender必须'));
|
||
}
|
||
if(empty(input('age'))){
|
||
return json(Tools::set_fail('age必须'));
|
||
}
|
||
if(empty(input('phone'))){
|
||
return json(Tools::set_fail('phone必须'));
|
||
}
|
||
if(!Validate::is_mobile(input('phone'))){
|
||
return json(Tools::set_fail('phone格式错误'));
|
||
}
|
||
//if(empty(input('hdrdepartment_id'))){
|
||
// return json(Tools::set_fail('hdrdepartment_id必须'));
|
||
//}
|
||
$basedata = [
|
||
'name'=>input('name',''),//病人姓名
|
||
'gender'=>input('gender',''),//病人性别
|
||
'age'=>input('age',''),//病人年龄
|
||
'edu'=>input('edu',''),//教育程度
|
||
'career_year'=>input('career_year',''),//职业及年限
|
||
'power'=>input('power',''),//体力要求
|
||
'satisfaction'=>input('satisfaction',''),//经济满意度
|
||
'old_career_year'=>input('old_career_year',''),//过往职业及年限
|
||
'old_career_power'=>input('old_career_power',''),//过往体力要求
|
||
'support'=>input('support',''),//社会支持度
|
||
'marriage'=>input('marriage',''),//婚姻状况
|
||
'live'=>input('live',''),//居住情况
|
||
'home'=>input('home',''),//家庭关系
|
||
'address'=>input('address',''),//地址
|
||
'phone'=>input('phone',''),//联系方式
|
||
'wechat_no'=>input('wechat_no',''),//微信号
|
||
'from'=>$from,//来源
|
||
'ticket'=>$ticket,//对话票据'
|
||
'date'=>date('Y-m-d'),
|
||
'hdrdepartment_id'=>$hdrdepartment_id,//科室
|
||
];
|
||
|
||
$contentstr = input('content');//对话记录
|
||
if(empty($contentstr)){
|
||
return json(Tools::set_fail('content必须'));
|
||
}
|
||
|
||
$content['name'] = "\n\n".'【姓名】:'.$basedata['name'];
|
||
$content['gender'] = '【性别】:'.$basedata['gender'];
|
||
$content['age'] = '【年龄】:'.$basedata['age'];
|
||
$content['edu'] = '【教育程度】:'.$basedata['edu'];
|
||
$content['career_year'] = '【职业及年限】:'.$basedata['career_year'];
|
||
$content['power'] = '【体力要求】:'.$basedata['power'];
|
||
$content['satisfaction'] = '【经济满意度】:'.$basedata['satisfaction'];
|
||
$content['old_career_year'] = '【过往职业及年限】:'.$basedata['old_career_year'];
|
||
$content['old_career_power'] = '【过往体力要求】:'.$basedata['old_career_power'];
|
||
$content['support'] = '【社会支持度】:'.$basedata['support'];
|
||
$content['marriage'] = '【婚姻状况】:'.$basedata['marriage'];
|
||
$content['live'] = '【居住情况】:'.$basedata['live'];
|
||
$content['home'] = '【家庭关系】:'.$basedata['home'];
|
||
$content['address'] = '【地址】:'.$basedata['address'];
|
||
$content['phone'] = '【联系方式】:'.$basedata['phone'];
|
||
$content['wechat_no'] = '【微信号】:'.$basedata['wechat_no'];
|
||
|
||
//
|
||
////设置基本信息
|
||
//$chatobj->setBefore($content);
|
||
|
||
$sub_content = json_decode($contentstr,true);
|
||
if(empty($sub_content)){
|
||
return json(Tools::set_fail('没有对话诊断信息'));
|
||
}
|
||
//Tools::log_to_write_txt(['聊天记录content:'=>$sub_content]);
|
||
//$sub_content = array_merge($content,$sub_content);
|
||
|
||
$ctn_msg_str = implode('',array_column($sub_content,'content'));
|
||
foreach ($content as $val){
|
||
$ctn_msg_str .= ','.$val;
|
||
}
|
||
|
||
|
||
$report_ai_content = '';
|
||
//$last_file_report = [];
|
||
//if($h5_login_user_phone){
|
||
//查询最新的附件
|
||
$last_file_report = Db::table(TabConf::$fa_drugs_images_medical_result)
|
||
->where('reportid','eq','')
|
||
->field('id,reportid,ai_result')
|
||
->where('phone',$h5_login_user_phone?$h5_login_user_phone:$basedata['phone'])
|
||
->select();
|
||
foreach ($last_file_report as $val){
|
||
if($report_ai_content){
|
||
$report_ai_content.= ',';
|
||
}
|
||
$report_ai_content .= $val['ai_result']?$val['ai_result']:'';
|
||
}
|
||
//}
|
||
|
||
//追加附件
|
||
$ctn_msg_str .= <<<EOF
|
||
|
||
|
||
【附件】:{$report_ai_content}
|
||
EOF;
|
||
$sub_content_msg = [
|
||
["role" => "user", "content" => $ctn_msg_str]
|
||
];
|
||
|
||
|
||
//chatGpt设置前置聊天上下文
|
||
$chatobj->setBefore($sub_content_msg);
|
||
|
||
//回答
|
||
$chatobj->returnAnswer($question,$config,$answer_json_arr);
|
||
//dump($answer_json_arr);die;
|
||
|
||
$res_content = [];
|
||
$data = [];
|
||
Tools::log_to_write_txt(['请求gpt'=>$chatobj->post_msg_body]);
|
||
Tools::log_to_write_txt(['gpt回复'=>$answer_json_arr]);
|
||
//把返回的报告保存起来
|
||
foreach ($answer_json_arr as $josn){
|
||
$item = json_decode($josn,true);
|
||
if(empty($item['choices'])){
|
||
return json(Tools::set_fail('choices is empty',$answer_json_arr));
|
||
}
|
||
$choices = $item['choices'];
|
||
foreach ($choices as $choice){
|
||
$data[] = [
|
||
'username'=>$user['username'],//登录的医生
|
||
'report_content'=>$choice['message']['content'],
|
||
'name'=>$basedata['name'],
|
||
'phone'=>$basedata['phone'],
|
||
];
|
||
$res_content[] = $choice['message']['content'];
|
||
}
|
||
}
|
||
if(empty($data)){
|
||
return json(Tools::set_fail('没有获取到报告'));
|
||
}
|
||
|
||
try {
|
||
$medical_report_id = '';
|
||
foreach ($data as $item){
|
||
//只新增不修改
|
||
$userbaseinfo_id = Db::table('fa_tt_userbaseinfo')->insert($basedata);
|
||
//表示用户信息从h5里面查询而来
|
||
if($h5_login_user_phone){//存在表示是通过搜索手机号进入数字人的
|
||
$baseuserinfo = Db::table(TabConf::$fa_ty_userbaseinfo)
|
||
->where('phone',$h5_login_user_phone)
|
||
->order('id desc')
|
||
->find();
|
||
if(empty($baseuserinfo)){
|
||
return json(Tools::set_fail('没有查询到用户信息'));
|
||
}
|
||
// 科室名称
|
||
$basedata['hdrdepartment'] = input('hdrdepartment','');
|
||
////修改
|
||
//Db::table(TabConf::$fa_tt_userbaseinfo)
|
||
// ->where('id',$baseuserinfo['id'])
|
||
// ->data($basedata)
|
||
// ->update();
|
||
$hdruserbaseinfo_id = $baseuserinfo['id'];
|
||
}else{
|
||
//新增统一基本信息
|
||
$basedata['hdrdepartment'] = input('hdrdepartment','');
|
||
$hdruserbaseinfo_id = Db::table(TabConf::$fa_tt_userbaseinfo)->insertGetId($basedata);
|
||
}
|
||
|
||
$item['userbaseid'] = $userbaseinfo_id;
|
||
$item['hdruserbaseinfo_id'] = $hdruserbaseinfo_id;//统一病历基本信息ID
|
||
$item['hdrdepartment_id'] = $hdrdepartment_id;
|
||
$item['from'] = $from;//来源:num_per=数智人,ty=听译助手,h5=h5网页问诊
|
||
$item['doctor'] = $user['name'];
|
||
$item['qa_type'] = $qa_type;
|
||
$tmp_data = array_merge($item,$basedata);
|
||
//保存报告
|
||
$medical_report_id = Db::table('fa_tt_medical_report')->insertGetId($tmp_data);
|
||
//日期
|
||
//$item['date'] = date('Y-m-d');
|
||
//保存疼痛科聊天记录【对话记录跟着报告走】
|
||
$this->setTtChatHistory($sub_content,$user['username'],$hdruserbaseinfo_id,$userbaseinfo_id,$ticket,$medical_report_id);
|
||
|
||
|
||
|
||
////统一存档(暂时不用)
|
||
//Db::table(TabConf::$fa_hdrmedical_report)->insert($item);
|
||
}
|
||
//循环修改报告id
|
||
foreach ($last_file_report as $item_info){
|
||
Db::table(TabConf::$fa_drugs_images_medical_result)
|
||
->where('id',$item_info['id'])
|
||
->data(['reportid'=>$medical_report_id])
|
||
->update();
|
||
}
|
||
|
||
return json(Tools::set_ok(['base_data'=>$content,'res_content'=>$res_content,'medical_report_id'=>$medical_report_id]));
|
||
}catch (\Exception $e){
|
||
Tools::error_txt_log($e);
|
||
return json(Tools::set_fail('新增报告失败.'.$e->getMessage()));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* desc:保存疼痛科聊天记录
|
||
* author:wh
|
||
* @param $sub_content
|
||
*/
|
||
private function setTtChatHistory($sub_content,$username,$hdruserbaseinfo_id,$userbaseinfo_id,$ticket,$reportid){
|
||
//$content = [
|
||
// //["role" => "user", "content" => '']
|
||
//];
|
||
$data = [];
|
||
foreach ($sub_content as $item){
|
||
$content = explode(':',$item['content']);
|
||
$d = [
|
||
'username'=>$username,
|
||
'type'=>empty($content[0])?'':$content[0],
|
||
'chat_msg'=>$item['content'],
|
||
'ticket'=>$ticket,
|
||
'userbaseinfo_id'=>$userbaseinfo_id,
|
||
'hdruserbaseinfo_id'=>$hdruserbaseinfo_id,
|
||
'reportid'=>$reportid,
|
||
];
|
||
$data[] = $d;
|
||
}
|
||
Db::table(TabConf::$fa_tt_chathistory)->insertAll($data);
|
||
}
|
||
|
||
/**
|
||
* desc:查询报告详情
|
||
*
|
||
* api/Reporttt/getReportById
|
||
* 参数:dataid 报告id
|
||
* ticket:用户ticket
|
||
* author:wh
|
||
*/
|
||
function getReportById(){
|
||
return Mmodel::catchJson(function (){
|
||
$info = Db::table('fa_tt_medical_report')
|
||
->where('id',input('dataid'))
|
||
->find();
|
||
return Tools::set_ok($info);
|
||
});
|
||
}
|
||
/**
|
||
* desc:查询报告列表
|
||
*
|
||
* api/Reporttt/getReportList
|
||
* 参数:phone
|
||
*
|
||
* author:wh
|
||
*/
|
||
function getReportList(){
|
||
return Mmodel::catchJson(function (){
|
||
$phone = input('phone');
|
||
if(empty( $phone)){
|
||
return Tools::set_fail( 'phone is empty');
|
||
}
|
||
$info = Db::table('fa_tt_medical_report')
|
||
->where('phone',$phone)
|
||
->select();
|
||
foreach ($info as &$item){
|
||
$item['hdruserbaseinfo_id_data'] = Db::table(TabConf::$fa_tt_userbaseinfo)
|
||
->where('id',$item['hdruserbaseinfo_id'])
|
||
->find();
|
||
}
|
||
return Tools::set_ok($info);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 修改报告
|
||
* api/Reporttt/editReport
|
||
* 参数:dataid 报告数据id
|
||
name 病人姓名
|
||
gender 病人性别: M=男性, F=女性
|
||
age 病人年龄
|
||
main_complaint 主诉
|
||
medical_history 病史
|
||
past_history 既往史
|
||
allergy_history 过敏史
|
||
family_history 家族史
|
||
personal_history 个人史
|
||
menstrual_marital_history 月经婚育史
|
||
diagnosis 诊断
|
||
treatment 医嘱
|
||
*/
|
||
// function editReport(){
|
||
// return Mmodel::catchJson(function (){
|
||
// $dataid = input('dataid');
|
||
// if(empty($dataid)){
|
||
// return Tools::set_fail('dataid必须');
|
||
// }
|
||
// //$ticket = input('ticket');
|
||
// //if(empty($ticket)){
|
||
// // return Tools::set_fail('ticket必须');
|
||
// //}
|
||
// //修改基本信息
|
||
// $data = [
|
||
// //'username'=>$user['username'],//医生
|
||
// 'name'=>input('name',''),//病人姓名
|
||
// 'gender'=>input('gender',''),//病人性别: M=男性, F=女性
|
||
// 'age'=>input('age',''),//病人年龄
|
||
// 'main_complaint'=>input('main_complaint',''),//主诉
|
||
// 'medical_history'=>input('medical_history',''),//病史
|
||
// 'past_history'=>input('past_history',''),//既往史
|
||
// 'allergy_history'=>input('allergy_history',''),//过敏史
|
||
// 'family_history'=>input('family_history',''),//家族史
|
||
// 'personal_history'=>input('personal_history',''),//个人史
|
||
// 'menstrual_marital_history'=>input('menstrual_marital_history',''),//月经婚育史
|
||
// 'diagnosis'=>input('diagnosis',''),//诊断
|
||
// 'treatment'=>input('treatment',''),//医嘱
|
||
// ];
|
||
// //查询报告
|
||
// $report = Db::table('fa_tt_medical_report')
|
||
// ->where('id',$dataid)
|
||
// ->find();
|
||
// if(empty($report)){
|
||
// return Tools::set_fail('报告不存在');
|
||
// }
|
||
//
|
||
// //修改基本信息
|
||
// //Mmodel::existsUpdateInsert('fa_tt_userbaseinfo',[
|
||
// // 'id'=>$report['userbaseid'],
|
||
// //],$data);
|
||
//
|
||
// //修改报告
|
||
// $str = <<<EOF
|
||
//【姓名】:{$data['name']}
|
||
//【年龄】:{$data['age']}
|
||
//【性别】:{$data['gender']}
|
||
//【主诉】:{$data['main_complaint']}
|
||
//【病史】:{$data['medical_history']}
|
||
//【既往史】:{$data['past_history']}
|
||
//【过敏史】:{$data['allergy_history']}
|
||
//【家族史】:{$data['family_history']}
|
||
//【个人史】:{$data['personal_history']}
|
||
//【月经婚育史】:{$data['menstrual_marital_history']}
|
||
//【诊断】:{$data['diagnosis']}
|
||
//【医嘱】:{$data['treatment']}
|
||
//EOF;
|
||
//
|
||
//
|
||
//
|
||
// Db::table('fa_tt_medical_report')
|
||
// ->where('id',$dataid)
|
||
// ->data(['report_content'=>$str])
|
||
// ->update();
|
||
//
|
||
// return Tools::set_ok();
|
||
// });
|
||
// }
|
||
} |