package cc.smtweb.framework.core.mvc.service;

import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.exception.BizException;

/**
 * Created by Akmm at 2022/3/3 20:04
 */
public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvider {
    public String tableName;

    public DefaultProvider(String tableName) {
        this.tableName = tableName;
    }

    public T getBean(long id) {
        return doGetData(tableName + "." + id, () -> this.loadBean(id));
    }

    private T loadBean(long id) {
        EntityDao<T> bdao = (EntityDao<T>) DbEngine.getInstance().findDao(tableName);
        T bean = bdao.queryEntity(id);
        if (bean == null) throw new BizException("没有找到指定数据（id=" + id + "）");
        EntityHelper.loadBeanText(bean.getTableName(), bean.getData(), null);
        return bean;
    }
}
