package weaver.mfbdt.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @ClassName: WorkflowCreateHandler 
 * @Description: TODO
 * {异构系统创建ecology工作流的辅助类,
 * 支持ecology7.x-ecology8.1所有版本的ecology的创建工作流
 * }
 * @author shil
 * @date 2020-03-25 下午5:03:03
 *
 */
public class WorkflowCreateHandler extends BaseBean {
	private static final int MINUSONE = -1;
	private static final int MINUSTWO = -2;
	private static final int MINUSTHREE = -3;
	private static final int MINUSFOUR = -4;
	private static final int MINUSFIVE = -5;
	private static final int MINUSSIX = -6;
	private static final int MINUSSEVEN = -7;
	private static final int MINUSEIGHT = -8;
	private static final int ZERO = 0;
	private static final String SUCCESS = "0";

	public WorkflowCreateHandler() {
		super();
	}

	/***
	 *
	 * @param workflowid
	 * @return
	 */
	public String findWorkflowForm(String workflowid)
	{
		String tablename = "";
		String cDandT = TimeUtil.getCurrentDateString()+" "+TimeUtil.getCurrentTimeString();
		RecordSet ws = new RecordSet();
		ws.execute("select t2.tablename from workflow_base t1,workflow_bill t2 where t2.id=t1.formid and t1.id ='"+workflowid+"'");
		if(ws.next()){
			tablename = Util.null2String(ws.getString("tablename"));
		}else{
			tablename = "-1";
			writeLog("shauter 当前时间:"+cDandT+"-workflowid="+workflowid+"-没有设置表单");
		}
		return tablename;

	}
	
	/**
	 * 
	 * @Title: bulidWorkflowTitle 
	 * @Description: TODO(构建流程标题) 
	 * @param @param workflowid
	 * @param @param creatorid
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public String bulidWorkflowTitle(String workflowid,String creatorid)
	{
		StringBuffer workflowtitle=new StringBuffer("");
		ResourceComInfo resourceComInfo = null;
		try {
			resourceComInfo = new ResourceComInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String latsname = resourceComInfo.getLastname(creatorid);
		workflowtitle.append(findWorkflowName(""+workflowid)+"--"+latsname);
		return workflowtitle.toString();
	}
	
	/**
	 * 
	 * @Title: findWorkflowName 
	 * @Description: TODO(获取流程名称) 
	 * @param @param workflowid
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public String findWorkflowName(String workflowid){
		String workflowname="";
		WorkflowComInfo workflowComInfo = null;
		try {
			workflowComInfo = new WorkflowComInfo();
			workflowname = workflowComInfo.getWorkflowname(""+workflowid);
		} catch (Exception e) {
			e.printStackTrace();
			workflowname ="-1";
		}
		return workflowname;
	}
	
	/**
	 * 
	 * @Title: findDataIDByReqID 
	 * @Description: TODO(获取表单数据id) 
	 * @param @param workflowid
	 * @param @param requestid
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public String findDataIDByReqID(String workflowid,String requestid){
		String dataid="";
		RecordSet rs = new RecordSet();
	    rs.execute("select id from "+findWorkflowForm(workflowid)+" where requestid='"+requestid+"' ");
		rs.next();
		dataid = Util.null2String(rs.getString(1));
	    return dataid;
	}
	/**
	 * 
	 * @Title: expoundRequest 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param requestid
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	
	public String expoundRequest(String requestid)
	{
		writeLog("--------------requestid:"+requestid);
		String dec = "";
		int ireqid = Util.getIntValue(requestid, 0);
	    if(ireqid>ZERO){
	    }else if(ireqid==MINUSONE){
        	dec = "创建流程失败";
	    }else if(ireqid==MINUSTWO){
        	dec = "用户没有流程创建权限";
	    }else if(ireqid==MINUSTHREE){
        	dec = "创建流程基本信息失败";
	    }else if(ireqid==MINUSFOUR){
        	dec = "保存表单主表信息失败";
	    }else if(ireqid==MINUSFIVE){
        	dec = "更新紧急程度失败";
	    }else if(ireqid==MINUSSIX){
        	dec = "流程操作者失败";
	    }else if(ireqid==MINUSSEVEN){
        	dec = "流转至下一节点失败";
	    }else if(ireqid==MINUSEIGHT){
        	dec = "节点附加操作失败";
	    }
	    return dec;
	}

	/**
	 * 
	 * @Title: WorkflowCreateByMainTableMap 
	 * @param @param creatorId    创建人Id
	 * @param @param workflowId   流程Id
	 * @param @param requestName  请求标题
	 * @param @param IsNextFlow   是否提交到下一节点
	 * @param @param requestMap   Map格式的主表数据<fieldname,fieldvalue>=<字段名称,字段值>
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public String workflowCreateByRequestMap(String creatorId ,String workflowId ,String requestName ,String isNextFlow,Map<String,String> requestMainMap) {
		writeLog("-----创建流程传递的参数个数--------");
		writeLog("creatorId:"+creatorId);
		writeLog("workflowId:"+workflowId);
		writeLog("requestName:"+requestName);
		writeLog("IsNextFlow:"+isNextFlow);
		
		String requestid = "";
		RequestInfo requestInfo = new RequestInfo();
		//创建人Id
		requestInfo.setCreatorid(creatorId);
		//工作流Id
		requestInfo.setWorkflowid(workflowId);
		//请求标题
		requestInfo.setDescription(requestName);
		if(SUCCESS.equals(isNextFlow))
		{
			requestInfo.setIsNextFlow(isNextFlow);
		}
		
		//主表字段
		MainTableInfo mainTableInfo = new MainTableInfo();
		Property[] propertyArray = new Property[requestMainMap.size()];
		int p = 0;
		for (Entry<String, String> entry : requestMainMap.entrySet()) 
		{
			 propertyArray[p] = new Property();
			 propertyArray[p].setName(Util.null2String(entry.getKey()));
			 propertyArray[p].setValue(Util.null2String(entry.getValue()));
			 writeLog("\r\n---p="+p+"---Key="+entry.getKey()+"---Value="+entry.getValue());
			 p++;
		}
		writeLog("-----创建流程传递的参数个数p="+p);
		mainTableInfo.setProperty(propertyArray);
		requestInfo.setMainTableInfo(mainTableInfo);

		RequestService service = new RequestService();
		try {
			//流程
			try {
				//创建请求id
				requestid = service.createRequest(requestInfo);
			} catch (Exception e) {
				writeLog("======="+e.getMessage());
			}
		    //String userId = requestInfo.getLastoperator();//请求最后的操作者
		    //记录日志
		    StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
		    WorkflowComInfo wfcif =new WorkflowComInfo();
		    RequestComInfo rcif = new RequestComInfo();
		    ResourceComInfo rscif = new ResourceComInfo();
		    sbf.append("\r\n-----姓名:"+rscif.getLastname(rcif.getRequestCreater(requestid)));
		    sbf.append("\r\n-----时间:"+rcif.getRequestCreateTime(requestid));
		    sbf.append("\r\n-----创建流程:"+wfcif.getWorkflowname(workflowId));
		    sbf.append("\r\n-----请求:"+rcif.getRequestname(requestid));
		    sbf.append("\r\n-----请求:"+requestid);
		    sbf.append("\r\n-----创建工作流记录日志结束");
		    writeLog(sbf.toString());
		    System.out.println(""+sbf.toString());
		} catch (Exception e) {
			writeLog("错误：" + e);
			System.out.println("错误：" + e);
		}
		return requestid;
	}

	/**
	* @Description:提交流程至下一节点
	 * @param workflowid 流程id
	 * @param requestid 流程请求ID
	 * @param userid 当前用户
	 * @param type 提交类型：submit 提交 subnoback提交不需回复  subback提交需要回复 reject退回
	 * @param remark 签字意见
	 * @return String 提交结果：error 请求出错 ，failed 失败，success 成功
	* @Author: wangj
	*/
	public String submitWorkflowRequestByRequestMap(String workflowid,String requestName,int requestid ,int userid ,String type,String remark ,Map<String,String> requestMainMap) {
		writeLog("-----创建流程传递的参数个数--------");
		writeLog("requestid:"+requestid);
		writeLog("userid:"+userid);
		writeLog("type:"+type);
		writeLog("remark:"+remark);

		RequestInfo requestInfo = new RequestInfo();
		//创建人Id
		requestInfo.setCreatorid(Util.null2String(userid));
		//工作流Id
		requestInfo.setWorkflowid(workflowid);
		//请求id
		requestInfo.setRequestid(Util.null2String(requestid));
		//请求标题
		requestInfo.setDescription(requestName);
		//主表字段
		MainTableInfo mainTableInfo = new MainTableInfo();
		Property[] propertyArray = new Property[requestMainMap.size()];
		int p = 0;
		for (Entry<String, String> entry : requestMainMap.entrySet())
		{
			propertyArray[p] = new Property();
			propertyArray[p].setName(Util.null2String(entry.getKey()));
			propertyArray[p].setValue(Util.null2String(entry.getValue()));
			writeLog("\r\n---p="+p+"---Key="+entry.getKey()+"---Value="+entry.getValue());
			p++;
		}
		mainTableInfo.setProperty(propertyArray);
		requestInfo.setMainTableInfo(mainTableInfo);



		RequestService service = new RequestService();
		String result = "";
		try {
			//流程
		try {
			boolean flag = service.nextNodeBySubmit(requestInfo,requestid,userid,remark);
			if(flag){
				result = "success";
			}else{
				result = "failed";
			}
		}catch (Exception e){
			writeLog("流程提交至下一节点错误：" + e);
			result = "error";
		}
			//记录日志
			StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
			WorkflowComInfo wfcif = new WorkflowComInfo();
			RequestComInfo rcif = new RequestComInfo();
			ResourceComInfo rscif = new ResourceComInfo();
			sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(Util.null2String(userid))));
			sbf.append("\r\n-----requestid:" + rscif.getLastname(rcif.getRequestname(Util.null2String(requestid))));
			sbf.append("\r\n-----提交流程下一节点流记录日志结束");
			writeLog(sbf.toString());
			System.out.println("" + sbf.toString());
		} catch (Exception e) {
			writeLog("错误：" + e);
			System.out.println("错误：" + e);
		}
		return result;
	}


	/**
	* @Description: 删除流程方法
	* @Param: requestid 流程请求id
	* @param: userid 用户id
	* @return: boolean true 成功 false 失败
	* @Author: wangj
	*/
	public boolean deleteRequest(String requestid, String userid) {
		boolean flag = false;
		RequestService service = new RequestService();
		try {
			flag = service.deleteRequest(Util.getIntValue(requestid),Util.getIntValue(userid));
		}catch (Exception e){
			writeLog("删除流程错误：" + e);
			System.out.println("删除流程错误：" + e);
		}
		return flag;
	}

}
