CaiWuXiTong/modules/EcommerceFinance/Services/FollowedUpstreamColumns.php
renshen 401dfb089a 店铺账号密码:上游新增字段自动跟随(镜像补列 + 页面自动多出这一列)
新增 business-mirror:follow-upstream-columns:先把数据字典刷新到最新,再把上游
视图里新增、当前数据集还没同步的字段追加进镜像定义并立刻建列,下一次常规同步
就会带回数据。只做新增,不删除/不改写已有字段;字段名带 password/secret/token/
cipher/credential 等敏感关键字的一律跳过。已登记到发布 cron(每天 06:40)。

页面侧:config/upstream_mirrors.php 的 auto_follow_columns 用「页面 => 数据集」
登记,FollowedUpstreamColumns 服务把 followed_columns 变成表格列;这类列只展示、
不生成查询条件(后端还没有对应筛选口径)。

数据集自带的数据变动仍然由每分钟的 business-mirror:sync-tables 增量同步。
2026-10-08 13:19:53 +08:00

50 lines
1.6 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 Modules\EcommerceFinance\Services;
/**
* 自动跟随上游新增字段:读取数据集定义里记录的 followed_columns。
*
* 字段由 `business-mirror:follow-upstream-columns` 写入(见 config/upstream_mirrors.php
* 的 auto_follow_columns);页面据此自动补列,镜像行里带上同名键即可直接展示。
*/
class FollowedUpstreamColumns
{
public function __construct(private readonly BusinessMirrorDatasetRegistry $datasets)
{
}
/** @return list<string> */
public function columns(string $datasetKey): array
{
$columns = [];
foreach ((array) $this->datasets->definitionValue($datasetKey, 'followed_columns', []) as $column) {
$column = trim((string) $column);
if ($column !== '' && ! str_starts_with($column, '_mirror')) {
$columns[] = $column;
}
}
return array_values(array_unique($columns));
}
/**
* 给页面字段列表补上自动跟随进来的列(字段名与标签都用上游列名)。
*
* @param list<array<string,mixed>> $fields
* @return list<array<string,mixed>>
*/
public function appendFields(string $datasetKey, array $fields): array
{
$existing = array_map(static fn (array $field): string => (string) ($field['name'] ?? ''), $fields);
foreach ($this->columns($datasetKey) as $column) {
if (in_array($column, $existing, true)) {
continue;
}
$fields[] = ['name' => $column, 'label' => $column, 'followed' => true];
}
return $fields;
}
}