learshaw 4 сар өмнө
parent
commit
819309f9ac

+ 22 - 2
ems/ems-cloud/ems-dev-adapter/src/main/java/com/ruoyi/ems/TaskExecutor.java

@@ -12,6 +12,8 @@ package com.ruoyi.ems;
 
 import com.ruoyi.ems.core.ObjectCache;
 import com.ruoyi.ems.handle.MqttBaseHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -30,6 +32,11 @@ import javax.annotation.Resource;
  */
 @Service
 public class TaskExecutor {
+    /**
+     * 日志服务
+     */
+    protected static final Logger log = LoggerFactory.getLogger(TaskExecutor.class);
+
     @Autowired
     private ObjectCache objectCache;
 
@@ -42,11 +49,24 @@ public class TaskExecutor {
      */
     @Scheduled(cron = "0 0/5 * * * ?")
     public void cleanDevResCache() {
-        objectCache.cleanDevResCache();
+        int cnt = objectCache.cleanDevResCache();
+        log.info("清理过期设备上报响应: {} 条", cnt);
     }
 
+    /**
+     * 定时刷新设备在线状态
+     */
     @Scheduled(cron = "0 10 0/1 * * ?")
     public void refreshOnline() {
-        geekOpenCbHandler.refreshOnline();
+        geekOpenCbHandler.refreshOnline(2 * 60 * 60 * 1000);
+    }
+
+    /**
+     * 每小时产出设备计量数据
+     */
+    @Scheduled(cron = "0 0 0/1 * * ?")
+    public void meterHourProd() {
+        int cnt = geekOpenCbHandler.meterHourProd();
+        log.info("产出设备计量数据: {} 条", cnt);
     }
 }

+ 5 - 1
ems/ems-cloud/ems-dev-adapter/src/main/java/com/ruoyi/ems/core/ObjectCache.java

@@ -37,7 +37,8 @@ public class ObjectCache {
     /**
      * 清理过期的设备响应
      */
-    public void cleanDevResCache() {
+    public int cleanDevResCache() {
+        int cnt = 0;
         long currentTime = new Date().getTime();
         long threshold = 5 * 60 * 1000; // 分钟
 
@@ -48,8 +49,11 @@ public class ObjectCache {
             if ((currentTime - msg.getReceiveTime().getTime()) > threshold) {
                 // 删除缓存中的消息
                 iterator.remove();
+                cnt++;
             }
         }
+
+        return cnt;
     }
 
     public void setDevResCache(String messageId, MqttCacheMsg mqttMessage) {

+ 9 - 108
ems/ems-cloud/ems-dev-adapter/src/main/java/com/ruoyi/ems/handle/GeekOpenCbHandler.java

@@ -13,10 +13,8 @@ package com.ruoyi.ems.handle;
 import com.alibaba.fastjson2.JSONObject;
 import com.huashe.common.exception.Assert;
 import com.huashe.common.exception.BusinessException;
-import com.huashe.common.utils.DateUtils;
 import com.huashe.common.utils.ThreadUtils;
 import com.ruoyi.ems.core.ObjectCache;
-import com.ruoyi.ems.domain.ElecMeterH;
 import com.ruoyi.ems.domain.EmsDevice;
 import com.ruoyi.ems.domain.EmsObjAbilityCallLog;
 import com.ruoyi.ems.domain.MeterDevice;
@@ -24,11 +22,7 @@ import com.ruoyi.ems.enums.DevOnlineStatus;
 import com.ruoyi.ems.model.AbilityPayload;
 import com.ruoyi.ems.model.CallResponse;
 import com.ruoyi.ems.model.MqttCacheMsg;
-import com.ruoyi.ems.model.Price;
 import com.ruoyi.ems.model.QueryDevice;
-import com.ruoyi.ems.service.IElecMeterHService;
-import com.ruoyi.ems.service.IMeterDeviceService;
-import com.ruoyi.ems.service.IPriceService;
 import com.ruoyi.ems.util.IdUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -37,13 +31,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
-import java.math.BigDecimal;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import java.time.temporal.ChronoUnit;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
 
 /**
  * GeekOpen 断路器服务层
@@ -61,19 +51,8 @@ public class GeekOpenCbHandler extends MqttBaseHandler {
     @Autowired
     private ObjectCache messageCache;
 
-    @Autowired
-    private IMeterDeviceService meterDeviceService;
-
-    @Autowired
-    private IElecMeterHService elecMeterHService;
-
-    @Autowired
-    private IPriceService priceService;
-
     private static final String TOPIC_PREFIX = "/device/dlq/";
 
-    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
-
     /**
      * 能力执行
      *
@@ -183,88 +162,13 @@ public class GeekOpenCbHandler extends MqttBaseHandler {
         return deviceService.selectList(queryDevice);
     }
 
-    /**
-     * 据根据累计量更新电量计量数
-     *
-     * @param deviceCode  设备代码
-     * @param msgBody 消息体
-     */
-    private void syncElecMeterByCumulant(String deviceCode, JSONObject msgBody) {
-        MeterDevice meterDev = meterDeviceService.selectMeterDeviceByCode(deviceCode);
-
-        if (null == meterDev) {
-            log.warn("计量设备未注册, deviceCode:{}", deviceCode);
-            return;
-        }
-
-        Map<String, String> arrtMap = attrCache.get(deviceCode);
-
-        // 获取当前时间
-        String currentTime = DateUtils.dateToString(new Date(), "yyyy-MM-dd HH:00:00");
-        LocalDateTime currentLdt = LocalDateTime.parse(currentTime, TIME_FORMATTER);
-        // 获取缓存时间
-        String cacheTime = arrtMap.get("elecMeterHour");
-
-        if (StringUtils.isNotEmpty(cacheTime)) {
-            LocalDateTime cacheLdt = LocalDateTime.parse(cacheTime, TIME_FORMATTER);
-
-            String newEngValue = msgBody.getString("energy");
-
-            long hours = ChronoUnit.HOURS.between(cacheLdt, currentLdt);
-
-            if (hours == 1) {
-                String lastEngValue = arrtMap.get("elecMeterHourValue");
-
-                // 计算电量差值
-                BigDecimal lastEngValueBig = new BigDecimal(lastEngValue);
-                BigDecimal newEngValueBig = new BigDecimal(newEngValue);
-                BigDecimal diff = newEngValueBig.subtract(lastEngValueBig);
-
-                if (null != meterDev.getMagnification()) {
-                    diff = diff.multiply(new BigDecimal(String.valueOf(meterDev.getMagnification())));
-                }
-
-                Date date = DateUtils.stringToDate(cacheTime, "yyyy-MM-dd HH:mm:ss");
-
-                ElecMeterH elecMeterH = new ElecMeterH();
-                elecMeterH.setAreaCode(meterDev.getAreaCode());
-                elecMeterH.setDeviceCode(meterDev.getDeviceCode());
-                elecMeterH.setRecordTime(date);
-                elecMeterH.setDate(date);
-                elecMeterH.setTime(date);
-                elecMeterH.setTimeIndex(getHourIndex(date));
-                elecMeterH.setElecQuantity(diff.doubleValue());
-
-                Price price = priceService.getElecHourPrice(meterDev.getAreaCode(), date);
-
-                if (null != price) {
-                    BigDecimal cost = diff.multiply(new BigDecimal(String.valueOf(price.getPriceValue())));
-                    elecMeterH.setMeterType(price.getMeterType());
-                    elecMeterH.setMeterUnitPrice(price.getPriceValue());
-                    elecMeterH.setUseElecCost(cost.doubleValue());
-                }
-
-                elecMeterHService.insertElecMeterH(elecMeterH);
-                LocalDateTime nextTime = cacheLdt.plusHours(1);
-                String nextHour = nextTime.format(TIME_FORMATTER);
-                arrtMap.put("elecMeterHour", nextHour);
-                arrtMap.put("elecMeterHourValue", newEngValue);
-            }
-            else if (hours == 0) {
-                arrtMap.computeIfAbsent("elecMeterHourValue", key -> newEngValue);
-            }
-            else if (hours > 1) {
-                LocalDateTime nextTime = currentLdt.plusHours(1);
-                String nextHour = nextTime.format(TIME_FORMATTER);
-                arrtMap.put("elecMeterHour", nextHour);
-                arrtMap.remove("elecMeterHourValue");
-            }
-        }
-        else {
-            LocalDateTime nextTime = currentLdt.plusHours(1);
-            String nextHour = nextTime.format(TIME_FORMATTER);
-            arrtMap.put("elecMeterHour", nextHour);
-        }
+    @Override
+    public List<MeterDevice> getMeterDeviceList() {
+        QueryDevice queryDevice = new QueryDevice();
+        queryDevice.setDeviceModel("M_W2_QF_GEEKOPEN");
+        queryDevice.setMeterCls(45);
+        queryDevice.setColMode(0);
+        return meterDeviceService.selectByModel(queryDevice);
     }
 
     /**
@@ -305,10 +209,7 @@ public class GeekOpenCbHandler extends MqttBaseHandler {
         updateAttr(device, jsonBody, "voltage");
         updateAttr(device, jsonBody, "current");
         updateAttr(device, jsonBody, "power");
-
-        if (updateAttr(device, jsonBody, "energy")) {
-            syncElecMeterByCumulant(device.getDeviceCode(), jsonBody);
-        }
+        updateAttr(device, jsonBody, "energy");
     }
 
     private CallResponse<Void> checkResult(JSONObject sendObject, JSONObject receiveObject) {

+ 163 - 19
ems/ems-cloud/ems-dev-adapter/src/main/java/com/ruoyi/ems/handle/MqttBaseHandler.java

@@ -13,18 +13,24 @@ package com.ruoyi.ems.handle;
 import com.alibaba.fastjson2.JSONObject;
 import com.huashe.common.utils.DateUtils;
 import com.ruoyi.ems.core.MqttTemplate;
+import com.ruoyi.ems.domain.ElecMeterH;
 import com.ruoyi.ems.domain.EmsDevice;
 import com.ruoyi.ems.domain.EmsObjAbilityCallLog;
 import com.ruoyi.ems.domain.EmsObjAttrValue;
 import com.ruoyi.ems.domain.EmsObjReportLog;
+import com.ruoyi.ems.domain.MeterDevice;
 import com.ruoyi.ems.enums.DevOnlineStatus;
 import com.ruoyi.ems.model.AbilityPayload;
 import com.ruoyi.ems.model.CallResponse;
 import com.ruoyi.ems.model.MqttCacheMsg;
+import com.ruoyi.ems.model.Price;
+import com.ruoyi.ems.service.IElecMeterHService;
 import com.ruoyi.ems.service.IEmsDeviceService;
 import com.ruoyi.ems.service.IEmsObjAbilityCallLogService;
 import com.ruoyi.ems.service.IEmsObjAttrValueService;
 import com.ruoyi.ems.service.IEmsObjReportLogService;
+import com.ruoyi.ems.service.IMeterDeviceService;
+import com.ruoyi.ems.service.IPriceService;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -34,6 +40,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
@@ -61,11 +70,15 @@ public abstract class MqttBaseHandler {
      */
     protected static final Map<String, Map<String, String>> attrCache = new ConcurrentHashMap<>();
 
+    protected static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
     /**
      * 最后一次消息时间缓存key
      */
     protected static final String MQTT_LAST_TIME = "mqttLastMsgTime";
 
+    protected static final String LAST_HOUR_READING = "lastHourReading";
+
     @Resource
     @Qualifier("mqttTemplate")
     protected MqttTemplate mqttTemplate;
@@ -74,13 +87,22 @@ public abstract class MqttBaseHandler {
     protected IEmsDeviceService deviceService;
 
     @Autowired
+    protected IMeterDeviceService meterDeviceService;
+
+    @Autowired
     protected IEmsObjAttrValueService objAttrValueService;
 
     @Autowired
-    private IEmsObjAbilityCallLogService objAbilityCallLogService;
+    protected IElecMeterHService elecMeterHService;
+
+    @Autowired
+    protected IPriceService priceService;
+
+    @Autowired
+    protected IEmsObjAbilityCallLogService objAbilityCallLogService;
 
     @Autowired
-    private IEmsObjReportLogService objReportLogService;
+    protected IEmsObjReportLogService objReportLogService;
 
     /**
      * 抽象方法,用于能力调用
@@ -101,11 +123,19 @@ public abstract class MqttBaseHandler {
 
     /**
      * 获取设备列表
+     *
      * @return 设备列表
      */
     public abstract List<EmsDevice> getDeviceList();
 
     /**
+     * 获取计量设备列表
+     *
+     * @return 设备列表
+     */
+    public abstract List<MeterDevice> getMeterDeviceList();
+
+    /**
      * 添加消息ID到消息中
      *
      * @param msg 消息
@@ -119,9 +149,10 @@ public abstract class MqttBaseHandler {
 
     /**
      * 发送MQTT消息
-     * @param topic 主题
-     * @param payload 负载数据
-     * @param qos 质量服务等级
+     *
+     * @param topic    主题
+     * @param payload  负载数据
+     * @param qos      质量服务等级
      * @param retained 是否保留消息
      */
     public void sendMqttMsg(String topic, String payload, int qos, boolean retained) {
@@ -131,7 +162,8 @@ public abstract class MqttBaseHandler {
 
     /**
      * 更新最后消息时
-     * @param device 设备
+     *
+     * @param device    设备
      * @param timeValue 时间值
      */
     public void updateMsgTime(EmsDevice device, Date timeValue) {
@@ -141,7 +173,7 @@ public abstract class MqttBaseHandler {
             attrCache.computeIfAbsent(device.getDeviceCode(), k -> new ConcurrentHashMap<>());
 
             // 更新缓存
-            updateCacheAfterSuccess(device, MQTT_LAST_TIME, dateTime);
+            updateCacheAfterSuccess(device.getDeviceCode(), MQTT_LAST_TIME, dateTime);
         }
         catch (Exception e) {
             log.error("刷新缓存消息时间失败!", e);
@@ -149,12 +181,11 @@ public abstract class MqttBaseHandler {
     }
 
     /**
-     * 定时检测在线状态
+     * 定时刷新在线状态
      * <br/>每小时执行一次,扫描2个小时无消息的设备,标记为离线状态
      */
-    public void refreshOnline() {
+    public void refreshOnline(long threshold) {
         long currentTime = new Date().getTime();
-        long threshold = 2 * 60 * 60 * 1000; // 120分钟
 
         List<EmsDevice> deviceList = getDeviceList();
 
@@ -162,12 +193,14 @@ public abstract class MqttBaseHandler {
             for (EmsDevice device : deviceList) {
                 Map<String, String> attrMap = attrCache.get(device.getDeviceCode());
 
-                if (MapUtils.isNotEmpty(attrMap)) {
+                if (MapUtils.isNotEmpty(attrMap) && null != attrMap.get(MQTT_LAST_TIME)) {
                     String lastMsgTime = attrMap.get(MQTT_LAST_TIME);
 
-                    if (StringUtils.isNotEmpty(lastMsgTime)
-                        && (currentTime - DateUtils.stringToDate(lastMsgTime, DateUtils.YYYY_MM_DD_HH_MM_SS).getTime())
-                        > threshold) {
+                    // 计算最后一次消息至今的时间差
+                    long time =
+                        currentTime - DateUtils.stringToDate(lastMsgTime, DateUtils.YYYY_MM_DD_HH_MM_SS).getTime();
+
+                    if (time > threshold) {
                         refreshStatus(device, DevOnlineStatus.OFFLINE);
                     }
                 }
@@ -176,6 +209,116 @@ public abstract class MqttBaseHandler {
     }
 
     /**
+     * 产出小时电量计量
+     */
+    public int meterHourProd() {
+        int cnt = 0;
+
+        try {
+            List<MeterDevice> deviceList = getMeterDeviceList();
+            List<ElecMeterH> meterHList = new ArrayList<>();
+
+            for (MeterDevice device : deviceList) {
+                ElecMeterH elecMeterH = meterHourProdSub(device);
+
+                if (null != elecMeterH) {
+                    meterHList.add(elecMeterH);
+                }
+            }
+
+            if (CollectionUtils.isNotEmpty(meterHList)) {
+                cnt = elecMeterHService.insertBatch(meterHList);
+            }
+        }
+        catch (Exception e) {
+            log.error("定时产出小时电量计量失败!", e);
+        }
+
+        return cnt;
+    }
+
+    private ElecMeterH meterHourProdSub(MeterDevice device) {
+        ElecMeterH elecMeterH = null;
+
+        // 获取设备属性缓存
+        Map<String, String> attrMap = attrCache.computeIfAbsent(device.getDeviceCode(), k -> new ConcurrentHashMap<>());
+
+        String lastMeterReading = attrMap.get(LAST_HOUR_READING);
+        String newMeterReading = attrMap.get("energy");
+
+        if (StringUtils.isNotEmpty(lastMeterReading)) {
+            elecMeterH = execHourMeter(device, lastMeterReading, newMeterReading);
+        }
+        else {
+            ElecMeterH dbElecMeterH = elecMeterHService.selectLatelyItem(device.getDeviceCode());
+
+            if (null != dbElecMeterH && null != dbElecMeterH.getMeterReading()) {
+                elecMeterH = execHourMeter(device, String.valueOf(dbElecMeterH.getMeterReading()), newMeterReading);
+            }
+            else {
+                // 无缓存也无数据库记录,则认为是首次上报
+                if (StringUtils.isNotEmpty(newMeterReading)) {
+                    updateCacheAfterSuccess(device.getDeviceCode(), LAST_HOUR_READING, newMeterReading);
+                }
+            }
+        }
+
+        return elecMeterH;
+    }
+
+    private ElecMeterH execHourMeter(MeterDevice device, String lastMeterReading, String newMeterReading) {
+        ElecMeterH elecMeterH = null;
+        BigDecimal lastEngValue = new BigDecimal(lastMeterReading);
+
+        if (StringUtils.isNotEmpty(newMeterReading)) {
+            BigDecimal newEngValue = new BigDecimal(newMeterReading);
+            // 计算电量差值
+            BigDecimal useQuantity = newEngValue.subtract(lastEngValue);
+            // 倍率计算
+            useQuantity = magnification(useQuantity, device.getMagnification());
+            // 更新缓存
+            updateCacheAfterSuccess(device.getDeviceCode(), LAST_HOUR_READING, newMeterReading);
+            // 封装计量对象
+            elecMeterH = buildElecMeterH(device, newEngValue, useQuantity);
+        }
+        else {
+            BigDecimal useQuantity = BigDecimal.ZERO;
+            // 封装计量对象
+            elecMeterH = buildElecMeterH(device, lastEngValue, useQuantity);
+        }
+
+        return elecMeterH;
+    }
+
+    private ElecMeterH buildElecMeterH(MeterDevice device, BigDecimal newEngValue, BigDecimal useQuantity) {
+        Date date = new Date();
+        ElecMeterH elecMeterH = new ElecMeterH();
+        elecMeterH.setAreaCode(device.getAreaCode());
+        elecMeterH.setDeviceCode(device.getDeviceCode());
+        elecMeterH.setRecordTime(date);
+        elecMeterH.setDate(date);
+        elecMeterH.setTime(date);
+        elecMeterH.setTimeIndex(getHourIndex(date));
+        elecMeterH.setElecQuantity(useQuantity.doubleValue());
+        elecMeterH.setMeterReading(newEngValue.doubleValue());
+
+        Price price = priceService.getElecHourPrice(device.getAreaCode(), date);
+
+        if (null != price) {
+            BigDecimal cost = useQuantity.multiply(new BigDecimal(String.valueOf(price.getPriceValue())));
+            elecMeterH.setMeterType(price.getMeterType());
+            elecMeterH.setMeterUnitPrice(price.getPriceValue());
+            elecMeterH.setUseElecCost(cost.doubleValue());
+        }
+
+        return elecMeterH;
+    }
+
+    private BigDecimal magnification(BigDecimal diff, Integer magnification) {
+        return null != magnification ? diff.multiply(new BigDecimal(String.valueOf(magnification))) : diff;
+    }
+
+    /**
      * 校验是否需要更新属性值
      *
      * @param jsonBody jsonBody 消息体
@@ -218,7 +361,7 @@ public abstract class MqttBaseHandler {
                 // 更新数据库
                 objAttrValueService.mergeObjAttrValue(attrValue);
                 // 更新缓存
-                updateCacheAfterSuccess(device, attrKey, currentValue);
+                updateCacheAfterSuccess(device.getDeviceCode(), attrKey, currentValue);
 
                 flag = true;
             }
@@ -232,12 +375,13 @@ public abstract class MqttBaseHandler {
 
     /**
      * 更新缓存值
-     * @param device 设备信息
-     * @param attrKey 属性值
+     *
+     * @param deviceCode 设备信息
+     * @param attrKey    属性值
      * @param newValue
      */
-    public void updateCacheAfterSuccess(EmsDevice device, String attrKey, String newValue) {
-        attrCache.computeIfPresent(device.getDeviceCode(), (k, attrMap) -> {
+    public void updateCacheAfterSuccess(String deviceCode, String attrKey, String newValue) {
+        attrCache.computeIfPresent(deviceCode, (k, attrMap) -> {
             attrMap.put(attrKey, newValue);
             return attrMap;
         });

+ 13 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/domain/ElecMeterH.java

@@ -69,6 +69,10 @@ public class ElecMeterH extends BaseEntity
     @Excel(name = "小时电费")
     private Double useElecCost;
 
+    /** 抄表读数 */
+    @Excel(name = "抄表读数")
+    private Double meterReading;
+
     public Long getId() {
         return id;
     }
@@ -173,6 +177,14 @@ public class ElecMeterH extends BaseEntity
         this.useElecCost = useElecCost;
     }
 
+    public Double getMeterReading() {
+        return meterReading;
+    }
+
+    public void setMeterReading(Double meterReading) {
+        this.meterReading = meterReading;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
@@ -187,6 +199,7 @@ public class ElecMeterH extends BaseEntity
             .append("meterType", getMeterType())
             .append("meterUnitPrice", getMeterUnitPrice())
             .append("useElecCost", getUseElecCost())
+            .append("meterReading", getMeterReading())
             .append("createTime", getCreateTime())
             .toString();
     }

+ 15 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/mapper/ElecMeterHMapper.java

@@ -31,6 +31,13 @@ public interface ElecMeterHMapper {
     List<ElecMeterH> selectElecMeterByDevs(QueryMeter queryMeter);
 
     /**
+     * 查询最近一条计量
+     * @param deviceCode 设备编码
+     * @return 用电计量
+     */
+    ElecMeterH selectLatelyItem(@Param("deviceCode") String deviceCode);
+
+    /**
      * 查询用电计量-小时列表
      *
      * @param queryMeter 查询计量条件
@@ -47,6 +54,14 @@ public interface ElecMeterHMapper {
     int insertElecMeterH(ElecMeterH elecMeterH);
 
     /**
+     * 新增用电计量-小时
+     *
+     * @param list 用电计量-小时
+     * @return 结果
+     */
+    int insertBatch(List<ElecMeterH> list);
+
+    /**
      * 修改用电计量-小时
      *
      * @param elecMeterH 用电计量-小时

+ 8 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/mapper/MeterDeviceMapper.java

@@ -42,6 +42,14 @@ public interface MeterDeviceMapper {
     List<MeterDevice> selectMeterDeviceList(QueryDevice queryDevice);
 
     /**
+     * 查询计量设备列表
+     *
+     * @param queryDevice 计量设备
+     * @return 计量设备集合
+     */
+    List<MeterDevice> selectByModel(QueryDevice queryDevice);
+
+    /**
      * 新增计量设备
      *
      * @param meterDevice 计量设备

+ 15 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/service/IElecMeterHService.java

@@ -38,6 +38,13 @@ public interface IElecMeterHService {
     ElecMeter staByMeterDev(QueryMeter queryMeter);
 
     /**
+     * 查询最近的一条记录
+     * @param deviceCode 设备编码
+     * @return 用电计量-小时集合
+     */
+    ElecMeterH selectLatelyItem(String deviceCode);
+
+    /**
      * 新增用电计量-小时
      *
      * @param elecMeterH 用电计量-小时
@@ -46,6 +53,14 @@ public interface IElecMeterHService {
     int insertElecMeterH(ElecMeterH elecMeterH);
 
     /**
+     * 新增用电计量-小时
+     *
+     * @param list 用电计量-小时
+     * @return 结果
+     */
+    int insertBatch(List<ElecMeterH> list);
+
+    /**
      * 修改用电计量-小时
      *
      * @param elecMeterH 用电计量-小时

+ 12 - 3
ems/ems-core/src/main/java/com/ruoyi/ems/service/IMeterDeviceService.java

@@ -38,11 +38,20 @@ public interface IMeterDeviceService {
     List<MeterDevice> selectMeterDeviceList(QueryDevice queryDevice);
 
     /**
+     * 查询计量设备列表
+     *
+     * @param queryDevice 模型编码
+     * @return 计量设备集合
+     */
+    List<MeterDevice> selectByModel(QueryDevice queryDevice);
+
+    /**
      * 查询区域计量表计
-     * @param areaCode 区域代码
-     * @param objType 对象类型
+     *
+     * @param areaCode    区域代码
+     * @param objType     对象类型
      * @param boundaryObj 对象类型
-     * @param meterCls 计量对象分类
+     * @param meterCls    计量对象分类
      * @return 结果
      */
     List<MeterDevice> selectMeterDeviceByObj(String areaCode, MeterObjType objType, String boundaryObj,

+ 10 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/service/impl/ElecMeterHServiceImpl.java

@@ -54,6 +54,11 @@ public class ElecMeterHServiceImpl implements IElecMeterHService {
         return elecMeterHMapper.selectElecMeterSumByDev(queryMeter);
     }
 
+    @Override
+    public ElecMeterH selectLatelyItem(String deviceCode) {
+        return elecMeterHMapper.selectLatelyItem(deviceCode);
+    }
+
     /**
      * 新增用电计量-小时
      *
@@ -65,6 +70,11 @@ public class ElecMeterHServiceImpl implements IElecMeterHService {
         return elecMeterHMapper.insertElecMeterH(elecMeterH);
     }
 
+    @Override
+    public int insertBatch(List<ElecMeterH> list) {
+        return elecMeterHMapper.insertBatch(list);
+    }
+
     /**
      * 修改用电计量-小时
      *

+ 6 - 0
ems/ems-core/src/main/java/com/ruoyi/ems/service/impl/MeterDeviceServiceImpl.java

@@ -8,6 +8,7 @@ import com.ruoyi.ems.service.IMeterDeviceService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -49,6 +50,11 @@ public class MeterDeviceServiceImpl implements IMeterDeviceService {
     }
 
     @Override
+    public List<MeterDevice> selectByModel(QueryDevice queryDevice) {
+        return meterDeviceMapper.selectByModel(queryDevice);
+    }
+
+    @Override
     public List<MeterDevice> selectMeterDeviceByObj(String areaCode, MeterObjType objType, String boundaryObj,
         Integer meterCls) {
         return meterDeviceMapper.selectMeterDeviceByObj(areaCode, objType.getType(), boundaryObj, meterCls);

+ 27 - 2
ems/ems-core/src/main/resources/mapper/ems/ElecMeterHMapper.xml

@@ -17,6 +17,7 @@
         <result property="meterType" column="meter_type"/>
         <result property="meterUnitPrice" column="meter_unit_price"/>
         <result property="useElecCost" column="use_elec_cost"/>
+        <result property="meterReading" column="meter_reading"/>
         <result property="createTime" column="create_time"/>
     </resultMap>
 
@@ -41,6 +42,7 @@
                meter_type,
                meter_unit_price,
                use_elec_cost,
+               meter_reading,
                create_time
         from adm_elec_meter_h
     </sql>
@@ -50,7 +52,6 @@
         m.id, m.area_code, m.device_code, d.device_name, m.record_time, m.`date`, m.`time`, m.time_index,
         m.elec_quantity, m.meter_type, m.meter_unit_price, m.use_elec_cost, m.create_time
         from adm_elec_meter_h m
-        left join adm_meter_device d on m.device_code = d.device_code
         <where>
             <if test="areaCode != null  and areaCode != ''">and m.area_code = #{areaCode}</if>
             <if test="deviceCode != null  and deviceCode != ''">and m.device_code = #{deviceCode}</if>
@@ -85,6 +86,12 @@
         ORDER BY m.record_time ${orderFlag}
     </select>
 
+    <select id="selectLatelyItem" parameterType="java.lang.String" resultMap="ElecMeterHResult">
+        <include refid="selectElecMeterHVo"/>
+        where device_code = #{deviceCode}
+        ORDER BY record_time desc limit 1
+    </select>
+
     <select id="selectElecMeterSumByDev" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterResult">
         select
          area_code,
@@ -122,6 +129,7 @@
             <if test="meterType != null">meter_type,</if>
             <if test="meterUnitPrice != null">meter_unit_price,</if>
             <if test="useElecCost != null">use_elec_cost,</if>
+            <if test="meterReading != null">meter_reading,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
@@ -133,10 +141,26 @@
             <if test="elecQuantity != null">#{elecQuantity},</if>
             <if test="meterType != null">#{meterType},</if>
             <if test="meterUnitPrice != null">#{meterUnitPrice},</if>
-            <if test="useElecCost != null">#{useElecCost},</if>
+            <if test="meterReading != null">#{meterReading},</if>
         </trim>
     </insert>
 
+    <insert id="insertRelBatch" parameterType="java.util.List">
+        insert into adm_co_charging_config_rel (price_cfg_id, area_code)
+        values
+        <foreach collection="list" item="item" index="index" separator=",">
+            (#{item.priceCfgId}, #{item.areaCode})
+        </foreach>
+    </insert>
+
+    <insert id="insertBatch" parameterType="java.util.List">
+        insert into adm_elec_meter_h (area_code, device_code, record_time, `date`, `time`, time_index, elec_quantity, meter_type, meter_unit_price, use_elec_cost, meter_reading)
+        values
+        <foreach collection="list" item="item" index="index" separator=",">
+            (#{item.areaCode}, #{item.deviceCode}, #{item.recordTime}, #{item.date}, #{item.time}, #{item.timeIndex}, #{item.elecQuantity}, #{item.meterType}, #{item.meterUnitPrice}, #{item.useElecCost}, #{item.meterReading})
+        </foreach>
+    </insert>
+
     <update id="updateElecMeterH" parameterType="com.ruoyi.ems.domain.ElecMeterH">
         update adm_elec_meter_h
         <trim prefix="SET" suffixOverrides=",">
@@ -150,6 +174,7 @@
             <if test="meterType != null">meter_type = #{meterType},</if>
             <if test="meterUnitPrice != null">meter_unit_price = #{meterUnitPrice},</if>
             <if test="useElecCost != null">use_elec_cost = #{useElecCost},</if>
+            <if test="meterReading != null">meter_reading = #{meterReading},</if>
         </trim>
         where id = #{id}
     </update>

+ 15 - 1
ems/ems-core/src/main/resources/mapper/ems/MeterDeviceMapper.xml

@@ -40,7 +40,21 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             </if>
         </where>
     </select>
-    
+
+    <select id="selectByModel" parameterType="com.ruoyi.ems.model.QueryDevice" resultMap="meterDeviceResult">
+        select
+            m.id, m.area_code, m.device_code, m.device_name, m.location, m.location_ref, m.meter_cls, m.obj_tag, m.col_cycle, m.col_mode, m.magnification, m.spec_desc
+        from adm_meter_device m
+        left join adm_ems_device d on m.device_code = d.device_code
+        <where>
+            d.device_model = #{deviceModel}
+            <if test="areaCode != null and areaCode != ''"> and area_code = #{areaCode}</if>
+            <if test="meterCls != null and meterCls != ''"> and meter_cls = #{meterCls}</if>
+            <if test="objTag != null and objTag != ''"> and obj_tag = #{objTag}</if>
+            <if test="colMode != null "> and col_mode = #{colMode}</if>
+        </where>
+    </select>
+
     <select id="selectMeterDeviceById" parameterType="Long" resultMap="meterDeviceResult">
         <include refid="selectMeterDeviceVo"/>
         where id = #{id}

+ 1 - 0
ems/sql/ems_server.sql

@@ -1162,6 +1162,7 @@ create table adm_elec_meter_h (
   `meter_type`         int             default null                 comment '计量类型 -1:低谷电 0:平峰电 1:高峰电 2:尖峰电',
   `meter_unit_price`   double          default null                 comment '单位电价(1度电)',
   `use_elec_cost`      double          default null                 comment '小时电费',
+  `meter_reading`      double          default null                 comment '抄表示数',
   `create_time`        datetime        default CURRENT_TIMESTAMP    comment '创建时间',
   primary key (`id`),
   unique key ux_elec_meter_h(`device_code`, `record_time`)