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

import cc.smtweb.framework.core.common.R;
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.mvc.service.SwListData;
import cc.smtweb.system.bpm.web.common.BpmConst;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;

import static cc.smtweb.system.bpm.web.common.BpmConst.KEY_HEADER_MENU;

/**
 * Created by Akmm at 2022/4/21 17:53
 */
public class DynPageLoadHandler extends AbstractDynPageHandler {
    //数据集
    private String dbName;
    //过滤条件
    private SwMap filter = new SwMap();

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

    //列表工具类
    public DynPageListHandler getListWorker(SwMap filter, PageDataset pageDataSet) {
        DynPageListHandler listHandler = new DynPageListHandler(form.getId(), filter, pageDataSet);
        listHandler.init(params, us);
        return listHandler;
    }

    //树工具类
    public DynPageTreeHandler getTreeWorker(SwMap filter, PageDataset pageDataSet) {
        DynPageTreeHandler listHandler = new DynPageTreeHandler(form.getId(), filter, pageDataSet);
        listHandler.init(params, us);
        return listHandler;
    }

    //新增操作，初始化定义的数据集
    public R add() {
        //返回的数据，以dataset.name为key，查出的结果（bean或list）为value
        SwMap mapRet = new SwMap();
        for (PageDataset dataSet : datasets.list) {
            if (!dataSet.canEdit) {
                continue;
            }
            if (SwEnum.DatasetType.FORM.value.equals(dataSet.type)) {//单表
                SwMap data;
                //懒加载，给个空对象
                if (dataSet.lazy) {
                    data = new SwMap();
                } else {
                    data = DynPageHelper.createBean(dataSet);
                }
                afterAddBean(dataSet, data);
                mapRet.put(dataSet.name, DynRetBean.createBean(data));
            } else if (SwEnum.DatasetType.TREE.value.equals(dataSet.type)) {//树
                SwMap data = new SwMap();
                afterAddBean(dataSet, data);
                mapRet.put(dataSet.name, DynRetBean.createBean(data));
            } else {//非枚举 if (!SwEnum.DatasetType.ENUM.value.equals(dataSet.type))
                SwListData listData = SwListData.create(null, 0);
                afterAddList(dataSet, listData);
                mapRet.put(dataSet.name, DynRetBean.createList(listData));
            }
        }
        return R.success(mapRet);
    }

    //新增初始化 - bean
    protected void afterAddBean(PageDataset dataset, SwMap bean) {
    }

    //加载之后
    protected void afterLoadBean(PageDataset dataset, SwMap bean) {
    }

    //新增初始化 - list
    protected void afterAddList(PageDataset dataset, SwListData bean) {
    }

    //新增操作，初始化定义的数据集
    public R addOne() {
        //对应的数据集定义
        PageDataset dataset = readParamDs();

        if (!dataset.canEdit) {
            return R.error("指定数据集为只读数据集[" + dataset.label + "]！");
        }
        //懒加载，给个空对象
        SwMap data = DynPageHelper.createBean(dataset);
        afterAddBean(dataset, data);
        return R.success(DynRetBean.createBean(data));
    }

    public R treeFilter() {
        //过滤条件
        SwMap filter = params.readMap("filter");
        //对应的数据集定义
        PageDataset pageDataSet = readParamDs();
        DynRetBean bean = DynRetBean.createList(SwListData.create(getTreeWorker(filter, pageDataSet).buildFilter(), 0));

        return R.success(bean);
    }

    public R loadOne() {
        //过滤条件
        SwMap filter = params.readMap("filter");
        SwMap header = params.readMap(SwConsts.PARAMS_HEADER_KEY);
        if(header!=null){
            filter.put(BpmConst.MENU_KEY,header.get(KEY_HEADER_MENU));
        }
        //对应的数据集定义
        PageDataset pageDataSet = readParamDs();

        DynRetBean bean = null;
        if (SwEnum.DatasetType.LIST.value.equals(pageDataSet.type)) {//列表类
            bean = DynRetBean.createList(getListWorker(filter, pageDataSet).buildListData());
        } else if (SwEnum.DatasetType.TREE.value.equals(pageDataSet.type)) {//树类
            bean = DynRetBean.createList(SwListData.create(getTreeWorker(filter, pageDataSet).buildData(), 0));
        } else {//表单类
            SwMap data = provider.loadData(filter, pageDataSet);
            afterLoadBean(pageDataSet, data);
            bean = DynRetBean.createBean(data);
        }

        return R.success(bean);
    }

    /**
     * 计算分页数据
     *
     * @return
     */
    public R getTotal() {
        //数据集
        PageDataset pageDataSet = readParamDs();
        //过滤条件
        SwMap filter = params.readMap("filter");
        SwMap header = params.readMap(SwConsts.PARAMS_HEADER_KEY);
        if(header!=null){
            filter.put(BpmConst.MENU_KEY,header.get(KEY_HEADER_MENU));
        }
        return getListWorker(filter, pageDataSet).getTotal();
    }
}
