package com.warewms.hailiang.service.impl; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.warewms.common.core.domain.base.page.PageDomain; import com.warewms.common.core.domain.base.page.TableDataInfo; import com.warewms.common.utils.StringUtils; import com.warewms.hailiang.config.DeviceMessageSocket; import com.warewms.hailiang.domain.Device; import com.warewms.hailiang.domain.DeviceLog; import com.warewms.hailiang.mapper.DeviceLogMapper; import com.warewms.hailiang.service.DeviceLogService; import com.warewms.hailiang.service.DeviceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author AD * @description 针对表【device_log(设备日志表)】的数据库操作Service实现 * @createDate 2023-08-21 13:24:57 */ @Service public class DeviceLogServiceImpl extends ServiceImpl implements DeviceLogService { @Autowired DeviceLogMapper deviceLogMapper; @Autowired DeviceService deviceService; @Autowired DeviceMessageSocket deviceMessageSocket; @Override public TableDataInfo getList(DeviceLog deviceLog, PageDomain pageDomain) { Map params = deviceLog.getParams(); IPage deviceLogIPage = deviceLogMapper.selectPage(pageDomain.build(), new LambdaQueryWrapper() .eq(StringUtils.isNotEmpty(deviceLog.getDeviceId()), DeviceLog::getDeviceId, deviceLog.getDeviceId()) .eq(StringUtils.isNotEmpty(deviceLog.getDeviceName()), DeviceLog::getDeviceName, deviceLog.getDeviceName()) .eq(StringUtils.isNotEmpty(deviceLog.getStatus()), DeviceLog::getStatus, deviceLog.getStatus()) .between(params.get("beginTime") != null && params.get("endTime") != null, DeviceLog::getCreateTime, params.get("beginTime"), params.get("endTime")) .orderByDesc(DeviceLog::getCreateTime)); return TableDataInfo.build(deviceLogIPage); } @Override @Async @EventListener public void createLog(DeviceLog deviceLog) { if (StringUtils.isEmpty(deviceLog.getCreateBy())){ deviceLog.setCreateBy("system"); deviceLog.setUpdateBy("system"); } deviceLogMapper.insert(deviceLog); //推送消息 for (Device device : deviceService.getList()) { if (device.getDeviceId().equals(deviceLog.getDeviceId())){ deviceLog.setDeviceName(device.getAbbreviation()); } } JSONObject jsonObject = new JSONObject(); jsonObject.set("type","notify"); jsonObject.set("content",JSONUtil.parse(deviceLog)); deviceMessageSocket.sendAllMessage(jsonObject.toString()); } @Override public List getList(DeviceLog deviceLog) { List deviceLogs = deviceLogMapper.selectList(new LambdaQueryWrapper() .eq(StringUtils.isNotEmpty(deviceLog.getDeviceId()), DeviceLog::getDeviceId, deviceLog.getDeviceId()) .eq(StringUtils.isNotEmpty(deviceLog.getDeviceName()), DeviceLog::getDeviceName, deviceLog.getDeviceName()) .eq(StringUtils.isNotEmpty(deviceLog.getStatus()), DeviceLog::getStatus, deviceLog.getStatus()) .likeLeft(DeviceLog::getContent,deviceLog.getContent())); return deviceLogs.stream().map(item->{ for (Device device : deviceService.getList()) { if (item.getDeviceId().equals(device.getDeviceId())){ item.setDeviceName(device.getAbbreviation()); } } return item; }).collect(Collectors.toList()); } @Override public int cleanUpDataFromAWeekAgo() { return deviceLogMapper.cleanUpDataFromAWeekAgo(); } }