From b5b107f7defc0792e82fd4e94354b56c45457d4e Mon Sep 17 00:00:00 2001 From: zhenghaoyu Date: Wed, 5 Jun 2024 11:28:13 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=A1=A5=E6=89=93=E6=89=98=E7=9B=98=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=80=BB=E8=BE=91=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/SyncOldTrayTypeLogEntity.java | 3 + .../config/InterceptorAdapterConfig.java | 28 +++++ .../LocalServerLoginAccountsInterceptor.java | 112 ++++++++++++++++++ .../patch/jobhandle/SyncOldTrayTypeData.java | 2 + .../logpm/patch/mapper/OldTrayTypeMapper.java | 2 + .../logpm/patch/mapper/OldTrayTypeMapper.xml | 7 ++ .../patch/service/IOldTrayTypeService.java | 2 + .../service/impl/OldTrayTypeServiceImpl.java | 5 + .../mapper/WarehouseUpdownTypeMapper.xml | 3 +- .../receiver/OldTrayTypeListener.java | 1 + 10 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 blade-service/logpm-patch/src/main/java/com/logpm/patch/config/InterceptorAdapterConfig.java create mode 100644 blade-service/logpm-patch/src/main/java/com/logpm/patch/interceptor/LocalServerLoginAccountsInterceptor.java diff --git a/blade-service-api/logpm-warehouse-api/src/main/java/com/logpm/warehouse/entity/SyncOldTrayTypeLogEntity.java b/blade-service-api/logpm-warehouse-api/src/main/java/com/logpm/warehouse/entity/SyncOldTrayTypeLogEntity.java index 822f50b07..79e890d59 100644 --- a/blade-service-api/logpm-warehouse-api/src/main/java/com/logpm/warehouse/entity/SyncOldTrayTypeLogEntity.java +++ b/blade-service-api/logpm-warehouse-api/src/main/java/com/logpm/warehouse/entity/SyncOldTrayTypeLogEntity.java @@ -51,4 +51,7 @@ public class SyncOldTrayTypeLogEntity extends TenantEntity { @ApiModelProperty(value = "同步次数") private Integer syncNum; + @ApiModelProperty(value = "队列状态") + private Integer mqStatus; + } diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/config/InterceptorAdapterConfig.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/config/InterceptorAdapterConfig.java new file mode 100644 index 000000000..83e7c2a59 --- /dev/null +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/config/InterceptorAdapterConfig.java @@ -0,0 +1,28 @@ +package com.logpm.patch.config; + +import com.logpm.patch.interceptor.LocalServerLoginAccountsInterceptor; +import lombok.AllArgsConstructor; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.redis.lock.RedisLockClient; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@AllArgsConstructor +public class InterceptorAdapterConfig implements WebMvcConfigurer { + + private final BladeRedis redis; + + private final Environment environment; + + private final RedisLockClient redisLockClient; + + + @Override + public void addInterceptors(InterceptorRegistry interceptorRegistry) { + interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,environment,redisLockClient)).addPathPatterns("/**").order(1); + + } +} diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/interceptor/LocalServerLoginAccountsInterceptor.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/interceptor/LocalServerLoginAccountsInterceptor.java new file mode 100644 index 000000000..9bdc18823 --- /dev/null +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/interceptor/LocalServerLoginAccountsInterceptor.java @@ -0,0 +1,112 @@ +package com.logpm.patch.interceptor; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springblade.common.cache.CacheNames; +import org.springblade.common.constant.LauncherConstant; +import org.springblade.common.exception.CustomerException; +import org.springblade.common.wrapper.CustomHttpServletRequestWrapper; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.redis.lock.LockType; +import org.springblade.core.redis.lock.RedisLockClient; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +@Log4j2 +@AllArgsConstructor +public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapter { + + private final BladeRedis bladeRedis; + private final Environment environment; + private final RedisLockClient redisLockClient; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException { + + + try { + CustomHttpServletRequestWrapper wrappedRequest = new CustomHttpServletRequestWrapper(request); + String account ="shujutongbu"; + JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS); + if(flag){ + data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; + HttpRequest urlRequest = HttpRequest.post(url); + urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + urlRequest.header("Tenant-Id", "627683"); + + Map params = new HashMap<>(); + params.put("grant_type", "local_server"); + params.put("scope", "all"); + params.put("username", account); + params.put("tenantId", "627683"); + HttpResponse execute = urlRequest.form(params).execute(); + String body = execute.body(); + data = JSONObject.parseObject(body); + bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); + redisLockClient.unLock("local_server_user", LockType.FAIR); + } + } + } + + // 修改或添加header + + // 发送登录请求 + log.info("##########preHandle: request={}", request); + + wrappedRequest.addHeader("Blade-Auth", "bearer "+data.getString("access_token")); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Blade-Auth","bearer "+data.get("access_token") ); + httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + ThreadLocalUtil.put("bladeContext", httpHeaders); + + + // 用包装后的request替换原始request + request = wrappedRequest; + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + return true; + } catch (Exception e) { + returnJson(response, JSONObject.toJSONString(R.fail(500, "服务异常,请联系管理员"))); + return false; + } + } + + private void returnJson(HttpServletResponse response, String json) { + PrintWriter writer = null; + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + try { + writer = response.getWriter(); + writer.print(json); + + } catch (IOException e) { + System.out.println(e.getMessage()); + } finally { + if (writer != null) { + writer.close(); + } + } + } + + +} diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/jobhandle/SyncOldTrayTypeData.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/jobhandle/SyncOldTrayTypeData.java index 61a52d835..d26822a79 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/jobhandle/SyncOldTrayTypeData.java +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/jobhandle/SyncOldTrayTypeData.java @@ -32,6 +32,8 @@ public class SyncOldTrayTypeData { for (SyncOldTrayTypeLogEntity syncOldTrayTypeLogEntity : ls) { String args = syncOldTrayTypeLogEntity.getArgs(); Long id = syncOldTrayTypeLogEntity.getId(); + syncOldTrayTypeLogEntity.setMqStatus(1); + oldTrayTypeService.updateMqStatus(id, 1); Integer syncNum = syncOldTrayTypeLogEntity.getSyncNum(); log.info("##############syncOldTrayTypeInfo: 当前同步的数据为 {}",args); JSONObject jsonObject = JSONObject.parseObject(args); diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.java index 604b5954b..d3ae4e6e1 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.java +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.java @@ -13,4 +13,6 @@ public interface OldTrayTypeMapper { void updateSyncStatus(@Param("id") Long id, @Param("syncStatus") int syncStatus); void updateSyncNum(@Param("id") Long id, @Param("syncNum") int syncNum); + + void updateMqStatus(@Param("id") Long id, @Param("mqStatus") int mqStatus); } diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.xml b/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.xml index 25c58c4f2..bf43fefff 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.xml +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/mapper/OldTrayTypeMapper.xml @@ -7,6 +7,7 @@ from sync_old_tray_type_log where sync_status = #{syncStatus} and sync_num < #{syncNum} + and mq_status = 0 @@ -21,4 +22,10 @@ where id = #{id} + + update sync_old_tray_type_log + set mq_status = #{mqStatus} + where id = #{id} + + diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/IOldTrayTypeService.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/IOldTrayTypeService.java index e7c35f4a2..060ee1e4d 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/IOldTrayTypeService.java +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/IOldTrayTypeService.java @@ -10,4 +10,6 @@ public interface IOldTrayTypeService { void updateSyncStatus(Long id, int syncStatus); void updateSyncNum(Long id, int syncNum); + + void updateMqStatus(Long id, int mqStatus); } diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/impl/OldTrayTypeServiceImpl.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/impl/OldTrayTypeServiceImpl.java index d57d885fa..b47c78cba 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/impl/OldTrayTypeServiceImpl.java +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/service/impl/OldTrayTypeServiceImpl.java @@ -31,4 +31,9 @@ public class OldTrayTypeServiceImpl implements IOldTrayTypeService { public void updateSyncNum(Long id, int syncNum) { oldTrayTypeMapper.updateSyncNum(id,syncNum); } + + @Override + public void updateMqStatus(Long id, int mqStatus) { + oldTrayTypeMapper.updateMqStatus(id,mqStatus); + } } diff --git a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/mapper/WarehouseUpdownTypeMapper.xml b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/mapper/WarehouseUpdownTypeMapper.xml index c7c88ec91..0a39d8864 100644 --- a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/mapper/WarehouseUpdownTypeMapper.xml +++ b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/mapper/WarehouseUpdownTypeMapper.xml @@ -86,7 +86,8 @@ update sync_old_tray_type_log - set sync_num = sync_num + 1 + set sync_num = sync_num + 1, + mq_status = 0 where id = #{oldTrayTypeId} diff --git a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/receiver/OldTrayTypeListener.java b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/receiver/OldTrayTypeListener.java index 0e4c891d5..a92169a2b 100644 --- a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/receiver/OldTrayTypeListener.java +++ b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/receiver/OldTrayTypeListener.java @@ -71,6 +71,7 @@ public class OldTrayTypeListener { syncOldTrayTypeLogEntity.setArgs(com.alibaba.fastjson.JSONObject.toJSONString(trayInfoDTO)); syncOldTrayTypeLogEntity.setSyncStatus(0); syncOldTrayTypeLogEntity.setSyncNum(0); + syncOldTrayTypeLogEntity.setMqStatus(0); syncOldTrayTypeLogService.save(syncOldTrayTypeLogEntity); } }