PatientController.php 42 KB

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