فهرست منبع

提交630贸易订单相关逻辑

chenxiaofei 2 هفته پیش
والد
کامیت
0ca68ca8e2

+ 6 - 1
sckw-modules/sckw-order/pom.xml

@@ -130,6 +130,11 @@
             <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
             <version>2.6.0</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>com.github.xiaoymin</groupId>
             <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
@@ -164,4 +169,4 @@
         </plugins>
     </build>
 
-</project>
+</project>

+ 6 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/model/KwoTradeOrder.java

@@ -149,5 +149,11 @@ public class KwoTradeOrder extends BaseModel implements Serializable {
     @TableField("add_vehicles")
     private Integer addVehicles;
 
+    /**
+     * 是否代理属性:0-否,1-是
+     */
+    @TableField("agent_flag")
+    private Integer agentFlag;
+
 
 }

+ 5 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/model/dto/OrderListResDTO.java

@@ -156,6 +156,11 @@ public class OrderListResDTO {
      */
     private Integer addVehicles;
 
+    /**
+     * 是否代理属性:0-否,1-是
+     */
+    private Integer agentFlag;
+
     /**
      * 装货地址信息
      */

+ 5 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/model/dto/TradeOrderListSelectDTO.java

@@ -149,4 +149,9 @@ public class TradeOrderListSelectDTO {
      * 数据权限是否强制返回空结果
      */
     private Boolean dataPermissionForceEmpty;
+    /**
+     * 是否代理属性:0-否,1-是
+     */
+    @Schema(description = "是否代理属性:0-否,1-是")
+    private Integer agentFlag;
 }

+ 6 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/model/vo/req/TradeOrderListStatisticParam.java

@@ -90,4 +90,10 @@ public class TradeOrderListStatisticParam {
      */
     private Integer status;
 
+    /**
+     * 是否代理属性:0-否,1-是
+     */
+    @Schema(description = "是否代理属性:0-否,1-是")
+    private Integer agentFlag;
+
 }

+ 10 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/model/vo/res/OrderListResVO.java

@@ -100,6 +100,16 @@ public class OrderListResVO {
      */
     private Integer addVehicles;
 
+    /**
+     * 是否代理属性:0-否,1-是
+     */
+    private Integer agentFlag;
+
+    /**
+     * 是否代理属性描述
+     */
+    private String agentFlagDesc;
+
     /**
      * 合同主键
      */

+ 144 - 0
sckw-modules/sckw-order/src/main/java/com/sckw/order/serivce/KwoTradeOrderService.java

@@ -119,6 +119,11 @@ import java.util.stream.Collectors;
 @RequiredArgsConstructor
 public class KwoTradeOrderService {
 
+    /**
+     * 供应单位代理属性类型。
+     */
+    static final int SUPPLY_AGENT_ENT_TYPE = 4;
+
     @DubboReference(version = "1.0.0", group = "design", check = false)
     private RemoteSystemService remoteSystemService;
     @DubboReference(version = "1.0.0", group = "design", check = false)
@@ -1750,6 +1755,7 @@ public class KwoTradeOrderService {
             vo.setActualPrice(actualAmount.multiply(unitPrice).setScale(2, RoundingMode.HALF_UP)).setUnitPrice(setScale(e.getUnitPrice())).setPrice(setScale(e.getPrice())).setUnit(e.getUnit()).setUnitLabel(unitType == null ? e.getUnit() : (unitType.get(e.getUnit()) == null ? e.getUnit() : unitType.get(e.getUnit()).getLabel())).setAmount(setScale(vo.getAmount())).setEntrustAmount(setScale(e.getEntrustAmount())).setActualAmount(setScale(e.getActualAmount())).setCreateByName(Objects.isNull(userCache) ? null : userCache.getName());
             vo.setDeliveryTypeLabel(DictEnum.getLabel(DictTypeEnum.LOAD_UNLOAD_WAY.getType(), String.valueOf(e.getChargeType())));
             vo.setAddVehicles(e.getAddVehicles());
+            vo.setAgentFlagDesc(Objects.equals(e.getAgentFlag(), Global.YES) ? "是" : "否");
             if (Objects.equals(e.getChargeType(), 1)) { //按装货量
                 vo.setDealAmount(e.getLoadAmount());
             } else {
@@ -2177,6 +2183,143 @@ public class KwoTradeOrderService {
         return kwoTradeOrderMapper.querySaleOrder(start, end);
     }
 
+    /**
+     * 根据贸易合同ID查询订单的代理属性标识。
+     * <p>
+     * 逻辑说明:
+     * 1. 校验贸易合同ID是否为空。
+     * 2. 调用远程服务查询该合同下的所有单位信息。
+     * 3. 筛选出类型为“供应方”(unitType="1")的单位,并获取其企业ID。
+     * 4. 根据供应方企业ID,进一步查询该企业是否具备“代理”属性。
+     *
+     * @param tradeContractId 贸易合同ID
+     * @return 代理属性标识:1-是代理,0-非代理
+     * @throws BusinessException 当合同ID为空、未找到供应单位或查询过程异常时抛出
+     */
+    private Integer queryOrderAgentFlag(Long tradeContractId) {
+        // 参数校验:贸易合同ID不能为空
+        if (Objects.isNull(tradeContractId)) {
+            throw new BusinessException("贸易合同ID不能为空");
+        }
+
+        try {
+            // 记录开始查询日志
+            log.debug("开始查询贸易合同代理属性,tradeContractId={}", tradeContractId);
+
+            // 1. 调用远程服务获取合同关联的单位列表
+            List<TradeContractUnitDto> unitList = remoteContractService.queryContractUnitByContractId(tradeContractId);
+
+            // 2. 从单位列表中筛选出供应方(unitType="1")的企业ID
+            Long supplyEntId = Optional.ofNullable(unitList)
+                    .orElseGet(Collections::emptyList)
+                    .stream()
+                    .filter(Objects::nonNull)
+                    // 过滤出单位类型为供应方的记录 (假设 "1" 代表供应方)
+                    .filter(item -> StrUtil.equals("1", item.getUnitType()))
+                    .map(TradeContractUnitDto::getEntId)
+                    .filter(Objects::nonNull)
+                    .findFirst()
+                    .orElseThrow(() -> {
+                        log.warn("贸易合同中未找到有效的供应单位,tradeContractId={}", tradeContractId);
+                        return new BusinessException("贸易合同供应单位不存在");
+                    });
+
+            log.debug("找到供应单位ID,supplyEntId={},准备查询代理属性", supplyEntId);
+
+            // 3. 根据供应方企业ID查询其是否具备代理属性
+            Integer agentFlag = querySupplyAgentFlag(supplyEntId);
+            
+            log.debug("贸易合同代理属性查询完成,tradeContractId={}, agentFlag={}", tradeContractId, agentFlag);
+            return agentFlag;
+
+        } catch (Exception e) {
+            // 捕获其他未知异常,记录详细堆栈日志,并抛出通用业务异常
+            log.error("查询贸易订单代理属性发生系统异常,tradeContractId={}", tradeContractId, e);
+            throw new BusinessException("查询贸易订单代理属性失败");
+        }
+    }
+
+
+    /**
+     * 查询供应单位企业类型,判断是否包含代理属性。
+     * <p>
+     * 业务逻辑:
+     * 1. 校验供应单位ID有效性。
+     * 2. 调用远程系统服务获取该企业的类型信息列表。
+     * 3. 通过辅助方法 {@link #hasSupplyAgentAttribute(List, Long)} 判断企业类型中是否包含“供应代理”标识(类型为4)。
+     * 4. 返回全局常量标识:Global.YES (1) 表示是代理,Global.NO (0) 表示非代理。
+     *
+     * @param supplyEntId 供应单位ID
+     * @return 代理属性标识:1-是代理,0-非代理
+     * @throws BusinessException 当供应单位ID为空或查询过程发生异常时抛出
+     */
+    private Integer querySupplyAgentFlag(Long supplyEntId) {
+        // 参数校验:确保供应单位ID不为空
+        if (Objects.isNull(supplyEntId)) {
+            log.warn("查询供应单位代理属性失败,原因:供应单位ID为空");
+            throw new BusinessException("供应单位不能为空");
+        }
+
+        try {
+            // 记录调试日志,开始查询流程
+            log.debug("开始查询供应单位代理属性,supplyEntId={}", supplyEntId);
+
+            // 调用远程系统服务,根据企业ID查询企业类型信息
+            // 使用 Collections.singleton 包装单个ID以符合接口参数要求
+            List<EntTypeResDto> entTypeList = remoteSystemService.queryEntTypeByIds(Collections.singleton(supplyEntId));
+
+            // 判断该企业是否具有供应代理属性
+            boolean isAgent = hasSupplyAgentAttribute(entTypeList, supplyEntId);
+            
+            // 记录结果日志
+            log.debug("供应单位代理属性查询完成,supplyEntId={}, isAgent={}", supplyEntId, isAgent);
+
+            // 根据判断结果返回对应的全局常量标识
+            return isAgent ? Global.YES : Global.NO;
+        } catch (Exception e) {
+            // 捕获异常,记录错误日志(包含堆栈信息以便排查)
+            log.error("查询供应单位代理属性发生系统异常,supplyEntId={}", supplyEntId, e);
+            // 抛出统一的业务异常,避免暴露底层技术细节
+            throw new BusinessException("查询供应单位代理属性失败");
+        }
+    }
+
+    /**
+     * 判断指定供应企业是否具有代理属性。
+     * <p>
+     * 业务逻辑:
+     * 1. 校验参数有效性:若供应企业ID为空或企业类型列表为空,直接返回false。
+     * 2. 遍历企业类型列表,筛选出与给定supplyEntId匹配的企业记录。
+     * 3. 检查匹配记录的企业类型(type)是否等于预设的供应单位代理属性类型(SUPPLY_AGENT_ENT_TYPE,值为4)。
+     * 4. 若存在至少一条匹配且类型为代理的记录,则返回true,否则返回false。
+     *
+     * @param entTypeList  企业类型信息列表,来自远程系统服务查询结果
+     * @param supplyEntId  待校验的供应企业ID
+     * @return boolean     true-该企业具有代理属性; false-该企业不具有代理属性或参数无效
+     */
+    static boolean hasSupplyAgentAttribute(List<EntTypeResDto> entTypeList, Long supplyEntId) {
+        // 参数预校验:确保企业ID和企业类型列表非空
+        if (Objects.isNull(supplyEntId) || CollUtil.isEmpty(entTypeList)) {
+            log.debug("判断供应单位代理属性:参数无效,supplyEntId={}, entTypeListSize={}", supplyEntId, CollUtil.size(entTypeList));
+            return false;
+        }
+
+        // 流式处理:过滤非空对象 -> 匹配企业ID -> 判断是否为代理类型
+        boolean isAgent = entTypeList.stream()
+                .filter(Objects::nonNull)
+                .filter(item -> Objects.equals(item.getEntId(), supplyEntId))
+                .anyMatch(item -> {
+                    boolean match = Objects.equals(item.getType(), SUPPLY_AGENT_ENT_TYPE);
+                    if (match) {
+                        log.debug("找到供应单位代理属性匹配项,entId={}, type={}", item.getEntId(), item.getType());
+                    }
+                    return match;
+                });
+
+        log.debug("供应单位代理属性判断结果,supplyEntId={}, isAgent={}", supplyEntId, isAgent);
+        return isAgent;
+    }
+
     /**
      * 新增贸易订单
      *
@@ -2190,6 +2333,7 @@ public class KwoTradeOrderService {
         KwpGoods goodsById = goodsInfoService.getGoodsById(tradeOrderParam.getGoodsId());
 
         TradeContractResDto tradeContractResDto = checkPara(tradeOrderParam, order, goodsById);
+        order.setAgentFlag(queryOrderAgentFlag(tradeOrderParam.getTradeContractId()));
 
         // ====== 下单前校验 start ======
 

+ 9 - 1
sckw-modules/sckw-order/src/main/resources/mapper/KwoTradeOrderMapper.xml

@@ -31,6 +31,7 @@
         <result column="createBy" property="createBy"/>
         <result column="createTime" property="createTime"/>
         <result column="addVehicles" property="addVehicles"/>
+        <result column="agentFlag" property="agentFlag"/>
 
         <collection property="loadAddresses" ofType="com.sckw.order.model.dto.LoadAddressDTO">
             <id column="loadAddressId" property="loadAddressId"/>
@@ -75,7 +76,8 @@
         a.unit           AS unit,
         a.create_by      AS createBy,
         a.create_time    AS createTime,
-        a.add_vehicles   AS addVehicles
+        a.add_vehicles   AS addVehicles,
+        a.agent_flag     AS agentFlag
     </sql>
 
     <select id="tradeOrderSelect" resultMap="mainEntityMap">
@@ -151,6 +153,9 @@
             <if test="query.saleEntId != null">
                 and e.ent_id = #{query.saleEntId}
             </if>
+            <if test="query.agentFlag != null">
+                and a.agent_flag = #{query.agentFlag}
+            </if>
             <if test="query.status != null">
                 and a.status = #{query.status}
             </if>
@@ -522,6 +527,9 @@
             <if test="query.saleEntId != null">
                 and e.ent_id = #{query.saleEntId}
             </if>
+            <if test="query.agentFlag != null">
+                and a.agent_flag = #{query.agentFlag}
+            </if>
 <!--            <if test="query.dataPermissionForceEmpty != null and query.dataPermissionForceEmpty">-->
 <!--                and 1 = 0-->
 <!--            </if>-->

+ 49 - 0
sckw-modules/sckw-order/src/test/java/com/sckw/order/serivce/KwoTradeOrderServiceTest.java

@@ -0,0 +1,49 @@
+package com.sckw.order.serivce;
+
+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 KwoTradeOrderServiceTest {
+
+    /**
+     * 供应单位存在类型4时,识别为代理属性。
+     */
+    @Test
+    public void hasSupplyAgentAttributeWhenSupplyEntHasAgentType() {
+        EntTypeResDto agentType = new EntTypeResDto();
+        agentType.setEntId(1001L);
+        agentType.setType(KwoTradeOrderService.SUPPLY_AGENT_ENT_TYPE);
+
+        boolean result = KwoTradeOrderService.hasSupplyAgentAttribute(Collections.singletonList(agentType), 1001L);
+
+        Assert.assertTrue(result);
+    }
+
+    /**
+     * 只有其他企业存在类型4时,当前供应单位不识别为代理属性。
+     */
+    @Test
+    public void hasSupplyAgentAttributeWhenAgentTypeBelongsToOtherEnt() {
+        EntTypeResDto otherEntAgentType = new EntTypeResDto();
+        otherEntAgentType.setEntId(2002L);
+        otherEntAgentType.setType(KwoTradeOrderService.SUPPLY_AGENT_ENT_TYPE);
+
+        EntTypeResDto supplyNormalType = new EntTypeResDto();
+        supplyNormalType.setEntId(1001L);
+        supplyNormalType.setType(1);
+
+        boolean result = KwoTradeOrderService.hasSupplyAgentAttribute(
+                Arrays.asList(otherEntAgentType, supplyNormalType),
+                1001L
+        );
+
+        Assert.assertFalse(result);
+    }
+}

+ 2 - 0
sql/2026/06/2026_06_02_add_trade_order_agent_flag.sql

@@ -0,0 +1,2 @@
+ALTER TABLE kwo_trade_order
+    ADD COLUMN agent_flag tinyint NOT NULL DEFAULT 0 COMMENT '是否代理属性:0-否,1-是';