add:增加统一外部调用服务
This commit is contained in:
@@ -32,13 +32,16 @@ public class TableDataInfo<T> implements Serializable {
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
private int code;
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 反馈数据
|
||||
*/
|
||||
private Object data;
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
@@ -52,7 +55,7 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build(IPage<T> page) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
rspData.setContent(page.getRecords());
|
||||
rspData.setTotalElements(page.getTotal());
|
||||
@@ -61,7 +64,7 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build(List<T> list) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
rspData.setContent(list);
|
||||
rspData.setTotalElements(list.size());
|
||||
@@ -70,9 +73,17 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build() {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
return rspData;
|
||||
}
|
||||
|
||||
public static <T> TableDataInfo<T> buildJson(Object result) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setData(result);
|
||||
rspData.setMsg("操作成功");
|
||||
return rspData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
public interface CallBack {
|
||||
/**
|
||||
* 回调执行方法
|
||||
*/
|
||||
void executor();
|
||||
|
||||
/**
|
||||
* 本回调任务名称
|
||||
* @return /
|
||||
*/
|
||||
default String getCallBackName() {
|
||||
return Thread.currentThread().getId() + ":" + this.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/12/14 8:40 下午
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LConsumer<X,Y,Z> {
|
||||
|
||||
void accept(X x,Y y,Z z);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@FunctionalInterface
|
||||
public
|
||||
interface LockProcess {
|
||||
void process() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@FunctionalInterface
|
||||
public
|
||||
interface ReturnLockProcess<T> {
|
||||
T process() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.nl.common.utils;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.domain.interfaces.LockProcess;
|
||||
import org.nl.common.domain.interfaces.ReturnLockProcess;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.config.SpringContextHolder;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/3/27 10:30
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedissonUtils {
|
||||
/**
|
||||
*
|
||||
* @param process 业务代码
|
||||
* @param key
|
||||
* @param seconds 尝试获取锁的等待时间,允许为空
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static void lock(LockProcess process, String key, Integer seconds){
|
||||
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
boolean isLock;
|
||||
if (seconds == null){
|
||||
isLock = lock.tryLock();
|
||||
}else {
|
||||
isLock = lock.tryLock(seconds, TimeUnit.SECONDS);
|
||||
}
|
||||
try {
|
||||
if (isLock){
|
||||
process.process();
|
||||
} else {
|
||||
throw new BadRequestException("当前业务:"+key+"正在执行请稍后再试");
|
||||
}
|
||||
}catch (Exception ex){
|
||||
log.error("key:"+ex.getMessage());
|
||||
throw ex;
|
||||
}finally {
|
||||
if (lock.isHeldByCurrentThread() && lock.isLocked()){
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static<T> T lockAndReturn(ReturnLockProcess<T> process, String key, Integer seconds){
|
||||
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
boolean isLock;
|
||||
if (seconds == null){
|
||||
isLock = lock.tryLock();
|
||||
}else {
|
||||
isLock = lock.tryLock(seconds, TimeUnit.SECONDS);
|
||||
}
|
||||
try {
|
||||
if (isLock){
|
||||
T result = process.process();
|
||||
return result;
|
||||
} else {
|
||||
throw new BadRequestException("当前业务 key:"+key+"正在执行请稍后再试");
|
||||
}
|
||||
}finally {
|
||||
if (isLock){
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.nl.wms.gateway.controller;
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.logging.annotation.Log;
|
||||
import org.nl.wms.gateway.service.GateWayService;
|
||||
import org.nl.wms.gateway.dto.InteracteDto;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.nl.common.base.TableDataInfo;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 网关执行控制层
|
||||
*
|
||||
* @author gbx
|
||||
* @since 2025-06-27
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/wms/out")
|
||||
@Slf4j
|
||||
public class GateWayController {
|
||||
|
||||
@Resource
|
||||
private GateWayService gateWayService;
|
||||
@PostMapping("/apply")
|
||||
@SaIgnore
|
||||
@Log("外层服务请求wms")
|
||||
public ResponseEntity<Object> apply(@RequestBody InteracteDto form) {
|
||||
return new ResponseEntity<>(TableDataInfo.buildJson(gateWayService.apply(form)),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.nl.wms.gateway.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/5/29 16:26
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class InteracteDto<T> {
|
||||
|
||||
private String service;
|
||||
private String type;
|
||||
private String ip;
|
||||
private String request_time;
|
||||
private String trace_id;
|
||||
/**
|
||||
* 要么JSONArray要么JSONObject
|
||||
*/
|
||||
private T data;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.nl.wms.gateway.service;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.common.utils.RedissonUtils;
|
||||
import org.nl.wms.ext.service.AcsToWmsService;
|
||||
import org.nl.wms.gateway.dto.InteracteDto;
|
||||
import org.nl.wms.gateway.service.impl.GateWayServiceImpl;
|
||||
import org.nl.wms.sch_manage.service.ISchBasePointService;
|
||||
import org.nl.wms.sch_manage.service.ISchBaseTaskService;
|
||||
import org.nl.wms.sch_manage.service.dao.SchBasePoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* 网关执行接口层
|
||||
*
|
||||
* @author gbx
|
||||
* @since 2025-06-27
|
||||
*/
|
||||
@Service
|
||||
public class GateWayService {
|
||||
|
||||
@Autowired
|
||||
private GateWayServiceImpl gateWayServiceImpl;
|
||||
@Autowired
|
||||
private ISchBasePointService iSchBasePointService;
|
||||
@Autowired
|
||||
private ISchBaseTaskService iSchBaseTaskService;
|
||||
|
||||
|
||||
public JSONObject apply(InteracteDto<Map> param) {
|
||||
JSONObject jsonObject = new JSONObject(param.getData());
|
||||
//处理日志相关
|
||||
JSONObject result = new JSONObject();
|
||||
String service = param.getService();
|
||||
String type = param.getType();
|
||||
//根据服务拆分不同的业务
|
||||
if ("InStorage".equals(service)) {
|
||||
RedissonUtils.lock(() -> {
|
||||
String taskCode = gateWayServiceImpl.applyTask(param.getService(), type, jsonObject, param);
|
||||
result.put("taskCode", taskCode);
|
||||
}, param.getService() + param.getType(), null);
|
||||
}
|
||||
if ("Task".equals(service)) {
|
||||
iSchBaseTaskService.operation(jsonObject);
|
||||
}
|
||||
if ("DeviceInfo".equals(service)) {
|
||||
|
||||
}
|
||||
if ("Device".equals(service)) {
|
||||
Assert.noNullElements(new Object[]{jsonObject.getString("devicePoint"), jsonObject.getString("status")}, "请求参数不能为空");
|
||||
if (!"1207".equals(jsonObject.getString("devicePoint")) && !"1210".equals(jsonObject.getString("devicePoint"))) {
|
||||
throw new BadRequestException("您输入的拣选位不存在,请输入1207或1210拣选位!");
|
||||
}
|
||||
RedissonUtils.lock(() -> {
|
||||
LambdaUpdateWrapper<SchBasePoint> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
updateWrapper.set(SchBasePoint::getIs_used, "0".equals(jsonObject.getString("status")) ? 0 : 1);
|
||||
iSchBasePointService.update(updateWrapper);
|
||||
}, param.getService() + param.getType(), null);
|
||||
}
|
||||
if ("ErrorInfo".equals(service)) {
|
||||
Assert.noNullElements(new Object[]{type, jsonObject.getString("msg")}, "请求参数不能为空");
|
||||
String msg = jsonObject.getString("msg").trim();
|
||||
//iSchBasePointService.sendErrorMsg(type,null, msg);
|
||||
}
|
||||
if ("ErrorTask".equals(service)) {
|
||||
AtomicReference<JSONObject> reference = new AtomicReference<>(new JSONObject());
|
||||
RedissonUtils.lock(() -> {
|
||||
// reference.set(iSchBaseTaskService.errorTask(jsonObject, param.getType()));
|
||||
}, param.getService() + param.getType(), null);
|
||||
return reference.get();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.nl.wms.gateway.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.wms.gateway.dto.InteracteDto;
|
||||
import org.nl.wms.sch_manage.service.ISchBasePointService;
|
||||
import org.nl.wms.sch_manage.service.ISchBaseTaskService;
|
||||
import org.nl.wms.sch_manage.service.util.AbstractTask;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 网关执行实现层
|
||||
*
|
||||
* @author gbx
|
||||
* @since 2025-06-27
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class GateWayServiceImpl {
|
||||
|
||||
|
||||
@Resource
|
||||
private Map<String, AbstractTask> applyTaskMap;
|
||||
|
||||
public String applyTask(String service, String type, JSONObject data, InteracteDto<Map> param) {
|
||||
if ("InStorage".equals(service)) {
|
||||
try {
|
||||
String taskId = applyTaskMap.get(type).create(data);
|
||||
if (StringUtils.isBlank(taskId)) {
|
||||
log.error("创建任务失败:返回任务信息为空,申请参数为:" + JSON.toJSONString(param));
|
||||
throw new BadRequestException("创建任务失败:返回任务信息为空,申请参数为:" + JSON.toJSONString(param));
|
||||
}
|
||||
return taskId;
|
||||
} catch (Exception e) {
|
||||
if (param.getData() != null) {
|
||||
String title = param.getType() + DateUtil.today() + param.getData().toString();
|
||||
// iSchBasePointService.sendErrorMsg("1", title, "任务申请失败,申请参数为:" + JSON.toJSONString(param) + "请查看错误日志" + e);
|
||||
}
|
||||
throw new BadRequestException("申请任务失败,申请参数为:" + JSON.toJSONString(param) + "请查看错误日志:" + e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class AutoTask {
|
||||
private TaskFactory taskFactory;
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
//定时任务
|
||||
@SneakyThrows
|
||||
public void run() {
|
||||
RLock lock = redissonClient.getLock(this.getClass().getName());
|
||||
@@ -54,7 +54,7 @@ public class AutoTask {
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发任务
|
||||
* 定时下发任务
|
||||
*/
|
||||
private void sendTask() {
|
||||
List<SchBaseTask> taskList = taskService.list(new LambdaQueryWrapper<SchBaseTask>()
|
||||
|
||||
Reference in New Issue
Block a user