package cn.cslg.report.service.business;

import cn.cslg.report.common.utils.DataUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.ImportTask;
import cn.cslg.report.entity.LitigationHistory;
import cn.cslg.report.entity.Report;
import cn.cslg.report.mapper.ImportTaskMapper;
import cn.cslg.report.mapper.LitigationHistoryMapper;
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 LitigationHistoryService extends ServiceImpl<LitigationHistoryMapper, LitigationHistory> {
    private final ReportFileService reportFileService;
    private final AssoLitigationHisFileService  assoLitigationHisFileService;
    public String deleteLitigationHistory(int id){
        this.removeById(id);
       return Response.success();

    }

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

    public String updateLitigationHistory(LitigationHistory litigationHistory){
        litigationHistory.updateById();
        return Response.success();
    }

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

}
