package com.engine.odoc.cmd.standard.uploadPDF;


import java.util.Map;


import com.api.doc.detail.util.DocSecretLevelUtil;
import com.api.doc.detail.util.ImageConvertUtil;
import com.api.odoc.util.OdocFileUtil;
import com.engine.core.interceptor.Command;
import com.engine.core.interceptor.CommandContext;
import com.engine.hrm.biz.HrmClassifiedProtectionBiz;

import com.engine.odoc.util.DocUtil;
import com.google.common.collect.Maps;


import weaver.conn.RecordSet;

import weaver.docs.docs.DocCheckInOutUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.wps.view.linux.convert.SmartOfficial;


public class OdocUploadPDFCmd  implements Command<Map<String,Object>> {
	private User user;
	private Map<String, Object> params;
	private int imagefileid;
	private int secid;
	private int docid;
	private int workflowid;
	private int nodeid;
	private int requestid;
	private String extendName;
	private int secretLevel;
	private String secretValidity;
	public OdocUploadPDFCmd(User user,int imagefileid,int secid,int docid,int workflowid){
		this.user = user;
		this.imagefileid = imagefileid;
		this.secid = secid;
		this.docid = docid;
		this.workflowid = workflowid;
	}

	public OdocUploadPDFCmd(User user,Map<String, Object> params){
		this.user = user;
		this.imagefileid = Util.getIntValue(Util.null2String(params.get("imagefileid")),-1);
		this.secid = Util.getIntValue(Util.null2String(params.get("secid")),-1);
		this.docid = Util.getIntValue(Util.null2String(params.get("docid")),-1);
		this.workflowid = Util.getIntValue(Util.null2String(params.get("workflowid")),-1);
		this.nodeid = Util.getIntValue(Util.null2String(params.get("nodeid")),-1);
		this.requestid = Util.getIntValue(Util.null2String(params.get("requestid")),-1);
		this.extendName = Util.null2String(params.get("docType"));
		this.secretLevel = Util.getIntValue(Util.null2String(params.get("secretLevel")),4);
		this.secretValidity = Util.null2String(params.get("secretValidity"));
		this.params = params;
	}
	@Override
	public Map<String,Object> execute(CommandContext commandContext){
		Map<String, Object> apidatas = Maps.newHashMap();
		int pdfdocid  = -1;
		OdocFileUtil odocFileUtil = new OdocFileUtil();
		odocFileUtil.setSecId(secid);
		odocFileUtil.setDocStatus(workflowid);
		odocFileUtil.setParamIp(Util.null2String(params.get("param_ip")));
		String docname = "";
		if(docid <= 0){
			/*
			 * 上传文档时，不要自动排版
			if(ImageConvertUtil.canViewForWpsForLinux()
					&& DocUtil.autoSmartOfficialNode(workflowid,nodeid)
					&& DocUtil.docSupportedForSmartOfficial(extendName)){
				imagefileid = new SmartOfficial(user).smartOfficial(DocUtil.getSmartMouldImagefileId(workflowid),imagefileid,"test"+extendName);
			}
			if(imagefileid <= 0){
				apidatas.put("api_status", false);
				apidatas.put("api_message", "imagefileid <= 0");
				return apidatas;
			}
			 */
			String isExtfile="";
			RecordSet recordSet=new RecordSet();
			recordSet.executeQuery("select imagefilename from imagefile where imagefileid=?",imagefileid);
			if (recordSet.next()){
				String imagefilename=recordSet.getString("imagefilename");
				String extname=odocFileUtil.getFileExt(imagefilename);
				if (!extname.equalsIgnoreCase("doc")&&!extname.equalsIgnoreCase("docx")&&!extname.equalsIgnoreCase("wps")&&
						!extname.equalsIgnoreCase("pdf")&&!extname.equalsIgnoreCase("ofd")){
					isExtfile="1";
				}

			}
			pdfdocid = odocFileUtil.getDocIdByImageFileId(imagefileid,user,isExtfile);
			//若开启密级保护功能
			if(HrmClassifiedProtectionBiz.isOpenClassification() && pdfdocid > 0){
				//更新文档密级和保密期限
				DocSecretLevelUtil.modifyDocSecretLevel(pdfdocid+"",secretLevel+"",secretValidity);
				//记录当前文档是新创建的文档
				RecordSet rs = new RecordSet();
				rs.executeQuery("select docid from created_doc where docid=?",pdfdocid);
				if(!rs.next()){
					rs.executeUpdate("insert into created_doc (docid) values (?)",pdfdocid);
				}
			}
			Map<String,String> map = odocFileUtil.getDocNameAndDocTypeById(pdfdocid);
			docname = Util.null2String(map.get("docsubject"));
		}else{
			//odocFileUtil.replacePDFImagefileid(imagefileid,docid);
			pdfdocid = docid;
			Map<String,String> map = odocFileUtil.getDocNameAndDocTypeById(pdfdocid);
			docname = Util.null2String(map.get("docsubject"));
			String docFileType = Util.null2String(map.get("docType"));
			if ("13".equals(docFileType)){
				extendName = ".ofd";
			}
			DocUtil.saveDocImagefile(docid,imagefileid,docname + extendName,docFileType,"0");
			//如果有签章，替换PDF后签章数量设置为0
			DocCheckInOutUtil docCheckInOutUtil = new DocCheckInOutUtil();
			docCheckInOutUtil.saveIsignatureFun(String.valueOf(this.requestid),String.valueOf(this.nodeid),String.valueOf(this.user.getUID()),this.user.getLogintype(),"0");
		}

		//apidatas.put("settings", settings);
		apidatas.put("docid",pdfdocid);
		apidatas.put("docname",docname);
		apidatas.put("api_status", true);
		return apidatas;
	}

}
