package com.leanwo.activiti.delegate.base;


import java.util.List;

import javax.inject.Named;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.leanwo.activiti.dto.base.UserReadDocumentSimpleDto;
import com.leanwo.activiti.service.base.impl.ProdogServer;
import com.leanwo.activiti.service.base.impl.UserPreference;
import com.leanwo.activiti.util.FormatUtil;
import com.leanwo.activiti.util.SpringUtil;



/**
 * 添加抄送人(ServiceTask)
 * @author GuoZhiBo 20201009
 * @author YangZhiJie 20200126
 *
 */
@Named
public class AddCopyUserDelegate implements JavaDelegate {
	
	private static Logger logger = LoggerFactory.getLogger(AddCopyUserDelegate.class);
	
	private Expression userIds;
	
	@Override
	public void execute(DelegateExecution execution)  {
		String userIdsString = userIds.getExpressionText();
		
		if(userIdsString != null && userIdsString.length() > 0) {
			List<Long> userIdsAdded = FormatUtil.getGson().fromJson(userIdsString, new TypeToken<List<Long>>() {}.getType());
			
			if(userIdsAdded != null && userIdsAdded.size() > 0) {
				ProdogServer prodogServer = SpringUtil.getBean(ProdogServer.class.getName());
				UserReadDocumentSimpleDto userReadDocumentSimpleDto = new UserReadDocumentSimpleDto();
				userReadDocumentSimpleDto.setUserIds(userIdsAdded);
				userReadDocumentSimpleDto.setProcessInstanceId(execution.getProcessDefinitionId());
				
				String windowNo = (String) execution.getVariable(UserPreference.WINDOW_NO);	
				userReadDocumentSimpleDto.setWindowNo(windowNo);
				
				Long recordId = (Long) execution.getVariable(UserPreference.RECORD_ID);	
				userReadDocumentSimpleDto.setRecordId(recordId);
				
				Integer tabIndex = (Integer) execution.getVariable(UserPreference.TAB_INDEX);	
				userReadDocumentSimpleDto.setTabIndex(tabIndex);
				
				prodogServer.saveByUserReadDocumentSimpleDto(userReadDocumentSimpleDto);
			}
		}
	}

	public Expression getUserIds() {
		return userIds;
	}

	public void setUserIds(Expression userIds) {
		this.userIds = userIds;
	}
}
