package com.njcn.filesync.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.device.mapper.PqDeviceMapper;
import com.njcn.device.mapper.RmpEventDetailMapper;
import com.njcn.device.pojo.po.PqDevice;
import com.njcn.device.pojo.po.RmpEventDetailPO;
import com.njcn.device.service.PqDeviceService;
import com.njcn.filesync.config.ServerConfig;
import com.njcn.filesync.config.SourceConfig;
import com.njcn.filesync.config.TargetConfig;
import com.njcn.filesync.service.FileSyncService;
import com.njcn.filesync.sftp.IncrementalFileSync;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2025/10/20  下午 3:03【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FileSyncServiceImpl implements FileSyncService {

    private final SourceConfig sourceConfig;
    private final TargetConfig targetConfig;
    private final PqDeviceMapper pqDeviceMapper;
    private final RmpEventDetailMapper rmpEventDetailMapper;
    @Override
    public boolean triggerCrossSync() {
        log.info("begintriggerCrossSync"+ LocalDateTime.now());

        ServerConfig source = new ServerConfig();
        ServerConfig  target = new ServerConfig();
        BeanUtils.copyProperties(sourceConfig,source);
        BeanUtils.copyProperties(targetConfig,target);
        List<PqDevice> list =pqDeviceMapper.selectList(null);
        if(!CollectionUtils.isEmpty(list)){

            List<String> ipList = list.stream().map(PqDevice::getIp).collect(Collectors.toList());
            System.out.println("ipList"+ipList);
            IncrementalFileSync fileSync = new IncrementalFileSync(
                    source, target, "sync-state.json",ipList,new ArrayList<>());
            // 同步文件
            fileSync.syncOnce();
        }
        log.info("endtriggerCrossSync"+ LocalDateTime.now());

        return true;

    }

    @Override
    public void EventWavePathSYnc(String startTime,String endTime ) {

        List<RmpEventDetailPO> detailsOfTransientEvents = rmpEventDetailMapper.selectList(new LambdaQueryWrapper<RmpEventDetailPO>().between(RmpEventDetailPO::getStartTime,startTime,endTime));
        List<String> pathList = new ArrayList<>();
        for (RmpEventDetailPO detailsOfTransientEvent : detailsOfTransientEvents) {
            String lineId = detailsOfTransientEvent.getLineId();
            PqDevice pqDevice = pqDeviceMapper.selectByLineId(lineId);
            if(Objects.nonNull(detailsOfTransientEvent.getWavePath())){
                String path = pqDevice.getIp()+"/"+detailsOfTransientEvent.getWavePath()+".CFG";
                pathList.add(path);
                String path2 = pqDevice.getIp()+"/"+detailsOfTransientEvent.getWavePath()+".DAT";
                pathList.add(path2);
                String path3 = pqDevice.getIp()+"/"+detailsOfTransientEvent.getWavePath()+".cfg";
                pathList.add(path3);
                String path4= pqDevice.getIp()+"/"+detailsOfTransientEvent.getWavePath()+".dat";
                pathList.add(path4);

            }
        }
        ServerConfig source = new ServerConfig();
        ServerConfig  target = new ServerConfig();
        BeanUtils.copyProperties(sourceConfig,source);
        BeanUtils.copyProperties(targetConfig,target);
        IncrementalFileSync fileSync = new IncrementalFileSync(
                source, target, "sync-state.json",new ArrayList<>(),pathList);
        // 同步文件
        fileSync.syncOnceByPathList();
    }
}
