<?php
namespace app\api\controller;

use app\common\logic\task\TaskLogic;
use app\common\model\Company;
use app\common\model\company\CompanyAccountLog;
use app\common\model\task\Task;
use app\common\model\task_scheduling\TaskScheduling;
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
use app\common\model\task_template\TaskTemplate;
use app\common\model\user\User as UserModel;
use app\common\model\user\UserAccountLog;
use think\facade\Log;
use app\job\TaskAdd;
use app\job\TaskInformationJob;

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

    /**
     * 任务结算
     */
    public function index()
    {

        // $all=TaskSchedulingPlan::where('is_pay',0)->with(['template_info','scheduling'])->select()->toArray();
        // foreach($all as $k=>$v){
        //     queue(TaskInformationJob::class,$v);
        // }
return true;
        $plan_all = TaskScheduling::where('id',99)->with('company_info')->select()->toArray();
        $plan_ids = [];
        foreach ($plan_all as $k => $v) {
            $all = TaskTemplate::where('status', 1)->where('task_scheduling', $v['id'])->limit(30)->select()->toArray();
            $plan_ids[] = $v['id'];
            if ($all) {
                $plan_all[$k]['template'] = $all;
            } else {
                unset($plan_all[$k]);
            }
        }
        $company_id = [];
        foreach ($plan_all as $k => $v) {
            foreach ($v['template'] as $kk => $vv) {

                queue(TaskAdd::class,['data'=>$vv,'data_two'=>$v]);
            }
            $company_id[] = $v['company_id'];
        }
    }

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

        $time=strtotime(date('Y-m-d'));
        // $time=strtotime(date('Y-m-d'));
        // $tiem_end=$time+86399;where('cron_time','<',$time)->
        $plan_all=TaskScheduling::where('id',99)->with('company_info')->select()->toArray();
        foreach($plan_all as $k=>$v){
            $all=TaskTemplate::where('status',1)->where('task_scheduling',$v['id'])->limit(30)->select()->toArray();
            if($all){
                $plan_all[$k]['template']=$all;
            }else{
                unset($plan_all[$k]);
            }
        }
        $company_id=[];
        foreach($plan_all as $k=>$v){
            foreach($v['template'] as $kk=>$vv){
                TaskLogic::CronAdd($vv,$v);
            }
            $company_id[]=$v['company_id'];
        }
        Company::where('id','in',$company_id)->inc('day_count')->update();
    }

    public function stage_inspection($v,$moeny_type='moeny',$title='一'){
        $count_moeny=0;
        foreach($v['template'] as $kkkk=>$vvvv){
            $count_moeny+=$vvvv[$moeny_type];
        }
        if($count_moeny>200){
             Log::info($title.'阶段金额大于200无法下达'.json_encode($v['company_info']));
             return false;
        }
        return true;
    }
    //三轮车判断
    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(){
        $all=TaskSchedulingPlan::where('id',699)->where('is_pay',0)->with(['template_info','scheduling'])->select()->toArray();
        foreach($all as $k=>$data){
            // $company = Company::where('id', $data['company_id'])->field('id,deposit,company_money,user_id,day_count,company_type,province,city,area,street,village,brigade')->find(); // 可能要判断预存金是否满足
            $task = Task::where('id', $data['task_id'])->field('director_uid')->where('status', 3)->with('director_info')->find();
            $arr['money'] = $data['template_info']['money'];
            $arr['company_id'] = $data['scheduling']['company_id'];
            $arr['msg'] = '来自任务【' . $data['template_info']['title'] . '】,完成人,的任务结算';
            $arr['proportion_one'] = $data['template_info']['proportion_one'];
            $arr['proportion_two'] = $data['template_info']['proportion_two'];
            $arr['sn'] = $data['sn'];
            $arr['id'] = $data['id'];
        }
        halt($arr);
    }

}