<?php

namespace app\admin\model;

use think\Model;

class Product extends Model
{
    protected $pk = 'product_id';

    public function porder()
    {
        return $this->hasMany('Order', 'product_id', 'product_id');
    }


    //模糊查询时使用,返回地商品的ID数组
    public function getIdsByNameLike($name)
    {
        $return = [];
        $ret = $this->field('product_id')->where(['product_name' => ['like', "%$name%"]])->select();
        if ($ret) {
            foreach ($ret as $val) {
                $return[] = $val->product_id;
            }
        }
        return $return;
    }


    public function getSonList($condition)
    {
        $result = $this
            ->where($condition)
            ->paginate(10);
        return $result;
    }

    public function getFartherList()
    {
        $result = $this
            ->where(['product_pid' => 0])
            ->select();
        return $result;
    }

    public function findProduct($condition)
    {
        $result = $this
            ->where($condition)
            ->find();
        return $result;
    }

    public function updateProduct($id, $data)
    {
        $result = $this
            ->where(['product_id' => $id])
            ->update($data);
        return $result;
    }

    public function addProduct($data)
    {
        $result = $this
            ->insert($data);
        return $result;
    }

    public function deleteProduct($condition)
    {
        $result = $this
            ->where($condition)
            ->delete();
        return $result;
    }


}
