Timedtask.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\controller\api\business\Order;
  4. use app\data\model\DataNewsItem;
  5. use app\data\model\DataNewsMark;
  6. use app\data\model\DataNewsXCollect;
  7. use app\data\model\DataUser;
  8. use app\data\model\ShopOrder;
  9. use app\data\model\ShopOrderPay;
  10. use app\data\model\ShopOrderSend;
  11. use app\data\model\SystemUser;
  12. use app\data\model\SystemUserAmount;
  13. use app\data\service\NewsService;
  14. use think\admin\Controller;
  15. use think\admin\model\SystemConfig;
  16. use think\exception\ErrorException;
  17. use think\facade\Db;
  18. /**
  19. * 定时脚本
  20. * Class News
  21. * @package app\data\controller\api
  22. */
  23. class Timedtask extends Controller
  24. {
  25. /**
  26. * 用户vip到期判断
  27. */
  28. public function checkuservip(){
  29. DataUser::mk()->where('is_vip',1)->field('id,is_vip,vip_datetime')->chunk(10,function ($list){
  30. $list = $list->toArray();
  31. foreach ($list as &$v) {
  32. if (strtotime($v['vip_datetime'])<time()){
  33. DataUser::mk()->where('id',$v['id'])->update(
  34. [
  35. 'is_vip'=>0,
  36. 'vip_name'=>'普通用户',
  37. 'update_at'=>date('Y-m-d H:i:s')
  38. ]
  39. );
  40. }
  41. }
  42. },'id','asc');
  43. }
  44. /**
  45. * 订单过期
  46. */
  47. public function checkshoporder(){
  48. $config = SystemConfig('merchant_config');
  49. $time = date('Y-m-d H:i:s',time()-($config['cancellation_time']*60));
  50. ShopOrder::mk()->where('status',2)->where('create_at','<',$time)->chunk(20,function ($list){
  51. $list = $list->toArray();
  52. foreach ($list as &$v){
  53. ShopOrder::mk()->where('id',$v['id'])->save(['status'=>0,'cancel_remark'=>'超时自动取消']);
  54. }
  55. });
  56. }
  57. /**
  58. * 订单退款
  59. */
  60. public function order_timeout_refund(){
  61. $config = SystemConfig('merchant_config');
  62. $timeout = $config['refund_time'];
  63. $time = date('Y-m-d H:i:s',time()-($timeout*60*60));
  64. ShopOrder::mk()->where('status',7)->where('refund_time','<',$time)->chunk(20,function ($list){
  65. $list = $list->toArray();
  66. foreach ($list as &$v){
  67. //同意退款
  68. $pay_money = ShopOrderPay::mk()->where(['status' => 2, 'pay_no'=>$v['payment_trade']])->value('money');
  69. if($v['payment_type']=='支付宝支付') {
  70. $result = json_decode(Order::refund_order($v['payment_trade'], $v['refund_money']), true);
  71. }
  72. if($v['payment_type']=='微信支付') {
  73. $result = json_decode(Order::wxrefund_order($v['payment_trade'], $v['refund_money'],$pay_money), true);
  74. }
  75. if($result) {
  76. ShopOrder::mk()->where('id',$v['id'])->save(['status' => 8, 'refund_status' => 2, 'dk_time' => date('Y-m-d H:i:s')]);
  77. }else{
  78. // $this->error('微信账户余额不足');
  79. }
  80. }
  81. });
  82. // $list = ShopOrder::mk()->where('status',7)->where('refund_time','<',$time)->select()->toArray();
  83. //// return json($list);
  84. //// $result = json_decode(Order::wxrefund_order('PAY20230909143935695142', 0.01,0.01), true);
  85. //// return $result;
  86. // foreach ($list as &$v){
  87. //// //同意退款
  88. // $pay_money = ShopOrderPay::mk()->where(['status' => 2, 'pay_no'=>$v['payment_trade']])->value('money');
  89. // if($v['payment_type']=='支付宝支付') {
  90. // if($pay_money){
  91. // $result = json_decode(Order::refund_order($v['payment_trade'], $v['refund_money']), true);
  92. // }else{
  93. // $result = false;
  94. // };
  95. // }
  96. // if($v['payment_type']=='微信支付') {
  97. // if($pay_money){
  98. // $result = json_decode(Order::wxrefund_order($v['payment_trade'], $v['refund_money'],$pay_money), true);
  99. // }else{
  100. // $result = false;
  101. // };
  102. //
  103. // }
  104. // if($result) {
  105. // ShopOrder::mk()->where('id',$v['id'])->save(['status' => 8, 'refund_status' => 2, 'dk_time' => date('Y-m-d H:i:s')]);
  106. // }else{
  107. //// return json($result);
  108. //// $this->error('微信账户余额不足');
  109. // }
  110. // }
  111. $this->success('执行完毕');
  112. }
  113. /**
  114. * 订单自动确认收货
  115. */
  116. public function order_timeout_end(){
  117. $order_timeout_end_time = SystemConfig('merchant_config.order_timeout_end_time');
  118. $outtime = date('Y-m-d H:i:s',time()-($order_timeout_end_time*60*60*24));
  119. $where = [
  120. 'deleted'=>0,
  121. ];
  122. $list = ShopOrderSend::mk()->whereExists('select * from shop_order where shop_order.order_no=shop_order_send.order_no and shop_order.status=5')->where($where)->whereIn('status','1,2')->where('send_datetime','<',$outtime)->select();
  123. foreach ($list as $k=>$v){
  124. Db::startTrans();
  125. try {
  126. $order_info =ShopOrder::mk()->where(array('order_no'=>$v['order_no']))->field('amount_total,admin_id')->find();
  127. $user_money = SystemUser::mk()->where('id',$order_info['admin_id'])->value('money');
  128. ShopOrder::mk()->where(array('order_no'=>$v['order_no']))->save(['status'=>6]);
  129. $result = $user_money+$order_info['amount_total'];
  130. SystemUser::mk()->where('id',$order_info['admin_id'])->save(['money'=>$result]);
  131. $record = [
  132. 'admin_id' => $order_info['admin_id'],
  133. 'before' => $user_money,
  134. 'after' => $result,
  135. 'type' => 1,
  136. 'amount' => $order_info['amount_total'],
  137. 'form' => '商品订单',
  138. 'create_time' => date('y-m-d h:i:s')
  139. ];
  140. SystemUserAmount::mk()->insert($record);
  141. Db::commit();
  142. $this->success("确认收货");
  143. } catch (ErrorException $e) {
  144. // 回滚事务
  145. Db::rollback();
  146. $this->error($e);
  147. }
  148. }
  149. }
  150. }