mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
添加水电表月度报表
This commit is contained in:
@@ -7,6 +7,8 @@ import cn.lihongjie.coal.department.entity.DepartmentEntity;
|
||||
import cn.lihongjie.coal.dictionary.entity.DictionaryEntity;
|
||||
import cn.lihongjie.coal.dictionary.entity.DictionaryItemEntity;
|
||||
import cn.lihongjie.coal.file.entity.FileEntity;
|
||||
import cn.lihongjie.coal.meter.entity.MeterEntity;
|
||||
import cn.lihongjie.coal.meterLog.entity.MeterLogEntity;
|
||||
import cn.lihongjie.coal.organization.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.permission.entity.PermissionEntity;
|
||||
import cn.lihongjie.coal.resource.entity.ResourceEntity;
|
||||
@@ -48,7 +50,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
DictionaryEntity e = new DictionaryEntity();
|
||||
var e = new DictionaryEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -58,7 +60,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
DictionaryItemEntity e = new DictionaryItemEntity();
|
||||
var e = new DictionaryItemEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -68,7 +70,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
SysLogEntity e = new SysLogEntity();
|
||||
var e = new SysLogEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -78,7 +80,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
OrganizationEntity e = new OrganizationEntity();
|
||||
var e = new OrganizationEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -88,7 +90,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
PermissionEntity e = new PermissionEntity();
|
||||
var e = new PermissionEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -98,7 +100,7 @@ public interface CommonMapper {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return null;
|
||||
}
|
||||
ResourceEntity e = new ResourceEntity();
|
||||
var e = new ResourceEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -109,7 +111,7 @@ public interface CommonMapper {
|
||||
|
||||
return null;
|
||||
}
|
||||
RoleEntity e = new RoleEntity();
|
||||
var e = new RoleEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -120,7 +122,7 @@ public interface CommonMapper {
|
||||
|
||||
return null;
|
||||
}
|
||||
ScriptEntity e = new ScriptEntity();
|
||||
var e = new ScriptEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -131,7 +133,7 @@ public interface CommonMapper {
|
||||
|
||||
return null;
|
||||
}
|
||||
SupplierEntity e = new SupplierEntity();
|
||||
var e = new SupplierEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -142,7 +144,7 @@ public interface CommonMapper {
|
||||
|
||||
return null;
|
||||
}
|
||||
CoalInfoEntity e = new CoalInfoEntity();
|
||||
var e = new CoalInfoEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
@@ -153,18 +155,39 @@ public interface CommonMapper {
|
||||
|
||||
return null;
|
||||
}
|
||||
FileEntity e = new FileEntity();
|
||||
var e = new FileEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
|
||||
default MeterEntity createMeter(String id) {
|
||||
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
var e = new MeterEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
|
||||
default MeterLogEntity createMeterLog(String id) {
|
||||
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
var e = new MeterLogEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
default DepartmentEntity createDepartment(String id) {
|
||||
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
DepartmentEntity e = new DepartmentEntity();
|
||||
var e = new DepartmentEntity();
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import cn.lihongjie.coal.meterLog.dto.UpdateMeterLogDto;
|
||||
import cn.lihongjie.coal.meterLog.entity.MeterLogEntity;
|
||||
import cn.lihongjie.coal.meterLog.mapper.MeterLogMapper;
|
||||
import cn.lihongjie.coal.meterLog.repository.MeterLogRepository;
|
||||
import cn.lihongjie.coal.meterMonthLog.service.MeterMonthLogService;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
@@ -81,6 +82,8 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
@Autowired MeterMonthLogService meterMonthLogService;
|
||||
|
||||
public void syncMeterLog(String meterId) {
|
||||
|
||||
Query nativeQuery =
|
||||
@@ -107,14 +110,16 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
|
||||
nativeQuery.setParameter("meterId", meterId);
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
stopWatch.start("syncMeterLog");
|
||||
int count = nativeQuery.executeUpdate();
|
||||
|
||||
stopWatch.stop();
|
||||
log.info(
|
||||
"syncMeterLog {} 更新了 {} 条数据, 耗时 {} ms",
|
||||
meterId,
|
||||
count,
|
||||
stopWatch.getLastTaskInfo().getTimeMillis());
|
||||
|
||||
stopWatch.start("syncMeterMonthLog");
|
||||
|
||||
meterMonthLogService.syncMeter(meterId);
|
||||
stopWatch.stop();
|
||||
|
||||
log.info("syncMeterLog {} 更新了 {} 条数据\n {}", meterId, count, stopWatch.prettyPrint());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.controller;
|
||||
|
||||
import cn.lihongjie.coal.annotation.OrgScope;
|
||||
import cn.lihongjie.coal.annotation.SysLog;
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.CreateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.MeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.UpdateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.service.MeterMonthLogService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/meterMonthLog")
|
||||
@SysLog(module = "水电表抄表记录月度统计")
|
||||
@Slf4j
|
||||
@OrgScope
|
||||
public class MeterMonthLogController {
|
||||
@Autowired private MeterMonthLogService service;
|
||||
|
||||
@PostMapping("/create")
|
||||
public MeterMonthLogDto create(@RequestBody CreateMeterMonthLogDto request) {
|
||||
return this.service.create(request);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public MeterMonthLogDto update(@RequestBody UpdateMeterMonthLogDto request) {
|
||||
return this.service.update(request);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public Object delete(@RequestBody IdRequest request) {
|
||||
this.service.delete(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/getById")
|
||||
public MeterMonthLogDto getById(@RequestBody IdRequest request) {
|
||||
return this.service.getById(request.getId());
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public Page<MeterMonthLogDto> list(@RequestBody CommonQuery request) {
|
||||
return this.service.list(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
@Data
|
||||
public class CreateMeterMonthLogDto extends OrgCommonDto {
|
||||
private String meter;
|
||||
|
||||
@Comment("抄表月份")
|
||||
private java.time.LocalDate time;
|
||||
|
||||
@Comment("抄表值")
|
||||
private java.lang.Double value;
|
||||
|
||||
@Comment("上次抄表值")
|
||||
private java.lang.Double previousValue;
|
||||
|
||||
@Comment("使用量")
|
||||
private java.lang.Double usage;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
import cn.lihongjie.coal.meter.dto.MeterDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
@Data
|
||||
public class MeterMonthLogDto extends OrgCommonDto {
|
||||
private MeterDto meter;
|
||||
|
||||
@Comment("抄表月份")
|
||||
private java.time.LocalDate time;
|
||||
|
||||
@Comment("抄表值")
|
||||
private java.lang.Double value;
|
||||
|
||||
@Comment("上次抄表值")
|
||||
private java.lang.Double previousValue;
|
||||
|
||||
@Comment("使用量")
|
||||
private java.lang.Double usage;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
@Data
|
||||
public class UpdateMeterMonthLogDto extends OrgCommonDto {
|
||||
private String meter;
|
||||
|
||||
@Comment("抄表月份")
|
||||
private java.time.LocalDate time;
|
||||
|
||||
@Comment("抄表值")
|
||||
private java.lang.Double value;
|
||||
|
||||
@Comment("上次抄表值")
|
||||
private java.lang.Double previousValue;
|
||||
|
||||
@Comment("使用量")
|
||||
private java.lang.Double usage;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.entity;
|
||||
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class MeterMonthLogEntity extends OrgCommonEntity {
|
||||
|
||||
@ManyToOne private cn.lihongjie.coal.meter.entity.MeterEntity meter;
|
||||
|
||||
@Comment("抄表月份")
|
||||
private java.time.LocalDate time;
|
||||
|
||||
@Comment("抄表值")
|
||||
private java.lang.Double value;
|
||||
|
||||
@Comment("上次抄表值")
|
||||
private java.lang.Double previousValue;
|
||||
|
||||
@Comment("使用量")
|
||||
private java.lang.Double usage;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.mapper;
|
||||
|
||||
import cn.lihongjie.coal.base.mapper.BaseMapper;
|
||||
import cn.lihongjie.coal.base.mapper.CommonMapper;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.CreateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.MeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.UpdateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.entity.MeterMonthLogEntity;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.control.DeepClone;
|
||||
|
||||
@Mapper(
|
||||
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
|
||||
uses = {CommonMapper.class},
|
||||
mappingControl = DeepClone.class)
|
||||
public interface MeterMonthLogMapper
|
||||
extends BaseMapper<
|
||||
MeterMonthLogEntity,
|
||||
MeterMonthLogDto,
|
||||
CreateMeterMonthLogDto,
|
||||
UpdateMeterMonthLogDto> {}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.repository;
|
||||
|
||||
import cn.lihongjie.coal.base.dao.BaseRepository;
|
||||
import cn.lihongjie.coal.meter.entity.MeterEntity;
|
||||
import cn.lihongjie.coal.meterMonthLog.entity.MeterMonthLogEntity;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface MeterMonthLogRepository extends BaseRepository<MeterMonthLogEntity> {
|
||||
long deleteByMeter(MeterEntity meter);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package cn.lihongjie.coal.meterMonthLog.service;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
import cn.lihongjie.coal.meter.entity.MeterEntity;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.CreateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.MeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.dto.UpdateMeterMonthLogDto;
|
||||
import cn.lihongjie.coal.meterMonthLog.entity.MeterMonthLogEntity;
|
||||
import cn.lihongjie.coal.meterMonthLog.mapper.MeterMonthLogMapper;
|
||||
import cn.lihongjie.coal.meterMonthLog.repository.MeterMonthLogRepository;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.Query;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class MeterMonthLogService
|
||||
extends BaseService<MeterMonthLogEntity, MeterMonthLogRepository> {
|
||||
@Autowired private MeterMonthLogRepository repository;
|
||||
|
||||
@Autowired private MeterMonthLogMapper mapper;
|
||||
|
||||
@Autowired private ConversionService conversionService;
|
||||
@PersistenceContext private EntityManager entityManager;
|
||||
|
||||
public MeterMonthLogDto create(CreateMeterMonthLogDto request) {
|
||||
MeterMonthLogEntity entity = mapper.toEntity(request);
|
||||
|
||||
this.repository.save(entity);
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public MeterMonthLogDto update(UpdateMeterMonthLogDto request) {
|
||||
MeterMonthLogEntity entity = this.repository.get(request.getId());
|
||||
this.mapper.updateEntity(entity, request);
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
public MeterMonthLogDto getById(String id) {
|
||||
MeterMonthLogEntity entity = repository.get(id);
|
||||
|
||||
return mapper.toDto(entity);
|
||||
}
|
||||
|
||||
public Page<MeterMonthLogDto> list(CommonQuery query) {
|
||||
Page<MeterMonthLogEntity> page =
|
||||
repository.findAll(
|
||||
query.specification(conversionService),
|
||||
PageRequest.of(
|
||||
query.getPageNo(),
|
||||
query.getPageSize(),
|
||||
Sort.by(query.getOrders())));
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
public void syncMeter(String meterId) {
|
||||
|
||||
MeterEntity meter = new MeterEntity();
|
||||
meter.setId(meterId);
|
||||
repository.deleteByMeter(meter);
|
||||
|
||||
Query nativeQuery =
|
||||
entityManager.createNativeQuery(
|
||||
"""
|
||||
|
||||
select DATE_TRUNC('month', time)::date as time,
|
||||
meter_id,
|
||||
sum(usage) as value
|
||||
from t_meter_log
|
||||
where meter_id = :meterId group by DATE_TRUNC('month', time)::date, meter_id
|
||||
""");
|
||||
|
||||
nativeQuery.setParameter("meterId", meterId);
|
||||
|
||||
nativeQuery
|
||||
.getResultList()
|
||||
.forEach(
|
||||
o -> {
|
||||
Object[] result = (Object[]) o;
|
||||
MeterMonthLogEntity entity = new MeterMonthLogEntity();
|
||||
entity.setMeter(meter);
|
||||
entity.setTime((java.time.LocalDate) result[0]);
|
||||
entity.setValue((Double) result[2]);
|
||||
repository.save(entity);
|
||||
});
|
||||
|
||||
Query nativeQuery2 =
|
||||
entityManager.createNativeQuery(
|
||||
"""
|
||||
|
||||
|
||||
update t_meter_month_log ta
|
||||
|
||||
|
||||
set previous_value = tb.previous_value,
|
||||
usage = ta.value - tb.previous_value
|
||||
|
||||
from (
|
||||
|
||||
select id, time, value, COALESCE(lag(value) over (order by time), value ) as previous_value from t_meter_month_log where meter_id = :meterId
|
||||
) tb
|
||||
|
||||
where tb.id = ta.id
|
||||
|
||||
|
||||
""");
|
||||
|
||||
nativeQuery2.setParameter("meterId", meterId);
|
||||
|
||||
nativeQuery2.executeUpdate();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user