package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentAgent;
import cn.cslg.pas.domain.PatentAgentLink;
import cn.cslg.pas.common.model.params.PatentAgentParams;
import cn.cslg.pas.mapper.PatentAgentMapper;
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.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 专利信息代理人表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-24
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentAgentService extends ServiceImpl<PatentAgentMapper, PatentAgent> {

    private final PatentAgentLinkService patentAgentLinkService;

    public List<PatentAgent> getPatentAgentByPatentIds(List<Integer> patentIds) {
        if (patentIds == null || patentIds.size() == 0) {
            return new ArrayList<>();
        }
        List<PatentAgentLink> patentAgentLinkList = patentAgentLinkService.getPatentAgentLinkByPatentIds(patentIds);
        List<PatentAgent> patentAgentList = this.getPatentAgentByIds(patentAgentLinkList.stream().map(PatentAgentLink::getAgentId).distinct().collect(Collectors.toList()));
        List<PatentAgent> dataList = new ArrayList<>();
        for (Integer patentId : patentIds) {
            List<PatentAgentLink> patentAgentLinks = patentAgentLinkList.stream().filter(item -> item.getPatentId().equals(patentId)).collect(Collectors.toList());
            for (PatentAgentLink patentAgentLink : patentAgentLinks) {
                PatentAgent patentAgent = patentAgentList.stream().filter(item -> item.getId().equals(patentAgentLink.getAgentId())).findFirst().orElse(null);
                if (patentAgent != null) {
                    PatentAgent data = new PatentAgent();
                    data.setName(patentAgent.getName());
                    data.setPatentId(patentId);
                    data.setId(patentAgent.getId());
                    dataList.add(data);
                }
            }
        }
        return dataList;
    }

    public List<PatentAgent> getPatentAgentByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PatentAgent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentAgent::getId, ids);
        return this.list(queryWrapper);
    }

    public List<PatentAgent> getPatentAgentByNameList(List<String> nameList) {
        if (nameList == null || nameList.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PatentAgent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentAgent::getName, nameList);
        return this.list(queryWrapper);
    }

    public List<PatentAgent> getPatentAgentByPatentId(Integer patentId) {
        List<PatentAgentLink> linkList = patentAgentLinkService.getPatentAgentLinkByPatentId(patentId);
        List<Integer> ids = linkList.stream().map(PatentAgentLink::getAgentId).collect(Collectors.toList());
        if (ids.size() == 0) {
            return new ArrayList<>();
        }
        return this.list(Wrappers.<PatentAgent>lambdaQuery().in(PatentAgent::getId, ids));
    }

    public void importPatentAgent(Integer localPatentId, Integer importPatentId, List<PatentAgent> importPatentAgentList, List<PatentAgentLink> importPatentAgentLinkList) {
        patentAgentLinkService.deleteByPatentId(localPatentId);
        List<PatentAgentLink> importPatentAgentLinks = importPatentAgentLinkList.stream().filter(item -> item.getPatentId().equals(importPatentId)).collect(Collectors.toList());
        List<Integer> importAgentIds = importPatentAgentLinks.stream().map(PatentAgentLink::getAgentId).collect(Collectors.toList());
        List<PatentAgent> importPatentAgents = importPatentAgentList.stream().filter(item -> importAgentIds.contains(item.getId())).collect(Collectors.toList());
        List<PatentAgent> localPatentAgents = this.getPatentAgentByNameList(importPatentAgents.stream().map(PatentAgent::getName).collect(Collectors.toList()));
        for (PatentAgent importPatentAgent : importPatentAgents) {
            PatentAgent localPatentAgent = localPatentAgents.stream().filter(item -> item.getName().equals(importPatentAgent.getName())).findFirst().orElse(null);
            if (localPatentAgent == null) {
                localPatentAgent = new PatentAgent();
                localPatentAgent.setName(importPatentAgent.getName());
                localPatentAgent.insert();
            }
            List<PatentAgentLink> patentAgentLinkList = importPatentAgentLinks.stream().filter(item -> item.getAgentId().equals(importPatentAgent.getId())).collect(Collectors.toList());
            for (PatentAgentLink patentAgentLink : patentAgentLinkList) {
                patentAgentLink.setPatentId(localPatentId);
                patentAgentLink.setAgentId(localPatentAgent.getId());
                patentAgentLink.setId(null);
            }
            patentAgentLinkService.saveOrUpdateBatch(patentAgentLinkList);
        }
    }

    /**
     * 代理人数据入代理人表“os_patent_agent”，代理人ids和专利id入代理人与专利关联表“os_patent_agent_link”
     *
     * @param patentAgentParams 代理人相关参数
     */
    public void updatePatentAgent(PatentAgentParams patentAgentParams) {
        if (patentAgentParams.getAgent() == null || patentAgentParams.getAgent().size() == 0) {
            return;
        }

        //创建ids集合，用于存放代理人ids
        List<Integer> ids = new ArrayList<>();
        //根据代理人名称集合查询代理人表“os_patent_agent”数据
        LambdaQueryWrapper<PatentAgent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentAgent::getName, patentAgentParams.getAgent());
        List<PatentAgent> tempList = this.list(queryWrapper);
        //遍历代理人名称集合
        patentAgentParams.getAgent().forEach(name -> {
            //将从表中查询出来的代理人集合进行过滤，过滤出与当前遍历的代理人相同的数据
            PatentAgent agent = tempList.stream().filter(item -> item.getName().equals(name)).findFirst().orElse(null);
            //判断若数据为null，则表示表中没有当前遍历的代理人，则新增该代理人入表“os_patent_agent”
            if (agent == null) {
                agent = this.add(name);
            }
            ids.add(agent.getId());
        });

        //更新代理人关联专利表“os_patent_agent_link”数据
        patentAgentLinkService.updatePatentAgentLink(patentAgentParams.getPatentId(), ids);
    }

    /**
     * 新增代理人入专利信息代理人表“os_patent_agent”
     *
     * @param name 代理人名称
     * @return 返回代理人数据
     */
    public PatentAgent add(String name) {
        PatentAgent agent = new PatentAgent();
        agent.setName(name);
        agent.insert();
        return agent;
    }
}
