package com.mortals.flowable.listener.global;

import com.mortals.xhx.common.code.flowable.runtime.ProcessStatusEnum;
import com.mortals.xhx.module.extend.model.ExtendHisprocinstEntity;
import com.mortals.xhx.module.extend.model.ExtendProcinstEntity;
import com.mortals.xhx.module.extend.model.ExtendProcinstQuery;
import com.mortals.xhx.module.extend.service.ExtendHisprocinstService;
import com.mortals.xhx.module.extend.service.ExtendProcinstService;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 全局的流程启动的监听器
 *
 * @author: zxfei
 * @date: 2021/9/9 11:45
 */
@Component
@CommonsLog
public class GlobalProcistStartListener extends AbstractFlowableEngineEventListener {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private ExtendProcinstService extendProcinstService;
    @Autowired
    private ExtendHisprocinstService extendHisprocinstService;

    @Override
    protected void processStarted(FlowableProcessStartedEvent event) {
        log.info("全局的流程启动的监听器");
        FlowableEntityEventImpl flowableEntityEvent = (FlowableEntityEventImpl) event;
        ExecutionEntityImpl processInstance = (ExecutionEntityImpl) flowableEntityEvent.getEntity();
        //创建子流程的扩展信息
        ExecutionEntityImpl parent = processInstance.getParent();
        if (StringUtils.isNotBlank(parent.getProcessInstanceId()) && StringUtils.isBlank(parent.getBusinessKey())){
            ProcessInstance superProcessInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getRootProcessInstanceId()).singleResult();
            //1、创建扩展信息
            ExtendProcinstEntity extendProcinst = extendProcinstService.selectOne(new ExtendProcinstQuery().processInstanceId(processInstance.getProcessInstanceId()),null);
            if (extendProcinst == null){
                extendProcinst = new ExtendProcinstEntity();
                extendProcinst.setProcessInstanceId(processInstance.getProcessInstanceId());
                extendProcinst.setModelKey(processInstance.getProcessDefinitionKey());
                extendProcinst.setProcessDefinitionId(processInstance.getProcessDefinitionId());
                extendProcinst.setCurrentUserCode(parent.getStartUserId());
                extendProcinst.setProcessStatus(ProcessStatusEnum.SPZ.toString());
                extendProcinst.setCreateTime(new Date());
                extendProcinstService.save(extendProcinst);
                ExtendHisprocinstEntity extendHisprocinst = new ExtendHisprocinstEntity();
                BeanUtils.copyProperties(extendProcinst, extendHisprocinst);
                extendHisprocinstService.save(extendHisprocinst);
                //2、修改业务表单id到子流程中
                runtimeService.updateBusinessKey(processInstance.getProcessInstanceId(), superProcessInstance.getBusinessKey());
                runtimeService.setProcessInstanceName(processInstance.getProcessInstanceId(), superProcessInstance.getName());
            }
        }
    }
}
