Pārlūkot izejas kodu

增加定时任务

hym 4 gadi atpakaļ
vecāks
revīzija
484b14d4ac

+ 2 - 1
water_query_api/src/main/java/com/zcxk/WaterQueryApiApplication.java

@@ -5,10 +5,11 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
-
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 
 @SpringBootApplication
+@EnableScheduling
 public class WaterQueryApiApplication extends SpringBootServletInitializer {
 
     public static void main(String[] args) {

+ 17 - 1
water_query_api/src/main/java/com/zcxk/controller/WaterApiController.java

@@ -150,7 +150,7 @@ public class WaterApiController {
         return new AjaxMessage<>(ResultStatus.OK, result);
     }
     /**
-     * 规则列表
+     * 水表用水量
      *
      * @param meterNo 水表档案号
      *
@@ -166,5 +166,21 @@ public class WaterApiController {
         Double deviceVolume = meterReadRecordService.getDeviceVolume(meterNo, customerNo, startDate, endDate);
         return new AjaxMessage<>(ResultStatus.OK, deviceVolume);
     }
+    /**
+     * 删除警告规则
+     *
+     * @param id 规则id
+     *
+     * @return Response对象
+     */
+    @RequestMapping(value = "delWaringRule", method = RequestMethod.POST)
+    @ApiOperation(value = "用水量")
+    public AjaxMessage< Double> delWaringRule(
+            @ApiParam(value = "id", required = true) @RequestParam Integer id
+            ,@ApiParam(value = "用户名", required = true)  @RequestParam String userName
+           ) {
+        Double deviceVolume = warningRuleService.delWaringRule(id,userName);
+        return new AjaxMessage<>(ResultStatus.OK, deviceVolume);
+    }
 
 }

+ 3 - 2
water_query_api/src/main/java/com/zcxk/dao/MeterReadRecordMapper.java

@@ -6,7 +6,8 @@ import com.zcxk.entity.MeterReadRecord;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
-import java.time.LocalDateTime;
+import java.util.Date;
+
 import java.util.List;
 
 @Mapper
@@ -21,7 +22,7 @@ public interface MeterReadRecordMapper {
 
     List<UseWaterDto> getUseWaterByMonth(@Param("deviceId") Long deviceId, @Param("startDate") Integer startDate, @Param("endDate") Integer endDate);
 
-    Integer queryNotWaterConsumptionDays(@Param("deviceId") Long deviceId, @Param("dateCreate") LocalDateTime dateCreate);
+    Integer queryNotWaterConsumptionDays(@Param("deviceId") Long deviceId, @Param("dateCreate") Date dateCreate);
 
     Boolean exceedConsumption(@Param("deviceId") Long deviceId, @Param("date") Integer date);
 }

+ 19 - 0
water_query_api/src/main/java/com/zcxk/dao/WarningLogMapper.java

@@ -0,0 +1,19 @@
+package com.zcxk.dao;
+
+
+import com.zcxk.entity.WarningLog;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+@Mapper
+public interface WarningLogMapper {
+    int insertSelective(WarningLog record);
+
+    int updateByPrimaryKeySelective(WarningLog record);
+
+    int batchInsert(@Param("list") List<WarningLog> list);
+
+    WarningLog findByDeviceId(@Param("deviceId") Long deviceId, @Param("warningType") Integer warningType);
+}

+ 66 - 0
water_query_api/src/main/java/com/zcxk/entity/WarningLog.java

@@ -0,0 +1,66 @@
+package com.zcxk.entity;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.math.BigInteger;
+import java.time.LocalDateTime;
+
+@ApiModel(value="com-zcxk-water-entity-WarningLog")
+@Data
+public class WarningLog {
+    @ApiModelProperty(value="")
+    private Integer id;
+
+    /**
+    * 客户id
+    */
+    @ApiModelProperty(value="客户id")
+    private BigInteger clientUserId;
+
+    /**
+    * 设备id
+    */
+    @ApiModelProperty(value="设备id")
+    private Long deviceId;
+
+    /**
+    * 预警类型
+    */
+    @ApiModelProperty(value="预警类型")
+    private Integer warningType;
+
+    /**
+    * 反馈状态
+    */
+    @ApiModelProperty(value="反馈状态 0:待反馈 1:已确认无异常 2:已反馈信息")
+    private Integer feedbackStatus;
+
+    /**
+    * 反馈内容
+    */
+    @ApiModelProperty(value="反馈内容")
+    private String feedbackContent;
+
+    /**
+    * 最后预警时间
+    */
+    @ApiModelProperty(value="最后预警时间")
+    private LocalDateTime lastDate;
+
+    @ApiModelProperty(value="")
+    private Integer status;
+
+    @ApiModelProperty(value="")
+    private String createBy;
+
+    @ApiModelProperty(value="")
+    private LocalDateTime dateCreate;
+
+    @ApiModelProperty(value="")
+    private String updateBy;
+
+    @ApiModelProperty(value="")
+    private LocalDateTime dateUpdate;
+}

+ 163 - 0
water_query_api/src/main/java/com/zcxk/scheduled/WarningMessageJob.java

@@ -0,0 +1,163 @@
+package com.zcxk.scheduled;
+
+
+import com.zcxk.dao.MeterReadRecordMapper;
+import com.zcxk.dao.WarningLogMapper;
+import com.zcxk.dao.WarningMessageMapper;
+import com.zcxk.dao.WarningRuleMapper;
+import com.zcxk.entity.WarningLog;
+import com.zcxk.entity.WarningMessage;
+import com.zcxk.entity.WarningRule;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.List;
+
+@Slf4j
+@Component
+@Configuration
+@EnableScheduling
+public class WarningMessageJob {
+
+    @Autowired
+    private MeterReadRecordMapper meterReadRecordMapper;
+    @Resource
+    private WarningRuleMapper warningRuleMapper;
+    @Resource
+    private WarningLogMapper warningLogMapper;
+    @Resource
+    private WarningMessageMapper warningMessageMapper;
+
+
+    /**
+     * 每天预警消息
+     */
+    @Scheduled(cron = "0 0 10 * * ?")
+    public void warningMessage() {
+        log.info("begin warningMessage job");
+        List<WarningRule> list = warningRuleMapper.getList(null);
+        if (list != null && list.size() > 0) {
+            for (WarningRule warningRule : list) {
+                if(warningRule.getWarningType() == 1){
+                    Integer date = Integer.valueOf(LocalDateTime.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
+                    Boolean b =  meterReadRecordMapper.exceedConsumption(warningRule.getDeviceId(),date);
+                    if (b !=null && b) {
+                        WarningLog warningLogNew = new WarningLog();
+                        //warningLogNew.setClientUserId(clientUserDevice.getClientUserId());
+                        warningLogNew.setDeviceId(warningRule.getDeviceId());
+                        warningLogNew.setWarningType(warningRule.getWarningType());
+                        warningLogNew.setFeedbackStatus(0);
+                        warningLogNew.setFeedbackContent("");
+                        warningLogNew.setLastDate(LocalDateTime.now());
+                        warningLogNew.setStatus(1);
+                        warningLogNew.setCreateBy("system");
+                        warningLogNew.setDateCreate(LocalDateTime.now());
+                        warningLogNew.setUpdateBy("system");
+                        warningLogNew.setDateUpdate(LocalDateTime.now());
+                        warningLogMapper.insertSelective(warningLogNew);
+
+                        WarningMessage warningMessageNew = new WarningMessage();
+                        warningMessageNew.setWarningLogId(warningLogNew.getId());
+                        warningMessageNew.setContent("较上日用水量激增30%");
+                        warningMessageNew.setStatus(1);
+                        warningMessageNew.setCreateBy("system");
+                        warningMessageNew.setDateCreate(new Date());
+                        warningMessageNew.setUpdateBy("system");
+                        warningMessageNew.setDateUpdate(new Date());
+                        warningMessageMapper.insertSelective(warningMessageNew);
+                    }
+                }
+
+                if(warningRule.getWarningType() == 2){
+                    Integer days =  meterReadRecordMapper.queryNotWaterConsumptionDays(warningRule.getDeviceId(),warningRule.getDateCreate());
+                    if(days != null && days > 7){
+                        WarningLog warningLog = warningLogMapper.findByDeviceId(warningRule.getDeviceId(),warningRule.getWarningType());
+                        if (warningLog != null) {
+                            long differDays = LocalDateTime.now().toLocalDate().toEpochDay() - warningLog.getLastDate().toLocalDate().toEpochDay();
+                            if(differDays == 1){
+                                //无反馈继续预警
+                                if(warningLog.getFeedbackStatus()==0){
+                                    WarningMessage warningMessageNew = new WarningMessage();
+                                    warningMessageNew.setWarningLogId(warningLog.getId());
+                                    warningMessageNew.setContent("连续无用水量超"+days+"天");
+                                    warningMessageNew.setStatus(1);
+                                    warningMessageNew.setCreateBy("system");
+                                    warningMessageNew.setDateCreate(new Date());
+                                    warningMessageNew.setUpdateBy("system");
+                                    warningMessageNew.setDateUpdate(new Date());
+                                    warningMessageMapper.insertSelective(warningMessageNew);
+
+                                    WarningLog warningLogNew = new WarningLog();
+                                    warningLogNew.setId(warningLog.getId());
+                                    warningLogNew.setLastDate(LocalDateTime.now());
+                                    warningLogNew.setDateUpdate(LocalDateTime.now());
+                                    warningLogMapper.updateByPrimaryKeySelective(warningLogNew);
+                                }
+                            }else {
+                                WarningLog warningLogNew = new WarningLog();
+                                //warningLogNew.setClientUserId(clientUserDevice.getClientUserId());
+                                warningLogNew.setDeviceId(warningRule.getDeviceId());
+                                warningLogNew.setWarningType(warningRule.getWarningType());
+                                warningLogNew.setFeedbackStatus(0);
+                                warningLogNew.setFeedbackContent("");
+                                warningLogNew.setLastDate(LocalDateTime.now());
+                                warningLogNew.setStatus(1);
+                                warningLogNew.setCreateBy("system");
+                                warningLogNew.setDateCreate(LocalDateTime.now());
+                                warningLogNew.setUpdateBy("system");
+                                warningLogNew.setDateUpdate(LocalDateTime.now());
+                                warningLogMapper.insertSelective(warningLogNew);
+
+                                WarningMessage warningMessageNew = new WarningMessage();
+                                warningMessageNew.setWarningLogId(warningLogNew.getId());
+                                warningMessageNew.setContent("连续无用水量超"+days+"天");
+                                warningMessageNew.setStatus(1);
+                                warningMessageNew.setCreateBy("system");
+                                warningMessageNew.setDateCreate(new Date());
+                                warningMessageNew.setUpdateBy("system");
+                                warningMessageNew.setDateUpdate(new Date());
+                                warningMessageMapper.insertSelective(warningMessageNew);
+                            }
+
+                        }else {
+                            WarningLog warningLogNew = new WarningLog();
+                            //warningLogNew.setClientUserId(clientUserDevice.getClientUserId());
+                            warningLogNew.setDeviceId(warningRule.getDeviceId());
+                            warningLogNew.setWarningType(warningRule.getWarningType());
+                            warningLogNew.setFeedbackStatus(0);
+                            warningLogNew.setFeedbackContent("");
+                            warningLogNew.setLastDate(LocalDateTime.now());
+                            warningLogNew.setStatus(1);
+                            warningLogNew.setCreateBy("system");
+                            warningLogNew.setDateCreate(LocalDateTime.now());
+                            warningLogNew.setUpdateBy("system");
+                            warningLogNew.setDateUpdate(LocalDateTime.now());
+                            warningLogMapper.insertSelective(warningLogNew);
+
+                            WarningMessage warningMessageNew = new WarningMessage();
+                            warningMessageNew.setWarningLogId(warningLogNew.getId());
+                            warningMessageNew.setContent("连续无用水量超"+days+"天");
+                            warningMessageNew.setStatus(1);
+                            warningMessageNew.setCreateBy("system");
+                            warningMessageNew.setDateCreate(new Date());
+                            warningMessageNew.setUpdateBy("system");
+                            warningMessageNew.setDateUpdate(new Date());
+                            warningMessageMapper.insertSelective(warningMessageNew);
+
+
+                        }
+                    }
+                }
+            }
+        }
+        log.info("end warningMessage job");
+    }
+}

+ 3 - 0
water_query_api/src/main/java/com/zcxk/service/WarningRuleService.java

@@ -19,4 +19,7 @@ public interface WarningRuleService {
 
     List<WarningRule> getList(String meterNo, String customerNo);
     long getDeviceId(String meterNo, String customerNo);
+
+    Double delWaringRule(Integer id, String userName);
+
 }

+ 11 - 0
water_query_api/src/main/java/com/zcxk/service/impl/WarningRuleServiceImpl.java

@@ -62,4 +62,15 @@ public class WarningRuleServiceImpl implements WarningRuleService {
         }
         return id;
     }
+
+    @Override
+    public Double delWaringRule(Integer id, String userName) {
+        WarningRule warningRule = new WarningRule();
+        warningRule.setId(id);
+        warningRule.setStatus(0);
+        warningRule.setUpdateBy(userName);
+        warningRule.setDateUpdate(new Date());
+        warningRuleMapper.updateByPrimaryKeySelective(warningRule);
+        return null;
+    }
 }