package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentAgentLink;
import cn.cslg.pas.mapper.PatentAgentLinkMapper;
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 PatentAgentLinkService extends ServiceImpl<PatentAgentLinkMapper, PatentAgentLink> {

    public List<PatentAgentLink> getPatentAgentLinkByPatentId(Integer patentId) {
        return this.list(Wrappers.<PatentAgentLink>lambdaQuery().eq(PatentAgentLink::getPatentId, patentId));
    }

    public List<PatentAgentLink> getPatentAgentLinkByPatentIds(List<Integer> patentIds) {
        if (patentIds == null || patentIds.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PatentAgentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PatentAgentLink::getPatentId, patentIds);
        return this.list(queryWrapper);
    }

    /**
     * 更新代理人关联专利表“os_patent_agent_link”数据
     *
     * @param patentId 专利id
     * @param ids      代理人ids
     */
    public void updatePatentAgentLink(Integer patentId, List<Integer> ids) {
        //根据专利id删除表中原数据
        this.deleteByPatentId(patentId);

        //代理人关联专利表实体类集合装载数据
        List<PatentAgentLink> dataList = ids.stream().map(item -> {
            PatentAgentLink link = new PatentAgentLink();
            link.setAgentId(item);
            link.setPatentId(patentId);
            return link;
        }).collect(Collectors.toList());

        //数据批量插入代理人关联专利表“os_patent_agent_link”
        this.saveOrUpdateBatch(dataList);
    }

    /**
     * 根据专利id删除代理人关联专利表“os_patent_agent_link”数据
     *
     * @param patentId 专利id
     */
    public void deleteByPatentId(Integer patentId) {
        this.remove(Wrappers.<PatentAgentLink>lambdaQuery().eq(PatentAgentLink::getPatentId, patentId));
    }

}
