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.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.util.JsonUtil;
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.springframework.util.Assert;

import java.util.List;

/**
 * @Author:lip
 * @Date : 2022/8/26 10:20
 * 普通列表（含编辑卡片）
 */
public class LcNormalWorker extends BaseModelWorker {
    private final static String templateList = "model_list_normal";
    private final static String templateCard = "model_card_normal";

    @Override
    protected void saveModule(ModelForm bean, List<ModelForm> listFormChild) {
        EntityDao<ModelForm> dao = DbEngine.getInstance().findDao(ModelForm.class);

        String baseName = ModelFormHelper.getPageName(bean);
        String listName = baseName + StringUtil.upFirst(SwEnum.PageType.LIST.value);
        String cardName = baseName + StringUtil.upFirst(SwEnum.PageType.CARD.value);
        //
        SwMap opts = bean.getOpts();
        SwMap cfg = opts.readMap(ModelFormHelper.OPT_CONFIG);
        //
        SwMap cfgPage = new SwMap();
        cfg.put(ModelFormHelper.OPT_PAGE, cfgPage);
        //
        cfgPage.put(SwEnum.PageType.LIST.value, listName);
        cfgPage.put(SwEnum.PageType.CARD.value, cardName);
        bean.setOption(JsonUtil.encodeString(opts));
        //
        ModelForm listPage;
        ModelForm cardPage;
        if (bean.isNew()) {
            //新增
            listPage = createPage(bean, SwEnum.PageType.LIST.value);
            cardPage = createPage(bean, SwEnum.PageType.CARD.value);
            listFormChild.add(listPage);
            listFormChild.add(cardPage);
            dao.batchInsertEntity(listFormChild);
        } else {
            //修改，先不考虑修改模型的情况 todo
            listPage = ModelFormHelper.getFromCache(listName);
            cardPage = ModelFormHelper.getFromCache(cardName);
            Assert.isTrue(listPage != null && cardPage != null, "获取页面失败");
            listFormChild.add(listPage);
            listFormChild.add(cardPage);
            boolean modified = false;
            //服务名发生变化
            if (!StringUtil.isStrEquals(listPage.getService(), bean.getService())) {
                listPage.setService(bean.getService());
                cardPage.setService(bean.getService());
                modified = true;
            }
            if (modified) {
                dao.batchUpdateEntity(listFormChild, "mf_content,mf_dataset,mf_tmpl");
            }
        }
    }

    @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);
    }
}
