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

import com.alibaba.fastjson.JSON;
import com.trs.cms.CMSConstants;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.tool.tyjyh.ConfigConstant;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.util.Log_Exception;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class ServiceImplGgzy {
    private String sqlType = ConfigConstant.SQLTYPE;

    public List<Map<String,String>> getNmgGgzy(String channelName,String tableName){
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  nmg.channle = '"+channelName+"' " +
                            " " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }


    public String GetWcmMetaTableGovDocNewsMetaDataIdByOldDocId(String oldDocId,String hyChannelId){
        String id = null;
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        try {
            StringBuilder sb = new StringBuilder(
                    "SELECT MetaDataId FROM wcmmetatablegovdocnews where oldDocid = \""+oldDocId+"\" and CHANNELID = \""+hyChannelId+"\" limit 1"
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionHy("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                Map<String, String> map = new HashMap<>();
                map.put("MetaDataId", set.getString("MetaDataId"));
                oldMaps.add(map);
            }
            if(oldMaps != null && oldMaps.size() >= 1){
                id = oldMaps.get(0).get("MetaDataId");
            }
        } catch (Exception e) {
            e.printStackTrace();
            id = null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
        return id;
    }

    public List<Map<String, String>> getNmgZWGGGgzy(String tableName) {
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        String name1 = "政策法规/公共资源交易/国家级";
        String name2 = "政策法规/公共资源交易/自治区级";
        String name3 = "政策法规/政府采购/国家级";
        String name4 = "政策法规/政府采购/自治区级";
        String name5 = "政策法规/工程建设/国家级";
        String name6 = "政策法规/工程建设/自治区级";
        String name7 = "政策法规/工程建设/自治区级";
        String name8 = "政策法规/土地矿权交易/国家级";
        String name9 = "政策法规/产权交易/国家级";
        String name10 = "政策法规/产权交易/自治区级";
        String name11 = "政策法规/盟市级/国家级";
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  " +
                            "nmg.channle = '"+name1+"'  or  " +
                            "nmg.channle = '"+name2+"'  or  " +
                            "nmg.channle = '"+name3+"'  or  " +
                            "nmg.channle = '"+name4+"'  or  " +
                            "nmg.channle = '"+name5+"'  or  " +
                            "nmg.channle = '"+name6+"'  or  " +
                            "nmg.channle = '"+name7+"'  or  " +
                            "nmg.channle = '"+name8+"'  or  " +
                            "nmg.channle = '"+name9+"'  or  " +
                            "nmg.channle = '"+name10+"'  or  " +
                            "nmg.channle = '"+name11+"'  " +
                            " " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }
    public List<Map<String, String>> getNmgZWGGGgzyTLS(String tableName) {
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        String name1 = "政策法规/综合类/法律法规";
        String name2 = "政策法规/综合类/政策文件";
        String name3 = "政策法规/综合类/中心制度";
        String name4 = "政策法规/工程建设/法律法规";
        String name5 = "政策法规/工程建设/政策文件";
        String name6 = "政策法规/工程建设/中心制度";
        String name7 = "政策法规/政府采购/法律法规";
        String name8 = "政策法规/政府采购/政策文件";
        String name9 = "政策法规/政府采购/中心制度";
        String name10 = "政策法规/土地矿业权/法律法规";
        String name11 = "政策法规/土地矿业权/政策文件";
        String name12 = "政策法规/国有产权/法律法规";
        String name13 = "政策法规/国有产权/政策文件";
        String name14 = "政策法规/惠企政策/法律法规";
        String name15 = "政策法规/惠企政策/政策文件";
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  " +
                            "nmg.channle = '"+name1+"'  or  " +
                            "nmg.channle = '"+name2+"'  or  " +
                            "nmg.channle = '"+name3+"'  or  " +
                            "nmg.channle = '"+name4+"'  or  " +
                            "nmg.channle = '"+name5+"'  or  " +
                            "nmg.channle = '"+name6+"'  or  " +
                            "nmg.channle = '"+name7+"'  or  " +
                            "nmg.channle = '"+name8+"'  or  " +
                            "nmg.channle = '"+name9+"'  or  " +
                            "nmg.channle = '"+name10+"'  or  " +
                            "nmg.channle = '"+name11+"'  or  " +
                            "nmg.channle = '"+name12+"'  or  " +
                            "nmg.channle = '"+name13+"'  or  " +
                            "nmg.channle = '"+name14+"'  or  " +
                            "nmg.channle = '"+name15+"'  " +
                            " " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }
    public List<Map<String, String>> getNmgZWGGGgzyWLCB(String tableName) {
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        String name1 = "政策法规/综合";
        String name2 = "政策法规/政策解读";
        String name3 = "政策法规/政府采购";
        String name4 = "政策法规/工程建设";
        String name5 = "政策法规/土地/矿业权";
        String name6 = "政策法规/国有产权";
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  " +
                            "nmg.channle = '"+name1+"'  or  " +
                            "nmg.channle = '"+name2+"'  or  " +
                            "nmg.channle = '"+name3+"'  or  " +
                            "nmg.channle = '"+name4+"'  or  " +
                            "nmg.channle = '"+name5+"'  or  " +
                            "nmg.channle = '"+name6+"'  " +
                            " " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }
    public List<Map<String, String>> getNmgCFSGgzyWLCB(String tableName) {
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        String name1 = "法律法规/公共资源交易相关法律和政策";
        String name2 = "法律法规/法律";
        String name3 = "法律法规/法规";
        String name4 = "法律法规/规范性文件";
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  " +
                            "nmg.channle = '"+name1+"'  or  " +
                            "nmg.channle = '"+name2+"'  or  " +
                            "nmg.channle = '"+name3+"'  or  " +
                            "nmg.channle = '"+name4+"'  " +
                            " " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }

    public List<Map<String, String>> getNmgGgzyByTime(String time,String channelName, String tableName) {
        List<Map<String, String>> oldMaps = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        try {
            StringBuilder sb=new StringBuilder(
                    "select " +
                            " nmg.*  " +
                            " from  "+tableName+"  nmg" +
                            " where  nmg.channle = '"+channelName+"' " +
                            " and  time > '"+time+"' " +
                            " order by nmg.time asc "
            );
            System.out.println(sb);
            connection = ConnectionUtil.getConnectionBenji("mysql");
            statement = connection.createStatement();
            set = statement.executeQuery(sb.toString());
            while (set.next()){
                System.out.println(set);
                Map<String, String> map = new HashMap<>();
                map.put("id", set.getString("id"));
                map.put("title", set.getString("title"));
                map.put("url", set.getString("url"));
                map.put("time", set.getString("time"));
                map.put("content", set.getString("content"));
                map.put("image_ids", set.getString("image_ids"));
                map.put("file_ids", set.getString("file_ids"));
                map.put("channle", set.getString("channle"));
                oldMaps.add(map);
            }
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB() error"+e.getMessage());
            return null;
        } finally {
            ConnectionUtil.closeConnection(set, statement, connection);
        }
    }
}