package com.api.doc.detail.util;

import com.engine.doc.util.IWebOfficeConf;
import weaver.conn.RecordSet;
import weaver.general.Util;

public class CooperativeUtilForDoc {

    public static boolean isopencolla(int fileid){
        RecordSet rs = new RecordSet();
        String collasql = " select d3.ISCOLLASEC,d3.OPENCOLLAEDIT,d1.ISOPENCOLLA from DOCDETAIL d1,DOCIMAGEFILE d2 ,DOCSECCATEGORY d3 where d1.ID=d2.DOCID and d1.SECCATEGORY=d3.ID and d2.IMAGEFILEID=? order by d2.ID DESC ";
        rs.executeQuery(collasql,fileid);
        boolean iscollaborativedoc = false;
        if(rs.next()){
            int iscollasec = Util.getIntValue(rs.getString("ISCOLLASEC"), 0);
            int isopencolla = Util.getIntValue(rs.getString("ISOPENCOLLA"), 0);
            int opencollaedit = Util.getIntValue(rs.getString("OPENCOLLAEDIT"), 0);
            iscollaborativedoc = iscollasec ==1 && isopencolla == 1 && opencollaedit==1;
        }
        return iscollaborativedoc;
    }

    public static boolean isColla(int docid){
        RecordSet rs = new RecordSet();
        String collasql = " select d1.DOCTYPE,d2.ISCOLLASEC,d1.ISOPENCOLLA from DOCDETAIL d1,DOCSECCATEGORY d2 where d1.ID=? and d1.SECCATEGORY=d2.ID ";
        rs.executeQuery(collasql,docid);
        boolean iscollaborativedoc = false;
        if(rs.next()){
            int isopencolla = Util.getIntValue(rs.getString("ISOPENCOLLA"), 0);
            int doctype = Util.getIntValue(rs.getString("DOCTYPE"), 0);
            iscollaborativedoc = isopencolla == 1;
        }
        return iscollaborativedoc;
    }

    public static boolean isCollaEdit(int docid){
        RecordSet rs = new RecordSet();
        String collasql = " select d2.ISCOLLASEC,d2.OPENCOLLAEDIT,d1.ISOPENCOLLA,d1.doctype from DOCDETAIL d1,DOCSECCATEGORY d2 where d1.ID=? and d1.SECCATEGORY=d2.ID ";
        rs.executeQuery(collasql,docid);
        boolean iscollaborativedoc = false;
        if(rs.next()){
            int isopencolla = Util.getIntValue(rs.getString("ISOPENCOLLA"), 0);
            int opencollaedit = Util.getIntValue(rs.getString("OPENCOLLAEDIT"), 0);
            int doctype = Util.getIntValue(rs.getString("doctype"), 0);
            iscollaborativedoc = isopencolla == 1 && opencollaedit == 1 && doctype==2;
        }
        return iscollaborativedoc;
    }

    public static void openCollaForDoc(int docid){
        RecordSet rs = new RecordSet();
        String collasql = " select d2.ISCOLLASEC,d1.ISOPENCOLLA,d1.doctype from DOCDETAIL d1,DOCSECCATEGORY d2 where d1.ID=? and d1.SECCATEGORY=d2.ID ";
        rs.executeQuery(collasql,docid);
        boolean iscollaborativesec = false;
        if(rs.next()){
            int iscollasec = Util.getIntValue(rs.getString("ISCOLLASEC"), 0);
            int doctype = Util.getIntValue(rs.getString("doctype"), 0);
            iscollaborativesec = iscollasec == 1;
        }
        if(iscollaborativesec){
            rs.executeSql(" update docdetail set ISOPENCOLLA=1 where id="+docid);
        }
    }

    public static String getmFileType(int fileid){
        RecordSet rs = new RecordSet();
        String sql = "select a.imagefileid,a.imagefilename,a.docfiletype,a.versionId,b.filesize from DocImageFile a,Imagefile b " +
                " where a.imagefileid=b.imagefileid and a.imagefileid="+fileid+" and (a.isextfile <> '1' or a.isextfile is null) order by a.versionId desc";
        rs.executeQuery(sql);
        String mFileType = "";
        int docFileType = rs.getInt("docfiletype");
        String imageFileName = rs.getString("imagefilename");
        int versionId = rs.getInt("versionId");
        if(docFileType == 3){
            mFileType = ".doc";
        } else if(docFileType == 7){
            mFileType = ".docx";
        }else if(docFileType == 4){
            mFileType = ".xls";
        } else if(docFileType == 8){
            mFileType = ".xlsx";
        }else if(docFileType == 6){
            mFileType = ".wps";
        }else if(docFileType == 10){
            mFileType = ".et";
        }else if(docFileType == 12){
            mFileType = ".html";
        }else if(imageFileName.toLowerCase().endsWith(".uot") && IWebOfficeConf.isyozoOffice()){
            mFileType = ".doc";
        }
        return mFileType;
    }
}
