diff --git a/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java b/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java index d947644..d0c9a97 100644 --- a/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java +++ b/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java @@ -142,6 +142,17 @@ public class DeviceController { } } + /** + * 设备开关状态数据导出 + */ + @GetMapping("/exportSwitchStatusData") + @OperationLog(description = "设备开关状态数据导出",type = LogType.EXPORT) + public void exportSwitchStatusData(HttpServletResponse response, + @RequestParam String startTime, + @RequestParam String endTime) { + deviceService.exportSwitchStatusData(response, startTime, endTime); + } + } diff --git a/src/main/java/com/isu/gaswellwatch/dao/SwitchStatusExportDao.java b/src/main/java/com/isu/gaswellwatch/dao/SwitchStatusExportDao.java new file mode 100644 index 0000000..ba4195d --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/dao/SwitchStatusExportDao.java @@ -0,0 +1,19 @@ +package com.isu.gaswellwatch.dao; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.isu.gaswellwatch.vo.SwitchStatusExport; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface SwitchStatusExportDao extends BaseMapper { + + void deleteData(); + + List getSwitchStatusExport(@Param("startTime")String startTime, @Param("endTime")String endTime); +} + diff --git a/src/main/java/com/isu/gaswellwatch/modbus/data/listener/BusinessMessageHandlerListener.java b/src/main/java/com/isu/gaswellwatch/modbus/data/listener/BusinessMessageHandlerListener.java index a9d99ec..e1010ce 100755 --- a/src/main/java/com/isu/gaswellwatch/modbus/data/listener/BusinessMessageHandlerListener.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/data/listener/BusinessMessageHandlerListener.java @@ -9,7 +9,9 @@ import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import com.isu.gaswellwatch.service.DeviceOptLogService; import com.isu.gaswellwatch.service.DeviceService; import com.isu.gaswellwatch.service.RemindService; +import com.isu.gaswellwatch.service.SwitchStatusExportService; import com.isu.gaswellwatch.vo.DeviceVO; +import com.isu.gaswellwatch.vo.SwitchStatusExport; import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -37,6 +39,8 @@ public class BusinessMessageHandlerListener implements BatchMessageListener { private RemindService remindService; @Resource private SnowflakeConfig snowflakeConfig; + @Resource + private SwitchStatusExportService switchStatusExportService; @Override @@ -51,6 +55,8 @@ public class BusinessMessageHandlerListener implements BatchMessageListener { List settingList = this.remindService.getAllRemindSetting(); List recordList = new ArrayList<>(); + List switchStatusExportList = new ArrayList<>(); + //将settingList转换为map,key为deviceId,value为List Map> deviceRemindSettingMap = new HashMap<>(); for (RemindSetting setting : settingList) { @@ -84,6 +90,18 @@ public class BusinessMessageHandlerListener implements BatchMessageListener { //比对新旧数据,看开关状态是否一致 if (oldObject.getString(wellStatusKey) != null && (!oldObject.getString(wellStatusKey).equals(newObject.getString(wellStatusKey)))) { deviceWellStatusMap.put(Long.valueOf(deviceId), newObject.getInteger(wellStatusKey)); + + //记录开关井状态及对应的套压油压数据 + SwitchStatusExport switchStatusExport = new SwitchStatusExport(); + switchStatusExport.setId(snowflakeConfig.snowflakeId()); + switchStatusExport.setGasWellName(device.getGasWell().getName()); + switchStatusExport.setProductName(device.getProduct().getName()); + switchStatusExport.setCasPressure(newObject.getString("casPressure")); + switchStatusExport.setOilPressure(newObject.getString("oilPressure")); + if (newObject.getInteger(wellStatusKey)!=null){ + switchStatusExport.setSwitchStatus(newObject.getInteger(wellStatusKey) == 1 ? "开井" : "关井"); + } + switchStatusExportList.add(switchStatusExport); } //根据ID找到对应的提醒字段配置 @@ -117,6 +135,11 @@ public class BusinessMessageHandlerListener implements BatchMessageListener { } } + //保存开关状态数据 + if (!switchStatusExportList.isEmpty()) { + this.switchStatusExportService.saveBatch(switchStatusExportList); + } + //若不一致则记录设备操作日志 if (!deviceWellStatusMap.isEmpty()) { diff --git a/src/main/java/com/isu/gaswellwatch/service/DeviceService.java b/src/main/java/com/isu/gaswellwatch/service/DeviceService.java index 416c962..8cf9bc1 100644 --- a/src/main/java/com/isu/gaswellwatch/service/DeviceService.java +++ b/src/main/java/com/isu/gaswellwatch/service/DeviceService.java @@ -39,5 +39,7 @@ public interface DeviceService extends IService { List getSwitchStatusData(Long deviceId, String startTime, String endTime,String deviceProduct); void exportHistoryData(HttpServletResponse response, Long deviceId, String startTime, String endTime) throws ParseException; + + void exportSwitchStatusData(HttpServletResponse response, String startTime, String endTime); } diff --git a/src/main/java/com/isu/gaswellwatch/service/SwitchStatusExportService.java b/src/main/java/com/isu/gaswellwatch/service/SwitchStatusExportService.java new file mode 100644 index 0000000..40bd50f --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/service/SwitchStatusExportService.java @@ -0,0 +1,12 @@ +package com.isu.gaswellwatch.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.isu.gaswellwatch.vo.SwitchStatusExport; + +import java.util.List; + +public interface SwitchStatusExportService extends IService { + + List getSwitchStatusExport(String startTime, String endTime); +} + diff --git a/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java b/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java index f0b5d47..565904c 100644 --- a/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java @@ -17,16 +17,9 @@ import com.isu.gaswellwatch.exception.BusinessException; import com.isu.gaswellwatch.modbus.data.CacheService; import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService; -import com.isu.gaswellwatch.modbus.data.listener.DynamicRabbitListener; -import com.isu.gaswellwatch.service.DeviceOptLogService; -import com.isu.gaswellwatch.service.DeviceService; -import com.isu.gaswellwatch.service.DictionaryService; -import com.isu.gaswellwatch.service.GasWellService; +import com.isu.gaswellwatch.service.*; import com.isu.gaswellwatch.utils.ConverterUtil; -import com.isu.gaswellwatch.vo.DeviceHistoryDataExportVO; -import com.isu.gaswellwatch.vo.DeviceHistoryVO; -import com.isu.gaswellwatch.vo.DeviceVO; -import com.isu.gaswellwatch.vo.UserOperationRecordExportVO; +import com.isu.gaswellwatch.vo.*; import jakarta.annotation.Resource; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; @@ -63,9 +56,9 @@ public class DeviceServiceImpl extends ServiceImpl implements @Resource private DeviceOptLogService deviceOptLogService; @Resource - private DynamicRabbitListener dynamicRabbitListener; - @Resource private CacheService cacheService; + @Resource + private SwitchStatusExportService switchStatusExportService; @Override public Page page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName, Long deviceTypeId, Long blockId) { @@ -282,6 +275,31 @@ public class DeviceServiceImpl extends ServiceImpl implements } + @Override + public void exportSwitchStatusData(HttpServletResponse response, String startTime, String endTime) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + List list = switchStatusExportService.getSwitchStatusExport(startTime, endTime); + + List export = ConverterUtil.convert(list, SwitchStatusExportVO.class); + + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + String fileName = null; + try { + ServletOutputStream outputStream = response.getOutputStream(); + fileName = URLEncoder.encode("全井数据-"+simpleDateFormat.format(new Date()), StandardCharsets.UTF_8).replaceAll("\\+", "%20"); + response.setHeader("Access-Control-Expose-Headers","Content-Disposition"); + response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); + EasyExcel.write(outputStream) + // 这里放入动态头 + .head(SwitchStatusExportVO.class).sheet("data") + // 当然这里数据也可以用 List> 去传入 + .doWrite(export); + } catch (Exception e) { + log.error(e.getMessage()); + } + } + @Override public Page getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException { diff --git a/src/main/java/com/isu/gaswellwatch/service/impl/SwitchStatusExportServiceImpl.java b/src/main/java/com/isu/gaswellwatch/service/impl/SwitchStatusExportServiceImpl.java new file mode 100644 index 0000000..987b2c3 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/service/impl/SwitchStatusExportServiceImpl.java @@ -0,0 +1,27 @@ +package com.isu.gaswellwatch.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.isu.gaswellwatch.dao.SwitchStatusExportDao; +import com.isu.gaswellwatch.service.SwitchStatusExportService; +import com.isu.gaswellwatch.vo.SwitchStatusExport; +import jakarta.annotation.Resource; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service("switchStatusExportService") +@Transactional(rollbackFor = Exception.class) +public class SwitchStatusExportServiceImpl extends ServiceImpl implements SwitchStatusExportService { + + @Resource + private SwitchStatusExportDao switchStatusExportDao; + + @Override + public List getSwitchStatusExport(String startTime, String endTime) { + return switchStatusExportDao.getSwitchStatusExport(startTime, endTime); + } +} + + diff --git a/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExport.java b/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExport.java new file mode 100644 index 0000000..7987253 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExport.java @@ -0,0 +1,59 @@ +package com.isu.gaswellwatch.vo; + +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.*; +import lombok.experimental.SuperBuilder; + +import java.io.Serial; +import java.io.Serializable; + +/** + * + * @author scwsl + * @date 2024-11-17 + */ +@Getter +@Setter +@NoArgsConstructor +@EqualsAndHashCode +@ToString(callSuper = true) +public class SwitchStatusExport extends Model { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 主键 + */ + private Long id; + + /** + * 气井名称 + */ + private String gasWellName; + + /** + * 品牌名称 + */ + private String productName; + + /** + * 油压 + */ + private String oilPressure; + + /** + * 套压 + */ + private String casPressure; + /** + * 开关状态 + */ + private String switchStatus; + + /** + * 创建时间 + */ + private String createTime; + +} diff --git a/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExportVO.java b/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExportVO.java new file mode 100644 index 0000000..aa7db14 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/vo/SwitchStatusExportVO.java @@ -0,0 +1,53 @@ +package com.isu.gaswellwatch.vo; +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class SwitchStatusExportVO { + + + /** + * 气井名称 + */ + @ExcelProperty(value = "井号") + private String gasWellName; + + /** + * 品牌名称 + */ + @ExcelProperty(value = "设备品牌") + private String productName; + + /** + * 油压 + */ + @ExcelProperty(value = "油压") + private String oilPressure; + + /** + * 套压 + */ + @ExcelProperty(value = "套压") + private String casPressure; + /** + * 开关状态 + */ + @ExcelProperty(value = "开关状态") + private String switchStatus; + + /** + * 创建时间 + */ + @ExcelProperty(value = "状态变更时间") + private String createTime; + + + +} diff --git a/src/main/resources/mapper/SwitchStatusExportDao.xml b/src/main/resources/mapper/SwitchStatusExportDao.xml new file mode 100644 index 0000000..ba99866 --- /dev/null +++ b/src/main/resources/mapper/SwitchStatusExportDao.xml @@ -0,0 +1,24 @@ + + + + + + + delete from switch_status_export + + create_time <= DATE_SUB(NOW(), INTERVAL 7 DAY) + + + + +