package cc.smtweb.framework.core.mvc.service;

import cc.smtweb.framework.core.annotation.SwBody;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.session.UserSession;

/**
 * Created by Akmm at 2022/3/2 10:39
 * 通用业务mvc总调度
 */
public abstract class AbstractCompService {
    public final static String TYPE_LIST = "list";
    public final static String TYPE_COMBO = "combo";
    public final static String TYPE_TREE = "tree";
    public final static String TYPE_LOAD = "load";
    public final static String TYPE_SAVE = "save";
    public final static String TYPE_DEL = "del";

    protected abstract AbstractHandler createHandler(String type);

    protected AbstractHandler getHandler(SwMap params, UserSession us, String type) throws Exception {
        AbstractHandler handler = createHandler(type);
        if (handler == null) throw new BizException("暂不支持此类服务：" + type);
        if (params == null) params = new SwMap();
        if (us == null) us = UserSession.createSys();
        handler.init(params, us);
        return handler;
    }

    protected R pageHandler(SwMap params, UserSession us, String type, IWorker worker) {
        try {
            AbstractHandler handler = getHandler(params, us, type);
            return worker.doWork(handler);
        } catch (Exception e) {
            return R.error("操作失败！", e);
        }
    }

    //保存
    public R save(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_SAVE, handler -> ((DefaultSaveHandler) handler).save());
    }

    //树，换爹
    public R trcp(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_SAVE, handler -> ((DefaultSaveHandler) handler).changeParent());
    }

    //读取
    public R load(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((DefaultLoadHandler) handler).load());
    }

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

    //列表数据
    public R list(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LIST, handler -> ((AbstractListHandler) handler).data());
    }

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

    //combo数据
    public R combo(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_COMBO, handler -> ((DefaultComboHandler) handler).data());
    }

    //combo总记录数及合计栏
    public R comboTotal(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_COMBO, handler -> ((DefaultComboHandler) handler).getTotal());
    }

    //combo数据过滤
    public R comboFilter(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_COMBO, handler -> ((DefaultComboHandler) handler).filter());
    }

    //树数据
    public R tree(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_TREE, handler -> ((AbstractTreeHandler<Object>) handler).data());
    }

    //树过滤
    public R treeFilter(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_TREE, handler -> ((AbstractTreeHandler<Object>) handler).filter());
    }
}
