PatientController.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. $msg = $this->ReceivingReminderOK($find['id']);
  234. order_trace(['完成订单日志', '请求参数' => $find['id'] ?? '', '返回数据' => $msg ?? ''], 'info');
  235. $axbwhere['docter_id'] = $user['id'];
  236. $axbwhere['user_id'] = $find['user_id'];
  237. $finds = Axb::where($axbwhere)->orderBy('id', 'desc')->first();
  238. if ($status == 1 && $finds) {
  239. $unlok = (new Commons())->unLokPhone($finds['xphone'], $finds['subs_id']);
  240. Axb::where(['subs_id' => $finds['subs_id']])->delete();
  241. }
  242. return out();
  243. }
  244. /**
  245. * 门诊预约完成订单
  246. * @return mixed
  247. * @throws \Exception
  248. */
  249. public function mzPatientok()
  250. {
  251. $req = request()->post();
  252. $this->validate(request(), [
  253. 'order_id|订单id' => 'required|integer',
  254. ]);
  255. $user = $this->user;
  256. $find = Order::where('id', '=', $req['order_id'])->first()->toArray();
  257. if (!$find) {
  258. return out('', 500, '订单错误!');
  259. }
  260. if ($find['order_status'] > 3) {
  261. return out('', 500, '此订单不可操作!');
  262. }
  263. $lable = '';
  264. switch ($find['product_type']) {
  265. case 1:
  266. $lable = '电话咨询';
  267. break;
  268. case 2:
  269. $lable = '图文咨询';
  270. break;
  271. case 3:
  272. $lable = '门诊预约';
  273. break;
  274. case 4:
  275. $lable = '疫苗接种预约';
  276. break;
  277. case 5:
  278. $lable = '儿保预约';
  279. break;
  280. case 6:
  281. $lable = '服务包';
  282. break;
  283. default:
  284. $lable = '参数错误!';
  285. }
  286. $relation_id = 0;
  287. $status = $find['product_type'];
  288. $lab = '';
  289. $mesid = $relation_id;
  290. if ($status == 1) {
  291. $lab = '电话咨询';
  292. } else if ($status == 2) {
  293. $lab = '图文咨询';
  294. } else if ($status == 3) {
  295. $mesid = $find['id'];
  296. $lab = '门诊预约';
  297. }
  298. $order_sn = $find['order_sn'];
  299. $doctername = $user['name'];
  300. DB::beginTransaction();
  301. try {
  302. Order::where('id', '=', $req['order_id'])->update(['order_status' => 4, 'updated_at' => date('Y-m-d H:i:s', time()), 'end_time' => time()]);
  303. Docter::where('id', '=', $user['id'])->increment('service_persons');
  304. // 添加到用户记录中!
  305. UserMessages::create([
  306. 'user_id' => $find['user_id'],
  307. 'docter_id' => $user['id'],
  308. 'status' => 1,
  309. 'type' => 2,
  310. 'relation_id' => $mesid,
  311. 'content' => "您的 " . $lab . " 订单,订单号" . $order_sn . ",医生" . $doctername . "已经确认完成(点击查看订单)",
  312. ]);
  313. DB::commit();
  314. } catch (\Exception $e) {
  315. DB::rollBack();
  316. return out('', 500, $e->getMessage());
  317. } catch (\PDOException $e) {
  318. DB::rollBack();
  319. return out('', 500, $e->getMessage());
  320. }
  321. order_trace(['门诊完成订单日志', '请求参数' => $find['id'] ?? '', '返回数据' => $msg ?? ''], 'info');
  322. $this->ReceivingReminderOK($find['id']);
  323. return out();
  324. }
  325. /**
  326. * @return mixed
  327. * 接单
  328. * @author Yuanhang Liu & Xiaoyun Liu
  329. */
  330. public function putOrderPatient()
  331. {
  332. $req = request()->post();
  333. $this->validate(request(), [
  334. 'order_id|订单id' => 'required|integer',
  335. ]);
  336. $user = $this->user;
  337. $find = Order::where('id', '=', $req['order_id'])->first()->toArray();
  338. $status = $find['product_type'];
  339. $lab = '';
  340. if ($status == 1) {
  341. $lab = '电话咨询';
  342. } else if ($status == 2) {
  343. $lab = '图文咨询';
  344. } else if ($status == 3) {
  345. $lab = '门诊预约';
  346. }
  347. $order_sn = $find['order_sn'];
  348. $order_id = $find['id'];
  349. $doctername = $user['name'];
  350. if (!$find) {
  351. return out('', 500, '订单错误!');
  352. }
  353. if ($find['order_status'] != 2) {
  354. return out('', 500, '此订单已被接单或未付款!');
  355. }
  356. if ($find['payment_status'] >= 4) {
  357. return out('', 500, '此订单可能已完成!');
  358. }
  359. DB::beginTransaction();
  360. try {
  361. Order::where('id', '=', $req['order_id'])->update(['order_status' => 3, 'updated_at' => date('Y-m-d H:i:s', time()), 'receiving_time' => time()]);
  362. // 添加到用户记录中!
  363. UserMessages::create([
  364. 'user_id' => $find['user_id'],
  365. 'docter_id' => $user['id'],
  366. 'status' => 1,
  367. 'type' => 1,
  368. 'relation_id' => $order_id,
  369. 'content' => "您的 " . $lab . " 订单,订单号" . $order_sn . ",医生" . $doctername . "已经确认接单(点击查看订单详情)",
  370. ]);
  371. DB::commit();
  372. $msg = $this->ReceivingReminder($req['order_id']);
  373. order_trace(['接单日志', '请求参数' => $req['order_id'] ?? '', '返回数据' => $msg ?? ''], 'info');
  374. return out();
  375. } catch (\Exception $e) {
  376. DB::rollBack();
  377. return out('', 500, $e->getMessage());
  378. } catch (\PDOException $e) {
  379. DB::rollBack();
  380. return out('', 500, $e->getMessage());
  381. }
  382. }
  383. /**
  384. * @return mixed
  385. * 患者咨询详情
  386. * @author Yuanhang Liu & Xiaoyun Liu
  387. */
  388. public function orderPatientDetail()
  389. {
  390. $req = request()->post();
  391. $this->validate(request(), [
  392. 'patient_id' => 'required|integer',
  393. ]);
  394. $data = [];
  395. // 订单查询
  396. $data = Order::with(['orderPatient', 'user', 'calllog'])->where('id', $req['patient_id'])->first();
  397. // 通话记录查询
  398. $datas = CallLog::where('order_id', $req['patient_id'])->get();
  399. // 机构查询
  400. $user = $this->user;
  401. $doctor_id = (new Commons)->getUserIdByDoctorId($user['phone']);
  402. // 返回数组
  403. $res_patient = [];
  404. //电话咨询
  405. //电话咨询
  406. if ($data){
  407. $data=$data->toArray();
  408. }
  409. if ($data['product_type'] == 1) {
  410. $res_patient['patient_id'] = $data['order_patient']['id'];
  411. $res_patient['user_id'] = $data['user_id'];
  412. $res_patient['order_sn'] = $data['order_sn'];
  413. $res_patient['patient_id'] = $data['patient_id'];
  414. $res_patient['product_type'] = $data['product_type'];
  415. $res_patient['name'] = $data['order_patient']['name'];//患者姓名
  416. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);//年龄
  417. $res_patient['card_number'] = $data['order_patient']['card_number'];//身份证号
  418. $res_patient['created_at'] = $data['created_at'];//下单时间
  419. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  420. $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---';//接单时间
  421. $res_patient['order_status'] = $data['order_status'];//订单状态
  422. $res_patient['call_list'] = [];//通话记录
  423. $res_patient['one_call'] = '';
  424. if ($data['calllog']) {
  425. $res_patient['one_call'] = $data['calllog'][0]['call_time'];
  426. $res_patient['secret_no'] = $data['calllog'][count($data['calllog']) - 1]['secret_no'];//X号码
  427. foreach ($data['calllog'] as $ks => $vs) {
  428. $res_patient['call_list'][$ks]['frequency'] = '通话' . ($ks += 1);//拨打电话开始时间
  429. $res_patient['call_list'][$ks]['start'] = $vs['call_time'];//拨打电话开始时间
  430. $res_patient['call_list'][$ks]['end'] = $vs['ring_time'];//拨打电话结束时间
  431. $res_patient['call_list'][$ks]['duration'] = gmdate('i:s', $vs['talk_time']);//拨打电话结束时间
  432. }
  433. }
  434. }
  435. //图文咨询
  436. if ($data['product_type'] == 2) {
  437. $res_patient['order_sn'] = $data['order_sn'];
  438. $res_patient['patient_id'] = $data['patient_id'];
  439. $res_patient['user_id'] = $data['user_id'];
  440. $res_patient['user_name'] = $data['user']['nickname'];
  441. $res_patient['user_avatar'] = $data['user']['avatar'];
  442. $res_patient['sex'] = $data['order_patient']['sex'];
  443. $res_patient['product_type'] = $data['product_type'];
  444. $res_patient['name'] = $data['order_patient']['name'];
  445. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);
  446. $res_patient['card_number'] = $data['order_patient']['card_number'];
  447. $res_patient['symptoms'] = $data['order_patient']['symptoms'];//病情描述
  448. $res_patient['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
  449. $res_patient['created_at'] = $data['created_at'];//下单时间
  450. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  451. $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---'; //接单时间
  452. $res_patient['order_status'] = $data['order_status'];//接单时间
  453. }
  454. //门诊预约
  455. if ($data['product_type'] == 3) {
  456. $res_patient['order_sn'] = $data['order_sn'];
  457. $res_patient['patient_id'] = $data['patient_id'];
  458. $res_patient['product_type'] = $data['product_type'];
  459. $res_patient['name'] = $data['order_patient']['name']; // 患者
  460. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']); //年龄
  461. $res_patient['card_number'] = $data['order_patient']['card_number']; // 证件号
  462. $res_patient['appoint_time'] = date('Y年m月d日 H:i', $data['order_patient']['appoint_start_time']);// 预约时间
  463. $res_patient['order_status'] = $data['order_status'];// 订单状态
  464. $organization = Organization::where('id', '=', $data['organization_id'])->first();
  465. if ($organization) {
  466. $organization = $organization->toArray();
  467. $res_patient['organization'] = $organization['name']; // 门诊机构
  468. } else {
  469. $res_patient['organization'] = ''; // 门诊机构
  470. }
  471. $res_patient['order_sn'] = $data['order_sn'];//订单号
  472. $res_patient['created_at'] = $data['created_at'];//下单时间
  473. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  474. }
  475. return out($res_patient);
  476. }
  477. /**
  478. * 拨打电话/绑定电话
  479. * @return \Illuminate\Http\JsonResponse
  480. * @author Liu-Yh
  481. * Create By 2020/11/24 19:19
  482. */
  483. public function callPhones()
  484. {
  485. $req = request()->post();
  486. $user = $this->user;
  487. $this->validate(request(), [
  488. 'order_id' => 'required',
  489. 'user_id' => 'required|integer',
  490. ]);
  491. $docter_id = $user['id'];
  492. $docter_phone = $user['phone'];
  493. if (!$docter_phone) {
  494. return out('', 500, '医生电话不存在!');
  495. }
  496. $find = Order::with('orderPatient')->where('order_sn', $req['order_id'])->first()->toArray();
  497. if (empty($find['order_patient'])) {
  498. return out('', 500, '患者电话不存在');
  499. }
  500. $phone = $find['order_patient']['phone'];
  501. if ($docter_phone == $phone) {
  502. return out('', 500, '医生和患者电话号不能一样!');
  503. }
  504. $wheres['docter_id'] = $docter_id;
  505. $wheres['user_id'] = $req['user_id'];
  506. $commons = new Commons();
  507. $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
  508. if ($finds) {
  509. $querylok = $commons->QuerySubsId($finds['xphone']);
  510. if ($querylok['Code'] == 'OK') {
  511. // 可能是数组
  512. $new_arr = explode(',', $querylok['SubsId']);
  513. foreach ($new_arr as $v) {
  514. $queryCallStatus = $commons->QuerySubscriptionDetail($finds['xphone'], $v);
  515. if ($queryCallStatus['Code'] == "OK") {
  516. if ($queryCallStatus['SecretBindDetailDTO']['PhoneNoA'] == $docter_phone && $queryCallStatus['SecretBindDetailDTO']['PhoneNoB'] == $phone) {
  517. return out($finds['xphone']);
  518. } else {
  519. if ($phone) {
  520. Axb::where('id', $finds['id'])->delete();
  521. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  522. } else {
  523. return out('', 500, '患者电话不存在');
  524. }
  525. }
  526. } else {
  527. if ($phone) {
  528. Axb::where('id', $finds['id'])->delete();
  529. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  530. } else {
  531. return out('', 500, '患者电话不存在');
  532. }
  533. }
  534. }
  535. } else {
  536. if ($phone) {
  537. Axb::where('id', $finds['id'])->delete();
  538. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  539. } else {
  540. return out('', 500, '患者电话不存在');
  541. }
  542. }
  543. } else {
  544. if ($phone) {
  545. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  546. } else {
  547. return out('', 500, '患者电话不存在');
  548. }
  549. }
  550. }
  551. /**
  552. * 创建电话
  553. * @param $phone
  554. * @param $docter_phone
  555. * @param $docter_id
  556. * @param $user_id
  557. * @return \Illuminate\Http\JsonResponse
  558. */
  559. protected function createCall($phone, $docter_phone, $docter_id, $user_id)
  560. {
  561. $commons = new Commons();
  562. $callModel = $commons->BindAxb($docter_phone, $phone);
  563. if ($callModel['Code'] == "OK") {
  564. Axb::create([
  565. 'docter_id' => $docter_id,
  566. 'user_id' => $user_id,
  567. 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
  568. 'subs_id' => $callModel['SecretBindDTO']['SubsId'],
  569. 'createtime' => time(),
  570. ]);
  571. return out($callModel['SecretBindDTO']['SecretNo']);
  572. } else {
  573. return out($callModel);
  574. }
  575. }
  576. /**
  577. * 电话随访
  578. * @return \Illuminate\Http\JsonResponse
  579. * @author Liu-Yh
  580. * Create By 2020/11/24 19:19
  581. */
  582. public function callPhoneSure()
  583. {
  584. $req = request()->post();
  585. $user = $this->user;
  586. $this->validate(request(), [
  587. 'user_id' => 'required|integer',
  588. ]);
  589. $docter_id = $user['id'];
  590. $docter_phone = $user['phone'];
  591. if (!$docter_phone) {
  592. return out('', 500, '医生电话不存在!');
  593. }
  594. $find = User::where('id', $req['user_id'])->first()->toArray();
  595. $phone = $find['phone'];
  596. $wheres['docter_id'] = $docter_id;
  597. $wheres['user_id'] = $find['id'];
  598. $commons = new Commons();
  599. $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
  600. if ($finds) {
  601. $querylok = $commons->QuerySubsId($finds['xphone']);
  602. if ($querylok['Code'] == 'OK') {
  603. $queryCallStatus = $commons->QueryCallStatus($phone, $querylok['SubsId']);
  604. if ($queryCallStatus['Code'] == 'OK') {
  605. if ($queryCallStatus['SecretCallStatusDTO'] != 4) {
  606. return out($finds['xphone']);
  607. } else {
  608. if ($phone) {
  609. $callModel = $commons->BindAxb($docter_phone, $phone);
  610. if ($callModel['Code'] == "OK") {
  611. return out($callModel['SecretBindDTO']['SecretNo']);
  612. }
  613. } else {
  614. return out('', 500, '患者电话不存在');
  615. }
  616. }
  617. }
  618. }
  619. } else {
  620. if ($phone) {
  621. $callModel = $commons->BindAxb($docter_phone, $phone);
  622. if ($callModel['Code'] == "OK") {
  623. Axb::create([
  624. 'docter_id' => $docter_id,
  625. 'user_id' => $req['user_id'],
  626. 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
  627. 'createtime' => time(),
  628. ]);
  629. return out($callModel['SecretBindDTO']['SecretNo']);
  630. } else {
  631. return out($callModel);
  632. }
  633. } else {
  634. return out('', 500, '患者电话不存在');
  635. }
  636. }
  637. }
  638. /**
  639. * 取消订单接口
  640. * @throws \Illuminate\Validation\ValidationException
  641. */
  642. public function cancelOrder()
  643. {
  644. $req = request()->post();
  645. $this->validate(request(), [
  646. 'order_id' => 'required|integer',
  647. ]);
  648. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
  649. if ($order['order_status'] == 2 && $order['payment_status'] == 2) {
  650. DB::beginTransaction();
  651. try {
  652. if ($order['product_type'] == 3) {
  653. Order::where('id',$req['order_id'])->update(['order_status'=>5,'payment_status'=>5,'order_notes'=>'医生拒绝接单']);
  654. }else{
  655. //退钱到余额
  656. if (!empty($order['payment_amount'])) {
  657. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '医生拒绝接单');
  658. }
  659. Order::where('id', $req['order_id'])->update(['order_status' => 5, 'order_notes' => '医生拒绝接单', 'payment_status' => 4]);
  660. }
  661. DB::commit();
  662. $this->CancelReminder($req['order_id']);
  663. return out('', 200, '订单取消成功');
  664. } catch (\Exception $e) {
  665. DB::rollBack();
  666. return out('', 500, $e->getMessage());
  667. } catch (\PDOException $e) {
  668. DB::rollBack();
  669. return out('', 500, $e->getMessage());
  670. }
  671. } else {
  672. return out('', 500, '订单不可取消');
  673. }
  674. }
  675. /**
  676. * 获取病例信息接口!
  677. * @author Liu
  678. * @return \Illuminate\Http\JsonResponse
  679. * @throws \Illuminate\Validation\ValidationException
  680. */
  681. public function CaseAcquisition()
  682. {
  683. $req = request()->post();
  684. $this->validate(request(), [
  685. 'user_id' => 'required|integer',
  686. 'order_id',
  687. // 'is_showAll'=>'required|integer'
  688. ]);
  689. $user = $this->user;
  690. $docter_id = $user['id'];
  691. // 订单内点击
  692. if (isset($req['order_id'])&&!empty($req['order_id'])){
  693. $data = Order::with(['orderPatient','user'])->where('id',$req['order_id'])->first();
  694. }else{
  695. $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();
  696. }
  697. // 返回数组
  698. $res_patient = [];
  699. // if ($req['is_showAll']==1){
  700. // $list = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2])->orderBy('id','asc')->get();
  701. // if ($list){
  702. // $list = $list->toArray();
  703. // foreach ($list as $k=>$v){
  704. // $res_patient['list'][$k]['order_sn'] = $v['order_sn'];
  705. // $res_patient['list'][$k]['user_id'] = $v['user_id'];
  706. // $res_patient['list'][$k]['user_name'] = $v['user']['nickname'];
  707. // $res_patient['list'][$k]['user_avatar'] = $v['user']['avatar'];
  708. // $res_patient['list'][$k]['sex'] = $v['order_patient']['sex'];
  709. // $res_patient['list'][$k]['name'] = $v['order_patient']['name'];
  710. // $res_patient['list'][$k]['numbirthday'] = numBirthday($v['order_patient']['birthday']);
  711. // $res_patient['list'][$k]['symptoms'] = $v['order_patient']['symptoms'];//病情描述
  712. // $res_patient['list'][$k]['medical_imgs'] = json_decode($v['order_patient']['medical_imgs'], true);//病情照片
  713. // }
  714. // }
  715. // }
  716. if ($data) {
  717. $data = $data->toArray();
  718. $res_patient['order_sn'] = $data['order_sn'];
  719. $res_patient['user_id'] = $data['user_id'];
  720. $res_patient['user_name'] = $data['user']['nickname'];
  721. $res_patient['user_avatar'] = $data['user']['avatar'];
  722. $res_patient['sex'] = $data['order_patient']['sex'];
  723. $res_patient['name'] = $data['order_patient']['name'];
  724. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);
  725. $res_patient['symptoms'] = $data['order_patient']['symptoms'];//病情描述
  726. $res_patient['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
  727. }
  728. return out($res_patient);
  729. }
  730. /**
  731. * 修改关闭状态
  732. * $type 1=开启小程序,2=关闭小程序
  733. * @return \Illuminate\Http\JsonResponse
  734. * @throws \Illuminate\Validation\ValidationException
  735. */
  736. public function docter_open(){
  737. $req = request()->post();
  738. $this->validate(request(), [
  739. 'type' => 'required|integer|in:1,2',
  740. ]);
  741. $user = $this->user;
  742. $docter_id = $user['id'];
  743. Docter::where('id',$docter_id)->update(['is_open'=>$req['type']]);
  744. return out();
  745. }
  746. /**
  747. * 确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
  748. */
  749. public function AppointReminder($id=''){
  750. if (!empty($id)){
  751. $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$id,'order_status'=>3,'payment_status'=>2])->first();
  752. $send = send_wechatSubscription_message('appoint_reminder',[$Order['docter']['openid'], "pages/index/index",$Order['order_sn'],$Order['user']['nickname'],$Order['user']['phone'],$Order['created_at']]);
  753. }else{
  754. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  755. $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  756. $Order = Order::with(['orderPatient','docter','user'])->where(['order_status'=>3,'payment_status'=>2,'product_type'=>3])->whereBetween('receiving_time',[$beginToday,$endToday])->get();
  757. foreach ($Order as $k=>$v){
  758. if ($v['docter']){
  759. if ($v['docter']['openid']){
  760. $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. /**
  767. * 取消订单提醒
  768. * @param $order_id
  769. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  770. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  771. * @throws \GuzzleHttp\Exception\GuzzleException
  772. */
  773. public function CancelReminder($order_id){
  774. $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$order_id])->first();
  775. order_trace(['类型'=>'订单医生取消','订单号'=>$order_id,'记录时间'=>date('Ymd His',time())],'info');
  776. $type = '';
  777. if ($Order['product_type']==1){
  778. $type = '电话咨询';
  779. }elseif ($Order['product_type']==2){
  780. $type = '图文咨询 ';
  781. }elseif ($Order['product_type']==3){
  782. $type = '门诊预约';
  783. }
  784. if ($Order){
  785. if ($Order['docter']['openid']){
  786. $send = send_wechatSubscription_message('cancel_reminder',[
  787. $Order['docter']['openid'],
  788. "pages/index/index",
  789. $Order['docter']['name'],
  790. $Order['order_sn'],
  791. $type,
  792. $Order['total_amount'],
  793. $Order['created_at'],
  794. '医生取消订单',
  795. ]);
  796. }
  797. }
  798. }
  799. /**
  800. * 咨询订单接单提醒
  801. * @param $order_id
  802. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  803. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  804. * @throws \GuzzleHttp\Exception\GuzzleException
  805. */
  806. public function ReceivingReminder($order_id){
  807. $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first()->toArray();
  808. order_trace(['类型'=>'咨询订单接单提醒','订单号'=>$order_id,'记录时间'=>date('Ymd His',time())],'info');
  809. $type = '';
  810. if ($Order['product_type']==1){
  811. $type = '电话咨询';
  812. }elseif ($Order['product_type']==2){
  813. $type = '图文咨询 ';
  814. }elseif ($Order['product_type']==3){
  815. $type = '门诊预约';
  816. }
  817. if ($Order){
  818. if ($Order['user']['openid']){
  819. $send = send_wechatSubscription_message('receiving_reminder',[
  820. $Order['user']['openid'],
  821. "pages/index/index",
  822. $Order['order_sn'],
  823. $type,
  824. date('Y-m-d H:i',$Order['receiving_time']),
  825. $Order['order_patient']['phone'],
  826. ],'wechat_small_program');
  827. }
  828. }
  829. }
  830. /**
  831. * 消息回复提醒(医生)
  832. * @param $order_id
  833. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  834. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  835. * @throws \GuzzleHttp\Exception\GuzzleException
  836. */
  837. public function ReplyReminder($docter_id,$user_id){
  838. $list = Docter::with(['DocterOrganization','organization'])->where(['id'=>$docter_id,'is_open'=>2])->first();
  839. order_trace(['消息回复', '请求参数' => $docter_id.'-'.$user_id ?? '', 'list数据' => $list ?? ''], 'info');
  840. $user = User::where('id',$user_id)->first();
  841. if ($list){
  842. if ($user['openid']){
  843. $send = send_wechatSubscription_message('reply_reminder',[
  844. $user['openid'],
  845. "pages/index/index",
  846. $list['organization'][0]['name'],
  847. $list['name'],
  848. $user['nickname'],
  849. ],'wechat_small_program');
  850. }
  851. }
  852. }
  853. /**
  854. * 排班变更提醒
  855. * @param $docter_id 医生id
  856. * @param int $type 类型0=周排班 1=月排班
  857. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  858. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  859. * @throws \GuzzleHttp\Exception\GuzzleException
  860. */
  861. public function ScheduleReminder($docter_id=81,$type=0){
  862. $list = Docter::where(['id'=>$docter_id])->first();
  863. $type = '';
  864. $openid = $list['openid'];
  865. if ($type==0){
  866. $type = '周排班变动';
  867. }elseif ($type==1){
  868. $type = '月排班变动 ';
  869. }
  870. if ($list){
  871. if ($openid){
  872. $send = send_wechatSubscription_message('schedule_reminder',[$openid, "pages/index/index", $type, '社区']);
  873. }
  874. }
  875. }
  876. /**
  877. * 审核认证提醒
  878. * @param $docter_id 医生id
  879. * @param int $msg 消息
  880. * @param $time 时间
  881. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  882. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  883. * @throws \GuzzleHttp\Exception\GuzzleException
  884. */
  885. public function ThenReminder($docter_id,$msg='',$time){
  886. $list = Docter::where(['id'=>$docter_id])->first();
  887. $openid = $list['openid'];
  888. if ($list){
  889. if ($openid){
  890. $send = send_wechatSubscription_message('then_reminder',[$openid, "pages/index/index", $list['name'],$msg,$time]);
  891. }
  892. }
  893. }
  894. /**
  895. * 完成订单
  896. * @param $order_id
  897. */
  898. public function ReceivingReminderOK($order_id){
  899. $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first();
  900. $type = '';
  901. if ($Order['product_type']==1){
  902. $type = '电话咨询已结束';
  903. }elseif ($Order['product_type']==2){
  904. $type = '图文咨询已结束 ';
  905. }elseif ($Order['product_type']==3){
  906. $type = '门诊预约';
  907. }
  908. order_trace(['发送微信模板消息ReceivingReminder', '请求参数' => $order_id ?? '', '返回数据' => $Order ?? ''], 'error');
  909. if ($Order){
  910. if ($Order['user']['openid']){
  911. order_trace(['订单类型'=>$type,'订单id'=>$Order,'操作时间'=>date('Ymd His',time())],'info');
  912. $send = send_wechatSubscription_message('receiving_reminderok',[
  913. $Order['user']['openid'],
  914. "pages/index",
  915. $Order['order_sn'],
  916. $type,
  917. $Order['payment_amount'],
  918. ],'wechat_small_program');
  919. }
  920. }
  921. }
  922. }