package cn.cslg.report.service.business;

import cn.cslg.report.common.model.dto.LabelDTO;
import cn.cslg.report.common.model.vo.PatentIndexSettingVO;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.common.utils.StringUtils;
import cn.cslg.report.entity.*;
import cn.cslg.report.mapper.ReportFieldPatentLinkMapper;
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.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标引内容关联专利 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-22
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ReportFieldPatentLinkService extends ServiceImpl<ReportFieldPatentLinkMapper, ReportFieldPatentLink> {

    private final ReportFieldTextService reportFieldTextService;
    private final ReportFieldOptionService projectFieldOptionService;
    private final ReportFieldTreeService projectFieldTreeService;
    private final ReportFieldService reportFieldService;
    private final LoginUtils loginUtils;
    private final ReportFieldOptionService reportFieldOptionService;
    private final ReportFieldTreeService reportFieldTreeService;

    public List<ReportFieldPatentLink> getProjectPatentLinkByPatentAndProId(String patentNo, Integer projectId) {
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldPatentLink::getPatentNo, patentNo);
        queryWrapper.eq(ReportFieldPatentLink::getReportId, projectId);
        return this.list(queryWrapper);
    }

    public List<ReportFieldPatentLink> getProjectPatentLinkByPatentAndProjectIdAndFieldId(String patentNo, Integer projectId, Integer fieldId) {
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldPatentLink::getPatentNo, patentNo);
        queryWrapper.eq(ReportFieldPatentLink::getReportId, projectId);
        queryWrapper.eq(ReportFieldPatentLink::getFieldId, fieldId);
        List<ReportFieldPatentLink> list = this.list(queryWrapper);
        int i=list.size();
        return list;
    }

    public ReportFieldPatentLink getProjectPatentLinkByPatentAndProjectIdAndFieldIdAndOptionId(Integer patentId, Integer projectId, Integer fieldId, Integer optionId) {
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldPatentLink::getPatentNo, patentId);
        queryWrapper.eq(ReportFieldPatentLink::getReportId, projectId);
        queryWrapper.eq(ReportFieldPatentLink::getFieldId, fieldId);
        queryWrapper.eq(ReportFieldPatentLink::getOptionId, optionId);
        return this.getOne(queryWrapper);
    }

    public void updateProjectPatentLink(String patentNo, Integer type, Integer fieldId, Integer optionId, Integer userId, Integer projectId) {
        ReportFieldPatentLink reportFieldPatentLink = new ReportFieldPatentLink();
        reportFieldPatentLink.setType(type);
        reportFieldPatentLink.setPatentNo(patentNo);
        reportFieldPatentLink.setOptionId(optionId);
        reportFieldPatentLink.setFieldId(fieldId);
        reportFieldPatentLink.setCreateBy(userId);
        reportFieldPatentLink.setReportId(projectId);
        reportFieldPatentLink.insert();
    }

    public List<ReportFieldPatentLink> getProjectPatentLinkByPatentIdsAndProjectId(List<String> patentNos, Integer projectId) {
        if (patentNos == null || patentNos.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFieldPatentLink::getPatentNo, patentNos);
        queryWrapper.eq(ReportFieldPatentLink::getReportId, projectId);
        return this.list(queryWrapper);
    }

    public List<ReportFieldPatentLink> getProjectPatentLinkByPatentIdsAndFieldId(List<String> patentNos, Integer fieldId) {
        if (patentNos == null || patentNos.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFieldPatentLink::getPatentNo, patentNos);
        queryWrapper.eq(ReportFieldPatentLink::getFieldId, fieldId);
        return this.list(queryWrapper);
    }



    public void deleteByFieldId(Integer fieldId) {
        this.remove(Wrappers.<ReportFieldPatentLink>lambdaQuery().eq(ReportFieldPatentLink::getFieldId, fieldId));
    }

    public void deleteByOptionId(Integer optionId) {
        this.remove(Wrappers.<ReportFieldPatentLink>lambdaQuery().eq(ReportFieldPatentLink::getOptionId, optionId));
    }

    public void deleteByOptionIds(List<Integer> optionIds) {
        if (optionIds != null && optionIds.size() != 0) {
            this.remove(Wrappers.<ReportFieldPatentLink>lambdaQuery().in(ReportFieldPatentLink::getOptionId, optionIds));
        }
    }


    public void deleteByPatentIdsAndProjectIdAndFieldId(List<Integer> patentNos, Integer projectId, Integer fieldId) {
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFieldPatentLink::getPatentNo, patentNos);
        queryWrapper.eq(ReportFieldPatentLink::getReportId, projectId);
        queryWrapper.eq(ReportFieldPatentLink::getFieldId, fieldId);
        this.remove(queryWrapper);
    }
    public void delete(Integer patentId, String patentNo, Integer fieldId) {
        LambdaQueryWrapper<ReportFieldPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldPatentLink::getPatentNo, patentNo)
                .eq(ReportFieldPatentLink::getReportId, patentId)
                .eq(ReportFieldPatentLink::getFieldId, fieldId);
        this.remove(queryWrapper);



    }
    public List<LabelDTO> getPatentIndexSetting(PatentIndexSettingVO params) throws IOException {
        List<ReportFieldPatentLink> projectFieldPatentLinkList = this.getProjectPatentLinkByPatentAndProjectIdAndFieldId(params.getPatentNo(), params.getReportId(), params.getFieldId());
        List<Integer> optionIds = projectFieldPatentLinkList.stream().map(ReportFieldPatentLink::getOptionId).collect(Collectors.toList());
        List<LabelDTO> mapList = new ArrayList<>();
        switch (params.getType()) {
            case 0:
            case 1:
            case 2:
                List<ReportFieldText> projectFieldTextList = reportFieldTextService.getProjectFieldTextByIds(optionIds);
                for (ReportFieldText projectFieldText : projectFieldTextList) {
                    LabelDTO map = new LabelDTO();
                    map.setValue(projectFieldText.getId());
                    map.setLabel(projectFieldText.getText());
                    mapList.add(map);
                }
                break;
            case 3:
            case 4:
            case 5:
                List<ReportFieldOption> projectFieldOptionList = reportFieldOptionService.getFieldOptionListByIds(optionIds);
                for (ReportFieldOption projectFieldOption : projectFieldOptionList) {
                    LabelDTO map = new LabelDTO();
                    map.setValue(projectFieldOption.getId());
                    map.setLabel(projectFieldOption.getName());
                    mapList.add(map);
                }
                break;
            case 6:
                List<ReportFieldTree> projectFieldTreeList = reportFieldTreeService.getProjectFieldTreeOptionByIds(optionIds);
                for (ReportFieldTree projectFieldTree : projectFieldTreeList) {
                    LabelDTO map = new LabelDTO();
                    map.setValue(projectFieldTree.getId());
                    map.setLabel(projectFieldTree.getName());
                    mapList.add(map);
                }
                break;
        }
        return mapList;
    }
    @Transactional(rollbackFor = Exception.class)
    public void updatePatentIndexSetting(PatentIndexSettingVO params) {
        this.delete(params.getReportId(), params.getPatentNo(), params.getFieldId());
        List<Integer> optionIds = params.getSelected().stream().map(LabelDTO::getValue).distinct().collect(Collectors.toList());
        List<String> texts = params.getSelected().stream().map(LabelDTO::getLabel).distinct().collect(Collectors.toList());
        List<ReportFieldPatentLink> projectFieldPatentLinkList = new ArrayList<>();
        switch (params.getType()) {
            case 0:
            case 1:
            case 2:
                List<ReportFieldText> tempList = reportFieldTextService.getProjectFieldTextByFieldId(params.getFieldId());
                for (String text : texts) {
                    if (StringUtils.isEmpty(text)) {
                        continue;
                    }
                    ReportFieldText reportFieldText = tempList.stream().filter(item -> item.getText().equals(text)).findFirst().orElse(null);
                    if (reportFieldText == null) {
                        reportFieldText = new ReportFieldText();
                        reportFieldText.setFieldId(params.getFieldId());
                        reportFieldText.setText(text);
                        reportFieldText.insert();
                    }
                    ReportFieldPatentLink projectFieldPatentLink = new ReportFieldPatentLink();
                    projectFieldPatentLink.setPatentNo(params.getPatentNo());
                    projectFieldPatentLink.setReportId(params.getReportId());
                    projectFieldPatentLink.setType(params.getType());
                    projectFieldPatentLink.setFieldId(params.getFieldId());
                    projectFieldPatentLink.setCreateBy(loginUtils.getId());
                    projectFieldPatentLink.setOptionId(reportFieldText.getId());
                    projectFieldPatentLinkList.add(projectFieldPatentLink);
                }
                break;
            case 3:
            case 4:
            case 5:
            case 6:
                for (Integer optionId : optionIds) {
                    ReportFieldPatentLink projectFieldPatentLink = new ReportFieldPatentLink();
                    projectFieldPatentLink.setPatentNo(params.getPatentNo());
                    projectFieldPatentLink.setReportId(params.getReportId());
                    projectFieldPatentLink.setType(params.getType());
                    projectFieldPatentLink.setFieldId(params.getFieldId());
                    projectFieldPatentLink.setCreateBy(loginUtils.getId());
                    projectFieldPatentLink.setOptionId(optionId);
                    projectFieldPatentLinkList.add(projectFieldPatentLink);
                }
                break;
        }
        if (projectFieldPatentLinkList.size() != 0) {
            this.saveOrUpdateBatch(projectFieldPatentLinkList);
        }

    }
    public List<ReportFieldPatentLink> get(int id) throws IOException{
        LambdaQueryWrapper<ReportFieldPatentLink> linkLambdaQueryWrapper=new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(ReportFieldPatentLink::getReportId,id);
        return this.list(linkLambdaQueryWrapper);
    }






}
