package cn.cslg.report.service.business;

import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.ImportTask;
import cn.cslg.report.entity.InvalidRecord;
import cn.cslg.report.entity.LitigationHistory;
import cn.cslg.report.mapper.ImportTaskMapper;
import cn.cslg.report.mapper.InvalidRecordMapper;
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 InvalidRecordService extends ServiceImpl<InvalidRecordMapper, InvalidRecord> {
    public final InvalidRecordMapper invalidRecordMapper;
    private final ReportFileService reportFileService;
    private final  AssoInvalidRecordFileService assoInvalidRecordFileService;
    public String deleteInvalidRecord(int id) throws IOException {
        this.removeById(id);
        return Response.success();
    }

    public String updateInvalidRecord(InvalidRecord invalidRecord) throws IOException {
        invalidRecord.updateById();
        return Response.success();
    }

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

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


    }

