package cn.axzo.workflow.core.engine.cmd;

import cn.hutool.core.date.DateUtil;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.common.engine.impl.persistence.entity.PropertyEntity;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;

import java.util.Date;
import java.util.Objects;

/**
 * 获取 ACT_GE_PROPERTY 表的指定属性值的命令
 *
 * @author wangli
 * @since 2024-09-30 15:15
 */
public class CustomGetPropertyCmd implements Command<Date> {
    private final String propertyName;

    public CustomGetPropertyCmd(String propertyName) {
        this.propertyName = propertyName;
    }

    @Override
    public Date execute(CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);
        PropertyEntity entity = processEngineConfiguration.getPropertyEntityManager().findById(propertyName);
        if (Objects.nonNull(entity)) {
            return DateUtil.parseDate(entity.getValue());
        }
        return null;
    }
}
