<?php
namespace app\api\model;
use think\Model;

/**
 * Created by PhpStorm
 * Author: ihavoc
 * Date: 2020/11/22
 * Time: 16:24
 *
 */
class Ccjc extends model{

    protected $table = 't_ccjc';
    protected $pk = 'id';


    function undertake()
    {
        return $this->hasMany('undertake', 'task_id', 'id');
    }

    function orgs()
    {
        return $this->hasOne('orgs', 'task_id', 'id');
    }

    public function selectCcjcAndOrgsData($task_uid,$task_class='例行监测',$pageNum=1,$pageSize=10)
    {
        $ccjc = new Ccjc();
        return $ccjc->where([['task_class', '=', $task_class],])->with([
            'orgs'=>function ($query) use ($task_uid){
                $query->where('unit_id',$task_uid);
            }
        ])->page((int)$pageNum,(int)$pageSize)->select();
    }


    public function selectUndertakeData($task_class,$where, $pageNum, $pageSize)
    {
        $result = Ccjc::hasWhere('undertake', function ($query) use ($where){
            $query->where($where);
        })->where('task_class','=',$task_class)->select();
        return $result;
    }
    public function countUndertakeData($task_class,$where)
    {
        $Ccjc = new Ccjc();
        return $Ccjc->where('task_class','=',$task_class)
            ->with([
                'undertake'=> function ($query) use ($where){
                    $query->where($where);
                }
            ])->count();
    }

    //查询任务列表数据
    public function getInfoByTaskClass($where,$pageNum=1,$pageSize=10)
    {
        return $this->where($where)->page($pageNum,$pageSize)->select();
    }

    public function getInfoListCount($where='')
    {
        return $this->where($where)->select()->count();
    }


    //删除任务
    public function deleteTaskByID($id)
    {
        if (empty($id)) {
            return false;
        }
        return $this->select($id)->delete();
    }

    //根据id查询任务发布者releaser
    public function getReleaserById($id)
    {
        return $this->select($id)->column('releaser');
    }

    //添加任务 返回添加数据的主键id
    public function createTask($data)
    {
        return $this->insertGetId($data);
    }
    public function getTaskInfoByTaskID($task_id)
    {
        return $this->where('id','=',$task_id)->find();
    }
    public function updateInfoByTaskID($task_id, $data)
    {
        return $this->where('id', '=', $task_id)->save($data);
    }

    /**
     * 更新任务状态
     * @param $arr_condition:['id'=>1,'task_name'=>'更改的名称','isPublish'=>'2']
     * @return false|\think\Collection
     * @throws \Exception
     */
    public function updateTaskByArrCondition($arr_condition)
    {
        if (empty($arr_condition)) {
            return false;
        }
        $result = $this->saveAll($arr_condition);
        return $result;
    }




}