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

namespace app\api\model;

use think\Model;

class TestModel extends Model
{
    protected $table = 't_test_model';
    protected $pk = 'id';

    //关联模型一对多定义
    public function testProduct()
    {
        return $this->hasMany(TestProduct::class, 'test_model_id', 'id');
    }
    //关联模型一对多定义
    public function testPesticides()
    {
        return $this->hasMany(TestPesticides::class, 'test_model_id', 'id');
    }

    //关联模型一对多使用
    public function getProductInfo($model_id,$where='')
    {
        $models = TestModel::find($model_id);
        $productInfo = $models->testProduct()->where($where)->select();
        return $productInfo;
    }

    //关联模型一对多使用
    public function getPesticidesInfo($model_id,$where='')
    {
        $models = TestModel::find($model_id, $where = '');
        $pesticides = $models->testPesticides()->where($where)->select();
        return $pesticides;
    }

    /**
     * @param $uid
     * @param int $pageNum
     * @param int $pageSize
     * @param array $where:type_id为int
     */
    public function getListByUserId($uid,$pageNum=1,$pageSize=10,$where=[])
    {
        return $this->where('user_id','=',(int)$uid)->where($where)->page($pageNum,$pageSize)->select();
    }

    public function countListByUserId($uid, $where = [])
    {
        return $this->where('user_id','=',(int)$uid)->where($where)->count();

    }

    public function getModelInfoByModelID($model_id)
    {
        return $this->find($model_id);
    }

    public function updateModelInfoByModelID($model_id,$update_data)
    {
        return $this->find($model_id)->save($update_data);
    }

    public function createModelInfo($model_name,$type_id,$user_id)
    {
        return $this->insertGetId([
            'name'=>$model_name,
            'type_id'=>$type_id,
            'user_id'=>$user_id
        ]);
    }



    public function getUserIdByModelID($model_id)
    {
        if (empty($model_id)) {
            return false;
        }
        return $this->where('id', '=', $model_id)->field('user_id')->find();
    }

    public function deleteModelInfoByID($model_id)
    {
        if (empty($model_id)) {
            return false;
        }
        return $this->where('id', '=', $model_id)->delete();
    }
}