package cn.cslg.report.service.business;

import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.InvalidRecord;
import cn.cslg.report.entity.ReviewHistory;
import cn.cslg.report.mapper.InvalidRecordMapper;
import cn.cslg.report.mapper.ReviewHistoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.List;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ReviewHistoryService extends ServiceImpl<ReviewHistoryMapper, ReviewHistory> {
private final   ReportFileService reportFileService;
private final AssoReviewHisFileService assoReviewHisFileService;
    public String deleteReviewHistory(int id) throws IOException {
        this.removeById(id);
        return Response.success();
    }

    public String updateReviewHistory(ReviewHistory reviewHistory)  {
        reviewHistory.updateById();
        return Response.success();
    }

    public String addReviewHistory(ReviewHistory reviewHistory,List<MultipartFile> files) throws IOException {
        reviewHistory.insert();
        if (files==null&&files.size() != 0) {
            //将文档上传并返回文件入库的Id
            List<Integer> fileIds = reportFileService.uploadFiles(files);
            assoReviewHisFileService.addAsso(reviewHistory.getId(),fileIds);
        }
        return Response.success();
    }

    public String queryReviewHistory(ReviewHistory reviewHistory){
        LambdaQueryWrapper<ReviewHistory> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ReviewHistory::getPatentNo,reviewHistory.getPatentNo());
        if (reviewHistory.getSize() != null && reviewHistory.getSize() != null) {
            IPage<ReviewHistory> pages = this.page(new Page<>(reviewHistory.getCurrent(), reviewHistory.getSize()), wrapper);
            return Response.success(pages);
        } else {
            List<ReviewHistory> reviewHistories= this.list(wrapper);
            return Response.success(reviewHistories);
        }
    }


}