Plantask.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Cache;
  6. use app\common\model\Wenzhenorder;
  7. class Plantask extends Controller
  8. {
  9. //未支付订单,下单N分钟后,自动取消
  10. public function auto_cancel_nopay_order(){
  11. $nowtime = time();
  12. $second = config('site.nopay_order_autocancel_minute') * 60;
  13. $lasttime = $nowtime - $second;
  14. //dump(datetime($lasttime));exit;
  15. $rs = Db::name('wenzhen_order')->where('status',0)->where('createtime','lt',$lasttime)->update([
  16. 'status' => 3,
  17. 'cancel_time' => $nowtime,
  18. 'cancel_reason' => '超时未支付',
  19. 'finish_time' => $nowtime,
  20. ]);
  21. echo $rs;
  22. }
  23. //待接诊订单,医生不操作,支付N分钟后,自动退诊,即时视频订单除外
  24. public function auto_tuizhen_noaccept_order(){
  25. $nowtime = time();
  26. $second = config('site.payorder_noaccept_autotuizhen_minute') * 60;
  27. $lasttime = $nowtime - $second;
  28. //dump(datetime($lasttime));exit;
  29. Db::startTrans();
  30. $wenzhen_order = Db::name('wenzhen_order')->where('status',10)->where('video_model','NEQ',2)->where('pay_time','lt',$lasttime)->order('id asc')->lock(true)->find();
  31. if(empty($order_list)){
  32. Db::rollback();
  33. echo 'empty';
  34. exit;
  35. }
  36. $order_id = $wenzhen_order['id'];
  37. //修改订单
  38. $update = [
  39. 'status' => 16,
  40. 'cancel_time' => $nowtime,
  41. 'cancel_reason' => '超时未接诊',
  42. 'finish_time' => $nowtime,
  43. ];
  44. //余额支付的直接退款
  45. if($wenzhen_order['comefrom'] == 1){
  46. if($wenzhen_order['pay_type'] == 'wallet'){
  47. //用户加钱
  48. $logtype = $wenzhen_order['ordertype'] == 1 ? 13 : 14;
  49. $rs_wallet = model('wallet')->lockChangeAccountRemain($wenzhen_order['user_id'],'money',$wenzhen_order['total_fee'],$logtype,$remark='超时未接诊','wenzhen_order',$order_id);
  50. if($rs_wallet['status'] === false){
  51. Db::rollback();
  52. echo $rs_wallet['msg'];
  53. exit;
  54. }
  55. //订单退款参数
  56. $update['refund_price'] = $wenzhen_order['total_fee'];
  57. $update['refund_status'] = 3;
  58. }elseif($wenzhen_order['pay_type'] == 'wechat' || $wenzhen_order['pay_type'] == 'alipay'){
  59. $update['refund_status'] = 1; //待退款
  60. //发起退款
  61. $Wenzhenorder_model = new Wenzhenorder;
  62. $refund_rs = $Wenzhenorder_model->old_refund($wenzhen_order,$wenzhen_order['total_fee']);
  63. if($refund_rs === true){
  64. $update['refund_status'] = 3; //退款完成
  65. }
  66. }
  67. }
  68. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->update($update);
  69. if(!$update_rs){
  70. Db::rollback();
  71. echo '退诊失败';
  72. exit;
  73. }
  74. Db::commit();
  75. echo '退诊成功'.$order_id.'结束';
  76. exit;
  77. }
  78. //app视频订单,已拨打,通话N分钟后,自动结束
  79. public function auto_finish_firstvideo_videoorder(){
  80. $nowtime = time();
  81. $second = config('site.firstvideo_videoorder_autofinish_minute') * 60;
  82. $lasttime = $nowtime - $second;
  83. $list = Db::name('wenzhen_order')->where('status',25)->where('ordertype',2)->where('comefrom',1)->where('video_time','lt',$lasttime)->limit(10)->select();
  84. if(empty($list)){
  85. echo 'empty';exit;
  86. }
  87. foreach($list as $key => $order){
  88. echo $order['id'].':'.$this->finish($order['id']);
  89. }
  90. }
  91. //TV视频订单,已拨打,通话N分钟后,自动结束
  92. public function auto_finish_firstvideo_videoorder_tv(){
  93. $nowtime = time();
  94. $second = config('site.tv_firstvideo_videoorder_autofinish_minute') * 60;
  95. $lasttime = $nowtime - $second;
  96. $list = Db::name('wenzhen_order')->where('status',25)->where('ordertype',2)->where('comefrom',2)->where('video_time','lt',$lasttime)->limit(10)->select();
  97. if(empty($list)){
  98. echo 'empty';exit;
  99. }
  100. foreach($list as $key => $order){
  101. echo $order['id'].':'.$this->finish($order['id']);
  102. }
  103. }
  104. //图文订单,已接诊,接诊N分钟后,自动结束
  105. public function auto_finish_accept_textorder(){
  106. $nowtime = time();
  107. $second = config('site.accept_textorder_autofinish_minute') * 60;
  108. $lasttime = $nowtime - $second;
  109. $list = Db::name('wenzhen_order')->where('status',20)->where('ordertype',1)->where('accept_time','lt',$lasttime)->limit(10)->select();
  110. if(empty($list)){
  111. echo 'empty';exit;
  112. }
  113. foreach($list as $key => $order){
  114. echo $order['id'].':'.$this->finish($order['id']);
  115. }
  116. }
  117. public function finish($order_id){
  118. //订单详情
  119. Db::startTrans();
  120. $wenzhen_order = Db::name('wenzhen_order')->where('id',$order_id)->lock(true)->find();
  121. if(empty($wenzhen_order)){
  122. Db::rollback();
  123. //$this->error('不存在的订单');
  124. return '不存在的订单';
  125. }
  126. if($wenzhen_order['ordertype'] == 1){
  127. if($wenzhen_order['status'] != 20){
  128. Db::rollback();
  129. //$this->error('已接诊订单才能完成');
  130. return '已接诊订单才能完成';
  131. }
  132. $old_status = 20;
  133. }
  134. if($wenzhen_order['ordertype'] == 2){
  135. if($wenzhen_order['status'] != 25){
  136. Db::rollback();
  137. //$this->error('尚未发起视频,不能结束');
  138. return '尚未发起视频,不能结束';
  139. }
  140. $old_status = 25;
  141. }
  142. //修改订单
  143. $nowtime = time();
  144. $update = [
  145. 'status' => 30,
  146. 'finish_time' => $nowtime,
  147. ];
  148. //给医生结算钱
  149. $logtype = $wenzhen_order['ordertype'] == 1 ? 111 : 112;
  150. if($wenzhen_order['comefrom'] == 1 && $wenzhen_order['total_fee'] > 0){
  151. $rs_wallet = model('walletdoctor')->lockChangeAccountRemain($wenzhen_order['doctor_id'],'money',$wenzhen_order['total_fee'],$logtype,$remark='问诊订单','wenzhen_order',$order_id);
  152. if($rs_wallet['status'] === false){
  153. Db::rollback();
  154. //$this->error($rs_wallet['msg']);
  155. return $rs_wallet['msg'];
  156. }
  157. }
  158. //订单状态
  159. $update_rs = Db::name('wenzhen_order')->where('id',$order_id)->where('status',$old_status)->update($update);
  160. if(!$update_rs){
  161. Db::rollback();
  162. //$this->error('操作失败');
  163. return '操作失败';
  164. }
  165. //医生接诊量
  166. Db::name('doctor')->where('id',$wenzhen_order['doctor_id'])->setInc('ordernum');
  167. Db::commit();
  168. //发送im消息给用户,让他不能再输入
  169. /*if($wenzhen_order['ordertype'] == 1){
  170. $tenim = new Tenim();
  171. $message = [
  172. 'businessID' => 'order_status_doctor_type',
  173. 'name' => '',
  174. 'status' => '30',
  175. 'id' => (string)$order_id,
  176. 'content' => '',
  177. ];
  178. $rs = $tenim->sendCustomMessageToUser('user'.$wenzhen_order['user_id'],'doctor'.$wenzhen_order['doctor_id'],$message);
  179. }*/
  180. //$this->success('操作成功');
  181. return '操作成功';
  182. }
  183. /////////////////////////////////////////////////////////////////////////
  184. //定时跑用户活跃,改成离线
  185. public function auto_user_active(){
  186. $actitime = time() - 7200;
  187. $sql = 'update `mt_user` set is_active = 0 where is_active = 1 and id in (select user_id from mt_user_active where requesttime < '.$actitime.')';
  188. db()->query($sql);
  189. }
  190. //vip过期的,三个隐私设置改成0
  191. //vip过期的,更新用户表is_vip
  192. public function auto_vipend(){
  193. // $sql = 'update `mt_user_power` set yinsi = 0,yinshen = 0,wuhen = 0 where user_id in (select user_id from mt_user_wallet where vip_endtime > 0 and vip_endtime < '.time().')';
  194. // db()->query($sql);
  195. /*$sql2 = 'update `mt_user` set is_vip = 0 where is_vip = 1 and user_id in (select user_id from mt_user_wallet where vip_endtime > 0 and vip_endtime < '.time().')';
  196. db()->query($sql2);*/
  197. }
  198. }