package cc.smtweb.system.bpm.web.engine.model.common;

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.db.impl.DefaultEntity;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageService;

/**
 * Created by Akmm at 2022-08-12 15:37
 * 模型通用服务类
 */
public class ModelService extends DynPageService {
    public final static String TYPE_MODEL_LIST = "modelList";
    public final static String TYPE_MODEL_LOAD = "modelLoad";
    public final static String TYPE_MODEL_SAVE = "modelSave";
    public final static String TYPE_MODEL_DEL = "modelDel";

    @Override
    protected AbstractHandler createHandler(String type) {
        switch (type) {
            case TYPE_MODEL_LIST:
                return new ModelListHandler();
            case TYPE_MODEL_LOAD:
                return new ModelLoadHandler<DefaultEntity>();
            case TYPE_MODEL_SAVE:
                return new ModelSaveHandler<DefaultEntity>();
            case TYPE_MODEL_DEL:
                return new ModelDelHandler();
        }
        return super.createHandler(type);
    }
    //保存
    public R modelSave(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SAVE, handler -> ((ModelSaveHandler) handler).save());
    }

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

    //读取
    public R modelAdd(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_LOAD, handler -> ((ModelLoadHandler) handler).add());
    }

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

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

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