<?php
/**
 *
 *User:Administrator
 *Date:2021/10/12
 */

namespace app\api\business;

use app\api\exception\ApiException;
use app\api\model\Ccjc;
use app\api\model\Jgry;
use app\api\model\Orgs;
use app\api\model\TestModel;
use app\api\model\Undertake;
use app\common\lib\auth\JwtAuth;

class OrganizerTaskBus
{
    private $uid;
    public function __construct()
    {
        $jwtAuth=JwtAuth::getInstance();            //实例化jwtAuth
        $this->uid = $jwtAuth->getUid();          //setRybh是在middleware/jwtAuth中完成的
    }

    //查询任务列表数据
    public function selectTask($task_class,$pageNum,$pageSize,$data)
    {
        $where=[['unit_id','=',$this->uid],['task_class','=',$task_class]];

        if ($data['year']!='') array_push($where, ['year', '=', $data['year']]);
        //任务状态：0：未发布，1：执行中，2：已结束未完成，3：已结束已完成
        if ($data['ispublic'] != '') array_push($where, ['ispublic', '=', $data['ispublic']]);
        //任务开始时间
        if ($data['starttime'] != '') array_push($where, ['starttime', '>=', $data['starttime']]);
        //任务结束时间
        if ($data['endtime'] != '') array_push($where, ['endtime', '<=', $data['endtime']]);
        //任务名称：模糊查询
        if ($data['task_name'] != '' ) array_push($where, ['task_name', 'LIKE', '%'.$data['task_name'].'%']);

        $data = (new Orgs())->selectCcjcData($where,$pageNum,$pageSize);
        //$data = (new Ccjc())->selectCcjcAndOrgsData($this->uid);
        $count =(new Orgs())->countCcjcData($where);
        return ['rows'=>$data,'total'=>$count];
    }

    //牵头单位发布任务
    public function releaseTask($task_id)
    {
        $data=['ispublic' => 5];//发布任务
        $result = (new Ccjc())->updateInfoByTaskID($task_id, $data);
        return $result;
    }

    //获取任务基本信息
    public function getTaskInfo($task_id,$pageNum=1,$pageSize=null)
    {
        $uid=$this->uid;
//        //判断 在orgs表中的当前任务的id所对应的unit_id是否等于uid
//        $unit_id = (new Orgs())->getUnitIdByTaskID($task_id)['unit_id'];
//        if ($unit_id != $uid) {
//            throw new ApiException(config('status.err_task_auth'));
//        }
        //任务信息
        $task_info = (new Ccjc())->getTaskInfoByTaskID($task_id);
        $task_releaser_info = (new Jgry())->getJgryInfoByRymc($task_info['releaser']);
        //获取uid下创建的所有模型信息
        if (isset($task_info['test_model_id'])) {
            $task_product_info = (new TestModel())->getProductInfo($task_info['test_model_id']);
            $task_pesticides_info = (new TestModel())->getPesticidesInfo($task_info['test_model_id']);
            $task_model_info=(new TestModel())->getModelInfoByModelID($task_info['test_model_id']);

        }else{
            $task_product_info ='';
            $task_pesticides_info='';
            $task_model_info='';
        }
        //牵头的任务列表的信息(抽样)
        $unitTaskList_info=(new Undertake())->selectTaskListByTaskIdAndUid($task_id,$uid,'','',$pageNum,$pageSize);
        $unitTaskList_total = (new Undertake())->countTaskListByTaskIdAndUid($task_id, $uid);

        //承担的任务列表的信息(检测)
        $underTaskList_info=(new Undertake())->selectTaskListByTaskIdAndUid($task_id,'',$uid,'',$pageNum,$pageSize);
        $underTaskList_total = (new Undertake())->countTaskListByTaskIdAndUid($task_id, '',$uid);

        //报告上传的任务列表信息(报告上传)
        $reportTaskList_info=(new Undertake())->selectTaskListByTaskIdAndUid($task_id,'','',$uid,$pageNum,$pageSize);
        $reportTaskList_total = (new Undertake())->countTaskListByTaskIdAndUid($task_id, '','',$uid);



        $data=[
            'taskInfo'=>$task_info,
            'releaserInfo'=>$task_releaser_info,
            'product'=>$task_product_info,
            'pesticides'=>$task_pesticides_info,
            'unitTaskList'=>['rows'=>$unitTaskList_info,'total'=>$unitTaskList_total],
            'underTaskList'=>['rows'=>$underTaskList_info,'total'=>$underTaskList_total],
            'reportTaskList'=>['rows'=>$reportTaskList_info,'total'=>$reportTaskList_total],
            'modelInfo'=>$task_model_info,
        ];
        return $data;
    }



}