package com.engine.kq.wfset.attendance.manager;

import com.engine.kq.wfset.attendance.dao.HrmAttProcActionDao;
import com.engine.kq.wfset.attendance.domain.HrmAttProcAction;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.framework.BaseManager;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weaver.common.DateUtil;
import weaver.common.StringUtil;
import weaver.workflow.action.WorkflowActionManager;

/**
 * HrmAttProcActionManager
 * @author wcd
 * @version 1.0 2015-04-27
 */
public class HrmAttProcActionManager extends BaseManager<HrmAttProcAction> {

	private HrmAttProcActionDao dao = null;

	public HrmAttProcActionManager() {
		dao = new HrmAttProcActionDao();
		this.setDao(dao);
	}

	public Long save(HrmAttProcAction bean) {
		String id = String.valueOf(bean.getId());
		if(id.equals("0") || id.equals("-1")) id = String.valueOf(this.insert(bean));
		else this.update(bean);
		return Long.valueOf(id);
	}
	
	public void delete(Map<String, Comparable> map){
		dao.delete(map);
	}
	
	public void save(HttpServletRequest request, HttpServletResponse response) {
		set(request, response);
		String field001 = getDecodeParam("field001");
		com.engine.kq.wfset.attendance.manager.HrmAttProcSetManager setManager = new HrmAttProcSetManager();
		int workflowid = 0;
		HrmAttProcSet setBean = setManager.get(StringUtil.parseToInt(field001, 0));
		if(setBean != null) {
			workflowid = setBean.getField001();
			setBean.setField009(DateUtil.getCurrentDate());
			setManager.update(setBean);
		}
		delete(getMapParam("field001:"+field001));
		
		WorkflowActionManager workflowActionManager = new WorkflowActionManager();
		HrmAttProcAction bean = null;
		String[] allDatas = getDecodeParam("field002").split("___");
		String[] data = null;
		dao.deleteAction(workflowid);
		
		for(String _data : allDatas){
			data = _data.split(";");
			if(data == null || data.length == 0) continue;
			
			bean = new HrmAttProcAction();
			bean.setField001(StringUtil.parseToLong(field001));
			bean.setField002(data[0].split("\\:")[0].split("_")[0]);
			String[] _pAv = null;
			for(String _rData : data){
				_pAv = _rData.split("\\:");
				if(_pAv[0].endsWith("field003")) bean.setField003(StringUtil.vString(_pAv[1]));
				else if(_pAv[0].endsWith("field005")) bean.setField005(StringUtil.parseToInt(_pAv[1]));
				else if(_pAv[0].endsWith("field006")) bean.setField006(_pAv.length>=2?StringUtil.vString(_pAv[1], "-1") : "-1");
				else if(_pAv[0].endsWith("field007")) bean.setField007(Boolean.valueOf(_pAv[1]) ? 1 : 0);
				else if(_pAv[0].endsWith("field008")) bean.setField008(StringUtil.parseToInt(_pAv[1]));
			}
			insert(bean);
			
			if(bean.getField006().equals("-1")) continue;
			int size = bean.getField006().split(",").length;
			String defaultValue = "";
			for(int i=0; i<size; i++) defaultValue += "0"+(i!=size-1?",":"");
			
			int preoperator = 0;
			String nodeId = defaultValue, nodeLinkId = defaultValue;
			switch (bean.getField005()) {
			case 2:
				preoperator = 1;
				nodeId = bean.getField006();
				nodeLinkId = defaultValue;
				break;
			case 1:
				nodeId = bean.getField006();
				nodeLinkId = defaultValue;
				break;
			case 0:
				nodeId = defaultValue;
				nodeLinkId = bean.getField006();
				break;
			}
			
			String[] nodeIds = nodeId.split(",");
			String[] nodeLinkIds = nodeLinkId.split(",");
			if(nodeIds.length != nodeLinkIds.length) continue;
			
			String field003Name = getLabelName(bean.getField003());
			for(int index=0; index<nodeIds.length; index++) {
				workflowActionManager.setActionid(0);
				workflowActionManager.setActionname(field003Name);
				workflowActionManager.setWorkflowid(workflowid);
				workflowActionManager.setNodeid(StringUtil.parseToInt(nodeIds[index]));
				workflowActionManager.setNodelinkid(StringUtil.parseToInt(nodeLinkIds[index]));
				workflowActionManager.setIspreoperator(preoperator);
				workflowActionManager.setActionorder(1);
				workflowActionManager.setInterfaceid(bean.getField002());
				workflowActionManager.setInterfacetype(3);
				workflowActionManager.setIsused(1);
				workflowActionManager.doSaveWsAction();
			}
			dao.updateBackFlag(bean.getField005(), bean.getField007(), workflowid, nodeId);
		}
	}

	public void save(List<HrmAttProcAction> list) {
		dao.insert(list);
	}

}
