PatientController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Yuanhang Liu
  5. * Date: 20-10-15
  6. * Time: 下午8:46
  7. */
  8. namespace App\Http\Controllers\Api\V2;
  9. use App\Models\Docter;
  10. use App\Models\Order;
  11. use App\Models\Patient;
  12. use App\Models\CallLog;
  13. use App\Models\OrderPatient;
  14. use App\Models\DocterOrganization;
  15. use App\Models\SchedulePeriod;
  16. use App\Models\Suggest;
  17. use App\Models\SystemConfig;
  18. use App\Models\User;
  19. use App\Models\UserMessages;
  20. use App\Models\Organization;
  21. use App\Http\Controllers\Api\V2\CommonController as Commons;
  22. use Illuminate\Support\Facades\DB;
  23. use App\Models\Axb;
  24. use App\Models\CallPhone;
  25. /**
  26. * 咨询订单用户类
  27. * Class PatientController
  28. * @package App\Http\Controllers\Api\V2
  29. */
  30. class PatientController extends AuthController
  31. {
  32. protected $relationSearch = true;
  33. /**
  34. * @return mixed
  35. * 患者咨询列表
  36. * @author Yuanhang Liu & Xiaoyun Liu
  37. */
  38. public function orderPatientList()
  39. {
  40. $req = request()->post();
  41. $this->validate(request(), [
  42. 'curPage' => 'required|integer',
  43. 'pageSize' => 'required|integer',
  44. ]);
  45. $pageSize = ($req['curPage']-1)*$req['pageSize'];
  46. $user = $this->user;
  47. $doctor_id = $user['id'];
  48. $where = [];
  49. $order=['created_at','desc'];
  50. $where['docter_id'] = $doctor_id;
  51. if (isset($req['appointment']) && $req['appointment']==1){
  52. // 门诊预约
  53. $where['product_type'] = 3;
  54. if (isset($req['timeSort']) && $req['timeSort']=='0') {
  55. $order = ['order_patients.appoint_start_time','asc']; //预约时间正序
  56. }
  57. if (isset($req['timeSort']) && $req['timeSort']=='1') {
  58. $order= ['order_patients.appoint_start_time','desc']; //预约时间倒叙
  59. }
  60. if (isset($req['timeSort']) && $req['timeSort']=='2') {
  61. $order = ['created_at','asc']; //下单时间正序
  62. }
  63. if (isset($req['timeSort']) && $req['timeSort']=='3') {
  64. $order = ['created_at','desc']; //下单时间倒叙
  65. }
  66. if (isset($req['comStatus']) && $req['comStatus']=='0') {
  67. $comstatus = [1,2,3];
  68. $where[] = [function($query) use ($comstatus){
  69. $query->whereIn('order_status', $comstatus);//处理中
  70. }];
  71. }
  72. if (isset($req['comStatus']) && $req['comStatus']=='1') {
  73. $where['order_status'] = 4; //已完成
  74. }
  75. }else{
  76. // 图文和电话的
  77. $ids = [1,2];
  78. $where[] = [function($query) use ($ids){
  79. $query->whereIn('product_type', $ids);
  80. }];
  81. if(isset($req['typeAll']) && $req['typeAll']==1){
  82. $where['product_type']=2;//图文咨询
  83. }
  84. if(isset($req['typeAll']) && $req['typeAll']==2){
  85. $where['product_type']=1;//电话咨询
  86. }
  87. if(isset($req['processing']) && $req['processing']==0){
  88. $status = [1,2,3];
  89. $where[] = [function($query) use ($status){
  90. $query->whereIn('order_status', $status);//处理中
  91. }];
  92. }
  93. if(isset($req['processing']) && $req['processing']==1){
  94. $where['order_status'] = 4; //历史记录,已完成订单
  95. }
  96. }
  97. // $data = Order::With(['orderPatient'=>function($query) use ($orders){
  98. // $query->orderBy($orders[0],$orders[1]);
  99. // }])->where($where)->orderBy($order[0],$order[1])->skip($pageSize)->take($req['pageSize'])->groupBy('id')->get()->toArray();
  100. $data = DB::table('orders')
  101. ->leftJoin('order_patients','order_patients.order_id','=','orders.id')
  102. ->select(['orders.*','order_patients.birthday as obirthday','order_patients.appoint_start_time','order_patients.name as oanme'])
  103. ->where($where)->orderBy($order[0],$order[1])
  104. ->skip($pageSize)->take($req['pageSize'])
  105. ->groupBy('id')
  106. ->get()->toArray();
  107. // $data = Order::with('orderPatient')->where($where)->skip($pageSize)->take($req['pageSize'])->groupBy('id')->toSql();
  108. // $data = OrderPatient::with("order")->where('id', $user['id'])->orderBy('id', 'desc')->limit($pageSize,$req['pageSize'])->get()->toarray();
  109. $list = [];
  110. try {
  111. foreach ($data as $k=>$v){
  112. $v = get_object_vars($v);
  113. if (!$v['obirthday']){
  114. unset($list[$k]);
  115. }else{
  116. $list[$k]['id'] = $v['id'];
  117. $list[$k]['user_id'] = $v['user_id'];
  118. $list[$k]['order_sn'] = $v['order_sn'];
  119. $list[$k]['zl'] = $v['product_type'];
  120. $list[$k]['zt'] = $v['order_status'];
  121. unset($list[$k]['order_status'],$list[$k]['product_type']);
  122. $list[$k]['created_at'] = $v['created_at'];
  123. $list[$k]['appoint_time'] = empty($v['appoint_start_time'])?'---':date('Y-m-d H:i:s',$v['appoint_start_time']);
  124. $list[$k]['name'] = $v['oanme'];
  125. $list[$k]['birthday'] = numBirthday($v['obirthday']);
  126. }
  127. }
  128. return out($list);
  129. }catch (\Exception $e){
  130. return out($e->getFile().'中第 '.$e->getLine().'行发生了 '.$e->getMessage().'错误');
  131. }
  132. return out($list);
  133. }
  134. public function mzPutOrder(){
  135. }
  136. /**
  137. * @return mixed
  138. * 完成订单
  139. * @author Yuanhang Liu & Xiaoyun Liu
  140. */
  141. public function orderPatientok()
  142. {
  143. $req = request()->post();
  144. $this->validate(request(), [
  145. 'order_id|订单id' => 'required|integer',
  146. 'pathogen|病因' => 'required',
  147. 'suggest|建议' => 'required',
  148. 'patient_id|患者ID' => 'required',
  149. ]);
  150. $user = $this->user;
  151. $find = Order::where('id','=',$req['order_id'])->first()->toArray();
  152. if (!$find){
  153. return out('',500,'订单错误!');
  154. }
  155. if ($find['order_status']>3){
  156. return out('',500,'此订单不可操作!');
  157. }
  158. $lable = '';
  159. switch ($find['product_type']){
  160. case 1:
  161. $lable = '电话咨询';
  162. break;
  163. case 2:
  164. $lable = '图文咨询';
  165. break;
  166. case 3:
  167. $lable = '门诊预约';
  168. break;
  169. case 4:
  170. $lable = '疫苗接种预约';
  171. break;
  172. case 5:
  173. $lable = '儿保预约';
  174. break;
  175. case 6:
  176. $lable = '服务包';
  177. break;
  178. default:
  179. $lable = '参数错误!';
  180. }
  181. $relation_id = '';
  182. if ($find['product_type'] == 1 || $find['product_type'] == 2) {
  183. // 需要有意见单的
  184. $suggests = Suggest::create([
  185. 'order_id'=> $find['id'],
  186. 'user_id'=> $find['user_id'],
  187. 'symptoms'=> $req['symptoms'],
  188. 'pathogen'=> $req['pathogen'],
  189. 'suggest'=> $req['suggest'],
  190. 'patient_id'=> $req['patient_id']
  191. ]);
  192. $suggests = $suggests->toArray();
  193. $relation_id= $suggests['id'];
  194. }else{
  195. $relation_id= 0;
  196. }
  197. $status = $find['product_type'];
  198. $lab='';
  199. $mesid = $relation_id;
  200. if ($status==1){
  201. $lab = '电话咨询';
  202. }else if ($status==2){
  203. $lab = '图文咨询';
  204. }else if($status==3){
  205. $mesid = $find['id'];
  206. $lab = '门诊预约';
  207. }
  208. $order_sn = $find['order_sn'];
  209. $doctername = $user['name'];
  210. DB::beginTransaction();
  211. try {
  212. Order::where('id', '=', $req['order_id'])->update(['order_status' => 4, 'updated_at' => date('Y-m-d H:i:s', time()), 'end_time' => time()]);
  213. Docter::where('id', '=', $user['id'])->increment('service_persons');
  214. // 添加到用户记录中!
  215. UserMessages::create([
  216. 'user_id'=>$find['user_id'],
  217. 'docter_id'=>$user['id'],
  218. 'status'=>1,
  219. 'type'=>2,
  220. 'relation_id'=>$mesid,
  221. 'content'=>"您的 ".$lab." 订单,订单号".$order_sn.",医生".$doctername."已经确认完成(点击查看意见单)",
  222. ]);
  223. DB::commit();
  224. }catch (\Exception $e){
  225. DB::rollBack();
  226. return out('',500,$e->getMessage());
  227. }catch (\PDOException $e){
  228. DB::rollBack();
  229. return out('',500,$e->getMessage());
  230. }
  231. $axbwhere['docter_id'] = $user['id'];
  232. $axbwhere['user_id'] = $req['user_id'];
  233. $finds = Axb::where($axbwhere)->orderBy('id','desc')->first();
  234. if ($status==1 && $finds){
  235. $unlok = (new Commons())->unLokPhone($finds['xphone'],$finds['subs_id']);
  236. Axb::where(['subs_id'=>$finds['subs_id']])->delete();
  237. }
  238. return out();
  239. }
  240. /**
  241. * 门诊预约完成订单
  242. * @return mixed
  243. * @throws \Exception
  244. */
  245. public function mzPatientok()
  246. {
  247. $req = request()->post();
  248. $this->validate(request(), [
  249. 'order_id|订单id' => 'required|integer',
  250. ]);
  251. $user = $this->user;
  252. $find = Order::where('id','=',$req['order_id'])->first()->toArray();
  253. if (!$find){
  254. return out('',500,'订单错误!');
  255. }
  256. if ($find['order_status']>3){
  257. return out('',500,'此订单不可操作!');
  258. }
  259. $lable = '';
  260. switch ($find['product_type']){
  261. case 1:
  262. $lable = '电话咨询';
  263. break;
  264. case 2:
  265. $lable = '图文咨询';
  266. break;
  267. case 3:
  268. $lable = '门诊预约';
  269. break;
  270. case 4:
  271. $lable = '疫苗接种预约';
  272. break;
  273. case 5:
  274. $lable = '儿保预约';
  275. break;
  276. case 6:
  277. $lable = '服务包';
  278. break;
  279. default:
  280. $lable ='参数错误!';
  281. }
  282. $relation_id= 0;
  283. $status = $find['product_type'];
  284. $lab='';
  285. $mesid = $relation_id;
  286. if ($status==1){
  287. $lab = '电话咨询';
  288. }else if ($status==2){
  289. $lab = '图文咨询';
  290. }else if($status==3){
  291. $mesid = $find['id'];
  292. $lab = '门诊预约';
  293. }
  294. $order_sn = $find['order_sn'];
  295. $doctername = $user['name'];
  296. DB::beginTransaction();
  297. try {
  298. Order::where('id','=',$req['order_id'])->update(['order_status'=>4,'updated_at'=>date('Y-m-d H:i:s',time()),'end_time'=>time()]);
  299. Docter::where('id','=',$user['id'])->increment('service_persons');
  300. // 添加到用户记录中!
  301. UserMessages::create([
  302. 'user_id'=>$find['user_id'],
  303. 'docter_id'=>$user['id'],
  304. 'status'=>1,
  305. 'type'=>2,
  306. 'relation_id'=>$mesid,
  307. 'content'=>"您的 ".$lab." 订单,订单号".$order_sn.",医生".$doctername."已经确认完成(点击查看订单)",
  308. ]);
  309. DB::commit();
  310. }catch (\Exception $e){
  311. DB::rollBack();
  312. return out('',500,$e->getMessage());
  313. }catch (\PDOException $e){
  314. DB::rollBack();
  315. return out('',500,$e->getMessage());
  316. }
  317. return out();
  318. }
  319. /**
  320. * @return mixed
  321. * 接单
  322. * @author Yuanhang Liu & Xiaoyun Liu
  323. */
  324. public function putOrderPatient(){
  325. $req = request()->post();
  326. $this->validate(request(), [
  327. 'order_id|订单id' => 'required|integer',
  328. ]);
  329. $user = $this->user;
  330. $find = Order::where('id','=',$req['order_id'])->first()->toArray();
  331. $status = $find['product_type'];
  332. $lab='';
  333. if ($status==1){
  334. $lab = '电话咨询';
  335. }else if ($status==2){
  336. $lab = '图文咨询';
  337. }else if($status==3){
  338. $lab = '门诊预约';
  339. }
  340. $order_sn = $find['order_sn'];
  341. $order_id = $find['id'];
  342. $doctername = $user['name'];
  343. // 1.电话咨询 2.图文咨询 3.门诊预约 4.疫苗接种预约 5.儿保预约 6.服务包 7.充值
  344. if (!$find){
  345. return out('',500,'订单错误!');
  346. }
  347. if ($find['order_status']!=2){
  348. return out('',500,'此订单已被接单或未付款!');
  349. }
  350. if ($find['payment_status']>=4){
  351. return out('',500,'此订单可能已完成!');
  352. }
  353. DB::beginTransaction();
  354. try {
  355. Order::where('id','=',$req['order_id'])->update(['order_status'=>3,'updated_at'=>date('Y-m-d H:i:s',time()),'receiving_time'=>time()]);
  356. // 添加到用户记录中!
  357. UserMessages::create([
  358. 'user_id'=>$find['user_id'],
  359. 'docter_id'=>$user['id'],
  360. 'status'=>1,
  361. 'type'=>1,
  362. 'relation_id'=>$order_id,
  363. 'content'=> "您的 ".$lab." 订单,订单号".$order_sn.",医生".$doctername."已经确认接单(点击查看订单详情)",
  364. ]);
  365. DB::commit();
  366. return out();
  367. }catch (\Exception $e){
  368. DB::rollBack();
  369. return out('',500,$e->getMessage());
  370. }catch (\PDOException $e){
  371. DB::rollBack();
  372. return out('',500,$e->getMessage());
  373. }
  374. }
  375. /**
  376. * @return mixed
  377. * 患者咨询详情
  378. * @author Yuanhang Liu & Xiaoyun Liu
  379. */
  380. public function orderPatientDetail()
  381. {
  382. $req = request()->post();
  383. $this->validate(request(), [
  384. 'patient_id' => 'required|integer',
  385. ]);
  386. $data = [];
  387. // 订单查询
  388. $data = Order::with(['orderPatient','user','calllog'])->where('id', $req['patient_id'])->first()->toArray();
  389. // 通话记录查询
  390. $datas = CallLog::where('order_id', $req['patient_id'])->get();
  391. // 机构查询
  392. $user = $this->user;
  393. $doctor_id = (new Commons)->getUserIdByDoctorId($user['phone']);
  394. // 返回数组
  395. $res_patient = [];
  396. //电话咨询
  397. if($data['product_type']==1){
  398. $res_patient['patient_id'] = $data['order_patient']['id'];
  399. $res_patient['user_id'] = $data['user_id'];
  400. $res_patient['order_sn'] = $data['order_sn'];
  401. $res_patient['patient_id'] = $data['patient_id'];
  402. $res_patient['product_type'] = $data['product_type'];
  403. $res_patient['name']=$data['order_patient']['name'];//患者姓名
  404. $res_patient['numbirthday']=numBirthday($data['order_patient']['birthday']);//年龄
  405. $res_patient['card_number']=$data['order_patient']['card_number'];//身份证号
  406. $res_patient['created_at']=$data['created_at'];//下单时间
  407. $res_patient['nickname']=$data['user']['nickname'];//下单用户
  408. $res_patient['receiving_time']=!empty($data['receiving_time'])?date('Y-m-d H:i:s',$data['receiving_time']):'---';//接单时间
  409. $res_patient['order_status']=$data['order_status'];//订单状态
  410. $res_patient['call_list']=[];//通话记录
  411. $res_patient['one_call'] = '';
  412. if ($data['calllog']){
  413. $res_patient['one_call'] = $data['calllog'][0]['call_time'];
  414. $res_patient['secret_no']=$data['calllog'][count($data['calllog'])-1]['secret_no'];//X号码
  415. foreach ($data['calllog'] as $ks=>$vs){
  416. $res_patient['call_list'][$ks]['frequency']='通话'.($ks+=1);//拨打电话开始时间
  417. $res_patient['call_list'][$ks]['start']=$vs['call_time'];//拨打电话开始时间
  418. $res_patient['call_list'][$ks]['end']=$vs['ring_time'];//拨打电话结束时间
  419. $res_patient['call_list'][$ks]['duration']=gmdate('i:s',$vs['talk_time']);//拨打电话结束时间
  420. }
  421. }
  422. }
  423. //图文咨询
  424. if($data['product_type']==2){
  425. $res_patient['order_sn'] = $data['order_sn'];
  426. $res_patient['patient_id'] = $data['patient_id'];
  427. $res_patient['user_id'] = $data['user_id'];
  428. $res_patient['user_name'] = $data['user']['nickname'];
  429. $res_patient['user_avatar'] = $data['user']['avatar'];
  430. $res_patient['sex'] = $data['order_patient']['sex'];
  431. $res_patient['product_type'] = $data['product_type'];
  432. $res_patient['name']=$data['order_patient']['name'];
  433. $res_patient['numbirthday']=numBirthday($data['order_patient']['birthday']);
  434. $res_patient['card_number']=$data['order_patient']['card_number'];
  435. $res_patient['symptoms']=$data['order_patient']['symptoms'];//病情描述
  436. $res_patient['medical_imgs']=json_decode($data['order_patient']['medical_imgs'],true);//病情照片
  437. $res_patient['created_at']=$data['created_at'];//下单时间
  438. $res_patient['nickname']=$data['user']['nickname'];//下单用户
  439. $res_patient['receiving_time']=!empty($data['receiving_time'])?date('Y-m-d H:i:s',$data['receiving_time']):'---'; //接单时间
  440. $res_patient['order_status']=$data['order_status'];//接单时间
  441. }
  442. //门诊预约
  443. if($data['product_type']==3){
  444. $res_patient['order_sn'] = $data['order_sn'];
  445. $res_patient['patient_id'] = $data['patient_id'];
  446. $res_patient['product_type'] =$data['product_type'];
  447. $res_patient['name']=$data['order_patient']['name']; // 患者
  448. $res_patient['numbirthday']=numBirthday($data['order_patient']['birthday']); //年龄
  449. $res_patient['card_number']=$data['order_patient']['card_number']; // 证件号
  450. $res_patient['appoint_time'] = date('Y年m月d日 H:i',$data['order_patient']['appoint_start_time']);// 预约时间
  451. $res_patient['order_status'] = $data['order_status'];// 订单状态
  452. $organization = Organization::where('id','=',$data['organization_id'])->first();
  453. if($organization){
  454. $organization = $organization->toArray();
  455. $res_patient['organization']= $organization['name']; // 门诊机构
  456. }else{
  457. $res_patient['organization']= ''; // 门诊机构
  458. }
  459. $res_patient['order_sn']=$data['order_sn'];//订单号
  460. $res_patient['created_at']=$data['created_at'];//下单时间
  461. $res_patient['nickname']=$data['user']['nickname'];//下单用户
  462. }
  463. return out($res_patient);
  464. }
  465. /**
  466. * 拨打电话/绑定电话
  467. * @return \Illuminate\Http\JsonResponse
  468. * @author Liu-Yh
  469. * Create By 2020/11/24 19:19
  470. */
  471. public function callPhones(){
  472. $req = request()->post();
  473. $user = $this->user;
  474. $this->validate(request(), [
  475. 'order_id' => 'required',
  476. 'user_id' => 'required|integer',
  477. ]);
  478. $docter_id = $user['id'];
  479. $docter_phone = $user['phone'];
  480. if(!$docter_phone){
  481. return out('',500,'医生电话不存在!');
  482. }
  483. $find = Order::with('orderPatient')->where('order_sn',$req['order_id'])->first()->toArray();
  484. if (empty($find['order_patient'])){
  485. return out('',500,'患者电话不存在');
  486. }
  487. $phone = $find['order_patient']['phone'];
  488. if($docter_phone==$phone){
  489. return out('',500,'医生和患者电话号不能一样!');
  490. }
  491. $wheres['docter_id'] = $docter_id;
  492. $wheres['user_id'] = $req['user_id'];
  493. $commons = new Commons();
  494. $finds = Axb::where($wheres)->orderBy('id','desc')->first();
  495. if ($finds){
  496. $querylok = $commons->QuerySubsId($finds['xphone']);
  497. if ($querylok['Code']=='OK'){
  498. // 可能是数组
  499. $new_arr = explode(',',$querylok['SubsId']);
  500. foreach ($new_arr as $v){
  501. $queryCallStatus = $commons->QuerySubscriptionDetail($finds['xphone'],$v);
  502. if ($queryCallStatus['Code']=="OK"){
  503. if ($queryCallStatus['SecretBindDetailDTO']['PhoneNoA']==$docter_phone&&$queryCallStatus['SecretBindDetailDTO']['PhoneNoB']==$phone){
  504. return out($finds['xphone']);
  505. }else{
  506. if ($phone){
  507. Axb::where('id',$finds['id'])->delete();
  508. return $this->createCall($phone,$docter_phone,$docter_id,$req['user_id']);
  509. }else{
  510. return out('',500,'患者电话不存在');
  511. }
  512. }
  513. }else{
  514. if ($phone){
  515. Axb::where('id',$finds['id'])->delete();
  516. return $this->createCall($phone,$docter_phone,$docter_id,$req['user_id']);
  517. }else{
  518. return out('',500,'患者电话不存在');
  519. }
  520. }
  521. }
  522. }else{
  523. if ($phone){
  524. Axb::where('id',$finds['id'])->delete();
  525. return $this->createCall($phone,$docter_phone,$docter_id,$req['user_id']);
  526. }else{
  527. return out('',500,'患者电话不存在');
  528. }
  529. }
  530. }else{
  531. if ($phone){
  532. return $this->createCall($phone,$docter_phone,$docter_id,$req['user_id']);
  533. }else{
  534. return out('',500,'患者电话不存在');
  535. }
  536. }
  537. }
  538. /**
  539. * 创建电话
  540. * @param $phone
  541. * @param $docter_phone
  542. * @param $docter_id
  543. * @param $user_id
  544. * @return \Illuminate\Http\JsonResponse
  545. */
  546. protected function createCall($phone,$docter_phone,$docter_id,$user_id){
  547. $commons = new Commons();
  548. $callModel = $commons->BindAxb($docter_phone,$phone);
  549. if ($callModel['Code']=="OK"){
  550. Axb::create([
  551. 'docter_id'=>$docter_id,
  552. 'user_id'=>$user_id,
  553. 'xphone'=>$callModel['SecretBindDTO']['SecretNo'],
  554. 'subs_id'=>$callModel['SecretBindDTO']['SubsId'],
  555. 'createtime'=>time(),
  556. ]);
  557. return out($callModel['SecretBindDTO']['SecretNo']);
  558. }else{
  559. return out($callModel);
  560. }
  561. }
  562. /**
  563. * 电话随访
  564. * @return \Illuminate\Http\JsonResponse
  565. * @author Liu-Yh
  566. * Create By 2020/11/24 19:19
  567. */
  568. public function callPhoneSure(){
  569. $req = request()->post();
  570. $user = $this->user;
  571. $this->validate(request(), [
  572. 'user_id' => 'required|integer',
  573. ]);
  574. $docter_id = $user['id'];
  575. $docter_phone = $user['phone'];
  576. if(!$docter_phone){
  577. return out('',500,'医生电话不存在!');
  578. }
  579. $find = User::where('id',$req['user_id'])->first()->toArray();
  580. $phone = $find['phone'];
  581. $wheres['docter_id'] = $docter_id;
  582. $wheres['user_id'] = $find['id'];
  583. $commons = new Commons();
  584. $finds = Axb::where($wheres)->orderBy('id','desc')->first();
  585. if ($finds){
  586. $querylok = $commons->QuerySubsId($finds['xphone']);
  587. if ($querylok['Code'] == 'OK') {
  588. $queryCallStatus = $commons->QueryCallStatus($phone, $querylok['SubsId']);
  589. if ($queryCallStatus['Code'] == 'OK') {
  590. if ($queryCallStatus['SecretCallStatusDTO'] != 4) {
  591. return out($finds['xphone']);
  592. } else {
  593. if ($phone) {
  594. $callModel = $commons->BindAxb($docter_phone, $phone);
  595. if ($callModel['Code'] == "OK") {
  596. return out($callModel['SecretBindDTO']['SecretNo']);
  597. }
  598. } else {
  599. return out('', 500, '患者电话不存在');
  600. }
  601. }
  602. }
  603. }
  604. } else {
  605. if ($phone) {
  606. $callModel = $commons->BindAxb($docter_phone, $phone);
  607. if ($callModel['Code'] == "OK") {
  608. Axb::create([
  609. 'docter_id' => $docter_id,
  610. 'user_id' => $req['user_id'],
  611. 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
  612. 'createtime' => time(),
  613. ]);
  614. return out($callModel['SecretBindDTO']['SecretNo']);
  615. } else {
  616. return out($callModel);
  617. }
  618. } else {
  619. return out('', 500, '患者电话不存在');
  620. }
  621. }
  622. }
  623. /**
  624. * 取消订单接口
  625. * @throws \Illuminate\Validation\ValidationException
  626. */
  627. public function cancelOrder()
  628. {
  629. $req = request()->post();
  630. $this->validate(request(), [
  631. 'order_id' => 'required|integer',
  632. ]);
  633. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
  634. if ($order['order_status'] == 2 && $order['payment_status'] == 2) {
  635. DB::beginTransaction();
  636. try {
  637. if ($order['product_type'] == 3) {
  638. Order::where('id',$req['order_id'])->update(['order_status'=>5,'payment_status'=>5,'order_notes'=>'医生拒绝接单']);
  639. }else{
  640. //退钱到余额
  641. if (!empty($order['payment_amount'])) {
  642. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '医生拒绝接单');
  643. }
  644. Order::where('id', $req['order_id'])->update(['order_status' => 5, 'order_notes' => '医生拒绝接单', 'payment_status' => 4]);
  645. }
  646. DB::commit();
  647. return out('', 200, '订单取消成功');
  648. } catch (\Exception $e) {
  649. DB::rollBack();
  650. return out('', 500, $e->getMessage());
  651. } catch (\PDOException $e) {
  652. DB::rollBack();
  653. return out('', 500, $e->getMessage());
  654. }
  655. } else {
  656. return out('', 500, '订单不可取消');
  657. }
  658. }
  659. /**
  660. * 订单超时自动完成(定时)
  661. */
  662. public function overTimeOrers(){
  663. $user = $this->user;
  664. $docter_id = $user['id'];
  665. $config_chat = SystemConfig::get('docter_config','chat_complete_time');
  666. $config_phone = SystemConfig::get('docter_config','phone_complete_time');
  667. // 换算为秒
  668. $config_chat = $config_chat*60;
  669. $config_phone = $config_phone*60;
  670. $inOrder = Order::with('orderPatient')->where(['docter_id'=>$docter_id,'order_status'=>3,'payment_status'=>2])->get();
  671. $catNewIds = [];
  672. $menNewIds = [];
  673. foreach ($inOrder as $k=>$v){
  674. if ($v['product_type']==1){
  675. if ((time()-$v['receiving_time'])>=$config_chat){
  676. $catNewIds[$k] = $v['id'];
  677. }
  678. }else if($v['product_type']==2){
  679. if ((time()-$v['receiving_time'])>=$config_phone){
  680. $catNewIds[$k] = $v['id'];
  681. }
  682. }else if($v['product_type']==3){
  683. if ((time()-$v['receiving_time'])>=(1*60*60*24)){
  684. $menNewIds[$k] = $v['id'];
  685. }
  686. }
  687. }
  688. if ($catNewIds || $menNewIds){
  689. // 操作图文和电话订单为已完成
  690. Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
  691. // 操作门诊订单为已超时
  692. Order::whereIn('id',$menNewIds)->update(['order_status'=>6]);
  693. }
  694. }
  695. /**
  696. * 获取病例信息接口!
  697. * @author Liu
  698. * @return \Illuminate\Http\JsonResponse
  699. * @throws \Illuminate\Validation\ValidationException
  700. */
  701. public function CaseAcquisition()
  702. {
  703. $req = request()->post();
  704. $this->validate(request(), [
  705. 'user_id' => 'required|integer',
  706. 'order_id',
  707. 'is_showAll'=>'required|integer'
  708. ]);
  709. $user = $this->user;
  710. $docter_id = $user['id'];
  711. // 订单内点击
  712. if (isset($req['order_id'])&&!empty($req['order_id'])){
  713. $data = Order::with(['orderPatient','user'])->where('id',$req['order_id'])->first();
  714. }else{
  715. $data = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2,'order_status'=>3])->orderBy('id','desc')->first();
  716. }
  717. // 返回数组
  718. $res_patient = [];
  719. if ($req['is_showAll']==1){
  720. $list = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2])->orderBy('id','asc')->get();
  721. if ($list){
  722. $list = $list->toArray();
  723. foreach ($list as $k=>$v){
  724. $res_patient['list'][$k]['order_sn'] = $v['order_sn'];
  725. $res_patient['list'][$k]['user_id'] = $v['user_id'];
  726. $res_patient['list'][$k]['user_name'] = $v['user']['nickname'];
  727. $res_patient['list'][$k]['user_avatar'] = $v['user']['avatar'];
  728. $res_patient['list'][$k]['sex'] = $v['order_patient']['sex'];
  729. $res_patient['list'][$k]['name'] = $v['order_patient']['name'];
  730. $res_patient['list'][$k]['numbirthday'] = numBirthday($v['order_patient']['birthday']);
  731. $res_patient['list'][$k]['symptoms'] = $v['order_patient']['symptoms'];//病情描述
  732. $res_patient['list'][$k]['medical_imgs'] = json_decode($v['order_patient']['medical_imgs'], true);//病情照片
  733. }
  734. }
  735. }
  736. if ($data) {
  737. $data = $data->toArray();
  738. $res_patient['data']['order_sn'] = $data['order_sn'];
  739. $res_patient['data']['user_id'] = $data['user_id'];
  740. $res_patient['data']['user_name'] = $data['user']['nickname'];
  741. $res_patient['data']['user_avatar'] = $data['user']['avatar'];
  742. $res_patient['data']['sex'] = $data['order_patient']['sex'];
  743. $res_patient['data']['name'] = $data['order_patient']['name'];
  744. $res_patient['data']['numbirthday'] = numBirthday($data['order_patient']['birthday']);
  745. $res_patient['data']['symptoms'] = $data['order_patient']['symptoms'];//病情描述
  746. $res_patient['data']['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
  747. }
  748. return out($res_patient);
  749. }
  750. }