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

import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.mvc.service.AbstractTreeHandler;
import cc.smtweb.framework.core.mvc.service.SqlNamedPara;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;

import java.util.List;

/**
 * Created by Akmm at 2022/7/5 11:51
 * 动态页面树型服务
 */
public class DynPageTreeHandler extends AbstractTreeHandler<SwMap> {
    protected long pageId;
    //过滤条件
    protected SwMap filter;
    //对应的数据集定义
    private PageDataset pageDataSet;

    public DynPageTreeHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
        this.pageId = pageId;
        this.filter = filter;
        this.pageDataSet = pageDataSet;
    }

    @Override
    protected List<SwMap> filterData() {
        ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable);
        if (table == null) throw new BizException("未找到数据集表定义：" + pageDataSet.masterTable);

        String text = "%" + filter.readString("text") + "%";
        SqlNamedPara sqlPara = buildFilterSqlPara(text);

        return DbEngine.getInstance().queryN(sqlPara.sql, sqlPara.mapParas, SwMap.class);
    }

    @Override
    public List<SwMap> buildData() {
        List<SwMap> rows = getChildren(filter.readLong("parent_id"));
        return buildNodes(rows, filter.readBool("lazy"));
    }

    /**
     * 构建sql
     *
     * @return
     */
    protected SqlNamedPara buildDataSqlPara(long id) {
        return DynPageHelper.buildTreeSelectSql(pageDataSet, filter,us, id);
    }

    /**
     * 构建sql
     *
     * @return
     */
    protected SqlNamedPara buildFilterSqlPara(String text) {
        //
        filter.remove("parent_id");
        return DynPageHelper.buildFilterSelectSql(pageDataSet, filter,us, text);
    }

    @Override
    protected List<SwMap> getChildren(long id) {
        if (id <= 0) id = SwConsts.DEF_ROOT_ID_LONG;
        SqlNamedPara sqlPara = buildDataSqlPara(id);
        return DbEngine.getInstance().queryN(sqlPara.sql, sqlPara.mapParas, SwMap.class);
    }

    @Override
    protected long getId(SwMap bean) {
        ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable);
        if (table == null) throw new BizException("未找到数据集表定义：" + pageDataSet.masterTable);

        return bean.readLong(table.getIdField());
    }

    @Override
    protected String getText(SwMap bean) {
        ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable);
        if (table == null) throw new BizException("未找到数据集表定义：" + pageDataSet.masterTable);
        ModelField field = table.findFieldByType(SwEnum.FieldType.NAME.value);
        return field != null ? bean.readString(field.getName()): bean.readString(table.getIdField());
    }
}
