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

namespace app\api\business;

use app\api\exception\ApiException;
use app\api\model\Ccjc;
use app\api\model\TestModel;
use app\api\model\TestPesticides;
use app\api\model\TestProduct;
use app\common\lib\auth\JwtAuth;

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

    //获取当前用户的模型页列表
    public function selectModelList($data)
    {
        $pageNum = $data['pageNum'];
        $pageSize = $data['pageSize'];
        $type_id = $data['type_id'];//1.种植业 2.畜牧业 3.渔业 4.其他

        $where = [];
        if (!empty($type_id)) {
            array_push($where, ['type_id', '=', (int)$type_id]);
        }
        $result = (new TestModel())->getListByUserId($this->uid,$pageNum,$pageSize,$where)->toArray();
        $count = (new TestModel())->countListByUserId($this->uid, $where);
        return ['rows'=>$result, 'total' => $count,];
    }

    //获取模型信息
    public function getModelInfo($model_id)
    {
        //todo 这里可以连表查询，可以多对多，以后再研究优化
        $model_info=(new TestModel())->getModelInfoByModelID($model_id);//获取模型信息
        $model_product=(new TestModel())->getProductInfo($model_id)->toArray();//检测对象
        $model_pesticides = (new TestModel())->getPesticidesInfo($model_id)->toArray();//检测项
        $data=[
            'name'=>$model_info,
            'product'=>$model_product,
            'pesticides'=>$model_pesticides
        ];
        return $data;
    }

    public function updateInfoByTaskID($task_id, $data)
    {
        $result = (new Ccjc())->updateInfoByTaskID($task_id, $data);
        return $result;
    }



    //修改模型信息
    public function updateModelInfo($model_id,$model_info,$product='',$pesticides='')
    {
////todo ------------- foreach有问题！！！ 直接复制的，要重写！！
        if ($product != '') {
            $product_update_data=[];
            foreach ($product as $k=>$v){
                $j=[];
                $j['test_model_id']=$model_id;
                $j['product_id']=$v['id'];
                $j['product_name']=$v['name'];
                $product_update_data[]=$j;
            }
        }

        if ($pesticides != '') {
            $pesticides_update_data=[];
            foreach ($pesticides as $i=>$o){
                $p=[];
                $p['test_model_id']=$model_id;
                $p['test_name']=$o['name'];
                $p['test_id']=$o['id'];
                $pesticides_update_data[]=$p;

            }
        }

        //更新model_test表中的信息
        (new TestModel())->updateModelInfoByModelID($model_id,$model_info);
        //删除原有Product中model_id的内容
        (new TestProduct())->deleteProductInfoByTestModelID($model_id);
        //删除原有Pesticides中model_id的内容
        (new TestPesticides())->deletePesticidesInfoByTestModelID($model_id);

        //添加新的Product中model_id的内容
        $updateProductInfo = (new TestProduct())->insertProductInfo($product_update_data);
        //添加新的Pesticides中model_id的内容
        $updatePesticidesInfo = (new TestPesticides())->insertPesticidesInfo($pesticides_update_data);

        if ($updateProductInfo == 0) {
            return showError(config('status.none_model_product'),config('status.none_model_product'));
        }
        if ($updatePesticidesInfo == 0) {
            return showError(config('status.none_model_pesticides'),config('status.none_model_pesticides'));
        }
        return true;
    }

    //新增模型
    public function createModelInfo($model_name,$type_id,$product_data,$pesticides_data)
    {
        $uid=$this->uid;
        $model_id = (new TestModel())->createModelInfo($model_name, $type_id, $uid);

        //格式化$product_data=》$product
        $product=[];
        //todo 需要重写foreach
        foreach ($product_data as $k=>$v){
            $j=[];
            $j['test_model_id']=$model_id;
            $j['product_id']=$v['id'];
            $j['product_name']=$v['name'];
            $product[]=$j;
        }
        //格式化$pesticides_data=》$pesticides
        $pesticides=[];
        foreach ($pesticides_data as $i=>$o){
            $p=[];
            $p['test_model_id']=$model_id;
            $p['test_name']=$o['name'];
            $p['test_id']=$o['id'];
            $pesticides[]=$p;
        }
        //todo 写到一个model里
        $update_test_product = (new TestProduct())->createProductInfo($product);
        if ($update_test_product == 0) {
            throw new ApiException(config('status.none_model_product'));
        }
        $update_test_pesticide =(new TestPesticides())->insertPesticidesInfo($pesticides);
        if ($update_test_pesticide == 0) {
            throw new ApiException(config('status.none_model_pesticides'));
        }
        return true;
    }

    //删除模型
    public function deleteModelInfo($del_arr)
    {
        //验证模型创建者是否一致
        foreach ($del_arr as $k => $v) {
            //从数据库获取当前模型的user_id
            if (empty($v['id'])) {
                throw new ApiException(config('status.none_model_info'));
            }
            $user_id = (new TestModel())->getUserIdByModelID($v['id'])['user_id'];
            if ($user_id != $this->uid) {
                throw new ApiException(config('status.none_authority'));
            }
            //删除test_model信息
            (new TestModel())->deleteModelInfoByID($v['id']);
            //删除test_product信息
            (new TestProduct())->deleteProductInfoByTestModelID($v['id']);
            //删除test_pesticides信息
            (new TestPesticides())->deletePesticidesInfoByTestModelID($v['id']);
        }
        return true;
    }



}