package cc.smtweb.system.bpm.web.engine.model.listcard.lcms;

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.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.model.common.*;

/**
 * @Author:lip
 * @Date : 2022/8/28 20:38
 * 模型LC_LT_MS（普通主子列表（含编辑卡片）） 服务类
 */
@SwService
public class LCMsService extends ModelService {
    public final static String TYPE_MODEL_SUB_LIST = "modelSubList";
    public final static String TYPE_MODEL_SUB_LOAD = "modelSubLoad";
    public final static String TYPE_MODEL_SUB_SAVE = "modelSubSave";
    public final static String TYPE_MODEL_SUB_DEL = "modelSubDel";

    public AbstractHandler createHandler(String type) {
        switch (type) {
            case TYPE_MODEL_LOAD:
                return new LCMsLoadHandler<>();
            case TYPE_MODEL_SAVE:
                return new LCMsSaveHandler<>();
            case TYPE_MODEL_DEL:
                return new LCMsDelHandler();
            case TYPE_MODEL_SUB_LIST:
                return new ModelListHandler(LCMsConfig.dsSList);
            case TYPE_MODEL_SUB_LOAD:
                return new ModelLoadHandler<>(LCMsConfig.dsSCard);
            case TYPE_MODEL_SUB_SAVE:
                return new ModelSaveHandler<>(LCMsConfig.dsSCard);
            case TYPE_MODEL_SUB_DEL:
                return new ModelDelHandler(LCMsConfig.dsSCard);
        }
        return super.createHandler(type);
    }

    /**
     * 子表单独保存
     * @param params
     * @param us
     * @return
     */
    public R modelSubSave(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SUB_SAVE, handler -> ((ModelSaveHandler) handler).save());
    }

    /**
     * 子表编辑
     * @param params
     * @param us
     * @return
     */
    public R modelSubLoad(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SUB_LOAD, handler -> ((ModelLoadHandler) handler).load());
    }

    /**
     * 子表新增初始化
     * @param params
     * @param us
     * @return
     */
    public R modelSubAdd(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SUB_LOAD, handler -> ((ModelLoadHandler) handler).add());
    }

    /**
     * 子表单独删除
     * @param params
     * @param us
     * @return
     */
    public R modelSubDel(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SUB_DEL, handler -> ((ModelDelHandler) handler).del());
    }

    /**
     * 子表列表数据
     * @param params
     * @param us
     * @return
     */
    public R modelSubList(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_MODEL_SUB_LIST, handler -> ((ModelListHandler) handler).data());
    }

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