package weaver.interfaces.htsc.task;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.collaboration.service.TaskLogService;
import org.apache.commons.lang.StringUtils;
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.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author shil   20220413 优化，讲三个节点保存整合成1个
 *
 */
public class TasksaveModeExpandNew 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>();

		try {
			int billid = -1;//数据id
			int modeid = -1;//模块id
			RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
			if(requestInfo!=null)
			{
				User user = (User)param.get("user");
				billid = Util.getIntValue(requestInfo.getRequestid());
				modeid = Util.getIntValue(requestInfo.getWorkflowid());
				if(billid>0&&modeid>0)
				{
					result = updateData(billid, modeid, user);
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务保存异常!");
			result.put("flag", "false");
		}
		return result;
	}

	/***
	 *
	 * @param billid
	 * @param modeid
	 * @param user
	 * @return
	 */
	public Map<String, String> updateData(int billid, int modeid, User user)
	{
		Map<String, String> result = new HashMap<String, String>();
		RecordSet rs = new RecordSet();
		BaseBean bb = new BaseBean();
		PropBean pb = new PropBean();
		ModeRightInfo modeRightInfo = new ModeRightInfo();

		String url = pb.getPropName("rw_mobile_url");
		ResourceComInfo resourceComInfo = null;
		String rwbh = "" ;
		String jzrq = "";
		String xzsy = "";
		String dbsy = "";
		String rwzt = "" ;
		String zxrbf = "" ;
		String rwbt = "";

		String cjr="";
		String jbr = "";
		String gzr = "";

		String rwlx = "" ;
		try{
			resourceComInfo = new ResourceComInfo();
		}catch (Exception e){

		}
		String sql = " select rwbt,rwbh,jzrq,xzsy,dbsy,rwlx,rwzt,cjr,jbr,zxrbf,gzr from uf_rw where id="+billid ;
		rs.execute(sql);
		if(rs.next()){
			rwbh = Util.null2String(rs.getString("rwbh"));
			rwbt = Util.null2String(rs.getString("rwbt"));
			rwzt = Util.null2String(rs.getString("rwzt"));

			jzrq = Util.null2String(rs.getString("jzrq"));

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

			cjr = Util.null2String(rs.getString("cjr"));
			jbr = Util.null2String(rs.getString("jbr"));
			zxrbf = Util.null2String(rs.getString("zxrbf"));
			gzr = Util.null2String(rs.getString("gzr"));
		}

		bb.writeLog("rwbh:"+rwbh);
		bb.writeLog("jzrq:"+jzrq);
		//更新任务的编号及创建状态=1
		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_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");
			}
		}

		String mainjbr="";
		String maingzr="";
		String maincjr="";
		if(!"".equals(jbr)){
			mainjbr= JAVATools.GetZctogid(jbr);
		}
		if(!"".equals(gzr)){
			maingzr= JAVATools.GetZctogid(gzr);
		}
		if(!"".equals(cjr)){
			maincjr= JAVATools.GetZctogid(cjr);
		}

		String sql_update=" update uf_rw set jbrzzhyc=?,tzydzzhyc=?,cjrzczhyc=? where id="+billid;
		bb.writeLog("sql_update:"+sql_update);
		rs.executeUpdate(sql_update,new Object[]{mainjbr,maingzr,maincjr});

		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);
			boolean bool = rs.executeUpdate(sql);
			if(!bool){
				result.put("errmsg","编辑保存异常!");
				result.put("flag", "false");
			}
		}

		if("2".equals(rwzt)){ //进行中
			//新增的执行人消息通知
			List<String> newZxrList = new ArrayList<>();
			List<String> newJbrList = Arrays.asList(jbr.split(","));
			List<String> oldJbrList = Arrays.asList(zxrbf.split(","));
			for(String jbrid:newJbrList)
			{
				if(StringUtils.isNotEmpty(jbrid) && !oldJbrList.contains(jbrid)){
					newZxrList.add(jbrid);
				}
			}
			if(newZxrList.size()>0)
			{
				Smstools smstools=new Smstools();
				int smstypeid = Integer.parseInt(Util.null2String(pb.getPropName("uf_ry_sms")));
				String title = "任务提醒";
				String message = resourceComInfo.getLastname(cjr)+"邀请您参与任务“"+rwbt+"”，请点击查看详情";
				String link = url+"&billid="+billid;
				Set<String> sendSet = new HashSet<>();
				sendSet.addAll(newZxrList);
				if(sendSet.size()>0){
					smstools.sendMessage(smstypeid,sendSet,title,message,link,link);
				}
			}

			//查询是否完成
			int uncount = 0;
			for(String jbrid:newJbrList)
			{
				String id = "";
				String jbrMainid = JAVATools.getmainhrmid(jbrid);
				sql = " select id from uf_rw where mainid = '"+billid+"' and ry = '"+jbrMainid+"'";
				bb.writeLog(sql);
				rs.executeQuery(sql);
				if(rs.next()){
					id = Util.null2String(rs.getString("id"));
				}
				if("".equals(id)){
					uncount++;
					break;
				}
			}
			if(uncount > 0)
			{
				String updateSql = "update uf_rw set rwzt = '3' where id = '"+billid+"'";
				bb.writeLog(updateSql);
				rs.executeUpdate(updateSql);
			}
		}

		//更新时间，更新上次的经办人
		String updateSql = " update uf_rw set zxrbf = '"+jbr+"',gxsj = to_char(sysdate,'yyyy-mm-dd') where id = '"+billid+"'";
		bb.writeLog(updateSql);
		boolean bool = rs.executeUpdate(updateSql);
		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");
				}
			}

			String rwids = "" ;
			sql = " select id from uf_rw where xzsy="+xzsy;
			rs.execute(sql);
			while (rs.next()){
				String rwid = Util.null2String(rs.getString("id"));
				rwids += rwids==""? rwid : ","+rwid;
			}
			sql = " update uf_xzsy set rwdx='"+rwids+"' where id="+xzsy;
			rs.execute(sql);
		}

		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");
				}
			}

			String rwids = "" ;
			sql = " select id from uf_rw where dbsy="+dbsy;
			rs.execute(sql);
			while (rs.next()){
				String rwid = Util.null2String(rs.getString("id"));
				rwids += rwids==""? rwid : ","+rwid;
			}
			sql = " update uf_xzsx set rwdx='"+rwids+"' where id="+dbsy;
			rs.execute(sql);
		}
		String mainid = "" ;
		if("0".equals(rwlx)){
			mainid = xzsy ;
		}else if("1".equals(rwlx)){
			mainid = dbsy ;
		}
		String sjlx="0";//1是评论，0是反馈
		String jllx="1";//1是任务，0是协作
		if(!"".equals(mainid))
		{
			String context="任务标题：\""+rwbt+"\"";
			TaskLogService taskLogService= new TaskLogService();
			String title = "新建了任务";
			taskLogService.createTaskLog(user,mainid,"",jllx,sjlx,context,title);
		}
		modeRightInfo.editModeDataShare(user.getUID(),modeid,billid);

		return result;
	}
}