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

namespace app\api\controller\v1;


//监测模型
use app\api\business\ModelBus;
use app\api\validate\ModelValidate;
use think\exception\ValidateException;

class Model
{
    //获取模型表
    public function selectModelList()
    {
        $data = [
            'type_id' => request()->param('type_id', '', 'int'),
            'pageNum' => request()->param('pageNum,', 1, 'int'),
            'pageSize' => request()->param('pageSize', 10, 'int'),
        ];

        try {
            validate(ModelValidate::class)->scene('selectQtModelList')->check($data);
        } catch (ValidateException $exception) {
            return showError($exception->getError());
        }
        $result = (new ModelBus())->selectModelList($data);
        return showSuccess($result);
    }

    public function bindModel()
    {
        $data=[
            'endtime_cydsb' => request()->param('endtime_cydsb', '', 'trim'),
            'is_divide'=>request()->param('is_divide', '0', 'int'),
            'test_model_id' => request()->param('test_model_id', '', 'int'),
            'task_id' => request()->param('id', '', 'int'),

        ];
        try {
            validate(ModelValidate::class)->scene('bindModel')->check($data);

        } catch (ValidateException $exception) {
            return showError($exception->getError());
        }

        $result = (new ModelBus())->updateInfoByTaskID($data);
        return showSuccess($result);
    }

    //获取模型信息
    public function getModelInfo()
    {

        $data['id'] = request()->param('id', '', 'int');
        try {
            validate(ModelValidate::class)->scene('getModelInfo')->check($data);

        } catch (ValidateException $exception) {
            return showError($exception->getError());
        }
        $result = (new ModelBus())->getModelInfo($data);
        return showSuccess($result);
    }

    //更新模型
    public function updateModelInfo()
    {

        //更新testModel;当提交时，修改updatetime
        $model_id = request()->param('id','','int');
        $model_info = [
            'name' => request()->param('name', '', 'trim'),
            'type_id' => request()->param('type_id', '', 'int'),
        ];

        $product=request()->param('product');
        $pesticides=request()->param('pesticides');
        //更新模型信息包括三步，1、更新test_model 2、删除&重新添加test_product 3、删除&重新添加test_pesticides
        $result = (new ModelBus())->updateModelInfo($model_id,$model_info,$product,$pesticides);//==true
        return showSuccess($result);
    }

    //新增模型
    public function createModelInfo()
    {
        //新增test_model表信息
        $model_type_id = request()->param('type_id');
        $model_name = request()->param('name');
        $product_data = request()->param('product');
        $pesticides_data = request()->param('pesticides');
        $result = (new ModelBus())->createModelInfo($model_name, $model_type_id,$product_data,$pesticides_data);
        return showSuccess($result);

    }

    //删除模型
    public function deleteModelInfo()
    {
        $del_arr = request()->param('delarr');

        $data=[];
        $data_count=0;
        foreach ($del_arr as $k => $v) {
            $data_count+=1;
            $k = ['id' => $v['id']];
            $data[]=$k;
        }
        $result = (new ModelBus())->deleteModelInfo($data,$data_count);
        return showSuccess($result);
    }

}