ProcessSpecInfoManagerImpl.java

package com.mycim.server.spec.manager.impl;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.mycim.framework.utils.lang.BooleanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.server.prp.manager.ProcessManager;
import com.mycim.server.spec.dao.ProcessSpecInfoDao;
import com.mycim.server.spec.manager.ProcessSpecInfoManager;
import com.mycim.valueobject.bas.TransactionLog;
import com.mycim.valueobject.consts.VersionStatus;
import com.mycim.valueobject.prp.ProcessSpecInfo;
import com.mycim.valueobject.prp.ProcessVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author Qiansheng.Wang
 * @version 1.0
 * @since 2020-09-03
 */
@Service
@Transactional
public class ProcessSpecInfoManagerImpl implements ProcessSpecInfoManager {

    @Autowired
    ProcessSpecInfoDao processSpecInfoDao;

    @Autowired
    ProcessManager processManager;

    @Override
    public void saveProcessSpecItemInfo(TransactionLog transactionLog, ProcessSpecInfo processSpecInfo) {
        processSpecInfo.setLastUpdatedUser(transactionLog.getTransPerformedBy());
        processSpecInfo.setLastUpdatedTime(transactionLog.getTransStartTimestamp());
        // 每次save 更新 versionLastUpdateTime
        ProcessVersion processVersionInfo = processManager
                .getProcessVersionInfo(processSpecInfo.getProcessRrn(), processSpecInfo.getProcessVersion());

        processSpecInfo.setVersionLastUpdateTime(processVersionInfo.getLastUpdateTimestamp());

        if (StringUtils.isBlank(processSpecInfo.getCreateUserId()) || processSpecInfo.getCreateTime() == null) {
            processSpecInfo.setCreateUserId(transactionLog.getTransPerformedBy());
            processSpecInfo.setCreateTime(transactionLog.getTransStartTimestamp());

            processSpecInfoDao.insertProcessSpecInfo(processSpecInfo);
        } else {
            processSpecInfoDao.updateProcessSpecInfo(processSpecInfo);
        }
    }

    @Override
    public void updateStatusOfProcessSpecInfo(TransactionLog transactionLog, ProcessSpecInfo processSpecInfo,
                                              String targetStatus) {
        Assert.isFalse(targetStatus == null, Errors.create().content("Target Status cannot be empty.").build());

        if (!BooleanUtils.toBoolean(processSpecInfo.getActiveFlag()) &&
                StringUtils.equalsIgnoreCase(targetStatus, VersionStatus.ACTIVE_KEY)) {//在原本的判定方式中,冻结时就会进入开始激活
            processSpecInfo.setActiveFlag(true);
            processSpecInfo.setActiveTime(transactionLog.getTransStartTimestamp());
            processSpecInfo.setActiveUser(transactionLog.getTransPerformedBy());
        }

        processSpecInfo.setCurrentStatus(targetStatus);
        processSpecInfo.setLastUpdatedTime(transactionLog.getTransStartTimestamp());
        processSpecInfo.setLastUpdatedUser(transactionLog.getTransPerformedBy());

        processSpecInfoDao.updateProcessSpecInfo(processSpecInfo);
    }

    @Override
    public ProcessSpecInfo getProcessSpecInfoByPrimaryKey(Long processRrn, Integer processVersion) {
        return processSpecInfoDao.getProcessSpecInfoByPrimaryKey(processRrn, processVersion);
    }

    @Override
    public List<ProcessSpecInfo> getProcessSpecItemInfos(Long processRrn) {
        List<ProcessSpecInfo> result = processSpecInfoDao.getProcessSpecInfos(processRrn);
        if (CollectionUtils.isEmpty(result)) {
            result = Collections.emptyList();
        }
        return result;
    }

    @Override
    public List<Integer> getAllProcessVersions(Long processRrn) {
        List<Integer> result = processSpecInfoDao.getAllProcessVersions(processRrn);
        if (CollectionUtils.isEmpty(result)) {
            result = Collections.emptyList();
        }
        return result;
    }

    @Override
    public List<Integer> getAllActivatedProcessVersions(Long processRrn) {
        List<Integer> result = processSpecInfoDao.getAllActivatedProcessVersions(processRrn);
        if (CollectionUtils.isEmpty(result)) {
            result = Collections.emptyList();
        }
        return result;
    }

    @Override
    public Integer getHighestActivatedProcessVersion(Long processRrn) {
        return processSpecInfoDao.getHighestActivatedProcessVersion(processRrn);
    }

    @Override
    public boolean hasActiveProcessSpec(long processRrn, Integer processVersionId) {
        return processSpecInfoDao.hasActiveProcessSpec(processRrn, processVersionId);
    }

}