PatientController.php 40 KB

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