Browse Source

feat(all): 增加我乐自定义数据推送

dist.1.3.0
zhaoqiaobo 10 months ago
parent
commit
ff16d043e9
  1. 93
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/JobAnnotationAspect.java
  2. 74
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/XxlJobConfig.java
  3. 36
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/job/PushDataJob.java
  4. 3
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/mapper/DeliveryNoteMapper.java
  5. 15
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/mapper/FactoryNodeOrderMapper.xml
  6. 9
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/service/DeliveryNoteService.java
  7. 36
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/service/impl/DeliveryNoteServiceImpl.java

93
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/JobAnnotationAspect.java

@ -0,0 +1,93 @@
package com.logpm.factorydata.olo.aspect;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant;
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.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect
@Component
@AllArgsConstructor
public class JobAnnotationAspect {
private final BladeRedis bladeRedis;
private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
JSONObject jsonObject = mockLogin();
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token"));
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") );
httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
ThreadLocalUtil.put("bladeContext", httpHeaders);
DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id"));
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
private JSONObject mockLogin() throws InterruptedException {
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<String, Object> 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);
}
}
}
return data;
}
}

74
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/XxlJobConfig.java

@ -0,0 +1,74 @@
package com.logpm.factorydata.olo.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration(proxyBeanMethods = false)
public class XxlJobConfig {
private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
/**
* 针对多网卡容器内部署等情况可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP
*
* 1引入依赖
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2配置文件或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}

36
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/job/PushDataJob.java

@ -0,0 +1,36 @@
package com.logpm.factorydata.olo.job;
import com.logpm.factorydata.olo.service.DeliveryNoteService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author zhaoqiaobo
* @create 2024-04-02
*/
@AllArgsConstructor
@Component
@Slf4j
public class PushDataJob {
private final DeliveryNoteService deliveryNoteService;
/**
* 定时扫描我乐手动维护的数据推送给我乐
*
* @param param
* @return
* @throws Exception
*/
@XxlJob("pushNodeData")
public ReturnT<String> pushDataJob(String param) throws Exception {
deliveryNoteService.customPushNodeData();
return ReturnT.SUCCESS;
}
}

3
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/mapper/DeliveryNoteMapper.java

@ -1,5 +1,6 @@
package com.logpm.factorydata.olo.mapper;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.logpm.factorydata.olo.entity.DeliveryNoteEntity;
import org.apache.ibatis.annotations.Mapper;
@ -13,4 +14,6 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DeliveryNoteMapper extends BaseMapper<DeliveryNoteEntity> {
JSONObject customPushNodeData();
}

15
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/mapper/FactoryNodeOrderMapper.xml

@ -2,4 +2,19 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.logpm.factorydata.olo.mapper.DeliveryNoteMapper">
<select id="customPushNodeData" resultType="cn.hutool.json.JSONObject">
select status,
opTime,
content,
orderNo,
deliveryNo,
nodeName,
shipmentSplitNo,
shipmentNo,
group_concat(sn) sns
from olo_custom_push_data
where sendStatus = 0
group by orderNo,deliveryNo
limit 1
</select>
</mapper>

9
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/service/DeliveryNoteService.java

@ -17,4 +17,13 @@ public interface DeliveryNoteService extends BaseService<DeliveryNoteEntity> {
String order(List<DeliveryNoteVO> vo);
void buildAdvance(List<DeliveryNoteEntity> entities);
/**
* 我乐自定义数据推送
* @return void
* @author zqb 2024/5/29
**/
void customPushNodeData();
}

36
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/service/impl/DeliveryNoteServiceImpl.java

@ -7,6 +7,8 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.logpm.factorydata.base.feign.IFactoryDataBaseClient;
@ -17,9 +19,11 @@ import com.logpm.factorydata.feign.IFactoryDataClient;
import com.logpm.factorydata.olo.entity.DeliveryNoteEntity;
import com.logpm.factorydata.olo.entity.FactoryOrderLogEntity;
import com.logpm.factorydata.olo.mapper.DeliveryNoteMapper;
import com.logpm.factorydata.olo.pros.OldProperties;
import com.logpm.factorydata.olo.service.DeliveryNoteService;
import com.logpm.factorydata.olo.service.FactoryOrderLogService;
import com.logpm.factorydata.olo.vo.DeliveryNoteVO;
import com.logpm.factorydata.vo.PushData;
import com.logpm.factorydata.vo.SendMsg;
import com.logpm.trunkline.entity.TrunklineAdvanceDetailEntity;
import com.logpm.trunkline.entity.TrunklineAdvanceEntity;
@ -70,6 +74,8 @@ public class DeliveryNoteServiceImpl extends BaseServiceImpl<DeliveryNoteMapper,
private final FactoryOrderLogService factoryOrderLogService;
private final IOssClient ossClient;
private final IFactoryDataBaseClient baseClient;
private final OldProperties oldProperties;
private final RabbitTemplate rabbitTemplate;
@Override
public String order(List<DeliveryNoteVO> vo) {
@ -228,6 +234,36 @@ public class DeliveryNoteServiceImpl extends BaseServiceImpl<DeliveryNoteMapper,
}
}
@Override
public void customPushNodeData() {
JSONObject js = this.baseMapper.customPushNodeData();
String sns = js.getStr("sns");
List<String> pushDatas = StrUtil.split(sns, ",");
js.set("siteLevel", 3);
js.set("receiveOrSend", 1);
js.set("receivable", 0);
js.set("receipts", 0);
JSONArray snArray = new JSONArray(pushDatas.size());
for (String data : pushDatas) {
JSONObject jsonObject = new JSONObject();
jsonObject.set("sn", data);
jsonObject.set("qty", 1);
snArray.add(jsonObject);
}
js.set("snList", snArray);
js.remove("sns");
if (StrUtil.isNotEmpty(oldProperties.getPushNodeUrl())) {
try {
log.info("自定义推送节点数据:{}", js.toString());
String post = HttpUtil.post(oldProperties.getPushNodeUrl(), js.toString());
log.info("推送结果:{}", post);
} catch (Exception e) {
e.printStackTrace();
log.error("推送节点数据错误:{}", e);
}
}
}
private String uploadFile(String body) {
//文本内容和保存为本地文件 并上传
String logPath = FileLogsUtil.saveFileLogs(body);

Loading…
Cancel
Save