Commit f2b5ee4e authored by “yiyousong”'s avatar “yiyousong”
parents 3a86d198 78d23419
...@@ -54,6 +54,16 @@ ...@@ -54,6 +54,16 @@
</dependency> </dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
......
package com.mortals.xhx.common.keys;
/**
* rabbit 队列key定义
*/
public class QueueKey {
public static final String ACCESS_LOG_QUEUE = "ACCESS_LOG_QUEUE";
public static final String BIZ_LOG_QUEUE = "BIZ_LOG_QUEUE";
public static final String ERROR_LOG_QUEUE = "ERROR_LOG_QUEUE";
public static final String OPERATION_LOG_QUEUE = "OPERATION_LOG_QUEUE";
public static final String EXCHANGE = "LOG";
public static final String ROUTING_KEY = "LOG_ROUTING_KEY";
}
package com.mortals.xhx.common.pdu.device; package com.mortals.xhx.common.pdu.device;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
......
package com.mortals.xhx.common.pdu.firm;
import java.util.Date;
import java.util.List;
import com.mortals.framework.model.BaseEntityLong;
import lombok.Data;
/**
* 设备生产厂商Pdu对象
*
* @author zxfei
* @date 2023-06-21
*/
@Data
public class FirmPdu extends BaseEntityLong {
private static final long serialVersionUID = 1L;
/**
* 设备生产厂商名称
*/
private String firmName;
/**
* 设备生产商编码
*/
private String firmCode;
/**
* 备注
*/
private String firmRemark;
public void initAttrValue(){
this.firmName = "";
this.firmCode = "";
this.firmRemark = "";
}
}
\ No newline at end of file
package com.mortals.xhx.feign.firm;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.firm.FirmPdu;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.feign.IFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 设备生产厂商 Feign接口
* @author zxfei
* @date 2023-06-21
*/
@FeignClient(name = "device-manager", path = "/m", fallbackFactory = FirmFeignFallbackFactory.class)
public interface IFirmFeign extends IFeign {
/**
* 查看设备生产厂商列表
*
* @param firmPdu
* @return
*/
@PostMapping(value = "/firm/list")
Rest<RespData<List<FirmPdu>>> list(@RequestBody FirmPdu firmPdu);
/**
* 查看设备生产厂商
*
* @param id
* @return
*/
@GetMapping(value = "/firm/info")
Rest<FirmPdu> info(@RequestParam(value = "id") Long id);
/**
* 删除设备生产厂商
*
* @param ids
* @return
*/
@GetMapping(value = "/firm/delete")
Rest<Void> delete(Long[] ids,@RequestHeader("Authorization") String authorization);
/**
* 设备生产厂商保存更新
*
* @param firmPdu
* @return
*/
@PostMapping(value = "/firm/save")
Rest<RespData<FirmPdu>> save(@RequestBody FirmPdu firmPdu,@RequestHeader("Authorization") String authorization);
}
@Slf4j
@Component
class FirmFeignFallbackFactory implements FallbackFactory<IFirmFeign> {
@Override
public IFirmFeign create(Throwable t) {
return new IFirmFeign() {
@Override
public Rest<RespData<List<FirmPdu>>> list(FirmPdu firmPdu) {
return Rest.fail("暂时无法获取设备生产厂商列表,请稍后再试!");
}
@Override
public Rest<FirmPdu> info(Long id) {
return Rest.fail("暂时无法获取设备生产厂商详细,请稍后再试!");
}
@Override
public Rest<Void> delete(Long[] ids, String authorization) {
return Rest.fail("暂时无法删除设备生产厂商,请稍后再试!");
}
@Override
public Rest<RespData<FirmPdu>> save(FirmPdu firmPdu, String authorization) {
return Rest.fail("暂时无法保存设备生产厂商,请稍后再试!");
}
};
}
}
package com.mortals.xhx.system;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.model.AccessLogPdu;
import com.mortals.framework.model.BizLogPdu;
import com.mortals.framework.model.ErrorLogPdu;
import com.mortals.framework.model.OperateLogPdu;
import com.mortals.framework.service.IMessageProduceService;
import com.mortals.xhx.common.keys.QueueKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
@Slf4j
public class MessageProducer implements IMessageProduceService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void syncAccessSend(AccessLogPdu accessLogPdu) {
//new Message(JSON.toJSONString(accessLogPdu).getBytes(StandardCharsets.UTF_8))
//rabbitTemplate.send(QueueKey.EXCHANGE, QueueKey.ACCESS_LOG_QUEUE,new Message(JSON.toJSONString(accessLogPdu).getBytes(StandardCharsets.UTF_8)));
rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.ACCESS_LOG_QUEUE, JSON.toJSONString(accessLogPdu));
//rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.ACCESS_LOG_QUEUE, accessLogPdu);
}
@Override
public void syncBizSend(BizLogPdu bizLogPdu) {
rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.BIZ_LOG_QUEUE, JSON.toJSONString(bizLogPdu));
}
@Override
public void syncErrorSend(ErrorLogPdu errorLogPdu) {
rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.ERROR_LOG_QUEUE, JSON.toJSONString(errorLogPdu));
}
@Override
public void syncOperSend(OperateLogPdu operLogPdu) {
rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.OPERATION_LOG_QUEUE, JSON.toJSONString(operLogPdu));
}
}
-- ----------------------------
2023-06-21
-- ----------------------------
ALTER TABLE mortals_xhx_device
ADD COLUMN `deviceFirmId` bigint(20) DEFAULT NULL COMMENT '设备生产厂商ID' AFTER siteName;
ALTER TABLE mortals_xhx_device
ADD COLUMN `deviceFirmname` varchar(200) DEFAULT NULL COMMENT '设备生产厂商名称' AFTER deviceFirmId;
ALTER TABLE mortals_xhx_device
ADD COLUMN `ip` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '设备访问ip' AFTER deviceFirmname;
ALTER TABLE mortals_xhx_device
ADD COLUMN `port` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '端口' AFTER ip;
ALTER TABLE mortals_xhx_device
ADD COLUMN `enabled` tinyint(2) DEFAULT NULL COMMENT '启用状态 (0.停止,1.启用)' AFTER port;
ALTER TABLE mortals_xhx_device
ADD COLUMN `deviceRemark` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '备注' AFTER enabled;
ALTER TABLE mortals_xhx_device
ADD COLUMN `onlineTime` datetime DEFAULT NULL COMMENT '最近上线时间' AFTER deviceRemark;
ALTER TABLE mortals_xhx_device
ADD COLUMN `offlineTime` datetime DEFAULT NULL COMMENT '最近离线时间' AFTER onlineTime;
\ No newline at end of file
...@@ -4,6 +4,11 @@ import java.util.Date; ...@@ -4,6 +4,11 @@ import java.util.Date;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import cn.hutool.core.util.StrUtil;
import com.mortals.framework.model.OperateLogPdu;
import com.mortals.framework.service.IMessageProduceService;
import com.mortals.xhx.common.code.OperTypeEnum;
import com.mortals.xhx.system.MessageProducer;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
...@@ -25,25 +30,46 @@ import com.mortals.xhx.base.system.oper.service.OperLogService; ...@@ -25,25 +30,46 @@ import com.mortals.xhx.base.system.oper.service.OperLogService;
* 操作日志记录 * 操作日志记录
*/ */
@Component @Component
public class OperlogAspect extends FileLogServiceImpl implements ILogService { public class OperlogAspect extends FileLogServiceImpl implements ILogService {
private final static Logger logger = LoggerFactory.getLogger(OperlogAspect.class); private final static Logger logger = LoggerFactory.getLogger(OperlogAspect.class);
@Autowired @Autowired
private OperLogService operLogService; private OperLogService operLogService;
@Autowired
private IMessageProduceService messageProducer;
@Override @Override
public void doHandlerLog(String platformMark, Long userId, String userName, String loginName, String requestUrl, public void doHandlerLog(String platformMark, Long userId, String userName, String loginName, String requestUrl,
String content, String ip, Date logDate) { String content, String ip, Date logDate) {
super.doHandlerLog(platformMark, userId, userName, loginName, requestUrl, content, ip, logDate); super.doHandlerLog(platformMark, userId, userName, loginName, requestUrl, content, ip, logDate);
if(ObjectUtils.isEmpty(userId)) return;
operLogService.insertOperLog(ip, requestUrl, userId, userName, loginName, content); operLogService.insertOperLog(ip, requestUrl, userId, userName, loginName, content);
int operType = OperTypeEnum.OTHER.getValue();
//判断内容包含
if (StrUtil.contains(content, "查询") || StrUtil.contains(content, "查看")) {
operType = OperTypeEnum.SEARCH.getValue();
} else if (StrUtil.contains(content, "保存") || StrUtil.contains(content, "新增") || StrUtil.contains(content, "修改")) {
operType = OperTypeEnum.SAVE.getValue();
} else if (StrUtil.contains(content, "删除")) {
operType = OperTypeEnum.DELETE.getValue();
}
OperateLogPdu operateLogPdu = new OperateLogPdu();
operateLogPdu.initAttrValue();
operateLogPdu.setIp(ip);
operateLogPdu.setRequestUrl(requestUrl);
operateLogPdu.setUserId(userId);
operateLogPdu.setUserName(userName);
operateLogPdu.setLoginName(loginName);
operateLogPdu.setPlatformMark(platformMark);
operateLogPdu.setLogDate(logDate);
operateLogPdu.setContent(content);
operateLogPdu.setOperType(operType);
messageProducer.syncOperSend(operateLogPdu);
} }
@Override @Override
public void doHandlerLog(String platformMark, String loginName, String requestUrl, String content, String ip) { public void doHandlerLog(String platformMark, String loginName, String requestUrl, String content, String ip) {
// operLogService.insertOperLog(ip, requestUrl, null, "", loginName,
// content);
this.doHandlerLog(platformMark, null, "", loginName, requestUrl, content, ip, new Date()); this.doHandlerLog(platformMark, null, "", loginName, requestUrl, content, ip, new Date());
} }
...@@ -59,7 +85,6 @@ public class OperlogAspect extends FileLogServiceImpl implements ILogService { ...@@ -59,7 +85,6 @@ public class OperlogAspect extends FileLogServiceImpl implements ILogService {
// url // url
logger.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL()); logger.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL());
// 参数第1和第2个参数为HttpServletRequest request, HttpServletResponse // 参数第1和第2个参数为HttpServletRequest request, HttpServletResponse
// response
if (joinPoint.getArgs().length > 2) { if (joinPoint.getArgs().length > 2) {
logger.info("args={}", joinPoint.getArgs()[2]); logger.info("args={}", joinPoint.getArgs()[2]);
} else { } else {
......
package com.mortals.xhx.busiz.web;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.common.code.DeviceMethodEnum;
import com.mortals.xhx.common.code.DeviceStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.device.DeviceReq;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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 java.util.Date;
import static com.mortals.xhx.common.key.ErrorCode.*;
@RestController
@Slf4j
@RequestMapping("/api/device")
public class DeviceCallbackController {
@Autowired
private DeviceService deviceService;
@PostMapping("callback")
@UnAuth
public Rest<String> callback(@RequestBody DeviceReq req) {
log.info("【设备接收】【请求体】--> " + JSONObject.toJSONString(req));
StringBuilder message = new StringBuilder();
message.append(String.format("【外部请求】类型【%s】 内容:%s", DeviceMethodEnum.getByValue(req.getReceiveMethod()).getDesc(), JSONObject.toJSONString(req)));
try {
switch (DeviceMethodEnum.getByValue(req.getReceiveMethod())) {
case ADD:
deviceAdd(req);
break;
case UPDATE:
deviceUpdate(req);
break;
case DEL:
deviceDel(req);
break;
case ACTIVE:
deviceActive(req);
break;
case ENABLED:
deviceEnabled(req);
break;
case STOP:
deviceStop(req);
break;
case ONLINE:
deviceOnline(req);
break;
case OFFLINE:
deviceOffline(req);
break;
}
} catch (Exception e) {
log.error("接收数据失败", e);
Rest.fail(e.getMessage());
}
return Rest.ok();
}
private void deviceAdd(DeviceReq req) throws AppException {
log.info("【设备新增】【请求体】--> " + JSONObject.toJSONString(req));
//根据设备编码查询设备
DeviceEntity deviceEntity = deviceService.selectOne(new DeviceQuery().deviceCode(req.getDeviceCode()));
if (!ObjectUtils.isEmpty(deviceEntity)) {
throw new AppException(DEVICE_CODE_IS_EXIST, DEVICE_CODE_IS_EXIST_CONTENT);
}
deviceEntity = new DeviceEntity();
deviceEntity.initAttrValue();
deviceEntity.setDeviceName(req.getDeviceName());
deviceEntity.setDeviceCode(req.getDeviceCode());
deviceEntity.setDeviceMac(req.getDeviceCode());
deviceEntity.setSiteId(req.getSiteId());
deviceEntity.setSiteCode(req.getSiteCode());
deviceEntity.setSiteName(req.getSiteName());
deviceEntity.setProductCode(req.getProductCode());
deviceEntity.setIp(req.getIp());
deviceEntity.setPort(req.getPort());
deviceEntity.setLeadingOfficial(req.getLeadingOfficial());
deviceEntity.setLeadingOfficialTelephone(req.getLeadingOfficialTelephone());
deviceEntity.setDeviceRemark(req.getDeviceRemark());
deviceEntity.setCreateUserId(1L);
deviceEntity.setCreateTime(new Date());
deviceService.save(deviceEntity);
}
private void deviceUpdate(DeviceReq req) throws AppException {
log.info("【设备更新或新增】【请求体】--> " + JSONObject.toJSONString(req));
//根据设备编码查询设备
DeviceEntity deviceEntity = deviceService.selectOne(new DeviceQuery().deviceCode(req.getDeviceCode()));
if (ObjectUtils.isEmpty(deviceEntity)) {
//不存在设备 则新增
this.deviceAdd(req);
} else {
log.info("设备更新~");
deviceEntity.setDeviceName(req.getDeviceName());
deviceEntity.setDeviceCode(req.getDeviceCode());
deviceEntity.setDeviceMac(req.getDeviceCode());
deviceEntity.setSiteId(req.getSiteId());
deviceEntity.setSiteCode(req.getSiteCode());
deviceEntity.setSiteName(req.getSiteName());
deviceEntity.setProductCode(req.getProductCode());
deviceEntity.setIp(req.getIp());
deviceEntity.setPort(req.getPort());
deviceEntity.setDeviceRemark(req.getDeviceRemark());
deviceEntity.setUpdateUserId(1L);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setLeadingOfficial(req.getLeadingOfficial());
deviceService.update(deviceEntity);
}
}
private void deviceDel(DeviceReq req) throws AppException {
log.info("【设备删除】【请求体】--> " + JSONObject.toJSONString(req));
//根据设备编码查询设备
DeviceEntity deviceEntity = checkDeviceExist(req);
deviceService.remove(new Long[]{deviceEntity.getId()}, null);
}
private void deviceActive(DeviceReq req) throws AppException {
log.info("【设备激活】【请求体】--> " + JSONObject.toJSONString(req));
//根据设备编码查询设备
DeviceEntity deviceEntity = checkDeviceExist(req);
if (deviceEntity.getDeviceStatus() > DeviceStatusEnum.未激活.getValue()) {
throw new AppException("当前设备已激活!");
}
deviceEntity.setDeviceStatus(DeviceStatusEnum.离线.getValue());
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceService.update(deviceEntity);
}
private void deviceEnabled(DeviceReq req) throws AppException {
log.info("【设备启用】【请求体】--> " + JSONObject.toJSONString(req));
//根据设备编码查询设备
DeviceEntity deviceEntity = checkDeviceExist(req);
deviceEntity.setEnabled(YesNoEnum.YES.getValue());
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceService.update(deviceEntity);
}
private void deviceStop(DeviceReq req) throws AppException {
log.info("【设备停用】【请求体】--> " + JSONObject.toJSONString(req));
DeviceEntity deviceEntity = checkDeviceExist(req);
deviceEntity.setEnabled(YesNoEnum.NO.getValue());
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceService.update(deviceEntity);
}
private void deviceOnline(DeviceReq req) throws AppException {
log.info("【设备上线】【请求体】--> " + JSONObject.toJSONString(req));
DeviceEntity deviceEntity = checkDeviceExist(req);
deviceEntity.setDeviceStatus(DeviceStatusEnum.在线.getValue());
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceService.update(deviceEntity);
}
private void deviceOffline(DeviceReq req) throws AppException {
log.info("【设备离线】【请求体】--> " + JSONObject.toJSONString(req));
DeviceEntity deviceEntity = checkDeviceExist(req);
deviceEntity.setDeviceStatus(DeviceStatusEnum.离线.getValue());
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(1L);
deviceService.update(deviceEntity);
}
private DeviceEntity checkDeviceExist(DeviceReq req) {
if (ObjectUtils.isEmpty(req.getDeviceCode())) {
throw new AppException(DEVICE_CODE_IS_EMPTY, DEVICE_CODE_IS_EMPTY_CONTENT);
}
DeviceEntity deviceEntity = deviceService.selectOne(new DeviceQuery().deviceCode(req.getDeviceCode()));
if (ObjectUtils.isEmpty(deviceEntity)) {
throw new AppException(DEVICE_NOT_EXIST, DEVICE_NOT_EXIST_CONTENT);
}
return deviceEntity;
}
}
package com.mortals.xhx.common.code;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 设备请求方式,1:新增,2:修改,3:删除,4:激活,5:启用,6:停用
*
* @author zxfei
*/
public enum DeviceMethodEnum {
ADD(1, "新增"),
UPDATE(2, "修改"),
DEL(3, "删除"),
ACTIVE(4, "激活"),
ENABLED(5, "启用"),
STOP(6, "停用"),
ONLINE(7, "上线"),
OFFLINE(8, "下线")
;
private Integer value;
private String desc;
DeviceMethodEnum(Integer value, String desc) {
this.value = value;
this.desc = desc;
}
public Integer getValue() {
return this.value;
}
public String getDesc() {
return this.desc;
}
public static DeviceMethodEnum getByValue(Integer value) {
for (DeviceMethodEnum deviceTypeEnum : DeviceMethodEnum.values()) {
if (deviceTypeEnum.getValue() == value) {
return deviceTypeEnum;
}
}
return null;
}
/**
* 获取Map集合
*
* @param eItem 不包含项
* @return
*/
public static Map<String, String> getEnumMap(Integer... eItem) {
Map<String, String> resultMap = new LinkedHashMap<>();
for (DeviceMethodEnum item : DeviceMethodEnum.values()) {
try {
boolean hasE = false;
for (Integer e : eItem) {
if (item.getValue() == e) {
hasE = true;
break;
}
}
if (!hasE) {
resultMap.put(item.getValue() + "", item.getDesc());
}
} catch (Exception ex) {
}
}
return resultMap;
}
}
\ No newline at end of file
package com.mortals.xhx.common.code;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 启用状态 (0.停止,1.启用)枚举类
*
* @author zxfei
*/
public enum EnabledEnum {
停止(0, "停止"),
启用(1, "启用");
private Integer value;
private String desc;
EnabledEnum(Integer value, String desc) {
this.value = value;
this.desc = desc;
}
public Integer getValue() {
return this.value;
}
public String getDesc() {
return this.desc;
}
public static EnabledEnum getByValue(Integer value) {
for (EnabledEnum enabledEnum : EnabledEnum.values()) {
if (enabledEnum.getValue() == value) {
return enabledEnum;
}
}
return null;
}
/**
* 获取Map集合
*
* @param eItem 不包含项
* @return
*/
public static Map<String, String> getEnumMap(Integer... eItem) {
Map<String, String> resultMap = new LinkedHashMap<>();
for (EnabledEnum item : EnabledEnum.values()) {
try {
boolean hasE = false;
for (Integer e : eItem) {
if (item.getValue() == e) {
hasE = true;
break;
}
}
if (!hasE) {
resultMap.put(item.getValue() + "", item.getDesc());
}
} catch (Exception ex) {
}
}
return resultMap;
}
}
\ No newline at end of file
...@@ -3,20 +3,19 @@ package com.mortals.xhx.common.code; ...@@ -3,20 +3,19 @@ package com.mortals.xhx.common.code;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* Created by chendilin on 2018/3/7.
*/
public enum OperTypeEnum { public enum OperTypeEnum {
SAVE(0,"添加"), SAVE(0, "添加"),
UPDATE(1,"更新"), UPDATE(1, "更新"),
DELETE(2,"删除"), DELETE(2, "删除"),
OTHER(-1,"其它"); SEARCH(3, "查询"),
OTHER(-1, "其它");
private int value; private int value;
private String msg; private String msg;
private OperTypeEnum(int value,String msg) { private OperTypeEnum(int value, String msg) {
this.value = value; this.value = value;
this.msg = msg; this.msg = msg;
} }
...@@ -25,11 +24,11 @@ public enum OperTypeEnum { ...@@ -25,11 +24,11 @@ public enum OperTypeEnum {
return this.value; return this.value;
} }
public static Map<String,String> getEnumMap(){ public static Map<String, String> getEnumMap() {
Map<String,String> resultMap = new HashMap<>(); Map<String, String> resultMap = new HashMap<>();
OperTypeEnum[] operTypeEnum = OperTypeEnum.values(); OperTypeEnum[] operTypeEnum = OperTypeEnum.values();
for (OperTypeEnum typeEnum : operTypeEnum) { for (OperTypeEnum typeEnum : operTypeEnum) {
resultMap.put(String.valueOf(typeEnum.value),typeEnum.msg); resultMap.put(String.valueOf(typeEnum.value), typeEnum.msg);
} }
return resultMap; return resultMap;
} }
......
package com.mortals.xhx.common.key;
/**
* 错误码
*
* @author: zxfei
* @date: 2022/5/12 14:56
*/
public interface ErrorCode {
public static final int STATUS_MS_EXCEPTION = 500;
public static final int STATUS_VALIDATE_EXCEPTION = 420;
public static final int STATUS_UNCHECKED_EXCEPTION = 605;
public static final int STATUS_TOKEN_NULL_EXCEPTION = 604;
public static final int STATUS_CODE_SUCCESS = 0;
public static final int STATUS_CODE_WARN = 1;
public static final int STATUS_CODE_ERROR = 2;
public static final int STATUS_CODE_INFO = 3;
public static final int STATUS_CODE_TOKEN_EXPIRED = 4;
public static final int STATUS_CODE_FATAL = 5;
public static final int STATUS_CODE_TRADE_PWD_NOT_SET = 6;
public static final int STATUS_ACCOUNT_LOCKED = 7;
public static final int STATUS_TRADE_PWD_OVER_THREE_TIME = 8;
public static final int STATUS_TRADE_PWD_ERROR = 9;
public static final int STATUS_EMPTY_PWD_ERROR = 10;
public static final int STATUS_TEL_NOT_RGI_ERROR = 11;
public static final int STATUS_TEL_ALREADY_REGI = 12;
public static final int STATUS_SAFETY_RISK = 13;
public static final int STATUS_LOGIN_CODE = 15;
public static final int BOOK_FAKUAN_CODE = 16;
public static final String ERROR_TRADE_PWD_OVER_THREE_TIME = "支付密码错误,请15分钟后再试";
public static final String ERROR_TRADE_PWD_ERROR = "支付密码错误,请重试";
public static final String ERROR_EMPTY_PWD_ERROR = "请设置登录密码";
public static final String ERROR_TEL_NOT_RGI = "该号码未注册";
public static final String ERROR_USERNAME_OR_PASSWORD = "用户名或者密码错误";
public static final String ERROR_TRADE_PWD = "交易密码错误";
public static final String ERROR_FORBIDDEN_OPER = "非法操作";
public static final String ERROR_TRADE_PWD_NOT_SET = "非法操作";
public static final String ERROR_NOT_REAL_NAME_AUTH = "您未实名认证,禁止该操作";
public static final String ERROR_INTERNAL_SERVER_ERROR = "服务器内部错误";
public static final String ERROR_UNAUTHORIZED = "token不正确或已过期";
public static final String ERROR_TOKEN_IS_NULL = "token不能为空";
public static final String ERROR_MISS_SERVLET = "服务不存在";
public static final String ERROR_CAPTCHA_OFTEN = "验证码已发送";
public static final String ERROR_CAPTCHA_WRONG = "验证码错误";
public static final String ERROR_TEL_ALREADY_REGI = "该手机号已被注册";
public static final String ERROR_CODE_DUPLICATE_KEY = "重复添加信息(含部分)";
public static final String ERROR_NOT_EXITS = "对应记录不存在";
public static final String ERROR_STATUS_CATEGORY = "状态错误";
public static final String ERROR_FRIEND_SHIP_ALREADY = "已经是你好友";
public static final String ERROR_FRIEND_SHIP_WAIT = "已向改好友发出邀请,等待接受";
public static final String ERROR_CODE_ACCOUNT_LOCKED = "账号被锁定,请联系客服";
public static final String WARN_ARGUMENT = "参数错误";
public static final String ERROR_USERNAME_EXIST = "该号码已被注册";
public static final String ERROR_SAFETY_RISK = "不在常用设备上登录";
public static final String INFO_TEL_BIND = "手机号码已经被绑定";
public static final String INFO_TEL_FORMAT_WRONG = "手机号码格式不正确";
public static final String ERROR_NOT_FOUND = "404 not found";
public static final String DISABLED="该账号已被封禁,如有疑问请联系平台";
public static final String DATENULL="缺少参数";
public static final String ERRDATE="无效参数";
public static final String ERRSTAE="状态异常";
public static final String EXTDATE="参数异常";
public static final String NUMEXE="账号异常";
public static final String CAPDON="资产已被冻结,如有疑问请联系平台";
public static final String CONOTS="操作失败";
public static final String OK="成功!";
public static final String TOKENX="身份验证失败,请重新登录";
public static final String CAPNOT="充值余额不足请充值";
public static final String SYSNOT="系统繁忙,请稍后再试...";
public static final String NOWER="没有权限";
public static final String PAGEDATA="分页参数不能为空";
public static final String CARADD_MEMBERS="该司机已有绑定车辆,不能绑定多个";
public static final int DEVICE_CODE_IS_EMPTY = 1001;
public static final String DEVICE_CODE_IS_EMPTY_CONTENT = "当前设备编码为空!";
public static final int DEVICE_CODE_IS_EXIST = 1002;
public static final String DEVICE_CODE_IS_EXIST_CONTENT = "当前设备编码已存在!";
public static final int PRODUCT_IS_EMPTY = 1003;
public static final String PRODUCT_IS_EMPTY_CONTENT = "所属产品编码不存在!";
public static final int DEVICE_UNACTIVE = 1004;
public static final String DEVICE_UNACTIVE_CONTENT = "当前设备未激活,请在后台配置后再激活!";
public static final int PLATFORM_IS_EMPTY = 1005;
public static final String PLATFORM_IS_EMPTY_CONTENT = "当前设备编码不存在!";
public static final int PLATFORM_UNEXIST = 1006;
public static final String PLATFORM_UNEXIST_CONTENT = "当前设备所属产品平台未配置,请在后台配置后再激活!";
public static final int SITEID_IS_EMPTY = 1007;
public static final String SITEID_IS_EMPTY_CONTENT = "站点ID为空!";
public static final int DEVICE_CONFIG_IS_EMPTY = 1008;
public static final String DEVICE_CONFIG_IS_EMPTY_CONTENT = "设备创建,请完善设备配置信息!";
public static final int DEVICE_NOT_EXIST = 1009;
public static final String DEVICE_NOT_EXIST_CONTENT = "当前设备不存在!";
public static final int TOKEN_AUTH_FAIL = 1010;
public static final String TOKEN_AUTH_FAIL_CONTENT = "token认证失败!";
public static final int ERROR_TOKEN_EXPIRED = 9001;
public static final String ERROR_TOKEN_EXPIRED_CONTENT = "用户登录过期,请重新登录!";
public static final int ERROR_TOKEN_UNAUTHORIZED = 9002;
public static final String ERROR_TOKEN_UNAUTHORIZED_CONTENT = "token不正确或已过期";
public static final int ERROR_USER_OPERATION = 9009;
public static final String ERROR_USER_OPERATION_CONTENT = "用户无该操作权限!";
}
package com.mortals.xhx.module.device.service; package com.mortals.xhx.module.device.service;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICRUDService; import com.mortals.framework.service.ICRUDService;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity; import com.mortals.xhx.module.device.model.DeviceEntity;
/** /**
* DeviceService * DeviceService
...@@ -11,4 +13,20 @@ import com.mortals.xhx.module.device.model.DeviceEntity; ...@@ -11,4 +13,20 @@ import com.mortals.xhx.module.device.model.DeviceEntity;
*/ */
public interface DeviceService extends ICRUDService<DeviceEntity,Long>{ public interface DeviceService extends ICRUDService<DeviceEntity,Long>{
DeviceDao getDao();
/**
* 设备激活
* @param deviceCode
* @param context
*/
void active(String deviceCode, Context context);
/**
* 设备启用停用
* @param id
* @param context
*/
void deviceEnabled(Long id, Integer status, Context context);
} }
\ No newline at end of file
package com.mortals.xhx.module.device.service.impl; package com.mortals.xhx.module.device.service.impl;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl; import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.DeviceMethodEnum;
import com.mortals.xhx.common.code.DeviceStatusEnum;
import com.mortals.xhx.common.code.EnabledEnum;
import com.mortals.xhx.common.pdu.LoginForm;
import com.mortals.xhx.common.pdu.device.DeviceReq;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.module.device.dao.DeviceDao; import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity; import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService; import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
/** /**
* DeviceService * DeviceService
* 设备 service实现 * 设备 service实现
...@@ -13,6 +31,58 @@ import org.springframework.stereotype.Service; ...@@ -13,6 +31,58 @@ import org.springframework.stereotype.Service;
* @date 2023-02-25 * @date 2023-02-25
*/ */
@Service("deviceService") @Service("deviceService")
@Slf4j
public class DeviceServiceImpl extends AbstractCRUDServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService { public class DeviceServiceImpl extends AbstractCRUDServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {
@Autowired
private IDeviceFeign deviceFeign;
@Value("${token.loginName:'admin'}")
private String loginName;
@Value("${token.password:'admin'}")
private String password;
@Override
public void active(String deviceCode, Context context) {
DeviceEntity deviceEntity = this.selectOne(new DeviceQuery().deviceCode(deviceCode));
if (ObjectUtils.isEmpty(deviceEntity)) throw new AppException("当前设备不存在!");
if (deviceEntity.getDeviceStatus() > DeviceStatusEnum.未激活.getValue())
throw new AppException("当前设备已激活!");
deviceEntity.setDeviceStatus(DeviceStatusEnum.离线.getValue());
deviceEntity.setEnabled(EnabledEnum.启用.getValue());
this.getDao().update(deviceEntity);
String token = getToken();
DeviceReq deviceReq = new DeviceReq();
deviceReq.setReceiveMethod(DeviceMethodEnum.ACTIVE.getValue());
deviceReq.setDeviceCode(deviceCode);
Rest<String> rest = deviceFeign.deviceCall(deviceReq, token);
log.info("激活结果:{}", JSON.toJSONString(rest));
}
@Override
public void deviceEnabled(Long id, Integer enabled, Context context) {
DeviceEntity deviceEntity = this.get(id, context);
if (ObjectUtils.isEmpty(deviceEntity)) throw new AppException("当前设备不存在!");
deviceEntity.setEnabled(enabled);
deviceEntity.setUpdateTime(new Date());
deviceEntity.setUpdateUserId(getContextUserId(context));
this.getDao().update(deviceEntity);
String token = getToken();
DeviceReq deviceReq = new DeviceReq();
deviceReq.setReceiveMethod(DeviceMethodEnum.ENABLED.getValue());
deviceReq.setDeviceCode(deviceEntity.getDeviceCode());
Rest<String> rest = deviceFeign.deviceCall(deviceReq, token);
log.info("启用结果:{}", JSON.toJSONString(rest));
}
private String getToken() {
LoginForm loginForm = new LoginForm();
loginForm.setLoginName(loginName);
loginForm.setPassword(password);
Rest<String> rest = deviceFeign.getToken(loginForm);
String token = rest.getData();
return token;
}
} }
\ No newline at end of file
package com.mortals.xhx.module.device.web; package com.mortals.xhx.module.device.web;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController; import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService; import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DeviceMethodEnum;
import com.mortals.xhx.common.code.EnabledEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.LoginForm;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DeviceReq;
import com.mortals.xhx.common.pdu.firm.FirmPdu;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.feign.firm.IFirmFeign;
import com.mortals.xhx.module.device.model.DeviceQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController; import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.device.model.DeviceEntity; import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService; import com.mortals.xhx.module.device.service.DeviceService;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils; import com.mortals.framework.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import java.util.Arrays; import java.util.Arrays;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import static com.mortals.framework.ap.SysConstains.*; import static com.mortals.framework.ap.SysConstains.*;
/** /**
* * 设备
* 设备 *
* * @author zxfei
* @author zxfei * @date 2023-02-25
* @date 2023-02-25 */
*/
@RestController @RestController
@RequestMapping("device") @RequestMapping("device")
public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceService,DeviceEntity,Long> { @Slf4j
public class DeviceController extends BaseCRUDJsonBodyMappingController<DeviceService, DeviceEntity, Long> {
@Autowired @Autowired
private ParamService paramService; private ParamService paramService;
public DeviceController(){ @Autowired
super.setModuleDesc( "设备"); private IDeviceFeign deviceFeign;
@Autowired
private IFirmFeign firmFeign;
@Value("${token.loginName:'admin'}")
private String loginName;
@Value("${token.password:'admin'}")
private String password;
public DeviceController() {
super.setModuleDesc("设备");
} }
@Override @Override
protected void init(Map<String, Object> model, Context context) { protected void init(Map<String, Object> model, Context context) {
this.addDict(model, "deviceSrc", paramService.getParamBySecondOrganize("Device","deviceSrc")); this.addDict(model, "deviceSrc", paramService.getParamBySecondOrganize("Device", "deviceSrc"));
this.addDict(model, "deviceStatus", paramService.getParamBySecondOrganize("Device","deviceStatus")); this.addDict(model, "deviceStatus", paramService.getParamBySecondOrganize("Device", "deviceStatus"));
this.addDict(model, "source", paramService.getParamBySecondOrganize("Device","source")); this.addDict(model, "source", paramService.getParamBySecondOrganize("Device", "source"));
this.addDict(model, "enabled", EnabledEnum.getEnumMap());
Rest<RespData<List<FirmPdu>>> restList = firmFeign.list(new FirmPdu());
if (restList.getCode() == YesNoEnum.YES.getValue()) {
Map<String, String> firmMap = restList.getData().getData().stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getFirmName(), (o, n) -> n));
this.addDict(model, "deviceFirmId", firmMap);
} else {
this.addDict(model, "deviceFirmId", new JSONObject());
}
super.init(model, context); super.init(model, context);
} }
/**
* 设备激活
*/
@PostMapping(value = "active")
public Rest<Void> deviceActive(@RequestBody DeviceEntity deviceEntity) {
log.info("设备激活:{}", deviceEntity.getDeviceCode());
String busiDesc = this.getModuleDesc() + "设备激活";
Rest<Void> rest = Rest.ok(busiDesc + " 【成功】");
try {
this.service.active(deviceEntity.getDeviceCode(), getContext());
recordSysLog(request, busiDesc + " 【成功】");
} catch (Exception e) {
log.error("设备激活消息", e);
rest = Rest.fail(super.convertException(e));
}
return rest;
}
/**
* 设备启用停用
*/
@PostMapping(value = "enable")
public String deviceEnable(@RequestBody DeviceEntity deviceEntity) {
JSONObject jsonObject = new JSONObject();
Map<String, Object> model = new HashMap<>();
String busiDesc = this.getModuleDesc() + "设备";
try {
this.service.deviceEnabled(deviceEntity.getId(), deviceEntity.getEnabled(), getContext());
recordSysLog(request, busiDesc + EnabledEnum.getByValue(deviceEntity.getEnabled()).getDesc() + " 【成功】");
jsonObject.put(KEY_RESULT_DATA, model);
jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
} catch (Exception e) {
log.error("设备启用停用消息", e);
jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
}
return jsonObject.toJSONString();
}
@Override
protected int saveAfter(DeviceEntity entity, Map<String, Object> model, Context context) throws AppException {
if (ObjectUtils.isEmpty(entity.getUpdateTime())) {
String token = getToken();
DeviceReq deviceReq = new DeviceReq();
deviceReq.setReceiveMethod(DeviceMethodEnum.ADD.getValue());
deviceReq.setDeviceName(entity.getDeviceName());
deviceReq.setDeviceCode(entity.getDeviceCode());
deviceReq.setProductCode(entity.getProductCode());
deviceReq.setIp(entity.getIp());
deviceReq.setPort(entity.getPort());
deviceReq.setSiteId(1L);
deviceReq.setSiteCode("511500000000-0001");
deviceReq.setSiteName("宜宾市民中心");
deviceReq.setLeadingOfficial(entity.getLeadingOfficial());
deviceReq.setLeadingOfficialTelephone(entity.getLeadingOfficialTelephone());
deviceReq.setSource(1);
deviceReq.setDeviceStatus(entity.getDeviceStatus());
Rest<String> rest = deviceFeign.deviceCall(deviceReq, token);
log.info("添加结果:{}", JSON.toJSONString(rest));
} else {
String token = getToken();
DeviceReq deviceReq = new DeviceReq();
deviceReq.setReceiveMethod(DeviceMethodEnum.UPDATE.getValue());
deviceReq.setDeviceName(entity.getDeviceName());
deviceReq.setDeviceCode(entity.getDeviceCode());
deviceReq.setProductCode(entity.getProductCode());
deviceReq.setIp(entity.getIp());
deviceReq.setPort(entity.getPort());
deviceReq.setSiteId(1L);
deviceReq.setSiteCode("511500000000-0001");
deviceReq.setSiteName("宜宾市民中心");
deviceReq.setLeadingOfficial(entity.getLeadingOfficial());
deviceReq.setLeadingOfficialTelephone(entity.getLeadingOfficialTelephone());
deviceReq.setSource(1);
deviceReq.setDeviceStatus(entity.getDeviceStatus());
Rest<String> rest = deviceFeign.deviceCall(deviceReq, token);
log.info("更新结果:{}", JSON.toJSONString(rest));
}
return super.saveAfter(entity, model, context);
}
@Override
protected void deleteBefore(Long[] ids, Context context) throws AppException {
super.deleteBefore(ids, context);
String token = getToken();
DeviceQuery deviceQuery = new DeviceQuery();
deviceQuery.setIdList(Arrays.asList(ids));
List<DeviceEntity> deviceEntities = this.service.find(deviceQuery, context);
for (DeviceEntity entity : deviceEntities) {
DeviceReq deviceReq = new DeviceReq();
deviceReq.setReceiveMethod(DeviceMethodEnum.DEL.getValue());
deviceReq.setDeviceCode(entity.getDeviceCode());
Rest<String> rest = deviceFeign.deviceCall(deviceReq, token);
log.info("删除结果:{}", JSON.toJSONString(rest));
}
}
private String getToken() {
LoginForm loginForm = new LoginForm();
loginForm.setLoginName(loginName);
loginForm.setPassword(password);
Rest<String> rest = deviceFeign.getToken(loginForm);
String token = rest.getData();
return token;
}
} }
\ No newline at end of file
...@@ -16,6 +16,12 @@ spring: ...@@ -16,6 +16,12 @@ spring:
jackson: jackson:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8 time-zone: GMT+8
rabbitmq:
host: @profiles.rabbitmq.host@
port: @profiles.rabbitmq.port@
username: @profiles.rabbitmq.username@
password: @profiles.rabbitmq.password@
virtualHost: @profiles.rabbitmq.virtualhost@
cloud: cloud:
nacos: nacos:
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 # Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment