Wenzhen.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\library\Tenim;
  7. use app\common\model\Wenzhenorder;
  8. /**
  9. * 问诊订单
  10. */
  11. class Wenzhen extends Api
  12. {
  13. protected $noNeedLogin = '';
  14. protected $noNeedRight = '*';
  15. //创建订单
  16. public function create_order(){
  17. $member_id = input('member_id',0);
  18. $doctor_id = input('doctor_id',0);
  19. $ordertype = input('ordertype',0);
  20. $book_date = input('book_date',0); //2024-05-05
  21. $book_hour = input('book_time',0); //10:00
  22. $book_time = strtotime($book_date.' '.$book_hour);
  23. $member_disease = input('member_disease','');
  24. $member_images = input('member_images','');
  25. $auth_id = $this->auth->id;
  26. $order_no = createUniqueNo('W',$auth_id);
  27. //成员信息
  28. $member_info = Db::name('user_member')->where('user_id',$auth_id)->where('id',$member_id)->find();
  29. if(empty($member_info)){
  30. $this->error('不存在的就诊者');
  31. }
  32. //医生信息
  33. $doctor = Db::name('doctor')->where('id',$doctor_id)->find();
  34. $doctor_info = Db::name('doctor_info')->where('doctor_id',$doctor_id)->find();
  35. if(empty($doctor_info)){
  36. $this->error('不存在的医生');
  37. }
  38. //ordertype
  39. if(!in_array($ordertype,[1,2])){
  40. $this->error();
  41. }
  42. //价格
  43. $price = 0;
  44. if($ordertype == 1){
  45. if($doctor_info['typing_switch'] != 1){
  46. $this->error('该医生未开通图文问诊');
  47. }
  48. if($doctor['job_status'] != 1){
  49. $this->error('该医生现在不在上班中');
  50. }
  51. $price = $doctor_info['typing_price'];
  52. }
  53. if($ordertype == 2){
  54. if($doctor_info['video_switch'] != 1){
  55. $this->error('该医生未开通视频问诊');
  56. }
  57. if($doctor_info['video_model'] == 2 && $doctor['job_status'] != 1){ //即时方式,需要在上班中
  58. $this->error('该医生现在不在上班中');
  59. }
  60. $price = $doctor_info['video_price'];
  61. }
  62. /*if($price <= 0){
  63. $this->error('问诊价格为零');
  64. }*/
  65. //排班判断
  66. if($ordertype == 1){
  67. $book_time = 0;
  68. }
  69. if($ordertype == 2){
  70. if($doctor_info['video_model'] == 1){ //排班预约
  71. //检查
  72. if($book_time < time() + 7200){
  73. //测试临时屏蔽
  74. //$this->error('最早只能预约'.date('m-d H:00',time() + 10800)); //最早预约两个小时后的一个整点
  75. }
  76. //排班状态
  77. $doctor_paiban = Db::name('doctor_paiban')->where('doctor_id',$doctor_id)->where('activetime',$book_time)->find();
  78. if(empty($doctor_paiban)){
  79. $this->error('您预约的时间该医生无法接诊');
  80. }
  81. if($doctor_paiban['active'] != 1){
  82. $this->error('您预约的时间该医生无法接诊');
  83. }
  84. //是否排满
  85. $map = [
  86. 'doctor_id' => $doctor_id,
  87. 'book_time' => $book_time,
  88. 'ordertype' => 2,
  89. 'status' => ['IN','10,20,25,30'],//有效订单
  90. 'video_model' => 1,//预约模式
  91. ];
  92. $order_count = Db::name('wenzhen_order')->where($map)->count();
  93. if($order_count >= 4){
  94. $this->error('该时间点已排满,请重选预约时间');
  95. }
  96. }
  97. else
  98. {
  99. $book_time = time(); //即时预约
  100. }
  101. }
  102. //订单数据
  103. $order_data = [
  104. 'order_no' => $order_no,
  105. 'user_id' => $auth_id,
  106. 'member_id' => $member_id,
  107. 'doctor_id' => $doctor_id,
  108. 'ordertype' => $ordertype,
  109. 'total_fee' => $price,
  110. 'book_time' => $book_time,
  111. 'createtime' => time(),
  112. 'status' => 0,//订单状态enum
  113. 'video_model'=> $doctor_info['video_model'], //视频问诊预约方式
  114. ];
  115. Db::startTrans();
  116. //下单
  117. $order_id = Db::name('wenzhen_order')->insertGetId($order_data);
  118. if(!$order_id){
  119. Db::rollback();
  120. $this->error('下单失败');
  121. }
  122. //订单附表,就诊人信息
  123. $member_data = $member_info;
  124. unset($member_data['id']);
  125. $member_data['order_id'] = $order_id;
  126. $member_data['member_id'] = $member_info['id'];
  127. $member_data['member_disease'] = $member_disease;
  128. $member_data['member_images'] = $member_images;
  129. $order_member_id = Db::name('wenzhen_order_member')->insertGetId($member_data);
  130. if(!$order_member_id){
  131. Db::rollback();
  132. $this->error('下单失败');
  133. }
  134. //
  135. Db::commit();
  136. $this->success('下单成功',$order_id);
  137. }
  138. //支付订单
  139. public function pay_order(){
  140. $order_id = input('order_id',0);
  141. $pay_type = input('pay_type','wechat');
  142. //pay_type
  143. if(!in_array($pay_type,['wechat','alipay','wallet'])){
  144. $this->error();
  145. }
  146. //订单详情
  147. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  148. if(empty($wenzhen_order)){
  149. $this->error('不存在的订单');
  150. }
  151. if($wenzhen_order['status'] != 0){
  152. $this->error('该订单不能支付');
  153. }
  154. //如果订单是0元,医生设置免费0元,自动改成pay_type=wallet
  155. if($wenzhen_order['total_fee'] == 0){
  156. $pay_type = 'wallet';
  157. }
  158. if($pay_type == 'wallet'){
  159. Db::startTrans();
  160. //用户扣钱
  161. $logtype = $wenzhen_order['ordertype'] == 1 ? 11 : 12;
  162. if($wenzhen_order['total_fee'] > 0){
  163. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',-$wenzhen_order['total_fee'],$logtype,'图文问诊'.$wenzhen_order['order_no'],'wenzhen_order',$order_id);
  164. if($rs_wallet['status'] == false){
  165. Db::rollback();
  166. $this->error($rs_wallet['msg']);
  167. }
  168. }
  169. //订单改为已支付
  170. $wenzhen_update['status'] = 10;//订单状态enum
  171. $wenzhen_update['pay_type'] = $pay_type;
  172. $wenzhen_update['pay_time'] = time();
  173. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->where('status',0)->update($wenzhen_update);
  174. if(!$update_rs){
  175. Db::rollback();
  176. $this->error('支付失败,重试一下吧');
  177. }
  178. Db::commit();
  179. //发送im消息给医生
  180. if($wenzhen_order['ordertype'] == 1){
  181. $tenim = new Tenim();
  182. $message = [
  183. 'businessID' => 'order_status',
  184. 'name' => '待接单',
  185. 'status' => '10',
  186. 'id' => (string)$order_id,
  187. 'content' => '已通知医生尽快接诊,超时未接诊的将自动取消订单并退款',
  188. ];
  189. $rs = $tenim->sendCustomMessageToUser('user'.$this->auth->id,'doctor'.$wenzhen_order['doctor_id'],$message);
  190. }
  191. //输出
  192. $result = [
  193. 'pay_type' => $pay_type,
  194. 'pay_params' => '',
  195. ];
  196. $this->success(1,$result);
  197. }
  198. //创建订单
  199. $platform = 'app';
  200. $data = [];
  201. $data['user_id'] = $this->auth->id;
  202. $data['out_trade_no'] = createUniqueNo('W',$this->auth->id);
  203. $data['order_amount'] = $wenzhen_order['total_fee'];
  204. $data['createtime'] = time();
  205. $data['pay_type'] = $pay_type;
  206. $data['platform'] = $platform;
  207. $data['order_status'] = 0;
  208. $data['table_name'] = 'wenzhen_order';
  209. $data['table_id'] = $order_id;
  210. $data['args'] = '';
  211. $orderid = Db::name('pay_order')->insertGetId($data);
  212. $openid = '';
  213. //下单
  214. $params = [
  215. 'type' => $pay_type,
  216. 'orderid' => $data['out_trade_no'],
  217. 'title' => '支付订单',
  218. 'amount' => $data['order_amount'],
  219. 'method' => $platform,
  220. 'openid' => $openid,
  221. 'notifyurl' => config('pay_notify_url').'/api/notify/wenzhen_notify_base/paytype/'.$pay_type,
  222. 'returnurl' => '',
  223. ];
  224. $res = Service::submitOrder($params);
  225. $result = [
  226. 'pay_type'=> $pay_type,
  227. 'pay_params'=> $pay_type
  228. ];
  229. if($pay_type == 'wechat'){
  230. $result['pay_params'] = json_decode($res,true);
  231. }else{
  232. $result['pay_params'] = $res;
  233. }
  234. $this->success(1,$result);
  235. }
  236. //订单列表
  237. public function order_list(){
  238. $status = input('status','all');
  239. $where = ['user_id'=>$this->auth->id];
  240. if($status != 'all'){
  241. $where['order.status'] = $status;
  242. if($status == 20){// 已接诊、问诊中
  243. $where['order.status'] = ['IN',[20,25]];
  244. }
  245. }
  246. $field = [
  247. 'order.id','order.order_no','order.createtime','order.status','order.ordertype','order.accept_time','order.video_time','order.doctor_id','order.cancel_reason','order.comefrom','order.video_model',
  248. 'doctor.avatar as doctor_avatar','doctor.nickname as doctor_nickname',
  249. ];
  250. $list = Db::name('wenzhen_order')->alias('order')
  251. ->field($field)
  252. ->join('doctor','order.doctor_id = doctor.id','LEFT')
  253. ->order('order.id desc')
  254. ->where($where)
  255. ->autopage()->select();
  256. $list = list_domain_image($list,['doctor_avatar']);
  257. if(!empty($list)){
  258. //循环处理
  259. foreach($list as $key => $val){
  260. $val = $this->orderinfo_appen($val);
  261. //医生原因的退款,都直接算到退珍
  262. if(in_array($val['status'],[16,18,22])){
  263. $val['status'] = 100;
  264. }
  265. //视频拨打中 也算问诊中
  266. if($val['status'] == 25){
  267. $val['status'] = 20;
  268. }
  269. //视频订单是否能拨打,即时模式才可以
  270. $val['video_call_switch'] = 0;
  271. if($val['video_model'] == 2 && $val['ordertype'] == 2 && in_array($val['status'],[10,20,25])){ //三个状态都可以,理论上不存在20
  272. $val['video_call_switch'] = 1;
  273. }
  274. $list[$key] = $val;
  275. }
  276. }
  277. $this->success(1,$list);
  278. }
  279. //单个订单,追加数据
  280. private function orderinfo_appen($val){
  281. //订单类型
  282. $ordertype_data = [
  283. 1 => ['name'=>'图文订单','name_en'=>'Visual Order'],
  284. 2 => ['name'=>'视频订单','name_en'=>'Video Order'],
  285. ];
  286. //状态备注
  287. $status_data = $this->order_status_enum();
  288. //开始处理
  289. //订单类型,辅助
  290. $val['ordertype_data'] = $ordertype_data[$val['ordertype']];
  291. //订单状态,辅助
  292. $status_data_status = $val['status'];
  293. if($val['status'] == 10 && $val['video_model'] == 2){$status_data_status = 102;}
  294. $status_data_val = $status_data[$status_data_status];
  295. //未支付订单N分钟后自动取消
  296. if($val['status'] == 0){
  297. $remain_time = $val['createtime'] + (config('site.nopay_order_autocancel_minute')*60) - time();
  298. if($remain_time > 0){
  299. $status_data_val['minute'] = Sec2Time($remain_time);
  300. $status_data_val['minute_en'] = Sec2Time_en($remain_time);
  301. $status_data_val['seconds'] = $remain_time;
  302. }
  303. }
  304. //被医生退珍,追加原因
  305. if($val['status'] == 18){
  306. $status_data_val['name'] .= ':'.$val['cancel_reason'];
  307. }
  308. //图文订单接诊后N分钟自动结束
  309. if($val['status'] == 20 && $val['ordertype'] == 1){
  310. $remain_time = $val['accept_time'] + (config('site.accept_textorder_autofinish_minute')*60) - time();
  311. if($remain_time > 0){
  312. $status_data_val['minute'] = Sec2Time($remain_time);
  313. $status_data_val['minute_en'] = Sec2Time_en($remain_time);
  314. $status_data_val['seconds'] = $remain_time;
  315. }
  316. }
  317. //视频订单接通后N分钟自动结束
  318. if($val['status'] == 25 && $val['ordertype'] == 2){
  319. $minute = config('site.firstvideo_videoorder_autofinish_minute');
  320. if($val['comefrom'] == 2){
  321. $minute = config('site.tv_firstvideo_videoorder_autofinish_minute');
  322. }
  323. $remain_time = $val['video_time'] + ($minute*60) - time();
  324. if($remain_time > 0){
  325. $status_data_val['minute'] = Sec2Time($remain_time);
  326. $status_data_val['minute_en'] = Sec2Time_en($remain_time);
  327. $status_data_val['seconds'] = $remain_time;
  328. }
  329. }
  330. $val['status_data'] = $status_data_val;
  331. //处理完成,返回
  332. return $val;
  333. }
  334. //订单状态备注枚举
  335. private function order_status_enum(){
  336. //状态:0=未付款,3=已失效,10=待接诊,13=用户退诊,16=医生未接诊,18=医生退诊,20=已接诊,22=超时未拨打,25=视频通话中,30=问诊完成
  337. $status_data = [
  338. 0 => [
  339. 'name'=>'等待支付时间',
  340. 'name_en'=>'Waiting time for payment',
  341. 'minute' => '',
  342. 'minute_en' => '',
  343. 'seconds' => 0,
  344. ],
  345. 3 => [
  346. 'name'=>'您的订单未及时支付,已失效',
  347. 'name_en'=>'Your order was not paid on time and has expired.',
  348. 'minute' => '',
  349. 'minute_en' => '',
  350. 'seconds' => 0,
  351. ],
  352. 10 => [
  353. 'name'=>'等待医生接诊:已通知医生尽快接诊,超时自动取消订单并退款',
  354. 'name_en'=>'Waiting for the doctor. If not accepted within the specified time,the consultation will be automatically canceled, and a refund will be issued',
  355. 'minute' => '',
  356. 'minute_en' => '',
  357. 'seconds' => 0,
  358. ],
  359. //给即时视频用的
  360. 102 => [
  361. 'name'=>'视频问诊:您可立即发起视频问诊',
  362. 'name_en'=>'Video consultation: You can initiate a video consultation immediately',
  363. 'minute' => '',
  364. 'minute_en' => '',
  365. 'seconds' => 0,
  366. ],
  367. 13 => [
  368. 'name'=>'您的订单已退珍',
  369. 'name_en'=>'Your order has been canceled',
  370. 'minute' => '',
  371. 'minute_en' => '',
  372. 'seconds' => 0,
  373. ],
  374. 16 => [
  375. 'name'=>'医生未接诊',
  376. 'name_en'=>'Doctor Not received',
  377. 'minute' => '',
  378. 'minute_en' => '',
  379. 'seconds' => 0,
  380. ],
  381. 18 => [
  382. 'name'=>'退珍原因',
  383. 'name_en'=>'',
  384. 'minute' => '',
  385. 'minute_en' => '',
  386. 'seconds' => 0,
  387. ],
  388. 20 => [
  389. 'name'=>'医生已接诊,咨询中',
  390. 'name_en'=>'',
  391. 'minute' => '',
  392. 'minute_en' => '',
  393. 'seconds' => 0,
  394. ],
  395. 22 => [
  396. 'name'=>'医生超时未拨打视频',
  397. 'name_en'=>'',
  398. 'minute' => '',
  399. 'minute_en' => '',
  400. 'seconds' => 0,
  401. ],
  402. 25 => [
  403. 'name'=>'视频问诊进行中',
  404. 'name_en'=>'Time Remaining',
  405. 'minute' => '',
  406. 'minute_en' => '',
  407. 'seconds' => 0,
  408. ],
  409. 30 => [
  410. 'name'=>'点击查看医生反馈结果',
  411. 'name_en'=>'Click to view the consultation results',
  412. 'minute' => '',
  413. 'minute_en' => '',
  414. 'seconds' => 0,
  415. ],
  416. ];
  417. return $status_data;
  418. }
  419. //订单详情
  420. public function order_info(){
  421. $order_id = input('order_id',0);
  422. //订单详情
  423. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  424. if(empty($wenzhen_order)){
  425. $this->error('不存在的订单');
  426. }
  427. $wenzhen_order = info_domain_image($wenzhen_order,['feedback_images']);
  428. $wenzhen_order = $this->orderinfo_appen($wenzhen_order);
  429. //就诊人详情
  430. $order_member_info = Db::name('wenzhen_order_member')->where('order_id',$order_id)->find();
  431. $order_member_info = info_domain_image($order_member_info,['member_images']);
  432. $order_member_info['age'] = birthtime_to_age($order_member_info['birthday']);
  433. $wenzhen_order['member_info'] = $order_member_info;
  434. //医生详情
  435. $field = [
  436. 'd.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum',
  437. 'keshi.name as keshi_name',
  438. 'level.name as level_name'
  439. ];
  440. $doctor_info = Db::name('doctor')->alias('d')
  441. ->field($field)
  442. ->join('doctor_level level','d.level_id = level.id','LEFT')
  443. ->join('keshi','d.keshi_id = keshi.id','LEFT')
  444. ->where('d.id',$wenzhen_order['doctor_id'])->find();
  445. $doctor_info = info_domain_image($doctor_info,['avatar']);
  446. $wenzhen_order['doctor_info'] = $doctor_info;
  447. //视频订单是否能拨打,即时模式才可以
  448. $wenzhen_order['video_call_switch'] = 0;
  449. if($wenzhen_order['video_model'] == 2 && $wenzhen_order['ordertype'] == 2 && in_array($wenzhen_order['status'],[10,20,25])){ //三个状态都可以,理论上不存在20
  450. $wenzhen_order['video_call_switch'] = 1;
  451. }
  452. $this->success(1,$wenzhen_order);
  453. }
  454. //编辑订单里的就诊人资料
  455. public function edit_member(){
  456. $order_id = input('order_id',0);
  457. //订单详情
  458. $wenzhen_order_member = Db::name('wenzhen_order_member')->where('user_id',$this->auth->id)->where('order_id',$order_id)->find();
  459. if(empty($wenzhen_order_member)){
  460. $this->error('不存在的订单');
  461. }
  462. $update = [
  463. 'member_disease' => input('member_disease',''),
  464. 'member_images' => input('member_images',''),
  465. ];
  466. Db::name('wenzhen_order_member')->where('user_id',$this->auth->id)->where('order_id',$order_id)->update($update);
  467. $this->success();
  468. }
  469. //用户发起退珍
  470. //因为存在第三方,悲观锁
  471. public function tuizhen(){
  472. $apilimit = $this->apiLimit();
  473. if(!$apilimit){
  474. $this->error('操作频繁');
  475. }
  476. //订单详情
  477. $order_id = input('order_id',0);
  478. Db::startTrans();
  479. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->lock(true)->find();
  480. if(empty($wenzhen_order)){
  481. Db::rollback();
  482. $this->error('不存在的订单');
  483. }
  484. //仅限视频订单
  485. if($wenzhen_order['ordertype'] == 1){
  486. Db::rollback();
  487. $this->error('图文问诊不能退诊');
  488. }
  489. //下面全是视频订单
  490. //状态限制
  491. // if($wenzhen_order['video_model'] == 1){
  492. if(!in_array($wenzhen_order['status'],[10])){
  493. Db::rollback();
  494. $this->error('当前订单不能退诊');
  495. }
  496. //超过支付时间一小时不能退
  497. if($wenzhen_order['status'] == 10 && (time() - $wenzhen_order['pay_time'] > 3600)){
  498. Db::rollback();
  499. $this->error('支付超过一小时,不能退诊');
  500. }
  501. /*}else{
  502. //即时模式,已付款没接的能退
  503. if($wenzhen_order['status'] != 10){
  504. Db::rollback();
  505. $this->error('当前订单不能退诊');
  506. }
  507. }*/
  508. //修改订单
  509. $nowtime = time();
  510. $update = [
  511. 'status' => 13,
  512. 'cancel_time' => $nowtime,
  513. 'cancel_reason' => '用户付款后退诊',
  514. 'finish_time' => $nowtime,
  515. ];
  516. //退款要打折扣
  517. $wenzhen_refund_bili = config('site.payorder_userrefund_bili') ?: 70;
  518. $refund_price = bcdiv(bcmul($wenzhen_order['total_fee'],$wenzhen_refund_bili,2),100,2);
  519. //余额支付的直接退款
  520. if($wenzhen_order['pay_type'] == 'wallet'){
  521. //用户加钱
  522. $logtype = $wenzhen_order['ordertype'] == 1 ? 13 : 14;
  523. $rs_wallet = model('wallet')->lockChangeAccountRemain($wenzhen_order['user_id'],'money',$refund_price,$logtype,$remark='用户问诊退珍','wenzhen_order',$order_id);
  524. if($rs_wallet['status'] === false){
  525. Db::rollback();
  526. $this->error($rs_wallet['msg']);
  527. }
  528. //订单退款参数
  529. $update['refund_price'] = $refund_price;
  530. $update['refund_status'] = 3;
  531. }elseif($wenzhen_order['pay_type'] == 'wechat' || $wenzhen_order['pay_type'] == 'alipay'){
  532. $update['refund_status'] = 1; //待退款
  533. //发起退款
  534. $Wenzhenorder_model = new Wenzhenorder;
  535. $refund_rs = $Wenzhenorder_model->old_refund($wenzhen_order,$refund_price);
  536. if($refund_rs === true){
  537. $update['refund_status'] = 3; //退款完成
  538. }
  539. }
  540. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  541. if(!$update_rs){
  542. Db::rollback();
  543. $this->error('退诊失败');
  544. }
  545. Db::commit();
  546. $this->success('退珍成功');
  547. }
  548. ///////////////////////////////视频即时问诊///////////////////////////
  549. //第一次拨打视频之前,20,25可以调用
  550. public function before_first_video(){
  551. //订单详情
  552. $order_id = input('order_id',0);
  553. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  554. if(empty($wenzhen_order)){
  555. $this->error('不存在的订单');
  556. }
  557. if($wenzhen_order['ordertype'] != 2){
  558. $this->error('视频问诊才能拨打视频');
  559. }
  560. if($wenzhen_order['video_model'] != 2){
  561. $this->error('即时视频问诊才能主动拨打视频');
  562. }
  563. if($wenzhen_order['status'] == 30){
  564. $this->error('订单已结束');
  565. }
  566. if($wenzhen_order['status'] == 25){
  567. //判断医生在线状态
  568. /*$tenim = new Tenim();
  569. $rs_online = $tenim->is_online('doctor'.$wenzhen_order['doctor_id']);
  570. if($rs_online != true){
  571. $this->error('医生现在不在线,请稍后拨打');
  572. }*/
  573. $this->success();
  574. }
  575. //20直接忽略
  576. if($wenzhen_order['status'] != 10){
  577. $this->error('订单错误,请刷新重试');
  578. }
  579. //判断患者在线状态
  580. /*$tenim = new Tenim();
  581. $rs_online = $tenim->is_online('doctor'.$wenzhen_order['doctor_id']);
  582. if($rs_online != true){
  583. $this->error('医生现在不在线,请稍后拨打');
  584. }*/
  585. $this->success('操作成功');
  586. }
  587. //第一次拨打视频
  588. public function first_video(){
  589. $apilimit = $this->apiLimit();
  590. if(!$apilimit){
  591. $this->error('操作频繁');
  592. }
  593. //订单详情
  594. $order_id = input('order_id',0);
  595. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  596. if(empty($wenzhen_order)){
  597. $this->error('不存在的订单');
  598. }
  599. if($wenzhen_order['ordertype'] != 2){
  600. $this->error('视频问诊才能拨打视频');
  601. }
  602. if($wenzhen_order['video_model'] != 2){
  603. $this->error('即时视频问诊才能主动拨打视频');
  604. }
  605. if($wenzhen_order['status'] == 30){
  606. $this->error('订单已结束');
  607. }
  608. if($wenzhen_order['status'] == 25){
  609. //倒计时
  610. $minute = config('site.firstvideo_videoorder_autofinish_minute');
  611. if($wenzhen_order['comefrom'] == 2){
  612. $minute = config('site.tv_firstvideo_videoorder_autofinish_minute');
  613. }
  614. $rs = [
  615. 'video_time' => $wenzhen_order['video_time'],
  616. 'video_endtime' => $wenzhen_order['video_time'] + $minute*60,
  617. ];
  618. $rs['seconds'] = $rs['video_endtime'] - time();
  619. //修改最后拨通时间
  620. $update = [
  621. 'last_video_time' => time(),
  622. ];
  623. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  624. $this->success('操作成功',$rs);
  625. }
  626. //20直接忽略
  627. if($wenzhen_order['status'] != 10){
  628. $this->error('订单错误,请刷新重试');
  629. }
  630. //修改订单
  631. $update = [
  632. 'status' => 25,
  633. 'video_time' => time(),
  634. 'last_video_time' => time(),
  635. ];
  636. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->where('status',10)->update($update);
  637. if(!$update_rs){
  638. $this->error('操作失败');
  639. }
  640. //倒计时
  641. $minute = config('site.firstvideo_videoorder_autofinish_minute');
  642. if($wenzhen_order['comefrom'] == 2){
  643. $minute = config('site.tv_firstvideo_videoorder_autofinish_minute');
  644. }
  645. $rs = [
  646. 'video_time' => $update['video_time'],
  647. 'seconds' => $minute*60,
  648. 'video_endtime' => $update['video_time'] + $minute*60,
  649. ];
  650. $this->success('操作成功',$rs);
  651. }
  652. //挂断,医生挂断。这个方法主要用来统计是否已满足一分钟
  653. public function video_end(){
  654. $apilimit = $this->apiLimit();
  655. if(!$apilimit){
  656. $this->error('操作频繁');
  657. }
  658. $end_rule = input('end_rule','user'); //user 用户挂断,doctor 医生挂断。如果是医生挂断并且这个订单一次都没有接通,那就直接按拒绝接诊,结束订单
  659. //订单详情
  660. $order_id = input('order_id',0);
  661. $wenzhen_order = Db::name('wenzhen_order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  662. if(empty($wenzhen_order)){
  663. $this->error('不存在的订单');
  664. }
  665. if($wenzhen_order['ordertype'] != 2){
  666. $this->error('视频问诊才能拨打视频');
  667. }
  668. if($wenzhen_order['video_model'] != 2){
  669. $this->error('即时视频问诊才能主动拨打视频');
  670. }
  671. if($wenzhen_order['status'] != 25 && $wenzhen_order['status'] != 10){
  672. $this->error('订单错误,请刷新重试');
  673. }
  674. $nowtime = time();
  675. //接通了
  676. if($wenzhen_order['status'] == 25){
  677. //本次不超过1分钟
  678. if($nowtime - $wenzhen_order['last_video_time'] < 60 ){
  679. $this->success('已挂断');
  680. }
  681. //如果本次挂断之前已通话超过一分钟,那自动结束
  682. Db::startTrans();
  683. $wenzhen_order = Db::name('wenzhen_order')->where('id',$order_id)->lock(true)->find();
  684. $update = [
  685. 'status' => 30,
  686. 'finish_time' => $nowtime,
  687. ];
  688. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  689. if(!$update_rs){
  690. Db::rollback();
  691. $this->error('操作失败');
  692. }
  693. //给医生结算钱
  694. $logtype = $wenzhen_order['ordertype'] == 1 ? 111 : 112;
  695. if($wenzhen_order['comefrom'] == 1 && $wenzhen_order['total_fee'] > 0){
  696. $rs_wallet = model('walletdoctor')->lockChangeAccountRemain($wenzhen_order['doctor_id'],'money',$wenzhen_order['total_fee'],$logtype,$remark='问诊订单','wenzhen_order',$order_id);
  697. if($rs_wallet['status'] === false){
  698. Db::rollback();
  699. $this->error($rs_wallet['msg']);
  700. }
  701. }
  702. //医生接诊量
  703. Db::name('doctor')->where('id',$wenzhen_order['doctor_id'])->setInc('ordernum');
  704. Db::commit();
  705. $this->success('已挂断');
  706. }
  707. //没接通
  708. if($wenzhen_order['status'] == 10){
  709. if($end_rule == 'user'){
  710. $this->success('已挂断');
  711. }else{
  712. //如果是医生挂断并且这个订单一次都没有接通,那就直接按拒绝接诊,结束订单
  713. //参考医生发起退珍
  714. Db::startTrans();
  715. $wenzhen_order = Db::name('wenzhen_order')->where('id',$order_id)->lock(true)->find();
  716. //修改订单
  717. $nowtime = time();
  718. $update = [
  719. 'status' => 16,
  720. 'cancel_time' => $nowtime,
  721. 'cancel_reason' => '医生挂断未接诊',
  722. 'finish_time' => $nowtime,
  723. ];
  724. //余额支付的直接退款
  725. if($wenzhen_order['pay_type'] == 'wallet'){
  726. //用户加钱
  727. $logtype = $wenzhen_order['ordertype'] == 1 ? 13 : 14;
  728. $rs_wallet = model('wallet')->lockChangeAccountRemain($wenzhen_order['user_id'],'money',$wenzhen_order['total_fee'],$logtype,$remark='医生挂断未接诊','wenzhen_order',$order_id);
  729. if($rs_wallet['status'] === false){
  730. Db::rollback();
  731. // $this->error($rs_wallet['msg']);
  732. $this->error('医生挂断了');
  733. }
  734. //订单退款参数
  735. $update['refund_price'] = $wenzhen_order['total_fee'];
  736. $update['refund_status'] = 3;
  737. }else{
  738. $update['refund_status'] = 1; //待退款
  739. //发起退款
  740. $Wenzhenorder_model = new Wenzhenorder;
  741. $refund_rs = $Wenzhenorder_model->old_refund($wenzhen_order,$wenzhen_order['total_fee']);
  742. if($refund_rs === true){
  743. $update['refund_status'] = 3; //退款完成
  744. }
  745. }
  746. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  747. if(!$update_rs){
  748. Db::rollback();
  749. $this->error('医生挂断了');
  750. }
  751. Db::commit();
  752. $this->success('医生挂断并取消了订单');
  753. }
  754. }
  755. //没用的兜底
  756. $this->success('订单错误,请刷新重试');
  757. }
  758. }