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

namespace app\api\business;

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

class ChengDanBus
{
    private $uid;

    public function __construct()
    {
        $jwtAuth = JwtAuth::getInstance();
        $this->uid = $jwtAuth->getUid();
    }


    public function deleteUnderTaker($del_arr)
    {
        $result=(new Undertake())->deleteInfoByID($del_arr);
        if ($result == 0) {
            throw new ApiException(config('status.none_data'));
        }
        return $result;
    }


    public function createUnderTakerInfo($data)
    {
        $uid = $this->uid;
        $bear_name = (new Jcdw())->getInfoById($data['bear_id'])['name'];
        $check_name = (new Jcdw())->getInfoById($data['check_id'])['name'];
        $report_name = (new Jcdw())->getInfoById($data['report_id'])['name'];
        $save_data=[
            'task_id' => $data['task_id'],
            'unit_id' => $uid,
            'bear_id' => $data['bear_id'],
            'check_id' => $data['check_id'],
            'report_id' => $data['report_id'],
            'bear_name' => $bear_name,
            'check_name' => $check_name,
            'report_name' => $report_name,
            'sample_number' => $data['sample_number'],
            'address' => $data['address'],
            'uploadtime' => $data['upload_time'],
        ];

        $result = (new Undertake())->insert($save_data,true);
        if ($result == null) {
            throw new ApiException(config('status.err_data_upload'));
        }
        return true;
    }

    //查询任务列表数据
    public function selectUndertakerTaskList($task_class,$pageNum,$pageSize,$data)
    {
        $where_undertake = [['bear_id|check_id|report_id', '=', $this->uid],];
        $where = [
            ['task_class','=',$task_class],
        ];
        if ($data['year']!='') $where[] = ['year', '=', $data['year']];
        if ($data['ispublic'] != '') $where[] = ['ispublic', '=', $data['ispublic']];       //任务状态：0：未发布，1：执行中，2：已结束未完成，3：已结束已完成
        if ($data['starttime'] != '') $where[]=['starttime', '>=', $data['starttime']];        //任务开始时间
        if ($data['endtime'] != '') $where[] = ['endtime', '<=', $data['endtime']];        //任务结束时间
        if ($data['task_name'] != '' ) $where[] = ['task_name', 'LIKE', '%' . $data['task_name'] . '%'];        //任务名称：模糊查询

        $data = (new Ccjc())->selectUndertakeData($where_undertake,$where,$pageNum,$pageSize);
        $total = (new Ccjc())->countUndertakeData($where_undertake,$where);
        return ['rows'=>$data,'total'=>$total];
    }

}