package cn.cslg.pas.service;

import cn.cslg.pas.common.model.params.PatentLabelParams;
import cn.cslg.pas.domain.PatentLabel;
import cn.cslg.pas.common.model.vo.PatentCustomFieldSettingVO;
import cn.cslg.pas.mapper.PatentLabelMapper;
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 org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 专利信息标签表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-03-07
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentLabelService extends ServiceImpl<PatentLabelMapper, PatentLabel> {

    public List<PatentLabel> getPatentLabelByPatentIdAndProjectId(Integer patentId, Integer projectId) {
        LambdaQueryWrapper<PatentLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatentLabel::getPatentId, patentId);
        queryWrapper.eq(PatentLabel::getProjectId, projectId);
        return this.list(queryWrapper);
    }

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

    public void deleteByProjectIdAndPatentId(Integer projectId, Integer patentId) {
        this.remove(Wrappers.<PatentLabel>lambdaQuery().eq(PatentLabel::getPatentId, patentId).eq(PatentLabel::getProjectId, projectId));
    }

    @Transactional
    public void updatePatentLabel(PatentCustomFieldSettingVO params) {
        this.deleteByProjectIdAndPatentId(params.getProjectId(), params.getPatentId());
        List<PatentLabel> patentLabelList = new ArrayList<>();
        for (String label : params.getSelected()) {
            PatentLabel patentLabel = new PatentLabel();
            patentLabel.setPatentId(params.getPatentId());
            patentLabel.setProjectId(params.getProjectId());
            patentLabel.setName(label);
            patentLabelList.add(patentLabel);
        }
        if (patentLabelList.size() != 0) {
            this.saveOrUpdateBatch(patentLabelList);
        }
    }

    public void updatePatentLabel(PatentLabelParams params) {
        this.deleteByProjectIdAndPatentId(params.getProjectId(), params.getPatentId());
        if (params.getLabel() == null || params.getLabel().size() == 0) {
            return;
        }
        for (String name : params.getLabel()) {
            PatentLabel patentLabel = new PatentLabel();
            patentLabel.setPatentId(params.getPatentId());
            patentLabel.setProjectId(params.getProjectId());
            patentLabel.setName(name);
            patentLabel.insert();
        }
    }

}
