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

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

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.oa.entity.Leave;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.workapprovalcopy.service.ApprovalCopyService;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
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.project.entity.ProjectDepositManagement;
import com.jeeplus.modules.project.dao.ProjectDepositManagementDao;

/**
 * 保证金管理Service
 * @author yangfan
 * @version 2017-10-11
 */
@Service
@Transactional(readOnly = true)
public class ProjectDepositManagementService extends CrudService<ProjectDepositManagementDao, ProjectDepositManagement> {
	@Autowired
	private ProjectDepositManagementDao projectDepositManagementDao;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	protected TaskService taskService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ApprovalCopyService approvalCopyService;

	@Autowired
	private ActTaskService actTaskService;

	public ProjectDepositManagement get(String id) {
		return super.get(id);
	}
	
	public List<ProjectDepositManagement> findList(ProjectDepositManagement projectDepositManagement) {
		return super.findList(projectDepositManagement);
	}
	
	public Page<ProjectDepositManagement> findPage(Page<ProjectDepositManagement> page, ProjectDepositManagement projectDepositManagement) {
		projectDepositManagement.setPage(page);
		List<ProjectDepositManagement> list = dao.findList(projectDepositManagement);
		page.setList(list);
		return page;
	}

	/**
	 * 启动流程
	 * @param
	 */
	@Transactional(readOnly = false)
	public void startProcessInstanceByKey(ProjectDepositManagement projectDepositManagement, Map<String, Object> variables) {
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(projectDepositManagement.getCurrentUser().getId());
		// 启动流程
		String businessKey = projectDepositManagement.getId().toString();
		variables.put("type", "deposit");
		variables.put("busId", businessKey);
		variables.put("title", projectDepositManagement.getRemarks());//设置标题；
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ActUtils.PD_DEPOSIT[0], businessKey, variables);
		projectDepositManagement.setProcessInstance(processInstance);
		// 更新流程实例ID
		projectDepositManagement.setProcessInstanceId(processInstance.getId());
		projectDepositManagementDao.updateProcessInstanceId(projectDepositManagement);
		projectDepositManagement.setStatus("2");
		projectDepositManagement.preUpdate();
		projectDepositManagementDao.update(projectDepositManagement);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectDepositManagement projectDepositManagement) {
		super.save(projectDepositManagement);
	}
	
	@Transactional(readOnly = false)
	public void delete(ProjectDepositManagement projectDepositManagement) {
		super.delete(projectDepositManagement);
	}

	/**
	 * 获取流程详细Leave
	 * @param processInstanceId
	 */
	@SuppressWarnings("unchecked")
	public ProjectDepositManagement getByProcessInstanceId(String processInstanceId) {
		ProjectDepositManagement projectDepositManagement = projectDepositManagementDao.getByProcessInstanceId(processInstanceId);
		return projectDepositManagement;
	}
	
	
	
}