package weaver.interfaces.htsc.duban;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.setup.CodeBuild;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;

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


/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class DbrwSaveModeExpand 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 sql = " select rwbh,jzrq,xzsy,jbr,tzydry from uf_dbrw where id="+billid ;
					rs.execute(sql);
					if(rs.next()){
						rwbh = Util.null2String(rs.getString("xzbh"));
						jzrq = Util.null2String(rs.getString("jzrq"));
						xzsy = Util.null2String(rs.getString("xzsy"));
					}
					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);
						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_dbrw 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_dbrw 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)){
						String rq = jzrq.substring(0,10);
						String sj = jzrq.substring(11);
						sql = " update uf_dbrw 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");
						}
					}
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务保存异常!");
			result.put("flag", "false");
		}
		return result;
	}
	//
	public void ShareLoad(int billid){
		PropBean propBean=new PropBean();
		ModeRightInfo modeRightInfo = new ModeRightInfo();
		JSONObject jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_RW_CUBEUUID"));
		int nodeid=  Integer.parseInt(jsonObject.get("modeid").toString());
		modeRightInfo.editModeDataShare(1,nodeid,billid);
	}
}