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

namespace app\api\business;

use app\api\exception\ApiException;
use app\api\model\Ccjc;
use app\api\model\Jcdw;
use app\api\model\Orgs;
use app\common\lib\Arr;
use app\common\lib\auth\JwtAuth;

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

    //查询任务列表数据
    public function selectTask($data)
    {
        //如果需要查询的字段不为空就push一个查询条件
        $where=[];//初始化$where
        //任务年度
        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']]);
        //任务分类：例行or专项
        if ($data['task_class'] != '') array_push($where, ['task_class', '=', $data['task_class']]);
        //任务名称：模糊查询
        if ($data['task_name'] != '' ) array_push($where, ['task_name', 'LIKE', '%'.$data['task_name'].'%']);

        $result = (new Ccjc())->getInfoByTaskClass($where,(int)$data['pageNum'],(int)$data['pageSize'])->toArray();
        $count = (new Ccjc())->getInfoListCount($where);
        return ['rows'=>$result, 'total'=>$count,];
    }

    //创建任务
    public function createTask($data,$orgs_data)
    {
        $data['releaser'] = $this->rymc;
        //创建任务
        $task_id = (new Ccjc())->createTask($data);

        for($i=0;$i<count($orgs_data);$i++){
            $orgs[$i]['task_id']=$task_id;
            $orgs[$i]['unit_id']=$orgs_data[$i]['id'];
            $orgs[$i]['unit_name']=$orgs_data[$i]['name'];
        }

        $result = (new Orgs())->createOrgsInfo($orgs);
        return $result;
    }

    //删除任务
    public function deleteTask($arr_del_task)
    {
        //根据ID检索创建者
        $releaser = (new Ccjc())->getReleaserById($arr_del_task);
        if (empty($releaser)) {
            throw new ApiException(config('status.none_data'));
        }
        //将创建者releaser和token中的rymc匹配，不相等则throw无权限操作
        foreach ($releaser as $k => $v) {
            if (strcmp($this->rymc, $v) != 0) {
                throw new ApiException(config('status.none_authority'));
            }
        }
        //进行删除操作
        $result = (new Ccjc())->deleteTaskByID($arr_del_task);
        return $result;
    }

    public function updateTask($data)
    {
        //过滤字段，保留id,ispublic,releaser
        $arr = (new Arr())->Arr_columns($data, 'id,ispublic,releaser');

        //校验 任务状态 && 操作权限
        foreach ($arr as $k=>$value) {
            if ($value['ispublic'] != 1) {
                throw new ApiException(config('status.err_public_status'));
            }
            if (strcmp($value['releaser'], $this->rymc) != 0) {
                throw new ApiException(config('status.none_authority'));
            }
        }
        $result = (new Ccjc())->updateTaskByArrCondition($arr);
        return $result;
    }
}