package weaver.interfaces.htsc.cowork;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.setup.CodeBuild;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class TasksaveModeExpand 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();
		RecordSet rs = new RecordSet();
		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)
				{
					String rwbh = "" ;
					String jzrq = "";
					String xzsy = "";
					String rwlx = "";
					String dbsy = "";
					String sql = " select rwbh,jzrq,xzsy,dbsy,rwlx from uf_rw where id="+billid ;
					rs.execute(sql);
					if(rs.next()){
						rwbh = Util.null2String(rs.getString("rwbh"));
						jzrq = Util.null2String(rs.getString("jzrq"));

						xzsy = Util.null2String(rs.getString("xzsy"));
						rwlx = Util.null2String(rs.getString("rwlx"));
						dbsy = Util.null2String(rs.getString("dbsy"));
					}

					bb.writeLog("rwbh:"+rwbh);
					bb.writeLog("jzrq:"+jzrq);

					if("".equals(rwbh))
					{
						int formid = 0 ;
						sql = " select formid from modeinfo where id= "+ modeid;
						bb.writeLog("sql:"+sql);
						rs.execute(sql);
						if(rs.next()){
							formid = Util.getIntValue(Util.null2String(rs.getString("formid")));
						}
						bb.writeLog("formid:"+formid);

//						CodeBuilder codeBuild = new CodeBuilder(modeid);
//						codeBuild.setLanguage(7);
//						Map<String, Object> map = codeBuild.getModeCodeStr(formid,billid);

						rs.executeQuery("select id,codefieldid from ModeCode where modeid=? and isuse='1'", new Object[]{modeid});
						if(rs.next()) {
							CodeBuild codeBuild = new CodeBuild(rs.getInt("id"));
							codeBuild.setModeId(modeid);
							codeBuild.setLanguage(7);
							rwbh = codeBuild.getModeCodeStr(formid,billid);
						}
						bb.writeLog("rwbh:"+rwbh);

						sql = " update uf_rw set rwbh='"+rwbh+"',cjzt=1 where id="+billid;
						bb.writeLog("sql:"+sql);
						boolean bool = rs.executeUpdate(sql);
						if(!bool){
							result.put("errmsg","编辑保存异常!");
							result.put("flag", "false");
						}
					}

					sql = " update uf_rw set gxsj = to_char(sysdate,'yyyy-mm-dd') where id="+billid;
					bb.writeLog("sql:"+sql);
					boolean bool = rs.executeUpdate(sql);
					if(!bool){
						result.put("errmsg","编辑保存异常!");
						result.put("flag", "false");
					}


					if(!"".equals(jzrq) && jzrq.length() >=16){
						String rq = jzrq.substring(0,10);
						String sj = jzrq.substring(11,16);
						sql = " update uf_rw set rq='"+rq+"',sj='"+sj+"' where id="+billid;
						bb.writeLog("sql:"+sql);
						bool = rs.executeUpdate(sql);
						if(!bool){
							result.put("errmsg","编辑保存异常!");
							result.put("flag", "false");
						}
					}

					if(!"".equals(xzsy))
					{
						sql = " select cjzt from uf_xzsy where id= "+xzsy;
						bb.writeLog("sql:"+sql);
						rs.execute(sql);
						String xzcjzt = "";
						if(rs.next()){
							xzcjzt = Util.null2String(rs.getString("cjzt"));
						}

						if("".equals(xzcjzt) || "-1".equals(xzcjzt)){
							sql = " update uf_xzsy set cjzt=1 where id="+billid;
							bb.writeLog("sql:"+sql);
							boolean xzbool = rs.executeUpdate(sql);
							if(!xzbool){
								result.put("errmsg","编辑保存异常!");
								result.put("flag", "false");
							}
						}
					}

					if(!"".equals(dbsy))
					{
						sql = " select cjzt from uf_xzsx where id= "+xzsy;
						bb.writeLog("sql:"+sql);
						rs.execute(sql);
						String xzcjzt = "";
						if(rs.next()){
							xzcjzt = Util.null2String(rs.getString("cjzt"));
						}
						if("".equals(xzcjzt) || "-1".equals(xzcjzt))
						{
							sql = " update uf_xzsx set cjzt = 1 where id="+billid;
							bb.writeLog("sql:"+sql);
							boolean xzbool = rs.executeUpdate(sql);
							if(!xzbool){
								result.put("errmsg","编辑保存异常!");
								result.put("flag", "false");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务保存异常!");
			result.put("flag", "false");
		}
		return result;
	}
}