PatientController.php 38 KB

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