package cn.cslg.report.service.business.InvalidReReport;

import cn.cslg.report.common.model.dto.ProofAddNewDTO;
import cn.cslg.report.common.model.dto.invalidReReport.ProofQueryDTO;
import cn.cslg.report.common.model.vo.PersonnelVO;
import cn.cslg.report.common.model.vo.invalidReReport.ProofVO;
import cn.cslg.report.common.utils.CacheUtils;
import cn.cslg.report.common.utils.DataUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.common.utils.ThrowException;
import cn.cslg.report.entity.invalidReReport.Proof;
import cn.cslg.report.mapper.InvalidReReport.ProofMapper;
import cn.cslg.report.service.business.ReportFileService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.beans.BeanUtils;
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;

/**
 * 文献证据的Service层实现类
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProofService extends ServiceImpl<ProofMapper, Proof> {
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;
    private final ReportFileService reportFileService;

    //上传非专利文献证据
    public void addProofByFile(ProofAddNewDTO proofAddNewDTO, List<MultipartFile> files) {
        log.info("开始处理【上传非专利文献证据】的业务，参数为：{}, {}", proofAddNewDTO, files);

        //检查本次请求合法性（是否有上传非专利文献证据）
        if (files == null || files.size() == 0) {
            ThrowException.throwXiaoShiException("未上传文件");
        }

        //获取当前登陆人信息
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());

        ArrayList<Proof> proofs = new ArrayList<>();
        //上传非专利文献证据文件附件，附件信息入报告系统文件表后，返回附件ids
        List<Integer> fileIds = reportFileService.uploadFiles(files);
        //查询证据表，取出该报告已有证据最大排序序号
        List<Proof> proofList = this.list(new LambdaQueryWrapper<Proof>().eq(Proof::getReportId, proofAddNewDTO.getReportId()).orderByDesc(Proof::getSort));
        Integer biggestSort;
        //若该报告目前没有证据，则最大排序序号设为0；若有则最大排序序号就是当前按序号倒序的第一个证据的排序序号
        if (proofList == null || proofList.size() == 0) {
            biggestSort = 0;
        } else {
            biggestSort = proofList.get(0).getSort();
        }
        //遍历证据文件，证据表实体类装载数据
        for (Integer fileId : fileIds) {
            Proof proof = new Proof();
            BeanUtils.copyProperties(proofAddNewDTO, proof);  //DTO赋值给实体类（装载报告id、证据名称、备注）
            proof.setProofType(2);  //装载证据类型
            proof.setCreateId(personnelVO.getId());  //装载创建人id
            proof.setFileId(fileId);  //装载文件id
            proof.setSort(++biggestSort);
            proofs.add(proof);
        }
        //证据表新增数据
        boolean saveResult = this.saveBatch(proofs);
        if (!saveResult) {
            ThrowException.throwXiaoShiException("上传非专利文献证据失败，服务器忙请稍后再次尝试");
        }

        log.info("上传非专利文献证据完成");

    }

}