package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentApplicant;
import cn.cslg.pas.domain.PatentApplicantMergeLink;
import cn.cslg.pas.mapper.PatentApplicantMergeLinkMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 合并申请人管理表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-03-01
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentApplicantMergeLinkService extends ServiceImpl<PatentApplicantMergeLinkMapper, PatentApplicantMergeLink> {

    private final PatentApplicantService patentApplicantService;

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

    public List<PatentApplicantMergeLink> getPatentApplicantMergeLinkListByMergeId(Integer mergeId) {
        LambdaQueryWrapper<PatentApplicantMergeLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentApplicantMergeLink::getMergeId, mergeId);
        return this.list(queryWrapper);
    }

    public List<PatentApplicantMergeLink> getPatentApplicantMergeLinkListByApplicantIds(List<Integer> applicantIds) {
        if (applicantIds == null || applicantIds.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PatentApplicantMergeLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentApplicantMergeLink::getApplicantId, applicantIds);
        return this.list(queryWrapper);
    }

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

    public void deleteByMergeIdAndApplicantId(Integer applicantId, Integer mergeId) {
        this.remove(Wrappers.<PatentApplicantMergeLink>lambdaQuery().eq(PatentApplicantMergeLink::getMergeId, mergeId).eq(PatentApplicantMergeLink::getApplicantId, applicantId));
    }

    @Transactional
    public void deleteByProjectId(Integer projectId) {
        List<PatentApplicantMergeLink> patentApplicantMergeLinkList = this.getPatentApplicantMergeLinkListByProjectId(projectId);
        for (PatentApplicantMergeLink patentApplicantMergeLink : patentApplicantMergeLinkList) {
            patentApplicantService.delete(patentApplicantMergeLink.getMergeId());
        }
    }

    public void importPatentApplicantMergeLink(Integer projectId, List<PatentApplicant> importPatentApplicantList, List<PatentApplicantMergeLink> importPatentApplicantMergeLinkList) {
        List<PatentApplicant> importPatentApplicants = importPatentApplicantList.stream().filter(PatentApplicant::getMerge).collect(Collectors.toList());
        List<PatentApplicant> localPatentApplicants = patentApplicantService.getApplicantListByNameList(importPatentApplicantList.stream().map(PatentApplicant::getName).collect(Collectors.toList()));
        for (PatentApplicant importPatentApplicant : importPatentApplicants) {
            PatentApplicant localPatentApplicant = localPatentApplicants.stream().filter(item -> item.getName().equals(importPatentApplicant.getName()) && item.getProjectId().equals(projectId)).findFirst().orElse(null);
            Integer patentApplicantId;
            if (localPatentApplicant == null) {
                localPatentApplicant = new PatentApplicant();
                patentApplicantId = null;
            } else {
                patentApplicantId = localPatentApplicant.getId();
            }
            BeanUtils.copyProperties(importPatentApplicant, localPatentApplicant);
            localPatentApplicant.setId(patentApplicantId);
            localPatentApplicant.setProjectId(projectId);
            localPatentApplicant.insertOrUpdate();
            this.deleteByMergeId(localPatentApplicant.getId());
            List<PatentApplicantMergeLink> patentApplicantMergeLinkList = importPatentApplicantMergeLinkList.stream().filter(item -> item.getMergeId().equals(importPatentApplicant.getId())).collect(Collectors.toList());
            for (PatentApplicantMergeLink patentApplicantMergeLink : patentApplicantMergeLinkList) {
                PatentApplicant importPatentApplicant2 = importPatentApplicantList.stream().filter(item -> item.getId().equals(patentApplicantMergeLink.getApplicantId())).findFirst().orElse(new PatentApplicant());
                PatentApplicant localPatentApplicant2 = localPatentApplicants.stream().filter(item -> item.getName().equals(importPatentApplicant2.getName())).findFirst().orElse(new PatentApplicant());
                patentApplicantMergeLink.setId(null);
                patentApplicantMergeLink.setMergeId(localPatentApplicant.getId());
                patentApplicantMergeLink.setProjectId(projectId);
                patentApplicantMergeLink.setApplicantId(localPatentApplicant2.getId());
            }
            this.saveOrUpdateBatch(patentApplicantMergeLinkList.stream().filter(item -> item.getApplicantId() != null).collect(Collectors.toList()));
        }
    }

    public void updatePatentApplicantMergeLink(Integer mergeId, Integer projectId, List<Integer> applicantIds) {
        this.deleteByMergeId(mergeId);
        List<PatentApplicantMergeLink> dataList = applicantIds.stream().map(item -> {
            PatentApplicantMergeLink patentApplicantMergeLink = new PatentApplicantMergeLink();
            patentApplicantMergeLink.setApplicantId(item);
            patentApplicantMergeLink.setProjectId(projectId);
            patentApplicantMergeLink.setMergeId(mergeId);
            return patentApplicantMergeLink;
        }).collect(Collectors.toList());
        if (dataList.size() != 0) {
            this.saveOrUpdateBatch(dataList);
        }
    }
}
