package com.keyware.shandan.bianmu.controller;

import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.system.entity.SysFile;
import com.keyware.shandan.system.service.SysFileService;
import com.keyware.shandan.system.utils.ErbuCaller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/erbu")
public class ErbuController {

    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private ErbuCaller erbuCaller;

    @GetMapping("/fenxi/{dirId}")
    public Result<Object> fenxi(@PathVariable String dirId, String result) {
        List<SysFile> files = sysFileService.listByDirId(dirId);
        Assert.isTrue(files.size() > 0, "该目录下没有文件数据");
        StringBuffer stringBuffer = new StringBuffer();
        files.forEach(file -> {
            stringBuffer.append(erbuCaller.call(file, result));
            stringBuffer.append("\n");
        });
        return Result.of(stringBuffer.toString());
    }
}
