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

import cc.smtweb.framework.core.annotation.SwBody;
import cc.smtweb.framework.core.annotation.SwService;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.mvc.service.AbstractCompService;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.session.UserSession;

/**
 * Created by Akmm at 2022/4/21 17:43
 * 动态页面引擎
 */
@SwService
public class DynPageService extends AbstractCompService {

    @Override
    protected AbstractHandler createHandler(String type) {
        switch (type) {
            case TYPE_LOAD:
                return new DynPageLoadHandler();
            case TYPE_SAVE:
                return new DynPageSaveHandler();
            case TYPE_DEL:
                return new DynPageDelHandler();
        }
        return null;
    }

    //新增
    public R add(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DynPageLoadHandler) handler).add());
    }

    //新增
    public R addOne(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DynPageLoadHandler) handler).addOne());
    }

    //加载
    public R loadOne(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DynPageLoadHandler) handler).loadOne());
    }

    //保存指定数据集
    public R saveOne(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_SAVE, handler -> ((DynPageSaveHandler) handler).saveOne());
    }

    //保存指定数据集
    public R save(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_SAVE, handler -> ((DynPageSaveHandler) handler).saveAll());
    }

    //删除指定数据集
    public R delOne(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_DEL, handler -> ((DynPageDelHandler) handler).delOne());
    }

    //删除数据
    public R del(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_DEL, handler -> ((DynPageDelHandler) handler).delAll());
    }

    //列表总记录数及合计栏
    public R total(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DynPageLoadHandler) handler).getTotal());
    }

    //树过滤
    public R treeFilter(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DynPageLoadHandler) handler).treeFilter());
    }
}
