package cn.cslg.pas.service;

import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
import cn.cslg.pas.domain.ProjectFolderPatentLink;
import cn.cslg.pas.common.model.vo.ProjectFolderPatentVO;
import cn.cslg.pas.mapper.ProjectFolderPatentLinkMapper;
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;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件夹管理表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-22
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectFolderPatentLinkService extends ServiceImpl<ProjectFolderPatentLinkMapper, ProjectFolderPatentLink> {

    public List<ProjectFolderPatentLink> getProjectFolderLinkListByProId(Integer projectId) {
        LambdaQueryWrapper<ProjectFolderPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectFolderPatentLink::getProjectId, projectId);
        return this.list(queryWrapper);
    }

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

    public List<ProjectFolderPatentLink> getProjectFolderLinkByProjectIdAndFolder(Integer projectId, List<Integer> folders) {
        LambdaQueryWrapper<ProjectFolderPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectFolderPatentLink::getProjectId, projectId);
        if (folders != null && folders.size() != 0) {
            queryWrapper.in(ProjectFolderPatentLink::getFolderId, folders);
        }
        return this.list(queryWrapper);
    }

    public List<ProjectFolderPatentLink> getProjectFolderLinkByProjectIdAndFolder(Integer projectId, Integer folder) {
        LambdaQueryWrapper<ProjectFolderPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectFolderPatentLink::getProjectId, projectId);
        queryWrapper.eq(ProjectFolderPatentLink::getFolderId, folder);
        return this.list(queryWrapper);
    }

    public ProjectFolderPatentLink getProjectFolderLinkByProjectIdAndFolderAndPatentId(Integer projectId, Integer folder, Integer patentId) {
        LambdaQueryWrapper<ProjectFolderPatentLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectFolderPatentLink::getProjectId, projectId);
        queryWrapper.eq(ProjectFolderPatentLink::getFolderId, folder);
        queryWrapper.eq(ProjectFolderPatentLink::getPatentId, patentId);
        return this.getOne(queryWrapper);
    }

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

    public void updateProjectFolderLink(ProjectImportPatentVO params, Integer patentId) {
        if (params.getFolderIds() != null && params.getFolderIds().size() != 0) {
            List<ProjectFolderPatentLink> tempList = this.getProjectFolderLinkListByProId(params.getProjectId());
            params.getFolderIds().forEach(folderId -> {
                long count = tempList.stream().filter(temp -> temp.getPatentId().equals(patentId) && temp.getFolderId().equals(folderId)).count();
                if (count == 0) {
                    ProjectFolderPatentLink projectFolderPatentLink = new ProjectFolderPatentLink();
                    projectFolderPatentLink.setProjectId(params.getProjectId());
                    projectFolderPatentLink.setPatentId(patentId);
                    projectFolderPatentLink.setFolderId(folderId);
                    projectFolderPatentLink.insert();
                }
            });
        }
    }

    public void deleteByProjectId(Integer projectId) {
        this.remove(Wrappers.<ProjectFolderPatentLink>lambdaQuery().eq(ProjectFolderPatentLink::getProjectId, projectId));
    }

    public void deleteByProjectIdAndPatentIds(Integer projectId, List<Integer> patentIds) {
        this.remove(Wrappers.<ProjectFolderPatentLink>lambdaQuery().eq(ProjectFolderPatentLink::getProjectId, projectId).in(ProjectFolderPatentLink::getPatentId, patentIds));
    }

    public void deleteByFolderIdAndPatentIds(Integer folderId, List<Integer> patentIds) {
        this.remove(Wrappers.<ProjectFolderPatentLink>lambdaQuery().eq(ProjectFolderPatentLink::getFolderId, folderId).in(ProjectFolderPatentLink::getPatentId, patentIds));
    }

    public void deleteByFolderIds(List<Integer> folderIds, Integer projectId) {
        if (folderIds != null && folderIds.size() != 0) {
            this.remove(Wrappers.<ProjectFolderPatentLink>lambdaQuery().in(ProjectFolderPatentLink::getFolderId, folderIds).eq(ProjectFolderPatentLink::getProjectId, projectId));
        }
    }

    public void deleteByProjectIdAndFolderIdAndPatentIds(Integer projectId, List<Integer> folderId, List<Integer> patentIds) {
        if (patentIds != null && patentIds.size() != 0 && folderId != null && folderId.size() != 0) {
            this.remove(Wrappers.<ProjectFolderPatentLink>lambdaQuery().eq(ProjectFolderPatentLink::getProjectId, projectId).in(ProjectFolderPatentLink::getFolderId, folderId).in(ProjectFolderPatentLink::getPatentId, patentIds));
        }
    }

    @Transactional
    public void updateProjectFolderPatent(ProjectFolderPatentVO params) {
        List<ProjectFolderPatentLink> projectFolderPatentLinkList = new ArrayList<>();
        List<ProjectFolderPatentLink> tempList = this.getProjectFolderLinkByProjectIdAndFolder(params.getProjectId(), params.getNewFolder());
        for (Integer newFolder : params.getNewFolder()) {
            List<Integer> ids = params.getPatentIds().stream().filter(item -> !tempList.stream().filter(temp -> temp.getFolderId().equals(newFolder)).map(ProjectFolderPatentLink::getPatentId).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
            projectFolderPatentLinkList.addAll(ids.stream().map(patentId -> {
                ProjectFolderPatentLink projectFolderPatentLink = new ProjectFolderPatentLink();
                projectFolderPatentLink.setFolderId(newFolder);
                projectFolderPatentLink.setProjectId(params.getProjectId());
                projectFolderPatentLink.setPatentId(patentId);
                projectFolderPatentLink.setLook(0);
                return projectFolderPatentLink;
            }).collect(Collectors.toList()));
        }
        if (params.getType().equals(0)) {
            this.deleteByProjectIdAndFolderIdAndPatentIds(params.getProjectId(), params.getOldFolder(), params.getPatentIds());
        }
        if (projectFolderPatentLinkList.size() != 0) {
            this.saveOrUpdateBatch(projectFolderPatentLinkList);
        }
    }
}
