Wenzhen.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. namespace app\api\controller\tvuser;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 问诊订单,使用自动注册的用户
  7. */
  8. class Wenzhen extends Api
  9. {
  10. protected $noNeedLogin = '';
  11. protected $noNeedRight = '*';
  12. //添加成员
  13. private function add_member(){
  14. $field = [
  15. 'gender',
  16. 'age',
  17. 'marital_status',
  18. 'allergies_status',
  19. 'familymedical_status',
  20. 'chronicdisease_status',
  21. ];
  22. $data = request_post_hub($field);
  23. //选是,也要有点内容
  24. if($data['allergies_status'] == 1){
  25. $data['allergies_text'] = '是';
  26. }
  27. if($data['familymedical_status'] == 1){
  28. $data['familymedical_text'] = '是';
  29. }
  30. if($data['chronicdisease_status'] == 1){
  31. $data['chronicdisease_text'] = '是';
  32. }
  33. $data['user_id'] = $this->auth->id;
  34. $data['realname'] = $this->auth->nickname;
  35. return $data;
  36. }
  37. //判断是否 移动融合包的vip
  38. private function checkvip(){
  39. $is_vip = 0;
  40. $tv_userid = $this->auth->tv_userid;
  41. //跨数据库查询
  42. $tv_user = Db::connect('database_tv')->name('hu_user')->where('id',$tv_userid)->find();
  43. if(!empty($tv_user)){
  44. $is_vip = $tv_user['is_vip'];
  45. }
  46. return $is_vip;
  47. }
  48. //创建订单
  49. public function create_order(){
  50. //判断用户来源
  51. if($this->auth->comefrom != 2){
  52. $this->error('用户错误');
  53. }
  54. //判断是否 健康E家自己的vip
  55. $checkvip = $this->checkvip();
  56. if(!$checkvip){
  57. $this->error('您不是VIP用户','',402);
  58. }
  59. //检查本月是否已经下过一个有效单了
  60. $mouth_times = config('site.tv_wenzhen_month_times');
  61. $checkmap = [
  62. 'user_id' => $this->auth->id,
  63. 'comefrom'=> 2,
  64. 'status' => ['IN','10,20,25,30'],//有效订单
  65. ];
  66. $check_order = Db::name('wenzhen_order')->where($checkmap)->whereTime('createtime','month')->count();
  67. if($check_order >= $mouth_times){
  68. $this->error('您本月的问诊次数已用完');
  69. }
  70. //问诊患者
  71. $member_data = $this->add_member();
  72. //医生信息
  73. $doctor_id = input('doctor_id',0);
  74. $doctor = Db::name('doctor')->where('id',$doctor_id)->find();
  75. $doctor_info = Db::name('doctor_info')->where('doctor_id',$doctor_id)->find();
  76. if(empty($doctor_info)){
  77. $this->error('不存在的医生');
  78. }
  79. if($doctor_info['video_switch'] != 1){
  80. $this->error('该医生未开通视频问诊');
  81. }
  82. if($doctor_info['video_model'] != 2){
  83. $this->error('该医生未开通即时问诊');
  84. }
  85. if($doctor_info['video_model'] == 2 && $doctor['job_status'] != 1){ //即时方式,需要在上班中
  86. $this->error('该医生现在不在上班中');
  87. }
  88. if($doctor['is_online'] != 1){ //即时方式,im需要在线
  89. $this->error('该医生现在不在线');
  90. }
  91. //订单信息
  92. $auth_id = $this->auth->id;
  93. $order_no = createUniqueNo('TV',$auth_id);
  94. $ordertype = 2;
  95. //价格
  96. $price = 0;
  97. //排班判断
  98. if($ordertype == 2){
  99. if($doctor_info['video_model'] == 1){ //排班预约
  100. $book_time = input('book_time','');
  101. if(empty($book_time)){
  102. $this->error('没有选择预约时间');
  103. }
  104. $book_time = strtotime($book_time);
  105. //检查
  106. if($book_time < time() + 7200){
  107. //测试临时屏蔽
  108. $this->error('最早只能预约'.date('m-d H:00',time() + 10800)); //最早预约两个小时后的一个整点
  109. }
  110. //排班状态
  111. $doctor_paiban = Db::name('doctor_paiban')->where('doctor_id',$doctor_id)->where('activetime',$book_time)->find();
  112. if(empty($doctor_paiban)){
  113. $this->error('您预约的时间该医生无法接诊');
  114. }
  115. if($doctor_paiban['active'] != 1){
  116. $this->error('您预约的时间该医生无法接诊');
  117. }
  118. //是否排满
  119. $map = [
  120. 'doctor_id' => $doctor_id,
  121. 'book_time' => $book_time,
  122. 'ordertype' => 2,
  123. 'status' => ['IN','10,20,25,30'],//有效订单
  124. 'video_model' => 1,//预约模式
  125. ];
  126. $order_count = Db::name('wenzhen_order')->where($map)->count();
  127. if($order_count >= 4){
  128. $this->error('该时间点已排满,请重选预约时间');
  129. }
  130. }
  131. else
  132. {
  133. $book_time = time(); //即时预约
  134. }
  135. }
  136. //订单数据
  137. $order_data = [
  138. 'order_no' => $order_no,
  139. 'user_id' => $auth_id,
  140. 'member_id' => 0,
  141. 'doctor_id' => $doctor_id,
  142. 'ordertype' => $ordertype,
  143. 'total_fee' => $price,
  144. 'book_time' => $book_time,
  145. 'createtime' => time(),
  146. 'status' => 10,//订单状态enum
  147. 'pay_type' => 'tvvip',
  148. 'pay_time' => time(),
  149. 'comefrom' => 2,
  150. 'video_model'=> $doctor_info['video_model'], //视频问诊预约方式
  151. ];
  152. Db::startTrans();
  153. //下单
  154. $order_id = Db::name('wenzhen_order')->insertGetId($order_data);
  155. if(!$order_id){
  156. Db::rollback();
  157. $this->error('下单失败');
  158. }
  159. //订单附表,就诊人信息
  160. $member_data['order_id'] = $order_id;
  161. $order_member_id = Db::name('wenzhen_order_member')->insertGetId($member_data);
  162. if(!$order_member_id){
  163. Db::rollback();
  164. $this->error('下单失败');
  165. }
  166. //
  167. Db::commit();
  168. $this->success('下单成功',$order_id);
  169. }
  170. //订单列表
  171. public function order_list(){
  172. //判断用户来源
  173. if($this->auth->comefrom != 2){
  174. $this->error('用户错误');
  175. }
  176. $status = input('status','all');
  177. $where = ['user_id'=>$this->auth->id];
  178. if($status != 'all'){
  179. $where['order.status'] = $status;
  180. if($status == 20){// 已接诊、问诊中
  181. $where['order.status'] = ['IN',[20,25]];
  182. }
  183. }
  184. $field = [
  185. 'order.id','order.order_no','order.book_time','order.status','order.ordertype','order.accept_time','order.video_time','order.doctor_id','order.cancel_reason','order.video_model',
  186. 'doctor.avatar as doctor_avatar','doctor.nickname as doctor_nickname','doctor.hospital as doctor_hospital',
  187. 'keshi.name as keshi_name',
  188. 'level.name as level_name',
  189. ];
  190. $list = Db::name('wenzhen_order')->alias('order')
  191. ->field($field)
  192. ->join('doctor','order.doctor_id = doctor.id','LEFT')
  193. ->join('doctor_level level','doctor.level_id = level.id','LEFT')
  194. ->join('keshi','doctor.keshi_id = keshi.id','LEFT')
  195. ->order('order.id desc')
  196. ->where($where)
  197. ->autopage()->select();
  198. $list = list_domain_image($list,['doctor_avatar']);
  199. if(!empty($list)){
  200. //循环处理
  201. foreach($list as $key => $val){
  202. //医生原因的退款,都直接算到退珍
  203. if(in_array($val['status'],[16,18,22])){
  204. $val['status'] = 100;
  205. }
  206. //视频拨打中 也算问诊中
  207. if($val['status'] == 25){
  208. $val['status'] = 20;
  209. }
  210. //视频订单是否能拨打,即时模式才可以
  211. $val['video_call_switch'] = 0;
  212. if($val['video_model'] == 2 && $val['ordertype'] == 2 && in_array($val['status'],[10,20,25])){ //三个状态都可以,理论上不存在20
  213. $val['video_call_switch'] = 1;
  214. }
  215. $list[$key] = $val;
  216. }
  217. }
  218. $this->success(1,$list);
  219. }
  220. //订单详情
  221. public function order_info(){
  222. $order_id = input('order_id',0);
  223. //订单详情
  224. $field = [
  225. 'id','order_no','user_id','member_id','doctor_id','ordertype','total_fee','book_time','createtime','status','cancel_time','cancel_reason','video_model',
  226. 'pay_type','pay_time','accept_time','video_time','finish_time','feedback_time','feedback_remark','feedback_images','comefrom'
  227. ];
  228. $wenzhen_order = Db::name('wenzhen_order')->field($field)->where('id',$order_id)->find();
  229. if(empty($wenzhen_order)){
  230. $this->error('不存在的订单');
  231. }
  232. $wenzhen_order = info_domain_image($wenzhen_order,['feedback_images']);
  233. $wenzhen_order['feedback_images'] = empty($wenzhen_order['feedback_images']) ? '' : $wenzhen_order['feedback_images'];
  234. //医生详情
  235. $field = [
  236. 'd.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum',
  237. 'keshi.name as keshi_name',
  238. 'level.name as level_name'
  239. ];
  240. $doctor_info = Db::name('doctor')->alias('d')
  241. ->field($field)
  242. ->join('doctor_level level','d.level_id = level.id','LEFT')
  243. ->join('keshi','d.keshi_id = keshi.id','LEFT')
  244. ->where('d.id',$wenzhen_order['doctor_id'])->find();
  245. $doctor_info = info_domain_image($doctor_info,['avatar']);
  246. $wenzhen_order['doctor_info'] = $doctor_info;
  247. //视频订单是否能拨打,即时模式才可以
  248. $wenzhen_order['video_call_switch'] = 0;
  249. if($wenzhen_order['video_model'] == 2 && $wenzhen_order['ordertype'] == 2 && in_array($wenzhen_order['status'],[10,20,25])){ //三个状态都可以,理论上不存在20
  250. $wenzhen_order['video_call_switch'] = 1;
  251. }
  252. $this->success(1,$wenzhen_order);
  253. }
  254. //换医生
  255. public function change_doctor(){
  256. //判断用户来源
  257. if($this->auth->comefrom != 2){
  258. $this->error('用户错误');
  259. }
  260. //医生信息
  261. $doctor_id = input('doctor_id',0);
  262. $doctor = Db::name('doctor')->where('id',$doctor_id)->find();
  263. $doctor_info = Db::name('doctor_info')->where('doctor_id',$doctor_id)->find();
  264. if(empty($doctor_info)){
  265. $this->error('不存在的医生');
  266. }
  267. if($doctor_info['video_switch'] != 1){
  268. $this->error('该医生未开通视频问诊');
  269. }
  270. if($doctor_info['video_model'] != 2){
  271. $this->error('该医生未开通即时问诊');
  272. }
  273. if($doctor_info['video_model'] == 2 && $doctor['job_status'] != 1){ //即时方式,需要在上班中
  274. $this->error('该医生现在不在上班中');
  275. }
  276. if($doctor['is_online'] != 1){ //即时方式,im需要在线
  277. $this->error('该医生现在不在线');
  278. }
  279. //订单信息
  280. $order_id = input('order_id',0);
  281. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('comefrom',2)->where('id',$order_id)->find();
  282. if(empty($wenzhen_order)){
  283. $this->error('不存在的订单');
  284. }
  285. if(!in_array($wenzhen_order['status'],[10])){
  286. Db::rollback();
  287. $this->error('当前订单不能换医生');
  288. }
  289. //修改订单
  290. $update = [
  291. 'doctor_id' => $doctor_id,
  292. ];
  293. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  294. if($update_rs === false){
  295. $this->error('换医生失败');
  296. }
  297. $this->success('操作成功');
  298. }
  299. //用户发起退珍
  300. //因为存在第三方,悲观锁
  301. public function tuizhen(){
  302. $this->error('不允许取消');
  303. $apilimit = $this->apiLimit();
  304. if(!$apilimit){
  305. $this->error('操作频繁');
  306. }
  307. //判断用户来源
  308. if($this->auth->comefrom != 2){
  309. $this->error('用户错误');
  310. }
  311. //订单详情
  312. $order_id = input('order_id',0);
  313. Db::startTrans();
  314. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('comefrom',2)->where('id',$order_id)->lock(true)->find();
  315. if(empty($wenzhen_order)){
  316. Db::rollback();
  317. $this->error('不存在的订单');
  318. }
  319. //仅限视频订单
  320. if($wenzhen_order['ordertype'] == 1){
  321. Db::rollback();
  322. $this->error('图文问诊不能退诊');
  323. }
  324. //下面全是视频订单
  325. //状态限制
  326. // if($wenzhen_order['video_model'] == 1){
  327. if(!in_array($wenzhen_order['status'],[10])){
  328. Db::rollback();
  329. $this->error('当前订单不能退诊');
  330. }
  331. //没付款,所以没有支付时间限制
  332. //超过下单时间一小时不能退
  333. if($wenzhen_order['status'] == 10 && (time() - $wenzhen_order['createtime'] > 3600)){
  334. Db::rollback();
  335. $this->error('下单超过一小时,不能退诊');
  336. }
  337. /*}else{
  338. //即时模式,已付款没接的能退
  339. if($wenzhen_order['status'] != 10){
  340. Db::rollback();
  341. $this->error('当前订单不能退诊');
  342. }
  343. }*/
  344. //修改订单
  345. $nowtime = time();
  346. $update = [
  347. 'status' => 13,
  348. 'cancel_time' => $nowtime,
  349. 'cancel_reason' => '用户退诊',
  350. 'finish_time' => $nowtime,
  351. ];
  352. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  353. if(!$update_rs){
  354. Db::rollback();
  355. $this->error('退诊失败');
  356. }
  357. Db::commit();
  358. $this->success('退珍成功');
  359. }
  360. ///////////////////////////////视频即时问诊///////////////////////////
  361. //第一次拨打视频之前,20,25可以调用
  362. public function before_first_video(){
  363. //判断用户来源
  364. if($this->auth->comefrom != 2){
  365. $this->error('用户错误');
  366. }
  367. //订单详情
  368. $order_id = input('order_id',0);
  369. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('comefrom',2)->where('id',$order_id)->find();
  370. if(empty($wenzhen_order)){
  371. $this->error('不存在的订单');
  372. }
  373. if($wenzhen_order['ordertype'] != 2){
  374. $this->error('视频问诊才能拨打视频');
  375. }
  376. if($wenzhen_order['video_model'] != 2){
  377. $this->error('即时视频问诊才能主动拨打视频');
  378. }
  379. if($wenzhen_order['status'] == 30){
  380. $this->error('订单已结束');
  381. }
  382. if($wenzhen_order['status'] == 25){
  383. //判断医生在线状态
  384. /*$tenim = new Tenim();
  385. $rs_online = $tenim->is_online('doctor'.$wenzhen_order['doctor_id']);
  386. if($rs_online != true){
  387. $this->error('医生现在不在线,请稍后拨打');
  388. }*/
  389. $this->success();
  390. }
  391. //20直接忽略
  392. if($wenzhen_order['status'] != 10){
  393. $this->error('订单错误,请刷新重试');
  394. }
  395. //判断患者在线状态
  396. /*$tenim = new Tenim();
  397. $rs_online = $tenim->is_online('doctor'.$wenzhen_order['doctor_id']);
  398. if($rs_online != true){
  399. $this->error('医生现在不在线,请稍后拨打');
  400. }*/
  401. $this->success('操作成功');
  402. }
  403. //第一次拨打视频
  404. public function first_video(){
  405. $apilimit = $this->apiLimit();
  406. if(!$apilimit){
  407. $this->error('操作频繁');
  408. }
  409. //订单详情
  410. $order_id = input('order_id',0);
  411. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('comefrom',2)->where('id',$order_id)->find();
  412. if(empty($wenzhen_order)){
  413. $this->error('不存在的订单');
  414. }
  415. if($wenzhen_order['ordertype'] != 2){
  416. $this->error('视频问诊才能拨打视频');
  417. }
  418. if($wenzhen_order['video_model'] != 2){
  419. $this->error('即时视频问诊才能主动拨打视频');
  420. }
  421. if($wenzhen_order['status'] == 30){
  422. $this->error('订单已结束');
  423. }
  424. if($wenzhen_order['status'] == 25){
  425. //倒计时
  426. $minute = config('site.firstvideo_videoorder_autofinish_minute');
  427. if($wenzhen_order['comefrom'] == 2){
  428. $minute = config('site.tv_firstvideo_videoorder_autofinish_minute');
  429. }
  430. $rs = [
  431. 'video_time' => $wenzhen_order['video_time'],
  432. 'video_endtime' => $wenzhen_order['video_time'] + $minute*60,
  433. ];
  434. $rs['seconds'] = $rs['video_endtime'] - time();
  435. //修改最后拨通时间
  436. $update = [
  437. 'last_video_time' => time(),
  438. ];
  439. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  440. $this->success('操作成功',$rs);
  441. }
  442. //20直接忽略
  443. if($wenzhen_order['status'] != 10){
  444. $this->error('订单错误,请刷新重试');
  445. }
  446. //修改订单
  447. $update = [
  448. 'status' => 25,
  449. 'video_time' => time(),
  450. 'last_video_time' => time(),
  451. ];
  452. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->where('status',10)->update($update);
  453. if(!$update_rs){
  454. $this->error('操作失败');
  455. }
  456. //倒计时
  457. $minute = config('site.firstvideo_videoorder_autofinish_minute');
  458. if($wenzhen_order['comefrom'] == 2){
  459. $minute = config('site.tv_firstvideo_videoorder_autofinish_minute');
  460. }
  461. $rs = [
  462. 'video_time' => $update['video_time'],
  463. 'seconds' => $minute*60,
  464. 'video_endtime' => $update['video_time'] + $minute*60,
  465. ];
  466. $this->success('操作成功',$rs);
  467. }
  468. //挂断,医生挂断。这个方法主要用来统计是否已满足一分钟
  469. public function video_end(){
  470. $apilimit = $this->apiLimit();
  471. if(!$apilimit){
  472. $this->error('操作频繁');
  473. }
  474. $end_rule = input('end_rule','user'); //user 用户挂断,doctor 医生挂断。如果是医生挂断并且这个订单一次都没有接通,那就直接按拒绝接诊,结束订单
  475. //订单详情
  476. $order_id = input('order_id',0);
  477. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('comefrom',2)->where('id',$order_id)->find();
  478. if(empty($wenzhen_order)){
  479. $this->error('不存在的订单');
  480. }
  481. if($wenzhen_order['ordertype'] != 2){
  482. $this->error('视频问诊才能拨打视频');
  483. }
  484. if($wenzhen_order['video_model'] != 2){
  485. $this->error('即时视频问诊才能主动拨打视频');
  486. }
  487. if($wenzhen_order['status'] != 25 && $wenzhen_order['status'] != 10){
  488. $this->error('订单错误,请刷新重试');
  489. }
  490. $nowtime = time();
  491. //接通了
  492. if($wenzhen_order['status'] == 25){
  493. //本次不超过1分钟
  494. if($nowtime - $wenzhen_order['last_video_time'] < 60 ){
  495. $this->success('已挂断');
  496. }
  497. //如果本次挂断之前已通话超过一分钟,那自动结束
  498. Db::startTrans();
  499. $wenzhen_order = Db::name('wenzhen_order')->where('id',$order_id)->lock(true)->find();
  500. $update = [
  501. 'status' => 30,
  502. 'finish_time' => $nowtime,
  503. ];
  504. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  505. if(!$update_rs){
  506. Db::rollback();
  507. $this->error('操作失败');
  508. }
  509. //给医生结算钱
  510. //医生接诊量
  511. Db::name('doctor')->where('id',$wenzhen_order['doctor_id'])->setInc('ordernum');
  512. Db::commit();
  513. $this->success('已挂断');
  514. }
  515. //没接通
  516. if($wenzhen_order['status'] == 10){
  517. if($end_rule == 'user'){
  518. $this->success('已挂断');
  519. }else{
  520. //如果是医生挂断并且这个订单一次都没有接通,那就直接按拒绝接诊,结束订单
  521. //参考医生发起退珍
  522. Db::startTrans();
  523. $wenzhen_order = Db::name('wenzhen_order')->where('id',$order_id)->lock(true)->find();
  524. //修改订单
  525. $nowtime = time();
  526. $update = [
  527. 'status' => 16,
  528. 'cancel_time' => $nowtime,
  529. 'cancel_reason' => '医生挂断未接诊',
  530. 'finish_time' => $nowtime,
  531. ];
  532. //没有支付就没有退款
  533. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  534. if(!$update_rs){
  535. Db::rollback();
  536. $this->error('医生挂断了');
  537. }
  538. Db::commit();
  539. $this->success('医生挂断并取消了订单');
  540. }
  541. }
  542. //没用的兜底
  543. $this->success('订单错误,请刷新重试');
  544. }
  545. }