Browse Source

添加设备日志模块

zhifei 1 year ago
parent
commit
c0e69e20a7

+ 77 - 1
warewms-common/src/main/java/com/warewms/common/core/domain/base/page/PageDomain.java

@@ -1,13 +1,23 @@
 package com.warewms.common.core.domain.base.page;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.text.CharSequenceUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.metadata.OrderItem;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.warewms.common.exception.ServiceException;
+import com.warewms.common.utils.StringUtils;
+import com.warewms.common.utils.sql.SqlUtil;
+
+import java.util.ArrayList;
+import java.util.List;
 
 import static com.warewms.common.core.domain.base.page.PageSort.ASC;
 import static com.warewms.common.core.domain.base.page.PageSort.DESC;
 
 /**
  * 分页数据
- * 
+ *
  * @author ruoyi
  */
 public class PageDomain
@@ -24,6 +34,19 @@ public class PageDomain
     /** 排序的方向desc或者asc */
     private String isAsc = "asc";
 
+    /**
+     * 当前记录起始索引 默认值
+     */
+    public static final int DEFAULT_PAGE_NUM = 1;
+
+    /**
+     * 每页显示记录数 默认值 默认查全部
+     */
+    public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
+
+    private static final String SEPARATOR = ",";
+
+
     public Integer getPageNum()
     {
         return pageNum;
@@ -63,5 +86,58 @@ public class PageDomain
         this.isAsc = CharSequenceUtil.equals(ASC.getDescription(), isAsc) ? ASC.getDescription() : DESC.getDescription(); ;
     }
 
+    public <T> Page<T> build() {
+        Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
+        Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
+        if (pageNum <= 0) {
+            pageNum = DEFAULT_PAGE_NUM;
+        }
+        Page<T> page = new Page<>(pageNum, pageSize);
+        List<OrderItem> orderItems = buildOrderItem();
+        if (CollUtil.isNotEmpty(orderItems)) {
+            page.addOrder(orderItems);
+        }
+        return page;
+    }
 
+    /**
+     * 构建排序
+     *
+     * 支持的用法如下:
+     * {isAsc:"asc",orderByColumn:"id"} order by id asc
+     * {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
+     * {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc
+     * {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc
+     */
+    private List<OrderItem> buildOrderItem() {
+        if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
+            return null;
+        }
+        String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
+        orderBy = StringUtils.toUnderScoreCase(orderBy);
+
+        // 兼容前端排序类型
+        isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
+
+        String[] orderByArr = orderBy.split(SEPARATOR);
+        String[] isAscArr = isAsc.split(SEPARATOR);
+        if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
+            throw new ServiceException("排序参数有误");
+        }
+
+        List<OrderItem> list = new ArrayList<>();
+        // 每个字段各自排序
+        for (int i = 0; i < orderByArr.length; i++) {
+            String orderByStr = orderByArr[i];
+            String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
+            if ("asc".equals(isAscStr)) {
+                list.add(OrderItem.asc(orderByStr));
+            } else if ("desc".equals(isAscStr)) {
+                list.add(OrderItem.desc(orderByStr));
+            } else {
+                throw new ServiceException("排序参数有误");
+            }
+        }
+        return list;
+    }
 }

+ 27 - 37
warewms-common/src/main/java/com/warewms/common/core/domain/base/page/TableDataInfo.java

@@ -1,16 +1,21 @@
 package com.warewms.common.core.domain.base.page;
 
+import cn.hutool.http.HttpStatus;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.warewms.common.core.domain.base.BaseEntity;
+import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
 import java.util.List;
 
 /**
  * 表格分页数据对象
- * 
+ *
  * @author ruoyi
  */
-public class TableDataInfo<T extends BaseEntity> implements Serializable
+@Data
+public class TableDataInfo<T> implements Serializable
 {
     private static final long serialVersionUID = 1L;
 
@@ -35,7 +40,7 @@ public class TableDataInfo<T extends BaseEntity> implements Serializable
 
     /**
      * 分页
-     * 
+     *
      * @param list 列表数据
      * @param total 总记录数
      */
@@ -45,43 +50,28 @@ public class TableDataInfo<T extends BaseEntity> implements Serializable
         this.total = total;
     }
 
-    public long getTotal()
-    {
-        return total;
-    }
-
-    public void setTotal(long total)
-    {
-        this.total = total;
+    public static <T> TableDataInfo<T> build(IPage<T> page) {
+        TableDataInfo<T> rspData = new TableDataInfo<>();
+        rspData.setCode(HttpStatus.HTTP_OK);
+        rspData.setMsg("查询成功");
+        rspData.setRows(page.getRecords());
+        rspData.setTotal(page.getTotal());
+        return rspData;
     }
 
-    public List<?> getRows()
-    {
-        return rows;
+    public static <T> TableDataInfo<T> build(List<T> list) {
+        TableDataInfo<T> rspData = new TableDataInfo<>();
+        rspData.setCode(HttpStatus.HTTP_OK);
+        rspData.setMsg("查询成功");
+        rspData.setRows(list);
+        rspData.setTotal(list.size());
+        return rspData;
     }
 
-    public void setRows(List<T> rows)
-    {
-        this.rows = rows;
-    }
-
-    public int getCode()
-    {
-        return code;
-    }
-
-    public void setCode(int code)
-    {
-        this.code = code;
-    }
-
-    public String getMsg()
-    {
-        return msg;
-    }
-
-    public void setMsg(String msg)
-    {
-        this.msg = msg;
+    public static <T> TableDataInfo<T> build() {
+        TableDataInfo<T> rspData = new TableDataInfo<>();
+        rspData.setCode(HttpStatus.HTTP_OK);
+        rspData.setMsg("查询成功");
+        return rspData;
     }
 }

+ 20 - 0
warewms-system/src/main/java/com/warewms/hailiang/service/DeviceLogService.java

@@ -0,0 +1,20 @@
+package com.warewms.hailiang.service;
+
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.warewms.common.core.domain.base.page.PageDomain;
+import com.warewms.common.core.domain.base.page.TableDataInfo;
+import com.warewms.hailiang.domian.DeviceLog;
+import org.springframework.stereotype.Service;
+
+/**
+* @author AD
+* @description 针对表【device_log(设备日志表)】的数据库操作Service
+* @createDate 2023-08-21 13:24:57
+*/
+public interface DeviceLogService extends IService<DeviceLog> {
+
+    TableDataInfo<DeviceLog> getList(DeviceLog deviceLog, PageDomain pageDomain);
+
+    void crateLog(DeviceLog deviceLog);
+}

+ 56 - 0
warewms-system/src/main/java/com/warewms/hailiang/service/impl/DeviceLogServiceImpl.java

@@ -0,0 +1,56 @@
+package com.warewms.hailiang.service.impl;
+
+
+import cn.hutool.core.util.ObjectUtil;
+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.domian.DeviceLog;
+import com.warewms.hailiang.mapper.DeviceLogMapper;
+import com.warewms.hailiang.service.DeviceLogService;
+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.Map;
+
+/**
+ * @author AD
+ * @description 针对表【device_log(设备日志表)】的数据库操作Service实现
+ * @createDate 2023-08-21 13:24:57
+ */
+@Service
+public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog>
+        implements DeviceLogService {
+
+    @Autowired
+    DeviceLogMapper deviceLogMapper;
+
+
+    @Override
+    public TableDataInfo<DeviceLog> getList(DeviceLog deviceLog, PageDomain pageDomain) {
+        Map<String, Object> params = deviceLog.getParams();
+        IPage<DeviceLog> deviceLogIPage = deviceLogMapper.selectPage(pageDomain.build(), new LambdaQueryWrapper<DeviceLog>()
+                .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")));
+        return TableDataInfo.build(deviceLogIPage);
+    }
+
+    @Override
+    @Async
+    @EventListener
+    public void crateLog(DeviceLog deviceLog) {
+        deviceLogMapper.insert(deviceLog);
+    }
+}
+
+
+
+

+ 47 - 0
warewms-system/src/main/java/com/warewms/hailiang/service/impl/DeviceServiceImpl.java

@@ -0,0 +1,47 @@
+package com.warewms.hailiang.service.impl;
+
+import com.warewms.common.core.domain.base.page.TableDataInfo;
+import com.warewms.hailiang.domian.Device;
+import com.warewms.hailiang.service.DeviceService;
+import org.apache.ibatis.annotations.Update;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class DeviceServiceImpl implements DeviceService {
+
+    private List<Device> devices = new ArrayList<>();
+
+    {
+        devices.add(new Device("Z1_Daojia_PMJG-1-27.2", "DaoJiaoJiPlc", "倒角机", "Z1", "172.20.27.2", 102, "S7", "2"));
+        devices.add(new Device("Z1_DaoJiao_PMQ-1-27.4", "PanMaQi", "喷码器", "Z1", "172.20.27.4", 102, "TCP", "2"));
+        devices.add(new Device("Z1_DaoJia_DMQ-1-27.3", "CodeReader3", "倒角读码器", "Z1", "172.20.27.3", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_XiMian_DMQ-1-27.5", "CodeReader5", "铣面读码器", "Z1", "172.20.27.5", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_ZhaZhi_DMQ-1-27.6", "CodeReader6", "轧制读码器", "Z1", "172.20.27.6", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_DaSanPan_DMQ-1-27.7", "CodeReader7", "大散盘读码器1", "Z1", "172.20.27.7", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_DaSanPan_DMQ-2-27.8", "CodeReader8", "大散盘读码器2", "Z1", "172.20.27.8", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_DaSanPan_DMQ-3-27.9", "CodeReader9", "大散盘读码器3", "Z1", "172.20.27.9", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_DaSanPan_DMQ-4-27.12", "CodeReader12", "大散盘读码器4", "Z1", "172.20.27.12", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_TuiHuoSShangLiao_DMQ-1-27.13", "CodeReader13", "退货上料读码器", "Z1", "172.20.27.13", 51236, "TCP", "2"));
+        devices.add(new Device("Z1_TuiHuoXiaLiao_DMQ-1-27.14", "CodeReader14", "退货下料读码器", "Z1", "172.20.27.14", 51236, "TCP", "2"));
+    }
+
+    @Override
+    public List<Device> getList() {
+        return devices;
+    }
+
+    @Async
+    @EventListener
+    void updateStatus(Device d){
+        for (Device device : devices) {
+            if(device.getDeviceName().equals(d.getDeviceName())){
+                device.setStatus(d.getStatus());
+            }
+        }
+    }
+}