package com.trs.project.openweb;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.trs.project.entity.*;
import com.trs.project.file.FileReaderResource;
import com.trs.project.highclient.RestHighLevelClientService;
import com.trs.project.service.BusinessInfoService;
import com.trs.project.service.DataCleaningService;
import com.trs.project.service.NewsArticleInfoService;
import com.trs.project.service.RiskArticleInfoService;
import com.trs.project.timer.BusinessFtpTask;
import com.trs.project.timer.BusinessTask;
import com.trs.project.util.ESPrefix;
import com.trs.project.utils.*;
import dm.jdbc.driver.DBError;
import dm.jdbc.driver.DMException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.assertj.core.util.DateUtil;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/sys-business")
@Slf4j
public class TestController {
    @Resource
    private RestHighLevelClient client;
    @Autowired
    private RestHighLevelClientService restHighLevelClientService;

    @Autowired
    private BusinessInfoService businessInfoService;
    @Autowired
    private BusinessTask businessTask;
    @Autowired
    private RiskArticleInfoService riskArticleInfoService;

    @Autowired
    private NewsArticleInfoService newsArticleInfoService;

    @Autowired
    private FileReaderResource fileReaderResource;

    @Autowired
    private SpringSFTPUtils springSFTPUtils;

    @Value("${ftp.fileURL}")
    private String fileUrl_config;

    @Value("${ftp.host}")
    private String host_config;

    @Value("${ftp.port}")
    private int port_config;

    @Value("${ftp.username}")
    private String username_config;

    @Value("${ftp.password}")
    private String password_config;

    @Value("${ftp.targetUrl}")
    private String targetUrl;

    @Autowired
    private DataCleaningService dataCleaningService;

    @Autowired
    private BusinessFtpTask businessFtpTask;

    @Value("${timer.cron116}")
    private String cron116;

    @Value("${timer.cron5000}")
    private String cron5000;

    @Value("${timer.cronFTP}")
    private String cronFTP;

    @GetMapping("listCron")
    public void cron(){
        System.out.println(cron116);
        System.out.println(cron5000);
        System.out.println(cronFTP);
    }

    @GetMapping("listBusiness")
    public List<DemoData> save(){
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        String fileName = "C:\\Users\\ZHENG\\Desktop\\"+ "116个重点企业名单(1).xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行
        List<DemoData> demoDataList = new ArrayList<DemoData>();
        EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
            for (DemoData demoData : dataList) {
                demoDataList.add(demoData);
            }
        })).sheet().doRead();
        for (DemoData demoData : demoDataList) {
            String name = demoData.getName();
            businessTask.saveBusinessInfo(name);
        }
        return demoDataList;
    }
    @GetMapping("listBusinessSave")
    public List<DemoData> listBusinessSave(){
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        String fileName = "C:\\Users\\ZHENG\\Desktop\\"+ "116个重点企业名单(1).xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行
        List<DemoData> demoDataList = new ArrayList<DemoData>();
        EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
            for (DemoData demoData : dataList) {
                demoDataList.add(demoData);
            }
        })).sheet().doRead();
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setName("911506027014433063");
        businessTask.saveBusinessInfo(businessInfo.getName());
        BusinessInfo businessInfo2 = new BusinessInfo();
        businessInfo2.setName("91150300793612583N");
        businessTask.saveBusinessInfo(businessInfo2.getName());
        return demoDataList;
    }
    @GetMapping("listBusinessSaveList")
    public void listBusinessSaveList(){
        for(int i=1;i<=250;i++){
            businessTask.saveBusinessRiskLabelCheckFTP(i);
        }
    }
    @GetMapping("Business116AntOrther")
    public void Business116AntOrther(){
        log.info("========saveBusinessInfo116AndOrther=================");
        businessTask.saveBusinessInfo116AndOrther();
    }
    @GetMapping("BusinessFTPDATETask")
    public void BusinessFTPDATETask(){
        log.info("========BusinessFTPDATETask=================");
        try {
            businessFtpTask.saveOrUpdateFTP();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @GetMapping("Business5000Task")
    public void Business5000Task(){
        log.info("========Business5000Task=================");
        for(int i=1;i<=250;i++){
            businessTask.saveBusinessRiskLabelCheckFTP(i);
        }
    }

    @GetMapping("getListBusinessSaveList")
    public void getListBusinessSaveList(String name){
        QueryWrapper<BusinessInfo> queryWrapper = new QueryWrapper<BusinessInfo>();
        queryWrapper.eq("BUSINESS_TYPE",1);
        List<BusinessInfo> list = businessInfoService.list(queryWrapper);
        for (BusinessInfo businessInfo : list) {
            businessTask.savePunishBiangeng(businessInfo);
        }
    }

    @GetMapping("getBusinessTypeList")
    public void getBusinessTypeList(String name){
        try {
            businessFtpTask.saveOrUpdateFTP();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @GetMapping("getGroupByCount")
    public void getGroupByCount(String name){
        QueryWrapper<BusinessInfo> queryWrapper = new QueryWrapper<BusinessInfo>();
        queryWrapper.eq("BUSINESS_TYPE",2);
        List<BusinessInfo> list = businessInfoService.list(queryWrapper);
        int i = 0;
        for (BusinessInfo businessInfo : list) {
            String name1 = businessInfo.getName();
            QueryWrapper<BusinessInfo> queryWrapperName = new QueryWrapper<BusinessInfo>();
            queryWrapperName.eq("NAME",name1);
            queryWrapperName.eq("BUSINESS_TYPE",3);
            List<BusinessInfo> list1 = businessInfoService.list(queryWrapperName);
            for (BusinessInfo info : list1) {
                if(list1.size() >=1 && info.getBusinessType().equals("3")){
                    businessInfoService.removeById(info.getId());
                    i++;
                }
            }
        }
        System.out.println(i);

    }
    @GetMapping("listFTP")
    public void listFTP() throws Exception {
        try {
            businessFtpTask.saveOrUpdateFTP();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @GetMapping("getListBusinessUpdate")
    public void getListBusinessUpdate(String name){
            for(int i=1;i<=250;i++){
                businessTask.saveBusinessRiskLabel(i);
            }
    }

    @GetMapping("testTowComp")
    public void testTowComp(){
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setName("南京应龙卫星科技有限公司");

        //保存企业基本信息
        Map<String, String> businessInfo_size = businessTask.findBusinessInfo_size(businessInfo.getName());
        log.info(String.valueOf(businessInfo_size));

        Map<String, String> businessInfo_other = businessTask.findBusinessInfo_other(businessInfo.getName());
        log.info(String.valueOf(businessInfo_other));

        Map<String, String> getClusterTag = businessTask.find_getClusterTag(businessInfo.getName());
        log.info(String.valueOf(getClusterTag));

        Map<String, String> industryChain = businessTask.findBusinessInfo_major(businessInfo.getName(), "industryChain");
        log.info(String.valueOf(industryChain));

        Map<String, String> entQuality = businessTask.findBusinessInfo_major(businessInfo.getName(), "entQuality");
        log.info(String.valueOf(entQuality));

        JSONObject data = businessTask.getData(businessInfo.getName(), 225);
        log.info(String.valueOf(data));

        JSONObject data1 = businessTask.getData(businessInfo.getName(), 41);
        log.info(String.valueOf(data1));

        JSONObject data2 = businessTask.getData(businessInfo.getName(), 186);
        log.info(String.valueOf(data2));

        JSONObject data3 = businessTask.getData(businessInfo.getName(), 185);
        log.info(String.valueOf(data3));

        JSONObject data4 = businessTask.getData(businessInfo.getName(), 183);
        log.info(String.valueOf(data4));

        JSONObject data5 = businessTask.getData(businessInfo.getName(), 187);
        log.info(String.valueOf(data5));

        JSONObject data6 = businessTask.getData(businessInfo.getName(), 209);
        log.info(String.valueOf(data6));

        JSONObject data7 = businessTask.getData(businessInfo.getName(), 108);
        log.info(String.valueOf(data7));

        JSONObject data8 = businessTask.getData(businessInfo.getName(), 109);
        log.info(String.valueOf(data8));

        JSONObject data9 = businessTask.getData(businessInfo.getName(), 111);
        log.info(String.valueOf(data9));

        JSONObject data10 = businessTask.getData(businessInfo.getName(), 120);
        log.info(String.valueOf(data10));

        JSONObject data11 = businessTask.getData(businessInfo.getName(), 169);
        log.info(String.valueOf(data11));

        JSONObject data12 = businessTask.getData(businessInfo.getName(), 170);
        log.info(String.valueOf(data12));

        JSONObject data13 = businessTask.getData(businessInfo.getName(), 173);
        log.info(String.valueOf(data13));

        JSONObject data14 = businessTask.getData(businessInfo.getName(), 208);
        log.info(String.valueOf(data14));

        JSONObject data15 = businessTask.getData(businessInfo.getName(), 209);
        log.info(String.valueOf(data15));

        JSONObject data16 = businessTask.getData(businessInfo.getName(), 210);
        log.info(String.valueOf(data16));
    }
    @GetMapping("save116")
    public void save116(){
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setId("d7e3a599-4073-460b-a00d-84ce6489de4f");
        businessInfo.setName("内蒙古德晟实业集团有限公司");
        businessTask.saveJustice(businessInfo,169);//1.	被执行企业 169
        //log.info(businessInfo.toString());
        //保存企业基本信息
       /* businessTask.saveBusinessInfo(businessInfo.getName());
        //saveBusinessInfo(businessInfo);
        //保存对外投资invest_info
        businessTask.saveInvestInfo(businessInfo);
        businessTask.saveMajorShareholders(businessInfo);
        //保存价值分析
        businessTask.saveValueInfo(businessInfo,225);
        //年报
        businessTask.saveReportInfo(businessInfo,41);
        //知识产权
        businessTask.saveIntellectual(businessInfo,186);//1.	软件著作权 186
        businessTask.saveIntellectual(businessInfo,185);//2.	著作权 185
        businessTask.saveIntellectual(businessInfo,183);//3.	专利列表 183
        businessTask.saveIntellectual(businessInfo,187);//4.	域名信息 187
        //处罚
        businessTask.saveJustice(businessInfo,209);
        //重点企业名单和风险标签
        businessTask.savePunish(businessInfo, 108);//1.行政处罚 108
        businessTask.savePunish(businessInfo,109);//2.环保处罚列表 109
        //savePunish(businessInfo,110);//3.	环保处罚详情110
        businessTask.savePunish(businessInfo,111);//4.	动产抵押 111
        //savePunish(businessInfo,116);//5.	股权出质 116
        businessTask.savePunish(businessInfo,120);//6.	经营异常 120
        //savePunishBiangeng(businessInfo);//7.	股权变更 211----
        //savePunish(businessInfo,212);//8.	股权冻结 212------
        businessTask.savePunishWeifa(businessInfo);//9.	严重违法 121
        businessTask.saveJustice(businessInfo,169);//1.	被执行企业 169
        businessTask.saveJustice(businessInfo,170);//2.	失信被执行企业 170
        businessTask.saveJustice(businessInfo,173);//3.	法院公告 173
        businessTask.saveJustice(businessInfo,208);//4.	立案信息 208
        businessTask.saveJustice(businessInfo,209);//5.	整体诉讼 209
        businessTask.saveJustice(businessInfo,210);//6.	行贿违法 210*/
    }

    @GetMapping("readActiveQueue")
    public void readActiveQueue(String message) throws IOException {
        File fileJSON = new File(message);
        String content = FileUtils.readFileToString(fileJSON,"GBK");
        JSONArray array = JSONArray.parseArray(JSON.parseObject(content).get("RECLIST").toString());
        //fileJSON.delete();
        for (Object o : array) {
            log.info(o.toString());
            RiskArticleInfo riskArticleInfo = JSON.parseObject(o.toString(), RiskArticleInfo.class);
            String pubtime = riskArticleInfo.getPubtime();
            pubtime = pubtime.replace("/", "-");
            riskArticleInfo.setPubtime(pubtime);
            log.info(o.toString());
            riskArticleInfoService.saveOrUpdate(riskArticleInfo);
        }
    }

    @GetMapping("readActiveQueueAll")
    public void readActiveQueueAll(String message) throws IOException {
        QueryWrapper<BusinessInfo> queryWrapperBusinessInfo1 = new QueryWrapper<BusinessInfo>();
        queryWrapperBusinessInfo1.eq("BUSINESS_TYPE",1);
        List<BusinessInfo> listBusinessInfo1 = businessInfoService.list(queryWrapperBusinessInfo1);
        QueryWrapper<BusinessInfo> queryWrapperBusinessInfo2 = new QueryWrapper<BusinessInfo>();
        queryWrapperBusinessInfo2.eq("BUSINESS_TYPE",2);
        List<BusinessInfo> listBusinessInfo2 = businessInfoService.list(queryWrapperBusinessInfo2);
        listBusinessInfo1.addAll(listBusinessInfo2);
        for (BusinessInfo businessInfo : listBusinessInfo1) {
            String name = businessInfo.getName();
            System.out.println(name);
        }
    }

    @GetMapping("readActiveQueueAPI")
    public void readActiveQueueAPI(String message) throws IOException {
        ArrayList<String> cityNameList = fileReaderResource.ReaderFile();
        JSONObject jsonObject = JSONObject.parseObject(message);
        Object filelist = jsonObject.get("filelist");
        Object type = jsonObject.get("type");
        String types = type.toString();
        //log.info(jsonObject.toJSONString());
        if(types.equals("4")  || types.equals("10")){
            JSONArray arrayStr = JSONArray.parseArray(filelist.toString());
            for (Object oStr : arrayStr) {
                try {
                    targetUrl = oStr.toString();
                    String fileUrl = fileUrl_config;
                    SFTPUtils sftpUtils  =  new SFTPUtils(host_config, port_config, username_config, password_config);
                    String[] split = targetUrl.split("/");
                    String file = "";
                    for(int i = 0; i < split.length-1;i++){
                        file += split[i]+"/";
                    }
                    String name = split[split.length - 1];
                    sftpUtils.download(file,name,fileUrl_config+name, sftpUtils.sftp);
                    //解压
                    //Java 解压zip
                    String newFilename = ZipUtil.unzipFile(fileUrl+name, fileUrl);
                    log.info("newFilename==="+newFilename);
                    //解析文件对应的JSON数据
                    File fileJSON = new File(fileUrl_config+newFilename);
                    String content = FileUtils.readFileToString(fileJSON,"GBK");
                    JSONArray array = JSONArray.parseArray(JSON.parseObject(content).get("RECLIST").toString());
                    fileJSON.delete();
                    for (Object o : array) {
                        RiskArticleInfo riskArticleInfo = JSON.parseObject(o.toString(), RiskArticleInfo.class);
                        String pubtime = riskArticleInfo.getPubtime();
                        pubtime = pubtime.replace("/", "-");
                        riskArticleInfo.setPubtime(pubtime);
                        riskArticleInfo.setSavetime(LocalDateTime.now());
                        log.info("保存信号信息："+riskArticleInfo.getTitle());
                        riskArticleInfoService.saveOrUpdate(riskArticleInfo);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    continue;
                }

            }
        }
        if(types.equals("1") || types.equals("2") || types.equals("3")){
            JSONArray arrayStr = JSONArray.parseArray(filelist.toString());
            for (Object oStr : arrayStr) {
                try {
                    targetUrl = oStr.toString();
                    String fileUrl = fileUrl_config;
                    SFTPUtils sftpUtils  =  new SFTPUtils(host_config, port_config, username_config, password_config);
                    String[] split = targetUrl.split("/");
                    String file = "";
                    for(int i = 0; i < split.length-1;i++){
                        file += split[i]+"/";
                    }
                    String name = split[split.length - 1];
                    sftpUtils.download(file,name,fileUrl_config+name, sftpUtils.sftp);
                    //解压
                    //Java 解压zip
                    String newFilename = ZipUtil.unzipFile(fileUrl+name, fileUrl);
                    //解析文件对应的JSON数据
                    File fileJSON = new File(fileUrl_config+newFilename);
                    String content = FileUtils.readFileToString(fileJSON,"GBK");
                    JSONArray array = JSONArray.parseArray(JSON.parseObject(content).get("RECLIST").toString());
                    fileJSON.delete();
                    for (Object o : array) {
                        DataCleaning dataCleaning = JSON.parseObject(o.toString(), DataCleaning.class);
                        Object trsCust = JSON.parseObject(o.toString()).get("trsCust");
                        Object result = JSON.parseObject(trsCust.toString()).get("result");
                        JSONArray array1 = JSONArray.parseArray(result.toString());
                        dataCleaning.setSavetime(LocalDateTime.now());
                        for (Object o1 : array1) {
                            dataCleaning.setResultstr(o1.toString());
                            if(types.equals("1")){
                                dataCleaning.setSource("新闻");
                            }
                            if(types.equals("2")){
                                dataCleaning.setSource("微信");
                            }
                            if(types.equals("3")){
                                dataCleaning.setSource("微博");
                            }
                            dataCleaningService.saveOrUpdate(dataCleaning);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        //清洗程序,企业全称
        QueryWrapper<BusinessInfo> queryWrapperBusinessInfo1 = new QueryWrapper<BusinessInfo>();
        queryWrapperBusinessInfo1.eq("BUSINESS_TYPE",1);
        List<BusinessInfo> listBusinessInfo1 = businessInfoService.list(queryWrapperBusinessInfo1);
        QueryWrapper<BusinessInfo> queryWrapperBusinessInfo2 = new QueryWrapper<BusinessInfo>();
        queryWrapperBusinessInfo2.eq("BUSINESS_TYPE",2);
        List<BusinessInfo> listBusinessInfo2 = businessInfoService.list(queryWrapperBusinessInfo2);
        listBusinessInfo1.addAll(listBusinessInfo2);
        for (BusinessInfo businessInfo : listBusinessInfo1) {
            QueryWrapper<DataCleaning> queryWrapperDataCleaning = new QueryWrapper<DataCleaning>();
            queryWrapperDataCleaning.like("TITLE",businessInfo.getName());
            queryWrapperDataCleaning.or().like("CONTENT",businessInfo.getName());
            List<DataCleaning> list = dataCleaningService.list(queryWrapperDataCleaning);
            for (DataCleaning cleaning : list) {
                NewsArticleInfo newsArticleInfo = new NewsArticleInfo(cleaning);
                newsArticleInfo.setResultstr(businessInfo.getName());
                try {
                    newsArticleInfoService.saveOrUpdate(newsArticleInfo);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        //企业简称/地域清洗程序
        //地区名称
        ArrayList<String> arrayList = fileReaderResource.ReaderFile();
        //企业简称
        Map<String, String> enterpriseArrayALL = getEnterpriseArrayALL();
        Iterator<Map.Entry<String, String>> iterator = enterpriseArrayALL.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            for (String city : arrayList) {
                List<DataCleaning> list = dataCleaningService.list();
                for (DataCleaning cleaning : list) {
                    String title = cleaning.getTitle();
                    String content = cleaning.getContent();
                    boolean containsCityTitle = title.contains(city);
                    boolean containsValueTitle = title.contains(entry.getValue());
                    boolean containsCitycontent =  content.contains(city);
                    boolean containsValueContent = content.contains(entry.getValue());
                    if( (containsCityTitle && containsValueTitle) || (containsCitycontent && containsValueContent)){
                        NewsArticleInfo newsArticleInfo = new NewsArticleInfo(cleaning);
                        newsArticleInfo.setResultstr(entry.getKey());
                        try {
                            log.info("entry.getKey()=="+entry.getKey());
                            log.info("getUrl=="+newsArticleInfo.getUrl());
                            log.info(newsArticleInfo.getTitle());
                            newsArticleInfoService.saveOrUpdate(newsArticleInfo);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        QueryWrapper<DataCleaning> deleteAll  = new QueryWrapper<DataCleaning>();
        dataCleaningService.remove(deleteAll);
    }
    @GetMapping("TrsFile")
    public String TrsFile(String url){
        List<String> fileNames = FileArrayUtils.getFileNames(url);
        System.out.println("fileNames.size()==="+fileNames.size());
        int i = 0;
        for (String fileName : fileNames) {
            log.info("fileName====="+fileName);
            List<Map<String,Object>> list = TRSParser.readFile(
                    FileUtil.newFile(url+"/"+fileName),"GBK");
            String fileTrueName = fileName.split("\\.")[0];
            //String fileTrueName = fileName.split(";.")[0];
            System.out.println("list.size()==="+list.size());
            for (Map<String, Object> map : list) {
                Object SY_KEYWORDS = map.get("SY_KEYWORDS");//关键字
                Object IR_GROUPNAME = map.get("IR_GROUPNAME");//分组名称 国内新闻 国内论坛 国内博客 APP
                Object IR_SITENAME = map.get("IR_SITENAME");//站点
                Object IR_AUTHORS = map.get("IR_AUTHORS");//作者
                Object IR_URLNAME = map.get("IR_URLNAME");//文章链接
                Object IR_ABSTRACT = map.get("IR_ABSTRACT");//摘要
                Object IR_URLTITLE = map.get("IR_URLTITLE");//标题
                Object IR_URLTIME = map.get("IR_URLTIME");//路径创建时间
                Object IR_CONTENT = map.get("IR_CONTENT");//内容
                NewsArticleInfo riskArticleInfo = new NewsArticleInfo();
                try {
                    String time = IR_URLTIME+"".replaceAll("/","-");
                    riskArticleInfo.setUrldate(time);
                    riskArticleInfo.setPubtime(time);
                    riskArticleInfo.setSource(IR_GROUPNAME+"");
                    riskArticleInfo.setContent(IR_CONTENT+"");
                    riskArticleInfo.setTitle(IR_URLTITLE+"");
                    riskArticleInfo.setIrsrcname(IR_AUTHORS+"");
                    riskArticleInfo.setResultstr(fileTrueName+"");
                    riskArticleInfo.setUrl(IR_URLNAME+"");
                    riskArticleInfo.setSitename(IR_SITENAME+"");
                    riskArticleInfo.setChannel(IR_AUTHORS+"");
                    riskArticleInfo.setIrauthors(IR_AUTHORS+"");
                    riskArticleInfo.setGroupname(IR_GROUPNAME+"");
                    riskArticleInfo.setTrskeyworks(SY_KEYWORDS+"");
                    riskArticleInfo.setTrsabstract(IR_ABSTRACT+"");
                    riskArticleInfo.setSavetime(LocalDateTime.now());
                    newsArticleInfoService.saveOrUpdate(riskArticleInfo);
                    i++;
                }catch (Exception e){
                    String localizedMessage = e.getLocalizedMessage();
                    //log.info(localizedMessage);
                    int indexOf = localizedMessage.indexOf("网络通信异常");
                    int localize = localizedMessage.indexOf("wwww");
                    //log.info("=====indexOf=========="+indexOf);
                    //log.info("======localize========="+localize);
                   // log.info("===============");
                    e.printStackTrace();
                }
            }
            File fileJSON = new File(url+"/"+fileName);
            fileJSON.delete();
        }
        return "";
    }
    @GetMapping("TrsFile22")
    public String TrsFile22(String url){
        List<String> fileNames = FileArrayUtils.getFileNames(url);
        System.out.println("fileNames.size()==="+fileNames.size());
        int i = 0;
        for (String fileName : fileNames) {
            log.info("fileName====="+fileName);
            List<Map<String,Object>> list = TRSParser.readFile(
                    FileUtil.newFile(url+"/"+fileName),"GBK");
            //String fileTrueName = fileName.split("\\.")[0];
            String fileTrueName = fileName.split(";\\.")[0];
            System.out.println("list.size()==="+list.size());
            for (Map<String, Object> map : list) {
                Object SY_KEYWORDS = map.get("SY_KEYWORDS");//关键字
                Object IR_GROUPNAME = map.get("IR_GROUPNAME");//分组名称 国内新闻 国内论坛 国内博客 APP
                Object IR_SITENAME = map.get("IR_SITENAME");//站点
                Object IR_AUTHORS = map.get("IR_AUTHORS");//作者
                Object IR_URLNAME = map.get("IR_URLNAME");//文章链接
                Object IR_ABSTRACT = map.get("IR_ABSTRACT");//摘要
                Object IR_URLTITLE = map.get("IR_URLTITLE");//标题
                Object IR_URLTIME = map.get("IR_URLTIME");//路径创建时间
                Object IR_CONTENT = map.get("IR_CONTENT");//内容
                NewsArticleInfo riskArticleInfo = new NewsArticleInfo();
                try {
                    String time = IR_URLTIME+"".replaceAll("/","-");
                    riskArticleInfo.setUrldate(time);
                    riskArticleInfo.setPubtime(time);
                    riskArticleInfo.setSource(IR_GROUPNAME+"");
                    riskArticleInfo.setContent(IR_CONTENT+"");
                    riskArticleInfo.setTitle(IR_URLTITLE+"");
                    riskArticleInfo.setIrsrcname(IR_AUTHORS+"");
                    riskArticleInfo.setResultstr(fileTrueName+"");
                    riskArticleInfo.setUrl(IR_URLNAME+"");
                    riskArticleInfo.setSitename(IR_SITENAME+"");
                    riskArticleInfo.setChannel(IR_AUTHORS+"");
                    riskArticleInfo.setIrauthors(IR_AUTHORS+"");
                    riskArticleInfo.setGroupname(IR_GROUPNAME+"");
                    riskArticleInfo.setTrskeyworks(SY_KEYWORDS+"");
                    riskArticleInfo.setTrsabstract(IR_ABSTRACT+"");
                    riskArticleInfo.setSavetime(LocalDateTime.now());
                    newsArticleInfoService.saveOrUpdate(riskArticleInfo);
                    i++;
                }catch (Exception e){
                    String localizedMessage = e.getLocalizedMessage();
                    //log.info(localizedMessage);
                    int indexOf = localizedMessage.indexOf("网络通信异常");
                    int localize = localizedMessage.indexOf("wwww");
                    log.info("=====indexOf=========="+indexOf);
                    log.info("======localize========="+localize);
                    log.info("===============");
                    e.printStackTrace();
                }
            }
            File fileJSON = new File(url+"/"+fileName);
            fileJSON.delete();
        }
        return "";
    }

    @GetMapping("TrsFile2")
    public String TrsFile2(String url) throws IOException {
        ArrayList<String> arrayList = fileReaderResource.ReaderFile();
        String companyList[] = {"内蒙古伊利实业集团股份有限公司","内蒙古伊利集团","伊利集团","伊利公司","伊利"};
        String companyStr  = "内蒙古伊利实业集团股份有限公司";
        List<String> fileNames = FileArrayUtils.getFileNames(url);
        System.out.println("fileNames.size()==="+fileNames.size());
        int i = 0;
        for (String fileName : fileNames) {
            log.info("fileName====="+fileName);
            List<Map<String,Object>> list = TRSParser.readFile(
                    FileUtil.newFile(url+"/"+fileName),"GBK");
            //String fileTrueName = fileName.split("\\.")[0];
            String fileTrueName = fileName.split(";\\.")[0];
            System.out.println("list.size()==="+list.size());
            for (Map<String, Object> map : list) {
                Object SY_KEYWORDS = map.get("SY_KEYWORDS");//关键字
                Object IR_GROUPNAME = map.get("IR_GROUPNAME");//分组名称 国内新闻 国内论坛 国内博客 APP
                Object IR_SITENAME = map.get("IR_SITENAME");//站点
                Object IR_AUTHORS = map.get("IR_AUTHORS");//作者
                Object IR_URLNAME = map.get("IR_URLNAME");//文章链接
                Object IR_ABSTRACT = map.get("IR_ABSTRACT");//摘要
                Object IR_URLTITLE = map.get("IR_URLTITLE");//标题
                Object IR_URLTIME = map.get("IR_URLTIME");//路径创建时间
                Object IR_CONTENT = map.get("IR_CONTENT");//内容
                boolean containsCityNameCompanyStr = IR_URLTITLE.toString().contains(companyStr);
                boolean containsCompanyStr = IR_URLTITLE.toString().contains(companyStr);
                boolean contentCityNameCompanyStr = IR_CONTENT.toString().contains(companyStr);
                boolean contentCompanyStr = IR_CONTENT.toString().contains(companyStr);
                boolean flag1 = false;
                if((containsCityNameCompanyStr && containsCompanyStr) || (contentCityNameCompanyStr && contentCompanyStr)){
                    flag1 = true;
                }
                boolean flag2 = false;
                for (String city : arrayList) {
                    for (String company : companyList) {
                        boolean containsCityNameCompany = IR_URLTITLE.toString().contains(city);
                        boolean containsCompany = IR_URLTITLE.toString().contains(company);
                        boolean contentCityNameCompany = IR_CONTENT.toString().contains(city);
                        boolean contentCompany = IR_CONTENT.toString().contains(company);
                        flag2 = (containsCityNameCompany && containsCompany) || (contentCityNameCompany && contentCompany);
                    }
                }
                if(flag1 || flag2){
                    NewsArticleInfo riskArticleInfo = new NewsArticleInfo();
                    try {
                        String time = IR_URLTIME+"".replaceAll("/","-");
                        riskArticleInfo.setUrldate(time);
                        riskArticleInfo.setPubtime(time);
                        riskArticleInfo.setSource(IR_GROUPNAME+"");
                        riskArticleInfo.setContent(IR_CONTENT+"");
                        riskArticleInfo.setTitle(IR_URLTITLE+"");
                        riskArticleInfo.setIrsrcname(IR_AUTHORS+"");
                        riskArticleInfo.setResultstr(fileTrueName+"");
                        riskArticleInfo.setUrl(IR_URLNAME+"");
                        riskArticleInfo.setSitename(IR_SITENAME+"");
                        riskArticleInfo.setChannel(IR_AUTHORS+"");
                        riskArticleInfo.setIrauthors(IR_AUTHORS+"");
                        riskArticleInfo.setGroupname(IR_GROUPNAME+"");
                        riskArticleInfo.setTrskeyworks(SY_KEYWORDS+"");
                        riskArticleInfo.setTrsabstract(IR_ABSTRACT+"");
                        riskArticleInfo.setSavetime(LocalDateTime.now());
                        newsArticleInfoService.saveOrUpdate(riskArticleInfo);
                        i++;
                    }catch (Exception e){
                        String localizedMessage = e.getLocalizedMessage();
                        //log.info(localizedMessage);
                        e.printStackTrace();
                    }
                }
            }
            File fileJSON = new File(url+"/"+fileName);
            fileJSON.delete();
        }
        return "";
    }

    @GetMapping("outFile")
    public void outFile() throws IOException {
        ArrayList<String> arrayList = fileReaderResource.ReaderFile();
        String companyList[] = {"内蒙古伊利实业集团股份有限公司","内蒙古伊利集团","伊利集团","伊利公司","伊利"};
        for (String city : arrayList) {
            for (String company : companyList) {
                String cityNameCompany = city + company;
            }
        }
    }

    public String[] getEnterpriseArray(){
        String enterpriseArrayYl[] = {
                "伊利集团","伊利公司","伊利乳液",
                "伊利","鄂尔多斯资源","鄂尔多斯资源股份",
                "天首科技","天首集团","天首","华资实业",
                "华资","平庄能源","平庄","吉隆黄金","吉隆","兴业矿业","兴业",
                "亿利洁能","亿利","蒙电华能","蒙电","蒙电华能热电",
                "金宇生物","金宇","金宇生物技术","银泰","银泰黄金",
                "君正能源化工","君正能源","君正","化工","君正能源化工集团","君正",
                "第一机械","第一机械集团","北方重型汽车",
                "北方汽车","明天科技","明天","西水创业",
                "西水","远兴","远兴能源","兰太","兰太实业",
                "包钢钢联","包钢","包钢集团","北方稀土(集团)","北方稀土集团","北方稀土",
                "金河生物","金河","霍林河露天煤业","霍林河露天","露天煤业",
                "蒙草生态环境(集团)","蒙草生态环境集团","蒙草生态环境","蒙草生态","蒙草",
                "东宝生物","东宝生物技术","东宝","福瑞医疗","福瑞医疗科技","福瑞"};
        return enterpriseArrayYl;
    }

    public Map<String,String> getEnterpriseArrayALL(){

        Map<String,String> hashMap = new HashMap<String,String>();
        hashMap.put("内蒙古包钢钢联股份有限公司","包钢");
        hashMap.put("内蒙古君正能源化工集团股份有限公司","君正");
        hashMap.put("内蒙古君正能源化工集团股份有限公司","君正能源");
        hashMap.put("内蒙古伊利实业集团股份有限公司","伊利");
        hashMap.put("中国北方稀土（集团）高科技股份有限公司","北方稀土");
        hashMap.put("亿利洁能股份有限公司","亿利");
        hashMap.put("亿利洁能股份有限公司","亿利洁能");
        hashMap.put("银泰黄金股份有限公司","银泰");
        hashMap.put("银泰黄金股份有限公司","银泰黄金");
        hashMap.put("内蒙古电投能源股份有限公司","电投能源");
        hashMap.put("内蒙古电投能源股份有限公司","电投");
        hashMap.put("内蒙古兴业矿业股份有限公司","兴业");
        hashMap.put("内蒙古兴业矿业股份有限公司","兴业矿业");
        hashMap.put("内蒙古第一机械集团股份有限公司","内蒙古第一机械集团");
        hashMap.put("内蒙古第一机械集团股份有限公司","第一机械");
        hashMap.put("蒙草生态环境（集团）股份有限公司","蒙草");
        hashMap.put("内蒙古大中矿业股份有限公司","大中矿业");
        hashMap.put("内蒙古大中矿业股份有限公司","矿业");
        hashMap.put("内蒙古大中矿业股份有限公司","内蒙古大中矿业");
        hashMap.put("内蒙古鄂尔多斯资源股份有限公司","内蒙古鄂尔多斯资源");
        hashMap.put("内蒙古鄂尔多斯资源股份有限公司","鄂尔多斯资源");
        hashMap.put("金宇生物技术股份有限公司","金宇生物");
        hashMap.put("金宇生物技术股份有限公司","金宇生物技术");
        hashMap.put("金河生物科技股份有限公司","金河生物");
        hashMap.put("金河生物科技股份有限公司","金河生物科技");
        hashMap.put("包头东宝生物技术股份有限公司","东宝生物");
        hashMap.put("包头东宝生物技术股份有限公司","包头东宝生物");
        hashMap.put("内蒙古新华发行集团股份有限公司","新华发行");
        hashMap.put("内蒙古福瑞医疗科技股份有限公司","福瑞");
        hashMap.put("内蒙古福瑞医疗科技股份有限公司","福瑞医疗");
        hashMap.put("内蒙古大唐药业股份有限公司","大唐药业");
        hashMap.put("内蒙古北方重型汽车股份有限公司","北方重型");
        return hashMap;
    }
}
