package com.api.doc.detail.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.engine.doc.util.TimeZoneUtil;
import weaver.conn.RecordSet;
import weaver.docs.docs.util.DocPointAddUtil;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

public class DocPraiseService {

	/**
	 * 获取点赞信息
	 * @author wangqs
	 * */
	public Map<String,Object> getPraiseInfo(int docid,User user) throws Exception{
		Map<String,Object> apidatas = new HashMap<String,Object>();
		
		int language = user == null ? DocSptm.getDefaultLanguage() : user.getLanguage();
		
		if(docid == 0){
			apidatas.put("status",-1);
			apidatas.put("msg",SystemEnv.getHtmlLabelName(19711,language));
			return apidatas;
		}
		
		RecordSet rs = new RecordSet();
		
		rs.executeQuery("select c.replyable,d.docstatus " +
				"from DocSecCategory c,DocDetail d where c.id=d.seccategory and d.id=?" , docid);
		
		boolean canParse = false;
		if(rs.next()){
			String docstatus = rs.getString("docstatus");
			if("1".equals(rs.getString("replyable"))  && ("5".equals(docstatus) || "2".equals(docstatus) || "1".equals(docstatus))){
				canParse = true;
			}
		}
		
		rs.executeQuery("SELECT ID,USERID,PRAISE_ID,PRAISE_TYPE,PRAISE_DATE,PRAISE_TIME FROM PRAISE_INFO WHERE PRAISE_TYPE = 0 AND DOCID = " + docid);
		Map<String,Object> praiseInfo = new HashMap<String,Object>();
		List<Map<String,String>> users = new ArrayList<Map<String,String>>();
		praiseInfo.put("users",users);
		praiseInfo.put("isPraised","0");
		praiseInfo.put("userid",user == null ? "" : user.getUID() + "");
		praiseInfo.put("username",user == null ? "" : user.getLastname());
		ResourceComInfo rci = new ResourceComInfo();
		while(rs.next()){
			praiseInfo.put("id",rs.getString("ID"));
			praiseInfo.put("praiseid",rs.getString("PRAISE_ID"));
			praiseInfo.put("type",rs.getString("PRAISE_TYPE") + "");
			if(user != null && rs.getInt("USERID")== user.getUID()){
				praiseInfo.put("isPraised","1");
			}else{
				Map<String,String> u = new HashMap<String,String>();
				u.put("userid",rs.getString("USERID"));
				u.put("username",rci.getLastname(rs.getString("USERID")));
				users.add(u);
			}
		}
		
		praiseInfo.put("canParse",canParse);
		
		apidatas.put("status",1);
		apidatas.put("praiseInfo",praiseInfo);
		
		return apidatas;
	}
	
	/**
	 * 点赞、取消赞
	 * @params docid：文档id，praiseid：回复节点ID,当是文档时，此ID为文档ID，type：0 文档，1 回复。
	 * */
	public Map<String,Object> praiseOperate(int docid,int praiseid,int type,User user,String operate){
		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;
		}

		if("do".equals(operate)){
			 try{	
				 RecordSet rs= new RecordSet();
		         rs.executeQuery("select id from PRAISE_INFO where docid =" + docid + 
		        		 " and userid=" + user.getUID() + 
		        		 " and PRAISE_ID=" + praiseid + " and PRAISE_TYPE="+type);
				 if(!rs.next()){
		            String formatdate = TimeUtil.getCurrentDateString();
		            String formattime = TimeUtil.getOnlyCurrentTimeString();
		            String sql = "INSERT INTO PRAISE_INFO(USERID,PRAISE_ID,PRAISE_TYPE,PRAISE_DATE,PRAISE_TIME,DOCID) VALUES('"+user.getUID()+"',"+praiseid+","+type+",'"+formatdate+"','"+formattime+"',"+docid+")";
		            rs.executeUpdate(sql);
		            apidatas.put("status",1);
		            apidatas.put("userid",user.getUID());
		        	apidatas.put("username",user.getLastname());
		        	if(type==0){
						Map<String,Object> replyParams = new HashMap<>();
						replyParams.put("docid",docid);
//					 replyParams.put("content",content);
						replyParams.put("replydata",formatdate);
						replyParams.put("replytime",formattime);
						replyParams.put("replyuserid",user.getUID());
						replyParams.put("remindtype",ReplyRemindUtil.PRAISEREMIND);
						ReplyRemindUtil.isSendRemind(replyParams,user);

						RecordSet rs1 = new RecordSet();
						RecordSet rst = new RecordSet();
						rs1.executeQuery("select * from uf_jftz where jfhdr=?",user.getUID());
						if(rs1.next()){
							double zjf = Util.getDoubleValue(rs1.getString("zjf"),0);
							double syjfs = Util.getDoubleValue(rs1.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);
						}

						DocPointAddUtil docPointAddUtil = new DocPointAddUtil();
						String contributePointByTypeJf = docPointAddUtil.getContributePointByType(DocPointAddUtil.DOCISLIKED, rs);
						String studyPointByTypeJf = docPointAddUtil.getStudyPointByType(DocPointAddUtil.LIKEDDOC, rs);
						docPointAddUtil.addPointLog(DocPointAddUtil.DOCISLIKED, docid + "", user, contributePointByTypeJf, studyPointByTypeJf, DocPointAddUtil.LIKEDDOC);
					}
				  }
		        }catch(Exception ex){
		        	apidatas.put("status",-1);
		        	apidatas.put("msg",SystemEnv.getHtmlLabelName(83912,user.getLanguage()));
		            ex.printStackTrace();
		        }
		}else if("un".equals(operate)){
			try{	
				String sql = "DELETE from PRAISE_INFO WHERE PRAISE_ID = "+praiseid+" AND PRAISE_TYPE = "+type+" AND USERID = '"+user.getUID()+"' AND DOCID = "+docid;
	            RecordSet rs = new RecordSet();
	            rs.executeUpdate(sql);
	            apidatas.put("status",1);
	            apidatas.put("userid",user.getUID());
	        	apidatas.put("username",user.getLastname());

				RecordSet rs1 = new RecordSet();
				RecordSet rst = new RecordSet();
				rs1.executeQuery("select * from uf_jftz where jfhdr=?",user.getUID());
				if(rs1.next()){
					double zjf = Util.getDoubleValue(rs1.getString("zjf"),0);
					double syjfs = Util.getDoubleValue(rs1.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);
				}
			}catch(Exception ex){
				apidatas.put("status",-1);
	            apidatas.put("msg",SystemEnv.getHtmlLabelName(83912,user.getLanguage()));
	            ex.printStackTrace();
	        }
		}
		
		return apidatas;
	}
}
