package cn.cslg.report.service.business;

import cn.cslg.report.common.model.dto.PatentQueryFieldSourceDTO;
import cn.cslg.report.common.model.dto.UploadFileDTO;
import cn.cslg.report.common.model.vo.PatentQueryFieldSourceVO;
import cn.cslg.report.common.utils.DateUtils;
import cn.cslg.report.common.utils.FileUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.entity.ReportFiles;
import cn.cslg.report.mapper.ReportFileMapper;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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})
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> reportIds= reportFiles.stream().map(ReportFiles::getId).collect(Collectors.toList());
        return reportIds;


    }

    //删除文件
    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;
    }

}
