package cc.smtweb.system.bpm.web.design.form.model;

import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author:lip
 * @Date : 2022/8/28 12:11
 */
public class LcMsWorker extends LcNormalWorker {
    private final static String templateList = "model_list_normal";
    private final static String templateCard = "model_card_ms";

    @Override
    protected String getTmplId(ModelForm bean) {
        if (bean.getName().endsWith(StringUtil.upFirst(SwEnum.PageType.LIST.value))) {
            return templateList;
        }
        if (bean.getName().endsWith(StringUtil.upFirst(SwEnum.PageType.CARD.value))) {
            return templateCard;
        }
        return super.getTmplId(bean);
    }

    /**
     * 根据模型生成页面
     *
     * @param bean
     */
    public void buildSaveModel(ModelForm bean) {
        String tmplId = getTmplId(bean);
        if (StringUtils.isEmpty(tmplId)) throw new SwException("此类型页面没有找到对应的模板文件！");
        SwMap model = new SwMap();
        SwMap opts = bean.getOpts();
        SwMap cfg = opts.readMap(ModelFormHelper.OPT_CONFIG);
        //
        model.put("subIndSave",cfg.readMap("props").readBool("subIndSave"));

        ModelFormHelper.buildSaveModelByTmplExt(bean, model, tmplId);
    }
}
