package cn.cslg.pas.service;

import cn.cslg.pas.domain.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 javax.sound.sampled.Port;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 用来复制的service
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CopyService {
        private final ProjectFieldService projectFieldService;
        private final ProjectFieldTextService projectFieldTextService;
        private final ProjectFieldPatentLinkService projectFieldPatentLinkService;
        private final ProjectFieldOptionService projectFieldOptionService;
        private final ProjectFieldTreeService projectFieldTreeService;
        private final ProjectFolderService projectFolderService;
        private final ProjectFolderPatentLinkService projectFolderPatentLinkService;
    //复制标引信息
    @Transactional(rollbackFor = Exception.class)
    public void CopyPatentLabels(Integer oldProjectId,Integer newProjectId) {
        //复制父报告自定义栏位
        LambdaQueryWrapper<ProjectField> fieldWrapper = new LambdaQueryWrapper<>();
        fieldWrapper.eq(ProjectField::getProjectId,oldProjectId);
        List<ProjectField> fields = projectFieldService.list(fieldWrapper);
        String re = JSONObject.toJSON(fields).toString();
        List<ProjectField> oldFields = JSONArray.parseArray(re, ProjectField.class);
        if (fields.size() != 0) {
            for (ProjectField reportField : fields) {
                reportField.setId(null);
                reportField.setProjectId(newProjectId);
            }
            projectFieldService.saveBatch(fields);
        }
        for (int i = 0; i < oldFields.size(); i++) {
            ProjectField field = oldFields.get(i);
            ProjectField newField = fields.get(i);
            //当字段类型为文本类型时
            if (Arrays.asList(1, 2).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ProjectFieldText> textWrapper = new LambdaQueryWrapper<>();
                textWrapper.eq(ProjectFieldText::getFieldId, field.getId());
                List<ProjectFieldText> fieldTexts = projectFieldTextService.list(textWrapper);
                //遍历复制值
                for (ProjectFieldText texts : fieldTexts) {
                    Integer oldId = texts.getId();
                    texts.setId(null);
                    texts.setFieldId(newField.getId());
                    texts.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ProjectFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ProjectFieldPatentLink::getFieldId, field.getId())
                            .eq(ProjectFieldPatentLink::getOptionId, oldId)
                            .eq(ProjectFieldPatentLink::getProjectId, field.getProjectId());
                    List<ProjectFieldPatentLink> links = projectFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(texts.getId());
                        item.setProjectId(newField.getProjectId());
                        item.setId(null);
                    });
                    projectFieldPatentLinkService.saveBatch(links);
                }
            }
            //当字段类型为选项类型时
            if (Arrays.asList(4, 5).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ProjectFieldOption> optionWrapper = new LambdaQueryWrapper<>();
                optionWrapper.eq(ProjectFieldOption::getFieldId, field.getId());
                List<ProjectFieldOption> fieldOptions = projectFieldOptionService.list(optionWrapper);
                //遍历复制值
                for (ProjectFieldOption options : fieldOptions) {
                    Integer oldId = options.getId();
                    options.setId(null);
                    options.setFieldId(newField.getId());
                    options.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ProjectFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ProjectFieldPatentLink::getFieldId, field.getId())
                            .eq(ProjectFieldPatentLink::getOptionId, oldId)
                            .eq(ProjectFieldPatentLink::getProjectId, field.getProjectId());
                    List<ProjectFieldPatentLink> links = projectFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(options.getId());
                        item.setProjectId(newField.getProjectId());
                        item.setId(null);
                    });
                    projectFieldPatentLinkService.saveBatch(links);
                }
            }
            //当字段类型为树类型时
            if (Arrays.asList(6).contains(field.getType())) {
                //获得自定义字段值
                LambdaQueryWrapper<ProjectFieldTree> treeWrapper = new LambdaQueryWrapper<>();
                treeWrapper.eq(ProjectFieldTree::getFieldId, field.getId());
                List<ProjectFieldTree> fieldTrees = projectFieldTreeService.list(treeWrapper);
                //遍历复制值
                for (ProjectFieldTree tree : fieldTrees) {
                    Integer oldId = tree.getId();
                    tree.setId(null);
                    tree.setFieldId(newField.getId());
                    tree.insert();
                    //复制值和专利关系
                    LambdaQueryWrapper<ProjectFieldPatentLink> linkWrapper = new LambdaQueryWrapper<>();
                    linkWrapper.eq(ProjectFieldPatentLink::getFieldId, field.getId())
                            .eq(ProjectFieldPatentLink::getOptionId, oldId)
                            .eq(ProjectFieldPatentLink::getProjectId, field.getProjectId());
                    List<ProjectFieldPatentLink> links = projectFieldPatentLinkService.list(linkWrapper);
                    links.forEach(item -> {
                        item.setFieldId(newField.getId());
                        item.setOptionId(tree.getId());
                        item.setProjectId(newField.getProjectId());
                        item.setId(null);
                    });
                    projectFieldPatentLinkService.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);
                    });
                }
                projectFieldTreeService.updateBatchById(fieldTrees);
            }
        }
    }

    /**
     * 复制专题库文件夹
     */
    public void CopyProjectFolder(Integer oldProjectId,Integer newProjectId){
        LambdaQueryWrapper<ProjectFolder> projectFolderWrapper = new LambdaQueryWrapper<>();
        projectFolderWrapper.eq(ProjectFolder::getProjectId, oldProjectId);
        List<ProjectFolder> projectFolderList = projectFolderService.list(projectFolderWrapper);
        projectFolderList.forEach(
                item -> {
                    Integer oldFolderId =item.getId();
                    item.setProjectId(newProjectId);
                    item.setId(null);
                    item.insert();
                    projectFolderList.forEach(tem->{
                        if(tem.getParentId().equals(oldFolderId)){
                            tem.setParentId(item.getId());
                        }
                    });
                    LambdaQueryWrapper<ProjectFolderPatentLink> patentLinkWrapper = new LambdaQueryWrapper<>();
                    patentLinkWrapper.eq(ProjectFolderPatentLink::getProjectId, oldProjectId)
                            .eq(ProjectFolderPatentLink::getFolderId,oldFolderId);
                    List<ProjectFolderPatentLink> linkList = projectFolderPatentLinkService.list(patentLinkWrapper);
                    linkList.forEach(
                            tem -> {
                                tem.setProjectId(newProjectId);
                                tem.setId(null);
                                tem.setFolderId(item.getId());
                            }
                    );
                    projectFolderPatentLinkService.saveBatch(linkList);
                }
        );

        projectFolderService.updateBatchById(projectFolderList);

    }
}