package cn.cslg.report.service.business;


import cn.cslg.report.common.model.dto.TaskTDO;
import cn.cslg.report.common.model.vo.*;
import cn.cslg.report.common.model.vo.queryVo.QueryCMVO;
import cn.cslg.report.common.utils.*;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.common.utils.message.MessageUtils;
import cn.cslg.report.entity.*;
import cn.cslg.report.entity.asso.AssoTaskPersonel;
import cn.cslg.report.entity.asso.SyneryCode;
import cn.cslg.report.entity.asso.Synery_CC;
import cn.cslg.report.mapper.TaskMapper;
import cn.cslg.report.service.OutInterfaceService;
import cn.cslg.report.service.business.asso.SyneryCCService;
import cn.cslg.report.service.business.asso.SyneryCodeService;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class TaskService extends ServiceImpl<TaskMapper, Task> {
    public final ReportService reportService;
    public final OutInterfaceService outInterfaceService;
    public final CacheUtils cacheUtils;
    public final CompareFilesService compareFilesService;
    public final AssoTaskPersonelService assoTaskPersonelService;
    public final ReportFieldService reportFieldService;
    public final MessageUtils mailUtils;
    public final LoginUtils loginUtils;
    public final SyneryCCService syneryCCService;
    public final CompareRecordsService compareRecordsService;
    public final CompareMessageService compareMessageService;
    public final SyneryCodeService syneryCodeService;
    @Value("${PASUrl}")
    private String url;

    /**
     * @param id(Task 任务id)
     * @return
     * @function: 根据任务id 删除任务id
     */
    @Transactional(rollbackFor = Exception.class)
    public String deleteTask(Integer id) {
        try {
            this.removeById(id);
            return Response.success();
        } catch (Exception e) {
            log.error("删除任务异常,异常信息:" + LogExceptionUtil.getMessage(e));
            log.info("删除任务异常，开始回滚! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("回滚完毕! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return Response.error();
        }
    }

    /**
     * @param id(Task 任务id)
     * @return
     * @function: 根据任务id 删除任务id
     */

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTaskByreportId(Integer id) {
        try {
            LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Task::getReportId, id);
            //拿到对象，根据对象判断任务类型
            List<Task> list = this.list(queryWrapper);
            if (list.size() != 0) {
                for (Task task : list) {
                    if (task.getType().equals(1)) {
                        syneryCCService.deleteByTaskId(task.getId());
                        syneryCodeService.deleteByTaskId(task.getId());

                    }
                    if (task.getType().equals(2)) {
                        //删除对比记录
                        compareRecordsService.deleteByTaskId(task.getId());

                    }
                }
            }

            return this.remove(queryWrapper);
        } catch (Exception e) {
            log.error("删除任务异常,异常信息:" + LogExceptionUtil.getMessage(e));
            log.info("删除任务异常，开始回滚! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("回滚完毕! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return false;
        }
    }

    /**
     * @param Task
     * @return
     * @function: 修改导入任务
     */
    @Transactional(rollbackFor = Exception.class)
    public String updateTask(Task task) {
        try {
            this.updateById(task);
            return Response.success();
        } catch (Exception e) {
            log.error("修改任务异常,异常信息:" + LogExceptionUtil.getMessage(e));
            log.info("修改任务异常，开始回滚! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("回滚完毕! 时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return Response.error();
        }
    }

    /**
     * @param taskVO
     * @return
     * @function 查询任务
     */
    public IPage<TaskReportVO> queryPageList(TaskVO taskVO) throws IOException {
        taskVO.setLoginId(StpUtil.getLoginIdAsInt());
        if (taskVO.getRoleInTask() == 0 && taskVO.getTaskStatus() == 0) {
            taskVO.setTaskStatuses(Arrays.asList(0, 2));
        } else if (taskVO.getRoleInTask() == 0 && taskVO.getTaskStatus() == 1) {
            taskVO.setTaskStatuses(Arrays.asList(1));
        } else if (taskVO.getRoleInTask() == 1 && taskVO.getTaskStatus() == 0) {
            taskVO.setTaskStatuses(Arrays.asList(0));
        } else if (taskVO.getRoleInTask() == 1 && taskVO.getTaskStatus() == 1) {
            taskVO.setTaskStatuses(Arrays.asList(1, 2));
        }
        IPage<TaskReportVO> dataPage = new Page<>();
        //当查询使用发起人名称时
        if (taskVO.getCreateName() != null && taskVO.getCreateName() != "") {
            String res = outInterfaceService.getPersonIdByNamePCS(taskVO.getCreateName());
            List<Integer> createIds = JSONArray.parseArray(res, Integer.class);
            taskVO.setIds(createIds);
            //当未查询到时直接返回空页
            if (createIds == null || createIds.size() == 0) {
                taskVO.setIds(Arrays.asList(-1));
            }
        }
        //当查询使用处理人名称时
        if (taskVO.getHandlePersonName() != null && taskVO.getHandlePersonName() != "") {
            String res = outInterfaceService.getPersonIdByNamePCS(taskVO.getHandlePersonName());
            List<Integer> handleIds = JSONArray.parseArray(res, Integer.class);
            taskVO.setHandleIds(handleIds);
            //当未查询到时直接返回空页
            if (handleIds == null || handleIds.size() == 0) {
                taskVO.setIds(Arrays.asList(-1));
            }
        }
        dataPage = baseMapper.queryTasks(new Page<>(taskVO.getCurrent(), taskVO.getSize()), taskVO);
        List<TaskReportVO> taskReportVOS = dataPage.getRecords();
        taskReportVOS = this.reTaskReportVOs(taskReportVOS);
        dataPage.setRecords(taskReportVOS);
        return dataPage;
    }

    public List<TaskReportVO> reTaskReportVOs(List<TaskReportVO> taskReportVOS) throws IOException {
        if (taskReportVOS.size() != 0) {
            //创建人Id
            List<Integer> createIds = taskReportVOS.stream().map(TaskReportVO::getCreateID).collect(Collectors.toList());
            //处理人Id
            List<Integer> handleIds = taskReportVOS.stream().map(TaskReportVO::getHandlePersonId).collect(Collectors.toList());
            createIds.addAll(handleIds);
            String res1 = outInterfaceService.getPersonnelByIdsFromPCS(createIds);
            JSONObject jsonObject1 = JSONObject.parseObject(res1);
            //解析jason获得标的专利的权要
            List<Personnel> personnels = JSON.parseArray(jsonObject1.getString("data"), Personnel.class);

            for (TaskReportVO item : taskReportVOS) {
                //装载创建人名和负责人名
                List<Personnel> mapCre = personnels.stream().filter(tem -> tem.getId().equals(item.getCreateID())).collect(Collectors.toList());
                List<Personnel> mapDuty = personnels.stream().filter(tem -> tem.getId().equals(item.getHandlePersonId())).collect(Collectors.toList());
                if (mapCre.size() != 0)
                    item.setCreateName(mapCre.get(0).getPersonnelName());
                if (mapDuty.size() != 0) {
                    item.setHandlePersonName(mapDuty.get(0).getPersonnelName());
                }

            }
        }
        return taskReportVOS;
    }

    /**
     * @function 获取对比任务
     */
    public String getAllTask() {
        List<Task> taskList = this.list();
        return Response.success(taskList);
    }

    //添加审核任务
    @Transactional(rollbackFor = Exception.class)
    public String addTask(String taskVOStr, List<MultipartFile> file) throws ParseException, IOException {
        TaskVO taskVO = JSONObject.parseObject(taskVOStr, TaskVO.class);
        //先添加一个报告
        Integer reportId = reportService.addReport(taskVO.getReport(), file);

        //数据库导入task

        //任务人员关联表导入数据
        if (taskVO.getPersonIds() != null) {
            List<Task> tasks = new ArrayList<>();
            List<Integer> ids = taskVO.getPersonIds();
            String resBody = outInterfaceService.getPersonnelByIdsFromPCS(ids);
            JSONObject jsonObject = JSONObject.parseObject(resBody);
            //解析jason获得标的专利的权要
            List<Personnel> personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
            //获得登录人的信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(StpUtil.getLoginIdAsInt());
            for (int i = 0; i < ids.size(); i++) {
                Integer handlePersonId = ids.get(i);
                Personnel personnel = new Personnel();
                personnel = personnels.stream().filter(item -> item.getId().equals(handlePersonId)).findFirst().orElse(new Personnel());
                Task task = new Task();
                task.setReportId(reportId);
                task.setRemark(taskVO.getRemark());
                task.setTaskName(taskVO.getTaskName());
                task.setBeginTime(taskVO.getBeginTime());
                task.setEndTime(taskVO.getEndTime());
                task.setHandlePersonId(handlePersonId);
                task.setHandlePersonName(personnel.getPersonnelName());
                task.setType(taskVO.getType());
                task.setCreateID(personnelVO.getId());
                task.setCreateName(personnelVO.getName());
                task.setTaskStatus(0);
                task.setPersonEmail(personnel.getPersonnelEmail());
                tasks.add(task);
            }
            if (this.saveBatch(tasks)) {
                for (Task tem : tasks) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("title", "任务通知");
                    map.put("template", "mail/eTask.html");
                    map.put("value1", tem.getHandlePersonName());
                    map.put("img", "\\src\\main\\resources\\mail\\logo.png");
                    map.put("email", tem.getPersonEmail());
                    map.put("value2", tem.getCreateName());
                    map.put("value3", "审核任务");
                    map.put("value4", tem.getEndTime());
                    map.put("value5", "http://139.224.24.90:8086/");
                    mailUtils.sendEmailMessage(map);

                }
            }
            ;
        }
        return Response.success();
    }

    //审核任务
    public String auditTask(AuditTaskVO auditTaskVO) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getId, auditTaskVO.getTaskId());
        List<Task> tasks = this.list(queryWrapper);
        if (tasks.size() == 0 || tasks == null) {
            return Response.error("未查到任务");
        }
        Task task = tasks.get(0);
        LambdaQueryWrapper<Report> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Report::getId, task.getReportId());
        List<Report> reports = reportService.list(queryWrapper1);
        if (reports.size() == 0 || reports == null) {
            return Response.error("未查到报告");
        }
        Report report = reports.get(0);
        task.setTaskStatus(1);
        task.setResult(auditTaskVO.getResult());
        //task.setRemark(auditTaskVO.getRemark());
        Date date = new Date();
        task.setFinishTime(date);
        task.updateById();
        report.setStatus(auditTaskVO.getReportStatus());
        report.updateById();

        return Response.success();
    }


    //添加分配任务
    @Transactional(rollbackFor = Exception.class)
    public String addAssTask(AssignTaskVO assignTaskVO) throws ParseException, IOException {
        PatentVO patentVO = new PatentVO();
        if (assignTaskVO.getPatentVO() != null) {
            patentVO = assignTaskVO.getPatentVO();
        }
        LambdaQueryWrapper<Report> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Report::getId, assignTaskVO.getReportId());
        Report report = reportService.list(wrapper1).get(0);
        //添加分配任务时将报告的状态置为创建中
        if (report.getStatus() != 2) {
            report.setStatus(2);
            report.updateById();
        }
        String signPatentNO = report.getSignPatentNo();
        //遍历assignTaskVO的分配人员信息assigns，在任务表里插入分配任务
        List<String> selectedNos = new ArrayList<>();
        //记录所有人的id
        List<Integer> personIds = new ArrayList<>();
        //记录总共专利号数量
        int num = 0;
        List<AssignTaskVO.assignPatents> personPatents = assignTaskVO.getAssigns();
        for (int i = 0; i < personPatents.size(); i++) {
            selectedNos.addAll(personPatents.get(i).getPatentNos());
            num += personPatents.get(i).getAssignCount();
            personIds.add(personPatents.get(i).getPersonId());
        }
        String resBody = outInterfaceService.getPersonnelByIdsFromPCS(personIds);
        JSONObject jsonObject = JSONObject.parseObject(resBody);
        //解析jason获得标的专利的权要
        List<Personnel> personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
        //获得登录人的信息
        PersonnelVO personnelVO = cacheUtils.getLoginUser(StpUtil.getLoginIdAsInt());
        num += selectedNos.size();
        // 根据报告Id获得待分配的专利号
        LambdaQueryWrapper<CompareFiles> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CompareFiles::getPatentNo)
                .eq(CompareFiles::getReportId, assignTaskVO.getReportId())
                .eq(CompareFiles::getState, 0);
        List<CompareFiles> compareFiles = compareFilesService.list(wrapper);
        List<String> AllpatentNos = compareFiles.stream().map(CompareFiles::getPatentNo).collect(Collectors.toList());
        List<String> patentNos1 =new ArrayList<>();
        //无查询条件
        if (patentVO == null || patentVO.getFiledOptions() == null || patentVO.getFiledOptions().size() == 0) {
            patentNos1 =AllpatentNos;
        }
        //有查询条件时，自定义字段的专利号和任务的专利号取交集
        else {
            List<String> temPatentNos = reportFieldService.getPatentNo1(patentVO.getFiledOptions(), patentVO.getReportId(), patentVO.getTaskId(), patentVO.getTaskStatus());
            patentNos1=temPatentNos;
        }
        //如果有任务过滤条件
        List<String> patentNosTask = new ArrayList<>();
        if (patentVO.getTaskSources() != null && patentVO.getTaskSources().size() != 0) {
            patentNosTask = assoTaskPersonelService.getBaseMapper().getPatents(patentVO.getTaskSources());
        }
        List<String> allPatentsCopy = new ArrayList<>();
        if (patentVO.getNotAssignTypes() != null && patentVO.getNotAssignTypes().size() != 0) {
            for(Integer types :patentVO.getNotAssignTypes())
            {
                List<String>  PatentsCopy = new ArrayList<>(AllpatentNos);
                LambdaQueryWrapper<AssoTaskPersonel> assoWrapper = new LambdaQueryWrapper<>();
                assoWrapper.eq(AssoTaskPersonel::getReportId, patentVO.getReportId())
                        .eq(AssoTaskPersonel::getTaskType, types);
                List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(assoWrapper);
                List<String> tem = assoTaskPersonels.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
                PatentsCopy.removeAll(tem);
                allPatentsCopy.addAll(PatentsCopy);
            }

        }
        if ((patentVO.getTaskSources() != null && patentVO.getTaskSources().size() != 0) || (patentVO.getNotAssignTypes() != null && patentVO.getNotAssignTypes().size() != 0)) {
            patentNosTask.addAll(allPatentsCopy);
            patentNos1 = new ArrayList<>(CollectionUtils.intersection(patentNosTask, patentNos1));
        }
        patentVO.setPatentNos(patentNos1);
        String res = outInterfaceService.getConPantentNos(patentVO, 1);
        JSONObject jsonObject1 = JSONObject.parseObject(res);
        List<String> patentNos = JSONArray.parseArray(jsonObject1.get("data").toString(), String.class);
        //待分配的专利号列表剔除分配信息中已经存在的专利号
        patentNos.removeAll(selectedNos);
        List<AssoTaskPersonel> assoTaskPersonels = new ArrayList<>();
        //记录分配的位置
        int flag = 0;
        //遍历assignTaskVO的分配人员信息assigns，装配人员所被分配的专利信息
        for (int i = 0; i < personPatents.size(); i++) {
            Integer handlePersonId = personIds.get(i);
            Personnel personnel = personnels.stream().filter(item -> item.getId().equals(handlePersonId)).findFirst().orElse(new Personnel());
            Task task = new Task();
            task.setTaskName(assignTaskVO.getTaskName());
            task.setReportId(assignTaskVO.getReportId());
            task.setType(assignTaskVO.getTaskType());
            task.setTaskStatus(0);
            task.setProgress(0.0);
            task.setEndTime(assignTaskVO.getEndTime());
            task.setRemark(assignTaskVO.getRemark());
            task.setHandlePersonId(handlePersonId);
            task.setHandlePersonName(personnel.getPersonnelName());
            task.setCreateID(personnelVO.getId());
            task.setCreateName(personnelVO.getName());
            task.setSignPatentNo(signPatentNO);
            //发送邮件
            if (task.insert()) {
                Map<String, Object> map = new HashMap<>();
                map.put("title", "任务通知");
                map.put("template", "mail/eTask.html");
                map.put("value1", task.getHandlePersonName());
                map.put("img", "\\src\\main\\resources\\mail\\logo.png");
                map.put("email", task.getPersonEmail());
                map.put("value2", task.getCreateName());
                map.put("value3", "分配任务");
                map.put("value4", task.getEndTime());
                map.put("value5", "http://139.224.24.90:8086/");
                mailUtils.sendEmailMessage(map);
            }
            //根据数量从分配的专利号列表中截取该数量的专利号，加上分配信息的专利号进行装配
            List<String> partNos = new ArrayList<>();
            if (personPatents.get(i).getPatentNos() == null || personPatents.get(i).getPatentNos().size() == 0) {
                partNos = patentNos.subList(flag, flag + personPatents.get(i).getAssignCount());
                flag += personPatents.get(i).getAssignCount();
            }
            partNos.addAll(personPatents.get(i).getPatentNos());
            for (int t = 0; t < partNos.size(); t++) {
                AssoTaskPersonel assoTaskPersonel = new AssoTaskPersonel();
                assoTaskPersonel.setTaskId(task.getId());
                assoTaskPersonel.setTaskType(2);
                assoTaskPersonel.setPatentNo(partNos.get(t));
                assoTaskPersonel.setPersonelId(personPatents.get(i).getPersonId());
                assoTaskPersonel.setPersonelType(1);
                assoTaskPersonel.setReportId(assignTaskVO.getReportId());
                assoTaskPersonels.add(assoTaskPersonel);
            }
        }
        // 分配信息入库
        assoTaskPersonelService.saveBatch(assoTaskPersonels);
        return Response.success();
    }

    //返回任务专利清单
    @Transactional(rollbackFor = Exception.class)
    public String reTaskPatents(PatentVO patentVO) throws ParseException, IOException {
        List<String> taskPatentNos = new ArrayList<>();
        //根据报告Id和类型获得任务的清单
        LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssoTaskPersonel::getReportId, patentVO.getReportId());
        //当taskID存在时
        if (patentVO.getTaskId() != null) {
            queryWrapper.eq(AssoTaskPersonel::getTaskId, patentVO.getTaskId());
            if (patentVO.getTaskStatus() != 3) {
                queryWrapper.eq(AssoTaskPersonel::getState, patentVO.getTaskStatus());
            }
            List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(queryWrapper);
            if (patentVO.getTaskStatus() != 3) {
                taskPatentNos = assoTaskPersonels.stream().filter(item -> item.getState() == patentVO.getTaskStatus()).map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
            } else {
                taskPatentNos = assoTaskPersonels.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
            }
        }
        //当taskId不存在时
        else {
            List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(queryWrapper);
            taskPatentNos = assoTaskPersonels.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
        }
        //无查询条件
        if (patentVO.getFiledOptions().size() == 0 || patentVO.getFiledOptions() == null) {
            patentVO.setPatentNos(taskPatentNos);
        }
        //有查询条件时，自定义字段的专利号和任务的专利号取交集
        else {
            List<String> patentNos = reportFieldService.getPatentNo(patentVO.getFiledOptions(), taskPatentNos);
            List<String> jiaoList = new ArrayList<>(taskPatentNos);
            jiaoList.retainAll(patentNos);
            patentVO.setPatentNos(jiaoList);
        }
        String res = outInterfaceService.getPatentFromPAS(patentVO, 1);
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
        List<JSONObject> jsonObjects = JSONArray.parseArray(jsonObject1.get("records").toString(), JSONObject.class);
        for (int i = 0; i < jsonObjects.size(); i++) {
            jsonObjects.get(i).put("field", reportFieldService.getPatentFieldByPatentIdAndProjectId(patentVO.getReportId(), jsonObjects.get(i).getString("patentNo")));
        }
        jsonObject1.put("records", jsonObjects);
        jsonObject.put("data", jsonObject1);


        return jsonObject.toString();
    }

    //返回任务专利清单
    @Transactional(rollbackFor = Exception.class)
    public String reTaskPatents1(PatentVO patentVO) throws ParseException, IOException {
        List<String> taskPatentNos = new ArrayList<>();
        //无自定义字段查询条件
        if (patentVO.getFiledOptions().size() == 0 || patentVO.getFiledOptions() == null) {
            //当有任务Id时
            if (patentVO.getTaskId() != null) {
                LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(AssoTaskPersonel::getTaskId, patentVO.getTaskId());
                if (patentVO.getTaskStatus() != 3) {
                    queryWrapper.eq(AssoTaskPersonel::getState, patentVO.getTaskStatus());
                }
                List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(queryWrapper);
                taskPatentNos = assoTaskPersonels.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
            }
            // 当无任务Id时
            else {
                LambdaQueryWrapper<CompareFiles> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(CompareFiles::getReportId, patentVO.getReportId());
                List<CompareFiles> assoTaskPersonels = compareFilesService.list(queryWrapper);
                taskPatentNos = assoTaskPersonels.stream().map(CompareFiles::getPatentNo).collect(Collectors.toList());
            }
        }
        //有自定义查询条件
        else {
            taskPatentNos = reportFieldService.getPatentNo1(patentVO.getFiledOptions(), patentVO.getReportId(), patentVO.getTaskId(), patentVO.getTaskStatus());

        }
        patentVO.setPatentNos(taskPatentNos);
        String res = outInterfaceService.getPatentFromPAS(patentVO, 1);
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
        List<JSONObject> jsonObjects = JSONArray.parseArray(jsonObject1.get("records").toString(), JSONObject.class);
        for (int i = 0; i < jsonObjects.size(); i++) {
            jsonObjects.get(i).put("field", reportFieldService.getPatentFieldByPatentIdAndProjectId(patentVO.getReportId(), jsonObjects.get(i).getString("patentNo")));
        }
        jsonObject1.put("records", jsonObjects);
        jsonObject.put("data", jsonObject1);


        return jsonObject.toString();
    }


    public Map<String, Object> reTaskStateCount(Integer reportId, Integer taskId) {
        //根据任务Id和类型获得任务的清单
        LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssoTaskPersonel::getReportId, reportId);
        //当taskID存在时
        if (taskId != null) {
            queryWrapper.eq(AssoTaskPersonel::getTaskId, taskId);
        }
        Map<String, Object> reMap = new HashMap<>();
        reMap.put("all", 0);
        reMap.put("noRead", 0);
        reMap.put("read", 0);
        List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(queryWrapper);
        if (assoTaskPersonels.size() == 0) {
            return reMap;
        }
        List<AssoTaskPersonel> assoNoRead = assoTaskPersonels.stream().filter(item -> item.getState().equals(0)).collect(Collectors.toList());
        List<AssoTaskPersonel> assoRead = assoTaskPersonels.stream().filter(item -> item.getState().equals(1)).collect(Collectors.toList());
        reMap.put("all", assoTaskPersonels.size());
        reMap.put("noRead", assoNoRead.size());
        reMap.put("read", assoRead.size());
        return reMap;
    }

    /**
     * @param taskId
     * @return
     * @function 任务完成的进度，
     * @author ：cj
     */
    public Map<String, Object> reCount(Integer taskId) {
        //根据任务Id和类型获得任务的清单
        LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssoTaskPersonel::getTaskId, taskId);
        List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(queryWrapper);
        //未完成
        List<AssoTaskPersonel> nofinish = assoTaskPersonels.stream().filter(item -> item.getState().equals(0)).collect(Collectors.toList());
        //已完成
        List<AssoTaskPersonel> finish = assoTaskPersonels.stream().filter(item -> item.getState().equals(1)).collect(Collectors.toList());
        Map<String, Object> reMap = new HashMap<>();
        reMap.put("total", assoTaskPersonels.size());
        reMap.put("finish", finish.size());
        reMap.put("nofinish", nofinish.size());
        return reMap;
    }

    /**
     * @param ： 任务的id
     * @function :改变任务的状态
     * @return： 是否改变成功
     * @author ：cj
     */
    @Transactional
    public String finishTask(Integer id) throws IOException {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getId, id);
        List<Task> list = this.list(queryWrapper);
        Task task = list.get(0);
        //1为已完成
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //增加完成时间
        simpleDateFormat.format(now);
        task.setFinishTime(now);
        // 当是协同任务
        if (task.getType() == 1 && task.getTaskStatus() == 0) {
            task.setTaskStatus(2);
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("title", "任务通知");
            map.put("template", "mail/eTaskConfirm.html");
            map.put("img", "\\src\\main\\resources\\mail\\logo.png");
            if (task.getHandlePersonId() != null) {
                String resBody = outInterfaceService.getPersonnelByIdsFromPCS(Arrays.asList(task.getHandlePersonId()));
                JSONObject jsonObject = JSONObject.parseObject(resBody);
                //解析jason获得标的专利的权要
                List<Personnel> personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
                map.put("value1", personnels.get(0).getPersonnelName());
                map.put("value2", task.getTaskName());
                map.put("email", personnels.get(0).getPersonnelEmail());
                mailUtils.sendEmailMessage(map);
            } else {
                String orgin = task.getHandlePersonName();
                String parStr = "\\<([^}]*)\\>";
                Pattern pattern = Pattern.compile(parStr);
                Matcher matcher = pattern.matcher(orgin);
                if (matcher.find()) {
                    String personName = orgin.substring(0, matcher.start());
                    String email = matcher.group(0);
                    email = email.replace("<", "");
                    email = email.replace(">", "");
                    map.put("value1", personName);
                    map.put("value2", task.getTaskName());
                    map.put("email", email);
                    mailUtils.sendEmailMessage(map);
                }
            }
        } else {
            task.setTaskStatus(1);
        }
        //结果为已完成状态
        task.setResult(1);
        task.updateById();
        return Response.success();
    }

    /**
     * @param taskVO
     * @return
     * @function 根据报告查看任务
     * @author ：cj
     */
    public String ReviewTheTasks(TaskTDO taskVO) throws IOException {
//        PersonnelVO personnelVO = cacheUtils.getLoginUser(StpUtil.getLoginIdAsInt());
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Task::getReportId, taskVO.getReportId());
        if(taskVO.getTaskName()!=null){
            lambdaQueryWrapper.like(Task::getTaskName,taskVO.getTaskName());
        }
        if(taskVO.getType()!=null){
            lambdaQueryWrapper.eq(Task::getType,taskVO.getType());
        }//当查询使用处理人名称时
        if (taskVO.getHandlePersonName() != null && taskVO.getHandlePersonName() != "") {
            String res = outInterfaceService.getPersonIdByNamePCS(taskVO.getHandlePersonName());
            List<Integer> handleIds = JSONArray.parseArray(res, Integer.class);
            //当未查询到时直接返回空页
            if (handleIds != null && handleIds.size() != 0) {
                lambdaQueryWrapper.in(Task::getHandlePersonId,handleIds);
            }
            else{
                lambdaQueryWrapper.in(Task::getHandlePersonId,Arrays.asList(-1));
            }

        }


        List<Task> records = this.page(new Page<>(taskVO.getCurrent(), taskVO.getSize()), lambdaQueryWrapper).getRecords();
        for (Task task : records) {
            Integer createID = task.getCreateID();
            Integer handlePersonId = task.getHandlePersonId();
            PersonnelVO loginUser = cacheUtils.getLoginUser(createID);
            task.setCreateName(loginUser.getName());
            PersonnelVO loginUser1 = cacheUtils.getLoginUser(handlePersonId);
            task.setHandlePersonName(loginUser1.getName());
        }
        Long count = this.count(lambdaQueryWrapper);
        return Response.success(records, DataUtils.setPageColumn(taskVO.getSize(), taskVO.getCurrent(), Math.toIntExact(count)));

    }

    public List<Task> reTasks(List<Task> tasks) throws IOException {
        if (tasks.size() != 0) {
            //创建人Id
            List<Integer> createIds = tasks.stream().map(Task::getCreateID).collect(Collectors.toList());
            //处理人Id
            List<Integer> handleIds = tasks.stream().map(Task::getHandlePersonId).collect(Collectors.toList());
            createIds.addAll(handleIds);
            String res1 = outInterfaceService.getPersonnelByIdsFromPCS(createIds);
            JSONObject jsonObject1 = JSONObject.parseObject(res1);
            //解析jason获得标的专利的权要
            List<Personnel> personnels = JSON.parseArray(jsonObject1.getString("data"), Personnel.class);

            for (Task item : tasks) {
                //装载创建人名和负责人名
                List<Personnel> mapCre = personnels.stream().filter(tem -> tem.getId().equals(item.getCreateID())).collect(Collectors.toList());
                List<Personnel> mapDuty = personnels.stream().filter(tem -> tem.getId().equals(item.getHandlePersonId())).collect(Collectors.toList());
                if (mapCre.size() != 0)
                    item.setCreateName(mapCre.get(0).getPersonnelName());
                if (mapDuty.size() != 0 && item.getHandlePersonName() == null) {
                    item.setHandlePersonName(mapDuty.get(0).getPersonnelName());
                }
                //装载部门名

            }
        }
        return tasks;
    }

    /**
     * @param ： 任务的id 专利号
     * @function :点击自定义字段变成已读
     * @return： 是否改变成功
     * @author ：cj
     */
    @Transactional
    public String read(Integer taskID, String patentNo, Integer reportId) {

        if (taskID != null) {
            LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AssoTaskPersonel::getPatentNo, patentNo);
            queryWrapper.eq(AssoTaskPersonel::getTaskId, taskID);
            AssoTaskPersonel assoTaskPersonel = assoTaskPersonelService.list(queryWrapper).get(0);
            if (assoTaskPersonel.getState().equals(1)) {
                assoTaskPersonel.setState(0);
            } else {
                assoTaskPersonel.setState(1);
            }
            assoTaskPersonel.updateById();
        } else {
            LambdaQueryWrapper<CompareFiles> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CompareFiles::getReportId, reportId).eq(CompareFiles::getPatentNo, patentNo);
            CompareFiles compareFiles = compareFilesService.list(queryWrapper).get(0);
            compareFiles.setState(1);
            compareFiles.updateById();


        }
        return Response.success();

    }

    public String look(Integer taskID, String patentNo, Integer reoportId) {
        if (taskID != null) {
            LambdaQueryWrapper<AssoTaskPersonel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AssoTaskPersonel::getPatentNo, patentNo);
            queryWrapper.eq(AssoTaskPersonel::getTaskId, taskID);
            AssoTaskPersonel assoTaskPersonel = assoTaskPersonelService.list(queryWrapper).get(0);
            return Response.success(assoTaskPersonel.getState());
        } else {
            LambdaQueryWrapper<CompareFiles> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CompareFiles::getPatentNo, patentNo).eq(CompareFiles::getReportId, reoportId);
            List<CompareFiles> list = compareFilesService.list(queryWrapper);
            return Response.success(list.get(0).getState());

        }
    }

    @Transactional(rollbackFor = Exception.class)
    public String addSyneryTask(SyneryVO syneryVO) throws ParseException, IOException {
        //任务保存部分
        Report report = reportService.getById(syneryVO.getReportId());
        PatentVO patentVO = new PatentVO();
        List<Synery_CC> synery_ccs = syneryVO.getSyneryCCList();
        if (syneryVO.getPatentVO() != null) {
            patentVO = syneryVO.getPatentVO();
        }
        Task task = new Task();
        task.setTaskName(syneryVO.getTaskName());
        task.setReportId(syneryVO.getReportId());
        task.setType(1);
        task.setTaskStatus(0);
        task.setProgress(0.0);
        task.setEndTime(syneryVO.getEndTime());
        task.setRemark(syneryVO.getRemark());
        if (syneryVO.getHandlePersonId() != null) {
            task.setHandlePersonId(syneryVO.getHandlePersonId());
        } else {
            task.setHandlePersonName(syneryVO.getPersonEmail());
        }
        task.setCreateID(loginUtils.getId());
        PersonnelVO personnel =cacheUtils.getLoginUser(loginUtils.getId());
        task.insert();


        QueryCMVO queryCMVO = new QueryCMVO();
        queryCMVO.setReportId(syneryVO.getReportId());
        queryCMVO.setCompareResult(syneryVO.getCompareResult());
        List<String> patentNos = new ArrayList<>();
        //无查询条件,单个专利
        if (syneryVO.getPatentNos() != null&&syneryVO.getPatentNos().size()!=0) {
            patentNos =syneryVO.getPatentNos();
        }
        //无查询条件并且是对比过的专利
        else if ((patentVO == null || patentVO.getFiledOptions() == null || patentVO.getFiledOptions().size() == 0) && syneryVO.getIsAll() == 0) {
            List<String> AllpatentNos = compareMessageService.getBaseMapper().getComparedMessageAll(queryCMVO);
            patentNos = AllpatentNos;
            patentVO.setPatentNos(patentNos);
            patentVO.setPatentName(syneryVO.getPatentVO().getPatentName());
        }
        //无查询条件并且是全部的专利
        else if ((patentVO == null || patentVO.getFiledOptions() == null || patentVO.getFiledOptions().size() == 0) && syneryVO.getIsAll() == 1) {
            LambdaQueryWrapper<CompareFiles> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(CompareFiles::getReportId, syneryVO.getReportId());
            List<CompareFiles> compareFiles = compareFilesService.list(queryWrapper1);
            List<String> AllpatentNos = compareFiles.stream().map(CompareFiles::getPatentNo).collect(Collectors.toList());
            patentNos = AllpatentNos;
        }
        //有查询条件并且是对比过的专利
        else if (patentVO != null && patentVO.getFiledOptions() != null && patentVO.getFiledOptions().size() != 0 && syneryVO.getIsAll() == 0) {
            List<String> AllpatentNos = compareMessageService.getBaseMapper().getComparedMessageAll(queryCMVO);
            List<String> temPatentNos = reportFieldService.getPatentNo1(patentVO.getFiledOptions(), syneryVO.getReportId(), task.getId(), patentVO.getTaskStatus());
            List<String> strings1 = new ArrayList<>(CollectionUtils.intersection(temPatentNos, AllpatentNos));
            patentVO.setPatentNos(temPatentNos);
            patentVO.setPatentName(syneryVO.getPatentVO().getPatentName());
        }
        //有查询条件并且是全部的专利
        else if (patentVO != null && patentVO.getFiledOptions() != null && patentVO.getFiledOptions().size() != 0 && syneryVO.getIsAll() == 1) {
            LambdaQueryWrapper<CompareFiles> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(CompareFiles::getReportId, syneryVO.getReportId());
            List<CompareFiles> compareFiles = compareFilesService.list(queryWrapper1);
            List<String> AllpatentNos = compareFiles.stream().map(CompareFiles::getPatentNo).collect(Collectors.toList());
            List<String> temPatentNos = reportFieldService.getPatentNo1(patentVO.getFiledOptions(), syneryVO.getReportId(), task.getId(), patentVO.getTaskStatus());
            List<String> strings1 = new ArrayList<>(CollectionUtils.intersection(temPatentNos, AllpatentNos));
            patentVO.setPatentNos(temPatentNos);
        }
        if (patentVO != null && patentVO.getPatentNos() != null && patentVO.getPatentNos().size() != 0) {
            String res = outInterfaceService.getConPantentNos(patentVO, 1);
            JSONObject jsonObject1 = JSONObject.parseObject(res);
            patentNos = JSONArray.parseArray(jsonObject1.get("data").toString(), String.class);
        }
        List<AssoTaskPersonel> assoTaskPersonels = new ArrayList<>();
        patentNos.forEach(item -> {
            AssoTaskPersonel assoTaskPersonel = new AssoTaskPersonel();
            assoTaskPersonel.setTaskId(task.getId());
            assoTaskPersonel.setPatentNo(item);
            assoTaskPersonel.setTaskType(task.getType());
            assoTaskPersonels.add(assoTaskPersonel);
        });
        // 分配信息入库
        assoTaskPersonelService.saveBatch(assoTaskPersonels);
        //发送邮件部分
        String code = DataUtils.generateShortUuid();
        SyneryCode syneryCode = new SyneryCode();
        syneryCode.setCode(code);
        syneryCode.setTaskId(task.getId());
        syneryCode.insert();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("title", "任务通知");
        String name ="";
        if (syneryVO.getHandlePersonId() != null) {
            map.put("template", "mail/SyneryInside.html");
            String resBody = outInterfaceService.getPersonnelByIdsFromPCS(Arrays.asList(task.getHandlePersonId()));
            JSONObject jsonObject = JSONObject.parseObject(resBody);
            //解析jason获得标的专利的权要
            List<Personnel> personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
            map.put("value1", personnels.get(0).getPersonnelName());
            name=personnels.get(0).getPersonnelName();
            map.put("email", personnels.get(0).getPersonnelEmail());
            map.put("img", "\\src\\main\\resources\\mail\\logo.png");
            map.put("value2", url+"/Incomplete?taskId=" + task.getId() + "&reportId=" + task.getReportId() + "&type=1" + "&reportType=" + report.getType());
            mailUtils.sendEmailMessage(map);
        } else {
            map.put("template", "mail/Synery.html");
            String orgin = syneryVO.getPersonEmail();
            String parStr = "\\<([^}]*)\\>";
            Pattern pattern = Pattern.compile(parStr);
            Matcher matcher = pattern.matcher(orgin);
            if (matcher.find()) {
                String personName = orgin.substring(0, matcher.start());
                String email = matcher.group(0);
                email = email.replace("<", "");
                email = email.replace(">", "");
                map.put("value1", personName);
                name=personName;
                map.put("email", email);
                map.put("img", "\\src\\main\\resources\\mail\\logo.png");
                map.put("value2", syneryCode.getCode());
                map.put("value3", url+"/identificationCode");
                mailUtils.sendEmailMessage(map);
            }
        }
        if (synery_ccs != null && synery_ccs.size() != 0) {
            map.put("template", "mail/SyneryTo.html");
            for(Synery_CC item: synery_ccs)
            {
                item.setTaskId(task.getId());
                if (item.getCcId() != null) {
                    String resBody = null;
                    try {
                        resBody = outInterfaceService.getPersonnelByIdsFromPCS(Arrays.asList(item.getCcId()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    JSONObject jsonObject = JSONObject.parseObject(resBody);
                    //解析jason获得标的专利的权要
                    List<Personnel> personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
                    map.put("value1",personnel.getName());
                    map.put("email", personnels.get(0).getPersonnelEmail());
                    map.put("img", "\\src\\main\\resources\\mail\\logo.png");
                    map.put("value2",name);
                    mailUtils.sendEmailMessage(map);
                } else {
                    String orgin = item.getCcEmail();
                    String parStr = "\\<([^}]*)\\>";
                    Pattern pattern = Pattern.compile(parStr);
                    Matcher matcher = pattern.matcher(orgin);
                    if (matcher.find()) {
                        String personName = orgin.substring(0, matcher.start());
                        String email = matcher.group(0);
                        email = email.replace("<", "");
                        email = email.replace(">", "");
                        map.put("value1",personnel.getName());
                        map.put("email", email);
                        map.put("img", "\\src\\main\\resources\\mail\\logo.png");
                        map.put("value2",name);
                        mailUtils.sendEmailMessage(map);
                    }
                }
            }
            syneryCCService.saveBatch(synery_ccs);
        }
        return Response.success();
    }

    /**
     * 获得任务过滤条件
     *
     * @param reportId
     */
    public List<TaskSourceVO> getTaskSources(Integer reportId) throws IOException {
        List<TaskSourceVO> taskSourceVOS = new ArrayList<>();
        //任务类型列表
        List<Integer> types = Arrays.asList(2, 3);
        TaskSourceVO assignVO = new TaskSourceVO();
        assignVO.setTaskType("分配任务");
        TaskSourceVO siftVO = new TaskSourceVO();
        siftVO.setTaskType("初筛任务");
        //任务类型标识
        List<Integer> typeFlag = new ArrayList<>();
        //根据报告id获得所有对比文件
        LambdaQueryWrapper<CompareFiles> comWrapper = new LambdaQueryWrapper<>();
        comWrapper.select(CompareFiles::getPatentNo)
                .eq(CompareFiles::getReportId, reportId);
        Function<Object, String> f = (o -> o.toString());
        List<String> comPatentNos = compareFilesService.listObjs(comWrapper, f);
        //根据报告id获得初筛任务和对比任务
        LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.eq(Task::getReportId, reportId)
                .in(Task::getType, types);
        List<Task> tasks = this.list(taskWrapper);
        //筛选出任务id
        List<Integer> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
        //根据任务id获得任务专利关联记录
        LambdaQueryWrapper<AssoTaskPersonel> assoWrapper = new LambdaQueryWrapper<>();
        assoWrapper.in(AssoTaskPersonel::getTaskId, taskIds);
        List<AssoTaskPersonel> assoTaskPersonels = assoTaskPersonelService.list(assoWrapper);

        for (Integer type : types) {
            List<String> comPatentNosCopy = new ArrayList<>(comPatentNos);
            TaskSourceVO vo = new TaskSourceVO();
            if (type.equals(2)) {
                vo.setTaskType("对比任务");
            } else if (type.equals(3)) {
                vo.setTaskType("初筛任务");
            }
            List<Integer> typeTaskIds = tasks.stream().filter(te -> te.getType().equals(type))
                    .map(Task::getId).collect(Collectors.toList());
            List<AssoTaskPersonel> partAsso = assoTaskPersonels.stream()
                    .filter(te -> typeTaskIds.contains(te.getTaskId())).collect(Collectors.toList());
            Map<Integer, TaskSourceVO.personDate> personMap = new LinkedHashMap<>();
            for (AssoTaskPersonel item : partAsso) {
                if (personMap.get(item.getPersonelId()) != null) {
                    TaskSourceVO.personDate personDate = personMap.get(item.getPersonelId());
                    List<Integer> TaskIds = personDate.getTaskIds();
                    List<String> done = new ArrayList<>(personDate.getDone());
                    List<String> notDone = new ArrayList<>(personDate.getNotDone());
                    if (!TaskIds.contains(item.getTaskId())) {
                        TaskIds.add(item.getTaskId());
                    }
                    if (item.getState().equals(1) && !done.contains(item.getPatentNo())) {
                        done.add(item.getPatentNo());
                        personDate.setDone(done);
                        personDate.setDoneNum(personDate.getDoneNum() + 1);
                    }
                    if (item.getState().equals(0) && !notDone.contains(item.getPatentNo())) {
                        notDone.add(item.getPatentNo());
                        personDate.setNotDone(notDone);
                        personDate.setNotDoneNum(personDate.getNotDoneNum() + 1);
                    }
                } else {
                    TaskSourceVO.personDate personDate = new TaskSourceVO.personDate();
                    personDate.setTaskIds(Arrays.asList(item.getTaskId()));
                    if (item.getState().equals(0)) {
                        personDate.setNotDoneNum(1);
                        personDate.setDoneNum(0);
                        personDate.setNotDone(Arrays.asList(item.getPatentNo()));
                        personDate.setDone(Arrays.asList());
                    } else if (item.getState().equals(1)) {
                        personDate.setDoneNum(1);
                        personDate.setNotDoneNum(0);
                        personDate.setDone(Arrays.asList(item.getPatentNo()));
                        personDate.setNotDone(Arrays.asList());
                    }
                    personMap.put(item.getPersonelId(), personDate);
                }
                comPatentNosCopy.remove(item.getPatentNo());
            }
            List<TaskSourceVO.personDate> lists = new ArrayList<>();
            //根据人员Id获得人员信息
            String res1 = outInterfaceService.getPersonnelByIdsFromPCS(new ArrayList<>(personMap.keySet()));
            JSONObject jsonObject1 = JSONObject.parseObject(res1);
            //解析jason获得标的专利的权要
            List<Personnel> personnels = JSON.parseArray(jsonObject1.getString("data"), Personnel.class);
            for(Integer key: personMap.keySet()){
                TaskSourceVO.personDate tem =personMap.get(key);
                tem.setPersonId(key);
               Personnel personnel = personnels.stream().
                        filter(te -> te.getId().equals(key)).findFirst().orElse(new Personnel());
               tem.setPersonName(personnel.getPersonnelName());
               lists.add(tem);
            }
            vo.setNotAssignNum(comPatentNosCopy.size());
            vo.setPersonDates(lists);
            taskSourceVOS.add(vo);
        }


        //获得初筛任务ID


        return taskSourceVOS;

    }
}