package cn.stylefeng.team.meta.modular.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.team.meta.api.entity.MetaDefinition;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.meta.modular.service.MetaDefinitionService;
import cn.stylefeng.team.project.api.util.Version;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

public class ModelDefinitionFactory {

    public static MetaDefinition initDefinition(MetaModel metaModel) {

        MetaDefinition definition = new MetaDefinition();

        definition.setMetaDefinitionId(IdWorker.getId());

        definition.setBusinessCategoryId(metaModel.getBusinessCategoryId());
        definition.setBusinessId(metaModel.getBusinessId());
        definition.setMetaModelId(metaModel.getMetaModelId());

        definition.setFldVersion(new Version().toString());

        MetaDefinitionService metaDefinitionService = SpringUtil.getBean(MetaDefinitionService.class);
        metaDefinitionService.save(definition);

        return definition;
    }

    public static MetaDefinition upgradeDefinition(MetaDefinition oldMetaDefinition) {

        MetaDefinition metaDefinition = new MetaDefinition();
        BeanUtil.copyProperties(oldMetaDefinition, metaDefinition);

        metaDefinition.setMetaDefinitionId(IdWorker.getId());

        Version version = new Version(oldMetaDefinition.getFldVersion());
        version.upgrade();
        metaDefinition.setFldVersion(version.toString());

        return metaDefinition;
    }

}
