PatientController.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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,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 = [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'] = $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. 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();
  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. //电话咨询
  401. if ($data){
  402. $data=$data->toArray();
  403. }
  404. if ($data['product_type'] == 1) {
  405. $res_patient['patient_id'] = $data['order_patient']['id'];
  406. $res_patient['user_id'] = $data['user_id'];
  407. $res_patient['order_sn'] = $data['order_sn'];
  408. $res_patient['patient_id'] = $data['patient_id'];
  409. $res_patient['product_type'] = $data['product_type'];
  410. $res_patient['name'] = $data['order_patient']['name'];//患者姓名
  411. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);//年龄
  412. $res_patient['card_number'] = $data['order_patient']['card_number'];//身份证号
  413. $res_patient['created_at'] = $data['created_at'];//下单时间
  414. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  415. $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---';//接单时间
  416. $res_patient['order_status'] = $data['order_status'];//订单状态
  417. $res_patient['call_list'] = [];//通话记录
  418. $res_patient['one_call'] = '';
  419. if ($data['calllog']) {
  420. $res_patient['one_call'] = $data['calllog'][0]['call_time'];
  421. $res_patient['secret_no'] = $data['calllog'][count($data['calllog']) - 1]['secret_no'];//X号码
  422. foreach ($data['calllog'] as $ks => $vs) {
  423. $res_patient['call_list'][$ks]['frequency'] = '通话' . ($ks += 1);//拨打电话开始时间
  424. $res_patient['call_list'][$ks]['start'] = $vs['call_time'];//拨打电话开始时间
  425. $res_patient['call_list'][$ks]['end'] = $vs['ring_time'];//拨打电话结束时间
  426. $res_patient['call_list'][$ks]['duration'] = gmdate('i:s', $vs['talk_time']);//拨打电话结束时间
  427. }
  428. }
  429. }
  430. //图文咨询
  431. if ($data['product_type'] == 2) {
  432. $res_patient['order_sn'] = $data['order_sn'];
  433. $res_patient['patient_id'] = $data['patient_id'];
  434. $res_patient['user_id'] = $data['user_id'];
  435. $res_patient['user_name'] = $data['user']['nickname'];
  436. $res_patient['user_avatar'] = $data['user']['avatar'];
  437. $res_patient['sex'] = $data['order_patient']['sex'];
  438. $res_patient['product_type'] = $data['product_type'];
  439. $res_patient['name'] = $data['order_patient']['name'];
  440. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);
  441. $res_patient['card_number'] = $data['order_patient']['card_number'];
  442. $res_patient['symptoms'] = $data['order_patient']['symptoms'];//病情描述
  443. $res_patient['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
  444. $res_patient['created_at'] = $data['created_at'];//下单时间
  445. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  446. $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---'; //接单时间
  447. $res_patient['order_status'] = $data['order_status'];//接单时间
  448. }
  449. //门诊预约
  450. if ($data['product_type'] == 3) {
  451. $res_patient['order_sn'] = $data['order_sn'];
  452. $res_patient['patient_id'] = $data['patient_id'];
  453. $res_patient['product_type'] = $data['product_type'];
  454. $res_patient['name'] = $data['order_patient']['name']; // 患者
  455. $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']); //年龄
  456. $res_patient['card_number'] = $data['order_patient']['card_number']; // 证件号
  457. $res_patient['appoint_time'] = date('Y年m月d日 H:i', $data['order_patient']['appoint_start_time']);// 预约时间
  458. $res_patient['order_status'] = $data['order_status'];// 订单状态
  459. $organization = Organization::where('id', '=', $data['organization_id'])->first();
  460. if ($organization) {
  461. $organization = $organization->toArray();
  462. $res_patient['organization'] = $organization['name']; // 门诊机构
  463. } else {
  464. $res_patient['organization'] = ''; // 门诊机构
  465. }
  466. $res_patient['order_sn'] = $data['order_sn'];//订单号
  467. $res_patient['created_at'] = $data['created_at'];//下单时间
  468. $res_patient['nickname'] = $data['user']['nickname'];//下单用户
  469. }
  470. return out($res_patient);
  471. }
  472. /**
  473. * 拨打电话/绑定电话
  474. * @return \Illuminate\Http\JsonResponse
  475. * @author Liu-Yh
  476. * Create By 2020/11/24 19:19
  477. */
  478. public function callPhones()
  479. {
  480. $req = request()->post();
  481. $user = $this->user;
  482. $this->validate(request(), [
  483. 'order_id' => 'required',
  484. 'user_id' => 'required|integer',
  485. ]);
  486. $docter_id = $user['id'];
  487. $docter_phone = $user['phone'];
  488. if (!$docter_phone) {
  489. return out('', 500, '医生电话不存在!');
  490. }
  491. $find = Order::with('orderPatient')->where('order_sn', $req['order_id'])->first()->toArray();
  492. if (empty($find['order_patient'])) {
  493. return out('', 500, '患者电话不存在');
  494. }
  495. $phone = $find['order_patient']['phone'];
  496. if ($docter_phone == $phone) {
  497. return out('', 500, '医生和患者电话号不能一样!');
  498. }
  499. $wheres['docter_id'] = $docter_id;
  500. $wheres['user_id'] = $req['user_id'];
  501. $commons = new Commons();
  502. $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
  503. if ($finds) {
  504. $querylok = $commons->QuerySubsId($finds['xphone']);
  505. if ($querylok['Code'] == 'OK') {
  506. // 可能是数组
  507. $new_arr = explode(',', $querylok['SubsId']);
  508. foreach ($new_arr as $v) {
  509. $queryCallStatus = $commons->QuerySubscriptionDetail($finds['xphone'], $v);
  510. if ($queryCallStatus['Code'] == "OK") {
  511. if ($queryCallStatus['SecretBindDetailDTO']['PhoneNoA'] == $docter_phone && $queryCallStatus['SecretBindDetailDTO']['PhoneNoB'] == $phone) {
  512. return out($finds['xphone']);
  513. } else {
  514. if ($phone) {
  515. Axb::where('id', $finds['id'])->delete();
  516. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  517. } else {
  518. return out('', 500, '患者电话不存在');
  519. }
  520. }
  521. } else {
  522. if ($phone) {
  523. Axb::where('id', $finds['id'])->delete();
  524. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  525. } else {
  526. return out('', 500, '患者电话不存在');
  527. }
  528. }
  529. }
  530. } else {
  531. if ($phone) {
  532. Axb::where('id', $finds['id'])->delete();
  533. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  534. } else {
  535. return out('', 500, '患者电话不存在');
  536. }
  537. }
  538. } else {
  539. if ($phone) {
  540. return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
  541. } else {
  542. return out('', 500, '患者电话不存在');
  543. }
  544. }
  545. }
  546. /**
  547. * 创建电话
  548. * @param $phone
  549. * @param $docter_phone
  550. * @param $docter_id
  551. * @param $user_id
  552. * @return \Illuminate\Http\JsonResponse
  553. */
  554. protected function createCall($phone, $docter_phone, $docter_id, $user_id)
  555. {
  556. $commons = new Commons();
  557. $callModel = $commons->BindAxb($docter_phone, $phone);
  558. if ($callModel['Code'] == "OK") {
  559. Axb::create([
  560. 'docter_id' => $docter_id,
  561. 'user_id' => $user_id,
  562. 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
  563. 'subs_id' => $callModel['SecretBindDTO']['SubsId'],
  564. 'createtime' => time(),
  565. ]);
  566. return out($callModel['SecretBindDTO']['SecretNo']);
  567. } else {
  568. return out($callModel);
  569. }
  570. }
  571. /**
  572. * 电话随访
  573. * @return \Illuminate\Http\JsonResponse
  574. * @author Liu-Yh
  575. * Create By 2020/11/24 19:19
  576. */
  577. public function callPhoneSure()
  578. {
  579. $req = request()->post();
  580. $user = $this->user;
  581. $this->validate(request(), [
  582. 'user_id' => 'required|integer',
  583. ]);
  584. $docter_id = $user['id'];
  585. $docter_phone = $user['phone'];
  586. if (!$docter_phone) {
  587. return out('', 500, '医生电话不存在!');
  588. }
  589. $find = User::where('id', $req['user_id'])->first()->toArray();
  590. $phone = $find['phone'];
  591. $wheres['docter_id'] = $docter_id;
  592. $wheres['user_id'] = $find['id'];
  593. $commons = new Commons();
  594. $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
  595. if ($finds) {
  596. $querylok = $commons->QuerySubsId($finds['xphone']);
  597. if ($querylok['Code'] == 'OK') {
  598. $queryCallStatus = $commons->QueryCallStatus($phone, $querylok['SubsId']);
  599. if ($queryCallStatus['Code'] == 'OK') {
  600. if ($queryCallStatus['SecretCallStatusDTO'] != 4) {
  601. return out($finds['xphone']);
  602. } else {
  603. if ($phone) {
  604. $callModel = $commons->BindAxb($docter_phone, $phone);
  605. if ($callModel['Code'] == "OK") {
  606. return out($callModel['SecretBindDTO']['SecretNo']);
  607. }
  608. } else {
  609. return out('', 500, '患者电话不存在');
  610. }
  611. }
  612. }
  613. }
  614. } else {
  615. if ($phone) {
  616. $callModel = $commons->BindAxb($docter_phone, $phone);
  617. if ($callModel['Code'] == "OK") {
  618. Axb::create([
  619. 'docter_id' => $docter_id,
  620. 'user_id' => $req['user_id'],
  621. 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
  622. 'createtime' => time(),
  623. ]);
  624. return out($callModel['SecretBindDTO']['SecretNo']);
  625. } else {
  626. return out($callModel);
  627. }
  628. } else {
  629. return out('', 500, '患者电话不存在');
  630. }
  631. }
  632. }
  633. /**
  634. * 取消订单接口
  635. * @throws \Illuminate\Validation\ValidationException
  636. */
  637. public function cancelOrder()
  638. {
  639. $req = request()->post();
  640. $this->validate(request(), [
  641. 'order_id' => 'required|integer',
  642. ]);
  643. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
  644. if ($order['order_status'] == 2 && $order['payment_status'] == 2) {
  645. DB::beginTransaction();
  646. try {
  647. if ($order['product_type'] == 3) {
  648. Order::where('id',$req['order_id'])->update(['order_status'=>5,'payment_status'=>5,'order_notes'=>'医生拒绝接单']);
  649. }else{
  650. //退钱到余额
  651. if (!empty($order['payment_amount'])) {
  652. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '医生拒绝接单');
  653. }
  654. Order::where('id', $req['order_id'])->update(['order_status' => 5, 'order_notes' => '医生拒绝接单', 'payment_status' => 4]);
  655. }
  656. DB::commit();
  657. $this->CancelReminder($req['order_id']);
  658. return out('', 200, '订单取消成功');
  659. } catch (\Exception $e) {
  660. DB::rollBack();
  661. return out('', 500, $e->getMessage());
  662. } catch (\PDOException $e) {
  663. DB::rollBack();
  664. return out('', 500, $e->getMessage());
  665. }
  666. } else {
  667. return out('', 500, '订单不可取消');
  668. }
  669. }
  670. /**
  671. * 订单超时自动完成(定时)
  672. */
  673. public function overTimeOrers(){
  674. // $user = $this->user;
  675. // $docter_id = $user['id'];
  676. $config_chat = SystemConfig::get('docter_config','chat_complete_time');
  677. $config_phone = SystemConfig::get('docter_config','phone_complete_time');
  678. // 换算为秒
  679. $config_chat = $config_chat*60;
  680. $config_phone = $config_phone*60;
  681. $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
  682. $catNewIds = [];
  683. $menNewIds = [];
  684. foreach ($inOrder as $k=>$v){
  685. if ($v['product_type']==1){
  686. if ((time()-$v['receiving_time'])>=$config_chat){
  687. $catNewIds[$k] = $v['id'];
  688. }
  689. }else if($v['product_type']==2){
  690. if ((time()-$v['receiving_time'])>=$config_phone){
  691. $catNewIds[$k] = $v['id'];
  692. }
  693. }else if($v['product_type']==3){
  694. if ((time()-$v['receiving_time'])>=(1*60*60*24)){
  695. $menNewIds[$k] = $v['id'];
  696. }
  697. }
  698. }
  699. if ($catNewIds || $menNewIds){
  700. // 操作图文和电话订单为已完成
  701. Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
  702. // 操作门诊订单为已超时
  703. Order::whereIn('id',$menNewIds)->update(['order_status'=>6]);
  704. }
  705. }
  706. /**
  707. * 获取病例信息接口!
  708. * @author Liu
  709. * @return \Illuminate\Http\JsonResponse
  710. * @throws \Illuminate\Validation\ValidationException
  711. */
  712. public function CaseAcquisition()
  713. {
  714. $req = request()->post();
  715. $this->validate(request(), [
  716. 'user_id' => 'required|integer',
  717. 'order_id',
  718. 'is_showAll'=>'required|integer'
  719. ]);
  720. $user = $this->user;
  721. $docter_id = $user['id'];
  722. // 订单内点击
  723. if (isset($req['order_id'])&&!empty($req['order_id'])){
  724. $data = Order::with(['orderPatient','user'])->where('id',$req['order_id'])->first();
  725. }else{
  726. $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();
  727. }
  728. // 返回数组
  729. $res_patient = [];
  730. if ($req['is_showAll']==1){
  731. $list = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2])->orderBy('id','asc')->get();
  732. if ($list){
  733. $list = $list->toArray();
  734. foreach ($list as $k=>$v){
  735. $res_patient['list'][$k]['order_sn'] = $v['order_sn'];
  736. $res_patient['list'][$k]['user_id'] = $v['user_id'];
  737. $res_patient['list'][$k]['user_name'] = $v['user']['nickname'];
  738. $res_patient['list'][$k]['user_avatar'] = $v['user']['avatar'];
  739. $res_patient['list'][$k]['sex'] = $v['order_patient']['sex'];
  740. $res_patient['list'][$k]['name'] = $v['order_patient']['name'];
  741. $res_patient['list'][$k]['numbirthday'] = numBirthday($v['order_patient']['birthday']);
  742. $res_patient['list'][$k]['symptoms'] = $v['order_patient']['symptoms'];//病情描述
  743. $res_patient['list'][$k]['medical_imgs'] = json_decode($v['order_patient']['medical_imgs'], true);//病情照片
  744. }
  745. }
  746. }
  747. if ($data) {
  748. $data = $data->toArray();
  749. $res_patient['data']['order_sn'] = $data['order_sn'];
  750. $res_patient['data']['user_id'] = $data['user_id'];
  751. $res_patient['data']['user_name'] = $data['user']['nickname'];
  752. $res_patient['data']['user_avatar'] = $data['user']['avatar'];
  753. $res_patient['data']['sex'] = $data['order_patient']['sex'];
  754. $res_patient['data']['name'] = $data['order_patient']['name'];
  755. $res_patient['data']['numbirthday'] = numBirthday($data['order_patient']['birthday']);
  756. $res_patient['data']['symptoms'] = $data['order_patient']['symptoms'];//病情描述
  757. $res_patient['data']['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
  758. }
  759. return out($res_patient);
  760. }
  761. /**
  762. * 修改关闭状态
  763. * $type 1=开启小程序,2=关闭小程序
  764. * @return \Illuminate\Http\JsonResponse
  765. * @throws \Illuminate\Validation\ValidationException
  766. */
  767. public function docter_open(){
  768. $req = request()->post();
  769. $this->validate(request(), [
  770. 'type' => 'required|integer|in:1,2',
  771. ]);
  772. $user = $this->user;
  773. $docter_id = $user['id'];
  774. Docter::where('id',$docter_id)->update(['is_open'=>$req['type']]);
  775. return out();
  776. }
  777. /**
  778. * 确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
  779. */
  780. public function AppointReminder(){
  781. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  782. $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  783. $Order = Order::with(['orderPatient','docter','user'])->where(['order_status'=>3,'payment_status'=>2,'product_type'=>3])->whereBetween('receiving_time',[$beginToday,$endToday])->get();
  784. foreach ($Order as $k=>$v){
  785. if ($v['docter']){
  786. if ($v['docter']['openid']){
  787. $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
  788. }
  789. }
  790. }
  791. }
  792. /**
  793. * 取消订单提醒
  794. * @param $order_id
  795. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  796. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  797. * @throws \GuzzleHttp\Exception\GuzzleException
  798. */
  799. public function CancelReminder($order_id){
  800. $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$order_id])->first();
  801. $type = '';
  802. if ($Order['product_type']==1){
  803. $type = '电话咨询';
  804. }elseif ($Order['product_type']==2){
  805. $type = '图文咨询 ';
  806. }elseif ($Order['product_type']==3){
  807. $type = '门诊预约';
  808. }
  809. if ($Order){
  810. if ($Order['docter']['openid']){
  811. $send = send_wechatSubscription_message('cancel_reminder',[
  812. $Order['docter']['openid'],
  813. "pages/index/index",
  814. $Order['docter']['name'],
  815. $Order['order_sn'],
  816. $type,
  817. $Order['total_amount'],
  818. $Order['created_at'],
  819. '',
  820. ]);
  821. var_dump($send);
  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. ]);
  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. ]);
  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. }