Files
Leantime/app/Domain/Bom/Repositories/MasterRef.php
wangruiguo d647428529
Some checks failed
Security Audit / Composer Security Audit (push) Has been cancelled
Security Audit / NPM Security Audit (push) Has been cancelled
Security Audit / CodeQL Analysis (push) Has been cancelled
OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
2026-09-03 18:49:20 +08:00

136 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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,
]);
}
}