Commit a6c59d43 by HlQ

[add] 接收 Agent 发送的组织关系

1 parent 7e058801
......@@ -4,11 +4,11 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.dto.AgentDTO;
import vion.dto.monitor.AgentDTO;
import vion.model.monitor.RAgentService;
import vion.model.monitor.ServiceInfo;
import vion.service.monitor.IAgentService;
import vion.vo.AgentVO;
import vion.vo.monitor.AgentVO;
import java.util.List;
......@@ -60,7 +60,7 @@ public class AgentController {
return agentService.saveServicesByUid(uid, serviceInfoList);
}
@PostMapping("/service/{id}")
@PostMapping("/service/edit/{id}")
@SaCheckPermission(value = "agent:service:edit", orRole = "admin")
public String updateAgent2ServiceById(@PathVariable Long id, @RequestBody RAgentService rAgentService) {
return agentService.updateAgent2ServiceById(id, rAgentService);
......
......@@ -2,6 +2,7 @@ package vion.controller.mqtt;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.dto.monitor.OrgDTO;
import vion.model.monitor.Agent;
import vion.model.monitor.AgentRecord;
import vion.model.monitor.ServiceRecord;
......@@ -21,23 +22,58 @@ public class MonitorController {
private final IAgentService agentService;
/**
* Agent 注册
*
* @param agent agent自身信息
* @return java.lang.String
*/
@PostMapping("/reg")
public String reg(@RequestBody Agent agent) {
return agentService.reg(agent);
}
/**
* Agent 获取升级包信息
*
* @param type 1:store 2:mall
* @return Upgrade 升级包信息
*/
@GetMapping("/upgrade")
public Upgrade getUpgradeInfo(Short type) {
return agentService.getUpgradeInfo(type);
}
/**
* Agent 发送服务器信息
*
* @param agentRecord 服务器相关信息
* @return java.lang.String
*/
@PostMapping("/server")
public String recServerInfo(@RequestBody AgentRecord agentRecord) {
return agentService.recAgentRecord(agentRecord);
}
/**
* Agent 发送服务状态
*
* @param recList 服务状态
* @return java.lang.String
*/
@PostMapping("/service")
public String recServiceInfo(@RequestBody List<ServiceRecord> recList) {
return agentService.recServiceInfo(recList);
}
/**
* Agent 发送组织关系
*
* @param orgDTO account、mall
* @return java.lang.String
*/
@PostMapping("/org")
public String recOrgInfo(@RequestBody OrgDTO orgDTO) {
return agentService.recOrgInfo(orgDTO);
}
}
......@@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import vion.dto.MqttAuthDTO;
import vion.dto.monitor.MqttAuthDTO;
import vion.service.monitor.IAgentService;
/**
......@@ -35,5 +35,7 @@ public class MqttController {
}
@GetMapping("/stopEvent")
public void stopEvent() {agentService.stopEvent();}
public void stopEvent() {
agentService.stopEvent();
}
}
package vion.dto;
package vion.dto.monitor;
import lombok.Getter;
import lombok.Setter;
import vion.dto.BaseDTO;
/**
* @author vion
......
package vion.dto;
package vion.dto.monitor;
import lombok.Getter;
import lombok.Setter;
......
package vion.dto.monitor;
import lombok.Getter;
import lombok.Setter;
import vion.model.monitor.MAccount;
import vion.model.monitor.Mall;
import java.util.List;
/**
* @author vion
* @date 2024/11/1
*/
@Getter
@Setter
public class OrgDTO {
private String agentUid;
private Short agentType;
private List<MAccount> accountList;
private List<Mall> mallList;
}
......@@ -5,8 +5,8 @@ import io.github.linpeilie.annotations.AutoMapper;
import io.github.linpeilie.annotations.AutoMappers;
import lombok.Getter;
import lombok.Setter;
import vion.dto.AgentDTO;
import vion.vo.AgentVO;
import vion.dto.monitor.AgentDTO;
import vion.vo.monitor.AgentVO;
import java.time.LocalDateTime;
......
......@@ -37,8 +37,8 @@ public class Mall {
/**
* mall unid
*/
@TableField(value = "mall_uid")
private String mallUid;
@TableField(value = "uid")
private String uid;
/**
* mall名称
......
package vion.service.impl.monitor;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -30,16 +24,19 @@ import org.springframework.transaction.annotation.Transactional;
import vion.constant.AgentTypeEnum;
import vion.constant.EventStatusEnum;
import vion.constant.MqttTopic;
import vion.dto.AgentDTO;
import vion.dto.MqttAuthDTO;
import vion.dto.monitor.AgentDTO;
import vion.dto.monitor.MqttAuthDTO;
import vion.dto.monitor.OrgDTO;
import vion.mapper.monitor.AgentMapper;
import vion.model.monitor.*;
import vion.service.monitor.*;
import vion.utils.JsonUtil;
import vion.vo.AgentVO;
import vion.vo.monitor.AgentVO;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
......@@ -59,6 +56,8 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp
private final IUpgradeService upgradeService;
private final Converter converter;
private final MqttClientTemplate client;
private final IMAccountService accountService;
private final IMallService mallService;
@Value(value = "${mqtt.client.server-topic:/MS/receive}")
private String serverTopic;
......@@ -222,6 +221,52 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp
}
@Override
public String recOrgInfo(OrgDTO orgDTO) {
var agentUid = orgDTO.getAgentUid();
var agentType = orgDTO.getAgentType();
var accountList = orgDTO.getAccountList();
var mallList = orgDTO.getMallList();
var existAccList = accountService.lambdaQuery().eq(MAccount::getAgentUid, agentUid).list();
var uid2AccMap = existAccList.stream().collect(Collectors.toMap(MAccount::getUid, Function.identity()));
var existMallList = mallService.lambdaQuery().eq(Mall::getAgentUid, agentUid).list();
var uid2MallMap = existMallList.stream().collect(Collectors.toMap(Mall::getUid, Function.identity()));
List<MAccount> insAccList = new ArrayList<>();
List<MAccount> updAccList = new ArrayList<>();
accountList.forEach(a -> {
if (uid2AccMap.containsKey(a.getUid())) {
var acc = uid2AccMap.get(a.getUid());
a.setId(acc.getId());
updAccList.add(a);
} else {
a.setAgentUid(agentUid);
a.setAgentType(agentType);
insAccList.add(a);
}
});
List<Mall> insMallList = new ArrayList<>();
List<Mall> updMallList = new ArrayList<>();
mallList.forEach(m -> {
if (uid2MallMap.containsKey(m.getUid())) {
var acc = uid2MallMap.get(m.getUid());
m.setId(acc.getId());
updMallList.add(m);
} else {
m.setAgentUid(agentUid);
m.setAgentType(agentType);
insMallList.add(m);
}
});
Opt.ofEmptyAble(insAccList).ifPresent(accountService::saveBatch);
Opt.ofEmptyAble(insMallList).ifPresent(mallService::saveBatch);
Opt.ofEmptyAble(updAccList).ifPresent(accountService::updateBatchById);
Opt.ofEmptyAble(updMallList).ifPresent(mallService::updateBatchById);
return "success";
}
@Override
public ObjectNode auth(MqttAuthDTO dto) {
var clientid = dto.getClientid();
if (StrUtil.equals(clientid, "MS8011") || StrUtil.equals(clientid, "test")) {
......@@ -284,19 +329,18 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp
@Override
public String getAgentEventTopic(String agentUid) {
Agent agent = this.baseMapper.selectOne(new QueryWrapper<Agent>().eq("uid", agentUid));
if (agent == null) {
Agent agent = this.lambdaQuery().eq(Agent::getUid, agentUid).one();
if (ObjUtil.isNull(agent)) {
log.info("agent {} 不存在", agentUid);
return null;
}
if (agent.getType() == AgentTypeEnum.STORE.getCode()) {
if (ObjUtil.equals(agent.getType(), AgentTypeEnum.STORE.getCode())) {
return StrUtil.format(MqttTopic.STORE_EVENT_TOPIC, agentUid);
} else if (agent.getType() == AgentTypeEnum.MALL.getCode()) {
} else if (ObjUtil.equals(agent.getType(), AgentTypeEnum.MALL.getCode())) {
return StrUtil.format(MqttTopic.MALL_EVENT_TOPIC, agentUid);
} else {
log.info("未知的agent类型");
}
return null;
}
......
......@@ -49,7 +49,7 @@ public class UpgradeServiceImpl extends ServiceImpl<UpgradeMapper, Upgrade> impl
Assert.equals(version, upgrade.getVersion(), "升级包版本号不一致");
var name = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_TITLE);
Assert.equals(version, upgrade.getName(), "升级包名称不一致");
Assert.equals(name, upgrade.getName(), "升级包名称不一致");
upgrade.setVersion(version);
......
......@@ -5,10 +5,11 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.yulichang.base.MPJBaseService;
import org.springframework.web.bind.annotation.RequestBody;
import vion.dto.AgentDTO;
import vion.dto.MqttAuthDTO;
import vion.dto.monitor.AgentDTO;
import vion.dto.monitor.MqttAuthDTO;
import vion.dto.monitor.OrgDTO;
import vion.model.monitor.*;
import vion.vo.AgentVO;
import vion.vo.monitor.AgentVO;
import java.util.List;
......@@ -42,6 +43,8 @@ public interface IAgentService extends MPJBaseService<Agent> {
String recAgentRecord(AgentRecord agentRecord);
String recServiceInfo(List<ServiceRecord> recList);
String recOrgInfo(OrgDTO orgDTO);
// endregion
// region Mqtt Server 回调接口
......
package vion.vo;
package vion.vo.monitor;
import lombok.Getter;
import lombok.Setter;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!