电表bug修复

This commit is contained in:
guo 2023-11-03 11:17:47 +08:00
parent 904d6257d3
commit 1388a020d1
12 changed files with 97 additions and 78 deletions

View File

@ -13,7 +13,6 @@ import com.zhgd.xmgl.modules.ammeter.entity.*;
import com.zhgd.xmgl.modules.ammeter.mapper.AmmeterRecordDetailMapper;
import com.zhgd.xmgl.modules.ammeter.mapper.AmmeterSupplierRecordMapper;
import com.zhgd.xmgl.modules.ammeter.service.*;
import com.zhgd.xmgl.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
@ -172,7 +171,8 @@ public class AmmeterCallBackController {
.eq(AmmeterThreshold::getAmmeterNo, ammeterNo)
.eq(AmmeterThreshold::getMonthNum, monthNum));
if (threshold != null && threshold.getThresholdValue() != null) {
Double currentDegreeCurrentMonth = ammeterRecordDetailMapper.queryUseDegreeOnCurrentMonth(ammeterRecordDetail);
AmmeterMonthRecord currentMonth = ammeterMonthRecordService.getCurrentMonthAmmeterMonthRecord(new Ammeter().setAmmeterNo(ammeterNo).setProjectSn(projectSn));
Double currentDegreeCurrentMonth = currentMonth != null ? currentMonth.getUseDegree().doubleValue() : 0D;
if (currentDegreeCurrentMonth != null && !currentDegreeCurrentMonth.equals(0D) &&
threshold.getThresholdValue().compareTo(new BigDecimal(currentDegreeCurrentMonth)) <= 0) {
AmmeterAlarm meterAlarm = new AmmeterAlarm();

View File

@ -33,9 +33,6 @@ public class AmmeterTask {
@Autowired
private IAmmeterMonthRecordService ammeterMonthRecordService;
/**
* 统计前一月的用电
*/
@Scheduled(cron = "0 0 2 * * ?")
public void getMonthAmmeterTask() {
try {
@ -47,7 +44,7 @@ public class AmmeterTask {
}
}
} catch (Exception e) {
log.error("定时任务计算前一月的用电", e);
log.error("定时任务计算用电", e);
}
}
}

View File

@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.experimental.Accessors;
import org.jeecgframework.poi.excel.annotation.Excel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -19,10 +20,11 @@ import io.swagger.annotations.ApiModelProperty;
*/
@Data
@TableName("ammeter")
@ApiModel(value="Ammeter实体类",description="Ammeter")
@ApiModel(value = "Ammeter实体类", description = "Ammeter")
@Accessors(chain = true)
public class Ammeter implements Serializable {
private static final long serialVersionUID = 1L;
/**电表设备*/
@TableId(type = IdType.ASSIGN_ID)
@ApiModelProperty(value="电表设备")

View File

@ -24,5 +24,4 @@ public interface AmmeterMonthRecordMapper extends BaseMapper<AmmeterMonthRecord>
Map<String, Object> selectCurrentAmmeterMontRecord(Map<String, Object> map);
Map<String, Object> selectAmmeterMonthRecordByMeterNo(@Param("ammeterNo") String ammeterNo);
}

View File

@ -53,13 +53,20 @@ public interface AmmeterRecordDetailMapper extends BaseMapper<AmmeterRecordDetai
* @param ammeterRecordDetail
* @return
*/
@Deprecated
Double queryUseDegreeOnCurrentMonth(AmmeterRecordDetail ammeterRecordDetail);
List<AmmeterRecordDetail> queryNewestRecord(Map<String, Object> map);
/**
* 列表查询电表当前读数
*
* @param map
* @return
*/
List<AmmeterRecordDetail> queryNewestRecordList(Map<String, Object> map);
/**
* 查询用电量
* 查询用电量不是读数算法今日最大减去昨日最大
*
* @param map
* @return

View File

@ -36,39 +36,4 @@
where month_time=DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -1 MONTH), "%Y-%m") and ammeter_no=#{ammeterNo}) c ON a.ammeter_no=c.ammeter_no
WHERE a.ammeter_no=#{waterMeterNo} and a.project_sn=#{projectSn}
</select>
<select id="selectAmmeterMonthRecordByMeterNo" resultType="java.util.Map">
select t.*,
IFNULL(t.degree, IFNULL(t.startDegree, 0)) endDegree,
IFNULL(t.degree, IFNULL(t.startDegree, 0)) - IFNULL(t.startDegree, 0) useDegree
from (
SELECT a.ammeter_no ammeterNo,
a.project_sn projectSn,
DATE_FORMAT(NOW(), "%Y-%m") monthTime,
IFNULL(c.degree, (SELECT ifnull(degree,0) FROM `ammeter_record_detail` WHERE ammeter_no=#{ammeterNo} ORDER BY
add_time limit 1)) startDegree,
b.degree
FROM ammeter a
LEFT JOIN (
SELECT ammeter_no,
project_sn,
degree
FROM ammeter_record_detail
WHERE ammeter_no = #{ammeterNo}
AND DATE_FORMAT(add_time, "%Y-%m") = DATE_FORMAT(NOW(), "%Y-%m")
ORDER BY add_time DESC
LIMIT 1
) b ON a.ammeter_no = b.ammeter_no
LEFT JOIN (
SELECT ammeter_no,
project_sn,
degree
FROM ammeter_record_detail
WHERE ammeter_no = #{ammeterNo}
AND DATE_FORMAT(add_time, "%Y-%m") = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 MONTH), "%Y-%m")
ORDER BY add_time DESC
LIMIT 1
) c ON a.ammeter_no = c.ammeter_no
WHERE a.ammeter_no = #{ammeterNo}
)t
</select>
</mapper>

View File

@ -105,7 +105,7 @@
WHERE a.ammeter_no = #{ammeterNo}
</select>
<select id="queryNewestRecord" resultType="com.zhgd.xmgl.modules.ammeter.entity.AmmeterRecordDetail">
<select id="queryNewestRecordList" resultType="com.zhgd.xmgl.modules.ammeter.entity.AmmeterRecordDetail">
select *
from ammeter b
left join (select *
@ -113,6 +113,9 @@
where id in (select max(id) from ammeter_record_detail group by ammeter_no)) a
ON (a.ammeter_no = b.ammeter_no and a.project_sn = b.project_sn)
WHERE b.project_sn = #{projectSn}
<if test="ammeterNo != null and ammeterNo != ''">
and b.ammeter_no = #{ammeterNo}
</if>
group by b.ammeter_no
</select>
@ -124,16 +127,16 @@
WHERE id IN (
SELECT max(id)
FROM `ammeter_record_detail`
GROUP BY DATE_FORMAT(add_time, '%Y-%m-%d'))) t1
INNER JOIN(
GROUP BY ammeter_no, DATE_FORMAT(add_time, '%Y-%m-%d'))) t1
INNER JOIN (
SELECT *
FROM ammeter_record_detail
WHERE id IN (
SELECT MIN(id)
SELECT max(id)
FROM `ammeter_record_detail`
GROUP BY DATE_FORMAT(add_time, '%Y-%m-%d'))) t2
ON datediff(t1.add_time, t2.add_time) = -1 and t1.ammeter_no = t2.ammeter_no and
t1.project_sn = t2.project_sn
GROUP BY ammeter_no, DATE_FORMAT(add_time, '%Y-%m-%d'))) t2 ON datediff(t1.add_time, t2.add_time) = - 1
AND t1.ammeter_no = t2.ammeter_no
AND t1.project_sn = t2.project_sn
where 1 = 1
<if test="day != null and day != ''">
and DATE_FORMAT(t2.add_time, '%Y-%m-%d') = #{day}
@ -144,5 +147,8 @@
<if test="projectSn != null and projectSn != ''">
and t2.project_sn = #{projectSn}
</if>
<if test="addTime_begin != null and addTime_begin != ''">
and t2.add_time >= #{addTime_begin}
</if>
</select>
</mapper>

View File

@ -21,4 +21,6 @@ public interface IAmmeterMonthRecordService extends IService<AmmeterMonthRecord>
Map<String, Object> selectAmmeterStatisticsListByYear(Map<String, Object> map);
public void updateCurrentAmmeterMonthRecord(Ammeter ammeter);
AmmeterMonthRecord getCurrentMonthAmmeterMonthRecord(Ammeter ammeter);
}

View File

@ -1,5 +1,6 @@
package com.zhgd.xmgl.modules.ammeter.service.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.mybatis.EntityMap;
@ -45,14 +46,16 @@ public class AmmeterMonthRecordServiceImpl extends ServiceImpl<AmmeterMonthRecor
Map<String, Object> data = new HashMap<>();
QueryWrapper<Ammeter> queryWrapper = new QueryWrapper<>();
String ammeterNo = MapUtils.getString(map, "ammeterNo");
queryWrapper.lambda().eq(Ammeter::getProjectSn, MapUtils.getString(map, "projectSn"))
String projectSn = MapUtils.getString(map, "projectSn");
queryWrapper.lambda().eq(Ammeter::getProjectSn, projectSn)
.eq(Ammeter::getAmmeterNo, ammeterNo);
Ammeter ammeter = ammeterMapper.selectOne(queryWrapper);
data.put("ammeter", ammeter);
EntityMap count = ammeterMonthRecordMapper.getCurrentMonthMeterRecord(map);
AmmeterRecordDetail ad = new AmmeterRecordDetail();
ad.setAmmeterNo(ammeterNo);
Double use = ammeterRecordDetailMapper.queryUseDegreeOnCurrentMonth(ad);
AmmeterMonthRecord currentMonth = getCurrentMonthAmmeterMonthRecord(new Ammeter().setAmmeterNo(ammeterNo).setProjectSn(projectSn));
Double use = currentMonth != null ? currentMonth.getUseDegree().doubleValue() : 0D;
Double thresholdValue = MapUtils.getDouble(count, "thresholdValue");
count.put("month_user_water", use);
count.put("exceed_quota_num", use.compareTo(thresholdValue) > 0 ? use - thresholdValue : 0);
@ -144,26 +147,59 @@ public class AmmeterMonthRecordServiceImpl extends ServiceImpl<AmmeterMonthRecor
return listMap;
}
/**
* 更新最新月记录
*
* @param ammeter
*/
public void updateCurrentAmmeterMonthRecord(Ammeter ammeter) {
Map<String, Object> data = ammeterMonthRecordMapper.selectAmmeterMonthRecordByMeterNo(ammeter.getAmmeterNo());
AmmeterMonthRecord ammeterMonthRecord = new AmmeterMonthRecord();
ammeterMonthRecord.setMonthTime(MapUtils.getString(data, "monthTime"));
ammeterMonthRecord.setProjectSn(ammeter.getProjectSn());
ammeterMonthRecord.setAmmeterNo(ammeter.getAmmeterNo());
ammeterMonthRecord.setUseDegree(new BigDecimal(MapUtils.getString(data, "useDegree")));
ammeterMonthRecord.setStartDegree(new BigDecimal(MapUtils.getString(data, "startDegree")));
ammeterMonthRecord.setEndDegree(new BigDecimal(MapUtils.getString(data, "endDegree")));
QueryWrapper<AmmeterMonthRecord> qw = new QueryWrapper<>();
qw.lambda().eq(AmmeterMonthRecord::getProjectSn, ammeter.getProjectSn())
.eq(AmmeterMonthRecord::getMonthTime, MapUtils.getString(data, "monthTime"))
//2023-11的end取最大start取2023-10的enduse=end-start
HashMap<String, Object> m = new HashMap<>();
m.put("projectSn", ammeter.getProjectSn());
m.put("ammeterNo", ammeter.getAmmeterNo());
List<AmmeterRecordDetail> recordDetails = ammeterRecordDetailMapper.queryNewestRecordList(m);
AmmeterRecordDetail d = recordDetails.get(recordDetails.size() - 1);
if (d.getDegree() == null) {
return;
}
String monthTime = DateUtil.format(new Date(), "yyyy-MM");
String lastMonthTime = DateUtil.format(DateUtil.offsetMonth(new Date(), -1), "yyyy-MM");
QueryWrapper<AmmeterMonthRecord> lastQw = new QueryWrapper<>();
lastQw.lambda().eq(AmmeterMonthRecord::getProjectSn, ammeter.getProjectSn())
.eq(AmmeterMonthRecord::getMonthTime, lastMonthTime)
.eq(AmmeterMonthRecord::getAmmeterNo, ammeter.getAmmeterNo());
AmmeterMonthRecord oldAmmeterMonthRecord = ammeterMonthRecordMapper.selectOne(qw);
if (oldAmmeterMonthRecord != null) {
ammeterMonthRecord.setId(oldAmmeterMonthRecord.getId());
ammeterMonthRecordMapper.updateById(ammeterMonthRecord);
AmmeterMonthRecord lastRecord = ammeterMonthRecordMapper.selectOne(lastQw);
AmmeterMonthRecord old = getCurrentMonthAmmeterMonthRecord(ammeter);
if (old != null) {
old.setEndDegree(d.getDegree());
old.setUseDegree(old.getEndDegree().subtract(old.getStartDegree()));
ammeterMonthRecordMapper.updateById(old);
} else {
ammeterMonthRecord.setAddTime(new Date());
ammeterMonthRecordMapper.insert(ammeterMonthRecord);
old = new AmmeterMonthRecord();
old.setEndDegree(d.getDegree());
old.setMonthTime(monthTime);
old.setProjectSn(ammeter.getProjectSn());
old.setAmmeterNo(ammeter.getAmmeterNo());
old.setStartDegree(lastRecord != null ? lastRecord.getEndDegree() : BigDecimal.ZERO);
old.setAddTime(new Date());
old.setUseDegree(old.getEndDegree().subtract(old.getStartDegree()));
ammeterMonthRecordMapper.insert(old);
}
}
/**
* 获取当月电表
*
* @param ammeter
* @return
*/
public AmmeterMonthRecord getCurrentMonthAmmeterMonthRecord(Ammeter ammeter) {
String monthTime = DateUtil.format(new Date(), "yyyy-MM");
QueryWrapper<AmmeterMonthRecord> qw = new QueryWrapper<>();
qw.lambda().eq(AmmeterMonthRecord::getProjectSn, ammeter.getProjectSn())
.eq(AmmeterMonthRecord::getMonthTime, monthTime)
.eq(AmmeterMonthRecord::getAmmeterNo, ammeter.getAmmeterNo());
return ammeterMonthRecordMapper.selectOne(qw);
}
}

View File

@ -85,7 +85,7 @@ public class AmmeterRecordDetailServiceImpl extends ServiceImpl<AmmeterRecordDet
@Override
public List<AmmeterRecordDetail> queryNewestRecord(Map<String, Object> map) {
return baseMapper.queryNewestRecord(map);
return baseMapper.queryNewestRecordList(map);
}
@Override
@ -103,8 +103,8 @@ public class AmmeterRecordDetailServiceImpl extends ServiceImpl<AmmeterRecordDet
double lastMonth = Double.parseDouble(halfYears.get(halfYears.size() - 2).getY());
vo.setYesterdayElectricityConsumption(yesterday);
vo.setThisMonthElectricityConsumption(thisMonth);
vo.setMonthOnMonthElectricityConsumptionThisMonth(NumberUtils.div(thisMonth - lastMonth, lastMonth, 2) * 100);
vo.setTodayMonthOnMonthElectricityConsumption(NumberUtils.div(today - yesterday, yesterday, 2) * 100);
vo.setMonthOnMonthElectricityConsumptionThisMonth(NumberUtils.div((thisMonth - lastMonth) * 100, lastMonth, 2));
vo.setTodayMonthOnMonthElectricityConsumption(NumberUtils.div((today - yesterday) * 100, yesterday, 2));
AmmeterRecordDetail ard = baseMapper.selectOne(new LambdaQueryWrapper<AmmeterRecordDetail>().eq(AmmeterRecordDetail::getAmmeterNo, ammeterNo).orderByDesc(AmmeterRecordDetail::getId).last("limit 1"));
vo.setDegree(ard != null ? ard.getDegree().doubleValue() : 0.0);
return vo;

View File

@ -1,5 +1,6 @@
package com.zhgd.xmgl.modules.worker.entity.vo;
import com.zhgd.xmgl.util.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

View File

@ -548,14 +548,18 @@ public class DateUtils {
}
/**
* 偏移日期str
* 偏移日期str传入2023-11-02返回2023-11-02传入2023-11-02 00:00:00返回2023-11-02 00:00:00
*
* @param dayStr
* @param offset
* @return
*/
public static String offsetDayStr(String dayStr, int offset) {
return DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parseDate(dayStr), offset));
if (dayStr.length() == 10) {
return DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(dayStr), offset));
} else {
return DateUtil.format(DateUtil.offsetDay(DateUtil.parse(dayStr), offset), "yyyy-MM-dd HH:mm:ss");
}
}
/**
@ -593,6 +597,6 @@ public class DateUtils {
}
public static void main(String[] args) {
System.out.println(getDateStrList(93, "yyyy-MM"));
System.out.println("2023-11-02".length());
}
}