package weaver.interfaces.htsc.cowork;

import java.util.HashMap;
import java.util.Map;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
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;


/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class taskFeedbackModeExpand extends AbstractModeExpandJavaCodeNew {
	/**
	 * 执行模块扩展动作
	 * @param param
	 *  param包含(但不限于)以下数据
	 *  user 当前用户
	 *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
	 *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
	 * @return 
	 */
	public Map<String, String> doModeExpand(Map<String, Object> param) {
		Map<String, String> result = new HashMap<String, String>();

		BaseBean bb = new BaseBean();
		ResourceComInfo resourcecominfo = null;
		DepartmentComInfo departmentcominfo = null;
		try {
			resourcecominfo = new ResourceComInfo();
			departmentcominfo = new DepartmentComInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			int billid = -1;//数据id
			int modeid = -1;//模块id
			RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
			if(requestInfo!=null){
				billid = Util.getIntValue(requestInfo.getRequestid());
				modeid = Util.getIntValue(requestInfo.getWorkflowid());
				if(billid>0&&modeid>0)
				{
					RecordSet rs = new RecordSet();
					Map<String,String> requestMainMap = new HashMap<String,String>();
					String sfsp = "";
					String jbr = "" ;
					String rwfklc ="" ;
					String sql = " select sfsp,fkr,fkxq,fj,spr,rw,rwfklc from uf_rwfk where id = "+billid ;
					rs.execute(sql);
					if(rs.next())
					{
						sfsp = Util.null2String(rs.getString("sfsp"));
						rwfklc = Util.null2String(rs.getString("rwfklc"));
						requestMainMap.put("rw",Util.null2String(rs.getString("rw")));

						jbr = Util.null2String(rs.getString("fkr"));
						String szbm = Util.null2String(resourcecominfo.getDepartmentID(jbr),"0");
						requestMainMap.put("ngr",jbr);
						requestMainMap.put("szbm",szbm);

						requestMainMap.put("fkxq",Util.null2String(rs.getString("fkxq")));
						requestMainMap.put("fj",Util.null2String(rs.getString("fj")));
						requestMainMap.put("spr",Util.null2String(rs.getString("spr")));
					}

					if("0".equals(sfsp))
					{
						if("".equals(rwfklc))
						{
							String szjg = "" ;
							String szbm = Util.null2String(requestMainMap.get("szbm"));
							sql = " select id from HrmDepartment where supdepid = 0\n" +
								  " connect by prior supdepid = id \n" +
								  " start with id = "+szbm ;
							rs.execute(sql);
							if(rs.next()){
								szjg = Util.null2String(rs.getString("id"),"0");
							}
							bb.writeLog("szjg:"+szjg);
							String szjgnew = (szjg==""? "0" : szjg);
							bb.writeLog("szjgnew:"+szjgnew);
							requestMainMap.put("szjg",szjgnew);

							//String workflowId = bb.getPropValue("HTTASK","FK_WORKFLOWID") ;
							PropBean pb = new PropBean();
							String workflowId = pb.getPropName("TASK_FK_WORKFLOWID");

							bb.writeLog("workflowId:"+workflowId);
							String creatorId = ""+jbr ;
							bb.writeLog("creatorId:"+creatorId);
							String requestName = bulidWorkflowTitle(workflowId,creatorId); // 流程标题
							bb.writeLog("requestName:"+requestName);
							String IsNextFlow = pb.getPropName("TASK_FK_ISNEXTFLOW") ;
							bb.writeLog("IsNextFlow:"+IsNextFlow);

							int requestid = Util.getIntValue(WorkflowCreateByRequestMap(creatorId , workflowId , requestName , IsNextFlow , requestMainMap));
							bb.writeLog("requestid:"+requestid);
							if(requestid > 0){
								sql = " update uf_rwfk set rwfklc="+requestid+",fkzt=1 where id="+billid ;
								bb.writeLog("sql:"+sql);
								boolean flag = rs.execute(sql);
								if(!flag){
									result.put("errmsg","更新反馈信息失败！");
									result.put("flag", "false");
								}
							}else{
								result.put("errmsg","生产反馈审批流程失败，"+expoundRequest(requestid+""));
								result.put("flag", "false");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务反馈保存异常!");
			result.put("flag", "false");
		}
		return result;
	}

	/**
	 *
	 * @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)
	{
		BaseBean bs = new BaseBean();
		bs.writeLog("-----创建流程传递的参数个数--------");
		bs.writeLog("creatorId:"+creatorId);
		bs.writeLog("workflowId:"+workflowId);
		bs.writeLog("requestName:"+requestName);
		bs.writeLog("IsNextFlow:"+IsNextFlow);

		String requestid = "";
		RequestInfo requestInfo = new RequestInfo();
		requestInfo.setCreatorid(creatorId);//创建人Id
		requestInfo.setWorkflowid(workflowId);//工作流Id
		requestInfo.setDescription(requestName);//请求标题
		if(!"".equals(IsNextFlow))
		{
			requestInfo.setIsNextFlow(IsNextFlow);
		}
		//主表字段
		MainTableInfo mainTableInfo = new MainTableInfo();
		Property[] propertyArray = new Property[requestMainMap.size()];
		int p = 0;
		for (Map.Entry<String, String> entry : requestMainMap.entrySet())
		{
			propertyArray[p] = new Property();
			propertyArray[p].setName(Util.null2String(entry.getKey()));
			propertyArray[p].setValue(Util.null2String(entry.getValue()));
			bs.writeLog("\r\n---p="+p+"---Key="+entry.getKey()+"---Value="+entry.getValue());
			p++;
		}
		bs.writeLog("-----创建流程传递的参数个数p="+p);
		mainTableInfo.setProperty(propertyArray);
		requestInfo.setMainTableInfo(mainTableInfo);

		RequestService service = new RequestService();
		try {
			//流程
			try {
				requestid = service.createRequest(requestInfo);//创建请求id
			} catch (Exception e) {
				bs.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-----创建工作流记录日志结束");
			bs.writeLog(sbf.toString());
			System.out.println(""+sbf.toString());
		} catch (Exception e) {
			bs.writeLog("错误：" + e);
			System.out.println("错误：" + e);
		}
		return requestid;
	}


	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);
		String workflowname="";
		WorkflowComInfo WorkflowComInfo = null;
		try {
			WorkflowComInfo = new WorkflowComInfo();
			workflowname = WorkflowComInfo.getWorkflowname(""+workflowid);
		} catch (Exception e) {
			e.printStackTrace();
			workflowname ="-1";
		}
		workflowtitle.append(workflowname+"--"+latsname);
		return workflowtitle.toString();
	}

	public String expoundRequest(String requestid)
	{
		String dec = "";
		int ireqid = Util.getIntValue(requestid, 0);
		if(ireqid>0){

		}else if(ireqid==-1){
			dec = "创建流程失败";
		}else if(ireqid==-2){
			dec = "用户没有流程创建权限";
		}else if(ireqid==-3){
			dec = "创建流程基本信息失败";
		}else if(ireqid==-4){
			dec = "保存表单主表信息失败";
		}else if(ireqid==-5){
			dec = "更新紧急程度失败";
		}else if(ireqid==-6){
			dec = "流程操作者失败";
		}else if(ireqid==-7){
			dec = "流转至下一节点失败";
		}else if(ireqid==-8){
			dec = "节点附加操作失败";
		}
		return dec;
	}

}