<?php
namespace app\api\controller;

use app\common\logic\finance\ShareProfit;
use app\common\logic\task\TaskLogic;
use app\common\model\Company;
use app\common\model\task\Task;
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
use app\common\model\task_template\TaskTemplate;
use think\facade\Log;
use app\job\TaskInformationJob;
use Symfony\Component\HttpClient\HttpClient;

class CeshiController extends BaseApiController
{
    public array $notNeedLogin = ['index','ceshi','ceshi1','ceshi2'];

    /**
     * 任务结算
     */
    public function index()
    {
//         $all=TaskSchedulingPlan::whereDay('end_time','yesterday')->where('is_pay',0)->with(['template_info','scheduling'])->select()->toArray();
// halt($all);
        $all=TaskSchedulingPlan::whereDay('end_time')->where('is_pay',0)->with(['template_info','scheduling'])->select()->toArray();
        foreach($all as $k=>$v){
            queue(TaskInformationJob::class,$v);
        }
        halt(1);
    }

    /**
     * 任务刷新
     */
    public function ceshi(){

        $time=strtotime(date('Y-m-d'));
        // $time=strtotime(date('Y-m-d'));
        // $tiem_end=$time+86399;where('cron_time','<',$time)->
        $all=TaskTemplate::where('status',1)->with('company')->select()->toArray();
        // $all=TaskTemplate::where('id',73)->with('company')->select()->toArray();
        // $all=TaskSchedulingPlan::where('start_time','between',[$time,$tiem_end])->where('is_execute',0)->with(['template_info','scheduling'])->select()->toArray();
        $company_id=[];
        foreach($all as $k=>$v){
            TaskLogic::CronAdd($v);
            $company_id[]=$v['company_id'];
        }
        $arr=array_unique($company_id);
        if(count($arr)){
            Company::where('id','in',$arr)->inc('day_count')->update();
        }
    }

    //三轮车判断
    public function ceshi1(){
        $params=[
            'car_id'=>3,
            'start_time'=>'2023-08-18',
            'end_time'=>'2023-08-18 23:59:59'
        ];
        $task=Task::where('id',487)->select()->toArray();
        foreach($task as $k=>$v){
            if(isset($v['extend']['terminus']['lnglat'])&& isset($v['extend']['transfer']['lnglat'])){
                $arr=$v['extend']['terminus']['lnglat'];
                $a=app(RemoteController::class)->coordinate($params,$arr[0],$arr[1]);
                
                $arr_two=$v['extend']['transfer']['lnglat'];
                $b=app(RemoteController::class)->coordinate($params,$arr_two[0],$arr_two[1]);
                if($a<500 && $b<500){
                    Task::where('id',$v['id'])->update(['status'=>3]);
                }
            }

        }
    }

    // public function ceshi2(){
    //     $
    //     $b=app(RemoteController::class)->shang_date_list('');

    // }

}