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

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.impl.DefaultEntity;

/**
 * Created by Akmm at 2022/3/2 19:52
 * 默认实体实现
 */
public class DefaultLoadHandler<T extends DefaultEntity> extends AbstractHandler {
    protected String tableName;

    public R load() {
        long id = params.readLong("id", 0L);
        T bean;
        final EntityDao<T> dao = DbEngine.getInstance().findDao(tableName);
        if (id <= 0L) {
            bean = dao.createBean(us.getUserId());
            bean.setEntityId(DbEngine.getInstance().nextId());
        } else {
            bean = dao.queryEntity(id);
        }
        afterLoad(bean);
        return R.success(bean);
    }

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

    protected void afterLoad(T bean) {
    }
}
