DeviceLogServiceImpl.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.warewms.hailiang.service.impl;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.JSONUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.warewms.common.core.domain.base.page.PageDomain;
  8. import com.warewms.common.core.domain.base.page.TableDataInfo;
  9. import com.warewms.common.utils.StringUtils;
  10. import com.warewms.hailiang.config.DeviceMessageSocket;
  11. import com.warewms.hailiang.domain.Device;
  12. import com.warewms.hailiang.domain.DeviceLog;
  13. import com.warewms.hailiang.mapper.DeviceLogMapper;
  14. import com.warewms.hailiang.service.DeviceLogService;
  15. import com.warewms.hailiang.service.DeviceService;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.context.event.EventListener;
  18. import org.springframework.scheduling.annotation.Async;
  19. import org.springframework.stereotype.Service;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.stream.Collectors;
  23. /**
  24. * @author AD
  25. * @description 针对表【device_log(设备日志表)】的数据库操作Service实现
  26. * @createDate 2023-08-21 13:24:57
  27. */
  28. @Service
  29. public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog>
  30. implements DeviceLogService {
  31. @Autowired
  32. DeviceLogMapper deviceLogMapper;
  33. @Autowired
  34. DeviceService deviceService;
  35. @Autowired
  36. DeviceMessageSocket deviceMessageSocket;
  37. @Override
  38. public TableDataInfo<DeviceLog> getList(DeviceLog deviceLog, PageDomain pageDomain) {
  39. Map<String, Object> params = deviceLog.getParams();
  40. IPage<DeviceLog> deviceLogIPage = deviceLogMapper.selectPage(pageDomain.build(), new LambdaQueryWrapper<DeviceLog>()
  41. .eq(StringUtils.isNotEmpty(deviceLog.getDeviceId()), DeviceLog::getDeviceId, deviceLog.getDeviceId())
  42. .eq(StringUtils.isNotEmpty(deviceLog.getDeviceName()), DeviceLog::getDeviceName, deviceLog.getDeviceName())
  43. .eq(StringUtils.isNotEmpty(deviceLog.getStatus()), DeviceLog::getStatus, deviceLog.getStatus())
  44. .between(params.get("beginTime") != null && params.get("endTime") != null,
  45. DeviceLog::getCreateTime, params.get("beginTime"), params.get("endTime"))
  46. .orderByDesc(DeviceLog::getCreateTime));
  47. return TableDataInfo.build(deviceLogIPage);
  48. }
  49. @Override
  50. @Async
  51. @EventListener
  52. public void createLog(DeviceLog deviceLog) {
  53. if (StringUtils.isEmpty(deviceLog.getCreateBy())){
  54. deviceLog.setCreateBy("system");
  55. deviceLog.setUpdateBy("system");
  56. }
  57. deviceLogMapper.insert(deviceLog);
  58. //推送消息
  59. for (Device device : deviceService.getList()) {
  60. if (device.getDeviceId().equals(deviceLog.getDeviceId())){
  61. deviceLog.setDeviceName(device.getAbbreviation());
  62. }
  63. }
  64. JSONObject jsonObject = new JSONObject();
  65. jsonObject.set("type","notify");
  66. jsonObject.set("content",JSONUtil.parse(deviceLog));
  67. deviceMessageSocket.sendAllMessage(jsonObject.toString());
  68. }
  69. @Override
  70. public List<DeviceLog> getList(DeviceLog deviceLog) {
  71. List<DeviceLog> deviceLogs = deviceLogMapper.selectList(new LambdaQueryWrapper<DeviceLog>()
  72. .eq(StringUtils.isNotEmpty(deviceLog.getDeviceId()), DeviceLog::getDeviceId, deviceLog.getDeviceId())
  73. .eq(StringUtils.isNotEmpty(deviceLog.getDeviceName()), DeviceLog::getDeviceName, deviceLog.getDeviceName())
  74. .eq(StringUtils.isNotEmpty(deviceLog.getStatus()), DeviceLog::getStatus, deviceLog.getStatus())
  75. .likeLeft(DeviceLog::getContent,deviceLog.getContent()));
  76. return deviceLogs.stream().map(item->{
  77. for (Device device : deviceService.getList()) {
  78. if (item.getDeviceId().equals(device.getDeviceId())){
  79. item.setDeviceName(device.getAbbreviation());
  80. }
  81. }
  82. return item;
  83. }).collect(Collectors.toList());
  84. }
  85. @Override
  86. public int cleanUpDataFromAWeekAgo() {
  87. return deviceLogMapper.cleanUpDataFromAWeekAgo();
  88. }
  89. }