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

namespace app\api\controller\v1;

use app\api\business\JianGuanBus;
use app\api\business\SuperviserTaskBus;
use app\api\model\Jcdw;
use app\api\model\Orgs;
use app\api\validate\TaskValidate;
use think\exception\ValidateException;

//监管单位
class JianGuan
{

    //查找任务
    public function getTaskList()
    {
        //获取请求参数
        $data=[
            'pageNum' => request()->param('pageNum',1,'trim'),//页码
            'pageSize' => request()->param('pageSize',10,'trim'),//每页显示数量
            'task_class' => request()->param('task_class','','trim'),//任务列表：例行监测  专项监测
            'year'=>request()->param('year','','trim'),//年度
            'ispublic'=>request()->param('ispublic','','trim'),//任务状态：0：未发布，1：执行中，2：已结束未完成，3：已结束已完成
            'starttime' => request()->param('starttime','','trim'),//任务开始时间
            'endtime' => request()->param('endtime','','trim'),//任务结束时间
            'task_name' => request()->param('task_name','','trim'),//任务名称
        ];
        try {
            validate(TaskValidate::class)->scene('selectJgTaskList')->check($data);
        } catch (ValidateException $exception) {
            return showError($exception->getError());
        }

        $res= (new JianGuanBus())->selectTask($data);
        return showSuccess($res);
    }

    //添加任务
    public function addTaskItem()
    {
        //获取请求参数
        $data=[
            'task_name'=>request()->param('task_name','','trim'),//任务名称
            'level'=>request()->param('level','','trim'),//任务优先级，0：一般，1：紧急，2：特急
            'year'=>request()->param('year','','trim'),//年度
            'batch' => request()->param('batch', '', 'trim'),//批次，第一季度，第二季度，第三季度，第四季度
            'starttime' => request()->param('starttime','','trim'),//任务开始时间
            'endtime' => request()->param('endtime','','trim'),//任务结束时间
            'filenum' => request()->param('filenum', '', 'trim'),//文件号---选填
            'log' => request()->param('log', '', 'trim'),//备注---选填
            'file_url' => request()->param('file_url'),//附件地址
            'file_name' => request()->param('file_name', '', 'trim'),//附件名称
            'releaser' => request()->param('releaser', '', 'trim'),//发布单位
            'task_class' => request()->param('task_class', '', 'trim'),//任务类型：task_class
            'ispublic' => request()->param('ispublic', 0, 'trim'),//发布状态
            'main_unit' => request()->param('main_unit', '', 'trim'),//牵头单位 在orgs表中
        ];
        $orgs=request()->param('orgs',[],'trim');
        try {
            validate(TaskValidate::class)->scene('addJgTaskItem')->check($data);
        } catch (ValidateException $exception) {
            return showError($exception->getError());
        }

        $res= (new JianGuanBus())->createTask($data,$orgs);
        return showSuccess($res);
    }


    //删除任务
    public function deleteTaskItem()
    {
        $req_data=request()->param('delarr',[],'trim');
        $data=[];
        $data_count=0;
        foreach ($req_data as $k => $v) {
            $data[$k] = ['id' => $v];
            $data_count+=1;
        }
        $result = (new JianGuanBus())->deleteTaskItem($data,$data_count);
        return showSuccess($result);
    }

    //修改任务
    public function editTaskUpdateInfo()
    {

        $data = [];
        $data_count = 0;
        $request_data = request()->param('pubarr', []);
        foreach ($request_data as $k0 => $v0) {
            $data[$k0] = [
                'id' => $v0['id'],
                'ispublic'=>$v0['ispublic'],
                'releaser' => $v0['releaser'],
                'creater' => $v0['creater'],
            ];
            $data_count+=1;
        }

        $result = (new JianGuanBus())->updateTaskUpdateInfo($data,$data_count);
        return showSuccess($result);
    }


    //获取牵头单位名称和id
    public function getJgQtName()
    {
        $result = (new Jcdw())->getDistinctUnit();
        return showSuccess($result);
    }

}