package cn.cslg.pas.service;

import cn.cslg.pas.common.model.params.PatentInstructionTextParams;
import cn.cslg.pas.domain.PatentInstructionText;
import cn.cslg.pas.mapper.PatentInstructionTextMapper;
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;

/**
 * @author Administrator
 * @description 针对表【os_patent_instruction_text(专利说明书（文本）)】的数据库操作Service
 * @createDate 2022-04-16 08:38:10
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentInstructionTextService extends ServiceImpl<PatentInstructionTextMapper, PatentInstructionText> {

    private final PatentService patentService;

    /**
     * 根据专利id查询说明书表“os_patent_instruction_text”数据
     *
     * @param patentId 专利id
     * @return 返回说明书数据
     */
    public PatentInstructionText getPatentInstructionTextByPatentId(Integer patentId) {
        LambdaQueryWrapper<PatentInstructionText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentInstructionText::getPatentId, patentId);
        return this.getOne(queryWrapper);
    }

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

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

    public void importPatentInstructionText(Integer localPatentId, Integer importPatentId, List<PatentInstructionText> importPatentInstructionTextList) {
        this.deleteByPatentId(localPatentId);
        PatentInstructionText patentInstructionText = importPatentInstructionTextList.stream().filter(item -> item.getPatentId().equals(importPatentId)).findFirst().orElse(null);
        if (patentInstructionText != null) {
            patentInstructionText.setId(null);
            patentInstructionText.setPatentId(localPatentId);
            patentInstructionText.insert();
        }
    }

    public void updatePatentInstructionText(PatentInstructionTextParams params) {
        //根据专利id查询说明书文本表“os_patent_instruction_text”数据
        PatentInstructionText patentInstructionText = this.getPatentInstructionTextByPatentId(params.getPatentId());
        //若不存在则new说明书表实体类
        if (patentInstructionText == null) {
            patentInstructionText = new PatentInstructionText();
        }
        //装载专利id
        patentInstructionText.setPatentId(params.getPatentId());
        //装载说明书
        patentInstructionText.setManual(params.getManual());
        //装载说明书（译）
        patentInstructionText.setManualOut(params.getManualOut());
        //新增或更新表数据
        patentInstructionText.insertOrUpdate();
    }
}
