Commit a88c1446 authored by jichao's avatar jichao

定时任务--设备运行时长

parent 60eaa8ce
package org.rcisoft.business.device.assets.dao; package org.rcisoft.business.device.assets.dao;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.*;
import org.rcisoft.business.device.assets.vo.DeviceAssetStatisticVo; import org.rcisoft.business.device.assets.vo.DeviceAssetStatisticVo;
import org.rcisoft.core.base.BaseMapper; import org.rcisoft.core.base.BaseMapper;
import org.rcisoft.business.device.assets.entity.BusDevice; import org.rcisoft.business.device.assets.entity.BusDevice;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
...@@ -63,5 +61,20 @@ public interface BusDeviceRepository extends BaseMapper<BusDevice> { ...@@ -63,5 +61,20 @@ public interface BusDeviceRepository extends BaseMapper<BusDevice> {
*/ */
@Select("<script>select * from bus_device where dev_num = #{devNum}</script>") @Select("<script>select * from bus_device where dev_num = #{devNum}</script>")
List<Map<String,Object>> queryDeviceByDevNum(@Param("devNum") String devNum); List<Map<String,Object>> queryDeviceByDevNum(@Param("devNum") String devNum);
/**
* 查询所有项目中,所有设备的:设备id,最低耗电量,运行时长,耗电量
* @param time
* @return
*/
@Select("<script>select d.DEV_ID,d.SHUT_POWER,d.RUN_TM,sum(e.elec) ELEC from bus_device d,bus_device_meter m,energy_count_m e " +
"where d.DEV_NUM=m.DEV_NUM and m.MET_NUM=e.MET_NUM and date_format(e.CREATE_TIME, '%Y-%m-%d %H:%i')=#{time}" +
"group by d.DEV_ID,d.SHUT_POWER,d.RUN_TM</script>")
List<Map<String, Object>> addRunTime(@Param("time") String time);
@Update("<script><foreach collection=\"list\" item=\"item\" separator=\";\">" +
"update bus_device set RUN_TM=#{item.runTm} where DEV_ID=#{item.devId}" +
"</foreach></script>")
Integer batchUpdateRunTm(List<BusDevice> list);
} }
...@@ -4,6 +4,7 @@ import lombok.*; ...@@ -4,6 +4,7 @@ import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
/** /**
...@@ -42,100 +43,9 @@ public class BusDevice implements Serializable{ ...@@ -42,100 +43,9 @@ public class BusDevice implements Serializable{
private String ownId; private String ownId;
public String getDevId() { private BigDecimal shutPower;
return devId;
}
public void setDevId(String devId) { private BigDecimal runTm;
this.devId = devId;
}
public String getDevNm() {
return devNm;
}
public void setDevNm(String devNm) {
this.devNm = devNm;
}
public String getDevTpId() {
return devTpId;
}
public void setDevTpId(String devTpId) {
this.devTpId = devTpId;
}
public String getDevNum() {
return devNum;
}
public void setDevNum(String devNum) {
this.devNum = devNum;
}
public String getDevLocal() {
return devLocal;
}
public void setDevLocal(String devLocal) {
this.devLocal = devLocal;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getFacId() {
return facId;
}
public void setFacId(String facId) {
this.facId = facId;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public Date getInstallDate() {
return installDate;
}
public void setInstallDate(Date installDate) {
this.installDate = installDate;
}
public String getProId() {
return proId;
}
public void setProId(String proId) {
this.proId = proId;
}
public String getSysId() {
return sysId;
}
public void setSysId(String sysId) {
this.sysId = sysId;
}
public String getOwnId() {
return ownId;
}
public void setOwnId(String ownId) {
this.ownId = ownId;
}
} }
...@@ -69,4 +69,10 @@ public interface BusDeviceService { ...@@ -69,4 +69,10 @@ public interface BusDeviceService {
* @return * @return
*/ */
String getSysNameBySysId(String sysId); String getSysNameBySysId(String sysId);
/**
* 定时任务调用,更新设备的运行时长
* @return
*/
Integer addRunTime();
} }
...@@ -20,11 +20,9 @@ import org.springframework.transaction.annotation.Propagation; ...@@ -20,11 +20,9 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example;
...@@ -176,4 +174,34 @@ public class BusDeviceServiceImpl implements BusDeviceService { ...@@ -176,4 +174,34 @@ public class BusDeviceServiceImpl implements BusDeviceService {
public String getSysNameBySysId(String sysId) { public String getSysNameBySysId(String sysId) {
return busDeviceRepository.getSysNameBySysId(sysId); return busDeviceRepository.getSysNameBySysId(sysId);
} }
@Transactional
@Override
public Integer addRunTime() {
//最后需要保存的数据
List<BusDevice> saveList = new ArrayList<>();
//得到当前时间前10分钟的时间
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(System.currentTimeMillis() - 600000));
//查询10分钟前所有数据的耗电量信息
List<Map<String, Object>> list = busDeviceRepository.addRunTime(time);
list.forEach(map -> {
BigDecimal shutPower = (BigDecimal) map.get("SHUT_POWER");
shutPower = shutPower == null ? new BigDecimal(0) : shutPower;
BigDecimal elec = (BigDecimal) map.get("ELEC");
elec = elec == null ? new BigDecimal(0) : elec;
//实际电量 > 关闭电量
if (elec.compareTo(shutPower) == 1) {
BusDevice busDevice = new BusDevice();
busDevice.setDevId((String) map.get("DEV_ID"));
BigDecimal runTm = (BigDecimal) map.get("RUN_TM");
runTm = runTm == null ? new BigDecimal(0) : runTm;
runTm = runTm.add(new BigDecimal(600));
busDevice.setRunTm(runTm);
saveList.add(busDevice);
}
});
//更新结果
return busDeviceRepository.batchUpdateRunTm(saveList);
}
} }
package org.rcisoft.business.device.task;
import org.rcisoft.business.device.assets.service.BusDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by JiChao on 2018/5/23.
* 更新所有设备的运行时长
*/
@Component
public class BusDeviceTask {
@Autowired
private BusDeviceService busDeviceServiceImpl;
/**
* 每10分钟查询更新一次
*/
@Scheduled(cron = "0 0/10 * * * ?")
public void runTime() {
busDeviceServiceImpl.addRunTime();
}
}
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
<result column="PRO_ID" jdbcType="VARCHAR" property="proId"/> <result column="PRO_ID" jdbcType="VARCHAR" property="proId"/>
<result column="SYS_ID" jdbcType="VARCHAR" property="sysId"/> <result column="SYS_ID" jdbcType="VARCHAR" property="sysId"/>
<result column="OWN_ID" jdbcType="VARCHAR" property="ownId"/> <result column="OWN_ID" jdbcType="VARCHAR" property="ownId"/>
<result column="SHUT_POWER" jdbcType="DECIMAL" property="shutPower"/>
<result column="RUN_TM" jdbcType="DECIMAL" property="runTm"/>
</resultMap> </resultMap>
<resultMap id="DeviceAssetStatistics" type="org.rcisoft.business.device.assets.vo.DeviceAssetStatisticVo"> <resultMap id="DeviceAssetStatistics" type="org.rcisoft.business.device.assets.vo.DeviceAssetStatisticVo">
<result column="DEV_NM" jdbcType="VARCHAR" property="devNm"></result> <result column="DEV_NM" jdbcType="VARCHAR" property="devNm"></result>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment