package com.trs.web2frame.tool.tyjyh.tonglaozheng;

import com.trs.infra.util.CMyString;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.tool.tyjyh.tongliao.JsoupUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Annex {
    /**
     * 根据DOcID获取文档相关的附件信息
     * @param manuscriptId
     * @return
     */
    public Map<String,Object> getZhiXunFileInfoByDocId(String manuscriptId){
        // 获取文档相关的附件信息
        Map<String,Object> map=new HashMap<>();
        JSONArray picRelFileJson = new JSONArray();
        JSONArray docRelFileJson = new JSONArray();
        try {
            List<Map<String, Object>> oldMaps = new ArrayList<>();
            Connection connection = null;
            Statement statement = null;
            ResultSet set = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select " +
                                " cn.extension," +
                                " cr.TYPE as filetype," +
                                " FS_NAMES_ID," +
                                " cr.title," +
                                " concat(cn.CURRENT_FILE_PATH,\"/\",substring(cn.FS_NAMES_ID,5,29)," +
                                " concat(\".\",cn.extension)) as fileurl " +
                                " from " +
                                " CMS_MANUSCRIPT_RELATED_RES cr" +
                                " left join CMS_FS_NAMES cn on cr.RES_RELATED_ID = cn.RES_ID " +
                               "  where cr.MANUSCRIPT_ID='"+manuscriptId+"'");
                connection = ConnectionUtil.getConnection("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    String file_type = set.getString("filetype");
                    String pic_name = set.getString("title");
                    String file_url = "http://10.21.245.114:81/" + set.getString("fileurl");
                    if(pic_name==null)continue;
                    JsoupUtilsView jsoupUtils = new JsoupUtilsView();
                    String name = null;
                    try {
                        name = set.getString("fileurl").substring(set.getString("fileurl").lastIndexOf("/")+1);
                    }catch (Exception e){
                        name = Math.random()+"";
                    }
                    String sUploadFile = jsoupUtils.updateFileUrl( file_url,name);

                    if(!CMyString.isEmpty(sUploadFile)) {
                        if("PICTURE".equals(file_type)){
                            JSONObject picRelObj = new JSONObject();
                            picRelObj.put("SrcFile", pic_name);
                            picRelObj.put("AppFile", sUploadFile);
                            picRelObj.put("AppDesc", pic_name);
                            picRelObj.put("AppendixId", 0);
                            picRelFileJson.add(picRelObj);
                        }else{
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SrcFile", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docRelFileJson.add(docRelObj);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
            }
            //默认视图
            map.put("docRelFile",docRelFileJson.toString());
            map.put("docRelPic",picRelFileJson.toString());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    public Map<String, Object> getZWGKFileInfoByDocId(String manuscriptId) {
        // 获取文档相关的附件信息
        Map<String,Object> map=new HashMap<>();
        JSONArray picRelFileJson = new JSONArray();
        JSONArray docRelFileJson = new JSONArray();
        JSONArray docPDFRelFileJson = new JSONArray();
        JSONArray docDOCRelFileJson = new JSONArray();
        try {
            Connection connection = null;
            Statement statement = null;
            ResultSet set = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select " +
                                " cn.extension," +
                                " cr.TYPE as filetype," +
                                " FS_NAMES_ID," +
                                " cr.title," +
                                " concat(cn.CURRENT_FILE_PATH,\"/\",substring(cn.FS_NAMES_ID,5,29)," +
                                " concat(\".\",cn.extension)) as fileurl " +
                                " from " +
                                " CMS_MANUSCRIPT_RELATED_RES cr" +
                                " left join CMS_FS_NAMES cn on cr.RES_RELATED_ID = cn.RES_ID " +
                                "  where cr.MANUSCRIPT_ID='"+manuscriptId+"'");
                connection = ConnectionUtil.getConnection("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());

                while (set.next()){
                    String file_type = set.getString("filetype");
                    String pic_name = set.getString("title");
                    String file_url = "http://10.21.245.114:81/" + set.getString("fileurl");
                    if(pic_name==null)continue;
                    JsoupUtilsView jsoupUtils = new JsoupUtilsView();
                    String name = null;
                    try {
                        name = set.getString("fileurl").substring(set.getString("fileurl").lastIndexOf("/")+1);
                    }catch (Exception e){
                        name = Math.random()+"";
                    }
                    String sUploadFile = jsoupUtils.updateFileUrl( file_url,name);
                    System.out.println(sUploadFile);
                    System.out.println(pic_name);
                    if(!CMyString.isEmpty(sUploadFile)) {
                        if("PICTURE".equals(file_type)){
                            JSONObject picRelObj = new JSONObject();
                            picRelObj.put("SRCFILE", pic_name);
                            picRelObj.put("AppFile", sUploadFile);
                            picRelObj.put("AppDesc", pic_name);
                            picRelObj.put("AppendixId", 0);
                            picRelFileJson.add(picRelObj);
                        }else{
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SRCFILE", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docRelFileJson.add(docRelObj);
                        }
                        if (sUploadFile.indexOf(".DOC") > 0 || sUploadFile.indexOf(".doc") > 0){
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SRCFILE", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docDOCRelFileJson.add(docRelObj);
                        }
                        if (sUploadFile.indexOf("下载") > 0 && sUploadFile.indexOf(".DOC") < 0){
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SRCFILE", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docDOCRelFileJson.add(docRelObj);
                        }
                        if (sUploadFile.indexOf(".PDF") > 0 || sUploadFile.indexOf(".pdf") > 0){
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SRCFILE", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docPDFRelFileJson.add(docRelObj);
                        }
                        if (pic_name.indexOf("阅读") > 0 && sUploadFile.indexOf(".PDF") < 0){
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SRCFILE", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docPDFRelFileJson.add(docRelObj);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
            }

            //默认视图
            map.put("DOCATTACHFILEFIELD",docRelFileJson.toString());
            map.put("qzbword",docDOCRelFileJson.toString());
            map.put("wjyl",docPDFRelFileJson.toString());
            map.put("DOCATTACHPICFIELD",docRelFileJson.toString());
            map.put("DOCRELAPPENDIX",docRelFileJson.toString());
            map.put("DOCRELPIC",picRelFileJson.toString());

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    public Map<String,Object> getZFGGFileInfoByDocId(String manuscriptId){
        // 获取文档相关的附件信息
        Map<String,Object> map=new HashMap<>();
        JSONArray picRelFileJson = new JSONArray();
        JSONArray docRelFileJson = new JSONArray();
        try {
            List<Map<String, Object>> oldMaps = new ArrayList<>();
            Connection connection = null;
            Statement statement = null;
            ResultSet set = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select " +
                                " cn.extension," +
                                " cr.TYPE as filetype," +
                                " FS_NAMES_ID," +
                                " cr.title," +
                                " concat(cn.CURRENT_FILE_PATH,\"/\",substring(cn.FS_NAMES_ID,5,29)," +
                                " concat(\".\",cn.extension)) as fileurl " +
                                " from " +
                                " CMS_MANUSCRIPT_RELATED_RES cr" +
                                " left join CMS_FS_NAMES cn on cr.RES_RELATED_ID = cn.RES_ID " +
                                "  where cr.MANUSCRIPT_ID='"+manuscriptId+"'");
                connection = ConnectionUtil.getConnection("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    String file_type = set.getString("filetype");
                    String pic_name = set.getString("title");
                    String file_url = "http://10.21.245.114:81/" + set.getString("fileurl");
                    if(pic_name==null)continue;
                    JsoupUtilsView jsoupUtils = new JsoupUtilsView();
                    String name = null;
                    try {
                        name = set.getString("fileurl").substring(set.getString("fileurl").lastIndexOf("/")+1);
                    }catch (Exception e){
                        name = Math.random()+"";
                    }
                    String sUploadFile = jsoupUtils.updateFileUrl( file_url,name);

                    if(!CMyString.isEmpty(sUploadFile)) {
                        if("PICTURE".equals(file_type)){
                            JSONObject picRelObj = new JSONObject();
                            picRelObj.put("SrcFile", pic_name);
                            picRelObj.put("AppFile", sUploadFile);
                            picRelObj.put("AppDesc", pic_name);
                            picRelObj.put("AppendixId", 0);
                            picRelFileJson.add(picRelObj);
                        }else{
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SrcFile", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docRelFileJson.add(docRelObj);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
            }
            //公报附件
            map.put("GBFJ",docRelFileJson.toString());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    public Map<String,Object> getZFGGQZBFileInfoByDocId(String manuscriptId){
        // 获取文档相关的附件信息
        Map<String,Object> map=new HashMap<>();
        JSONArray picRelFileJson = new JSONArray();
        JSONArray docRelFileJson = new JSONArray();
        try {
            List<Map<String, Object>> oldMaps = new ArrayList<>();
            Connection connection = null;
            Statement statement = null;
            ResultSet set = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select " +
                                " cn.extension," +
                                " cr.TYPE as filetype," +
                                " FS_NAMES_ID," +
                                " cr.title," +
                                " concat(cn.CURRENT_FILE_PATH,\"/\",substring(cn.FS_NAMES_ID,5,29)," +
                                " concat(\".\",cn.extension)) as fileurl " +
                                " from " +
                                " CMS_MANUSCRIPT_RELATED_RES cr" +
                                " left join CMS_FS_NAMES cn on cr.RES_RELATED_ID = cn.RES_ID " +
                                "  where cr.MANUSCRIPT_ID='"+manuscriptId+"'");
                connection = ConnectionUtil.getConnection("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    String file_type = set.getString("filetype");
                    String pic_name = set.getString("title");
                    String file_url = "http://10.21.245.114:81/" + set.getString("fileurl");
                    if(pic_name==null)continue;
                    JsoupUtilsView jsoupUtils = new JsoupUtilsView();
                    String name = null;
                    try {
                        name = set.getString("fileurl").substring(set.getString("fileurl").lastIndexOf("/")+1);
                    }catch (Exception e){
                        name = Math.random()+"";
                    }
                    String sUploadFile = jsoupUtils.updateFileUrl( file_url,name);

                    if(!CMyString.isEmpty(sUploadFile)) {
                        if("PICTURE".equals(file_type)){
                            JSONObject picRelObj = new JSONObject();
                            picRelObj.put("SrcFile", pic_name);
                            picRelObj.put("AppFile", sUploadFile);
                            picRelObj.put("AppDesc", pic_name);
                            picRelObj.put("AppendixId", 0);
                            picRelFileJson.add(picRelObj);
                        }else{
                            JSONObject docRelObj = new JSONObject();
                            docRelObj.put("SrcFile", pic_name);
                            docRelObj.put("AppFile", sUploadFile);
                            docRelObj.put("AppDesc", pic_name);
                            docRelObj.put("AppendixId", 0);
                            docRelFileJson.add(docRelObj);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
            }
            //公报附件
            map.put("qzb",docRelFileJson.toString());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

}
