Files
2026-08-08 18:28:49 +08:00

44 lines
1.0 KiB
PHP

<?php
namespace app\models;
use core\base\Model;
/**
* 库存模型
*/
class Inventory extends Model
{
protected $table = 'inventory';
public function getAll()
{
return $this->order(['id DESC'])->fetchAll();
}
public function getById($id)
{
return $this->where(['id = :id'], [':id' => $id])->fetch();
}
public function getLowStock()
{
return $this->where(['quantity <= min_stock AND min_stock > 0'])->order(['quantity ASC'])->fetchAll();
}
public function getCount()
{
$sql = 'SELECT COUNT(*) AS cnt, SUM(quantity) AS total_qty FROM ' . $this->getTable();
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetch();
}
public function search($keyword)
{
return $this->where(
['product_name LIKE :kw OR product_model LIKE :kw2 OR category LIKE :kw3'],
[':kw' => "%{$keyword}%", ':kw2' => "%{$keyword}%", ':kw3' => "%{$keyword}%"]
)->order(['id DESC'])->fetchAll();
}
}