package com.engine.htxc.commonutil.fileutil;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.htxc.commonutil.dao.FileDao;
import weaver.conn.RecordSet;
import weaver.docs.category.SecCategoryComInfo;
import weaver.docs.docs.DocComInfo;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.DocManager;
import weaver.docs.docs.DocViewer;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.wechat.util.Utils;

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

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName FileUtil.java
 * @createTime 2022年07月01日 09:35:00
 */
public class FileUtil {

    public FileDao filedao=new FileDao();
    /**
     * @title getfilelinfo
     * @author K1810015
     * @updateTime 2022/7/1 9:41
     * @Remark 获取旧的附件
     * @throws
     */
    public JSONArray getfilelinfo(User user, List retdoclist) {
        if(retdoclist==null && retdoclist.size()<=0 ){
            return null;
        }
        List<Map<String,Object>> list = filedao.getFileInfo(retdoclist);
        if(list==null || list.size()<=0){
            return null;
        }
        List<String> doclist=new ArrayList<>();
        JSONArray jsonarrayall=new JSONArray();
        list.forEach((Map<String,Object> returnmap)->{
            JSONObject jsonObject=new JSONObject();
            int size=Util.getIntValue(Util.null2String(returnmap.get("filesize")));
            String filesize="1K";
            String docid=Util.null2String(returnmap.get("docid"));
            String imagefilename= Util.null2String(returnmap.get("imagefilename"));
            if(size>0){
                int sizevalue=size/1024;
                if(sizevalue>0){
                    filesize=sizevalue+"K";
                }
            }
            try {
                String filelink = "/spa/document/index2file.jsp?f_weaver_belongto_userid=" + user.getUID() + "&f_weaver_belongto_usertype=0&id=" + docid + "&imagefileId=" + imagefilename + "&isFromAccessory=true&isrequest=1&requestid=-1&desrequestid=-1&authStr=undefined&authSignatureStr=undefined";
                String loadlink = "/weaver/weaver.file.FileDownload?fileid=" + docid + "&download=1&requestid=-1&desrequestid=-1&authStr=undefined&authSignatureStr=undefined&f_weaver_belongto_userid=" + user.getUID() + "&f_weaver_belongto_usertype=0";
                jsonObject.put("appID", "");
                jsonObject.put("fileID", docid);
                jsonObject.put("fileid", docid);
                jsonObject.put("filelink", filelink);
                jsonObject.put("fileExtendName", imagefilename.split("\\.")[imagefilename.split("\\.").length-1]);
                jsonObject.put("filesize", filesize);
                jsonObject.put("filename", imagefilename);
                jsonObject.put("imagefileid", Util.null2String(returnmap.get("imagefileid")));
                jsonObject.put("imgSrc", "/images/filetypeicons/pdf_wev8.png");
                jsonObject.put("isImg", false);
                jsonObject.put("loadlink", loadlink);
                jsonObject.put("operatedate", Util.null2String(returnmap.get("operatedate")));
                jsonObject.put("operatetime", Util.null2String(returnmap.get("operatetime")));
                jsonObject.put("showDelete", true);
                jsonObject.put("size", size);
                jsonObject.put("uploaddate", Util.null2String(returnmap.get("operatedate")) + " "+Util.null2String(returnmap.get("operatetime")) );
                jsonObject.put("userid", 0);
                jsonObject.put("username", "文档中心");
                jsonObject.put("versionCount", "v1");
                JSONArray jsonArray=new JSONArray();
                jsonArray.add(jsonObject);
                doclist.add(docid);
                JSONObject value1=new JSONObject();
                value1.put("value",doclist.stream().collect(Collectors.joining(",")));
                JSONObject filedatas=new JSONObject();
                filedatas.put("filedatas",jsonArray);
                value1.put("specialobj",filedatas);
                jsonarrayall.add(value1);
            }catch (Exception exo){
            }
        });

        return jsonarrayall;
    }

    public JSONArray getdocimagefileinfo(User user,  List retdoclist) {
        if(retdoclist==null && retdoclist.size()<=0 ){
            return null;
        }
        List<Map<String,Object>> list = filedao.getNotcoryFileInfo(retdoclist);
        if(list==null || list.size()<=0){
            return null;
        }
        JSONArray jsonarrayall=new JSONArray();
        list.forEach((Map<String,Object> returnmap)->{
            JSONObject jsonObject=new JSONObject();
            int size=Util.getIntValue(Util.null2String(returnmap.get("filesize")));
            String filesize="1K";
            String docid=Util.null2String(returnmap.get("docid"));
            String imagefilename= Util.null2String(returnmap.get("imagefilename"));
            if(size>0){
                int sizevalue=size/1024;
                if(sizevalue>0){
                    filesize=sizevalue+"K";
                }
            }
            try {
                String filelink = "/spa/document/index2file.jsp?f_weaver_belongto_userid=" + user.getUID() + "&f_weaver_belongto_usertype=0&id=" + docid + "&imagefileId=" + imagefilename + "&isFromAccessory=true&isrequest=1&requestid=-1&desrequestid=-1&authStr=undefined&authSignatureStr=undefined";
                String loadlink = "/weaver/weaver.file.FileDownload?fileid=" + docid + "&download=1&requestid=-1&desrequestid=-1&authStr=undefined&authSignatureStr=undefined&f_weaver_belongto_userid=" + user.getUID() + "&f_weaver_belongto_usertype=0";
                jsonObject.put("appID", "");
                jsonObject.put("fileID", docid);
                jsonObject.put("fileid", docid);
                jsonObject.put("filelink", filelink);
                jsonObject.put("filesize", filesize);
                jsonObject.put("filename", imagefilename);
                jsonObject.put("fileExtendName", imagefilename.split("\\.")[imagefilename.split("\\.").length-1]);
                jsonObject.put("imagefileid", Util.null2String(returnmap.get("imagefileid")));
                jsonObject.put("imgSrc", "/images/filetypeicons/pdf_wev8.png");
                jsonObject.put("isImg", false);
                jsonObject.put("loadlink", loadlink);
                jsonObject.put("operatedate", DateTime.now().toString("yyyy-MM-dd"));
                jsonObject.put("operatetime", DateTime.now().toString("hh:mm:ss"));
                jsonObject.put("showDelete", true);
                jsonObject.put("size", size);
                jsonObject.put("uploaddate", DateTime.now().toString("yyyy-MM-dd") + " "+DateTime.now().toString("hh:mm:ss") );
                jsonObject.put("userid", 0);
                jsonObject.put("username", "文档中心");
                jsonObject.put("versionCount", "v1");
                jsonarrayall.add(jsonObject);
            }catch (Exception exo){
            }
        });

        return jsonarrayall;
    }



    /**
     * @title createdocdetail
     * @author K1810015
     * @updateTime 2022/3/11 15:04
     * @Remark 创建docdetail表
     * @throws
     */
    public static int createdocdetail(Map<String,Object> map){
        String title= Utils.null2String(map.get("title"));
        String body=Utils.null2String(map.get("body"));
        int creater=Utils.getIntValue(Utils.null2String(map.get("creater")),0);
        String rq=Utils.null2String(map.get("rq"));
        String docextendname=Utils.null2String(map.get("docextendname"));
        int maincategory=Utils.getIntValue(Utils.null2String(map.get("maincategory")),0);
        int subcategory=Utils.getIntValue(Utils.null2String(map.get("subcategory")),0);
        int seccategory=Utils.getIntValue(Utils.null2String(map.get("seccategory")),0);
        int doctype=Utils.getIntValue(Utils.null2String(map.get("doctype")),0);
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        try{
            int docLanguage = 7 ;
            int sec = seccategory;
            ResourceComInfo r = new ResourceComInfo();
            int departmentid = Integer.parseInt(r.getDepartmentID(creater+""));
            bs.writeLog("departmentid:"+departmentid);
            SecCategoryComInfo secCategoryComInfo=new SecCategoryComInfo();

            DocComInfo docComInfo=new DocComInfo();
            DocManager docManager=new DocManager();
            DocViewer docViewer=new DocViewer();

            String nowTime= TimeUtil.getOnlyCurrentTimeString();

            String pubishdate = rq;
            String publishtime = nowTime;

            RecordSet recordSet = new RecordSet();
            int newDocId = docManager.getNextDocId(recordSet);

            docManager.setId(newDocId);
            docManager.setDocsubject(title);
            docManager.setMaincategory(maincategory);
            docManager.setSubcategory(subcategory);
            docManager.setSeccategory(sec);
            docManager.setLanguageid(docLanguage);
            docManager.setDocstatus("1");

            docManager.setDoccreaterid(creater);
            docManager.setDocdepartmentid(departmentid);
            docManager.setDocCreaterType("1");
            docManager.setUsertype("1");
            docManager.setOwnerid(creater);
            docManager.setOwnerType("1");
            docManager.setDoclastmoduserid(creater);
            docManager.setDocLastModUserType("1");

            docManager.setDoccreatedate(pubishdate);
            docManager.setDoclastmoddate(pubishdate);
            docManager.setDoccreatetime(publishtime);
            docManager.setDoclastmodtime(publishtime);

            docManager.setDoclangurage(docLanguage);

            docManager.setKeyword(title);
            docManager.setIsapprover("0");
            docManager.setIsreply("");
            docManager.setDocreplyable("1");
            docManager.setAccessorycount(1);
            docManager.setParentids(newDocId+"");
            docManager.setOrderable(secCategoryComInfo.getSecOrderable(seccategory)+"");
            docManager.setUserid(1);
            docManager.setDocCode("");
            docManager.setDocEditionId(docManager.getNextEditionId(recordSet));
            docManager.setDocEdition(1);
            docManager.setDoccontent(body);
            docManager.setDocType(doctype);
            docManager.setDocextendname(docextendname);
            docManager.AddDocInfo();
            docComInfo.addDocInfoCache("" + newDocId);
            docViewer.setDocShareByDoc("" + newDocId);
            docManager.AddShareInfo();

            return newDocId;
        } catch(Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @title createdocimagefile
     * @author K1810015
     * @updateTime 2022/3/11 15:04
     * @Remark 创建docimagefile表
     * @throws
     */
    public static void createdocimagefile(Map<String,Object> map){
        DocImageManager imgManger = new DocImageManager();
        String docid= Utils.null2String(map.get("docid"));
        String imagefileid=Utils.null2String(map.get("imagefileid"));
        String imagefilename=Utils.null2String(map.get("imagefilename"));
        String ext=Utils.null2String(map.get("docextendname"));
        int versionid=Utils.getIntValue(Utils.null2String(map.get("imagefilename")),0);
        int operateruserid=Utils.getIntValue(Utils.null2String(map.get("operateruserid")),0);

        String operatedate=Utils.null2String(map.get("operatedate"));
        String operatetime=Utils.null2String(map.get("operatetime"));

        if (ext.equalsIgnoreCase("doc")) {
            imgManger.setDocfiletype("3");
        } else if (ext.equalsIgnoreCase("xls")) {
            imgManger.setDocfiletype("4");
        } else if (ext.equalsIgnoreCase("ppt")) {
            imgManger.setDocfiletype("5");
        } else if (ext.equalsIgnoreCase("wps")) {
            imgManger.setDocfiletype("6");
        } else if (ext.equalsIgnoreCase("docx")) {
            imgManger.setDocfiletype("7");
        } else if (ext.equalsIgnoreCase("xlsx")) {
            imgManger.setDocfiletype("8");
        } else if (ext.equalsIgnoreCase("pptx")) {
            imgManger.setDocfiletype("9");
        } else if (ext.equalsIgnoreCase("et")) {
            imgManger.setDocfiletype("10");
        } else {
            imgManger.setDocfiletype("2");
        }
        imgManger.setDocid(Util.getIntValue(docid, 0));
        imgManger.setImagefileid(Util.getIntValue(imagefileid, 0));
        imgManger.setImagefilename(imagefilename);
        imgManger.setimagefilewidth(0);
        imgManger.setImagefileheight(0);
        imgManger.setIsextfile("1");
        imgManger.setImagefielsize(0);
        imgManger.setVersionId(versionid);
        imgManger.setOperatedate(operatedate);
        imgManger.setOperatetime(operatetime);
        imgManger.setOperateuserid(operateruserid);
        imgManger.AddDocImageInfo();
    }

    /**
     * @title createimagefile
     * @author K1810015
     * @updateTime 2022/3/11 15:05
     * @Remark 创建imagefile表
     * @throws
     */
    public static  void createimagefile(Map<String,Object> map){

        String tablename = "imagefile";
        String imagefileid = Utils.null2String(map.get("imagefileid"));
        String imagefiletype = Utils.null2String(map.get("imagefiletype"));
        String filesize = Utils.null2String(map.get("filesize"));
        String imagefilename= Utils.null2String(map.get("imagefilename"));
        String aescode=Utils.null2String(map.get("aescode"));
        String tokenkey=Utils.null2String(map.get("tokenkey"));
        String ftpconfigid=Utils.null2String(map.get("ftpconfigid"));
        String isaesencrypt=Utils.null2String(map.get("isaesencrypt"));
        String imagefileused=Utils.null2String(map.get("imagefileused"));
        String iszip=Utils.null2String(map.get("iszip"));
        String filerealpath=Utils.null2String(map.get("filerealpath"));
        Map<String, String> filemap = new HashMap<>(11);
        filemap.put("imagefileid", imagefileid);
        filemap.put("imagefilename", imagefilename);
        filemap.put("imagefiletype", imagefiletype);
        filemap.put("imagefileused",imagefileused);
        filemap.put("filerealpath",filerealpath);
        filemap.put("iszip", iszip);
        filemap.put("fileSize", filesize);
        filemap.put("tokenkey",tokenkey);
        filemap.put("aescode",aescode);
        filemap.put("ftpconfigid",ftpconfigid);
        filemap.put("isaesencrypt",isaesencrypt);

        JAVATools.Excetesql(filemap, tablename);


    }
}
