package weaver.interfaces.htsc.cowork;

import com.alibaba.fastjson.JSONObject;
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.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cowork.CoworkSaveModeThread;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;


/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class SaveSendMessageModeExpand 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 bs = new BaseBean();
		PropBean pb = new PropBean();
		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());
				bs.writeLog("billid:"+billid);
				bs.writeLog("modeid:"+modeid);
				if(billid>0&&modeid>0)
				{
					String xzmodeid = pb.getPropName("UF_XZSY_CUBEUUIUD");
					JSONObject jsonObject_xz = pb.getModeInfoByUUID(xzmodeid);
					Integer xzformmodeid = Util.getIntValue(jsonObject_xz.getString("modeid"));
					String dbmodeid = pb.getPropName("DUBAN_XZ_UUID");
					JSONObject jsonObject_db = pb.getModeInfoByUUID(dbmodeid);
					Integer dbformmodeid = Util.getIntValue(jsonObject_db.getString("modeid"));
					String rwmodeid = pb.getPropName("UF_RW_CUBEUUIUD");
					JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
					Integer rwformmodeid = Util.getIntValue(jsonObject_rw.getString("modeid"));

					String tablename = "";
					String dtablename = "";
					String type = "";
					String url = "";
					String ztField = "xzzt";
					String btField = "bt";
					if(xzformmodeid.equals(modeid)){
						tablename = "uf_xzsy";
						dtablename = "uf_xzsy_dt2";
						type = "协作事项";
						url = pb.getPropName("xz_mobile_url");
					}else if(dbformmodeid.equals(modeid)){
						tablename = "uf_xzsx";
						dtablename = "uf_xzsx_dt2";
						type = "督办任务";
						url = pb.getPropName("dbrw_mobile_url");
					}else if(rwformmodeid.equals(modeid)){
						tablename = "uf_rw";
						dtablename = "uf_rw_dt3";
						type = "协作任务";
						url = pb.getPropName("rw_mobile_url");
						ztField = "rwzt";
						btField = "rwbt";
					}

					String cjr="";
					String jbr = "";
					String zxrbf = "" ;
					String status = "";
					String bt = "";
					String sql = " select cjr,jbr,zxrbf,"+ztField+" as zt,"+btField+" as bt from "+tablename+" where id="+billid ;
					rs.execute(sql);
					if(rs.next()){
						cjr = Util.null2String(rs.getString("cjr"));
						jbr = Util.null2String(rs.getString("jbr"));
						zxrbf = Util.null2String(rs.getString("zxrbf"));
						status = Util.null2String(rs.getString("zt"));
						bt = Util.null2String(rs.getString("bt"));
					}

					if("2".equals(status)){//进行中
						//新增的执行人消息通知
						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")));
							ResourceComInfo resourceComInfo = new ResourceComInfo();

							String title = type+"提醒";
							String message = resourceComInfo.getLastname(cjr)+"邀请您参与"+type+"“"+bt+"”，请点击查看详情";
							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);
							}
						}

						//查询是否完成
						boolean flag = true;
						for(String jbrid:newJbrList){
							String jbrMainid = JAVATools.getmainhrmid(jbrid);
							sql = "select id from "+dtablename+" where mainid = '"+billid+"' and ry = '"+jbrMainid+"'";
							bs.writeLog(sql);
							rs.executeQuery(sql);
							if(rs.next()){
							}else{
								flag = false;
								break;
							}
						}
						if(flag){
							String updateSql = "update "+tablename+" set "+ztField+" = '3' where id = '"+billid+"'";
							bs.writeLog(updateSql);
							rs.executeUpdate(updateSql);
						}
					}

					String updateSql = "update "+tablename+" set zxrbf = '"+jbr+"' where id = '"+billid+"'";
					bs.writeLog(updateSql);
					rs.executeUpdate(updateSql);
				}
			}
		} catch (Exception e) {
			result.put("errmsg","任务反馈保存异常!");
			result.put("flag", "false");
		}
		return result;
	}
}