Parcourir la source

[update] 货损统计运营商名称链返回优化

miajio il y a 3 semaines
Parent
commit
b3eb141042

+ 16 - 3
warehouse-admin-component/src/main/java/com/yr/warehouse/admin/component/auth/OperatorComponent.java

@@ -13,6 +13,7 @@ import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -38,7 +39,12 @@ public class OperatorComponent {
      * 通过id查询运营商信息
      */
     public OrgOperatorVo queryOperatorById(Long id) {
-        return queryOperatorMapByIdList(Lists.newArrayList(id)).get(id);
+        OrgOperatorQueryRequest request = new OrgOperatorQueryRequest();
+        request.setAppId(apolloConfigEvent.getAppId());
+        request.setIds(Lists.newArrayList(id));
+        Result<List<OrgOperatorResponse>> result = operatorsQueryApi.queryOperator(request);
+        List<OrgOperatorVo> orgOperatorResponse = OperatorMapstruct.MAPSTRUCT.toOrgOperatorResponse(result.getData());
+        return orgOperatorResponse.get(0);
     }
 
     /**
@@ -48,12 +54,19 @@ public class OperatorComponent {
         if (CollectionUtils.isEmpty(ids)) {
             return new HashMap<>();
         }
+        List<OrgOperatorVo> orgOperatorResponse = queryOperatorByIds(ids);
+        return orgOperatorResponse.stream().collect(Collectors.toMap(OrgOperatorVo::getId, vo -> vo, (v1, v2) -> v1));
+    }
+
+    public List<OrgOperatorVo> queryOperatorByIds(List<Long> ids) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return new ArrayList<>();
+        }
         OrgOperatorQueryRequest request = new OrgOperatorQueryRequest();
         request.setAppId(apolloConfigEvent.getAppId());
         request.setIds(ids.stream().distinct().toList());
         Result<List<OrgOperatorResponse>> result = operatorsQueryApi.queryOperator(request);
-        List<OrgOperatorVo> orgOperatorResponse = OperatorMapstruct.MAPSTRUCT.toOrgOperatorResponse(result.getData());
-        return orgOperatorResponse.stream().collect(Collectors.toMap(OrgOperatorVo::getId, vo -> vo, (v1, v2) -> v1));
+        return OperatorMapstruct.MAPSTRUCT.toOrgOperatorResponse(result.getData());
     }
 
 }

+ 2 - 1
warehouse-admin-data/src/main/java/com/yr/warehouse/admin/driver/vo/DriverGoodsLossRecordVo.java

@@ -1,6 +1,7 @@
 package com.yr.warehouse.admin.driver.vo;
 
 import com.yr.warehouse.admin.areastaff.fase.AreaStaffIdFace;
+import com.yr.warehouse.admin.operator.face.OperatorNameFace;
 import lombok.Data;
 
 import java.io.Serial;
@@ -10,7 +11,7 @@ import java.io.Serializable;
  * 货损记录vo
  */
 @Data
-public class DriverGoodsLossRecordVo implements AreaStaffIdFace, Serializable {
+public class DriverGoodsLossRecordVo implements OperatorNameFace, AreaStaffIdFace, Serializable {
 
     @Serial
     private static final long serialVersionUID = 1L;

+ 16 - 0
warehouse-admin-data/src/main/java/com/yr/warehouse/admin/operator/face/OperatorNameFace.java

@@ -0,0 +1,16 @@
+package com.yr.warehouse.admin.operator.face;
+
+/**
+ * 运营商名称Face
+ */
+public interface OperatorNameFace {
+
+    Long getOperatorId();
+
+    void setOperatorName(String operatorName);
+
+    String getOperatorChain();
+
+    void setOperatorChainName(String operatorChainName);
+
+}

+ 5 - 8
warehouse-admin-server/src/main/java/com/yr/warehouse/admin/service/driver/impl/DriverGoodsLossRecordServiceImpl.java

@@ -14,6 +14,7 @@ import com.yr.warehouse.admin.driver.vo.DriverGoodsLossRecordVo;
 import com.yr.warehouse.admin.service.areastaff.AreaStaffService;
 import com.yr.warehouse.admin.service.driver.DriverGoodsLossRecordService;
 import com.yr.warehouse.admin.service.driver.mapstruct.DriverGoodsLossRecordMapStruct;
+import com.yr.warehouse.admin.service.operator.OperatorBuildService;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 
@@ -37,7 +38,7 @@ public class DriverGoodsLossRecordServiceImpl extends ServiceImpl<DriverGoodsLos
     private AreaStaffComponent areaStaffComponent;
 
     @Resource
-    private OperatorComponent operatorComponent;
+    private OperatorBuildService operatorBuildService;
 
     @Resource
     private AreaStaffService areaStaffService;
@@ -73,8 +74,6 @@ public class DriverGoodsLossRecordServiceImpl extends ServiceImpl<DriverGoodsLos
         if (null == vos || vos.isEmpty()) {
             return null;
         }
-        List<Long> operatorIds = vos.stream().map(DriverGoodsLossRecordVo::getOperatorId).collect(Collectors.toList());
-        Map<Long, OrgOperatorVo> operatorMap = operatorComponent.queryOperatorMapByIdList(operatorIds);
 
         Map<Long, AreaStaffResponse> areaStaffResponseMap = areaStaffService.searchAreaStaffMap(vos);
 
@@ -83,12 +82,10 @@ public class DriverGoodsLossRecordServiceImpl extends ServiceImpl<DriverGoodsLos
             if (null != areaStaffResponse) {
                 v.setAreaStaffName(areaStaffResponse.getEmployeeName());
             }
-            OrgOperatorVo operatorVo = operatorMap.get(v.getOperatorId());
-            if (null != operatorVo) {
-                v.setOperatorName(operatorVo.getOperatorName());
-                v.setOperatorChainName(operatorVo.getOperatorChainName());
-            }
         });
+
+        operatorBuildService.buildOperatorName(vos);
+
         return vos;
     }
 

+ 19 - 0
warehouse-admin-server/src/main/java/com/yr/warehouse/admin/service/operator/OperatorBuildService.java

@@ -0,0 +1,19 @@
+package com.yr.warehouse.admin.service.operator;
+
+import com.yr.warehouse.admin.operator.face.OperatorNameFace;
+
+import java.util.List;
+
+/**
+ * 运营商构建服务
+ */
+public interface OperatorBuildService {
+
+    /**
+     * 构建运营商名称
+     *
+     * @param operatorNameFaces
+     */
+    void buildOperatorName(List<? extends OperatorNameFace> operatorNameFaces);
+
+}

+ 66 - 0
warehouse-admin-server/src/main/java/com/yr/warehouse/admin/service/operator/impl/OperatorBuildServiceImpl.java

@@ -0,0 +1,66 @@
+package com.yr.warehouse.admin.service.operator.impl;
+
+import com.yr.bluecat.common.entity.utils.StringUtils;
+import com.yr.warehouse.admin.component.auth.OperatorComponent;
+import com.yr.warehouse.admin.component.auth.vo.OrgOperatorVo;
+import com.yr.warehouse.admin.operator.face.OperatorNameFace;
+import com.yr.warehouse.admin.service.operator.OperatorBuildService;
+import jakarta.annotation.Resource;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+public class OperatorBuildServiceImpl implements OperatorBuildService {
+    @Resource
+    private OperatorComponent operatorComponent;
+
+    @Override
+    public void buildOperatorName(List<? extends OperatorNameFace> operatorNameFaces) {
+        if (CollectionUtils.isEmpty(operatorNameFaces)) {
+            return;
+        }
+        List<Long> operatorIds = operatorNameFaces.stream().map(OperatorNameFace::getOperatorId).distinct().toList();
+        Map<Long, OrgOperatorVo> operatorVoMap = operatorComponent.queryOperatorMapByIdList(operatorIds);
+        if (CollectionUtils.isEmpty(operatorVoMap)) {
+            return;
+        }
+
+        List<Long> superOperatorIds = operatorNameFaces.stream().filter(aware -> Objects.nonNull(aware.getOperatorChain()))
+                .map(aware -> aware.getOperatorChain().split("-"))
+                .flatMap(array -> Arrays.stream(array).map(Long::parseLong)).distinct().toList();
+
+        if (CollectionUtils.isEmpty(superOperatorIds)) {
+            Map<Long, OrgOperatorVo> superOperatorMap = operatorVoMap.values().stream().collect(Collectors.toMap(OrgOperatorVo::getId, vo -> vo, (v1, v2) -> v1));
+            operatorNameFaces.forEach(face -> {
+                OrgOperatorVo operatorVo = superOperatorMap.get(face.getOperatorId());
+                face.setOperatorName(operatorVo.getOperatorName());
+            });
+            return;
+        }
+        Map<Long, OrgOperatorVo> orgOperators = operatorComponent.queryOperatorMapByIdList(superOperatorIds);
+        for (OperatorNameFace operatorNameFace : operatorNameFaces) {
+
+            OrgOperatorVo operatorVo = operatorVoMap.get(operatorNameFace.getOperatorId());
+            String operatorChain = operatorNameFace.getOperatorChain();
+            if (StringUtils.isBlank(operatorChain)) {
+                continue;
+            }
+            String[] operatorIdArray = operatorChain.split("-");
+            List<String> operatorChainName = new ArrayList<>();
+            for (String operatorId : operatorIdArray) {
+                long id = Long.parseLong(operatorId);
+                if (id == 0) {
+                    continue;
+                }
+                OrgOperatorVo orgOperatorVo = orgOperators.get(id);
+                operatorChainName.add(Objects.nonNull(orgOperatorVo) ? orgOperatorVo.getOperatorName() : "");
+
+            }
+            operatorNameFace.setOperatorName(operatorVo.getOperatorName());
+            operatorNameFace.setOperatorChainName(String.join(">>", operatorChainName));
+        }
+    }
+}

+ 28 - 0
warehouse-admin-web/src/main/java/com/yr/warehouse/admin/web/common/handler/CORSHandler.java

@@ -0,0 +1,28 @@
+package com.yr.warehouse.admin.web.common.handler;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+@Configuration
+public class CORSHandler {
+
+    @Bean
+    public CorsFilter corsFilter() {
+        CorsConfiguration config = new CorsConfiguration();
+        //允许白名单域名进行跨域调用
+        config.addAllowedOriginPattern("*");
+        //允许跨越发送cookie
+        config.setAllowCredentials(true);
+        //放行全部原始头信息
+        config.addAllowedHeader("*");
+        //允许所有请求方法跨域调用
+        config.addAllowedMethod("*");
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", config);
+        return new CorsFilter(source);
+    }
+
+}

+ 73 - 0
warehouse-admin-web/src/main/java/com/yr/warehouse/admin/web/common/handler/ExceptionAdviceHandler.java

@@ -0,0 +1,73 @@
+package com.yr.warehouse.admin.web.common.handler;
+
+import com.yr.bluecat.common.entity.exception.MessageException;
+import com.yr.bluecat.common.entity.response.Result;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.MissingServletRequestParameterException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 统一异常拦截
+ *
+ * @Description
+ * @Author pete
+ * @Date 2024/2/22 11:55
+ **/
+@Slf4j
+@Order(9999)
+@RestControllerAdvice
+public class ExceptionAdviceHandler {
+
+    /**
+     * 参数校验
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public Result<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
+        log.info("请求方式不正确:", e);
+        return Result.fail(e.getMessage());
+    }
+
+    /**
+     * 请求参数解析异常
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    public Result<String> httpMessageNotReadableException(HttpMessageNotReadableException e) {
+        log.info("请求参数格式不正确:", e);
+        return Result.fail("请求参数格式不正确:" + e.getMessage());
+    }
+
+    @ExceptionHandler(MissingServletRequestParameterException.class)
+    public Result<String> missingServletRequestParameterException(MissingServletRequestParameterException e) {
+        log.info("请求参数缺失", e);
+        return Result.fail("请求参数错误:" + e.getMessage());
+    }
+
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public Result<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
+        log.info("参数异常", e);
+        return Result.fail(e.getAllErrors().get(0).getDefaultMessage());
+    }
+
+    @ExceptionHandler(MessageException.class)
+    public Result<String> messageExceptionHandler(MessageException e) {
+        log.info("业务异常", e);
+        return Result.fail(e.getCode(), e.getMessage());
+    }
+
+    @ExceptionHandler(Exception.class)
+    public Result<String> exceptionHandler(Exception e) {
+        log.error("系统异常", e);
+        return Result.fail("系统开小差了!请稍后重试!");
+    }
+}