package com.blueland.bpm.engine.core.executor;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
import com.blueland.bpm.identity.model.User;
/**
 * 驳回处理类
 * @version:  1.0
 * @author lw 
 */
@Service(WorkFlowContants.ACTION_TYPE_REJECT_ACTIVITY)
public class RejectActivityExecutor extends AbstractProcessExecutor implements ProcessExecutor{
	@Autowired
	ExecuteActivityExecutor executeActivityExecutor;
	@Override
	public ProcessInstanceModel execute(ProcessInputModel processInputModel)
			throws WorkFlowException {
		//additionalReceiver(processInputModel);
		// 设置返回参数
		return executeActivityExecutor.execute(processInputModel);
	}
	
	/**
	 * 驳回没有传递接受人时，自动找对应环节的人员
	 * @param processInputModel
	 */
	private void additionalReceiver(ProcessInputModel processInputModel){
		try{
		if(processInputModel.getWf_receiver()!=null){
			List<ActivityInstanceModel> result = processInstanceService.findBackAvtivity(processInputModel.getWf_curActInstId());
			if(!result.isEmpty()){
				for (ActivityInstanceModel actInst : result) {
					if(actInst.getActDefId().equals(processInputModel.getWf_nextActDefId())){
						//by lw 20220119  User user=this.userService.getUserByCode(actInst.getReceiverUserId(),actInst.getReceiverOrgId());
						User user=this.userService.getUserById(actInst.getReceiverUserId());
						processInputModel.setWf_receiver(user.getUserId());
					}
				}
			}
		}}catch(Exception e){
			e.printStackTrace();
		}
	}

}
