From 81a8678db62438d7efbbff66df21bd26cc2f3b19 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 26 Sep 2024 11:05:57 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A4=A7=E5=B1=8F=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2024新增脚本.sql | 6 + .../controller/DevicesManageController.java | 2 +- .../controller/DevicesParamsController.java | 10 +- .../mh/user/controller/ScreenController.java | 70 ++++++ .../mh/user/dto/ScreenRequestParamDTO.java | 64 +++++ .../com/mh/user/entity/DataResultEntity.java | 2 + .../mh/user/entity/DevicesManageEntity.java | 8 +- .../java/com/mh/user/job/DealDataJob.java | 2 +- .../com/mh/user/mapper/DataResultMapper.java | 4 +- .../com/mh/user/mapper/MeterManageMapper.java | 11 + .../java/com/mh/user/mapper/ScreenMapper.java | 238 ++++++++++++++++++ .../com/mh/user/service/ScreenService.java | 48 ++++ .../service/impl/DataResultServiceImpl.java | 95 ++++--- .../impl/GatewayManageServiceImpl.java | 12 +- .../service/impl/MeterManageServiceImpl.java | 2 +- .../user/service/impl/ScreenServiceImpl.java | 210 ++++++++++++++++ .../mh/user/strategy/EleProtocolStrategy.java | 58 +++-- .../user/strategy/ModbusProtocolStrategy.java | 9 +- .../main/java/com/mh/user/utils/DateUtil.java | 16 ++ 19 files changed, 781 insertions(+), 86 deletions(-) create mode 100644 user-service/src/main/java/com/mh/user/controller/ScreenController.java create mode 100644 user-service/src/main/java/com/mh/user/dto/ScreenRequestParamDTO.java create mode 100644 user-service/src/main/java/com/mh/user/mapper/ScreenMapper.java create mode 100644 user-service/src/main/java/com/mh/user/service/ScreenService.java create mode 100644 user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java diff --git a/2024新增脚本.sql b/2024新增脚本.sql index 95ac3c1..f869829 100644 --- a/2024新增脚本.sql +++ b/2024新增脚本.sql @@ -241,3 +241,9 @@ ALTER TABLE mh_jnd.dbo.device_code_param ADD data_type int NULL; EXEC mh_jnd.sys.sp_addextendedproperty 'MS_Description', N'数据类型', 'schema', N'dbo', 'table', N'device_code_param', 'column', N'data_type'; ALTER TABLE mh_jnd.dbo.device_code_param ADD protocol_type int NULL; EXEC mh_jnd.sys.sp_addextendedproperty 'MS_Description', N'协议类型(数据字典表)', 'schema', N'dbo', 'table', N'device_code_param', 'column', N'protocol_type'; + +-- 2024-09-24 data_result表添加字段 +ALTER TABLE data_result ADD register_addr varchar(50) NULL; +EXEC sys.sp_addextendedproperty 'MS_Description', N'寄存器地址', 'schema', N'dbo', 'table', N'data_result', 'column', N'register_addr'; +ALTER TABLE data_result ADD register_name varchar(50) NULL; +EXEC sys.sp_addextendedproperty 'MS_Description', N'寄存器名称', 'schema', N'dbo', 'table', N'data_result', 'column', N'register_name'; diff --git a/user-service/src/main/java/com/mh/user/controller/DevicesManageController.java b/user-service/src/main/java/com/mh/user/controller/DevicesManageController.java index 2be74c2..da88fed 100644 --- a/user-service/src/main/java/com/mh/user/controller/DevicesManageController.java +++ b/user-service/src/main/java/com/mh/user/controller/DevicesManageController.java @@ -45,7 +45,7 @@ public class DevicesManageController { @SysLogger(value="资产管理信息",optDesc = "添加资产管理信息") @PostMapping("/save") - public HttpResult saveGw(DevicesManageEntity entity) { + public HttpResult saveDevice(DevicesManageEntity entity) { devicesManageService.save(entity); return HttpResult.ok(); } diff --git a/user-service/src/main/java/com/mh/user/controller/DevicesParamsController.java b/user-service/src/main/java/com/mh/user/controller/DevicesParamsController.java index ed3337e..6819291 100644 --- a/user-service/src/main/java/com/mh/user/controller/DevicesParamsController.java +++ b/user-service/src/main/java/com/mh/user/controller/DevicesParamsController.java @@ -23,34 +23,34 @@ public class DevicesParamsController { @Autowired private DeviceParamsService deviceParamsService; - @SysLogger(value="资产管理信息",optDesc = "查询资产管理信息") + @SysLogger(value="设备参数管理",optDesc = "查询设备参数管理") @PostMapping("/findByPage") public PageResult queryGatewayManage(@RequestBody PageRequest pageRequest) { return deviceParamsService.queryByPage(pageRequest); } - @SysLogger(value="资产管理信息",optDesc = "编辑资产管理信息") + @SysLogger(value="设备参数管理",optDesc = "编辑设备参数管理") @PostMapping("/update") public HttpResult updateProInfo(DeviceParamsEntity entity) { deviceParamsService.update(entity); return HttpResult.ok(); } - @SysLogger(value="资产管理信息",optDesc = "根据项目id查询对应信息") + @SysLogger(value="设备参数管理",optDesc = "根据项目id查询对应信息") @GetMapping("/findById") public HttpResult findById(@RequestParam("id") Long id) { DeviceParamsEntity entity = deviceParamsService.findById(id); return HttpResult.ok(entity); } - @SysLogger(value="资产管理信息",optDesc = "添加资产管理信息") + @SysLogger(value="设备参数管理",optDesc = "添加设备参数管理") @PostMapping("/save") public HttpResult saveGw(DeviceParamsEntity entity) { deviceParamsService.save(entity); return HttpResult.ok(); } - @SysLogger(value="资产管理信息",optDesc = "删除资产管理信息") + @SysLogger(value="设备参数管理",optDesc = "删除设备参数管理") @GetMapping("/deleteById") public HttpResult deleteProInfo(@RequestParam String id) { deviceParamsService.delete(id); diff --git a/user-service/src/main/java/com/mh/user/controller/ScreenController.java b/user-service/src/main/java/com/mh/user/controller/ScreenController.java new file mode 100644 index 0000000..3c848c5 --- /dev/null +++ b/user-service/src/main/java/com/mh/user/controller/ScreenController.java @@ -0,0 +1,70 @@ +package com.mh.user.controller; + +import com.alibaba.fastjson2.JSONObject; +import com.mh.common.http.HttpResult; +import com.mh.user.dto.ScreenRequestParamDTO; +import com.mh.user.service.ScreenService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +/** + * @author LJF + * @version 1.0 + * @project mh_esi + * @description 大屏接口类 + * @date 2024-09-24 11:40:15 + */ +@RestController +@RequestMapping("/screen") +public class ScreenController { + + @Resource + private ScreenService screenService; + + /** + * 能耗数据 + * @return + */ + @PostMapping("/energyData") + public HttpResult energyData(@RequestBody ScreenRequestParamDTO screenRequestParamDTO) { + return HttpResult.ok(screenService.energyData(screenRequestParamDTO)); + } + + /** + * 节能量概况 + * @return + */ + @PostMapping("/savingEnergyData") + public HttpResult savingEnergy(@RequestBody ScreenRequestParamDTO screenRequestParamDTO) { + return HttpResult.ok(screenService.savingEnergy(screenRequestParamDTO)); + } + + /** + * 节能量同比 + * @return + */ + @PostMapping("/savingYoyEnergyData") + public HttpResult savingYoyEnergyData(@RequestBody ScreenRequestParamDTO screenRequestParamDTO) { + return HttpResult.ok(screenService.savingYoyEnergyData(screenRequestParamDTO)); + } + + /** + * 故障详情 + * @return + */ + @PostMapping("/faultList") + public HttpResult faultList() { + return HttpResult.ok(screenService.faultList()); + } + + /** + * 项目坐标数据 + * @return + */ + @PostMapping("/projectData") + public HttpResult projectData() { + return HttpResult.ok(screenService.projectData()); + } + +} diff --git a/user-service/src/main/java/com/mh/user/dto/ScreenRequestParamDTO.java b/user-service/src/main/java/com/mh/user/dto/ScreenRequestParamDTO.java new file mode 100644 index 0000000..7b80eef --- /dev/null +++ b/user-service/src/main/java/com/mh/user/dto/ScreenRequestParamDTO.java @@ -0,0 +1,64 @@ +package com.mh.user.dto; + +/** + * @author LJF + * @version 1.0 + * @project mh_esi + * @description 大屏请求参数 + * @date 2024-09-24 14:12:01 + */ +public class ScreenRequestParamDTO { + + /** + * 查询类型:now:当前数据, day:日数据, week:周数据, month:月数据, year:年数据 + */ + private String timeType; + + private String startTime; + + private String endTime; + + private String projectId; + + public String getProjectId() { + return projectId; + } + + public void setProjectId(String projectId) { + this.projectId = projectId; + } + + public String getTimeType() { + return timeType; + } + + public void setTimeType(String timeType) { + this.timeType = timeType; + } + + public String getStartTime() { + return startTime; + } + + public void setStartTime(String startTime) { + this.startTime = startTime; + } + + public String getEndTime() { + return endTime; + } + + public void setEndTime(String endTime) { + this.endTime = endTime; + } + + @Override + public String toString() { + return "ScreenRequestParamDTO{" + + "timeType='" + timeType + '\'' + + ", startTime='" + startTime + '\'' + + ", endTime='" + endTime + '\'' + + ", projectId='" + projectId + '\'' + + '}'; + } +} diff --git a/user-service/src/main/java/com/mh/user/entity/DataResultEntity.java b/user-service/src/main/java/com/mh/user/entity/DataResultEntity.java index 105b03e..01f3c1a 100644 --- a/user-service/src/main/java/com/mh/user/entity/DataResultEntity.java +++ b/user-service/src/main/java/com/mh/user/entity/DataResultEntity.java @@ -29,4 +29,6 @@ public class DataResultEntity { private String havedUpdate; private String projectName; private String projectId; + private String registerName; + private String registerAddr; } diff --git a/user-service/src/main/java/com/mh/user/entity/DevicesManageEntity.java b/user-service/src/main/java/com/mh/user/entity/DevicesManageEntity.java index 59d8779..b86a725 100644 --- a/user-service/src/main/java/com/mh/user/entity/DevicesManageEntity.java +++ b/user-service/src/main/java/com/mh/user/entity/DevicesManageEntity.java @@ -116,6 +116,10 @@ public class DevicesManageEntity implements Serializable { */ private Long projectId; + private String factoryBarcode; + + private String imuCode; + @Override public String toString() { return "DevicesManageEntity{" + @@ -127,7 +131,7 @@ public class DevicesManageEntity implements Serializable { ", ratedRefrigerationCapacity=" + ratedRefrigerationCapacity + ", ratedCop=" + ratedCop + ", ratedFlow=" + ratedFlow + - ", productionTime=" + productionTime + + ", productionTime='" + productionTime + '\'' + ", createTime=" + createTime + ", updateTime=" + updateTime + ", createBy='" + createBy + '\'' + @@ -137,6 +141,8 @@ public class DevicesManageEntity implements Serializable { ", grade=" + grade + ", systemId=" + systemId + ", projectId=" + projectId + + ", factoryBarcode='" + factoryBarcode + '\'' + + ", imuCode='" + imuCode + '\'' + '}'; } } diff --git a/user-service/src/main/java/com/mh/user/job/DealDataJob.java b/user-service/src/main/java/com/mh/user/job/DealDataJob.java index 94f446e..abcefb3 100644 --- a/user-service/src/main/java/com/mh/user/job/DealDataJob.java +++ b/user-service/src/main/java/com/mh/user/job/DealDataJob.java @@ -83,7 +83,7 @@ public class DealDataJob { dealDataService.proDataResult(curDate,projectId); //1珠江酒店,2广合,3广大 } } - log.info("---------能效监测定时汇总,每一分钟!"+curDate); + log.info("---------能效监测定时汇总,每一分钟!{}", curDate); } catch (Exception e) { log.error("能效监测定时汇总异常", e); } diff --git a/user-service/src/main/java/com/mh/user/mapper/DataResultMapper.java b/user-service/src/main/java/com/mh/user/mapper/DataResultMapper.java index 4dae317..1272731 100644 --- a/user-service/src/main/java/com/mh/user/mapper/DataResultMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/DataResultMapper.java @@ -14,8 +14,8 @@ import java.util.List; public interface DataResultMapper extends BaseMapper { //保存水电表等数据 - @Insert("insert into data_result(device_addr,device_type,project_id,last_value,last_date,cur_value,cur_date,ratio,calc_value,grade) values (" + - " #{deviceAddr},#{deviceType},#{projectId},#{lastValue},#{lastDate},#{curValue},#{curDate},#{ratio},#{calcValue},#{grade})") + @Insert("insert into data_result(device_addr,device_type,project_id,last_value,last_date,cur_value,cur_date,ratio,calc_value,grade,register_addr, register_name) values (" + + " #{deviceAddr},#{deviceType},#{projectId},#{lastValue},#{lastDate},#{curValue},#{curDate},#{ratio},#{calcValue},#{grade}, #{registerAddr}, #{registerName})") void saveDataResult(DataResultEntity dataResultEntity); @Update("") + List> savingEnergy(ScreenRequestParamDTO screenRequestParamDTO); + + @Select("select " + + " t1.cur_date as curDate, " + + " t1.sumValue as curValue, " + + " isnull(t2.sumValue, 0) as lastValue, " + + " case " + + " when t2.sumValue > 0 then concat(convert(decimal(18, 2),(t1.sumValue - t2.sumValue)/ t2.sumValue * 100), '%') " + + " when (t1.sumValue - t2.sumValue) = 0 then '0.00%' " + + " else '同期无数据' " + + " end yoy " + + "from " + + " ( " + + " select " + + " cur_date, " + + " sum(elect) as sumValue " + + " from " + + " energy_data_day edd " + + " where " + + " edd.cur_date between #{startTime} and #{endTime} " + + " and edd.project_id = #{projectId} " + + " group by " + + " cur_date " + + ") t1 " + + "left join ( " + + " select " + + " convert(varchar(10), " + + " dateadd(YEAR, 1, cur_date), " + + " 120) as cur_date , " + + " case " + + " when elect is null then '同期无数据' " + + " else sum(elect) " + + " end " + + " sumValue " + + " from " + + " energy_data_day edd " + + " where " + + " edd.cur_date between #{lastStartTime} and #{lastEndTime} " + + " and edd.project_id = #{projectId} " + + " group by " + + " cur_date, elect " + + ") t2 on " + + " t1.cur_date = t2.cur_date order by cur_date ") + List> savingYoyEnergyData(@Param("projectId") String projectId, + @Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("lastStartTime") String lastStartTime, + @Param("lastEndTime") String lastEndTime); + @Select("select " + + " pi2.project_name, " + + " t1.other_name " + + "from " + + " project_info pi2 " + + "join ( " + + " select " + + " project_id, " + + " other_name " + + " from " + + " meter_manage mm " + + " where " + + " grade between 50 and 59 " + + " and cur_value = 1 " + + " and day(cur_time) = day(getdate()) " + + " group by " + + " project_id, " + + " other_name " + + " ) t1 on " + + " pi2.id = t1.project_id ") + List> queryFaultList(); +} diff --git a/user-service/src/main/java/com/mh/user/service/ScreenService.java b/user-service/src/main/java/com/mh/user/service/ScreenService.java new file mode 100644 index 0000000..f44d6f5 --- /dev/null +++ b/user-service/src/main/java/com/mh/user/service/ScreenService.java @@ -0,0 +1,48 @@ +package com.mh.user.service; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.mh.user.dto.ScreenRequestParamDTO; + +/** + * @author LJF + * @version 1.0 + * @project mh_esi + * @description 大屏服务类 + * @date 2024-09-24 14:02:44 + */ +public interface ScreenService { + + /** + * 获取项目概况以及当月项目数据 + * @param screenRequestParamDTO + * @return + */ + JSONArray energyData(ScreenRequestParamDTO screenRequestParamDTO); + + /** + * 项目坐标以及是否有故障 + * @return + */ + JSONArray projectData(); + + /** + * 当日节能量概况 + * @param screenRequestParamDTO + * @return + */ + JSONArray savingEnergy(ScreenRequestParamDTO screenRequestParamDTO); + + /** + * 获取节能量同比 + * @param screenRequestParamDTO + * @return + */ + JSONArray savingYoyEnergyData(ScreenRequestParamDTO screenRequestParamDTO); + + /** + * 故障详情 + * @return + */ + JSONArray faultList(); +} diff --git a/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java index e188dd0..8c780d8 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java @@ -1,9 +1,6 @@ package com.mh.user.service.impl; -import com.mh.user.entity.DataResultClEntity; -import com.mh.user.entity.DataResultEntity; -import com.mh.user.entity.DataResultChEntity; -import com.mh.user.entity.DeviceInstallEntity; +import com.mh.user.entity.*; import com.mh.user.mapper.*; import com.mh.user.service.DataResultService; import com.mh.user.service.DeviceInstallService; @@ -57,6 +54,9 @@ public class DataResultServiceImpl implements DataResultService { @Resource DataResultNowMapper dataResultNowMapper; + @Resource + MeterManageMapper meterManageMapper; + private final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override @@ -69,30 +69,26 @@ public class DataResultServiceImpl implements DataResultService { dataResultEntity.setCurDate(date); } //从安装表获取设备信息 - DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(dataResultEntity.getDeviceAddr(), dataResultEntity.getDeviceType(), dataResultEntity.getProjectId()); + MeterManageEntity meterManageEntity = meterManageMapper.selectDevice(dataResultEntity.getDeviceAddr(), + dataResultEntity.getDeviceType(), + dataResultEntity.getProjectId(), + dataResultEntity.getRegisterAddr()); + //判断是否有记录 -// int r = dataResultMapper.selectDataResultCount(sdf1.format(dataResultEntity.getCurDate()), -// dataResultEntity.getDeviceAddr(),dataResultEntity.getDeviceType()); -// if (r==0){//插入记录 DataResultEntity data = new DataResultEntity(); - if (dataResultEntity.getDeviceType().equals("水表") || dataResultEntity.getDeviceType().equals("电表")) { -// String str=dataResultEntity.getDeviceAddr(); -// data.setDeviceAddr(str.substring(str.length()-8,str.length())); //通讯地址 - data.setDeviceAddr(dataResultEntity.getDeviceAddr()); //通讯地址 - } else { - data.setDeviceAddr(dataResultEntity.getDeviceAddr()); //通讯地址 - } + //通讯地址 + data.setDeviceAddr(dataResultEntity.getDeviceAddr()); //通讯地址 data.setDeviceType(dataResultEntity.getDeviceType()); //设备类型 String curDate = sdf1.format(dataResultEntity.getCurDate()); data.setCurDate(sdf1.parse(curDate));//当前日期 data.setCurValue(dataResultEntity.getCurValue()); //当前读数 - data.setProjectId(deviceInstallEntity.getProjectId()); //楼栋编号 - data.setRatio(deviceInstallEntity.getRatio()); //倍率 - BigDecimal lastValue = deviceInstallEntity.getLastValue(); //安装基表上次读数 + data.setProjectId(String.valueOf(meterManageEntity.getProjectId())); //楼栋编号 + data.setRatio(BigDecimal.valueOf(meterManageEntity.getMtRatio())); //倍率 + BigDecimal lastValue = meterManageEntity.getCurValue(); //安装基表上次读数 BigDecimal calcValue = dataResultEntity.getCurValue().subtract(lastValue); //计算用量 data.setCalcValue(calcValue);//用量 - if (deviceInstallEntity.getLastDate() != null) {//上次抄表日期 - String lastDate = sdf1.format(deviceInstallEntity.getLastDate()); + if (meterManageEntity.getCurTime() != null) {//上次抄表日期 + String lastDate = sdf1.format(meterManageEntity.getCurTime()); data.setLastDate(sdf1.parse(lastDate)); } else { String lastDate = sdf1.format(date); @@ -100,23 +96,11 @@ public class DataResultServiceImpl implements DataResultService { } data.setLastValue(lastValue);//上次读数 data.setGrade(dataResultEntity.getGrade());//级别 + data.setRegisterAddr(dataResultEntity.getRegisterAddr()); + data.setRegisterName(dataResultEntity.getRegisterName()); dataResultMapper.saveDataResult(data); //插入新的记录 -// }else {//修改记录的curValue、calcValue -// DataResultEntity data2=dataResultMapper.selectDataResult(sdf1.format(dataResultEntity.getCurDate()),dataResultEntity.getDeviceAddr(),dataResultEntity.getDeviceType()); -// double lastValue=data2.getLastValue(); //安装基表上次读数 -// double calcValue=dataResultEntity.getCurValue()-lastValue; //计算用量 -// String curDate=sdf1.format(dataResultEntity.getCurDate()); -// data2.setCurDate(sdf1.parse(curDate));//当前日期 -// data2.setCurValue(dataResultEntity.getCurValue());//当前读数 -// data2.setCalcValue(calcValue);//用量 -// -// dataResultMapper.updateDataResult(data2); //更新记录 -// } - //修改安装表中lastValue,lastDate的值 - deviceInstallMapper.updateLastValue(deviceInstallEntity.getId(), dataResultEntity.getCurValue(), date); } catch (Exception e) { - e.printStackTrace(); - + log.error("保存数据异常", e); } } @@ -141,23 +125,34 @@ public class DataResultServiceImpl implements DataResultService { public void saveDataResultCl(DataResultClEntity dataResultClEntity) throws ParseException { try { //获取系统日期 - Date date = new Date(); - if (dataResultClEntity.getCurDate() == null) { - dataResultClEntity.setCurDate(date); - } //从安装表获取设备信息 - DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(dataResultClEntity.getDeviceAddr(), dataResultClEntity.getDeviceType(), dataResultClEntity.getProjectId()); - if (null == deviceInstallEntity) { +// DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(dataResultClEntity.getDeviceAddr(), dataResultClEntity.getDeviceType(), dataResultClEntity.getProjectId()); + MeterManageEntity meterManageEntity = meterManageMapper.selectDevice(dataResultClEntity.getDeviceAddr(), + dataResultClEntity.getDeviceType(), + dataResultClEntity.getProjectId(), + dataResultClEntity.getRegisterAddr()); + + if (null == meterManageEntity) { return; } - BigDecimal lastValue = deviceInstallEntity.getLastValue(); + + if (meterManageEntity.getCurTime() == null) { + Date date = new Date(); + dataResultClEntity.setLastDate(date); + } + + BigDecimal lastValue = meterManageEntity.getCurValue(); BigDecimal curValue = dataResultClEntity.getCurValue(); - dataResultClEntity.setRatio(deviceInstallEntity.getRatio()); //倍率 + dataResultClEntity.setLastValue(lastValue); + dataResultClEntity.setLastDate(meterManageEntity.getCurTime()); + dataResultClEntity.setRatio(BigDecimal.valueOf(meterManageEntity.getMtRatio())); //倍率 + dataResultClEntity.setCalcValue(curValue.subtract(lastValue).multiply(dataResultClEntity.getRatio())); + // if ((curValue-lastValue)<1000 && (curValue-lastValue)>=0){ if (curValue.compareTo(lastValue) >= 0) { dataResultClMapper.saveDataResultCl(dataResultClEntity); //插入新的记录 //修改安装表中lastValue,lastDate的值 - deviceInstallMapper.updateLastValue(deviceInstallEntity.getId(), curValue, date); +// deviceInstallMapper.updateLastValue(meterManageEntity.getId(), curValue, date); } } catch (Exception e) { @@ -175,10 +170,14 @@ public class DataResultServiceImpl implements DataResultService { dataResultClEntity.setCurDate(date); } //从安装表获取设备信息 - DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(dataResultClEntity.getDeviceAddr(), dataResultClEntity.getDeviceType(), dataResultClEntity.getProjectId()); - dataResultClEntity.setRatio(deviceInstallEntity.getRatio()); //倍率 +// DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(dataResultClEntity.getDeviceAddr(), dataResultClEntity.getDeviceType(), dataResultClEntity.getProjectId()); + MeterManageEntity meterManageEntity = meterManageMapper.selectDevice(dataResultClEntity.getDeviceAddr(), + dataResultClEntity.getDeviceType(), + dataResultClEntity.getProjectId(), + dataResultClEntity.getRegisterAddr()); + dataResultClEntity.setRatio(BigDecimal.valueOf(meterManageEntity.getMtRatio())); //倍率 String curDate = DateUtil.dateToString(dataResultClEntity.getCurDate(), "yyyy-MM-dd HH:mm:ss"); - String deviceAddr = deviceInstallEntity.getDeviceAddr(); + String deviceAddr = meterManageEntity.getMtCode(); String registerAddr = dataResultClEntity.getRegisterAddr(); String projectId = dataResultClEntity.getProjectId(); int row = dataResultClBakMapper.selectDataResultChBakCount(curDate, deviceAddr, registerAddr, projectId);//无记录 @@ -203,7 +202,7 @@ public class DataResultServiceImpl implements DataResultService { //从安装表获取设备信息 String deviceAddr = dataResultChEntity.getDeviceAddr(); String registerAddr = dataResultChEntity.getRegisterAddr(); - String curDate = DateUtil.dateToString(dataResultChEntity.getCurDate(), "yyyy-MM-dd HH:mm:ss"); + String curDate = DateUtil.dateToString(dataResultChEntity.getCurDate(), "yyyy-MM-dd HH:mm:00"); String projectId = dataResultChEntity.getProjectId(); int row = dataResultChMapper.selectDataResultChCount(curDate, deviceAddr, registerAddr, projectId, dataResultChEntity.getGrade());//无记录 if (row == 0) { diff --git a/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java index 117a423..914bdfd 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java @@ -39,10 +39,14 @@ public class GatewayManageServiceImpl implements GatewayManageService { if (pageNum != 0) { PageHelper.startPage(pageNum, pageSize); } - List manageEntities = gatewayManageMapper.selectList( - new QueryWrapper() - .eq("system_id", systemID) - .eq("project_id", projectId) + QueryWrapper queryWrapper = new QueryWrapper<>(); + if (!StringUtils.isBlank(systemID)) { + queryWrapper.eq("system_id", systemID); + } + if (!StringUtils.isBlank(projectId)) { + queryWrapper.eq("project_id", projectId); + } + List manageEntities = gatewayManageMapper.selectList(queryWrapper .orderByDesc("create_time")); return MybatisPageHelper.getPageResult(pageRequest, new PageInfo<>(manageEntities)); } diff --git a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java index 083118a..8e511e4 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java @@ -43,7 +43,7 @@ public class MeterManageServiceImpl implements MeterManageService { if (!StringUtils.isBlank(projectId)) { queryWrapper.eq("project_id", projectId); } - queryWrapper.eq("is_use", 1); + queryWrapper.eq("is_use", 0); return meterManageMapper.selectList(queryWrapper.orderByDesc("create_time")); } diff --git a/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java new file mode 100644 index 0000000..38a7316 --- /dev/null +++ b/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java @@ -0,0 +1,210 @@ +package com.mh.user.service.impl; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.mh.user.dto.ScreenRequestParamDTO; +import com.mh.user.mapper.ScreenMapper; +import com.mh.user.service.ScreenService; +import com.mh.user.utils.DateUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author LJF + * @version 1.0 + * @project mh_esi + * @description 大屏服务实现类 + * @date 2024-09-24 14:52:28 + */ +@Slf4j +@Service +public class ScreenServiceImpl implements ScreenService { + + @Resource + private ScreenMapper screenMapper; + + @Override + public JSONArray faultList() { + JSONArray resultJson = new JSONArray(); + List> faultList = screenMapper.queryFaultList(); + if (faultList.isEmpty()) { + return resultJson; + } + // 根据项目名称分组 + Map>> projectIds = faultList.stream().collect(Collectors.groupingBy(map -> map.get("project_name"))); + for (Map.Entry>> entry : projectIds.entrySet()) { + String projectName = (String) entry.getKey(); + List> projectId = faultList.stream().filter(map -> map.get("project_name").equals(projectName)).collect(Collectors.toList()); + JSONObject data = new JSONObject(); + data.put("projectName", projectName); + data.put("faultNum", projectId.size()); + String[] faultListStr = new String[projectId.size()]; + for (int i = 0; i < projectId.size(); i++) { + faultListStr[i] = (String) projectId.get(i).get("other_name"); + } + data.put("faultList", faultListStr); + resultJson.add(data); + } + return resultJson; + } + + @Override + public JSONArray savingYoyEnergyData(ScreenRequestParamDTO screenRequestParamDTO) { + // 判断类型 + JSONArray resultJson = new JSONArray(); + switch (screenRequestParamDTO.getTimeType()) { + case "day": + String projectId = screenRequestParamDTO.getProjectId(); + String startTime = screenRequestParamDTO.getStartTime(); + String endTime = screenRequestParamDTO.getEndTime(); + String lastStartTime = DateUtil.yoyDate(startTime + " 00:00:00").substring(0, 10); + String lastEndTime = DateUtil.yoyDate(endTime+ " 23:59:59").substring(0, 10); + List> monthData = screenMapper.savingYoyEnergyData(projectId, startTime, endTime, lastStartTime, lastEndTime); + resultJson.add(monthData); + break; + default: + break; + } + return resultJson; + } + + @Override + public JSONArray savingEnergy(ScreenRequestParamDTO screenRequestParamDTO) { + // 判断类型 + JSONArray resultJson = new JSONArray(); + switch (screenRequestParamDTO.getTimeType()) { + case "day": + screenRequestParamDTO.setStartTime(screenRequestParamDTO.getStartTime()); + screenRequestParamDTO.setEndTime(screenRequestParamDTO.getEndTime()); + List> monthData = screenMapper.savingEnergy(screenRequestParamDTO); + resultJson.add(monthData); + break; + default: + break; + } + return resultJson; + } + + @Override + public JSONArray projectData() { + JSONArray resultJson = new JSONArray(); + List> projectData = screenMapper.queryProjectData(); + if (projectData.isEmpty()) { + return resultJson; + } + for (Map map : projectData) { + JSONObject data = new JSONObject(); + float[] ll = new float[2]; + data.put("name", map.get("project_name")); + ll[0] = Float.parseFloat(map.get("longitude").toString()); + ll[1] = Float.parseFloat(map.get("latitude").toString()); + data.put("value", ll); + data.put("fault", map.get("fault")); + resultJson.add(data); + } + return resultJson; + } + + @Override + public JSONArray energyData(ScreenRequestParamDTO screenRequestParamDTO) { + // 判断类型 + JSONArray resultJson = new JSONArray(); + switch (screenRequestParamDTO.getTimeType()) { + case "now": + getNowData(resultJson); + break; + case "day": + screenRequestParamDTO.setStartTime(screenRequestParamDTO.getStartTime()); + screenRequestParamDTO.setEndTime(screenRequestParamDTO.getEndTime()); + getMonthData(resultJson, screenRequestParamDTO); + break; + default: + break; + } + return resultJson; + } + + private void getMonthData(JSONArray resultJson, ScreenRequestParamDTO screenRequestParamDTO) { + List> monthData = screenMapper.queryMonthData(screenRequestParamDTO); + resultJson.add(monthData); + } + + private void getNowData(JSONArray resultJson) { + List> nowData = screenMapper.queryNowData(); + if (nowData.isEmpty()) { + return; + } + + // 根据项目名进行分组 + Map>> projectIds = nowData.stream().collect(Collectors.groupingBy(map -> map.get("id"))); + // 根据项目名获取对应的冷量计、电表数据,计算EER + for (Map.Entry>> entry : projectIds.entrySet()) { + Long proId = (Long) entry.getKey(); + List> projectId = nowData.stream().filter(map -> map.get("id").equals(proId)).collect(Collectors.toList()); + if (projectId.isEmpty()) { + continue; + } + String projectName = (String) projectId.get(0).get("project_name"); + log.info("项目名:{}", projectName); + String cooling = "未采集"; + String ele = "未采集"; + JSONObject data = new JSONObject(); + data.put("projectName", projectName); + for (Map map : projectId) { + // 获取冷量计:2、电表数据:1 + Long mtType = (Long) map.get("mt_type"); + if (null == mtType) { + continue; + } + if (1 == mtType) { + ele = ((BigDecimal) map.get("cur_value")).setScale(2, RoundingMode.HALF_UP).toString(); + } + if (2 == mtType) { + cooling = ((BigDecimal) map.get("cur_value")).setScale(2, RoundingMode.HALF_UP).toString(); + } + } + JSONObject dataDetail = new JSONObject(); + dataDetail.clear(); + + HashMap map = new HashMap<>(); + map.put("name", projectName); + map.put("value", ele); + dataDetail.put("ele", new HashMap<>(map)); + + map.clear(); + map.put("name", projectName); + map.put("value", cooling); + dataDetail.put("cooling", new HashMap<>(map)); + + // 计算EER + map.clear(); + if ("未采集".equals(cooling) || "未采集".equals(ele)) { + map.put("name", projectName); + map.put("value", "未采集"); + dataDetail.put("eer", new HashMap<>(map)); + } else { + double coolingValue = Double.parseDouble(cooling); + double eleValue = Double.parseDouble(ele); + double eer = 0; + if (eleValue <= 0 || coolingValue <= 0) { + eer = 0; + } else { + eer = coolingValue / eleValue; + } + map.put("name", projectName); + map.put("value", (new BigDecimal(eer).setScale(2, RoundingMode.HALF_UP)).toString()); + dataDetail.put("eer", new HashMap<>(map)); + } + data.put("data", dataDetail); + resultJson.add(data); + } + } +} diff --git a/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java b/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java index 6d2e22b..c1b0693 100644 --- a/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java @@ -113,13 +113,7 @@ public class EleProtocolStrategy implements ProtocolStrategy { String meterId = checkStr.substring(12, 14) + checkStr.substring(10, 12) + checkStr.substring(8, 10) + checkStr.substring(6, 8) + checkStr.substring(4, 6) + checkStr.substring(2, 4); meterId = String.format("%012d", Long.parseLong(meterId)); - StringBuilder stringBuilder = getStringBuilder(receiveData, checkStr); - data = stringBuilder.toString(); - data = ExchangeStringUtil.cutThree(data); - // 0 代表前面补充0,4 代表长度为4,d 代表参数为正数型 - data = String.format("%08d", Long.parseLong(data)); - data = data.substring(0, 6) + "." + data.substring(6, 8); - log.info("电表表号:{},电表读数:{}", meterId, data); + data = getAnalysisData(receiveData, checkStr, meterId); } else { log.info("电表报文检验失败: {}", receiveData); } @@ -132,20 +126,44 @@ public class EleProtocolStrategy implements ProtocolStrategy { return data; } - private static StringBuilder getStringBuilder(String receiveData, String checkStr) { + private static String getAnalysisData(String receiveData, String checkStr, String meterId) { StringBuilder stringBuilder = new StringBuilder(); - if (receiveData.length() == 36 || receiveData.length() == 34) { - for (int i = 0; i < 4; i++) { - String data1 = checkStr.substring(32 - 2 * (i + 1), 32 - 2 * i); - stringBuilder.append(data1); - } - } else { - for (int i = 0; i < 4; i++) { - String data1 = checkStr.substring(36 - 2 * (i + 1), 36 - 2 * i); - stringBuilder.append(data1); + // 累计总读数据:FE FE FE FE 68 69 04 00 70 01 08 68 81 06 43 C3 A5 33 33 33 81 16 + // 瞬时有功功率:FE FE FE FE 68 69 04 00 70 01 08 68 81 05 63 E9 33 33 33 21 16 + // 判断属于哪个数据标识 + String resultData = Constant.FAIL; + if (receiveData.length() > 24) { + String identifyCode = ExchangeStringUtil.changePosition(ExchangeStringUtil.cutThree(receiveData.substring(20, 24))); + switch (identifyCode) { + case "9010": + // 累计总读数据 + for (int i = 0; i < 4; i++) { + String data1 = checkStr.substring(32 - 2 * (i + 1), 32 - 2 * i); + stringBuilder.append(data1); + } + resultData = stringBuilder.toString(); + resultData = ExchangeStringUtil.cutThree(resultData); + // 0 代表前面补充0,4 代表长度为4,d 代表参数为正数型 + resultData = String.format("%08d", Long.parseLong(resultData)); + resultData = resultData.substring(0, 6) + "." + resultData.substring(6, 8); + log.info("电表表号:{},电表累计总量读数:{}", meterId, resultData); + break; + case "B630": + // 瞬时有功功率 + for (int i = 0; i < 3; i++) { + String data1 = checkStr.substring(30 - 2 * (i + 1), 30 - 2 * i); + stringBuilder.append(data1); + } + resultData = stringBuilder.toString(); + resultData = ExchangeStringUtil.cutThree(resultData); + // 0 代表前面补充0,4 代表长度为4,d 代表参数为正数型 + resultData = String.format("%06d", Long.parseLong(resultData)); + resultData = resultData.substring(0, 2) + "." + resultData.substring(2, 6); + log.info("电表表号:{},电表瞬时有功功率读数:{}", meterId, resultData); + break; } } - return stringBuilder; + return resultData; } /** @@ -160,11 +178,13 @@ public class EleProtocolStrategy implements ProtocolStrategy { try { DataResultEntity dataResultEntity = new DataResultEntity(); dataResultEntity.setDeviceAddr(deviceCodeParam.getDeviceAddr()); //通讯编号 - dataResultEntity.setDeviceType("电表"); //类型 + dataResultEntity.setDeviceType(deviceCodeParam.getDeviceType()); //类型 dataResultEntity.setProjectId(deviceCodeParam.getProjectId()); //所属项目 dataResultEntity.setCurValue(new BigDecimal(dataStr)); //当前读数 dataResultEntity.setCurDate(sdf1.parse(dateStr)); //当前日期 dataResultEntity.setGrade(deviceCodeParam.getGrade()); + dataResultEntity.setRegisterAddr(deviceCodeParam.getRegisterAddr()); + dataResultEntity.setRegisterName(deviceCodeParam.getRegisterName()); String projectName = projectInfoService.selectName(deviceCodeParam.getProjectId()); log.info("电表==>{},读数==>{},项目名称==>{}", deviceCodeParam.getDeviceAddr(), dataStr, projectName); dataResultService.saveDataResult(dataResultEntity); diff --git a/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java b/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java index 70fe2f5..62b5445 100644 --- a/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java @@ -158,10 +158,10 @@ public void initialDataResultCh(DeviceCodeParamEntity deviceCodeParam, DataResul dataResultCh.setFunCode(deviceCodeParam.getFunCode()); dataResultCh.setProjectId(deviceCodeParam.getProjectId()); String projectName = projectInfoService.selectName(deviceCodeParam.getProjectId()); - log.info("冷水机:" + chillerAddr + ",状态:" + data + ",项目名称:" + projectName); + log.info("冷水机:{},状态:{},项目名称:{}", chillerAddr, data, projectName); dataResultService.saveDataResultChiller(dataResultCh); - dataResultService.deleteDataResultNow(deviceCodeParam.getDeviceAddr(), deviceCodeParam.getDeviceType(), deviceCodeParam.getRegisterAddr(), deviceCodeParam.getProjectId()); - log.info("冷水机保存成功!项目名称:" + projectName); + // dataResultService.deleteDataResultNow(deviceCodeParam.getDeviceAddr(), deviceCodeParam.getDeviceType(), deviceCodeParam.getRegisterAddr(), deviceCodeParam.getProjectId()); + log.info("冷水机保存成功!项目名称:{}", projectName); } //解析冷量表 @@ -185,6 +185,7 @@ public void analysisCloudOrder485(final String data, final DeviceCodeParamEntity if (registerAddr.equals("0004") || registerAddr.equals("0014")) { dataResultCh.setDeviceAddr(cloudId); dataResultCh.setDeviceType(deviceCodeParam.getDeviceType()); + dataResultCh.setFunCode(deviceCodeParam.getFunCode()); dataResultCh.setCurDate(sdf1.parse(dateStr)); dataResultCh.setCurValue(data); @@ -200,7 +201,7 @@ public void analysisCloudOrder485(final String data, final DeviceCodeParamEntity } else if (registerAddr.equals("0050")) { dataResultCl.setDeviceAddr(cloudId); dataResultCl.setDeviceType(deviceCodeParam.getDeviceType()); - dataResultCh.setCurDate(sdf1.parse(dateStr)); + dataResultCl.setCurDate(sdf1.parse(dateStr)); BigDecimal lData = new BigDecimal(data); dataResultCl.setCurValue(lData);//字符串转整型 dataResultCl.setRegisterAddr(deviceCodeParam.getRegisterAddr()); diff --git a/user-service/src/main/java/com/mh/user/utils/DateUtil.java b/user-service/src/main/java/com/mh/user/utils/DateUtil.java index 41ef900..7543746 100644 --- a/user-service/src/main/java/com/mh/user/utils/DateUtil.java +++ b/user-service/src/main/java/com/mh/user/utils/DateUtil.java @@ -3,6 +3,8 @@ package com.mh.user.utils; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; @@ -308,4 +310,18 @@ public class DateUtil { } return null; } + + /* + * 获取同一个时间的上一个时间 + * @param timerStr + * @return + */ + public static String yoyDate(String timerStr) { + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + DateTimeFormatter timeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate localDate = LocalDate.parse(timerStr, timeFormatter); + LocalDate localDate1 = localDate.minusYears(1); + return localDate1.format(timeFormatter1).concat(timerStr.substring(10)); + } + }