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

import cc.smtweb.framework.core.annotation.SwTable;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetField;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasets;

import java.util.Map;

/**
 * Created by Akmm at 2022/4/15 17:26
 */
@SwTable("ASP_MODEL_FORM")
public class ModelForm extends DefaultEntity {
    public static final String ENTITY_NAME = "ASP_MODEL_FORM";
    //将相关信息缓存起来
    private transient long masterTableId = -1;
    private transient PageDatasets datasets;
    private transient SwMap opts;

    public void clearEx() {
        masterTableId = -1L;
        datasets = null;
        opts = null;
    }

    public long getMasterTableId() {
        if (masterTableId < 0) {
            synchronized (ModelForm.class) {
                if (masterTableId < 0) {
                    datasets = ModelFormHelper.parsePageDataset(getDataset());
                    if (datasets == null) {
                        masterTableId = 0;
                    } else {
                        masterTableId = datasets.findMasterTableId();
                    }
                }
            }
        }
        return masterTableId;
    }

    public void setMasterTableId(long masterTableId) {
        this.masterTableId = masterTableId;
    }

    public PageDatasetField getField(String field) {
        for (PageDataset ds : getDatasets().list) {
            for (PageDatasetField df : ds.fields) {
                if (df.name.equals(field)) {
                    return df;
                }
            }
        }
        return null;
    }

    public PageDatasets getDatasets() {
        if (datasets == null) {
            synchronized (ModelForm.class) {
                if (datasets == null) {
                    datasets = ModelFormHelper.parsePageDataset(getDataset());
                }
            }
        }
        return datasets;
    }

    public void setDatasets(PageDatasets datasets) {
        this.datasets = datasets;
    }

    public SwMap getOpts() {
        if (opts == null) {
            synchronized (ModelForm.class) {
                if (opts == null) {
                    opts = ModelFormHelper.parseFormOption(getOption());
                }
            }
        }
        return opts;
    }

    public void setOpts(SwMap opts) {
        this.opts = opts;
    }

    public ModelForm() {
        super(ENTITY_NAME);
    }

    public long getId() {
        return getLong("mf_id");
    }

    public void setId(long mfId) {
        put("mf_id", mfId);
    }

    public long getPrjId() {
        return getLong("mf_prj_id");
    }

    public void setPrjId(long mfPrjId) {
        put("mf_prj_id", mfPrjId);
    }

    public long getMcId() {
        return getLong("mf_mc_id");
    }

    public void setMcId(long mfMcId) {
        put("mf_mc_id", mfMcId);
    }

    public String getName() {
        return getStr("mf_name");
    }

    public void setName(String mfName) {
        put("mf_name", mfName);
    }

    public String getTitle() {
        return getStr("mf_title");
    }

    public void setTitle(String mfTitle) {
        put("mf_title", mfTitle);
    }

    public int getType() {
        return getInt("mf_type");
    }

    public void setType(int mfType) {
        put("mf_type", mfType);
    }

    public String getService() {
        return getStr("mf_service");
    }

    public void setService(String mfService) {
        put("mf_service", mfService);
    }

    public String getContent() {
        return getStr("mf_content");
    }

    public void setContent(String mfContent) {
        String temStr = mfContent == null ? "{}" : mfContent;
        Map map = JsonUtil.parseMap(temStr);
        put("mf_content", JsonUtil.encodeString(map));
    }

    public String getOption() {
        return getStr("mf_option");
    }

    public void setOption(String mf_option) {
        put("mf_option", mf_option);
        opts = null;
    }

    public String getTmpl() {
        return getStr("mf_tmpl");
    }

    public void setTmpl(String mf_tmpl) {
        put("mf_tmpl", mf_tmpl);
    }

    public String getDataset() {
        return getStr("mf_dataset");
    }

    public void setDataset(String mf_dataset) {
        put("mf_dataset", mf_dataset);
        datasets = null;
        masterTableId = 0L;
    }

    public long getCreateUid() {
        return getLong("mf_create_uid");
    }

    public void setCreateUid(long mfCreateUid) {
        put("mf_create_uid", mfCreateUid);
    }

    public long getUpdateUid() {
        return getLong("mf_update_uid");
    }

    public void setUpdateUid(long mfUpdateUid) {
        put("mf_update_uid", mfUpdateUid);
    }

    public long getCreateAt() {
        return getLong("mf_create_at");
    }

    public void setCreateAt(long mfCreateAt) {
        put("mf_create_at", mfCreateAt);
    }

    public long getUpdateAt() {
        return getLong("mf_update_at");
    }

    public void setUpdateAt(long mfUpdateAt) {
        put("mf_update_at", mfUpdateAt);
    }

    public String getRemark() {
        return getStr("mf_remark");
    }

    public void setRemark(String mfRemark) {
        put("mf_remark", mfRemark);
    }

    public long getParent() {
        return getLong("mf_parent");
    }

    public void setParent(long mf_parent) {
        put("mf_parent", mf_parent);
    }

    //返回单据类型id
    public long getBillType() {
        SwMap opts = getOpts();
        return opts != null ? opts.readLong(ModelFormHelper.KEY_BILL_TYPE): 0L;
    }
}
