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

import com.trs.infra.util.CMyString;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
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 saveBaseUrl,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;
            Connection connectionImg = null;
            Statement statement = null;
            Statement statementImg = null;
            ResultSet set = null;
            ResultSet setImg = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select * from file_ids " +
                               "  where fid='"+manuscriptId+"'  order by sort asc");
                connection = ConnectionUtil.getConnectionBenji("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    String url = set.getString("url");
                    String name = set.getString("name");
                    String file_url = saveBaseUrl  +url;
                    String sUploadFile = updateFile(file_url);
                    if(!CMyString.isEmpty(sUploadFile)) {
                        JSONObject docRelObj = new JSONObject();
                        docRelObj.put("SrcFile", name);
                        docRelObj.put("AppFile", sUploadFile);
                        docRelObj.put("AppDesc", name);
                        docRelObj.put("AppendixId", 0);
                        docRelFileJson.add(docRelObj);
                    }
                }

                StringBuilder sbImg=new StringBuilder(
                        "select * from image_ids " +
                                "  where fid='"+manuscriptId+"'  order by sort asc");
                connectionImg = ConnectionUtil.getConnectionBenji("mysql");
                statementImg = connectionImg.createStatement();
                System.out.println(sbImg);
                setImg = statementImg.executeQuery(sbImg.toString());
                while (setImg.next()){
                    String url = set.getString("url");
                    String name = set.getString("name");
                    String file_url = saveBaseUrl +"" +url;
                    String sUploadFile = updateFile(file_url);
                    if(!CMyString.isEmpty(sUploadFile)) {
                        JSONObject docRelObj = new JSONObject();
                        docRelObj.put("SrcFile", name);
                        docRelObj.put("AppFile", sUploadFile);
                        docRelObj.put("AppDesc", name);
                        docRelObj.put("AppendixId", 0);
                        picRelFileJson.add(docRelObj);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
                ConnectionUtil.closeConnection(setImg, statementImg, connectionImg);
            }
            //默认视图
            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 saveBaseUrl,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;
            Connection connectionImg = null;
            Statement statement = null;
            Statement statementImg = null;
            ResultSet set = null;
            ResultSet setImg = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select * from file_ids " +
                                "  where fid='"+manuscriptId+"'  order by sort asc");
                connection = ConnectionUtil.getConnectionBenji("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    String url = set.getString("url");
                    String name = set.getString("name");
                    String file_url = saveBaseUrl  +url;
                    String sUploadFile = updateFile(file_url);
                    if(!CMyString.isEmpty(sUploadFile)) {
                        JSONObject docRelObj = new JSONObject();
                        docRelObj.put("SrcFile", name);
                        docRelObj.put("AppFile", sUploadFile);
                        docRelObj.put("AppDesc", name);
                        docRelObj.put("AppendixId", 0);
                        docRelFileJson.add(docRelObj);
                    }
                }

                StringBuilder sbImg=new StringBuilder(
                        "select * from image_ids " +
                                "  where fid='"+manuscriptId+"'  order by sort asc");
                connectionImg = ConnectionUtil.getConnectionBenji("mysql");
                statementImg = connectionImg.createStatement();
                System.out.println(sbImg);
                setImg = statementImg.executeQuery(sbImg.toString());
                while (setImg.next()){
                    String url = set.getString("url");
                    String name = set.getString("name");
                    String file_url = saveBaseUrl +"" +url;
                    String sUploadFile = updateFile(file_url);
                    if(!CMyString.isEmpty(sUploadFile)) {
                        JSONObject docRelObj = new JSONObject();
                        docRelObj.put("SrcFile", name);
                        docRelObj.put("AppFile", sUploadFile);
                        docRelObj.put("AppDesc", name);
                        docRelObj.put("AppendixId", 0);
                        picRelFileJson.add(docRelObj);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                return new HashMap<>();
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
                ConnectionUtil.closeConnection(setImg, statementImg, connectionImg);
            }
            //默认视图
            map.put("docRelFile",docRelFileJson.toString());
            map.put("docRelPic",picRelFileJson.toString());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    public String updateFile(String url) throws Exception {
        System.out.println("==============");
        String fileNameRe = null;
        try {
            if(url != null && url != ""){
                System.out.println(url);
                Dispatch dispatch = WCMServiceCaller.UploadFile(url);
                System.out.println("-----------"+dispatch.getUploadShowName());
                fileNameRe = dispatch.getUploadShowName();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return fileNameRe;
    }
    private String getFileTypeText(String text) {
        String suffixes="avi,mpeg,3gp,mp3,mp4,wav,jpeg,gif,jpg,png,apk,exe,pdf,rar,zip,docx,doc,txt,xls,rar,ppt,XLS,RAR,PDF," +
                "AVI,MPEG,3GP,MP3,MP4,WAV,JPEG,GIF,JPG,PNG,APK,EXE,PDF,RAR,ZIP,DOCX,DOC,TXT,XLS,RAR,PPT,XLS,RAR,PDF,PPT,XLSX,xlsx"
                ;
        String[] split = suffixes.split(",");
        String type = null;
        for (String st : split) {
            int i = text.indexOf(st);
            if(i >=0){
                type = st;
            }
        }
        return type;
    }


}
