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

namespace app\api\controller\v1;


//监测模型
use app\api\business\ModelBus;

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'),
        ];

        //todo validate

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

    public function bindModel()
    {
        $task_id = request()->param('id', '', 'int');
        $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')
        ];
        $result = (new ModelBus())->updateInfoByTaskID($task_id,$data);
        return showSuccess($result);
    }

    //获取模型信息
    public function getModelInfo()
    {
        $model_id = request()->param('id','','int');
        $result = (new ModelBus())->getModelInfo($model_id);
        return showSuccess($result);
    }

    //更新模型
    public function updateModelInfo()
    {
        //更新testModel;当提交时，修改updatetime
        $model_id = request()->param('id','','int');
        $model_type_id = request()->param('type_id','','int');
        $model_name = request()->param('name','','trim');

        $model_info = ['name' => $model_name, 'type_id' => $model_type_id];
        $product=request()->param('product');
        $pesticides=request()->param('pesticides');

        //todo 下面这个修改在不修改本表时无法自增updatetime，需要修改一下
        //更新模型信息包括三步，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');
        $result = (new ModelBus())->deleteModelInfo($del_arr);
        return showSuccess($result);
    }

    public function delModel(){
        $data = $this->request->post();
        $delarr = $data['delarr'];

        $id = '';
        for($j = 0; $j<count($delarr) ; $j++){
            if ($j == 0){
                $id = $delarr[$j]['id'];
            }else{
                $id = $delarr[$j]['id'].",".$id;
            }
        }

        $map=[];
        $map=[['test_model_id','in',$id]];

        $map1 = [['id','in',$id]];

        $result1 = Db::name("test_product")->where($map)->delete();

        $result2 = Db::name("test_pesticides")->where($map)->delete();

        $result3 = Db::name("test_model")->where($map1)->delete();


        if($result1 && $result2 && $result3){
            return $this->jsonData(0,"信息删除成功");
        }else{
            return $this->jsonData(-1,"信息删除失败");
        }
        return $this->jsonSuccessData($id);
    }
}