<?php
namespace app\common\command;

use think\facade\Db;
use think\console\Input;
use think\console\Output;
use think\console\Command;
use app\common\model\works\ServiceWork;
use app\common\model\master_worker\MasterWorkerTeam;

class CancelDispatch extends Command
{
    protected function configure()
    {
        $this->setName('cancel_dispatch')
            ->setDescription('派单10分钟未领取的工单自动取消');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->cancelDispatch();

    }

    /*
    * 超过10分钟未领取工单的，取消分配
    */
    protected function cancelDispatch()
    {
        $list = ServiceWork::where('work_status',1)
                ->where('dispatch_time','<',strtotime('-10 minutes'))
                ->where('dispatch_time','>=',strtotime('-1 days'))
                ->field('id,tenant_id,master_worker_id,appointment_time')
                ->limit(100)
                ->select()
                ->toArray();
        foreach($list as $item) {
            ServiceWork::where('id',$item['id'])
                        ->update([
                            'master_worker_id' => 0,
                            'tenant_id' => 0,
                            'work_status' => 0,
                            'dispatch_time' => 0,
                            'first_contact_time' => 0,
                            'estimated_finish_time' => 0,
                            'exec_time' => 0,
                        ]);
            if ($item['tenant_id'] > 0) {
                $updateData = date("H",strtotime($item['appointment_time'])) < 12 ? ['am_order' => Db::raw('am_order - 1')] : ['pm_order' => Db::raw('pm_order - 1')];
                MasterWorkerTeam::where('master_worker_id',$item['master_worker_id'])->where('tenant_id',$item['tenant_id'])->update($updateData);
                
            }
        }
        
    }

}