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