package cn.cslg.pas.service;

import cn.cslg.pas.domain.PatentAgency;
import cn.cslg.pas.mapper.PatentAgencyMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 PatentAgencyService extends ServiceImpl<PatentAgencyMapper, PatentAgency> {

    public PatentAgency getPatentAgencyById(Integer id) {
        return this.getById(id);
    }

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

    public String getAgencyStringIdByName(String name) {
        //根据代理机构名称查询os_patent_agency表
        LambdaQueryWrapper<PatentAgency> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentAgency::getName, name);
        PatentAgency temp = this.getOne(queryWrapper);
        //若没有，则将该代理机构新增入库获得该代理机构id
        if (temp == null) {
            temp = this.add(name);
        }
        //将id转成String类型返回
        return String.valueOf(temp.getId());
    }

    /**
     * 新增代理机构入表"os_patent_agency"
     *
     * @param name 代理机构名称
     * @return 返回代理机构数据
     */
    public PatentAgency add(String name) {
        PatentAgency agency = new PatentAgency();
        agency.setName(name);
        agency.insert();
        return agency;
    }
}
