/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sysuseroffice.service;

import java.util.List;
import java.util.Map;

import com.jeeplus.common.utils.Collections3;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.act.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.sysuseroffice.dao.UserofficeDao;

/**
 * 用户所在公司Service
 * @author yangfan
 * @version 2017-08-07
 */
@Service
@Transactional(readOnly = true)
public class UserofficeService extends CrudService<UserofficeDao, Useroffice> {

	@Autowired
	private HistoryService historyService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private ActTaskService actTaskService;
	public Useroffice get(String id) {
		Useroffice useroffice = super.get(id);
		// 获取流程实例对象
		if(useroffice!=null && "1".equals(useroffice.getStatus())){
			String processInstanceId = useroffice.getProcessInstanceId();
			HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
			if(historicProcessInstance!=null) {
				useroffice.setHistoricProcessInstance(historicProcessInstance);
				useroffice.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult());
			} else {
				ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
				if (processInstance != null){
					useroffice.setProcessInstance(processInstance);
					useroffice.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult());
				}
			}
			ProcessInstance processInstance = actTaskService.getProcIns(processInstanceId);
			if (processInstance!=null) {
				List<Task> taskList = actTaskService.getCurrentTaskList(processInstance);
				if(taskList!=null && taskList.size()>1){
					for (Task taskInfok:taskList) {
						if (taskInfok.getAssignee().equals(UserUtils.getUser().getId())) {
							Act act = new Act();
							act.setTaskId(taskInfok.getId());
							act.setTaskName(taskInfok.getName());
							act.setTaskDefKey(taskInfok.getTaskDefinitionKey());
							act.setProcDefId(taskInfok.getProcessDefinitionId());
							act.setProcInsId(taskInfok.getProcessInstanceId());
							act.setTask(taskInfok);
							useroffice.setAct(act);
						}
					}
				}else {
					Task task = actTaskService.getCurrentTaskInfo(processInstance);
					Act act = new Act();
					act.setTaskId(task.getId());
					act.setTaskName(task.getName());
					act.setTaskDefKey(task.getTaskDefinitionKey());
					act.setProcDefId(task.getProcessDefinitionId());
					act.setProcInsId(task.getProcessInstanceId());
					act.setTask(task);
					useroffice.setAct(act);
				}
			}
		}
		return useroffice;
	}

	public List<Useroffice> findList(Useroffice useroffice) {
		return super.findList(useroffice);
	}
	public List<Useroffice> findListByCompany(Useroffice useroffice) {
		return dao.findListByCompany(useroffice);
	}
	public List<Useroffice> searchUsers(Useroffice useroffice) {
		return dao.searchUsers(useroffice);
	}
	public List<Useroffice> searchUsersGroupByUserId(Useroffice useroffice) {
		return dao.searchUsersGroupByUserId(useroffice);
	}

	public Page<Useroffice> findPage(Page<Useroffice> page, Useroffice useroffice) {
		return super.findPage(page, useroffice);
	}
	
	@Transactional(readOnly = false)
	public void save(Useroffice useroffice) {
		super.save(useroffice);
	}
	
	@Transactional(readOnly = false)
	public void delete(Useroffice useroffice) {
		super.delete(useroffice);
	}
	
	
	
	
}