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

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.mvc.service.AbstractListHandler;
import cc.smtweb.framework.core.mvc.service.SqlNamedPara;
import cc.smtweb.framework.core.mvc.service.SqlPara;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;

import java.util.List;
import java.util.Map;

import static cc.smtweb.framework.core.common.SwConsts.TOTAL_KEY;

/**
 * Created by Akmm at 2022/5/26 15:58
 * 动态页面列表服务
 */
public class DynPageListHandler extends AbstractListHandler {
    protected long pageId;
    //过滤条件
    protected SwMap filter;

    //对应的数据集定义
    protected PageDataset pageDataSet;

    public DynPageListHandler() {
    }

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

    @Override
    protected String getCompId() {
        return "dynpage_" + pageId + "_" + pageDataSet.id;
    }

    @Override
    protected String getPkFieldName() {
        return pageDataSet.idField;
    }

    @Override
    protected SqlPara buildSqlPara() {
        return DynPageHelper.buildSelectSql(pageDataSet, filter,us);
    }

    @Override
    protected List<SwMap> queryData(String sql, SqlPara sqlPara) {
        List<SwMap> list;
        SqlNamedPara sp = (SqlNamedPara) sqlPara;
        if (sqlPara.page > 0 && sqlPara.rows > 0) {
            list = DbEngine.getInstance().pagedQueryN(sql, SwMap.class, (sqlPara.page - 1) * sqlPara.rows, sqlPara.rows, sp.mapParas);
        } else {
            list = DbEngine.getInstance().queryN(sql, sp.mapParas, SwMap.class);
        }
        if (pageDataSet.masterTable > 0L) {
            ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable);
            EntityHelper.loadBeanText(masterTable.getName(), list, sp.mapFieldAlias);
        }

        for (SwMap map : list) {
            DynPageHelper.setCalcFields(map, pageDataSet);
        }
        return list;
    }


    @Override
    protected SqlNamedPara buildSumSqlPara() {
        SqlNamedPara sqlPara = getCache(KEY_SQLPARA);
        if (sqlPara == null) sqlPara = buildDataSql();
        return DynPageHelper.buildSumSql(pageDataSet, sqlPara);
    }

    @Override
    public R getTotal() {
        SqlNamedPara sqlParaSum = getCache(KEY_SQLPARA_SUM);
        if (sqlParaSum == null) {
            Map<String, Object> map = getCache(KEY_PARAMS);
            if (map != null) params.putAll(map);
            sqlParaSum = buildSumSqlPara();
        }
        if (sqlParaSum == null) return R.success();
        setCache(KEY_SQLPARA_SUM, sqlParaSum);

        SwMap mapFooter = DbEngine.getInstance().queryEntityN(sqlParaSum.sql, sqlParaSum.mapParas, SwMap.class);

        SwMap r = new SwMap();
        r.put("total", mapFooter.get(TOTAL_KEY));
        r.put("footer", mapFooter);
        return R.success(r);
    }
}
