package cn.cslg.pas.service;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.domain.PatentClassNumberLink;
import cn.cslg.pas.common.model.params.PatentClassNumberParams;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.mapper.PatentClassNumberLinkMapper;
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 PatentClassNumberLinkService extends ServiceImpl<PatentClassNumberLinkMapper, PatentClassNumberLink> {

    private final PatentClassNumberService patentClassNumberService;
    private final PatentService patentService;

    public PatentClassNumberLink getPatentClassNumberLinkByPatentIdAndTypeAndMain(Integer patentId, Integer type, Integer main) {
        LambdaQueryWrapper<PatentClassNumberLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentClassNumberLink::getPatentId, patentId);
        queryWrapper.eq(PatentClassNumberLink::getType, type);
        queryWrapper.eq(PatentClassNumberLink::getMain, main);
        return this.getOne(queryWrapper);
    }

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

    public void importPatentClassNumberLink(Integer localPatentId, Integer importPatentId, List<PatentClassNumberLink> importPatentClassNumberLinkList) {
        this.deleteByPatentId(localPatentId);
        List<PatentClassNumberLink> patentClassNumberLinkList = importPatentClassNumberLinkList.stream().filter(item -> item.getPatentId().equals(importPatentId)).collect(Collectors.toList());
        for (PatentClassNumberLink patentClassNumberLink : patentClassNumberLinkList) {
            patentClassNumberLink.setPatentId(localPatentId);
            patentClassNumberLink.setId(null);
        }
        this.saveOrUpdateBatch(patentClassNumberLinkList);
    }

    public void updatePatentClassNumberLink(PatentClassNumberParams params) {
        if (params.getIpcList() != null && params.getIpcList().size() != 0) {
            this.updateData(params.getPatentId(), params.getIpcList(), Constants.PATENT_CLASS_NUMBER_IPC);
        }
        if (params.getCpcList() != null && params.getCpcList().size() != 0) {
            this.updateData(params.getPatentId(), params.getCpcList(), Constants.PATENT_CLASS_NUMBER_CPC);
        }
        if (params.getUpcList() != null && params.getUpcList().size() != 0) {
            this.updateData(params.getPatentId(), params.getUpcList(), Constants.PATENT_CLASS_NUMBER_UPC);
        }
        if (params.getLocList() != null && params.getLocList().size() != 0) {
            this.updateData(params.getPatentId(), params.getLocList(), Constants.PATENT_CLASS_NUMBER_LOC);
        }
        if (StringUtils.isNotEmpty(params.getMainIpc())) {
            this.updateMain(params.getPatentId(), Constants.PATENT_CLASS_NUMBER_IPC, params.getMainIpc());
        }
        if (StringUtils.isNotEmpty(params.getMainUpc())) {
            this.updateMain(params.getPatentId(), Constants.PATENT_CLASS_NUMBER_UPC, params.getMainUpc());
        }
    }

    public void updateMain(Integer patentId, Integer type, String code) {
        this.remove(Wrappers.<PatentClassNumberLink>lambdaQuery().eq(PatentClassNumberLink::getPatentId, patentId).eq(PatentClassNumberLink::getType, type).eq(PatentClassNumberLink::getCode, code));
        PatentClassNumberLink patentClassNumberLink = new PatentClassNumberLink();
        patentClassNumberLink.setCode(code);
        patentClassNumberLink.setPatentId(patentId);
        patentClassNumberLink.setType(type);
        patentClassNumberLink.setMain(1);
        patentClassNumberLink.insert();
    }

    public void updateData(Integer patentId, List<String> classNumber, Integer type) {
        this.deleteByPatentIdAndType(patentId, type, 0);
        List<PatentClassNumberLink> patentClassNumberLinkList = new ArrayList<>();
        for (String code : classNumber.stream().distinct().collect(Collectors.toList())) {
            PatentClassNumberLink patentClassNumberLink = new PatentClassNumberLink();
            patentClassNumberLink.setPatentId(patentId);
            patentClassNumberLink.setType(type);
            patentClassNumberLink.setCode(code);
            patentClassNumberLink.setMain(0);
            patentClassNumberLinkList.add(patentClassNumberLink);
        }
        this.saveOrUpdateBatch(patentClassNumberLinkList);
    }

    public void deleteByPatentIdAndType(Integer patentId, Integer type, Integer main) {
        LambdaQueryWrapper<PatentClassNumberLink> queryWrapper = new LambdaQueryWrapper<>();
        if (main != null) {
            queryWrapper.eq(PatentClassNumberLink::getMain, main);
        }
        queryWrapper.eq(PatentClassNumberLink::getType, type);
        queryWrapper.eq(PatentClassNumberLink::getPatentId, patentId);
        this.remove(queryWrapper);
    }

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