package cn.cslg.report.service.business;

import cn.cslg.report.common.model.vo.PatentField;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.*;
import cn.cslg.report.mapper.TemplateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用来复制的service
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CopyService {
    private final CompareFilesService compareFilesService;
    private final ReportFieldTextService reportFieldTextService;
    private final ReportFieldOptionService reportFieldOptionService;
    private final ReportFieldTreeService reportFieldTreeService;
    private final ReportFieldPatentLinkService reportFieldPatentLinkService;
    //复制对比文件
    @Transactional(rollbackFor = Exception.class)
    public Boolean CopyComparePatents(Integer oldReportId, Integer newReportId) {
        //根据报告id获得对比文件列表
        LambdaQueryWrapper<CompareFiles> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompareFiles::getReportId, oldReportId);
        List<CompareFiles> compareFilesList = compareFilesService.list(wrapper);
        if (compareFilesList.size() != 0) {
            for (CompareFiles compareFile : compareFilesList) {
                compareFile.setId(null);
                compareFile.setReportId(newReportId);
            }
            return compareFilesService.saveBatch(compareFilesList);
        }
        return true;
    }
    //复制标引信息
    @Transactional(rollbackFor = Exception.class)
    public void CopyPatentLabels(List<ReportField> oldFields, List<ReportField> newFields) {
        for (int i = 0; i < oldFields.size(); i++) {
            ReportField field = oldFields.get(i);
            ReportField newField = newFields.get(i);
            //当字段类型为文本类型时
            if (Arrays.asList(1, 2).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ReportFieldText> textWrapper = new LambdaQueryWrapper<>();
                textWrapper.eq(ReportFieldText::getFieldId, field.getId());
                List<ReportFieldText> fieldTexts = reportFieldTextService.list(textWrapper);
                //遍历复制值
                for (ReportFieldText texts : fieldTexts) {
                    Integer oldId = texts.getId();
                    texts.setId(null);
                    texts.setFieldId(newField.getId());
                    texts.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ReportFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ReportFieldPatentLink::getFieldId, field.getId())
                            .eq(ReportFieldPatentLink::getOptionId, oldId)
                            .eq(ReportFieldPatentLink::getReportId, field.getReportId());
                    List<ReportFieldPatentLink> links = reportFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(texts.getId());
                        item.setReportId(newField.getReportId());
                        item.setId(null);
                    });
                    reportFieldPatentLinkService.saveBatch(links);
                }
            }
            //当字段类型为选项类型时
            if (Arrays.asList(4, 5).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ReportFieldOption> optionWrapper = new LambdaQueryWrapper<>();
                optionWrapper.eq(ReportFieldOption::getFieldId, field.getId());
                List<ReportFieldOption> fieldOptions = reportFieldOptionService.list(optionWrapper);
                //遍历复制值
                for (ReportFieldOption options : fieldOptions) {
                    Integer oldId = options.getId();
                    options.setId(null);
                    options.setFieldId(newField.getId());
                    options.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ReportFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ReportFieldPatentLink::getFieldId, field.getId())
                            .eq(ReportFieldPatentLink::getOptionId, oldId)
                            .eq(ReportFieldPatentLink::getReportId, field.getReportId());
                    List<ReportFieldPatentLink> links = reportFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(options.getId());
                        item.setReportId(newField.getReportId());
                        item.setId(null);
                    });
                    reportFieldPatentLinkService.saveBatch(links);
                }
            }
            //当字段类型为树类型时
            if (Arrays.asList(6).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ReportFieldTree> treeWrapper = new LambdaQueryWrapper<>();
                treeWrapper.eq(ReportFieldTree::getFieldId, field.getId());
                List<ReportFieldTree> fieldTrees = reportFieldTreeService.list(treeWrapper);
                //遍历复制值
                for (ReportFieldTree tree : fieldTrees) {
                    Integer oldId = tree.getId();
                    tree.setId(null);
                    tree.setFieldId(newField.getId());
                    tree.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ReportFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ReportFieldPatentLink::getFieldId, field.getId())
                            .eq(ReportFieldPatentLink::getOptionId, oldId)
                            .eq(ReportFieldPatentLink::getReportId, field.getReportId());
                    List<ReportFieldPatentLink> links = reportFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(tree.getId());
                        item.setReportId(newField.getReportId());
                        item.setId(null);
                    });
                    reportFieldPatentLinkService.saveBatch(links);
                    //更新
                    fieldTrees.forEach(item -> {
                        if (item.getParentId().equals(oldId)) {
                            item.setParentId(tree.getId());
                        }
                        String[] strs = item.getPath().split("/");
                        List<String> lists = new ArrayList<>(Arrays.asList(strs));
                        Collections.replaceAll(lists, oldId + "", tree.getId() + "");
                        String paths = String.join("/", lists);
                        item.setPath(paths);
                    });
                }
                reportFieldTreeService.updateBatchById(fieldTrees);
            }
        }
    }


}