<?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;

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

    /**
     * 任务结算
     */
    public function index()
    {
        $user_ids = UserModel::where('company_id', 175)->where('is_captain', 1)->field('id,nickname,brigade')->select();
        $shareholder_user = UserModel::where('id', 322)->field('id,nickname,brigade')->find();
        $money=bcdiv(6000,count($user_ids),2);
        $left_amount=0;
        foreach ($user_ids as $kkk => $vvv) {
            $left_amount+=bcadd(1000,$money,2);
            $company_log = [
                'sn' => generate_sn(UserAccountLog::class, 'sn', 20),
                'company_id'=>175,
                'change_object' => CompanyAccountLog::SHAREHOLDER,//变动对象
                'change_type' => CompanyAccountLog::TASK_INC_SHAREHOLDER_MONEY,//变动类型
                'action' => CompanyAccountLog::INC,//1-增加 2-减少
                'left_amount' =>$left_amount,//变动后数量
                'change_amount' => $money,//变动数量
                'remark' =>$shareholder_user['nickname'].'完成了'.$vvv['brigade'].'队的股金:'.$money.'元',
                'status'=>1,
            ];
            CompanyAccountLog::create($company_log);
        }
    }

    /**
     * 任务刷新
     */
    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);
    }

}