package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentPledge;
import cn.cslg.pas.common.model.params.PatentPledgeParams;
import cn.cslg.pas.mapper.PatentPledgeMapper;
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 PatentPledgeService extends ServiceImpl<PatentPledgeMapper, PatentPledge> {

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

    public void importPatentPledge(Integer localPatentId, Integer importPatentId, List<PatentPledge> importPatentPledgeList) {
        this.deletePatentPledgeByPatentId(localPatentId);
        List<PatentPledge> patentPledgeList = importPatentPledgeList.stream().filter(item -> item.getPatentId().equals(importPatentId)).collect(Collectors.toList());
        for (PatentPledge patentPledge : patentPledgeList) {
            patentPledge.setId(null);
            patentPledge.setPatentId(localPatentId);
        }
        this.saveOrUpdateBatch(patentPledgeList);
    }

    /**
     * 新增或更新专利信息质押质权人表“os_patent_pledge”数据
     *
     * @param patentPledgeParams 质押质权人相关信息
     */
    public void updatePatentPledge(PatentPledgeParams patentPledgeParams) {
        if (patentPledgeParams.getPledgee() == null || patentPledgeParams.getPledgor() == null) {
            return;
        }

        //判断若有质押人与质权人并且两方数量相同
        if (patentPledgeParams.getPledgee().size() == patentPledgeParams.getPledgor().size() && patentPledgeParams.getPledgor().size() != 0) {
            //先根据专利id删除专利信息质押质权人表“os_patent_pledge”原数据
            this.deletePatentPledgeByPatentId(patentPledgeParams.getPatentId());
            //创建专利信息质押质权人表实体类集合，准备批量插入数据
            List<PatentPledge> patentPledgeList = new ArrayList<>();
            //遍历实体类集合装载数据
            for (int i = 0; i < patentPledgeParams.getPledgor().size(); i++) {
                PatentPledge patentPledge = new PatentPledge();
                patentPledge.setPledgee(patentPledgeParams.getPledgee().get(i));
                patentPledge.setPledgor(patentPledgeParams.getPledgor().get(i));
                patentPledge.setPatentId(patentPledgeParams.getPatentId());
                patentPledgeList.add(patentPledge);
            }
            //新增或更新专利信息质押质权人表“os_patent_pledge”数据
            this.saveOrUpdateBatch(patentPledgeList);
        }
    }

    /**
     * 根据专利id删除专利信息质押质权人表“os_patent_pledge”数据
     *
     * @param patentId 专利id
     */
    public void deletePatentPledgeByPatentId(Integer patentId) {
        this.remove(Wrappers.<PatentPledge>lambdaQuery().eq(PatentPledge::getPatentId, patentId));
    }
}
