package digital.laboratory.platform.inspection.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import digital.laboratory.platform.inspetion.api.enums.BusinessType;
import digital.laboratory.platform.inspection.dto.AssignmentInfoDto;
import digital.laboratory.platform.inspection.entity.*;
import digital.laboratory.platform.inspection.mapper.AssignmentInfoMapper;
import digital.laboratory.platform.inspection.service.*;
import digital.laboratory.platform.inspection.utils.PageUtils;
import digital.laboratory.platform.inspection.vo.AssignmentInfoVo;
import digital.laboratory.platform.inspetion.api.entity.EntrustInfo;
import digital.laboratory.platform.inspetion.api.entity.SampleInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/*
 *@title AssignmentInfoServiceImpl
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/13 11:46
 */
@Service
@Slf4j
public class AssignmentInfoServiceImpl extends ServiceImpl<AssignmentInfoMapper, AssignmentInfo> implements AssignmentInfoService {

    @Resource
    private SampleInfoService sampleInfoService;

    @Resource
    private EntrustInfoService entrustInfoService;

    @Resource
    private ScreenInfoService screenInfoService;

    @Resource
    private TaskInfoService taskInfoService;

    /**
     * 创建分配任务信息
     *
     * @param assignmentInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<AssignmentInfo> addAssignmentInfo(List<AssignmentInfoDto> assignmentInfoDtoList) {
        List<AssignmentInfo> assignmentInfos = new ArrayList<>();
        //1:通过业务 -1：通过检材
        Integer type = assignmentInfoDtoList.get(0).getType();
        //按业务进行分配
        if (type == 1) {
            for (AssignmentInfoDto assignmentInfoDto : assignmentInfoDtoList) {
                String businessId = assignmentInfoDto.getBusinessId();
                String businessType = assignmentInfoDto.getBusinessType();
                List<SampleInfo> sampleInfoList = sampleInfoService.list(Wrappers.<SampleInfo>lambdaQuery().eq(SampleInfo::getBusinessId, businessId));
                if (businessType.startsWith("1")) {
                    List<AssignmentInfo> infoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, businessId));
                    if (infoList != null && infoList.size() > 0 && !(infoList.get(0).getTestUser().equals(assignmentInfoDto.getTestUser()))) {
                        throw new RuntimeException(String.format("业务:" + infoList.get(0).getBusinessName() + "已分配给工作人员:" + infoList.get(0).getTestUserName() + ",如需继续分配，只能继续选择该工作人员，或者撤销已分配的任务后重新进分配!"));
                    }
                    EntrustInfo entrustInfo = entrustInfoService.getById(businessId);
                    entrustInfo.setStatus(1);
                    entrustInfoService.updateById(entrustInfo);
                } else if (businessType.startsWith("2")) {
                    List<AssignmentInfo> infoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, businessId));
                    if (infoList != null && infoList.size() > 0) {
                        throw new RuntimeException(String.format("业务:" + infoList.get(0).getBusinessName() + "中部分或全部检材已分配给其它工作人员,无法进行业务分配，你可以尝试点击右侧的分配检材功能继续进行分配!"));
                    }
                    TaskInfo taskInfo = taskInfoService.getById(businessId);
                    taskInfo.setDistributionSituation(sampleInfoList.size() + "/" + sampleInfoList.size());
                    taskInfoService.updateById(taskInfo);
                } else {
                    List<AssignmentInfo> infoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, businessId));
                    if (infoList != null && infoList.size() > 0) {
                        throw new RuntimeException(String.format("业务:" + infoList.get(0).getBusinessName() + "中部分或全部检材已分配给其它工作人员,无法进行业务分配，你可以尝试点击右侧的分配检材功能继续进行分配!"));
                    }
                    ScreenInfo screenInfo = screenInfoService.getById(businessId);
                    screenInfo.setDistributionSituation(sampleInfoList.size() + "/" + sampleInfoList.size());
                    screenInfoService.updateById(screenInfo);
                }
                if (CollUtil.isEmpty(sampleInfoList)) {
                    throw new RuntimeException("当前业务检材列表为空！不能添加分配信息。");
                }

                for (SampleInfo sampleInfo : sampleInfoList) {
                    AssignmentInfo assignmentInfo = new AssignmentInfo();
                    BeanUtils.copyProperties(assignmentInfoDto, assignmentInfo);
                    assignmentInfo.setId(IdWorker.get32UUID().toUpperCase());
                    assignmentInfo.setSampleId(sampleInfo.getId());
                    assignmentInfo.setSampleName(sampleInfo.getSampleName());
                    sampleInfo.setStatus(1);
                    assignmentInfos.add(assignmentInfo);
                }
                sampleInfoService.updateBatchById(sampleInfoList);
            }
            return this.saveBatch(assignmentInfos) ? assignmentInfos : null;
        } else {
            SampleInfo sampleInfo = sampleInfoService.getById(assignmentInfoDtoList.get(0).getSampleId());
            String businessType = sampleInfo.getBusinessType();
            String businessId = sampleInfo.getBusinessId();

            ArrayList<String> idList = new ArrayList<>();
            assignmentInfoDtoList.forEach(item -> idList.add(item.getSampleId()));
            List<SampleInfo> sampleInfoList = sampleInfoService.list(Wrappers.<SampleInfo>lambdaQuery().eq(SampleInfo::getBusinessId, businessId));
            List<SampleInfo> sampleInfos = sampleInfoService.list(Wrappers.<SampleInfo>lambdaQuery().in(SampleInfo::getId, idList));
            sampleInfos.forEach(item -> item.setStatus(1));
            sampleInfoService.updateBatchById(sampleInfos);

            List<AssignmentInfo> assignmentInfoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, businessId));
            int count = 0;
            //计算分配情况
            if (assignmentInfoList != null && assignmentInfoList.size() > 0) {
                count = assignmentInfoList.size() + assignmentInfoDtoList.size();
            } else {
                count = assignmentInfoDtoList.size();
            }
            String businessName = "";
            if (businessType.startsWith("1")) {
                EntrustInfo entrustInfo = entrustInfoService.getById(businessId);
                entrustInfo.setStatus(1);
                businessName = entrustInfo.getCaseName();
                entrustInfoService.updateById(entrustInfo);

            } else if (businessType.startsWith("2")) {
                TaskInfo taskInfo = taskInfoService.getById(businessId);
                //分配情况
                taskInfo.setDistributionSituation(count + "/" + sampleInfoList.size());
                businessName = taskInfo.getTaskName();
                taskInfoService.updateById(taskInfo);

            } else {
                ScreenInfo screenInfo = screenInfoService.getById(businessId);
                //分配情况
                screenInfo.setDistributionSituation(count + "/" + sampleInfoList.size());
                businessName = screenInfo.getCaseName();
                screenInfoService.updateById(screenInfo);
            }
            for (AssignmentInfoDto assignmentInfoDto : assignmentInfoDtoList) {
                AssignmentInfo assignmentInfo = new AssignmentInfo();
                BeanUtils.copyProperties(assignmentInfoDto, assignmentInfo);
                assignmentInfo.setId(IdWorker.get32UUID().toUpperCase());
                assignmentInfo.setBusinessId(businessId);
                assignmentInfo.setBusinessType(businessType);
                assignmentInfo.setBusinessName(businessName);
                assignmentInfos.add(assignmentInfo);
            }
            return this.saveBatch(assignmentInfos) ? assignmentInfos : null;
        }
    }

//        //通过业务ID创建
//        if (businessIdList != null && businessIdList.size() > 0) {
//            for (String businessId : businessIdList) {
//                List<AssignmentInfo> infoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, businessId));
//                if (infoList != null && infoList.size() > 0) {
//                    AssignmentInfo assignmentInfo = infoList.get(0);
//                    if (assignmentInfo.getBusinessType().startsWith("1") && !(assignmentInfo.getTestUser().equals(assignmentInfoDto.getTestUser()))) {
//                        throw new RuntimeException(String.format("业务:" + assignmentInfo.getBusinessName() + "已分配给工作人员:" + assignmentInfo.getTestUserName() + ",如需继续分配，只能继续选择该工作人员，或者撤销已分配的任务后重新进分配!"));
//                    }
//                    if (!assignmentInfo.getBusinessType().startsWith("1")) {
//                        throw new RuntimeException(String.format("业务:" + assignmentInfo.getBusinessName() + "中部分或全部检材已分配给其它工作人员,无法进行业务分配，你可以尝试点击右侧的分配检材功能继续进行分配!"));
//                    }
//                }
//                List<SampleInfo> list = sampleInfoService.list(Wrappers.<SampleInfo>lambdaQuery().eq(SampleInfo::getBusinessId, businessId).eq(SampleInfo::getStatus, 0));
//                String businessName = "";
//                String status = "";
//                status = "分配情况：" + sampleInfoList.size() + "/" + sampleInfoList.size();
//                if (businessType.startsWith("1")) {
//                    EntrustInfo entrustInfo = entrustInfoService.getById(businessId);
//                    businessName = entrustInfo.getCaseName();
//                    entrustInfo.setStatus(1);
//                } else if (businessType.startsWith("3")) {
//                    ScreenInfo screenInfo = screenInfoService.getById(businessId);
//                    screenInfo.setStatus(status);
//                    businessName = screenInfo.getCaseName();
//                } else {
//                    TaskInfo taskInfo = taskInfoService.getById(businessId);
//                    businessName = taskInfo.getTaskName();
//                    taskInfo.setStatus(status);
//                }
//                for (SampleInfo sampleInfo : list) {
//                    AssignmentInfo assignmentInfo = new AssignmentInfo();
//                    BeanUtils.copyProperties(assignmentInfoDto, assignmentInfo);
//                    assignmentInfo.setId(IdWorker.get32UUID().toUpperCase());
//                    assignmentInfo.setSampleId(sampleInfo.getId());
//                    assignmentInfo.setSampleName(sampleInfo.getSampleName());
//                    assignmentInfo.setBusinessId(businessId);
//                    assignmentInfo.setBusinessName(businessName);
//                    assignmentInfo.setBusinessType(sampleInfo.getBusinessType());
//                    sampleInfo.setStatus(1);
//                    assignmentInfoList.add(assignmentInfo);
//                    sampleInfoList.add(sampleInfo);
//                }
//
//                sampleInfoList.addAll(sampleInfoList);
//            }
//            //通过样本ID创建
//        } else if (sampleIdList != null && sampleIdList.size() > 0) {
//            SampleInfo sample = sampleInfoService.getById(sampleIdList.get(0));
//            String businessId = sample.getBusinessId();
//
//            for (String sampleId : sampleIdList) {
//                SampleInfo sampleInfo = sampleInfoService.getById(sampleId);
//                String businessName = "";
//                String status = "";
//                if (businessType.equals("筛查检验鉴定")) {
//                    ScreenInfo screenInfo = screenInfoService.getById(sampleInfo.getBusinessId());
//                    businessName = screenInfo.getCaseName();
//                } else {
//                    TaskInfo taskInfo = taskInfoService.getById(sampleInfo.getBusinessId());
//                    businessName = taskInfo.getTaskName();
//                }
//                AssignmentInfo assignmentInfo = new AssignmentInfo();
//                BeanUtils.copyProperties(assignmentInfoDto, assignmentInfo);
//                assignmentInfo.setId(IdWorker.get32UUID().toUpperCase());
//                assignmentInfo.setSampleId(sampleInfo.getId());
//                assignmentInfo.setSampleName(sampleInfo.getSampleName());
//                assignmentInfo.setBusinessName(businessName);
//                assignmentInfo.setBusinessId(sampleInfo.getBusinessId());
//                assignmentInfo.setBusinessType(sampleInfo.getBusinessType());
//                sampleInfo.setStatus(1);
//                assignmentInfoList.add(assignmentInfo);
//                sampleInfoList.add(sampleInfo);
//            }
//        } else {
//            throw new RuntimeException(String.format("至少传入一种类型的ID数组"));
//        }
//        if (this.saveBatch(assignmentInfoList) && sampleInfoService.updateBatchById(sampleInfoList)) {
//            return assignmentInfoList;
//        } else {
//            return null;
//        }
//    }

    /**
     * 撤销分配任务
     *
     * @param assignmentInfoList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelAssignmentInfo(List<AssignmentInfo> assignmentInfoList) {
        List<SampleInfo> sampleInfos = new ArrayList<>();
        for (AssignmentInfo assignmentInfo : assignmentInfoList) {
            SampleInfo sampleInfo = sampleInfoService.getById(assignmentInfo.getSampleId());
            if (sampleInfo.getStatus() == 2) {
                throw new RuntimeException(String.format("已使用检材%s进行实验，无法撤销!", sampleInfo.getSampleName()));
            }
            sampleInfo.setStatus(0);
            sampleInfos.add(sampleInfo);
        }
        return this.removeBatchByIds(assignmentInfoList) && sampleInfoService.updateBatchById(sampleInfos);
    }

    /**
     * 删除分配任务
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteAssignmentInfo(String id) {
        return this.removeById(id);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param assignmentInfoDto
     * @return
     */
    @Override
    public IPage getAssignmentInfoPageList(Page page, AssignmentInfoDto assignmentInfoDto) {
        //查询参数：可以是业务名称、样本名称、分配者姓名、接收者姓名
        String keywords = assignmentInfoDto.getKeywords();
        LambdaQueryWrapper<AssignmentInfo> wrapper = Wrappers.<AssignmentInfo>lambdaQuery().and(StringUtils.isNotBlank(keywords), queryWrapper -> queryWrapper
                        .like(AssignmentInfo::getBusinessName, keywords)
                        .or()
                        .like(AssignmentInfo::getSampleName, keywords)
                        .or()
                        .like(AssignmentInfo::getAssignUserName, keywords)
                        .or()
                        .like(AssignmentInfo::getTestUserName, keywords))
                .eq(StringUtils.isNotBlank(assignmentInfoDto.getAssignUser()), AssignmentInfo::getAssignUser, assignmentInfoDto.getAssignUser())
                .eq(StringUtils.isNotBlank(assignmentInfoDto.getTestUser()), AssignmentInfo::getTestUser, assignmentInfoDto.getTestUser())
                //这里前端由于无法一次性传入两个type，所以通过前缀来分类查询，例如2: 查询污水与毛发任务
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("1"), AssignmentInfo::getBusinessType, 1)
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("2"), AssignmentInfo::getBusinessType, 2)
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("3"), AssignmentInfo::getBusinessType, 3)
                .orderByDesc(AssignmentInfo::getCreateTime);

        if (StringUtils.isNotBlank(assignmentInfoDto.getTestUser())) {//检验人员查看我的待办
            List<AssignmentInfo> retList = this.list(wrapper);
            Map<String, List<AssignmentInfo>> map = retList.stream().collect(Collectors.groupingBy(obj -> obj.getBusinessId()));
            List<AssignmentInfoVo> assignmentInfoVos = new ArrayList<>();
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                List<AssignmentInfo> assignmentInfos = map.get(key);
                AssignmentInfoVo assignmentInfoVo = new AssignmentInfoVo();
                BeanUtils.copyProperties(map.get(key).get(0), assignmentInfoVo);
                assignmentInfoVo.setSampleId("");
                assignmentInfoVo.setSampleName("");
                List<SampleInfo> sampleInfos = new ArrayList<>();
                for (AssignmentInfo info : assignmentInfos) {
                    SampleInfo sampleInfo = sampleInfoService.getById(info.getSampleId());
                    if (sampleInfo.getStatus() == 1) {
                        sampleInfos.add(sampleInfo);
                    }
                }
                assignmentInfoVo.setSampleInfoList(sampleInfos);
                if (sampleInfos.size() > 0) {
                    assignmentInfoVos.add(assignmentInfoVo);
                }
            }
            return new PageUtils().getPages(page.getCurrent(), page.getSize(), assignmentInfoVos);
        } else {
            //管理者查看所有分配信息
            IPage<AssignmentInfo> ret = this.page(page, wrapper);
            List<AssignmentInfo> records = ret.getRecords();
            records.forEach(item -> {
                item.setBusinessTypeName(BusinessType.getBusinessTypeName(item.getBusinessType()));
            });
            return ret;
        }
    }

    /**
     * 列表查询
     *
     * @param assignmentInfoDto
     * @return
     */
    @Override
    public List getAssignmentInfoList(AssignmentInfoDto assignmentInfoDto) {
        //查询参数：可以是业务名称、样本名称、分配者姓名、接收者姓名
        String keywords = assignmentInfoDto.getKeywords();
        LambdaQueryWrapper<AssignmentInfo> wrapper = Wrappers.<AssignmentInfo>lambdaQuery().and(StringUtils.isNotBlank(keywords), queryWrapper -> queryWrapper
                        .like(AssignmentInfo::getBusinessName, keywords)
                        .or()
                        .like(AssignmentInfo::getSampleName, keywords)
                        .or()
                        .like(AssignmentInfo::getAssignUserName, keywords)
                        .or()
                        .like(AssignmentInfo::getTestUserName, keywords))
                .eq(StringUtils.isNotBlank(assignmentInfoDto.getAssignUser()), AssignmentInfo::getAssignUser, assignmentInfoDto.getAssignUser())
                .eq(StringUtils.isNotBlank(assignmentInfoDto.getTestUser()), AssignmentInfo::getTestUser, assignmentInfoDto.getTestUser())
                //这里前端由于无法一次性传入两个type，所以通过前缀来分类查询，例如2: 查询污水与毛发任务
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("1"), AssignmentInfo::getBusinessType, 1)
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("2"), AssignmentInfo::getBusinessType, 2)
                .likeRight(StringUtils.isNotBlank(assignmentInfoDto.getBusinessType()) && assignmentInfoDto.getBusinessType().startsWith("3"), AssignmentInfo::getBusinessType, 3)
                .orderByDesc(AssignmentInfo::getCreateTime);

        if (StringUtils.isNotBlank(assignmentInfoDto.getTestUser())) {//检验人员查看我的待办
            List<AssignmentInfo> retList = this.list(wrapper);
            Map<String, List<AssignmentInfo>> map = retList.stream().collect(Collectors.groupingBy(obj -> obj.getBusinessId()));
            List<AssignmentInfoVo> assignmentInfoVos = new ArrayList<>();
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                List<AssignmentInfo> assignmentInfos = map.get(key);
                String businessType = assignmentInfos.get(0).getBusinessType();
                AssignmentInfoVo assignmentInfoVo = new AssignmentInfoVo();
                BeanUtils.copyProperties(map.get(key).get(0), assignmentInfoVo);
                assignmentInfoVo.setBusinessTypeName(BusinessType.getBusinessTypeName(businessType));
                assignmentInfoVo.setSampleId("");
                assignmentInfoVo.setSampleName("");
                List<SampleInfo> sampleInfos = new ArrayList<>();
                for (AssignmentInfo info : assignmentInfos) {
                    SampleInfo sampleInfo = sampleInfoService.getById(info.getSampleId());
                    if (sampleInfo.getStatus() == 1) {
                        sampleInfos.add(sampleInfo);
                    }
                }
                if (sampleInfos.size() > 0) {
                    Collections.sort(sampleInfos, new Comparator<SampleInfo>() {
                        @Override
                        public int compare(SampleInfo o1, SampleInfo o2) {
                            return o1.getAcceptNo().compareTo(o2.getAcceptNo());
                        }
                    });
                }
                sampleInfos.forEach(o -> o.setBusinessTypeName(BusinessType.getBusinessTypeName(o.getBusinessType())));
                assignmentInfoVo.setSampleInfoList(sampleInfos);
                if (sampleInfos.size() > 0) {
                    assignmentInfoVos.add(assignmentInfoVo);
                }
            }
            return assignmentInfoVos;
        } else {
            //管理者查看所有分配信息
            List<AssignmentInfo> list = this.list(wrapper);
            for (AssignmentInfo assignmentInfo : list) {
                assignmentInfo.setBusinessTypeName(BusinessType.getBusinessTypeName(assignmentInfo.getBusinessType()));
            }
            return list;
        }
    }

    @Override
    public Boolean checkExist(String id) {
        List<AssignmentInfo> retList = this.list(Wrappers.<AssignmentInfo>lambdaQuery()
                .eq(AssignmentInfo::getId, id));
        if (retList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 批量撤销类别为委托的分配任务信息
     *
     * @param businessIdList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelByBusiness(List<String> businessIdList) {
        ArrayList<SampleInfo> sampleInfos = new ArrayList<>();
        businessIdList.forEach(item -> {
            List<SampleInfo> sampleInfoList = sampleInfoService.list(Wrappers.<SampleInfo>lambdaQuery().eq(SampleInfo::getBusinessId, item));
            if (sampleInfoList.get(0).getBusinessType().startsWith("1")) {
                EntrustInfo entrustInfo = entrustInfoService.getById(item);
                entrustInfo.setStatus(0);
                entrustInfoService.updateById(entrustInfo);
            }
            sampleInfoList.forEach(obj -> {
                if (obj.getStatus() == 2) {
                    throw new RuntimeException(String.format("已使用检材%s进行实验，无法撤销!", obj.getSampleName()));
                }
                obj.setStatus(0);
                sampleInfos.add(obj);
            });
        });
        return this.remove(Wrappers.<AssignmentInfo>lambdaQuery().in(AssignmentInfo::getBusinessId, businessIdList)) && sampleInfoService.updateBatchById(sampleInfos);
    }

    /**
     * 查询类别为委托的已分配任务列表
     *
     * @param page
     * @param keywords 接收者名、业务名
     * @return
     */
    @Override
    public IPage<AssignmentInfoVo> getAssignedEntrustPage(Page page, String keywords) {
        ArrayList<AssignmentInfoVo> assignmentInfoVos = new ArrayList<>();
        List<AssignmentInfo> assignmentInfoList = this.list(Wrappers.<AssignmentInfo>lambdaQuery()
                .and(StringUtils.isNotBlank(keywords), queryWrapper -> queryWrapper
                        .like(AssignmentInfo::getTestUserName, keywords)
                        .or()
                        .like(AssignmentInfo::getBusinessName, keywords))
                .and(queryWrapper -> queryWrapper
                        .eq(AssignmentInfo::getBusinessType, "10001")
                        .or()
                        .eq(AssignmentInfo::getBusinessType, "10002")));
        Map<String, List<AssignmentInfo>> map = assignmentInfoList.stream().collect(Collectors.groupingBy(item -> item.getBusinessId()));
        Set<String> keySet = map.keySet();
        //按业务分个组~~~
        keySet.forEach(key -> {
            List<AssignmentInfo> infoList = map.get(key);

            AssignmentInfoVo assignmentInfoVo = new AssignmentInfoVo();
            BeanUtils.copyProperties(infoList.get(0), assignmentInfoVo);
            assignmentInfoVo.setSampleCount(infoList.size());
            assignmentInfoVo.setSampleName("");
            assignmentInfoVo.setSampleId("");
            assignmentInfoVo.setBusinessTypeName(BusinessType.getBusinessTypeName(infoList.get(0).getBusinessType()));
            /*if (infoList.get(0).getBusinessType().equals("10001")) {
                assignmentInfoVo.setBusinessTypeName("NPS案件");
            } else {
                assignmentInfoVo.setBusinessTypeName("生物样本案件");
            }*/
            assignmentInfoVos.add(assignmentInfoVo);
        });
        //按时间排个序~~~
        Collections.sort(assignmentInfoVos, new Comparator<AssignmentInfoVo>() {
            @Override
            public int compare(AssignmentInfoVo o1, AssignmentInfoVo o2) {
                return o1.getCreateTime().compareTo(o2.getCreateTime());
            }
        });
        return new PageUtils().getPages(page.getCurrent(), page.getSize(), assignmentInfoVos);
    }
}
