/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的Dao或者多个Dao来实现数据库操作
 * 实际的数据库操作在对应的Dao或其他Dao中实现
 */
 
package com.blueland.bpm.engine.config.service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.common.base.BaseManager;
import com.blueland.bpm.common.base.EntityDao;
import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.engine.config.dao.ProcessInfoConfigDao;
import com.blueland.bpm.engine.config.model.ProcessInfoConfig;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.engine.util.WorkFlowException;

/**
 * @author lw
 * @version 1.0
 * @since  
 */

@Service
public class ProcessInfoConfigManager extends BaseManager<ProcessInfoConfig,String>{
	
	@Autowired
	//ProcessInfoConfig对应的DAO类,主要用于数据库的增删改查等操作
	private ProcessInfoConfigDao processInfoConfigDao;
	@Autowired
	ProcessDefinitionService processDefinitionService;
	public EntityDao getEntityDao() {
		return this.processInfoConfigDao;
	}
	//分页查询
	public Page findPage(ProcessInfoConfig query) {
		return processInfoConfigDao.pageQuery(query);
	}
	
	public Page<List<ProcessInfoConfig>> findConfigPage(ProcessInfoConfig query){
		return processInfoConfigDao.findConfigPage(query);
	}
	/**
	 * 获取所有满足要求的procDefId以供导出zip
	 * @param params
	 * @return
	 */
	public List<String> getAllProcessIdForExport(ProcessInfoConfig params){
		return processInfoConfigDao.getAllProcessIdForExport(params);
	}
	
	public ProcessInfoConfig getById(String processDefId){
		if(StringUtils.isEmpty(processDefId)){
			throw new WorkFlowException(ExceptionErrorCode.B2001,"processDefId is nulll");
		}
		ProcessInfoConfig processInfoConfig=processInfoConfigDao.getById(processDefId);
		if(processInfoConfig!=null){
			ProcessDefinitionModel processDefinition=processDefinitionService.getProcessDef(processDefId);
			processInfoConfig.setProcessDefName(processDefinition.getProcDefName());
			processInfoConfig.setProcessVersion(processDefinition.getVersion());
			processInfoConfig.setProcessDefKey(processDefinition.getProcDefKey());
		}
		return processInfoConfig;
	}
	
	
	public ProcessInfoConfig getLinkId(String processDefId){
		if(StringUtils.isEmpty(processDefId)){
			throw new WorkFlowException(ExceptionErrorCode.B2001,"processDefId is nulll");
		}
		ProcessInfoConfig processInfoConfig=processInfoConfigDao.getLinkId(processDefId);
		if(processInfoConfig!=null){
			ProcessDefinitionModel processDefinition=processDefinitionService.getProcessDef(processInfoConfig.getProcessDefId());
			processInfoConfig.setProcessDefName(processDefinition.getProcDefName());
			processInfoConfig.setProcessVersion(processDefinition.getVersion());
			processInfoConfig.setProcessDefKey(processDefinition.getProcDefKey());
		}
		return processInfoConfig;
	}
	
	public boolean saveOrUpdate(ProcessInfoConfig paramsConfig){
		boolean resultFlag=false;
		ProcessInfoConfig processInfoConfig=processInfoConfigDao.getById(paramsConfig.getProcessDefId());
		if(processInfoConfig==null){
			paramsConfig.setProcessModelSyncState(ProcessInfoConfig.PROCESS_MODEL_SYNC_STATE_Y);
			resultFlag=processInfoConfigDao.save(paramsConfig);
		}else{
			paramsConfig.setLastUpdateTime(new Date());
			if(StringUtils.isEmpty(paramsConfig.getProcessModelSyncState())){
				paramsConfig.setProcessModelSyncState(ProcessInfoConfig.PROCESS_MODEL_SYNC_STATE_Y);
			}if(StringUtils.isEmpty(paramsConfig.getProcessMgrIsshow())){
				paramsConfig.setProcessMgrIsshow(ProcessInfoConfig.PROCESS_MGR_ISSHOW_Y);
			}
			resultFlag=processInfoConfigDao.update(paramsConfig);
		}
		return resultFlag;
	}
	
	
	public void save(ProcessInfoConfig paramsConfig){
		if(paramsConfig==null)
			return;
		if(paramsConfig.getOtherSysDealStatus()==null){
			paramsConfig.setOtherSysDealStatus("yes");
		}
		processInfoConfigDao.save(paramsConfig);
	}
	
	
	public void recoverProcessInfoConfig(String procDefId){
		ProcessInfoConfig prevProcessInfoConfig=getPrevVersionProcessConfig(procDefId);
		if(prevProcessInfoConfig!=null){
			prevProcessInfoConfig.setProcessMgrIsshow(ProcessInfoConfig.PROCESS_MODEL_SYNC_STATE_Y);
			update(prevProcessInfoConfig);
		}
	}
	
	public ProcessInfoConfig getPrevVersionProcessConfig(String procDefId){
		ProcessInfoConfig processInfoConfig=null;
		String[] processDefinitionIdArray=procDefId.split(":");
		if(Integer.parseInt(processDefinitionIdArray[1])>1){
			processInfoConfig = getLinkId(processDefinitionIdArray[0]+":"+(Integer.parseInt(processDefinitionIdArray[1])-1));
		}
		return processInfoConfig;
	}
	
	public void update(ProcessInfoConfig paramsConfig){
		paramsConfig.setLastUpdateTime(new Date());
		processInfoConfigDao.update(paramsConfig);
	}
	
	public void updateProcessMgrIsshowN(String processDefKey){
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("processDefKey", processDefKey);
		params.put("processMgrIsshow", "N");
		processInfoConfigDao.getSqlSessionTemplate().update(processInfoConfigDao.getIbatisMapperNamesapce()+".updateProcessMgrIsshow", params);
	}
	
	
	
	public void setProcessInfoUpdateState(String processDefId){
		ProcessInfoConfig paramsConfig=this.getById(processDefId);
		paramsConfig.setLastUpdateTime(new Date());
		paramsConfig.setProcessMgrState("UPDATE");
		paramsConfig.setProcessModelSyncState(ProcessInfoConfig.PROCESS_MODEL_SYNC_STATE_N);
		processInfoConfigDao.update(paramsConfig);
	}
	
	public void releaseProcessInfo(ProcessInfoConfig paramsConfig){
		paramsConfig.setLastUpdateTime(new Date());
		paramsConfig.setProcessMgrState("RELEASE");
		paramsConfig.setProcessModelSyncState(ProcessInfoConfig.PROCESS_MODEL_SYNC_STATE_Y);
		processInfoConfigDao.update(paramsConfig);
	}
	
	public void unReleaseProcessInfo(ProcessInfoConfig paramsConfig){
		paramsConfig.setLastUpdateTime(new Date());
		paramsConfig.setProcessMgrState("UNRELEASE");
		processInfoConfigDao.update(paramsConfig);
	}
	
	/**
	 * @description 获取经办任务接收人
	 * @author hanj
	 * @version 1.0
	 */
	public List<String> getPreTaskInstance(String processInstanceId,String taskId) {
		Map params=new HashMap();
		params.put("processInstanceId", processInstanceId);
		params.put("taskId", taskId);
		return processInfoConfigDao.getSqlSessionTemplate().selectList(processInfoConfigDao.getIbatisMapperNamesapce()+".getPreTaskInstance", params);
	}
	
	public Page<List<ProcessInfoConfig>> findConfigAndModelPage(ProcessInfoConfig query){
		return processInfoConfigDao.findConfigAndModelPage(query);
	}
	
}
