Files
Leantime/app/Domain/Bom/Repositories/Bom.php

173 lines
4.5 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;
class Bom
{
private ConnectionInterface $db;
public function __construct(DbCore $db)
{
$this->db = $db->getConnection();
}
// ---- BOM 头 ----
public function getBoms(int $projectId): array
{
$rows = $this->db->table('zp_bom')
->where('projectId', $projectId)
->orderByDesc('id')
->get();
return array_map(fn ($r) => (array) $r, $rows->all());
}
/**
* 按类型列出全局主数据bom/process/tooling
* 主数据是全局的:始终只返回 projectId=0 的全局主数据,跨项目完全一致。
*/
public function getMasters(string $type, int $projectId = 0): array
{
$q = $this->db->table('zp_bom')
->where('projectId', 0);
if ($type !== '') {
$q->where('type', $type);
}
$rows = $q->orderByDesc('id')->get();
return array_map(fn ($r) => (array) $r, $rows->all());
}
public function getBom(int $id): array|false
{
$r = $this->db->table('zp_bom')->where('id', $id)->first();
return $r ? (array) $r : false;
}
public function createBom(array $v): int
{
return (int) $this->db->table('zp_bom')->insertGetId($v);
}
public function updateBom(int $id, array $v): bool
{
return (bool) $this->db->table('zp_bom')->where('id', $id)->update($v);
}
public function updateHiddenColumns(int $bomId, string $json): bool
{
return (bool) $this->db->table('zp_bom')->where('id', $bomId)->update(['hiddenColumns' => $json]);
}
public function deleteBom(int $id): bool
{
return (bool) $this->db->table('zp_bom')->where('id', $id)->delete();
}
// ---- Teable 源 ----
public function getSources(int $bomId): array
{
$rows = $this->db->table('zp_bom_source')->where('bomId', $bomId)->orderBy('id')->get();
return array_map(fn ($r) => (array) $r, $rows->all());
}
public function getSource(int $id): array|false
{
$r = $this->db->table('zp_bom_source')->where('id', $id)->first();
return $r ? (array) $r : false;
}
public function addSource(array $v): int
{
return (int) $this->db->table('zp_bom_source')->insertGetId($v);
}
public function deleteSource(int $id): bool
{
return (bool) $this->db->table('zp_bom_source')->where('id', $id)->delete();
}
// ---- 动态列 ----
public function getColumns(int $bomId): array
{
$rows = $this->db->table('zp_bom_column')
->where('bomId', $bomId)
->orderBy('sortOrder')
->orderBy('id')
->get();
return array_map(fn ($r) => (array) $r, $rows->all());
}
public function getColumn(int $id): array|false
{
$r = $this->db->table('zp_bom_column')->where('id', $id)->first();
return $r ? (array) $r : false;
}
public function addColumn(array $v): int
{
return (int) $this->db->table('zp_bom_column')->insertGetId($v);
}
public function deleteColumn(int $id): bool
{
return (bool) $this->db->table('zp_bom_column')->where('id', $id)->delete();
}
public function deleteColumnsByBom(int $bomId): bool
{
return (bool) $this->db->table('zp_bom_column')->where('bomId', $bomId)->delete();
}
// ---- 明细 ----
public function getItems(int $bomId): array
{
$rows = $this->db->table('zp_bom_item')
->where('bomId', $bomId)
->orderBy('seq')
->orderBy('id')
->get();
return array_map(fn ($r) => (array) $r, $rows->all());
}
public function getItem(int $id): array|false
{
$r = $this->db->table('zp_bom_item')->where('id', $id)->first();
return $r ? (array) $r : false;
}
public function createItem(array $v): int
{
return (int) $this->db->table('zp_bom_item')->insertGetId($v);
}
public function updateItem(int $id, array $v): bool
{
return (bool) $this->db->table('zp_bom_item')->where('id', $id)->update($v);
}
public function deleteItem(int $id): bool
{
return (bool) $this->db->table('zp_bom_item')->where('id', $id)->delete();
}
public function deleteItemsByBom(int $bomId): bool
{
return (bool) $this->db->table('zp_bom_item')->where('bomId', $bomId)->delete();
}
}