package com.ruoyi.system.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.domain.DataRecord;
import com.ruoyi.system.domain.ReportUrlAndError;
import com.ruoyi.system.domain.ReportUrlAndLinks;
import com.ruoyi.system.mapper.DataRecordMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DataMsgMapper;
import com.ruoyi.system.domain.DataMsg;
import com.ruoyi.system.service.IDataMsgService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
 * 配置信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-09
 */
@Service
public class DataMsgServiceImpl implements IDataMsgService 
{
    @Autowired
    private DataMsgMapper dataMsgMapper;

    @Autowired
    private DataRecordMapper dataRecordMapper;

    /**
     * 查询配置信息
     * 
     * @param id 配置信息主键
     * @return 配置信息
     */
    @Override
    public DataMsg selectDataMsgById(Long id)
    {
        return dataMsgMapper.selectDataMsgById(id);
    }

    /**
     * 查询配置信息列表
     * 
     * @param dataMsg 配置信息
     * @return 配置信息
     */
    @Override
    public List<DataMsg> selectDataMsgList(DataMsg dataMsg)
    {
        return dataMsgMapper.selectDataMsgList(dataMsg);
    }

    /**
     * 新增配置信息
     * 
     * @param dataMsg 配置信息
     * @return 结果
     */
    @Override
    public int insertDataMsg(DataMsg dataMsg)
    {
        return dataMsgMapper.insertDataMsg(dataMsg);
    }

    /**
     * 修改配置信息
     * 
     * @param dataMsg 配置信息
     * @return 结果
     */
    @Override
    public int updateDataMsg(DataMsg dataMsg)
    {
        return dataMsgMapper.updateDataMsg(dataMsg);
    }

    /**
     * 批量删除配置信息
     * 
     * @param ids 需要删除的配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDataMsgByIds(String ids)
    {
        dataRecordMapper.deleteDataRecordByMsgIds(Convert.toStrArray(ids));
        return dataMsgMapper.deleteDataMsgByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除配置信息信息
     * 
     * @param id 配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDataMsgById(Long id)
    {
        return dataMsgMapper.deleteDataMsgById(id);
    }

    @Override
    public Integer dowithsaomaotask(DataMsg dataMsg1) {
        DataMsg dataMsg = dataMsgMapper.selectDataMsgById(dataMsg1.getId());
        File file = new File(dataMsg1.getJsonpath());
        if(file.exists()){
            file.delete();
        }
        ProcessBuilder processBuilder = new ProcessBuilder();
       // processBuilder.command("cmd.exe", "/c", "muffet --format json https://www.tongliao.gov.cn/xwzx/mrtt/202410/t20241008_888632.html > D:\\work\\doc\\temp\\report8.json");
        if("linux".equals(dataMsg1.getOstype())){
            processBuilder.command("bash", "-c", dataMsg.getCommand() + " " + dataMsg.getUrl() + " > " + dataMsg.getJsonpath());
        }else {
            processBuilder.command("cmd.exe", "/c", dataMsg.getCommand() + " " + dataMsg.getUrl() + " > " + dataMsg.getJsonpath());
        }

        try {
            Process process = processBuilder.start();

            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();
            System.out.println("Exited with code: " + exitCode);
            return exitCode;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return 100;
    }

    public Integer insertsaomaodata(DataMsg dataMsg) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            File file = ResourceUtils.getFile(dataMsg.getJsonpath());
            String json = FileUtils.readFileToString(file,"utf-8");
            List<ReportUrlAndLinks> reportUrlAndLinksList = JSON.parseArray(json, ReportUrlAndLinks.class);
            for (int i = 0; i < reportUrlAndLinksList.size(); i++) {
                String url = reportUrlAndLinksList.get(i).getUrl();
                String links = reportUrlAndLinksList.get(i).getLinks();
                String time1 = reportUrlAndLinksList.get(i).getTimestamp();
                List<ReportUrlAndError> reportUrlAndErrorsList = JSON.parseArray(links, ReportUrlAndError.class);
                DataRecord dataRecord1 = new DataRecord();
                dataRecord1.setUrl(url);
                dataRecord1.setLinkurl(url);
                dataRecord1.setMsg("一级页面");
                dataRecord1.setRecord(dataMsg.getId());
                if(!StringUtils.isEmpty(time1)){
                    dataRecord1.setTimestamp(time1);
                }else{
                    dataRecord1.setTimestamp(sdf.format(new Date()));
                }

                List<DataRecord> dataRecordList = new ArrayList<>();
                dataRecordList.add(dataRecord1);
                for(ReportUrlAndError reportUrlAndError : reportUrlAndErrorsList){
                    String error = reportUrlAndError.getError();
                    String inner_url = reportUrlAndError.getUrl();
                    String time = reportUrlAndError.getTimestamp();
                    DataRecord dataRecord = new DataRecord();
                    dataRecord.setUrl(url);
                    dataRecord.setLinkurl(inner_url);
                    dataRecord.setMsg(error);
                    dataRecord.setRecord(dataMsg.getId());
                    dataRecord.setTimestamp(time);
                    dataRecordList.add(dataRecord);
                    //int i1 = dataRecordMapper.insertDataRecord(dataRecord);
                }
                int i1 = dataRecordMapper.batchDataRecord(dataRecordList);

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


}
