package cn.cslg.report.service.business;


import cn.cslg.report.common.model.dto.UploadFileDTO;

import cn.cslg.report.common.utils.FileUtils;

import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.entity.ReportFiles;
import cn.cslg.report.mapper.ReportFileMapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class ReportFileService extends ServiceImpl<ReportFileMapper, ReportFiles> {
    private final FileUtils fileUtils;
    private final LoginUtils loginUtils;

    //上传文档
    public List<Integer> uploadFiles(List<MultipartFile> file) {
        List<ReportFiles> reportFiles = new ArrayList<>();
        file.forEach(item -> {
            UploadFileDTO fileDTO = fileUtils.uploadFile(item);
            ReportFiles reportFile = new ReportFiles();
            reportFile.setSize((int) item.getSize());
            reportFile.setUrl(fileDTO.getPath());
            reportFile.setFileName(fileDTO.getFileName());
            reportFile.setName(fileDTO.getName());
            reportFile.setCreateBy(loginUtils.getId());
            reportFile.setSuffix(fileDTO.getExtName());
            reportFiles.add(reportFile);
        });
        this.saveBatch(reportFiles);
        List<Integer> fileIds = reportFiles.stream().map(ReportFiles::getId).collect(Collectors.toList());
        return fileIds;


    }

    //上传文档
    public List<String> uploadFilesGetName(List<MultipartFile> file) {
        List<ReportFiles> reportFiles = new ArrayList<>();
        file.forEach(item -> {
            UploadFileDTO fileDTO = fileUtils.uploadFile(item);
            ReportFiles reportFile = new ReportFiles();
            reportFile.setSize((int) item.getSize());
            reportFile.setUrl(fileDTO.getPath());
            reportFile.setFileName(fileDTO.getFileName());
            reportFile.setName(fileDTO.getName());
            reportFile.setCreateBy(loginUtils.getId());
            reportFile.setSuffix(fileDTO.getExtName());
            reportFiles.add(reportFile);
        });
        this.saveBatch(reportFiles);
        List<String> urls = reportFiles.stream().map(ReportFiles::getUrl).collect(Collectors.toList());
        return urls;


    }

    //删除文件
    public void deleteFiles(List<Integer> fileIds) {
        this.removeByIds(fileIds);
    }


    public ReportFiles getProjectFileByName(String name, Integer projectId, String type, String suffix) {
        LambdaQueryWrapper<ReportFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFiles::getName, name);
        queryWrapper.eq(ReportFiles::getReportId, projectId);
        queryWrapper.eq(ReportFiles::getType, type);
        queryWrapper.eq(ReportFiles::getSuffix, suffix);
        return this.getOne(queryWrapper);
    }

    //根据文件Id获得文件
    public List<ReportFiles> getFiles(List<Integer> FileIds) {
        LambdaQueryWrapper<ReportFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFiles::getId, FileIds);
        List<ReportFiles> file = this.list(queryWrapper);
        return file;
    }
    public String getFile(Integer fileId) {
        LambdaQueryWrapper<ReportFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFiles::getId, fileId);
        List<ReportFiles> file = this.list(queryWrapper);
        if(file.size()>0){
            return file.get(0).getUrl();
        }
        return "";
    }

}
