package cn.cslg.report.service.business;


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

import cn.cslg.report.entity.asso.AssoReportFile;

import cn.cslg.report.mapper.AssoReportFileMapper;
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.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class AssoReportFileService extends ServiceImpl<AssoReportFileMapper, AssoReportFile> {

    private final ReportFileService reportFileService;

    /**
     * @param reportId 报告id
     * @param fileIds  文件id
     * @return
     * @function 上传报告报告与文件关联表 对应实体AssoReportFile 0代表上传文件为附件
     */
    public String addAsso(Integer reportId, List<Integer> fileIds) {
        List<AssoReportFile> assoReportFiles = new ArrayList<>();
        fileIds.forEach(item -> {
            AssoReportFile assoReportFile = new AssoReportFile();
            assoReportFile.setReportId(reportId);
            assoReportFile.setFileId(item);
            assoReportFile.setFileType(0);
            assoReportFiles.add(assoReportFile);
        });
        this.saveBatch(assoReportFiles);
        return Response.success();
    }

    /**
     * @param reportId 报告id
     * @param fileIds  文件id
     * @return
     * @function 上传报告报告与文件关联表 对应实体AssoReportFile 1代表上传最终报告
     */

    public String add(Integer reportId, List<Integer> fileIds) {
        List<AssoReportFile> assoReportFiles = new ArrayList<>();
        fileIds.forEach(item -> {
            AssoReportFile assoReportFile = new AssoReportFile();
            assoReportFile.setReportId(reportId);
            assoReportFile.setFileId(item);
            assoReportFile.setFileType(1);
            assoReportFiles.add(assoReportFile);
        });
        this.saveBatch(assoReportFiles);
        return Response.success();
    }


    /**
     * @param reportIds 报告id
     * @return 查询上传报告报告与文件关联表 对应实体AssoReportFile
     * @function 根据报告id 查询上传报告报告与文件关联表
     */
    public List<AssoReportFile> queryFileByReportId(List<Integer> reportIds) {
        if (reportIds.size() == 0) {
            List<AssoReportFile> files = new ArrayList<>();
            return files;
        }
        LambdaQueryWrapper<AssoReportFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AssoReportFile::getReportId, reportIds);
        List<AssoReportFile> assoReportFiles = this.list(wrapper);
        return assoReportFiles;
    }


}
