chenxiaofei 20 часов назад
Родитель
Сommit
25bb7b7976

+ 220 - 129
sckw-modules/sckw-contract/src/main/java/com/sckw/contract/service/operateService/KwcContractTradeService.java

@@ -15,6 +15,7 @@ import com.sckw.contract.api.model.vo.LogisticsEntDtoVO;
 import com.sckw.contract.api.model.vo.LogisticsGoodsDto;
 import com.sckw.contract.api.model.vo.TradeContractUnitDto;
 import com.sckw.contract.dao.KwcContractTradeMapper;
+import com.sckw.contract.model.KwcContractProxy;
 import com.sckw.contract.model.dto.req.QueryListReqDto;
 import com.sckw.contract.model.dto.res.QueryListResDto;
 import com.sckw.contract.model.entity.*;
@@ -122,6 +123,7 @@ public class KwcContractTradeService {
     private final KwcContractLogisticsGoodsRepository kwcContractLogisticsGoodsRepository;
     private final KwcContractLogisticsRepository contractLogisticsRepository;
     private final KwcContractLogisticsScoreRepository contractLogisticsScoreRepository;
+    private final KwcContractProxyRepository kwcContractProxyRepository;
     private final DataPermissionFeignService dataPermissionFeignService;
 
     @DubboReference(version = "1.0.0", group = "design", check = false)
@@ -1901,38 +1903,7 @@ public class KwcContractTradeService {
             entIdAndEntMap =
                     ents.stream().collect(Collectors.toMap(EntTypeResDto::getEntId, Function.identity(), (v1, v2) -> v1));
         }
-        //如果登录方是采购方,创建方是提供方,那么采购方只能看到已经签约的合同
-        EntTypeResDto entTypeResDto = entIdAndEntMap.getOrDefault(entId, new EntTypeResDto());
-        //如果登录方是采购商,创建方是供应方,那么采购商只能看到已经签约的合同
-        List<KwcContractTrade> contractTrades = Lists.newArrayList();
-        List<KwcContractTrade> contractTrades1 = Lists.newArrayList();
-        if (entTypeResDto.getType().equals(EntTypeEnum.PURCHASER.getCode())) {
-            List<Integer> statusList = Arrays.asList(ContractStatusEnum.SIGNED.getCode(), ContractStatusEnum.COMPLETE.getCode());
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap1 = entIdAndEntMap;
-
-            records.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap1.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode()) && statusList.contains(record.getStatus())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        //如果登录方是供应方,创建方是采购方,那么供应方不能看到已作废的 合同
-        if (entTypeResDto.getType().equals(EntTypeEnum.SUPPLIER.getCode())) {
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap = entIdAndEntMap;
-            records.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode()) && !Objects.equals(record.getStatus(), ContractStatusEnum.CANNEL.getCode())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        contractTrades.addAll(contractTrades1);
-        records = contractTrades;
+        records = filterTradeContractsByLoginEntType(records, entId, entIdAndEntMap);
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(records)) {
             return Collections.emptyList();
         }
@@ -2021,18 +1992,197 @@ public class KwcContractTradeService {
     }
 
 
+
     /**
-     * 构建贸易合同查询的合同ID集合。
+     * 根据登录企业类型过滤贸易合同列表
      * <p>
-     * 根据当前登录企业ID、采购方企业ID、供应方企业ID等条件,从合同关联单位表中筛选出符合条件的合同ID。
-     * 主要用于分页查询和总数统计时的数据过滤。
-     * </p>
+     * 该方法根据登录企业的类型(采购商、供应商等)对合同列表进行权限过滤,确保企业只能查看其有权限的合同。
+     * 不同类型企业的可见合同范围如下:
+     * <ul>
+     *     <li>采购商(PURCHASER):
+     *         <ul>
+     *             <li>供应商创建的合同:仅可见"已签署"和"已完成"状态的合同</li>
+     *             <li>采购商创建的合同:可见所有状态的合同</li>
+     *         </ul>
+     *     </li>
+     *     <li>供应商(SUPPLIER):
+     *         <ul>
+     *             <li>供应商创建的合同:可见所有状态的合同</li>
+     *             <li>其他企业创建的合同:仅可见非"已取消"状态的合同</li>
+     *         </ul>
+     *     </li>
+     *     <li>其他类型企业:仅可见非"已取消"状态的合同</li>
+     * </ul>
+     *
+     * @param records      待过滤的贸易合同列表
+     * @param loginEntId   登录企业ID,用于确定企业类型
+     * @param entIdTypeMap 企业ID与企业类型信息的映射关系,key为企业ID,value为企业类型信息
+     * @return 过滤后的贸易合同列表,如果参数无效则返回空列表
+     */
+    private List<KwcContractTrade> filterTradeContractsByLoginEntType(List<KwcContractTrade> records,
+                                                                       Long loginEntId,
+                                                                       Map<Long, EntTypeResDto> entIdTypeMap) {
+        // 参数校验:合同列表为空或登录企业ID为空时,直接返回空列表
+        if (org.apache.commons.collections4.CollectionUtils.isEmpty(records) || Objects.isNull(loginEntId)) {
+            log.debug("过滤贸易合同失败:参数无效,records是否为空: {}, loginEntId: {}",
+                    org.apache.commons.collections4.CollectionUtils.isEmpty(records), loginEntId);
+            return Collections.emptyList();
+        }
+
+        // 安全处理企业类型映射,防止NPE
+        Map<Long, EntTypeResDto> safeEntIdTypeMap = Optional.ofNullable(entIdTypeMap).orElse(Collections.emptyMap());
+        
+        // 获取登录企业的类型
+        Integer loginType = safeEntIdTypeMap.getOrDefault(loginEntId, new EntTypeResDto()).getType();
+        log.debug("开始过滤贸易合同,登录企业ID: {}, 企业类型: {}, 待过滤合同数量: {}",
+                loginEntId, loginType, records.size());
+
+        // 场景1:采购商登录 - 根据合同创建方类型进行不同的可见性过滤
+        if (Objects.equals(loginType, EntTypeEnum.PURCHASER.getCode())) {
+            // 定义采购商可见的供应商创建合同状态:已签署、已完成
+            List<Integer> visibleStatusList = Arrays.asList(ContractStatusEnum.SIGNED.getCode(), ContractStatusEnum.COMPLETE.getCode());
+            
+            // 筛选供应商创建的合同,仅保留可见状态的合同
+            List<KwcContractTrade> supplierCreateRecords = records.stream()
+                    .filter(record -> Objects.equals(safeEntIdTypeMap.getOrDefault(record.getEntId(), new EntTypeResDto()).getType(),
+                            EntTypeEnum.SUPPLIER.getCode()))
+                    .filter(record -> visibleStatusList.contains(record.getStatus()))
+                    .collect(Collectors.toList());
+            
+            // 筛选采购商创建的合同,保留所有状态的合同
+            List<KwcContractTrade> purchaserCreateRecords = records.stream()
+                    .filter(record -> Objects.equals(safeEntIdTypeMap.getOrDefault(record.getEntId(), new EntTypeResDto()).getType(),
+                            EntTypeEnum.PURCHASER.getCode()))
+                    .collect(Collectors.toList());
+            
+            // 合并两个列表:供应商创建的合同(仅可见状态) + 采购商创建的合同(所有状态)
+            supplierCreateRecords.addAll(purchaserCreateRecords);
+            
+            log.debug("采购商过滤合同完成,供应商创建合同数量: {}, 采购商创建合同数量: {}, 总计: {}",
+                    supplierCreateRecords.size() - purchaserCreateRecords.size(), 
+                    purchaserCreateRecords.size(), 
+                    supplierCreateRecords.size());
+            
+            return supplierCreateRecords;
+        }
+
+        // 场景2:供应商登录 - 供应商创建的合同可见所有状态,其他企业创建的合同仅可见非取消状态
+        if (Objects.equals(loginType, EntTypeEnum.SUPPLIER.getCode())) {
+            List<KwcContractTrade> filteredRecords = records.stream()
+                    .filter(record -> {
+                        Integer createEntType = safeEntIdTypeMap.getOrDefault(record.getEntId(), new EntTypeResDto()).getType();
+                        // 供应商创建的合同:可见所有状态
+                        // 其他企业创建的合同:仅可见非"已取消"状态
+                        return Objects.equals(createEntType, EntTypeEnum.SUPPLIER.getCode())
+                                || !Objects.equals(record.getStatus(), ContractStatusEnum.CANNEL.getCode());
+                    })
+                    .collect(Collectors.toList());
+            
+            log.debug("供应商过滤合同完成,原始合同数量: {}, 过滤后合同数量: {}", records.size(), filteredRecords.size());
+            return filteredRecords;
+        }
+
+        // 场景3:其他类型企业登录 - 仅可见非"已取消"状态的合同
+        List<KwcContractTrade> filteredRecords = records.stream()
+                .filter(record -> !Objects.equals(record.getStatus(), ContractStatusEnum.CANNEL.getCode()))
+                .collect(Collectors.toList());
+        
+        log.debug("其他企业类型过滤合同完成,原始合同数量: {}, 过滤后合同数量: {}", records.size(), filteredRecords.size());
+        return filteredRecords;
+    }
+
+    private Set<Long> queryProxyEntIdsBySupplyId(Long supplyEntId) {
+        if (Objects.isNull(supplyEntId)) {
+            return Collections.emptySet();
+        }
+        try {
+            List<KwcContractProxy> proxyContracts = kwcContractProxyRepository.queryBySupplyId(supplyEntId);
+            if (CollectionUtils.isEmpty(proxyContracts)) {
+                return Collections.emptySet();
+            }
+            return proxyContracts.stream()
+                    .map(KwcContractProxy::getProxyId)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toSet());
+        } catch (Exception e) {
+            log.error("查询供应商代理企业失败,supplyEntId={}", supplyEntId, e);
+            return Collections.emptySet();
+        }
+    }
+
+    private boolean isSupplierEnt(Long entId) {
+        if (Objects.isNull(entId)) {
+            return false;
+        }
+        try {
+            List<EntTypeResDto> entTypes = remoteSystemService.queryEntTypeByIds(Collections.singleton(entId));
+            if (CollectionUtils.isEmpty(entTypes)) {
+                return false;
+            }
+            return entTypes.stream()
+                    .filter(Objects::nonNull)
+                    .anyMatch(entType -> Objects.equals(entType.getEntId(), entId)
+                            && Objects.equals(entType.getType(), EntTypeEnum.SUPPLIER.getCode()));
+        } catch (Exception e) {
+            log.error("查询企业类型失败,entId={}", entId, e);
+            return false;
+        }
+    }
+
+    /**
+     * 根据代理范围构建初始的贸易合同ID集合
+     * <p>
+     * 该方法用于从合同单位列表中筛选出符合条件的合同ID,筛选规则如下:
+     * <ul>
+     *     <li>1. 企业ID等于登录企业ID的合同单位(当前登录企业直接参与的合同)</li>
+     *     <li>2. 企业ID在代理企业ID集合中,且单位类型为代理商类型(unit_type=3)的合同单位
+     *         (供应商的代理商作为代理商角色参与的合同)</li>
+     * </ul>
      *
-     * @param entId  当前登录的企业ID
-     * @param req    查询请求参数,包含采购方ID、供应方ID等过滤条件
-     * @param entIds 参与查询的所有相关企业ID集合(包括当前企业、指定的采购/供应企业等)
-     * @return 符合条件的合同ID集合,若无匹配则返回空集合
+     * @param units        合同单位列表,包含合同参与方的信息
+     * @param loginEntId   登录企业ID,用于筛选当前企业直接参与的合同
+     * @param proxyEntIds  代理企业ID集合,用于筛选代理商参与的合同(可能为null
+     * @return 符合条件的合同ID集合,如果参数无效则返回空集合
      */
+    private Set<Long> buildInitialTradeContractIdsByProxyScope(List<KwcContractTradeUnit> units, Long loginEntId, Set<Long> proxyEntIds) {
+        // 参数校验:登录企业ID为空或合同单位列表为空时,直接返回空集合
+        if (Objects.isNull(loginEntId) || org.apache.commons.collections4.CollectionUtils.isEmpty(units)) {
+            log.debug("构建初始贸易合同ID集合失败:参数无效,loginEntId: {}, units是否为空: {}",
+                    loginEntId, org.apache.commons.collections4.CollectionUtils.isEmpty(units));
+            return Collections.emptySet();
+        }
+
+        // 安全处理代理企业ID集合,防止NPE
+        Set<Long> safeProxyEntIds = Optional.ofNullable(proxyEntIds).orElse(Collections.emptySet());
+        log.debug("开始构建初始贸易合同ID集合,loginEntId: {}, 合同单位数量: {}, 代理企业数量: {}",
+                loginEntId, units.size(), safeProxyEntIds.size());
+
+        // 筛选符合条件的合同ID
+        Set<Long> contractIds = units.stream()
+                // 步骤1: 过滤掉null对象,防止后续操作出现空指针异常
+                .filter(Objects::nonNull)
+                // 步骤2: 核心筛选逻辑 - 筛选出当前企业有权限查看的合同单位
+                .filter(unit -> Objects.equals(unit.getEntId(), loginEntId)
+                        || (safeProxyEntIds.contains(unit.getEntId())
+                        && Objects.equals(unit.getUnitType(), KwcContractTradeUnitService.TRADE_AGENT_UNIT_TYPE)))
+                // 筛选条件说明:
+                // 条件1: unit.getEntId() == loginEntId
+                //        -> 当前登录企业直接参与的合同(企业作为合同参与方)
+                // 条件2: safeProxyEntIds.contains(unit.getEntId()) && unit.getUnitType() == TRADE_AGENT_UNIT_TYPE
+                //        -> 代理企业作为"代理商角色"(unit_type=3)参与的合同
+                //        -> 用于供应商查看其代理商代理的贸易合同
+                // 步骤3: 提取合同ID,将KwcContractTradeUnit对象映射为合同ID(Long类型)
+                .map(KwcContractTradeUnit::getContractId)
+                // 步骤4: 过滤掉合同ID为null的记录,确保结果集中只包含有效的合同ID
+                .filter(Objects::nonNull)
+                // 步骤5: 收集为Set集合,自动去重,确保每个合同ID只出现一次
+                .collect(Collectors.toSet());
+
+        log.debug("构建初始贸易合同ID集合完成,符合条件的合同数量: {}, loginEntId: {}", contractIds.size(), loginEntId);
+
+        return contractIds;
+    }
+
     private Set<Long> buildTradeQueryContractIds(Long entId, QueryTradeReq req, Set<Long> entIds) {
         // 1. 基础参数校验:如果当前企业ID为空或关联企业ID集合为空,直接返回空集合
         if (Objects.isNull(entId) || org.apache.commons.collections4.CollectionUtils.isEmpty(entIds)) {
@@ -2040,20 +2190,24 @@ public class KwcContractTradeService {
             return Collections.emptySet();
         }
 
+        Set<Long> proxyEntIds = Collections.emptySet();
+        Set<Long> queryEntIds = new HashSet<>(entIds);
+        if (isSupplierEnt(entId)) {
+            proxyEntIds = queryProxyEntIdsBySupplyId(entId);
+            queryEntIds.addAll(proxyEntIds);
+            log.debug("供应商登录自动扩展代理商贸易合同查询范围,supplyEntId={}, proxyEntIds={}", entId, proxyEntIds);
+        }
+
         // 2. 查询这些企业关联的所有合同单位信息
-        List<KwcContractTradeUnit> units = kwcContractTradeUnitRepository.queryByEntIds(entIds);
+        List<KwcContractTradeUnit> units = kwcContractTradeUnitRepository.queryByEntIds(queryEntIds);
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(units)) {
             log.debug("构建贸易合同查询ID失败:未查询到任何合同单位信息, entIds: {}", entIds);
             return Collections.emptySet();
         }
         log.debug("查询到合同单位数量: {}, entIds: {}", units.size(), entIds);
 
-        // 3. 初步筛选:获取当前登录企业(entId)参与的所有合同ID
-        Set<Long> contractIds = units.stream()
-                .filter(unit -> Objects.equals(unit.getEntId(), entId))
-                .map(KwcContractTradeUnit::getContractId)
-                .filter(Objects::nonNull)
-                .collect(Collectors.toSet());
+        // 3. 初步筛选:代理商查自己的合同;供应商额外查询其代理商作为 unit_type=3 参与的贸易合同
+        Set<Long> contractIds = buildInitialTradeContractIdsByProxyScope(units, entId, proxyEntIds);
 
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractIds)) {
             log.debug("构建贸易合同查询ID失败:当前企业[{}]未参与任何合同", entId);
@@ -2105,7 +2259,7 @@ public class KwcContractTradeService {
 
             Set<Long> agentContractIds = units.stream()
                     .filter(unit -> Objects.equals(unit.getEntId(), agentEntId))
-                    .filter(unit -> Objects.equals(unit.getUnitType(), CooperateTypeEnum.SUPPLIER.getCode()))
+                    .filter(unit -> Objects.equals(unit.getUnitType(), KwcContractTradeUnitService.TRADE_AGENT_UNIT_TYPE))
                     .map(KwcContractTradeUnit::getContractId)
                     .filter(Objects::nonNull)
                     .collect(Collectors.toSet());
@@ -2163,35 +2317,7 @@ public class KwcContractTradeService {
             entIdAndEntMap =
                     ents.stream().collect(Collectors.toMap(EntTypeResDto::getEntId, Function.identity(), (v1, v2) -> v1));
         }
-        EntTypeResDto entTypeResDto = entIdAndEntMap.getOrDefault(entId, new EntTypeResDto());
-        List<KwcContractTrade> contractTrades = Lists.newArrayList();
-        List<KwcContractTrade> contractTrades1 = Lists.newArrayList();
-        if (entTypeResDto.getType().equals(EntTypeEnum.PURCHASER.getCode())) {
-            List<Integer> statusList = Arrays.asList(ContractStatusEnum.SIGNED.getCode(), ContractStatusEnum.COMPLETE.getCode());
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap1 = entIdAndEntMap;
-
-            records.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap1.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode()) && statusList.contains(record.getStatus())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        if (entTypeResDto.getType().equals(EntTypeEnum.SUPPLIER.getCode())) {
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap = entIdAndEntMap;
-            records.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode()) && !Objects.equals(record.getStatus(), ContractStatusEnum.CANNEL.getCode())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        contractTrades.addAll(contractTrades1);
-        records = contractTrades;
+        records = filterTradeContractsByLoginEntType(records, entId, entIdAndEntMap);
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(records)) {
             return 0L;
         }
@@ -2227,10 +2353,10 @@ public class KwcContractTradeService {
             }
             if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
                 records = records.stream().filter(record -> {
-                    KwcContractTradeUnit provideEnt =
-                            finalContractUnitTypeKeyAndUnitMap.getOrDefault(record.getId() + "-" + CooperateTypeEnum.SUPPLIER.getCode(),
+                    KwcContractTradeUnit agentEnt =
+                            finalContractUnitTypeKeyAndUnitMap.getOrDefault(record.getId() + "-" + KwcContractTradeUnitService.TRADE_AGENT_UNIT_TYPE,
                                     new KwcContractTradeUnit());
-                    return org.apache.commons.lang3.StringUtils.equals(String.valueOf(provideEnt.getEntId()), req.getAgentEntId());
+                    return org.apache.commons.lang3.StringUtils.equals(String.valueOf(agentEnt.getEntId()), req.getAgentEntId());
                 }).collect(Collectors.toList());
             }
         }
@@ -2292,8 +2418,11 @@ public class KwcContractTradeService {
         queryListResVo.setProvideEntId(String.valueOf(provideEnt.getEntId()));
         queryListResVo.setProvideEntName(provideEnt.getFirmName());
         if (Objects.equals(t.getAgentFlag(), Global.YES)) {
-            queryListResVo.setAgentEntId(String.valueOf(provideEnt.getEntId()));
-            queryListResVo.setAgentEntName(provideEnt.getFirmName());
+            KwcContractTradeUnit agentEnt =
+                    finalContractUnitTypeKeyAndUnitMap.getOrDefault(t.getId() + "-" + KwcContractTradeUnitService.TRADE_AGENT_UNIT_TYPE,
+                            new KwcContractTradeUnit());
+            queryListResVo.setAgentEntId(String.valueOf(agentEnt.getEntId()));
+            queryListResVo.setAgentEntName(agentEnt.getFirmName());
         }
         //queryListResVo.setCheckedEntName();
         queryListResVo.setContractNo(t.getContractNo());
@@ -2937,19 +3066,14 @@ public class KwcContractTradeService {
             entIds.add(Long.valueOf(req.getSupplyEntId()));
             // type=CooperateTypeEnum.SUPPLIER.getCode();
         }
-        Set<Long> contractIds = null;
-        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(entIds)) {
-            List<KwcContractTradeUnit> units = kwcContractTradeUnitRepository.queryByEntIds(entIds);
-            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(units)) {
-                Set<Long> contractList = units.stream().filter(x -> Objects.equals(x.getEntId(), entId))
-                        .map(KwcContractTradeUnit::getContractId)
-                        .collect(Collectors.toSet());
-                contractIds = units.stream()
-                        .map(KwcContractTradeUnit::getContractId)
-                        .filter(contractList::contains)
-                        .collect(Collectors.toSet());
-            }
+        if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getAgentEntId())) {
+            entIds.add(Long.valueOf(req.getAgentEntId()));
         }
+        QueryTradeReq queryTradeReq = new QueryTradeReq();
+        queryTradeReq.setPurchaseEntId(req.getPurchaseEntId());
+        queryTradeReq.setSupplyEntId(req.getSupplyEntId());
+        queryTradeReq.setAgentEntId(req.getAgentEntId());
+        Set<Long> contractIds = buildTradeQueryContractIds(entId, queryTradeReq, entIds);
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(contractIds)
                 && (!org.apache.commons.lang3.StringUtils.isAllBlank(req.getPurchaseEntId(), req.getSupplyEntId()) || Objects.nonNull(entId))) {
             Map<Integer, List<KwcContractTrade>> statusAndLogOrdersMap = new HashMap<>();
@@ -2993,39 +3117,7 @@ public class KwcContractTradeService {
             entIdAndEntMap =
                     ents.stream().collect(Collectors.toMap(EntTypeResDto::getEntId, Function.identity(), (v1, v2) -> v1));
         }
-        //如果登录方是采购方,创建方是提供方,那么采购方只能看到已经签约的合同
-        EntTypeResDto entTypeResDto = entIdAndEntMap.getOrDefault(entId, new EntTypeResDto());
-        //如果登录方是采购商,创建方是供应方,那么采购商只能看到已经签约的合同
-        List<KwcContractTrade> contractTrades = Lists.newArrayList();
-        List<KwcContractTrade> contractTrades1 = Lists.newArrayList();
-        if (entTypeResDto.getType().equals(EntTypeEnum.PURCHASER.getCode())) {
-
-            List<Integer> statusList = Arrays.asList(ContractStatusEnum.SIGNED.getCode(), ContractStatusEnum.COMPLETE.getCode());
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap1 = entIdAndEntMap;
-
-            kwcContractTrades.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap1.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode()) && statusList.contains(record.getStatus())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        //如果登录方是供应方,创建方是采购方,那么供应方不能看到已作废的 合同
-        if (entTypeResDto.getType().equals(EntTypeEnum.SUPPLIER.getCode())) {
-            Map<Long, EntTypeResDto> finalEntIdAndEntMap = entIdAndEntMap;
-            kwcContractTrades.forEach(record -> {
-                EntTypeResDto orDefault = finalEntIdAndEntMap.getOrDefault(record.getEntId(), new EntTypeResDto());
-                if (Objects.equals(orDefault.getType(), EntTypeEnum.PURCHASER.getCode()) && !Objects.equals(record.getStatus(), ContractStatusEnum.CANNEL.getCode())) {
-                    contractTrades.add(record);
-                } else if (Objects.equals(orDefault.getType(), EntTypeEnum.SUPPLIER.getCode())) {
-                    contractTrades1.add(record);
-                }
-            });
-        }
-        contractTrades.addAll(contractTrades1);
-        kwcContractTrades = contractTrades;
+        kwcContractTrades = filterTradeContractsByLoginEntType(kwcContractTrades, entId, entIdAndEntMap);
         if (org.apache.commons.collections4.CollectionUtils.isEmpty(kwcContractTrades)) {
             Map<Integer, List<KwcContractTrade>> statusAndLogOrdersMap = new HashMap<>();
             List<ContractStatusCountResp.ContractStatusCount> statusCounts = statusEnums.stream()
@@ -3410,5 +3502,4 @@ public class KwcContractTradeService {
         log.debug("贸易合同数据权限过滤完成,过滤后记录数: {}", filteredRecords.size());
         return filteredRecords;
     }
-}
-
+}

+ 0 - 137
sckw-modules/sckw-contract/src/test/java/com/sckw/contract/service/operateService/KwcContractTradeServiceTest.java

@@ -1,137 +0,0 @@
-package com.sckw.contract.service.operateService;
-
-import com.sckw.contract.model.vo.req.TradeBaseInfoReqVo;
-import com.sckw.contract.service.KwcContractTradeUnitService;
-import com.sckw.core.model.constant.Global;
-import com.sckw.core.exception.SystemException;
-import com.sckw.system.api.model.dto.res.EntTypeResDto;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-
-/**
- * 贸易合同服务单元测试。
- */
-public class KwcContractTradeServiceTest {
-
-    /**
-     * 验证供应单位存在类型为4的企业属性时,识别为代理属性。
-     */
-    @Test
-    public void hasSupplyAgentAttributeWhenSupplyEntTypeIsFour() {
-        EntTypeResDto supplierType = new EntTypeResDto();
-        supplierType.setEntId(1001L);
-        supplierType.setType(1);
-
-        EntTypeResDto agentType = new EntTypeResDto();
-        agentType.setEntId(1001L);
-        agentType.setType(4);
-
-        boolean result = KwcContractTradeService.hasSupplyAgentAttribute(Arrays.asList(supplierType, agentType), 1001L);
-
-        Assert.assertTrue(result);
-    }
-
-    /**
-     * 验证其他企业即使存在类型为4的企业属性,也不会误判当前供应单位。
-     */
-    @Test
-    public void hasSupplyAgentAttributeWhenTypeFourBelongsToOtherEnt() {
-        EntTypeResDto agentType = new EntTypeResDto();
-        agentType.setEntId(2002L);
-        agentType.setType(4);
-
-        boolean result = KwcContractTradeService.hasSupplyAgentAttribute(Collections.singletonList(agentType), 1001L);
-
-        Assert.assertFalse(result);
-    }
-
-    /**
-     * 代理单位参数完整时允许新增销售合同。
-     */
-    @Test
-    public void validateAgentUnitParamWhenAgentInfoComplete() {
-        TradeBaseInfoReqVo baseInfo = new TradeBaseInfoReqVo();
-        baseInfo.setAgentEntId(3001L);
-        baseInfo.setAgentPhone("13800000000");
-
-        KwcContractTradeService.validateAgentUnitParam(baseInfo);
-    }
-
-    /**
-     * 代理单位已选择但未填写联系电话时应拦截,避免落库半截数据。
-     */
-    @Test(expected = SystemException.class)
-    public void validateAgentUnitParamWhenAgentPhoneMissing() {
-        TradeBaseInfoReqVo baseInfo = new TradeBaseInfoReqVo();
-        baseInfo.setAgentEntId(3001L);
-
-        KwcContractTradeService.validateAgentUnitParam(baseInfo);
-    }
-
-    /**
-     * 补充合同的代理单位与联系电话完整时应通过校验。
-     */
-    @Test
-    public void validateSupplyAgentUnitParamWhenAgentInfoComplete() {
-        KwcContractTradeService.validateAgentUnitParam(3001L, "13800000000");
-    }
-
-    /**
-     * 补充合同只填写代理电话时应拦截。
-     */
-    @Test(expected = SystemException.class)
-    public void validateSupplyAgentUnitParamWhenAgentEntMissing() {
-        KwcContractTradeService.validateAgentUnitParam(null, "13800000000");
-    }
-
-    /**
-     * 已选择代理单位时,代理属性为是。
-     */
-    @Test
-    public void resolveSupplyAgentFlagWhenAgentEntIdPresent() {
-        Integer result = KwcContractTradeService.resolveSupplyAgentFlag(1001L, 3001L, Collections.emptyList());
-
-        Assert.assertEquals(Global.YES, result.intValue());
-    }
-
-    /**
-     * 未选择代理单位时,根据供应单位企业属性判断是否具备代理属性。
-     */
-    @Test
-    public void resolveSupplyAgentFlagWhenAgentEntIdAbsentAndSupplyHasAgentType() {
-        EntTypeResDto agentType = new EntTypeResDto();
-        agentType.setEntId(1001L);
-        agentType.setType(4);
-
-        Integer result = KwcContractTradeService.resolveSupplyAgentFlag(1001L, null,
-                Collections.singletonList(agentType));
-
-        Assert.assertEquals(Global.YES, result.intValue());
-    }
-
-    /**
-     * 未选择代理单位且供应单位不具备代理属性时,代理属性为否。
-     */
-    @Test
-    public void resolveSupplyAgentFlagWhenAgentEntIdAbsentAndSupplyHasNoAgentType() {
-        EntTypeResDto supplierType = new EntTypeResDto();
-        supplierType.setEntId(1001L);
-        supplierType.setType(1);
-
-        Integer result = KwcContractTradeService.resolveSupplyAgentFlag(1001L, null,
-                Collections.singletonList(supplierType));
-
-        Assert.assertEquals(Global.NO, result.intValue());
-    }
-
-    /**
-     * 销售合同代理单位落库类型固定为3。
-     */
-    @Test
-    public void tradeAgentUnitTypeShouldBeThree() {
-        Assert.assertEquals(3, KwcContractTradeUnitService.TRADE_AGENT_UNIT_TYPE);
-    }
-}