package cn.cslg.pas.service;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.ClassNumberDTO;
import cn.cslg.pas.common.model.params.PatentIpcMessageParams;
import cn.cslg.pas.common.utils.PatentUtils;
import cn.cslg.pas.domain.PatentClassNumber;
import cn.cslg.pas.domain.PatentClassNumberLink;
import cn.cslg.pas.mapper.PatentClassNumberMapper;
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;

/**
 * <p>
 * 国际分类号表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-24
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentClassNumberService extends ServiceImpl<PatentClassNumberMapper, PatentClassNumber> {

    public final PatentClassNumberLinkService patentClassNumberLinkService;

    public PatentClassNumber getPatentClassNumberByCodeAndTypeAndLevel(Integer type, String code, Integer level) {
        LambdaQueryWrapper<PatentClassNumber> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentClassNumber::getCode, code);
        queryWrapper.eq(PatentClassNumber::getType, type);
        queryWrapper.eq(PatentClassNumber::getLevel, level);
        return this.getOne(queryWrapper);
    }

    public void updateMainIpcMessage(PatentIpcMessageParams params) {
        PatentClassNumberLink patentClassNumberLink = patentClassNumberLinkService.getPatentClassNumberLinkByPatentIdAndTypeAndMain(params.getPatentId(), Constants.PATENT_CLASS_NUMBER_IPC, 1);
        if (patentClassNumberLink != null) {
            ClassNumberDTO dto = PatentUtils.getClassNumberDTO(patentClassNumberLink.getCode(), Constants.PATENT_CLASS_NUMBER_IPC);
            PatentClassNumber level1 = this.getPatentClassNumberByCodeAndTypeAndLevel(Constants.PATENT_CLASS_NUMBER_IPC, dto.getA(), 1);
            PatentClassNumber level2 = this.getPatentClassNumberByCodeAndTypeAndLevel(Constants.PATENT_CLASS_NUMBER_IPC, dto.getB(), 2);
            PatentClassNumber level3 = this.getPatentClassNumberByCodeAndTypeAndLevel(Constants.PATENT_CLASS_NUMBER_IPC, dto.getC(), 3);
            PatentClassNumber level4 = this.getPatentClassNumberByCodeAndTypeAndLevel(Constants.PATENT_CLASS_NUMBER_IPC, dto.getD(), 4);
            PatentClassNumber level5 = this.getPatentClassNumberByCodeAndTypeAndLevel(Constants.PATENT_CLASS_NUMBER_IPC, dto.getE(), 5);
            if (level1 == null) {
                level1 = new PatentClassNumber();
                level1.setLevel(1);
                level1.setCode(dto.getA());
                level1.setType(Constants.PATENT_CLASS_NUMBER_IPC);
                level1.setParaphrase(params.getLevel1());
                level1.insert();
            } else {
                level1.setParaphrase(params.getLevel1());
                level1.updateById();
            }
            if (level2 == null) {
                level2 = new PatentClassNumber();
                level2.setLevel(2);
                level2.setCode(dto.getB());
                level2.setType(Constants.PATENT_CLASS_NUMBER_IPC);
                level2.setParaphrase(params.getLevel2());
                level2.insert();
            } else {
                level2.setParaphrase(params.getLevel2());
                level2.updateById();
            }
            if (level3 == null) {
                level3 = new PatentClassNumber();
                level3.setLevel(3);
                level3.setCode(dto.getC());
                level3.setType(Constants.PATENT_CLASS_NUMBER_IPC);
                level3.setParaphrase(params.getLevel3());
                level3.insert();
            } else {
                level3.setParaphrase(params.getLevel3());
                level3.updateById();
            }
            if (level4 == null) {
                level4 = new PatentClassNumber();
                level4.setLevel(4);
                level4.setCode(dto.getD());
                level4.setType(Constants.PATENT_CLASS_NUMBER_IPC);
                level4.setParaphrase(params.getLevel4());
                level4.insert();
            } else {
                level4.setParaphrase(params.getLevel4());
                level4.updateById();
            }
            if (level5 == null) {
                level5 = new PatentClassNumber();
                level5.setLevel(5);
                level5.setCode(dto.getE());
                level5.setType(Constants.PATENT_CLASS_NUMBER_IPC);
                level5.setParaphrase(params.getLevel5());
                level5.insert();
            } else {
                level5.setParaphrase(params.getLevel5());
                level5.updateById();
            }
        }
    }
}
