UpdateApplyDevice.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\InnerDevice;
  4. use App\Models\Order;
  5. use App\Models\OrderDevice;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class UpdateApplyDevice extends Command
  9. {
  10. protected $name = 'updateApplyDevice';
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'command:apply';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description apply';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. /*
  40. * 归还时间逻辑 前天时间<订单结束时间<今天时间
  41. * 订单完成逻辑 订单里但凡有一个未到期,就算未完成,最后一个设备到期了,才算完成
  42. * 归还设备逻辑 到期的设备的状态直接变为闲置
  43. * */
  44. //当天日期
  45. $today_time = date('Y-m-d');
  46. Log::info($today_time."开始处理归还设备");
  47. //前天日期
  48. $before_yesterday_time = date('Y-m-d',strtotime('-2 days'));
  49. //获取到订单内有过期设备的订单数据
  50. $all_orders = OrderDevice::whereBetween('end_date',[$before_yesterday_time,$today_time])->whereNotNull('inner_device_id')->get(['order_id','inner_device_id'])->toArray();
  51. foreach ($all_orders as $value){
  52. //查找订单下是否有未过期的设备
  53. $conut = OrderDevice::where('order_id',$value['order_id'])->where('end_date','>',$today_time)->get()->count();
  54. //如果没有过期时间大于当天的设备就将他完成
  55. if ($conut == 0)
  56. {
  57. Log::info($value['order_id']."订单已完成");
  58. Order::where('id',$value['order_id'])->update(['status'=>3]);
  59. }
  60. //设备状态置为闲置
  61. Log::info($value['order_id']."订单".$value['inner_device_id']."设备闲置");
  62. InnerDevice::where('id',$value['inner_device_id'])->update(['status'=>6]);
  63. }
  64. }
  65. }