初始化
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
namespace app\models;
|
||||
|
||||
use core\base\Model;
|
||||
|
||||
/**
|
||||
* 产品型号模型
|
||||
*/
|
||||
class ProductModel extends Model
|
||||
{
|
||||
protected $table = 'product_model';
|
||||
|
||||
/** @var bool 迁移是否已检查(进程级缓存,避免重复检查) */
|
||||
private static $migrated = false;
|
||||
|
||||
/** @var bool type 列是否存在 */
|
||||
private static $typeColumnExists = false;
|
||||
|
||||
/**
|
||||
* 确保 type 列存在(自动迁移)
|
||||
* @return bool type 列是否存在(true=可用,false=不存在且无法创建)
|
||||
*/
|
||||
private function ensureTypeColumn()
|
||||
{
|
||||
if (self::$migrated) {
|
||||
return self::$typeColumnExists;
|
||||
}
|
||||
$table = $this->getTable();
|
||||
try {
|
||||
$this->query("SELECT type FROM {$table} LIMIT 1");
|
||||
self::$typeColumnExists = true;
|
||||
self::$migrated = true;
|
||||
} catch (\Throwable $e) {
|
||||
// 列不存在 → 尝试自动迁移
|
||||
try {
|
||||
$this->execute("ALTER TABLE {$table}
|
||||
ADD COLUMN `type` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '类型(成品/半成品/配件)'
|
||||
AFTER `id`");
|
||||
$this->execute("UPDATE {$table} SET `type` = `product_type` WHERE `type` = ''");
|
||||
self::$typeColumnExists = true;
|
||||
} catch (\Throwable $migErr) {
|
||||
error_log('[ProductModel] Auto-migration failed: ' . $migErr->getMessage());
|
||||
self::$typeColumnExists = false;
|
||||
}
|
||||
self::$migrated = true;
|
||||
}
|
||||
return self::$typeColumnExists;
|
||||
}
|
||||
|
||||
// 获取所有产品型号
|
||||
public function getAll()
|
||||
{
|
||||
$this->ensureTypeColumn();
|
||||
return $this->order(['id ASC'])->fetchAll();
|
||||
}
|
||||
|
||||
// 添加产品型号
|
||||
public function addModel($data)
|
||||
{
|
||||
if (!$this->ensureTypeColumn()) {
|
||||
unset($data['type']); // type 列不存在,移除该字段
|
||||
}
|
||||
return $this->add($data);
|
||||
}
|
||||
|
||||
// 更新产品型号
|
||||
public function updateModel($id, $data)
|
||||
{
|
||||
if (!$this->ensureTypeColumn()) {
|
||||
unset($data['type']);
|
||||
}
|
||||
return $this->where(['id = :id'], [':id' => $id])->update($data);
|
||||
}
|
||||
|
||||
// 删除产品型号
|
||||
public function deleteModel($id)
|
||||
{
|
||||
return $this->delete($id);
|
||||
}
|
||||
|
||||
// 根据型号代码查找
|
||||
public function findByCode($model_code)
|
||||
{
|
||||
return $this->where(['model_code = :code'], [':code' => $model_code])->fetch();
|
||||
}
|
||||
|
||||
// 根据类型+产品类型和型号代码联合查找(用于检测同类型下是否重复)
|
||||
public function findByTypeAndCode($type, $product_type, $model_code)
|
||||
{
|
||||
if ($this->ensureTypeColumn()) {
|
||||
return $this->where(
|
||||
['type = :t', 'product_type = :pt', 'model_code = :mc'],
|
||||
[':t' => $type, ':pt' => $product_type, ':mc' => $model_code]
|
||||
)->fetch();
|
||||
}
|
||||
// Fallback: type 列不存在,仅在 product_type 和 model_code 上查重
|
||||
return $this->where(
|
||||
['product_type = :pt', 'model_code = :mc'],
|
||||
[':pt' => $product_type, ':mc' => $model_code]
|
||||
)->fetch();
|
||||
}
|
||||
|
||||
// 根据类型获取该类型下的所有型号(用于前端联动)
|
||||
public function getByType($type)
|
||||
{
|
||||
if (!$this->ensureTypeColumn()) {
|
||||
return [];
|
||||
}
|
||||
return $this->where(['type = :t'], [':t' => $type])
|
||||
->order(['model_code ASC'])
|
||||
->fetchAll();
|
||||
}
|
||||
|
||||
// 获取类型列表(去重)
|
||||
public function getTypes()
|
||||
{
|
||||
if (!$this->ensureTypeColumn()) {
|
||||
return [];
|
||||
}
|
||||
$sql = 'SELECT DISTINCT type FROM ' . $this->getTable() . ' WHERE type != \'\' ORDER BY type';
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有产品类型(product_type 去重列表,用于下拉筛选)
|
||||
* 注意:这是 product_type 字段(自由文本),不是 type(成品/半成品/配件)
|
||||
*/
|
||||
public function getProductTypes()
|
||||
{
|
||||
$table = $this->getTable();
|
||||
return $this->query(
|
||||
"SELECT DISTINCT product_type FROM {$table} WHERE product_type != '' ORDER BY product_type"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取产品型号
|
||||
* 优先通过 type 字段查询(大分类:成品/半成品/配件),
|
||||
* 如果 type 查不到则通过 product_type 字段查询(子分类:PCBA/电池/外壳等)
|
||||
* 若 type 列不可用,回退到 product_type 字段查询
|
||||
*/
|
||||
public function getByProductType($productType)
|
||||
{
|
||||
if ($this->ensureTypeColumn()) {
|
||||
// 先尝试 type 字段
|
||||
$result = $this->getByType($productType);
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
// type 查不到,回退到 product_type 字段
|
||||
return $this->where(['product_type = :pt'], [':pt' => $productType])
|
||||
->order(['model_code ASC'])
|
||||
->fetchAll();
|
||||
}
|
||||
// Fallback: type 列不存在,使用 product_type 字段
|
||||
return $this->where(['product_type = :pt'], [':pt' => $productType])
|
||||
->order(['model_code ASC'])
|
||||
->fetchAll();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user