package com.trs.project.exchange;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.trs.project.entity.DataRecord;
import com.trs.project.entity.DataRecordGwyxx;
import com.trs.project.entity.DataRecordTt;
import com.trs.project.entity.GovDocVo;
import com.trs.project.mapper.DataRecordMapper;
import com.trs.project.service.DataRecordGwyxxService;
import com.trs.project.service.DataRecordService;
import com.trs.project.service.DataRecordTtService;
import com.trs.project.util.hycloud.Dispatch;
import com.trs.project.util.hycloud.WCMServiceCaller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Slf4j
public class CrawlingTask {


    @Autowired
    private DataRecordService dataRecordService;
    @Autowired
    private DataRecordTtService dataRecordTtService;
    @Autowired
    private DataRecordGwyxxService dataRecordGwyxxService;
    @Autowired
    private RestTemplate restTemplate;




    @Scheduled(cron = "0 0/3 * * * ? ")
    public void task_zzqyw() throws ParseException {
        log.info("***********************开始增量爬取任务***********************");
        String url = "http://10.8.3.137:7409/"; // 爬取地址

        // 1. 先获取原始字符串响应（避免直接解析成JSONObject）
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        String responseBody = responseEntity.getBody();

        // 2. 检查并移除BOM头（如果存在）
        if (responseBody != null && responseBody.startsWith("\uFEFF")) {
            responseBody = responseBody.substring(1);
        }

        // 3. 手动解析JSON
        JSONObject response = JSONObject.parseObject(responseBody);
//        System.out.println("response=" + response);

        // 4. 提取数据
        JSONArray newsList_tt = response.getJSONArray("newsList_tt");
        JSONArray newsList_qgyw = response.getJSONArray("newsList_qgyw");
        JSONArray newsList_jrgz = response.getJSONArray("newsList_jrgz");

        List<GovDocVo> listTtItems = JSONObject.parseArray(newsList_tt.toJSONString(), GovDocVo.class);
        List<GovDocVo> listQgywItems = JSONObject.parseArray(newsList_qgyw.toJSONString(), GovDocVo.class);
        List<GovDocVo> listJrgzItems = JSONObject.parseArray(newsList_jrgz.toJSONString(), GovDocVo.class);
        Collections.reverse(listTtItems); // 原地倒序
        Collections.reverse(listQgywItems); // 原地倒序
        Collections.reverse(listJrgzItems); // 原地倒序
        saveTtList(listTtItems);
        saveQgywTtList(listQgywItems);
        saveJrgzTtList(listJrgzItems);
        log.info("***********************爬取任务结束***********************");
    }

    private void saveJrgzTtList(List<GovDocVo> listItems) throws ParseException {
        for (int i=0; i<listItems.size(); i++) {
            LambdaQueryWrapper<DataRecord> wrapper = new LambdaQueryWrapper<>();
            String md5Hex = DigestUtil.md5Hex(listItems.get(i).getUrl());
            wrapper.eq(DataRecord::getUrl, md5Hex);
            DataRecord selectOne = dataRecordService.getOne(wrapper);
            if (selectOne != null) {
                log.info("当前稿件已爬取，跳过：{}",listItems.get(i).getTitle());
                continue;
            }

            Map<String, Object> oPostData = new HashMap<>();
            //标题
            oPostData.put("DocTitle", listItems.get(i).getTitle());
            //来源
            oPostData.put("DOCSOURCENAME", "内蒙古自治区人民政府");
            //撰写时间
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            oPostData.put("DOCRELTIME", sdf2.format( sdf.parse(listItems.get(i).getTime()) ));
            //必要参数   标准文档类型数据
            oPostData.put("DocType", 30);
            oPostData.put("DocLink", listItems.get(i).getUrl());

            String sServiceId = "gov_webdocument";// 必填，固定参数
            String sMethodName = "saveDocumentInWeb";// 必填，固定参数
            oPostData.put("CurrUserName", "dev");// 必填，固定参数
            oPostData.put("ObjectId", "0");// 必填，固定参数
            oPostData.put("ChannelId", "3374");  //目标栏目
            oPostData.put("serviceid", sServiceId);
            oPostData.put("methodname", sMethodName);
            try {
                Dispatch result = WCMServiceCaller.Call(sServiceId, sMethodName, oPostData, true);
                JSONObject rObject = JSON.parseObject(result.getResponseText());
                if (rObject.get("ISSUCCESS").toString().equals("true")) {
                    //把文章链接保存到数据库
                    DataRecord record = new DataRecord();
                    record.setUrl(DigestUtil.md5Hex(listItems.get(i).getUrl()));
                    dataRecordService.saveOrUpdate(record);
                }else {
                    log.error("推送文档失败：{}",result.getResponseText());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("***********************爬取任务结束***********************");
    }

    private void saveQgywTtList(List<GovDocVo> listItems) {
        //        // 倒叙循环
        for (int i=0; i<listItems.size(); i++) {
            LambdaQueryWrapper<DataRecord> wrapper = new LambdaQueryWrapper<>();
            String md5Hex = DigestUtil.md5Hex(listItems.get(i).getUrl());
            wrapper.eq(DataRecord::getUrl, md5Hex);
            DataRecord selectOne = dataRecordService.getOne(wrapper);
            if (selectOne != null) {
                log.info("当前稿件已爬取，跳过：{}",listItems.get(i).getTitle());
                continue;
            }

            Map<String, Object> oPostData = new HashMap<>();
            //标题
            oPostData.put("DocTitle", listItems.get(i).getTitle());
            //来源
            //oPostData.put("DOCSOURCENAME", extractedText);
            oPostData.put("DOCSOURCENAME", "中国政府网");
            //撰写时间
            oPostData.put("DOCRELTIME", listItems.get(i).getTime());
            //必要参数   标准文档类型数据
            oPostData.put("DocType", 30);
            oPostData.put("DocLink", listItems.get(i).getUrl());

            String sServiceId = "gov_webdocument";// 必填，固定参数
            String sMethodName = "saveDocumentInWeb";// 必填，固定参数
            oPostData.put("CurrUserName", "dev");// 必填，固定参数
            oPostData.put("ObjectId", "0");// 必填，固定参数
            oPostData.put("ChannelId", "3375");  //目标栏目
            oPostData.put("serviceid", sServiceId);
            oPostData.put("methodname", sMethodName);
            try {
                Dispatch result = WCMServiceCaller.Call(sServiceId, sMethodName, oPostData, true);
                JSONObject rObject = JSON.parseObject(result.getResponseText());
                if (rObject.get("ISSUCCESS").toString().equals("true")) {
                    //把文章链接保存到数据库
                    DataRecord record = new DataRecord();
                    record.setUrl(DigestUtil.md5Hex(listItems.get(i).getUrl()));
                    dataRecordService.saveOrUpdate(record);
                }else {
                    log.error("推送文档失败：{}",result.getResponseText());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void saveTtList(List<GovDocVo> listTtItems) {
        GovDocVo govDocVo = listTtItems.get(0);
        LambdaQueryWrapper<DataRecordTt> wrapper = new LambdaQueryWrapper<>();
        String md5Hex = DigestUtil.md5Hex(govDocVo.getUrl());
        wrapper.eq(DataRecordTt::getUrl, md5Hex);
        DataRecordTt selectOne = dataRecordTtService.getOne(wrapper);
        if (selectOne != null) {
            log.info("当前稿件已爬取，跳过：{}",govDocVo.getTitle());
            return;
        }

        Map<String, Object> oPostData = new HashMap<>();
        //标题
        oPostData.put("DocTitle", govDocVo.getTitle());
        //来源
        oPostData.put("DOCSOURCENAME", "中国政府网");
        //撰写时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        oPostData.put("DOCRELTIME", sdf.format(new Date()));
        //必要参数   标准文档类型数据
        oPostData.put("DocType", 30);
        oPostData.put("DocLink", govDocVo.getUrl());

        String sServiceId = "gov_webdocument";// 必填，固定参数
        String sMethodName = "saveDocumentInWeb";// 必填，固定参数
        oPostData.put("CurrUserName", "dev");// 必填，固定参数
        oPostData.put("ObjectId", "0");// 必填，固定参数
        oPostData.put("ChannelId", "3370");  //目标栏目
        oPostData.put("serviceid", sServiceId);
        oPostData.put("methodname", sMethodName);
        try {
            Dispatch result = WCMServiceCaller.Call(sServiceId, sMethodName, oPostData, true);
            JSONObject rObject = JSON.parseObject(result.getResponseText());
            if (rObject.get("ISSUCCESS").toString().equals("true")) {
                //把文章链接保存到数据库
                DataRecordTt record = new DataRecordTt();
                record.setUrl(DigestUtil.md5Hex(govDocVo.getUrl()));
                dataRecordTtService.saveOrUpdate(record);
            }else {
                log.error("推送文档失败：{}",result.getResponseText());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
