PatientController.php 41 KB

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