From 2f432f52c1cfb1567dadcf6e040c5d38b0a26a79 Mon Sep 17 00:00:00 2001
From: czt <czt18638530771@163.com>
Date: 星期五, 28 十一月 2025 17:31:49 +0800
Subject: [PATCH] 数量检测配置页面

---
 fzzy-igdss-core/src/main/java/com/fzzy/igds/service/DeviceIotService.java |  194 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 194 insertions(+), 0 deletions(-)

diff --git a/fzzy-igdss-core/src/main/java/com/fzzy/igds/service/DeviceIotService.java b/fzzy-igdss-core/src/main/java/com/fzzy/igds/service/DeviceIotService.java
new file mode 100644
index 0000000..b147541
--- /dev/null
+++ b/fzzy-igdss-core/src/main/java/com/fzzy/igds/service/DeviceIotService.java
@@ -0,0 +1,194 @@
+package com.fzzy.igds.service;
+
+import com.fzzy.igds.constant.RedisConst;
+import com.fzzy.igds.domain.DeviceIot;
+import com.fzzy.igds.repository.DeviceIotRepository;
+import com.fzzy.igds.utils.ContextUtil;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.StringUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+import javax.annotation.Resource;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @Description
+ * @Author CZT
+ * @Date 2025/11/28 14:45
+ */
+@Slf4j
+@Service
+public class DeviceIotService {
+
+    @Resource
+    private RedisCache redisCache;
+    @Resource
+    private DeviceIotRepository deviceIotRepository;
+
+    /**
+     * JPA鍒嗛〉鏌ヨ鏁版嵁
+     *
+     * @param specification
+     * @param pageable
+     * @return
+     */
+    public Page<DeviceIot> findAll(Specification<DeviceIot> specification, Pageable pageable) {
+        return deviceIotRepository.findAll(specification, pageable);
+    }
+
+    /**
+     * JPA - 淇濆瓨鏁版嵁
+     * @param deviceIot
+     */
+    public void updateDeviceIot(DeviceIot deviceIot) {
+        if (StringUtils.isEmpty(deviceIot.getCompanyId())) {
+            deviceIot.setCompanyId(ContextUtil.getCompanyId());
+        }
+        if (StringUtils.isEmpty(deviceIot.getDeptId())) {
+            deviceIot.setDeptId(ContextUtil.subDeptId(null));
+        }
+        if (StringUtils.isEmpty(deviceIot.getId())) {
+            deviceIot.setId(ContextUtil.generateId());
+            deviceIot.setCreateBy(ContextUtil.getLoginUserName());
+            deviceIot.setCreateTime(new Date());
+        }
+        deviceIot.setUpdateBy(ContextUtil.getLoginUserName());
+        deviceIot.setUpdateTime(new Date());
+        deviceIotRepository.save(deviceIot);
+    }
+
+    /**
+     * JPA - 鍒犻櫎鏁版嵁
+     * @param deviceIot
+     */
+    public void delDepotDeviceIot(DeviceIot deviceIot) {
+        deviceIotRepository.delete(deviceIot);
+    }
+
+    /**
+     * jpa鏇存柊璁惧浣嶇疆
+     *
+     * @param deviceId
+     * @param posX
+     * @param posY
+     */
+    public void updatePos(String deviceId, Double posX, Double posY) {
+        deviceIotRepository.updatePos(deviceId, posX, posY);
+    }
+
+    /**
+     * 鍒锋柊缂撳瓨
+     * @param companyId
+     */
+    public void refreshCache(String companyId) {
+        if (StringUtils.isEmpty(companyId)) {
+            companyId = ContextUtil.getCompanyId();
+        }
+        // 鑾峰彇鎵�鏈夌殑璁惧淇℃伅
+        List<DeviceIot> listAll =deviceIotRepository.getDeviceIotByCompanyId(companyId);
+
+        if (null != listAll) {
+            this.setCacheAllDeviceIot(listAll, companyId);
+        }
+    }
+
+    /**
+     * 璁剧疆缂撳瓨
+     * @param listAll
+     * @param companyId
+     */
+    public void setCacheAllDeviceIot(List<DeviceIot> listAll, String companyId) {
+        // 棣栧厛鎸夌収鍒嗘満鍒嗙粍锛岀劧鍚庡瓨鏇存柊缂撳瓨銆�
+        Map<String, List<DeviceIot>> map = listAll.stream().collect(
+                Collectors.groupingBy(DeviceIot::getSerId));
+
+        if (null == map || map.isEmpty()) {
+            log.error("瀛楀吀淇℃伅锛氭墍鏈夎澶囨寜鐓у垎鏈哄垎缁勪繚瀛樼紦瀛樺け璐ワ紝娌℃湁鍒嗙粍鎴愬姛=={}", companyId);
+            return;
+        }
+
+        for (String serId : map.keySet()) {
+            updateCacheDeviceIotBySerId(map.get(serId), companyId, serId);
+        }
+
+    }
+
+    /**
+     * 鏇存柊缂撳瓨
+     * @param listBySer
+     * @param companyId
+     * @param serId
+     */
+    public void updateCacheDeviceIotBySerId(List<DeviceIot> listBySer,
+                                            String companyId, String serId) {
+
+        String key = RedisConst.buildDeviceKey(companyId,
+                RedisConst.KEY_DEVICE_IOT_LIST, serId);
+
+        log.debug("鍒嗘満-璁惧-KEY={}", key);
+
+        redisCache.setCacheObject(key, listBySer);
+    }
+
+    /**
+     * 鑾峰彇缂撳瓨鏁版嵁
+     * @param companyId
+     * @param serId
+     * @return
+     */
+    public List<DeviceIot> getCacheDeviceIotBySerId(String companyId, String serId) {
+        String key = RedisConst.buildDeviceKey(companyId,
+                RedisConst.KEY_DEVICE_IOT_LIST, serId);
+        List<DeviceIot> list = (List<DeviceIot>) redisCache.getCacheObject(key);
+        if (null == list || list.isEmpty()) {
+            log.error("瀛楀吀淇℃伅锛氭病鏈夎幏鍙栧埌缂撳瓨淇℃伅锛孠EY={}", key);
+            return null;
+        }
+        return list;
+    }
+
+    /**
+     * 鑾峰彇缂撳瓨鏁版嵁
+     * @param companyId
+     * @param depotId
+     * @return
+     */
+    public List<DeviceIot> getCacheDeviceIotByDepotId(String companyId, String depotId) {
+
+        if(org.apache.commons.lang3.StringUtils.isEmpty(depotId)){
+            log.error("浠撳簱缂栫爜涓虹┖锛岃幏鍙朓ot璁惧澶辫触锛宒epotId={}", depotId);
+            return null;
+        }
+        if(StringUtils.isEmpty(companyId)){
+            companyId = ContextUtil.getCompanyId();
+        }
+
+        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_IOT_LIST) + "*";
+        Collection<String> keys = redisCache.keys(pattern);
+        if (null == keys) {
+            log.error("娌℃湁鑾峰彇鍒癐ot缂撳瓨key淇℃伅");
+            return null;
+        }
+
+        List<DeviceIot> list = new ArrayList<>();
+        List<DeviceIot> result;
+
+        for (String key : keys) {
+            result = (List<DeviceIot>) redisCache.getCacheObject(key);
+            if(null == result || result.isEmpty()){
+                continue;
+            }
+            for (DeviceIot iot : result) {
+                if(depotId.equals(iot.getDepotId())){
+                    list.add(iot);
+                }
+            }
+        }
+        return list;
+    }
+
+}

--
Gitblit v1.9.3