package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentApplicant;
import cn.cslg.pas.domain.PatentApplicantMergeLink;
import cn.cslg.pas.domain.PatentInventor;
import cn.cslg.pas.domain.PatentInventorMerge;
import cn.cslg.pas.mapper.PatentInventorMergeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【os_patent_inventor_merge】的数据库操作Service
* @createDate 2022-05-19 08:36:07
*/
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentInventorMergeService extends ServiceImpl<PatentInventorMergeMapper, PatentInventorMerge> {

    private final PatentInventorService patentInventorService;

    public List<PatentInventorMerge> getPatentInventorMergeByProjectId(Integer projectId) {
        LambdaQueryWrapper<PatentInventorMerge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentInventorMerge::getProjectId, projectId);
        return this.list(queryWrapper);
    }

    public void deleteByMergeId(Integer mergeId) {
        this.remove(Wrappers.<PatentInventorMerge>lambdaQuery().eq(PatentInventorMerge::getMergeId, mergeId));
    }

    public void updatePatentInventorMergeLink(Integer mergeId, Integer projectId, List<Integer> inventorIds) {
        this.deleteByMergeId(mergeId);
        List<PatentInventorMerge> dataList = inventorIds.stream().map(item -> {
            PatentInventorMerge patentInventorMerge = new PatentInventorMerge();
            patentInventorMerge.setInventorId(item);
            patentInventorMerge.setProjectId(projectId);
            patentInventorMerge.setMergeId(mergeId);
            return patentInventorMerge;
        }).collect(Collectors.toList());
        if (dataList.size() != 0) {
            this.saveOrUpdateBatch(dataList);
        }
    }

    public void importPatentInventorMergeLink(Integer projectId, List<PatentInventor> importPatentInventorList, List<PatentInventorMerge> importPatentInventorMergeList) {
        List<PatentInventor> importPatentInventors = importPatentInventorList.stream().filter(PatentInventor::getMerge).collect(Collectors.toList());
        List<PatentInventor> localPatentInventors = patentInventorService.getPatentInventorByNameList(importPatentInventorList.stream().map(PatentInventor::getName).collect(Collectors.toList()));
        for (PatentInventor importPatentInventor : importPatentInventors) {
            PatentInventor localPatentInventor = localPatentInventors.stream().filter(item -> item.getName().equals(importPatentInventor.getName()) && item.getProjectId().equals(projectId)).findFirst().orElse(null);
            Integer patentInventorId;
            if (localPatentInventor == null) {
                localPatentInventor = new PatentInventor();
                patentInventorId = null;
            } else {
                patentInventorId = localPatentInventor.getId();
            }
            BeanUtils.copyProperties(importPatentInventor, localPatentInventor);
            localPatentInventor.setId(patentInventorId);
            localPatentInventor.setProjectId(projectId);
            localPatentInventor.insertOrUpdate();
            this.deleteByMergeId(localPatentInventor.getId());
            List<PatentInventorMerge> patentInventorMergeList = importPatentInventorMergeList.stream().filter(item -> item.getMergeId().equals(importPatentInventor.getId())).collect(Collectors.toList());
            for (PatentInventorMerge patentInventorMerge : patentInventorMergeList) {
                PatentInventor importPatentInventor2 = importPatentInventorList.stream().filter(item -> item.getId().equals(patentInventorMerge.getInventorId())).findFirst().orElse(new PatentInventor());
                PatentInventor localPatentInventor2 = localPatentInventors.stream().filter(item -> item.getName().equals(importPatentInventor2.getName())).findFirst().orElse(new PatentInventor());
                patentInventorMerge.setId(null);
                patentInventorMerge.setMergeId(localPatentInventor.getId());
                patentInventorMerge.setProjectId(projectId);
                patentInventorMerge.setInventorId(localPatentInventor2.getId());
            }
            this.saveOrUpdateBatch(patentInventorMergeList.stream().filter(item -> item.getInventorId() != null).collect(Collectors.toList()));
        }
    }
}
