package com.bonus.material.part.service.impl;


import com.bonus.common.biz.enums.PurchaseTaskStageEnum;
import com.bonus.common.biz.enums.PurchaseTaskStatusEnum;
import com.bonus.common.biz.enums.TmTaskTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.common.biz.domain.BmFileInfo;
import com.bonus.material.basic.mapper.BmFileInfoMapper;
import com.bonus.material.countersign.domain.SignProcessVo;
import com.bonus.material.countersign.mapper.SignProcessMapper;
import com.bonus.material.part.domain.*;
import com.bonus.material.part.mapper.PartArrivedMapper;
import com.bonus.material.part.service.IPartArrivedService;
import com.bonus.material.purchase.domain.PurchaseSignRecord;
import com.bonus.material.task.domain.TmTask;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.bonus.common.biz.constant.MaterialConstants.PART_TASK_TYPE_LABEL;

/**
 * 配件新购管理Service业务层处理
 * 
 * @author hongchao
 * @date 2025-01-08
 */
@Service
public class PartArrivedServiceImpl implements IPartArrivedService
{
    @Resource
    private PartArrivedMapper partArrivedMapper;

    @Resource
    BmFileInfoMapper bmFileInfoMapper;

    @Resource
    private SignProcessMapper signProcessMapper;

    // 引入新购任务阶段常量
    private static final int PURCHASE_TASK_STAGE_MANAGE = 1;
    private static final int PURCHASE_TASK_STAGE_CHECK = 2;
    private static final int PURCHASE_TASK_STAGE_STORE = 3;

    /**
     * 查询配件类型三级树--前端联动式下拉框
     * @param partId 类型id
     * @param partName 类型名称
     */
    @Override
    public List<PartTypeDto> getTypeTree(Long partId, String partName) {
        List<PartTypeDto> maTypes = partArrivedMapper.getTypeTree("");
        List<PartTypeDto> list = new ArrayList<>();
        for (PartTypeDto maType : maTypes) {
            if (maType.getParentId() == 0) {
                list.add(maType);
            }
        }
        //根据父节点获取对应的儿子节点
        for (PartTypeDto maType : list) {
            List<PartTypeDto> child = getChild(maTypes, maType.getPartId());
            maType.setChildren(child);
        }
        return list;
    }

    /**
     * 递归调用获取子级
     * @param list 集合
     * @param parentId 父级id
     */
    public List<PartTypeDto> getChild(List<PartTypeDto> list, Long parentId) {
        List<PartTypeDto> childList = new ArrayList<PartTypeDto>();
        for (PartTypeDto maType : list) {
            Long partId = maType.getPartId();
            Long pid = maType.getParentId();
            if (parentId.equals(pid)) {
                List<PartTypeDto> childLists = getChild(list, partId);
                maType.setChildren(childLists);
                childList.add(maType);
            }
        }
        return childList;
    }

    /**
     * 新增新购验收任务
     *
     * @param partTypeCheckDto 新购验收任务
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertPartTypeCheckInfo(PartTypeCheckDto partTypeCheckDto) {
        partTypeCheckDto.getPartTypeCheckInfo().setCreateTime(DateUtils.getNowDate());
        partTypeCheckDto.getPartTypeCheckInfo().setPurchaseTime(DateUtils.getNowDate());
        partTypeCheckDto.getPartTypeCheckInfo().setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        try {
            final int thisMonthMaxOrder = partArrivedMapper.getMonthMaxOrderByDate(DateUtils.getCurrentYear(), DateUtils.getCurrentMonth(), TmTaskTypeEnum.TM_TASK_PART_TYPE.getTaskTypeId());
            String taskCode = genderTaskCode(thisMonthMaxOrder);
            final TmTask tmTask = new TmTask(null, TmTaskTypeEnum.TM_TASK_PART_TYPE.getTaskTypeId(),
                    PurchaseTaskStatusEnum.TO_NOTICE.getStatus(),
                    partTypeCheckDto.getPartTypeCheckInfo().getCompanyId(), thisMonthMaxOrder + 1, taskCode);
            tmTask.setCreateTime(DateUtils.getNowDate());
            partArrivedMapper.insertTmTask(tmTask);
            Long taskId = tmTask.getTaskId();
            partTypeCheckDto.getPartTypeCheckInfo().setTaskId(taskId);
            final int count = partArrivedMapper.insertPartTypeCheckInfo(partTypeCheckDto.getPartTypeCheckInfo());
            if (0 < count) {
                return insertPartTypeCheckDetails(partTypeCheckDto.getPartTypeCheckDetailsList(), taskId);
            } else {
                return AjaxResult.error("新增任务失败,purchase_part_info表插入0条");
            }
        } catch (final DataAccessException e) {
            return AjaxResult.error("数据库操作失败:" + e.getMessage());
        } catch (final Exception e) {
            return AjaxResult.error("新增任务失败:" + e.getMessage());
        }
    }

    private AjaxResult insertPartTypeCheckDetails(List<PartTypeCheckDetails> partTypeCheckDetailsList, Long taskId) {
        if (!CollectionUtils.isEmpty(partTypeCheckDetailsList)) {
            for (PartTypeCheckDetails details : partTypeCheckDetailsList) {
                details.setTaskId(taskId);
                details.setStatus(1);
                details.setInputStatus("0");
                final List<BmFileInfo> bmFileInfos = details.getBmFileInfos();
                if (!CollectionUtils.isEmpty(bmFileInfos)) {
                    bmFileInfos.forEach(o -> o.setTaskId(taskId));
                    bmFileInfoMapper.insertBmFileInfos(bmFileInfos);
                }
            }
            // 批量插入详情数据
            final int count = partArrivedMapper.insertPartTypeCheckDetailsList(partTypeCheckDetailsList);
            if (0 < count) {
                return AjaxResult.success("新增任务成功");
            } else {
                return AjaxResult.error("新增任务失败,purchase_part_detail详情表插入0条");
            }
        } else {
            return AjaxResult.success("新增任务成功");
        }
    }

    /**
     * 生成任务编号并构造Tm_Task任务对象
     * @param thisMonthMaxOrder 当月最大单号
     * @return 任务对象
     */
    private static String genderTaskCode(Integer thisMonthMaxOrder) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final Date nowDate = DateUtils.getNowDate();
        final String format = dateFormat.format(nowDate);
        final String result = format.replace("-", "");
        return PART_TASK_TYPE_LABEL + result + String.format("-%03d", thisMonthMaxOrder + 1);
    }


    /**
     * 查询新购验收任务列表
     *
     * @param partTypeQueryDto 新购验收任务请求参数
     * @return 新购验收任务
     */
    @Override
    public List<PartTypeCheckInfo> selectPartTypeCheckInfoList(PartTypeQueryDto partTypeQueryDto) {
         List<PartTypeCheckInfo> partTypeCheckInfos = partArrivedMapper.selectPartTypeCheckInfoJoinList(partTypeQueryDto);
        List<PartTypeCheckInfo> purchaseCheckInfoResult = new ArrayList<>();
        for (int i = 0; i < partTypeCheckInfos.size(); i++) {
            List<PartTypeCheckDetails> partTypeCheckDetails = partArrivedMapper.selectPartTypeCheckDetailsList(partTypeCheckInfos.get(i).getTaskId());
            boolean isNoticeFlag = false;
            boolean isCheckFlag = false;
            if (null != partTypeQueryDto.getTaskStage()) {
                // 根据阶段来过滤详情数据
                switch (partTypeQueryDto.getTaskStage()) {
                    case PURCHASE_TASK_STAGE_MANAGE:
                        // 新购管理阶段展示全部任务、不进行过滤
                        if (partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_NOTICE.getStatus())) {
                            partTypeCheckInfos.get(i).setTaskStatusName("未完成");
                        } else {
                            partTypeCheckInfos.get(i).setTaskStatusName("已完成");
                        }
                        break;
                    case PURCHASE_TASK_STAGE_CHECK:
                        /*partTypeCheckDetails = partTypeCheckDetails.stream().filter(o -> !PurchaseTaskStatusEnum.TO_NOTICE.getStatus().equals(o.getStatus())).collect(Collectors.toList());
                        partTypeCheckDetails = partTypeCheckDetails.stream().filter(o -> !PurchaseTaskStatusEnum.TO_CHECK_AFTER_REJECT.getStatus().equals(o.getStatus())).collect(Collectors.toList());*/
                        if (partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_CHECK.getStatus())) {
                            partTypeCheckInfos.get(i).setTaskStatusName("未完成");
                        } else {
                            partTypeCheckInfos.get(i).setTaskStatusName("已完成");
                        }
                        // 将待通知的数据过滤
                        if (partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_NOTICE.getStatus())) {
                            isNoticeFlag = true;
                        }
                        break;
                    case PURCHASE_TASK_STAGE_STORE:
                        /*partTypeCheckDetails = partTypeCheckDetails.stream().filter(o ->
                                PurchaseTaskStatusEnum.IN_STORE.getStatus().equals(o.getStatus()) ||
                                        PurchaseTaskStatusEnum.TO_STORE.getStatus().equals(o.getStatus()) ||
                                        PurchaseTaskStatusEnum.TASK_FINISHED.getStatus().equals(o.getStatus()) ||
                                        (PurchaseTaskStatusEnum.TO_CHECK.getStatus().equals(o.getStatus()))
                        ).collect(Collectors.toList());*/
                        if (partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TASK_IN_PROGRESS.getStatus()) ||
                                partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_STORE.getStatus()) ||
                                partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_CHECK.getStatus())) {
                            partTypeCheckInfos.get(i).setTaskStatusName("未完成");
                        } else {
                            partTypeCheckInfos.get(i).setTaskStatusName("已完成");
                        }
                        // 将待通知和待验收的数据过滤
                        if (partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_NOTICE.getStatus()) ||
                                partTypeCheckInfos.get(i).getTaskStatus().equals(PurchaseTaskStatusEnum.TO_CHECK.getStatus())) {
                            isCheckFlag = true;
                        }
                        break;
                    default:
                        System.err.println("未知的新购任务阶段：" + partTypeQueryDto.getTaskStage());
                }
            }
            if (isNoticeFlag || isCheckFlag) {
                continue;
            }
            if(!CollectionUtils.isEmpty(partTypeCheckDetails)) {
                BigDecimal purchaseNum = new BigDecimal(0);
                BigDecimal inputNum = new BigDecimal(0);
                BigDecimal purchasePrice = new BigDecimal(0);
                BigDecimal purchaseTaxPrice = new BigDecimal(0);
                Set<String> partNameSet = new HashSet<>();
                for (int j = 0; j < partTypeCheckDetails.size(); j++) {
                    purchaseNum = purchaseNum.add(partTypeCheckDetails.get(j).getPurchaseNum());
                    inputNum = inputNum.add(partTypeCheckDetails.get(j).getInputNum());
                    purchasePrice = purchasePrice.add(partTypeCheckDetails.get(j).getPurchasePrice().multiply(partTypeCheckDetails.get(j).getPurchaseNum()));
                    purchaseTaxPrice = purchaseTaxPrice.add(partTypeCheckDetails.get(j).getPurchaseTaxPrice().multiply(partTypeCheckDetails.get(j).getPurchaseNum()));
                    // 将名称添加到Set中（自动去重）
                    partNameSet.add(partTypeCheckDetails.get(j).getMaTypeName());
                }
                // 将Set中的元素用逗号拼接成字符串
                String partName = String.join(",", partNameSet);
                partTypeCheckInfos.get(i).setInputMaNumber(inputNum);
                partTypeCheckInfos.get(i).setPurchaseMaNumber(purchaseNum);
                partTypeCheckInfos.get(i).setPurchasePrice(purchasePrice);
                partTypeCheckInfos.get(i).setPurchaseTaxPrice(purchaseTaxPrice);
                partTypeCheckInfos.get(i).setPurchaseMaTypeName(partName);
                // 将purchaseCheckDetails中的status取出来
                List<Integer> statusList = partTypeCheckDetails.stream().map(PartTypeCheckDetails::getStatus).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(statusList)) {
                    if (statusList.contains(PurchaseTaskStatusEnum.TO_BIND.getStatus())
                            || statusList.contains(PurchaseTaskStatusEnum.TO_STORE.getStatus())
                            || statusList.contains(PurchaseTaskStatusEnum.TO_BIND_AFTER_REJECT.getStatus())
                            || statusList.contains(PurchaseTaskStatusEnum.TO_STORE_AFTER_REJECT.getStatus())) {
                        partTypeCheckInfos.get(i).setPurchaseStatus(PurchaseTaskStatusEnum.TO_BIND.getStatus());
                    } else if (statusList.contains(PurchaseTaskStatusEnum.IN_STORE.getStatus())
                            || statusList.contains(PurchaseTaskStatusEnum.TASK_IN_PROGRESS.getStatus())
                            ||  statusList.contains(PurchaseTaskStatusEnum.TASK_FINISHED.getStatus())) {
                        partTypeCheckInfos.get(i).setPurchaseStatus(PurchaseTaskStatusEnum.IN_STORE.getStatus());
                    } else {
                        partTypeCheckInfos.get(i).setPurchaseStatus(0);
                    }
                }
            }
            purchaseCheckInfoResult.add(partTypeCheckInfos.get(i));
        }
        if (!CollectionUtils.isEmpty(purchaseCheckInfoResult)) {
            // 根据taskId查询验收任务层级，返回前端，用于验收按钮显示
            for (PartTypeCheckInfo purchaseCheckInfo : purchaseCheckInfoResult) {
                // 查询已进行会签的org_id集合
                List<PurchaseSignRecord> purchaseSignRecordList = signProcessMapper.getPurchaseSignRecordByTask(purchaseCheckInfo.getTaskId());
                // 查询需要进行会签的org_id集合
                List<SignProcessVo> signProcessVoList = signProcessMapper.getList(new SignProcessVo());
                if (!CollectionUtils.isEmpty(purchaseSignRecordList) && !CollectionUtils.isEmpty(signProcessVoList)) {
                    // 查询出配置需要会签的org_id集合
                    Set<Long> configSignOrgSet = signProcessVoList.stream().map(SignProcessVo::getOrgId).collect(Collectors.toSet());
                    // 获取已进行会签的org_id集合
                    Set<Long> signedOrgSet = purchaseSignRecordList.stream().map(PurchaseSignRecord::getOrgId).collect(Collectors.toSet());
                    // 移除已会签的org_id
                    configSignOrgSet.removeAll(signedOrgSet);
                    if (signedOrgSet.size() == 1) {
                        purchaseCheckInfo.setSignLevel("2");
                    } else {
                        purchaseCheckInfo.setSignLevel("3");
                    }
                }
            }
            String keyWord = partTypeQueryDto.getKeyWord();
            // 如果关键字不为空，进行过滤
            if (StringUtils.isNotBlank(keyWord)) {
                purchaseCheckInfoResult = purchaseCheckInfoResult.stream()
                        .filter(item -> containsKeyword(item, keyWord))
                        .collect(Collectors.toList());
            }
            if (partTypeQueryDto.getTaskStatus() != null) {
                if (partTypeQueryDto.getTaskStatus() == 0) {
                    partTypeQueryDto.setTaskStatusName("未完成");
                } else if (partTypeQueryDto.getTaskStatus() == 1) {
                    partTypeQueryDto.setTaskStatusName("已完成");
                }
                purchaseCheckInfoResult = purchaseCheckInfoResult.stream()
                        .filter(item -> item.getTaskStatusName().equals(partTypeQueryDto.getTaskStatusName()))
                        .collect(Collectors.toList());
            }
        }
         return purchaseCheckInfoResult;
    }

    /**
     * 判断关键字是否在任务信息中
     * @param item
     * @param keyWord
     * @return
     */
    private boolean containsKeyword(PartTypeCheckInfo item, String keyWord) {
        return (item.getPurchaseMaTypeName() != null && item.getPurchaseMaTypeName().contains(keyWord)) ||
                (item.getCode() != null && item.getCode().contains(keyWord)) ||
                (item.getSupplier() != null && item.getSupplier().contains(keyWord)) ||
                (item.getCreateBy() != null && item.getCreateBy().contains(keyWord));
    }

    /**
     * 查询新购验收任务列表详情
     *
     * @param partTypeQueryDto 新购验收任务请求参数
     * @return 新购验收任务
     */
    @Override
    public PartTypeCheckDto selectPartTypeCheckInfoById(PartTypeQueryDto partTypeQueryDto) {
        PartTypeCheckInfo partTypeCheckInfo = partArrivedMapper.selectPartTypeCheckInfoById(partTypeQueryDto.getId());
        PartTypeCheckDto partTypeCheckDto = new PartTypeCheckDto();
        partTypeCheckDto.setPartTypeCheckInfo(partTypeCheckInfo);
        if(null != partTypeCheckInfo){
            List<PartTypeCheckDetails> partTypeCheckDetailsList = partArrivedMapper.selectPartTypeCheckDetailsListByQueryDto(partTypeQueryDto);
            partTypeCheckDto.setPartTypeCheckDetailsList(partTypeCheckDetailsList);
            //查询是否存在附件，给前端用于文件颜色判断
            extractedExitFile(partTypeCheckDetailsList);
        }
        return partTypeCheckDto;
    }

    /**
     * 查询是否存在附件，给前端用于文件颜色判断
     * @param partTypeCheckDetailsList
     */
    private void extractedExitFile(List<PartTypeCheckDetails> partTypeCheckDetailsList) {
        if (!CollectionUtils.isEmpty(partTypeCheckDetailsList)) {
            for (PartTypeCheckDetails partTypeCheckDetails : partTypeCheckDetailsList) {
                final BmFileInfo fileInfo = new BmFileInfo();
                fileInfo.setTaskType(13);
                fileInfo.setModelId(partTypeCheckDetails.getPartId());
                fileInfo.setTaskId(partTypeCheckDetails.getTaskId());
                final List<BmFileInfo> bmFileInfoList = bmFileInfoMapper.selectBmFileInfoList(fileInfo);
                if (!CollectionUtils.isEmpty(bmFileInfoList)) {
                    partTypeCheckDetails.setIsExitFile(0);
                }else {
                    partTypeCheckDetails.setIsExitFile(1);
                }
            }
        }
    }

    /**
     * 修改新购验收任务
     *
     * @param partTypeCheckDto 新购验收任务
     * @return 结果
     */
    @Override
    @Transactional
    public boolean updatePartTypeCheckInfo(PartTypeCheckDto partTypeCheckDto) {
        partTypeCheckDto.getPartTypeCheckInfo().setUpdateTime(DateUtils.getNowDate());
        partTypeCheckDto.getPartTypeCheckInfo().setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        // 先去查是否存在文件信息，存在就删除
        BmFileInfo fileInfo = new BmFileInfo();
        fileInfo.setTaskType(13);
        fileInfo.setTaskId(partTypeCheckDto.getPartTypeCheckInfo().getTaskId());
        List<BmFileInfo> bmFileInfoList = bmFileInfoMapper.selectBmFileInfoList(fileInfo);
        if (!CollectionUtils.isEmpty(bmFileInfoList)) {
            partArrivedMapper.deleteBmFileList(partTypeCheckDto.getPartTypeCheckInfo().getTaskId());
        }
        if (!CollectionUtils.isEmpty(partTypeCheckDto.getPartTypeCheckDetailsList())) {
            partTypeCheckDto.getPartTypeCheckDetailsList().removeIf(Objects::isNull);
            partTypeCheckDto.getPartTypeCheckDetailsList().removeIf(o -> Objects.isNull(o.getStatus()));
//            partTypeCheckDto.getPartTypeCheckDetailsList().removeIf(o ->
//                    !Objects.equals(o.getStatus(), 0));
        }
        try {
            Long[] ids = new Long[partTypeCheckDto.getPartTypeCheckDetailsList().size()];
            int index = 0;
            for (PartTypeCheckDetails details : partTypeCheckDto.getPartTypeCheckDetailsList()) {
                ids[index] = details.getId();
                index++;
            }
            partArrivedMapper.deletePartTypeCheckDetailsByParentIds(partTypeCheckDto.getPartTypeCheckInfo().getTaskId());
            if (ids.length > 0) {
                // 修改采购配件主表
                int res = partArrivedMapper.updatePurchaseCheckInfo(partTypeCheckDto.getPartTypeCheckInfo());
                if (res == 0) {
                    throw new ServiceException("修改失败");
                }
                insertPartTypeCheckDetails(partTypeCheckDto.getPartTypeCheckDetailsList(), partTypeCheckDto.getPartTypeCheckInfo().getTaskId());
            }
            return true;
        } catch (final Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 删除新购验收任务
     *
     * @param partTypeQueryDto 需要删除的新购验收任务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePartInfo(PartTypeQueryDto partTypeQueryDto) {
     int count =  partArrivedMapper.deleteTmTask(partTypeQueryDto.getTaskId());
     if(count==0){
         AjaxResult.error("删除失败");
     }
        partArrivedMapper.deleteByParentIds(partTypeQueryDto.getTaskId());
        return partArrivedMapper.deletePartTypCheckInfoById(partTypeQueryDto.getId());
    }


    /**
     * 查询附件列表
     *
     * @param bmPartFileInfo 查询附件列表
     * @return 查询附件列表
     */
    @Override
    public List<BmPartFileInfo> getPartFilelist(BmPartFileInfo bmPartFileInfo) {
        List<BmPartFileInfo> list = partArrivedMapper.getPartFilelist(bmPartFileInfo);
        for (BmPartFileInfo listEach : list) {
            List<BmPartFileDetail> fileDetailList = partArrivedMapper.getPartFileDetail(listEach);
            listEach.setFileDetailList(fileDetailList);
        }
        return list;
    }

    /**
     * 根据任务id查询入库单
     *
     * @param partTypeQueryDto 入库单
     * @return 入库单
     */
    @Override
    public PartTypeCheckDto getInBoundForm(PartTypeQueryDto partTypeQueryDto) {
        PartTypeCheckInfo partTypeCheckInfo = partArrivedMapper.getCheckInfoForm(partTypeQueryDto);
        PartTypeCheckDto partTypeCheckDto = new PartTypeCheckDto();
        partTypeCheckDto.setPartTypeCheckInfo(partTypeCheckInfo);
        if(null != partTypeCheckInfo){
            List<PartTypeCheckDetails> partTypeCheckDetailsList = partArrivedMapper.selectPartTypeCheckDetailsListByQueryDto(partTypeQueryDto);
            partTypeCheckDto.setPartTypeCheckDetailsList(partTypeCheckDetailsList);
            if (!CollectionUtils.isEmpty(partTypeCheckDetailsList)) {
                List<PurchaseSignRecord> purchaseSignRecordUserSignList = new ArrayList<>();
                HashMap<String, PurchaseSignRecord> purchaseSignRecordMap = new HashMap<>();
                for (PartTypeCheckDetails purchaseCheckDetails : partTypeCheckDetailsList) {
                    // 查询已签名的记录
                    if (purchaseSignRecordUserSignList.isEmpty()) {
                        purchaseSignRecordUserSignList = signProcessMapper.getPurchaseSignUrlListByTaskId(purchaseCheckDetails.getTaskId());
                        for (PurchaseSignRecord purchaseSignRecord : purchaseSignRecordUserSignList) {
                            purchaseSignRecordMap.put(purchaseSignRecord.getSignUrl(), purchaseSignRecord);
                        }
                    }
                }
                // 分流存入集合,利用Map去重
                purchaseSignRecordMap.forEach((k, v) -> {
                    if (null != v.getUserId()) {
                        if (311 == v.getOrgId()) {
                            partTypeCheckDto.getGySignUrl().add(v);
                        }
                        if (313 == v.getOrgId()) {
                            partTypeCheckDto.getScSignUrl().add(v);
                        }
                        if (312 == v.getOrgId()) {
                            partTypeCheckDto.getKgSignUrl().add(v);
                        }
                    }
                });
            }
        }
        return partTypeCheckDto;
    }
}
