413 lines
14 KiB
PHP
413 lines
14 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;
|
||
|
||
/**
|
||
* h5-数字人-报告
|
||
* Class Reporth5
|
||
* @package app\api\controller
|
||
*/
|
||
class Reporth5 extends BaseHttpApi
|
||
{
|
||
public function __construct()
|
||
{
|
||
|
||
//if(!Usersh5::checkLogin()){
|
||
// echo json_encode(['code'=>591,'msg'=>'未登录']);exit;
|
||
//}
|
||
}
|
||
|
||
/**
|
||
* desc:h5-数字人-生成听诊报告(同时提交固定问答聊天记录)
|
||
*
|
||
* api/Reporth5/createReport
|
||
*
|
||
* 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网页问诊
|
||
*
|
||
* 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');
|
||
|
||
$uniqueid = input('uniqueid');
|
||
if(empty($uniqueid)){
|
||
return json(Tools::set_fail('uniqueid必须'));
|
||
}
|
||
|
||
|
||
Tools::log_to_write_txt(['input'=>input()]);
|
||
$config = config('ai_num_report_config');
|
||
|
||
$question = '';//input('question','');
|
||
|
||
$chatobj = new ChatGPT();
|
||
$chatobj->url = $config['base_url'];
|
||
$chatobj->model = '';
|
||
$chatobj->apiKey = $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 = 'h5';
|
||
$qa_type = 'fix';
|
||
if(empty($qa_type)){
|
||
return json(Tools::set_fail('qa_type问答模式必须,可选项:问答模式:free=自由问答,fix=固定问答'));
|
||
}
|
||
//用户
|
||
$user = Db::table(TabConf::$fa_hdrusersh5)->where('ticket',$ticket)->find();
|
||
if(empty($user)){
|
||
return json(Tools::set_res(531,'用户不存在'));
|
||
}
|
||
//查询医生所属部门
|
||
$hdrdoctorusers = Db::table(TabConf::$fa_hdrdoctorusers)
|
||
->where('username',$user['doctor'])
|
||
->find();
|
||
if(empty($hdrdoctorusers)){
|
||
return json(Tools::set_fail('医生不存在'));
|
||
}
|
||
//部门id
|
||
$hdrdepartment_id = $hdrdoctorusers['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(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,//科室
|
||
];
|
||
//$uniqueid = md5(json_encode($basedata));
|
||
//查询报告
|
||
$report = Db::table('fa_tt_medical_report')
|
||
->where('uniqueid',$uniqueid)
|
||
->find();
|
||
if($report){
|
||
return json(Tools::set_fail('报告重复'));
|
||
}
|
||
|
||
$contentstr = input('content');
|
||
if(empty($contentstr)){
|
||
return json(Tools::set_fail('content必须'));
|
||
}
|
||
|
||
$content['name'] = '【姓名】:'.$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]);
|
||
//$content = [
|
||
// //["role" => "user", "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;
|
||
}
|
||
$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回复'=>$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'=>$hdrdoctorusers['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('没有获取到报告'));
|
||
}
|
||
|
||
Db::startTrans();
|
||
try {
|
||
foreach ($data as $item){
|
||
|
||
//新增统一基本信息
|
||
$hdruserbaseinfo_id = Db::table('fa_tt_userbaseinfo')->insertGetId($basedata);
|
||
//只新增不修改
|
||
$userbaseinfo_id = Db::table('fa_tt_userbaseinfo')->insert($basedata);
|
||
//保存疼痛科聊天记录
|
||
$this->setTtChatHistory($sub_content,$hdrdoctorusers['username'],$hdruserbaseinfo_id,$userbaseinfo_id,$ticket);
|
||
$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'] = $hdrdoctorusers['name'];
|
||
$item['qa_type'] = $qa_type;
|
||
$item['uniqueid'] = $uniqueid;
|
||
//保存报告
|
||
Db::table('fa_tt_medical_report')->insert($item);
|
||
//日期
|
||
//$item['date'] = date('Y-m-d');
|
||
|
||
|
||
////统一存档(暂时不用)
|
||
//Db::table(TabConf::$fa_hdrmedical_report)->insert($item);
|
||
}
|
||
|
||
Db::commit();
|
||
return json(Tools::set_ok(['base_data'=>$content,'res_content'=>$res_content]));
|
||
}catch (\Exception $e){
|
||
Db::rollback();
|
||
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){
|
||
//$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,
|
||
'type'=>$item['type'],
|
||
];
|
||
$data[] = $d;
|
||
}
|
||
Db::table(TabConf::$fa_tt_chathistory)->insertAll($data);
|
||
}
|
||
|
||
/**
|
||
* desc:查询报告详情
|
||
*
|
||
* api/Reporth5/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/Reporth5/getReportList
|
||
* 参数:username
|
||
* ticket:用户ticket
|
||
*
|
||
* author:wh
|
||
*/
|
||
function getReportList(){
|
||
return Mmodel::catchJson(function (){
|
||
$info = Db::table('fa_tt_medical_report')
|
||
->select();
|
||
return Tools::set_ok($info);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 修改报告
|
||
* api/Reporth5/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 (){
|
||
//$ticket = input('ticket');
|
||
//if(empty($ticket)){
|
||
// return Tools::set_fail('ticket必须');
|
||
//}
|
||
//修改基本信息
|
||
$data = [
|
||
//'username'=>$hdrdoctorusers['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',''),//医嘱
|
||
];
|
||
//修改基本信息
|
||
Mmodel::existsUpdateInsert('fa_tt_userbaseinfo',[
|
||
//'username'=>input('username',''),
|
||
'name'=>input('name',''),
|
||
],$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;
|
||
|
||
|
||
|
||
$dataid = input('dataid');
|
||
if(empty($dataid)){
|
||
return Tools::set_fail('dataid必须');
|
||
}
|
||
|
||
Db::table('fa_tt_medical_report')
|
||
->where('id',$dataid)
|
||
->data(['report_content'=>$str])
|
||
->update();
|
||
|
||
return Tools::set_ok();
|
||
});
|
||
}
|
||
} |