package cn.cslg.pas.service;

import cn.cslg.pas.domain.Patent;
import cn.cslg.pas.domain.PatentApplicant;
import cn.cslg.pas.domain.PatentSimpleFamilyLink;
import cn.cslg.pas.common.model.vo.PatentSimpleFamilyVO;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.mapper.PatentSimpleFamilyLinkMapper;
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 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 PatentSimpleFamilyLinkService extends ServiceImpl<PatentSimpleFamilyLinkMapper, PatentSimpleFamilyLink> {

    private final PatentService patentService;
    private final PatentApplicantService patentApplicantService;

    public IPage<PatentSimpleFamilyLink> getPageList(PatentSimpleFamilyVO params) {
        LambdaQueryWrapper<PatentSimpleFamilyLink> queryWrapper = new LambdaQueryWrapper<>();
        //从专利同族关联表里获取关联信息
        if (StringUtils.isNotEmpty(params.getPatentNo())) {
            queryWrapper.like(PatentSimpleFamilyLink::getPatentNo, params.getPatentNo());
        }
        queryWrapper.eq(PatentSimpleFamilyLink::getFamilyId, params.getFamilyId());
        IPage<PatentSimpleFamilyLink> pageList = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper);
        //根据从关联信息里获得的专利号获得专利列表
        List<Patent> patentList = patentService.getPatentListByPatentNo(pageList.getRecords().stream().map(PatentSimpleFamilyLink::getPatentNo).collect(Collectors.toList()));
        //根据专利ids获得申请人信息
        List<PatentApplicant> patentApplicantList = patentApplicantService.getPatentApplicantByPatentIds(patentList.stream().map(Patent::getId).collect(Collectors.toList()));
        //遍历关联信息
        pageList.getRecords().forEach(patentSimpleFamily -> {
            //根据专利号从专利列表中获得专利
            Patent patent = patentList.stream().filter(item -> item.getPatentNo().equals(patentSimpleFamily.getPatentNo())).findFirst().orElse(null);
            //根据专利装载信息，若专利未查询到，则设置名称为"暂未收录"
            if (patent == null) {
                patentSimpleFamily.setPatentId(-1);
                patentSimpleFamily.setName("暂未收录");
            } else {
                patentSimpleFamily.setPatentId(patent.getId());
                patentSimpleFamily.setName(patent.getName());
                patentSimpleFamily.setAbstractPath(patent.getAbstractPath());
                patentSimpleFamily.setApplicant(patentApplicantList.stream().filter(item -> item.getPatentId().equals(patentSimpleFamily.getPatentId())).collect(Collectors.toList()));
            }
        });
        return pageList;
    }

    public List<PatentSimpleFamilyLink> getPatentSimpleFamilyLinkByPatentNo(List<String> patentNos) {
        LambdaQueryWrapper<PatentSimpleFamilyLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentSimpleFamilyLink::getPatentNo, patentNos);
        return this.list(queryWrapper);
    }

    public List<PatentSimpleFamilyLink> getPatentSimpleFamilyLinkByPatentNoAndFamilyId(List<String> patentNo, Integer familyId) {
        LambdaQueryWrapper<PatentSimpleFamilyLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentSimpleFamilyLink::getPatentNo, patentNo);
        queryWrapper.eq(PatentSimpleFamilyLink::getFamilyId, familyId);
        return this.list(queryWrapper);
    }

    public List<PatentSimpleFamilyLink> getPatentSimpleFamilyLinkByFamilyIds(List<Integer> familyIds) {
        if (familyIds == null || familyIds.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PatentSimpleFamilyLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentSimpleFamilyLink::getFamilyId, familyIds);
        return this.list(queryWrapper);
    }
}
