123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\data\controller\api;
- use app\data\controller\api\business\Order;
- use app\data\model\DataNewsItem;
- use app\data\model\DataNewsMark;
- use app\data\model\DataNewsXCollect;
- use app\data\model\DataUser;
- use app\data\model\ShopOrder;
- use app\data\model\ShopOrderPay;
- use app\data\model\ShopOrderSend;
- use app\data\model\SystemUser;
- use app\data\model\SystemUserAmount;
- use app\data\service\NewsService;
- use think\admin\Controller;
- use think\admin\model\SystemConfig;
- use think\exception\ErrorException;
- use think\facade\Db;
- /**
- * 定时脚本
- * Class News
- * @package app\data\controller\api
- */
- class Timedtask extends Controller
- {
- /**
- * 用户vip到期判断
- */
- public function checkuservip(){
- DataUser::mk()->where('is_vip',1)->field('id,is_vip,vip_datetime')->chunk(10,function ($list){
- $list = $list->toArray();
- foreach ($list as &$v) {
- if (strtotime($v['vip_datetime'])<time()){
- DataUser::mk()->where('id',$v['id'])->update(
- [
- 'is_vip'=>0,
- 'vip_name'=>'普通用户',
- 'update_at'=>date('Y-m-d H:i:s')
- ]
- );
- }
- }
- },'id','asc');
- }
- /**
- * 订单过期
- */
- public function checkshoporder(){
- $config = SystemConfig('merchant_config');
- $time = date('Y-m-d H:i:s',time()-($config['cancellation_time']*60));
- ShopOrder::mk()->where('status',2)->where('create_at','<',$time)->chunk(20,function ($list){
- $list = $list->toArray();
- foreach ($list as &$v){
- ShopOrder::mk()->where('id',$v['id'])->save(['status'=>0,'cancel_remark'=>'超时自动取消']);
- }
- });
- }
- /**
- * 订单退款
- */
- public function order_timeout_refund(){
- $config = SystemConfig('merchant_config');
- $timeout = $config['refund_time'];
- $time = date('Y-m-d H:i:s',time()-($timeout*60*60));
- ShopOrder::mk()->where('status',7)->where('refund_time','<',$time)->chunk(20,function ($list){
- $list = $list->toArray();
- foreach ($list as &$v){
- //同意退款
- $pay_money = ShopOrderPay::mk()->where(['status' => 2, 'pay_no'=>$v['payment_trade']])->value('money');
- if($v['payment_type']=='支付宝支付') {
- $result = json_decode(Order::refund_order($v['payment_trade'], $v['refund_money']), true);
- }
- if($v['payment_type']=='微信支付') {
- $result = json_decode(Order::wxrefund_order($v['payment_trade'], $v['refund_money'],$pay_money), true);
- }
- if($result) {
- ShopOrder::mk()->where('id',$v['id'])->save(['status' => 8, 'refund_status' => 2, 'dk_time' => date('Y-m-d H:i:s')]);
- }else{
- // $this->error('微信账户余额不足');
- }
- }
- });
- // $list = ShopOrder::mk()->where('status',7)->where('refund_time','<',$time)->select()->toArray();
- //// return json($list);
- //// $result = json_decode(Order::wxrefund_order('PAY20230909143935695142', 0.01,0.01), true);
- //// return $result;
- // foreach ($list as &$v){
- //// //同意退款
- // $pay_money = ShopOrderPay::mk()->where(['status' => 2, 'pay_no'=>$v['payment_trade']])->value('money');
- // if($v['payment_type']=='支付宝支付') {
- // if($pay_money){
- // $result = json_decode(Order::refund_order($v['payment_trade'], $v['refund_money']), true);
- // }else{
- // $result = false;
- // };
- // }
- // if($v['payment_type']=='微信支付') {
- // if($pay_money){
- // $result = json_decode(Order::wxrefund_order($v['payment_trade'], $v['refund_money'],$pay_money), true);
- // }else{
- // $result = false;
- // };
- //
- // }
- // if($result) {
- // ShopOrder::mk()->where('id',$v['id'])->save(['status' => 8, 'refund_status' => 2, 'dk_time' => date('Y-m-d H:i:s')]);
- // }else{
- //// return json($result);
- //// $this->error('微信账户余额不足');
- // }
- // }
- $this->success('执行完毕');
- }
- /**
- * 订单自动确认收货
- */
- public function order_timeout_end(){
- $order_timeout_end_time = SystemConfig('merchant_config.order_timeout_end_time');
- $outtime = date('Y-m-d H:i:s',time()-($order_timeout_end_time*60*60*24));
- $where = [
- 'deleted'=>0,
- ];
- $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();
- foreach ($list as $k=>$v){
- Db::startTrans();
- try {
- $order_info =ShopOrder::mk()->where(array('order_no'=>$v['order_no']))->field('amount_total,admin_id')->find();
- $user_money = SystemUser::mk()->where('id',$order_info['admin_id'])->value('money');
- ShopOrder::mk()->where(array('order_no'=>$v['order_no']))->save(['status'=>6]);
- $result = $user_money+$order_info['amount_total'];
- SystemUser::mk()->where('id',$order_info['admin_id'])->save(['money'=>$result]);
- $record = [
- 'admin_id' => $order_info['admin_id'],
- 'before' => $user_money,
- 'after' => $result,
- 'type' => 1,
- 'amount' => $order_info['amount_total'],
- 'form' => '商品订单',
- 'create_time' => date('y-m-d h:i:s')
- ];
- SystemUserAmount::mk()->insert($record);
- Db::commit();
- $this->success("确认收货");
- } catch (ErrorException $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e);
- }
- }
- }
- }
|