package com.api.doc.detail.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.api.doc.detail.util.ReplyRemindUtil;
import com.engine.doc.util.TimeZoneUtil;
import weaver.conn.EncodingUtils;
import weaver.conn.RecordSet;
import weaver.docs.docs.reply.DocReplyManager;
import weaver.docs.docs.reply.DocReplyModel;
import weaver.docs.docs.util.DocPointAddUtil;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.system.SysRemindWorkflow;
import weaver.systeminfo.SystemEnv;

import com.api.doc.search.util.DocSptm;

public class DocReplyService {
	
	public static int MINS = 10;  //允许删除、修改的时间（分钟）
	
	public static String SPLIT = "////~~weaversplit~~////";

	/**
	 * 获取回复数量
	 * */
	public Map<String,Object> getReplyCount(int docid,User user){
		Map<String,Object> apidatas = new HashMap<String,Object>();
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		RecordSet rs = new RecordSet();
		rs.executeSql("select count(id) num from DOC_REPLY where docid='" + docid + "'");
		
		int count = 0;
		if(rs.next()){
			count = rs.getInt("num");
		}
		
		apidatas.put("status",1);
		apidatas.put("replyCount",count);
		
		return apidatas;
	}
	
	/**
	 * 获取回复列表
	 * @author wangqs
	 * */
	public Map<String,Object> getDocReply(int docid,String order,User user) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		DocReplyManager docReply = new DocReplyManager();
		List<DocReplyModel> jsonList = docReply.getDocReply(docid + "",user.getUID()+"",false,0,15,5,order);
		repackModel(jsonList,user);
		apidatas.put("replyList",jsonList);
		return apidatas;
	}
	
	/**
	 * 删除回复
	 * @author wangqs
	 * */
	public Map<String,Object> delReply(int docid,int replymainid,int replyid,User user) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("api_status", false);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		String msg = SystemEnv.getHtmlLabelName(83912,user.getLanguage());  //操作失败
		
		RecordSet rs = new RecordSet();
		String sql = "select count(0) as replycount from doc_reply where docid = '"+docid+"' and PARENTID = "+replymainid+" and id > " +replyid;
	    rs.executeSql(sql);
	   	boolean canDel = true;
	    if(rs.next()){
	        if(rs.getInt("replycount") > 0){
	            canDel = false;
	            msg = SystemEnv.getHtmlLabelName(500471,user.getLanguage());//500471该回复已被回复,不允许删除!
	        }
	    }
	    
	    if(canDel){
	    	sql = "select userid,REPLYDATE,REPLYTIME from doc_reply where id=" + replyid;
		    rs.executeSql(sql);
		    rs.next();
			int replyuserid=Util.getIntValue(rs.getString("userid"));
			if(user.getUID()!=replyuserid){
				canDel = false;
			}
		    String operateTime = rs.getString("REPLYDATE") + " " + rs.getString("REPLYTIME");
		     
		    Date d = null;
		    try{
		    	d = new SimpleDateFormat("yyyy'-'MM'-'dd' 'HH:mm:ss").parse(operateTime);
		    }catch (Exception e) {
		    	e.printStackTrace();
				d = null;
			}
		    
		    Calendar c = Calendar.getInstance();
		    
		    long time = MINS*60*1000l;
		    if(d == null){
		    	canDel = false;
		    	msg = SystemEnv.getHtmlLabelName(391250,user.getLanguage());//	数据异常！请检查
		    }else if(c.getTimeInMillis() - d.getTime() > time){
		    	canDel = false;
				//回复已超过 "分钟,不允许删除!"
		    	msg = SystemEnv.getHtmlLabelName(500491,user.getLanguage())+ MINS + SystemEnv.getHtmlLabelName(500492,user.getLanguage());
		    }
	    }
	    
	    
	    if(canDel){
	    	DocReplyManager drm = new DocReplyManager();
	        drm.deleteReplyContent(replyid,docid + "");
	        //删除相关附件、图片
	        
	        rs.executeSql("select imagefileid from REPLY_IMAGEFILE where reply_id="+replyid);
	        boolean hasFile = false;
	        DocAccService accService = new DocAccService();
	        while(rs.next()){
	        	accService.deleteAcc(rs.getInt("imagefileid"),true);
	        	hasFile = true;
	        }
	        if(hasFile){
	            rs.executeSql("delete from REPLY_IMAGEFILE where reply_id="+replyid);
	        }
            //删除相关文档
            rs.executeSql("delete from REPLY_DOC where replyid="+replyid);
            //删除相关流程
            rs.executeSql("delete from REPLY_WORKFLOW where replyid="+replyid);

			RecordSet rss = new RecordSet();
			RecordSet rst = new RecordSet();
			rss.executeQuery("select * from uf_jftz where jfhdr=?",user.getUID());
			if(rss.next()){
				double zjf = Util.getDoubleValue(rss.getString("zjf"),0);
				double syjfs = Util.getDoubleValue(rss.getString("syjfs"),0);
				zjf -= 1;
				syjfs -= 1;
				boolean bool = rst.executeUpdate("update uf_jftz set zjf=?,syjfs=? where jfhdr=?",zjf,syjfs,user.getUID());
				rs.writeLog(docid+ " ====  删除回复  ==== "+ user.getUID() +"  ======== 去除1积分  bool  =============== "+ bool);
			}

	        apidatas.put("api_status", true);
	        apidatas.put("status",1);
			apidatas.put("msg", SystemEnv.getHtmlLabelName(83885,user.getLanguage()));//操作成功！
	    }else{
			apidatas.put("api_status", false);
	    	apidatas.put("status",-1);
	    	apidatas.put("msg",msg);
	    }
	    return apidatas;
	}
	
	/**
	 * 保存回复
	 * @param docid 文档id
	 * @param replyid 回复id（如果是文档的回复则为文档id）
	 * @param  -文档，1-回复
	 * @param content 回复内容
	 * @param user 用户对象
	 * @params requestMap
	 * */
	public Map<String,Object> saveReply(int docid,int replyid,String content,String operate,User user,Map<String,String> params) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		content = content == null ? "" : content.replace("'","''");
		RecordSet rs = new RecordSet();

		content = EncodingUtils.toUNICODE(content);
		int maxid = 0;
		
		if("edit".equals(operate)){
			
			if(replyid <= 0){
				apidatas.put("status",-1);
				apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
				return apidatas;
			}
			
			rs.executeSql("select userid,REPLYDATE,REPLYTIME from doc_reply where id=" + replyid);
			rs.next();
		    String operateTime = rs.getString("REPLYDATE") + " " + rs.getString("REPLYTIME");
		    int replyuserid=Util.getIntValue(rs.getString("userid"));			
		    Date d = null;
		    try{
		    	d = new SimpleDateFormat("yyyy'-'MM'-'dd' 'HH:mm:ss").parse(operateTime);
		    }catch (Exception e) {
		    	e.printStackTrace();
				d = null;
			}
		    
		    Calendar c = Calendar.getInstance();
		    boolean canEdit = true;
		    String msg = "";
		    long time = MINS*60*1000l;
			if(user.getUID()!=replyuserid){
				canEdit = false;
			}
		    if(d == null){
		    	canEdit = false;
				msg = SystemEnv.getHtmlLabelName(391250,user.getLanguage());//	数据异常！请检查
		    }else if(c.getTimeInMillis() - d.getTime() > time){
		    	canEdit = false;
				//回复已超过 "分钟,不允许修改!"
				msg = SystemEnv.getHtmlLabelName(500491,user.getLanguage())+ MINS + SystemEnv.getHtmlLabelName(500493,user.getLanguage());
		    }
		    
		    if(canEdit){
		    	rs.executeUpdate("update doc_reply set CONTENT=? where id=?", content, replyid);
		    	maxid = replyid;
		    }else{
		    	apidatas.put("status",-1);
		    	apidatas.put("msg",msg);
		    }
			
		}else{
			String ruserid = user.getUID() + "";
			int parentid = 0;
			int type = 0;
			
			if(replyid > 0){   //子回复
				rs.executeSql("select userid,parentid from doc_reply where id=" + replyid);
				if(rs.next()){
					ruserid = Util.null2String(rs.getString("userid"));
					parentid = rs.getInt("parentid");
				}
				type = 1;
			}else{
				replyid = -1;
			}
			RecordSet rs1 = new RecordSet();
			RecordSet rss = new RecordSet();
			RecordSet rst = new RecordSet();
			rs1.executeQuery("select * from doc_reply where docid=? and userid=?",docid,user.getUID());
			if(!rs1.next()){
				rss.executeQuery("select * from uf_jftz where jfhdr=?",user.getUID());
				if(rss.next()){
					double zjf = Util.getDoubleValue(rss.getString("zjf"),0);
					double syjfs = Util.getDoubleValue(rss.getString("syjfs"),0);
					zjf += 1;
					syjfs += 1;
					boolean bool = rst.executeUpdate("update uf_jftz set zjf=?,syjfs=? where jfhdr=?",zjf,syjfs,user.getUID());
					rs.writeLog(docid+ " ====  文档第一次回复  ==== "+ user.getUID() +"  ======== 获得1积分  bool  =============== "+ bool);
				}
			}
			String REPLYDATE = TimeUtil.getCurrentDateString();
			String REPLYTIME = TimeUtil.getOnlyCurrentTimeString();
			String sql = "insert into doc_reply(DOCID, USERID, REPLY_PARENTID, CONTENT, RUSERID, RTYPE, REPLYDATE, REPLYTIME, PARENTID) values(?,?,?,?,?,?,?,?,?)";
			
			rs.executeUpdate(sql, new Object[]{docid, user.getUID(), replyid, content, ruserid, type, REPLYDATE, REPLYTIME, parentid});
			//rs.writeLog("===========Content============:"+content);
			rs.executeQuery("select max(id) mid from doc_reply where docid= ?", docid);
			if(rs.next()){
				maxid = rs.getInt("mid");
				if(type == 0){ //主回复
					rs.executeUpdate("update doc_reply set parentid=? where id=?", maxid, maxid);
				}
				rs.executeUpdate("update docdetail set replaydoccount=replaydoccount+1 where id =? ", docid);
			
				//触发提醒工作流
				SysRemindWorkflow srw = new SysRemindWorkflow();
	            sql = "select canremind,docsubject,doccreaterid from DocDetail where id=?";
	            String canRemind = "";
	            rs.executeQuery(sql, docid);
	            if (rs.next()) {
	                canRemind = rs.getString("canremind");
	                if (canRemind.equals("2")) {
	                    srw.setSysRemindInfo(SystemEnv.getHtmlLabelName(18641,user.getLanguage())+":" + rs.getString("docsubject"), docid, 0, 0, 0, user.getUID(),
	                    rs.getString("doccreaterid") + "", "");
	                }
	           }
			}
			if(replyid <= 0){
				Map<String,Object> replyParams = new HashMap<>();
				replyParams.put("docid",docid);
				replyParams.put("content",content);
				replyParams.put("replydata",REPLYDATE);
				replyParams.put("replytime",REPLYTIME);
				replyParams.put("replyuserid",user.getUID() + "");
				replyParams.put("remindtype",ReplyRemindUtil.REPLYREMIND);
				ReplyRemindUtil.isSendRemind(replyParams,user);
			}

		}
		
		if(maxid > 0){
			rs.executeSql("select *from doc_reply where id=" + maxid);
			
			if(rs.next()){
				DocReplyModel replyModel = new DocReplyModel();
				replyModel.setReplyid(rs.getString("id"));
				replyModel.setDocid(rs.getString("docid"));
				replyModel.setContent(EncodingUtils.toUTF8(rs.getString("content")));
				try {
					replyModel.setRuserid(rs.getString("ruserid"));
				} catch (Exception e) {
				}
				try {
					replyModel.setUserid(rs.getString("userid"));
				} catch (Exception e) {
				}
				replyModel.setRreplyid(rs.getString("reply_parentid"));
				replyModel.setReplymainid(rs.getString("parentid"));
				replyModel.setRtype(rs.getString("rtype"));
				String time_replydatetime = rs.getString("replydate") + " " + rs.getString("replytime");
				time_replydatetime = TimeZoneUtil.getYmdLocaleDate1(time_replydatetime);
				String time_replydate = time_replydatetime.substring(0,10);
				String time_replytime = time_replydatetime.substring(11);
				replyModel.setRdata(time_replydate);
	            replyModel.setRtime(time_replytime);
	            replyModel.setOrderno(rs.getString("orderno"));
	            
	            saveReplyImageFile(replyModel, params,operate);
	            saveReplyDoc(replyModel, params,operate);
	            saveReplyWorkflow(replyModel, params,operate);
	            
	            List<DocReplyModel> replyModels = new ArrayList<DocReplyModel>();
	            replyModels.add(replyModel);
	            repackModel(replyModels,user);
	            
	            apidatas.put("reply",replyModels.get(0));
	            apidatas.put("status",1);
			}
			
		}
		DocPointAddUtil docPointAddUtil = new DocPointAddUtil();
		String contributePointByTypeJf = docPointAddUtil.getContributePointByType(DocPointAddUtil.DOCISREPLIED, rs);
		String studyPointByTypeJf = docPointAddUtil.getStudyPointByType(DocPointAddUtil.REPLIEDDOC, rs);
		docPointAddUtil.addPointLog(DocPointAddUtil.DOCISREPLIED, docid + "", user, contributePointByTypeJf, studyPointByTypeJf, DocPointAddUtil.REPLIEDDOC);
		return apidatas;
	}
	
	
	private void saveReplyImageFile(DocReplyModel replyModel,Map<String,String> params,String operate){
		String imgFileids = params.get("imgFileids");
		String imgFilenames = params.get("imgFilenames");
		
		RecordSet rs = new RecordSet();
		List<Map<String,String>> imgs = new ArrayList<Map<String,String>>();
		Map<String,String> existImgs = new HashMap<String,String>();
		if(imgFileids != null && !imgFileids.isEmpty()){
			String sql = "";
			try{
				String[] ids = imgFileids.split(",");
				String[] names = imgFilenames.split(SPLIT);
				
				if("edit".equals(operate)){
					rs.executeSql("select imagefileid from REPLY_IMAGEFILE where incontent=1 and reply_id=" + replyModel.getReplyid());
					while(rs.next()){
						existImgs.put(rs.getString("imagefileid"),"1");
					}
				}
				
				for(int i = 0;i < ids.length;i++){
					if(ids[i].isEmpty()) continue;
					
					if(existImgs.get(ids[i]) != null){
						existImgs.put(ids[i],"0");
						continue;
					}
					
					sql = "insert into REPLY_IMAGEFILE(imagefileid,reply_id,incontent,type,imagefilename) values" +
							"(" +
							"" + ids[i] +
							"," + replyModel.getReplyid() +
							",1" +
							",0" +
							",'" + names[i] + "'" +
							")";
					boolean b = rs.executeSql(sql);
					if(b){
						Map<String,String> img = new HashMap<String,String>();
						img.put(ids[i],names[i]);
						imgs.add(img);
					}
				}
				
			}catch (Exception e) {
				rs.writeLog(e);
			}
		}
		replyModel.setAboutImgs(imgs);
		
		String fileids = params.get("field-annexupload");
		String filenames = params.get("field-annexupload-name");
		
		List<Map<String,String>> files = new ArrayList<Map<String,String>>();
		Map<String,String> existFiles = new HashMap<String,String>();
		if(fileids != null && !fileids.isEmpty()){
			String sql = "";
			try{
				String[] ids = fileids.split(",");
				String[] names = filenames.split(SPLIT);
				
				if("edit".equals(operate)){
					rs.executeSql("select imagefileid,imagefilename from REPLY_IMAGEFILE where incontent=0 and reply_id=" + replyModel.getReplyid());
					while(rs.next()){
						existFiles.put(rs.getString("imagefileid"),"1");
						
						Map<String,String> file = new HashMap<String,String>();
						file.put(rs.getString("imagefileid"),rs.getString("imagefilename"));
						files.add(file);
					}
				}
				
				for(int i = 0;i < ids.length;i++){
					if(ids[i].isEmpty()) continue;
					
					if(existFiles.get(ids[i]) != null){
						existFiles.put(ids[i],"0");
						continue;
					}
					
					sql = "insert into REPLY_IMAGEFILE(imagefileid,reply_id,incontent,type,imagefilename) values" +
							"(" +
							"" + ids[i] +
							"," + replyModel.getReplyid() +
							",0" +
							",0" +
							",'" + names[i] + "'" +
							")";
					boolean b = rs.executeSql(sql);
					
					if(b){
						Map<String,String> file = new HashMap<String,String>();
						file.put(ids[i],names[i]);
						files.add(file);
					}
				}
				
			}catch (Exception e) {
				rs.writeLog(e);
			}
		}
		
		String delIds = "";
		for(String key : existImgs.keySet()){
			if("1".equals(existImgs.get(key))){
				delIds += "," + key;
			}
		}
		for(String key : existFiles.keySet()){
			if("1".equals(existFiles.get(key))){
				delIds += "," + key;
			}
		}
		
		if(!delIds.isEmpty()){
			delIds = delIds.substring(1);
			//rs.executeSql("delete from REPLY_IMAGEFILE where reply_id=" + replyModel.getReplyid() + " and imagefileid in(" + delIds + ")");
		}
		
		replyModel.setAboutFiles(files);
		
		
	}
	
	private void saveReplyDoc(DocReplyModel replyModel,Map<String,String> params,String operate){
		String docids = params.get("signdocids");
		String docnames = params.get("signdocnames");
		
		RecordSet rs = new RecordSet();
		List<Map<String,String>> docs = new ArrayList<Map<String,String>>();
		Map<String,String> existDocs = new HashMap<String,String>();
		if(docids != null && !docids.isEmpty()){
			String sql = "";
			try{
				String[] ids = docids.split(",");
				String[] names = docnames.split(SPLIT);
				
				if("edit".equals(operate)){
					rs.executeSql("select a.docid,b.docsubject from REPLY_DOC a,DocDetail b where a.docid=b.id and a.replyid=" + replyModel.getReplyid());
					while(rs.next()){
						existDocs.put(rs.getString("docid"),"1");
						
						Map<String,String> doc = new HashMap<String,String>();
						doc.put(rs.getString("docid"),rs.getString("docsubject"));
						docs.add(doc);
					}
				}
				
				for(int i = 0;i < ids.length;i++){
					if(ids[i].isEmpty()) continue;
					
					if(existDocs.get(ids[i]) != null){
						existDocs.put(ids[i],"0");
						continue;
					}
					
					sql = "insert into REPLY_DOC(replyid,docid,replydate,replytime) values" +
							"(" +
							"" + replyModel.getReplyid() +
							"," + ids[i] +
							",'" + replyModel.getRdata() + "'" +
							",'" + replyModel.getRtime() + "'" +
							")";
					boolean b = rs.executeSql(sql);
					if(b){
						Map<String,String> doc = new HashMap<String,String>();
						doc.put(ids[i],names[i]);
						docs.add(doc);
					}	
				}
				
			}catch (Exception e) {
				rs.writeLog(e);
			}
		}
		String delIds = "";
		for(String key : existDocs.keySet()){
			if("1".equals(existDocs.get(key))){
				delIds += "," + key;
			}
		}
		
		if(!delIds.isEmpty()){
			delIds = delIds.substring(1);
			//rs.executeSql("delete from REPLY_DOC where replyid=" + replyModel.getReplyid() + " and docid in(" + delIds + ")");
		}
		
		replyModel.setAboutDocs(docs);
	}
	
	private void saveReplyWorkflow(DocReplyModel replyModel,Map<String,String> params,String operate){
		String workflowids = params.get("signworkflowids");
		String workflownames = params.get("signworkflownames");
		
		RecordSet rs = new RecordSet();
		List<Map<String,String>> workflows = new ArrayList<Map<String,String>>();
		Map<String,String> existWorkflows = new HashMap<String,String>();
		if(workflowids != null && !workflowids.isEmpty()){
			String sql = "";
			try{
				String[] ids = workflowids.split(",");
				String[] names = workflownames.split(SPLIT);
				
				if("edit".equals(operate)){
					rs.executeSql("select workflowid,workflowname from REPLY_WORKFLOW where replyid=" + replyModel.getReplyid());
					while(rs.next()){
						existWorkflows.put(rs.getString("workflowid"),"1");
						
						Map<String,String> workflow = new HashMap<String,String>();
						workflow.put(rs.getString("workflowid"),rs.getString("workflowname"));
						workflows.add(workflow);
					}
				}
				
				for(int i = 0;i < ids.length;i++){
					if(ids[i].isEmpty()) continue;
					
					if(existWorkflows.get(ids[i]) != null){
						existWorkflows.put(ids[i],"0");
						continue;
					}
					
					sql = "insert into REPLY_WORKFLOW(replyid,workflowid,replydate,replytime,workflowname) values" +
							"(" +
							"" + replyModel.getReplyid() +
							"," + ids[i] +
							",'" + replyModel.getRdata() + "'" +
							",'" + replyModel.getRtime() + "'" +
							",'" + names[i] + "'" +
							")";
					boolean b = rs.executeSql(sql);
					if(b){
						Map<String,String> workflow = new HashMap<String,String>();
						workflow.put(ids[i],names[i]);
						workflows.add(workflow);
					}	
				}
				
			}catch (Exception e) {
				rs.writeLog(e);
			}
		}
		String delIds = "";
		for(String key : existWorkflows.keySet()){
			if("1".equals(existWorkflows.get(key))){
				delIds += "," + key;
			}
		}
		
		if(!delIds.isEmpty()){
			delIds = delIds.substring(1);
			//rs.executeSql("delete from REPLY_WORKFLOW where replyid=" + replyModel.getReplyid() + " and workflowid in(" + delIds + ")");
		}
		
		replyModel.setAboutwfs(workflows);
	}
	
	/**
	 * 获取剩余子回复
	 * @author wangqs
	 * @params docid 文档id
	 * @params replymainid 主回复id
	 * @params lastReplyid 最后一个子回复id
	 * @params user 用户对象
	 * */
	public Map<String,Object> residueReply(int docid,String replymainid,String lastReplyid,User user) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		DocReplyManager drm = new DocReplyManager();
		List<DocReplyModel> jsonList = drm.getResidueReplysForReply(lastReplyid,replymainid,docid + "",user.getUID()+"",false);
		repackModel(jsonList,user);
		apidatas.put("replyList",jsonList);
		return apidatas;
	}
	
	/**
	 * 子回复获取更多
	 * @author wangqs
	 * */
	public Map<String,Object> moreReply(int docid,int replyid,String orderby,int pageSize,int childrenSize,User user) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,user.getLanguage()));
			return apidatas;
		}
		
		DocReplyManager drm = new DocReplyManager();
		List<DocReplyModel> jsonList = drm.getDocReply(docid + "",user.getUID()+"",false,replyid,pageSize,childrenSize,orderby);
		repackModel(jsonList,user);
		apidatas.put("replyList",jsonList);
		return apidatas;
	}
	
	/**
	 * 重新封装图片、附件、文档、流程等
	 * */
	public void repackModel(List<DocReplyModel> docReplyModels,User user){
		//String regEx_space = "\\s*|\t|\r|\n"; //定义空格回车换行符 
		String regEx_space = "\t|\r|\n"; //定义回车换行符 
		Pattern p_html1 = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
		for(DocReplyModel docReplyModel : docReplyModels){
			
			String content = docReplyModel.getContent();
			 Matcher m_html1 = p_html1.matcher(content); 
			 content = m_html1.replaceAll(""); 
			docReplyModel.setContent(content);
			
			List<Map<String,String>> imgs = docReplyModel.getAboutImgs();
			if(imgs != null && imgs.size() > 0){
				for(Map<String,String> img : imgs){
					
					String id = "";
					for(String key : img.keySet()){
						id = key;
						break;
					}
					img.put("linkUrl","<img src='" + DocSptm.REPLY_FILE_DOWNLOAD +
							"?docid=" + docReplyModel.getDocid() + 
							"&model=reply&download=1&fileid=" + id + "'/>");
				}
			}
			
			List<Map<String,String>> files = docReplyModel.getAboutFiles();
			if(files != null && files.size() > 0){
				RecordSet rs = new RecordSet();
				DocSptm docSptm = new DocSptm();
				for(Map<String,String> file : files){
					
					String id = "";
					String name = "";
					for(String key : file.keySet()){
						id = key;
						name = file.get(key);
						break;
					}
					int size = 0;
					if(Util.getIntValue(id,0) > 0){
						rs.executeSql("select filesize from imagefile where imagefileid=" + id);
						rs.next();
						size = rs.getInt("filesize");
					}
					file.put("linkUrl","<a contenteditable=\"false\"" +
							" href=\"" + DocSptm.ACC_DETAIL_LINK + "?docid=" + docReplyModel.getDocid() + 
							"&model=reply&imagefileId=" + id + DocSptm.ACC_DETAIL_ROUT + "\"" +
							" target=\"_blank\" "+
							" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px\" " +
							" unselectable=\"off\">" + name + "</a>" +
							" <a contenteditable=\"false\" href=\""+weaver.general.GCONST.getContextPath()+"/weaver/weaver.file.FileDownload?fileid="+ id +"&docid=8474&model=reply&download=1\" " +
							" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px\" " +
							" unselectable=\"off\">" + SystemEnv.getHtmlLabelName(258,user.getLanguage()) + "(" + docSptm.getFileSize(size + "") +")</a>");
				}
			}
			
			List<Map<String,String>> docs = docReplyModel.getAboutDocs();
			if(docs != null && docs.size() > 0){
				for(Map<String,String> doc : docs){
					
					String id = "";
					String name = "";
					for(String key : doc.keySet()){
						id = key;
						name = doc.get(key);
						break;
					}
					doc.put("linkUrl","<a contenteditable=\"false\" href=\"javascript:void(0);\" " +
							"onclick=\"ecCom.WeaRichText.openAppLink(this," + id + ",37)\" " +
							"style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px\" " +
							"unselectable=\"off\">" + name + "</a>");
					
				}
			}
			
			
			List<Map<String,String>> wfs = docReplyModel.getAboutwfs();
			if(wfs != null && wfs.size() > 0){
				for(Map<String,String> wf : wfs){
					
					String id = "";
					String name = "";
					for(String key : wf.keySet()){
						id = key;
						name = wf.get(key);
						break;
					}
					wf.put("linkUrl","<a contenteditable=\"false\" href=\"javascript:void(0);\" " +
							"onclick=\"ecCom.WeaRichText.openAppLink(this," + id + ",152)\" " +
							"style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px\" " +
							"unselectable=\"off\">" + name + "</a>");
				}
			}
			
		}
	}
}
