136 lines
3.7 KiB
PHP
136 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace Leantime\Domain\Bom\Repositories;
|
||
|
||
use Illuminate\Database\ConnectionInterface;
|
||
use Leantime\Core\Db\Db as DbCore;
|
||
|
||
/**
|
||
* 项目 ↔ 全局主数据(BOM/工艺文件/工具清单)引用层。
|
||
*
|
||
* 单向引用:项目引用主数据后,可追加项目级列(日期/库存/采购数量等)与值,
|
||
* 全部写在本引用层,绝不回写全局主数据(zp_bom_item)。
|
||
*/
|
||
class MasterRef
|
||
{
|
||
private ConnectionInterface $db;
|
||
|
||
public function __construct(DbCore $db)
|
||
{
|
||
$this->db = $db->getConnection();
|
||
}
|
||
|
||
// ---- 引用 ----
|
||
|
||
public function getRefsByProject(int $projectId): array
|
||
{
|
||
$rows = $this->db->table('zp_master_ref')
|
||
->where('projectId', $projectId)
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
return array_map(fn ($r) => (array) $r, $rows->all());
|
||
}
|
||
|
||
public function getRef(int $id): array|false
|
||
{
|
||
$r = $this->db->table('zp_master_ref')->where('id', $id)->first();
|
||
|
||
return $r ? (array) $r : false;
|
||
}
|
||
|
||
public function findRef(int $projectId, int $masterId): array|false
|
||
{
|
||
$r = $this->db->table('zp_master_ref')
|
||
->where('projectId', $projectId)
|
||
->where('masterId', $masterId)
|
||
->first();
|
||
|
||
return $r ? (array) $r : false;
|
||
}
|
||
|
||
public function createRef(array $v): int
|
||
{
|
||
return (int) $this->db->table('zp_master_ref')->insertGetId($v);
|
||
}
|
||
|
||
public function deleteRef(int $id): bool
|
||
{
|
||
$this->db->table('zp_master_ref_value')->where('refId', $id)->delete();
|
||
$this->db->table('zp_master_ref_column')->where('refId', $id)->delete();
|
||
|
||
return (bool) $this->db->table('zp_master_ref')->where('id', $id)->delete();
|
||
}
|
||
|
||
// ---- 项目级追加列 ----
|
||
|
||
public function getColumns(int $refId): array
|
||
{
|
||
$rows = $this->db->table('zp_master_ref_column')
|
||
->where('refId', $refId)
|
||
->orderBy('sortOrder')
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
return array_map(fn ($r) => (array) $r, $rows->all());
|
||
}
|
||
|
||
/**
|
||
* 按追加列 id 反查引用(用于鉴权)。
|
||
*/
|
||
public function getRefByColumnId(int $columnId): array|false
|
||
{
|
||
$col = $this->db->table('zp_master_ref_column')->where('id', $columnId)->first();
|
||
if (! $col) {
|
||
return false;
|
||
}
|
||
|
||
return $this->getRef((int) $col->refId);
|
||
}
|
||
|
||
public function addColumn(array $v): int
|
||
{
|
||
return (int) $this->db->table('zp_master_ref_column')->insertGetId($v);
|
||
}
|
||
|
||
public function deleteColumn(int $id): bool
|
||
{
|
||
$this->db->table('zp_master_ref_value')->where('columnId', $id)->delete();
|
||
|
||
return (bool) $this->db->table('zp_master_ref_column')->where('id', $id)->delete();
|
||
}
|
||
|
||
// ---- 项目级追加值 ----
|
||
|
||
public function getValues(int $refId): array
|
||
{
|
||
$rows = $this->db->table('zp_master_ref_value')
|
||
->where('refId', $refId)
|
||
->get();
|
||
|
||
return array_map(fn ($r) => (array) $r, $rows->all());
|
||
}
|
||
|
||
public function setValue(int $refId, int $columnId, int $itemId, string $value): bool
|
||
{
|
||
$existing = $this->db->table('zp_master_ref_value')
|
||
->where('refId', $refId)
|
||
->where('columnId', $columnId)
|
||
->where('itemId', $itemId)
|
||
->first();
|
||
|
||
if ($existing) {
|
||
return (bool) $this->db->table('zp_master_ref_value')
|
||
->where('id', $existing->id)
|
||
->update(['value' => $value]);
|
||
}
|
||
|
||
return (bool) $this->db->table('zp_master_ref_value')->insert([
|
||
'refId' => $refId,
|
||
'columnId' => $columnId,
|
||
'itemId' => $itemId,
|
||
'value' => $value,
|
||
]);
|
||
}
|
||
}
|