package cc.smtweb.system.bpm.web.design.form.define;

import cc.smtweb.framework.core.db.cache.ModelTableCache;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Akmm at 2022/5/25 15:53
 * 数据集集合
 */
public class PageDatasets {
    public List<PageDataset> list;

    @JsonIgnore
    private Map<String, PageDataset> mapId = null;
    @JsonIgnore
    private Map<String, PageDataset> mapName = null;

    public PageDatasets(PageDataset[] datasets) {
        this.list = new ArrayList<>(datasets.length);
        this.mapId = new HashMap<>(datasets.length);
        this.mapName = new HashMap<>(datasets.length);
        for (PageDataset ds : datasets) {
            this.list.add(ds);
            mapId.put(ds.id, ds);
            mapName.put(ds.name, ds);
            ds.masterTable_text = ModelTableCache.getInstance().getTableName(ds.masterTable);
            ds.resetFields();
        }
        //按依赖关系排序
        this.list.sort((o1, o2) -> {
            if (o1.hasDepends(o2.name)) return 1;
            if (o2.hasDepends(o1.name)) return -1;
            return 0;
        });
    }

    public PageDataset findMasterDataset() {
        if (list == null || list.isEmpty()) return null;
        return this.list.get(0);
    }

    public long findMasterTableId() {
        PageDataset ds = findMasterDataset();
        if (ds == null) return 0L;
        return ds.masterTable;
    }

    public PageDataset findById(String id) {
        return mapId.get(id);
    }

    public PageDataset findByName(String name) {
        return mapName.get(name);
    }
}
