package com.youlai.boot.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlai.boot.common.constant.SystemConstants;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.module.file.service.FileService;
import com.youlai.boot.system.model.entity.DirectoryNode;
import com.youlai.boot.system.model.entity.DirectoryTree;
import com.youlai.boot.system.model.entity.FolderNode;
import com.youlai.boot.system.model.entity.Record;
import com.youlai.boot.system.service.ConfigService;
import com.youlai.boot.system.service.RecordService;
import io.swagger.v3.core.util.Json;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.List;
// 公网服务器 接受局域网服务器的消息，要做的事情....
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(("/sync"))
public class SyncController {





    @PostMapping("/folder")
    public Result<Void> syncFolder(@RequestBody DirectoryNode directoryNode) {

        log.info("开始同步文件夹............{}", JSON.toJSONString(directoryNode));

        createDirectories(directoryNode);

        return Result.success();

    }

    private void createDirectories(DirectoryNode node) {
        if (node == null) {
            return;
        }

        // 创建当前文件夹
        System.setProperty("file.encoding", "UTF-8");
        File directory = new File(node.getAbsolutePath());
        if (!directory.exists()) {
            boolean created = directory.mkdirs(); // 创建文件夹（包括父文件夹）
            if (created) {
                log.info("文件夹创建成功: {}", node.getAbsolutePath());
            } else {
                log.error("文件夹创建失败: {}", node.getAbsolutePath());
            }
        } else {
            log.error("文件夹已存在，跳过: {}", node.getAbsolutePath());
        }

        // 递归创建子文件夹
        if (node.getChildren() != null) {
            for (DirectoryNode child : node.getChildren()) {
                createDirectories(child);
            }
        }
    }




}
