package cc.smtweb.system.bpm.web.engine.dynPage;

import cc.smtweb.framework.core.common.SwMap;
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.cache.ModelTableCache;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.mvc.service.AbstractCompProvider;
import cc.smtweb.framework.core.mvc.service.SqlNamedPara;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasets;

/**
 * Created by Akmm at 2022/5/26 18:40
 */
public class DynPageProvider extends AbstractCompProvider {
    protected long pageId;
    protected PageDatasets datasets;
    protected UserSession us;

    //加载表单类数据集（单条）
    public SwMap loadData(SwMap filter, PageDataset pageDataSet) {
        return doGetData(pageDataSet.id, () -> {
            SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter,us);
            SwMap map = DbEngine.getInstance().queryEntityN(sqlPara.sql, sqlPara.mapParas, SwMap.class);
            if (map == null) {
                return new SwMap();
                //throw new BizException("没有找到指定数据（ds=" + pageDataSet.name + "）");
            }
            ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable);
            EntityHelper.loadBeanText(masterTable.getName(), map, sqlPara.mapFieldAlias);
            DynPageHelper.setCalcFields(map, pageDataSet);
            return map;
        });
    }

    public <T extends DefaultEntity> T loadBean(PageDataset pageDataSet, SwMap filter) {
        return doGetData("b_" + pageDataSet.id, () -> {
            ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable);
            EntityDao<T> dao = DbEngine.getInstance().findDao(masterTable.getName());
            T bean = dao.createBean();

            SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter,us);
            SwMap map = DbEngine.getInstance().queryEntityN(sqlPara.sql, sqlPara.mapParas, SwMap.class);
            if (map == null) {
                return bean;
                //throw new BizException("没有找到指定数据（ds=" + pageDataSet.name + "）");
            }

            EntityHelper.loadBeanText(masterTable.getName(), map, sqlPara.mapFieldAlias);
            DynPageHelper.setCalcFields(map, pageDataSet);

            bean.getData().putAll(map);
            return bean;
        });
    }

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

    public <T extends DefaultEntity> T loadBean(PageDataset dataset, long id) {
        ModelTable table = dataset._getModelTable();
        if (table == null) throw new SwException("数据集主表未找到（" + dataset.name + "）！");
        return loadBean(table.getName(), id);
    }

}
