From 6cf8d965a30d7f3c364cc79818d9bf5344d234db Mon Sep 17 00:00:00 2001
From: XMnhwj_BackTechnologyDevelopment <3512363680@qq.com>
Date: Tue, 30 May 2023 14:07:59 +0800
Subject: [PATCH] =?UTF-8?q?2023-05-30=2014:07:36=20=E4=BC=98=E5=8C=96?=
 =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87=E7=8A=B6?=
 =?UTF-8?q?=E6=80=81=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=A2=9E?=
 =?UTF-8?q?=E5=8A=A0=E8=AE=BE=E5=A4=87=E5=BD=93=E5=89=8D=E5=AE=9E=E6=97=B6?=
 =?UTF-8?q?=E6=B8=A9=E5=BA=A6=E6=95=B0=E6=8D=AE!?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../psdc/mapper/PsdcElectricHtdataMapper.java |  2 +-
 .../mapper/PsdcThermometerRtdataMapper.java   |  9 +++
 .../service/impl/PsdcDeviceServiceImpl.java   | 74 ++++++++++++-------
 .../business/PsdcElectricRtdataMapper.xml     | 33 +++++----
 .../business/PsdcThermometerRtdataMapper.xml  | 12 +++
 5 files changed, 91 insertions(+), 39 deletions(-)

diff --git a/psdc-business/src/main/java/com/psdc/mapper/PsdcElectricHtdataMapper.java b/psdc-business/src/main/java/com/psdc/mapper/PsdcElectricHtdataMapper.java
index 700aa38..71bb2e1 100644
--- a/psdc-business/src/main/java/com/psdc/mapper/PsdcElectricHtdataMapper.java
+++ b/psdc-business/src/main/java/com/psdc/mapper/PsdcElectricHtdataMapper.java
@@ -29,7 +29,7 @@ public interface PsdcElectricHtdataMapper{
                                                                  @Param(value = "upDate") String upDate);
 
     List<TodayThisHourUseElectricInfoRes> selThisDayUseElectric(@Param(value = "deviceId") Integer deviceId,
-                                                                  @Param(value = "upDate") String upDate);
+                                                                @Param(value = "upDate") String upDate);
 
     Double selTodayUseEnergy(@Param(value = "deviceId") Integer deviceId,
                              @Param(value = "beginTime") String begin_time,
diff --git a/psdc-business/src/main/java/com/psdc/mapper/PsdcThermometerRtdataMapper.java b/psdc-business/src/main/java/com/psdc/mapper/PsdcThermometerRtdataMapper.java
index 4c6d085..805548c 100644
--- a/psdc-business/src/main/java/com/psdc/mapper/PsdcThermometerRtdataMapper.java
+++ b/psdc-business/src/main/java/com/psdc/mapper/PsdcThermometerRtdataMapper.java
@@ -66,4 +66,13 @@ public interface PsdcThermometerRtdataMapper{
      * @return 影响行数
      */
     int deleteById(Integer thermometerRtdataId);
+
+    /**
+     * 根据设备Id查询单设备实时温度数据
+     * @param deviceId
+     * @param upDateHour
+     * @return
+     */
+    PsdcThermometerDataRes selTempByDevId(@Param(value = "deviceId") Integer deviceId,
+                                          @Param(value = "upDateHour") String upDateHour);
 }
\ No newline at end of file
diff --git a/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java b/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java
index 4673708..578e56b 100644
--- a/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java
+++ b/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java
@@ -4,18 +4,17 @@ import com.psdc.entity.PsdcControlLog;
 import com.psdc.entity.PsdcDevice;
 import com.psdc.entity.PsdcScene;
 import com.psdc.entity.res.PsdcDeviceInfoRes;
+import com.psdc.entity.res.PsdcThermometerDataRes;
 import com.psdc.entity.vo.DeviceStatusVo;
 import com.psdc.entity.vo.ModelVo;
 import com.psdc.enums.ControlKeyEnum;
 import com.psdc.exception.ControlException;
-import com.psdc.mapper.PsdcControlLogMapper;
-import com.psdc.mapper.PsdcDeviceMapper;
-import com.psdc.mapper.PsdcModelMapper;
-import com.psdc.mapper.PsdcSceneMapper;
+import com.psdc.mapper.*;
 import com.psdc.mqtt.MyMQTTClient;
 import com.psdc.service.IPsdcDeviceService;
 import com.psdc.utils.SecurityUtils;
 import com.psdc.utils.poi.ExcelUtil;
+import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
@@ -24,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.InputStream;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
@@ -46,6 +46,11 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
     @Resource
     private PsdcModelMapper psdcModelMapper;
 
+    @Resource
+    private PsdcThermometerRtdataMapper thermometerRtdataMapper;
+
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
+
     /**
      * 通过ID查询单条数据
      *
@@ -59,6 +64,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
 
     /**
      * 根据用户id查询设备列表
+     *
      * @return 设备列表
      */
     public List<PsdcDevice> queryByUserId(Integer parent) {
@@ -76,15 +82,33 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
     }
 
     public List<DeviceStatusVo> queryDeviceStatus() {
+        Date data = new Date();
         List<PsdcDevice> psdcDevices = psdcDeviceMapper.selDevicesByUserId(SecurityUtils.getUserId());
-        for (PsdcDevice pd : psdcDevices){
+        for (PsdcDevice pd : psdcDevices) {
             String deviceModel = pd.getDeviceModel();
+            Integer deviceId = pd.getDeviceId();
             List<Map> ary = new ArrayList<>();
             List<ModelVo> list = psdcModelMapper.selDevIsControlByModelName(deviceModel);
-            for (ModelVo mv : list){
+            for (ModelVo mv : list) {
+                PsdcThermometerDataRes thermometerDataRes = thermometerRtdataMapper.selTempByDevId(deviceId, sdf.format(data));
                 Map<String, Object> map = new HashMap<>();
+                Double tempValue = 0.0;
                 map.put("controlElement", mv.getRelationField());
                 map.put("controlRemark", mv.getIoDesc());
+                if (null != thermometerDataRes) {
+                    switch (mv.getRelationField()) {
+                        case "intemp":
+                            tempValue = thermometerDataRes.getThermometerValueIn();
+                            break;
+                        case "outtemp":
+                            tempValue = thermometerDataRes.getThermometerValueOut();
+                            break;
+                        case "temp":
+                            tempValue = thermometerDataRes.getThermometerValue();
+                            break;
+                    }
+                }
+                map.put("tempValue", tempValue);
                 ary.add(map);
             }
             pd.setControl_elements(ary);
@@ -93,7 +117,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
         return psdcDevices.stream().map(psdcDevice -> {
             DeviceStatusVo deviceStatusVo = new DeviceStatusVo();
             BeanUtils.copyProperties(psdcDevice, deviceStatusVo);
-            if (psdcDevice.getDeviceRunstatus() !=null && psdcDevice.getDeviceRunstatus() == 1  ){
+            if (psdcDevice.getDeviceRunstatus() != null && psdcDevice.getDeviceRunstatus() == 1) {
                 deviceStatusVo.setDeviceRunstatus(true);
             } else {
                 deviceStatusVo.setDeviceRunstatus(false);
@@ -110,11 +134,11 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
     @Override
     public Boolean upDevUser(Long userId, List<Integer> devList) {
         int up = 0;
-        for (Integer dev : devList){
+        for (Integer dev : devList) {
             int devUser = psdcDeviceMapper.upDevStaUser(userId, dev);
             up = up + devUser;
         }
-        if (up > 0){
+        if (up > 0) {
             return true;
         } else {
             return false;
@@ -137,12 +161,12 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
     public List<PsdcDeviceInfoRes> queryAllByLimit(PsdcDevice psdcDevice) {
         List<PsdcDeviceInfoRes> list = psdcDeviceMapper.queryAllByLimit(psdcDevice);
         List<Integer> ary = new ArrayList<>();
-        for (PsdcDeviceInfoRes dev : list){
+        for (PsdcDeviceInfoRes dev : list) {
             // 导出时转换为String
-            if (null != dev){
+            if (null != dev) {
                 dev.setDevStatus(coverStr("s", dev.getDeviceStatus()));
                 dev.setDevRunstatus(coverStr("r", dev.getDeviceRunstatus()));
-                if (dev.getParentId() != 0){
+                if (dev.getParentId() != 0) {
                     ary.add(dev.getParentId());
                 }
             }
@@ -179,7 +203,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
             InputStream in = file.getInputStream();
             ExcelUtil<PsdcDevice> util = new ExcelUtil<>(PsdcDevice.class);
             List<PsdcDevice> devices = util.importExcel(in);
-            for (PsdcDevice pd : devices){
+            for (PsdcDevice pd : devices) {
                 pd.setCreateBy(cjr);
                 pd.setCreateTime(new Date());
                 // 添加新设备时,默认父模型为根目录
@@ -215,7 +239,8 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
 
     /**
      * 控制设备启动停止
-     * @param deviceId 主键
+     *
+     * @param deviceId  主键
      * @param runStatus 状态
      * @return 条数
      */
@@ -236,16 +261,16 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
             psdcControlLogMapper.insert(new PsdcControlLog(deviceId, null, null, "设备启停", value, controlMethod, 3, "未找到该设备", controlBy));
             throw new ControlException("控制失败,未找到该设备");
         }
-        if (psdcDevice.getIsControl() != 2 ){
+        if (psdcDevice.getIsControl() != 2) {
             throw new ControlException("该设备无法控制,请联系管理员");
         }
-        
+
 
         // TODO 发送MQTT指令
         // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",value,controlMethod,3,"手动控制,等待终端响应超时",controlBy));
         // 发送成功
 
-        psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(), psdcDevice.getDeviceSn(), "设备启停", value, controlMethod, 2, "手动控制,控制成功", controlBy));
+        psdcControlLogMapper.insert(new PsdcControlLog(deviceId, psdcDevice.getDeviceName(), psdcDevice.getDeviceSn(), "设备启停", value, controlMethod, 2, "手动控制,控制成功", controlBy));
         return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId, runStatus);
 
     }
@@ -263,13 +288,13 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
             psdcControlLogMapper.insert(new PsdcControlLog(deviceId, null, null, "设定温度", null, controlMethod, 3, "未找到该设备", controlBy));
             throw new ControlException("控制失败,未找到该设备");
         }
-        if (psdcDevice.getIsControl() != 2 ){
+        if (psdcDevice.getIsControl() != 2) {
             throw new ControlException("该设备无法控制,请联系管理员");
         }
 
         AtomicInteger atomicInteger = new AtomicInteger(0);
 
-        for (HashMap map: data) {
+        for (HashMap map : data) {
 
             String controlKey = map.get("controlKey").toString();
             String controlValue = map.get("controlValue").toString();
@@ -311,7 +336,6 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
         }
 
 
-
         //TODO 发送MQTT指令
         // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,2,"控制成功",controlBy));
         // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,3,"等待终端响应超时",controlBy));
@@ -323,16 +347,17 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
 
     /**
      * 执行策略
+     *
      * @param strategyCode 策略码
      * @return
      */
     @Override
     public String executiveStrategy(Integer strategyCode) {
-        int success = 0 ;
+        int success = 0;
         int lose = 0;
         List<PsdcScene> psdcScenes = psdcSceneMapper.queryBySceneCode(strategyCode);
         //根据策略码查询策略列表
-        for (PsdcScene psdcScene: psdcScenes ) {
+        for (PsdcScene psdcScene : psdcScenes) {
 
             String controlKey = psdcScene.getSceneKey();
             String controlValue = psdcScene.getSceneValue();
@@ -342,8 +367,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
             Boolean flag = Boolean.FALSE;
             //TODO 发送MQTT指令
             flag = Boolean.TRUE;
-            if (flag)
-            {
+            if (flag) {
                 //发送成功
                 int insert = psdcControlLogMapper.insert(new PsdcControlLog(psdcScene.getDeviceId(), deviceName, psdcScene.getDeviceSn(), controlContext, controlValue, 3, 2, "控制成功", SecurityUtils.getUsername()));
                 success = success + insert;
@@ -353,7 +377,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService {
                 // lose = lose + i;
             }
         }
-        if(success == psdcScenes.size()){
+        if (success == psdcScenes.size()) {
             return "控制成功,控制数量:" + success;
         } else {
             return "部分失败,成功:" + success + "条,失败:" + lose + "条";
diff --git a/psdc-business/src/main/resources/mapper/business/PsdcElectricRtdataMapper.xml b/psdc-business/src/main/resources/mapper/business/PsdcElectricRtdataMapper.xml
index 896fdba..5d61f7c 100644
--- a/psdc-business/src/main/resources/mapper/business/PsdcElectricRtdataMapper.xml
+++ b/psdc-business/src/main/resources/mapper/business/PsdcElectricRtdataMapper.xml
@@ -45,17 +45,18 @@
     </resultMap>
 
     <resultMap id="PsdcElectricRtdataMap2" type="com.psdc.entity.res.PsdcDevicePowerRes">
-        <result property="totp" column="TotP" />
-        <result property="totwh" column="TotWh" />
-        <result property="temp" column="thermometer_value" />
-        <result property="tempIn" column="thermometer_value_in" />
-        <result property="tempOut" column="thermometer_value_out" />
-        <result property="runStatus" column="device_runstatus" />
+        <result property="totp" column="TotP"/>
+        <result property="totwh" column="TotWh"/>
+        <result property="temp" column="thermometer_value"/>
+        <result property="tempIn" column="thermometer_value_in"/>
+        <result property="tempOut" column="thermometer_value_out"/>
+        <result property="runStatus" column="device_runstatus"/>
     </resultMap>
 
     <!-- 通过ID查询单条数据 -->
     <select id="queryById" resultMap="PsdcElectricRtdataMap2">
-        Select TotP, TotWh, ptr.thermometer_value, ptr.thermometer_value_in, ptr.thermometer_value_out, pd.device_runstatus
+        Select TotP, TotWh, ptr.thermometer_value, ptr.thermometer_value_in, ptr.thermometer_value_out,
+        pd.device_runstatus
         From psdc_device pd
         Left Join psdc_electric_rtdata per On per.device_id = pd.device_id
         Left Join psdc_thermometer_rtdata ptr On ptr.device_id = pd.device_id
@@ -338,11 +339,15 @@ totwh}
     </select>
 
     <!--新增数据-->
-    <insert id="insert" >
-        insert into psdc_electric_rtdata(electric_rtdata_id,device_id,update_time,Ua,Ub,Uc,Ia,Ib,Ic,Pa,Pb,Pc,TotP,Qa,Qb,Qc,TotQ,PFa,PFb,PFc,TotPF,SupWh,RtlWh,SupQh,RtlQh,Sa,Sb,Sc,TotS,
-                                         TotWh,TotWh_1,TotWh_2,TotWh_3,TotWh_4,HZ,Uab,Ubc,Uca,CombWh,CombQh)
-        values (#{electricRtdataId},#{deviceId},#{updateTime},#{ua},#{ub},#{uc},#{ia},#{ib},#{ic},#{pa},#{pb},#{pc},#{totp},#{qa},#{qb},#{qc},#{totq},#{pfa},#{pfb},#{pfc},#{totpf},#{supwh},#{rtlwh},#{supqh},#{rtlqh},#{sa},#{sb},#{sc},#{tots},#{
-totwh},#{totwh1},#{totwh2},#{totwh3},#{totwh4},#{hz},#{uab},#{ubc},#{uca},#{combwh},#{combqh})
+    <insert id="insert">
+        insert into psdc_electric_rtdata(electric_rtdata_id, device_id, update_time, Ua, Ub, Uc, Ia, Ib, Ic, Pa, Pb, Pc,
+                                         TotP, Qa, Qb, Qc, TotQ, PFa, PFb, PFc, TotPF, SupWh, RtlWh, SupQh, RtlQh, Sa,
+                                         Sb, Sc, TotS,
+                                         TotWh, TotWh_1, TotWh_2, TotWh_3, TotWh_4, HZ, Uab, Ubc, Uca, CombWh, CombQh)
+        values (#{electricRtdataId}, #{deviceId}, #{updateTime}, #{ua}, #{ub}, #{uc}, #{ia}, #{ib}, #{ic}, #{pa}, #{pb},
+                #{pc}, #{totp}, #{qa}, #{qb}, #{qc}, #{totq}, #{pfa}, #{pfb}, #{pfc}, #{totpf}, #{supwh}, #{rtlwh},
+                #{supqh}, #{rtlqh}, #{sa}, #{sb}, #{sc}, #{tots}, #{
+totwh}, #{totwh1}, #{totwh2}, #{totwh3}, #{totwh4}, #{hz}, #{uab}, #{ubc}, #{uca}, #{combwh}, #{combqh})
     </insert>
 
     <!-- 更新数据 -->
@@ -479,6 +484,8 @@ totwh},
 
     <!--通过主键删除-->
     <delete id="deleteById">
-        delete from psdc_electric_rtdata where electric_rtdata_id = #{electricRtdataId}
+        delete
+        from psdc_electric_rtdata
+        where electric_rtdata_id = #{electricRtdataId}
     </delete>
 </mapper>
\ No newline at end of file
diff --git a/psdc-business/src/main/resources/mapper/business/PsdcThermometerRtdataMapper.xml b/psdc-business/src/main/resources/mapper/business/PsdcThermometerRtdataMapper.xml
index 1398471..6154007 100644
--- a/psdc-business/src/main/resources/mapper/business/PsdcThermometerRtdataMapper.xml
+++ b/psdc-business/src/main/resources/mapper/business/PsdcThermometerRtdataMapper.xml
@@ -97,6 +97,18 @@
         </where>
     </select>
 
+    <select id="selTempByDevId" resultMap="PsdcThermometerRtdataMap2">
+        Select device_id, thermometer_value, thermometer_value_in, thermometer_value_out From psdc_thermometer_rtdata
+        <where>
+            <if test="deviceId != null and deviceId != ''">
+                And device_id = ${deviceId}
+            </if>
+            <if test="upDateHour != null and upDateHour != ''">
+                And DATE_FORMAT(update_time,'%Y-%m-%d %H') = #{upDateHour}
+            </if>
+        </where>
+    </select>
+
     <!--新增数据-->
     <insert id="insert" >
         insert into psdc_thermometer_rtdata(thermometer_rtdata_id,device_id,thermometer_value,thermometer_value_in,thermometer_value_out,update_time)