package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentApplicant;
import cn.cslg.pas.domain.PatentApplicantLink;
import cn.cslg.pas.mapper.PatentApplicantLinkMapper;
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;

/**
 * <p>
 * 申请人属性表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-24
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentApplicantLinkService extends ServiceImpl<PatentApplicantLinkMapper, PatentApplicantLink> {

    private final PatentApplicantService patentApplicantService;

    public List<PatentApplicantLink> getPatentApplicantAttributesByPatentId(Integer patentId) {
        LambdaQueryWrapper<PatentApplicantLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentApplicantLink::getPatentId, patentId);
        return this.list(queryWrapper);
    }

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

    /**
     * 更新申请人表"os_patent_applicant"和申请人属性关联表"os_applicant_attr"数据
     *
     * @param name 申请人/权利人
     * @param shortName 标准申请人/标准权利人
     * @param type 类型（1权利人 2申请人）
     * @param patentId 专利id
     * @param patentApplicantList 申请人/权利人/标准申请人/标准权利人集合
     */
    public void updatePatentApplicantLink(List<String> name, List<String> shortName, Integer type, Integer patentId, List<PatentApplicant> patentApplicantList) {
        if (shortName == null) {
            shortName = new ArrayList<>();
        }
        List<Integer> applicableIds = patentApplicantService.updatePatentApplicant(name, shortName, patentApplicantList);
        if (applicableIds.size() != 0) {
            //先根据专利id删除申请人属性表”os_applicant_attr“原数据
            this.remove(Wrappers.<PatentApplicantLink>lambdaQuery().eq(PatentApplicantLink::getPatentId, patentId).eq(PatentApplicantLink::getType, type));
            //遍历新增数据入申请人属性表”os_applicant_attr“原数据
            for (int i = 0; i < applicableIds.size(); i++) {
                PatentApplicantLink patentApplicantLink = new PatentApplicantLink();
                patentApplicantLink.setApplicantId(applicableIds.get(i));
                patentApplicantLink.setPatentId(patentId);
                patentApplicantLink.setType(type);
                patentApplicantLink.setOrder(i);
                patentApplicantLink.insert();
            }
        }
    }

    public void deleteByPatentId(Integer patentId) {
        this.remove(Wrappers.<PatentApplicantLink>lambdaQuery().eq(PatentApplicantLink::getPatentId, patentId));
    }
}
