package weaver.interfaces.htsc.cowork;

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.soa.workflow.request.RequestInfo;

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


/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class coworkSaveModeExpand 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();
		ModeRightInfo modeRightInfo = new ModeRightInfo();
		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 xzbh = "" ;
					String jzsj = "";
					String cjr="";
					String jbr = "";
					String tzydr = "" ;
					String cjrzczhyc_db = "" ;

					String sql = " select xzbh,jzsj,cjr,modedatacreater,tzydr,jbr,cjrzczhyc from uf_xzsy where id="+billid ;
					rs.execute(sql);
					if(rs.next()){
						xzbh = Util.null2String(rs.getString("xzbh"));
						jzsj = Util.null2String(rs.getString("jzsj"));
						cjr = Util.null2String(rs.getString("modedatacreater"));

						tzydr = Util.null2String(rs.getString("tzydr"));
						jbr = Util.null2String(rs.getString("jbr"));

						cjrzczhyc_db = Util.null2String(rs.getString("cjrzczhyc"));
					}
					bb.writeLog("xzbh:"+xzbh);
					if("".equals(xzbh))
					{
						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);
							xzbh = codeBuild.getModeCodeStr(formid,billid);
						}
						bb.writeLog("xzbh:"+xzbh);

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

						int count = 0 ;
						String cjrzczhyc= JAVATools.GetZctogid(cjr);
						String jbrzzhyc = "" ;
						if(!"".equals(jbr)){
							jbrzzhyc= JAVATools.GetZctogid(jbr);
						}
						String tzydzzhyc = "" ;
						if(!"".equals(tzydr)){
							tzydzzhyc= JAVATools.GetZctogid(tzydr);
						}

						sql = " update uf_xzsy set xzbh='"+xzbh+"',cjzt=1,cjrzczhyc='"+cjrzczhyc+"',jbrzzhyc='"+jbrzzhyc+"',tzydzzhyc='"+tzydzzhyc+"' where id="+billid;
						bb.writeLog("sql:"+sql);
						boolean bool = rs.executeUpdate(sql);
						if(bool){
							count++;
						}
						if(count > 0){
							bb.writeLog("editModeDataShare666:"+cjr);
							modeRightInfo.editModeDataShare(Util.getIntValue(cjr),modeid,billid);//编辑时重

							bb.writeLog("editModeDataShare777:"+System.currentTimeMillis());
							CoworkSaveModeThread coworkSaveModeThread = new CoworkSaveModeThread(Util.getIntValue(cjr),modeid,billid);
							new Thread(coworkSaveModeThread).start();
							bb.writeLog("editModeDataShare888："+System.currentTimeMillis());

						}else{
							result.put("errmsg","编辑保存异常!");
							result.put("flag", "false");
						}
					}else{
						int count = 0 ;
						String jbrzzhyc = "" ;
						if(!"".equals(jbr)){
							jbrzzhyc= JAVATools.GetZctogid(jbr);
						}
						String tzydzzhyc = "" ;
						if(!"".equals(tzydr)){
							tzydzzhyc= JAVATools.GetZctogid(tzydr);
						}

						String setvalue = "";
						if("".equals(cjrzczhyc_db))
						{
							String cjrzczhyc = JAVATools.GetZctogid(cjr);
							setvalue = ",cjrzczhyc='"+cjrzczhyc+"'";
						}
						sql = " update uf_xzsy set jbrzzhyc='"+jbrzzhyc+"',tzydzzhyc='"+tzydzzhyc+"'"+setvalue+" where id="+billid;
						bb.writeLog("sql:"+sql);
						boolean bool = rs.executeUpdate(sql);
						if(bool){
							count++;
						}
						if(count > 0){
							bb.writeLog("editModeDataShare222:"+cjr);
							modeRightInfo.editModeDataShare(Util.getIntValue(cjr),modeid,billid);//编辑时重

							bb.writeLog("editModeDataShare333:"+System.currentTimeMillis());
							CoworkSaveModeThread coworkSaveModeThread = new CoworkSaveModeThread(Util.getIntValue(cjr),modeid,billid);
							new Thread(coworkSaveModeThread).start();
							bb.writeLog("editModeDataShare4444："+System.currentTimeMillis());
						}else{
							result.put("errmsg","编辑保存异常!");
							result.put("flag", "false");
						}
					}
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务反馈保存异常!");
			result.put("flag", "false");
		}
		return result;
	}
}