From 6edf5c7efac79b8cd79dbc510677eae8ffb63032 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Wed, 21 Aug 2024 10:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0excel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lihongjie/coal/common/DictCode.java | 2 + .../controller/MeterLogController.java | 8 + .../coal/meterLog/dto/MeterLogDto.java | 4 + .../coal/meterLog/entity/MeterLogEntity.java | 3 + .../meterLog/service/MeterLogService.java | 153 +++++++++++++++--- src/main/resources/config/dictionary.json | 18 +++ .../db/migration/V50__meterLogType.sql | 1 + 7 files changed, 164 insertions(+), 25 deletions(-) create mode 100644 src/main/resources/db/migration/V50__meterLogType.sql diff --git a/src/main/java/cn/lihongjie/coal/common/DictCode.java b/src/main/java/cn/lihongjie/coal/common/DictCode.java index 4598d468..2f2359b5 100644 --- a/src/main/java/cn/lihongjie/coal/common/DictCode.java +++ b/src/main/java/cn/lihongjie/coal/common/DictCode.java @@ -8,6 +8,8 @@ public class DictCode { public static final String METER_TYPE = "meter.type"; + public static final String METER_LOG_SOURCE = "meter.log.source"; + public static final String EMP_POLITICALSTATUS = "emp.politicalStatus"; public static final String EMP_SALARY_BATCH_STATUS = "emp.salary.batch.status"; diff --git a/src/main/java/cn/lihongjie/coal/meterLog/controller/MeterLogController.java b/src/main/java/cn/lihongjie/coal/meterLog/controller/MeterLogController.java index f82e8a98..f0a59abc 100644 --- a/src/main/java/cn/lihongjie/coal/meterLog/controller/MeterLogController.java +++ b/src/main/java/cn/lihongjie/coal/meterLog/controller/MeterLogController.java @@ -64,4 +64,12 @@ public class MeterLogController { return true; } + + + @PostMapping("/excelImport") + public Object excelImport(@RequestBody IdRequest request) { + this.service.excelImport(request); + return true; + } + } diff --git a/src/main/java/cn/lihongjie/coal/meterLog/dto/MeterLogDto.java b/src/main/java/cn/lihongjie/coal/meterLog/dto/MeterLogDto.java index 9ab24093..428d9d14 100644 --- a/src/main/java/cn/lihongjie/coal/meterLog/dto/MeterLogDto.java +++ b/src/main/java/cn/lihongjie/coal/meterLog/dto/MeterLogDto.java @@ -27,6 +27,10 @@ public class MeterLogDto extends OrgCommonDto { @Comment("使用量") private java.lang.Double usage; + private String source; + + @DictTranslate(dictKey = DictCode.METER_LOG_SOURCE) + private String sourceName; @Comment("归档状态") private String archiveStatus = "0"; diff --git a/src/main/java/cn/lihongjie/coal/meterLog/entity/MeterLogEntity.java b/src/main/java/cn/lihongjie/coal/meterLog/entity/MeterLogEntity.java index 7e2668be..2d52c833 100644 --- a/src/main/java/cn/lihongjie/coal/meterLog/entity/MeterLogEntity.java +++ b/src/main/java/cn/lihongjie/coal/meterLog/entity/MeterLogEntity.java @@ -28,6 +28,9 @@ public class MeterLogEntity extends OrgCommonEntity { @ColumnDefault("'0'") private String archiveStatus = "0"; + @Comment("数据来源") + private String source; + diff --git a/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java b/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java index 5c2fd605..d931c9f6 100644 --- a/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java +++ b/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java @@ -1,9 +1,15 @@ package cn.lihongjie.coal.meterLog.service; +import cn.hutool.core.date.LocalDateTimeUtil; 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.common.Ctx; +import cn.lihongjie.coal.common.ExcelUtils; import cn.lihongjie.coal.exception.BizException; +import cn.lihongjie.coal.file.service.FileService; +import cn.lihongjie.coal.meter.entity.MeterEntity; +import cn.lihongjie.coal.meter.service.MeterService; import cn.lihongjie.coal.meterDayLog.service.MeterDayLogService; import cn.lihongjie.coal.meterLog.dto.CreateMeterLogDto; import cn.lihongjie.coal.meterLog.dto.MeterLogDto; @@ -13,9 +19,20 @@ import cn.lihongjie.coal.meterLog.mapper.MeterLogMapper; import cn.lihongjie.coal.meterLog.repository.MeterLogRepository; import cn.lihongjie.coal.meterMonthLog.service.MeterMonthLogService; +import com.google.common.primitives.Doubles; + +import io.vavr.Function1; +import io.vavr.Function2; + import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; +import lombok.Cleanup; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -23,9 +40,13 @@ 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.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.InputStream; +import java.util.List; + @Service @Slf4j @Transactional @@ -45,29 +66,8 @@ public class MeterLogService extends BaseService meterGetter = + Function2.of( + (x, y) -> { + return meterService + .findAll( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + + return criteriaBuilder.and( + criteriaBuilder.equal( + root.get( + "ybiotDeviceId"), + y), + criteriaBuilder.equal( + root.get( + "organizationId"), + x)); + } + }) + .stream() + .findFirst() + .orElseThrow(() -> new BizException("未找到对应的设备: " + y)); + }) + .memoized() + .apply(Ctx.currentUser().getOrganizationId()); + + List logEntities = + ExcelUtils.readFirstSheetToObjectList( + inputStream, + () -> { + MeterLogEntity entity = new MeterLogEntity(); + entity.setSource("1"); + return entity; + }, + new ExcelUtils.CellValueToObjectSetter() { + @Override + public void set(String header, MeterLogEntity object, Object value) { + + switch (header) { + + /** + * 数据行id 终端ID IMEI 终端无线信号质量 终端电池电压 采集时间 上传时间 基表ID 正累积流量 + * 负累积流量 净累积流量 瞬时流量 流速 模拟量压力 + */ + case "数据行id" -> {} + case "终端ID" -> { + object.setMeter(meterGetter.apply(value.toString())); + } + case "IMEI" -> {} + case "终端无线信号质量" -> {} + case "终端电池电压" -> {} + case "采集时间" -> { + object.setTime( + LocalDateTimeUtil.parse( + value + "", "yyyy-MM-dd HH:mm:ss")); + } + case "上传时间" -> {} + case "基表ID" -> {} + case "正累积流量" -> { + object.setValue(Doubles.tryParse(value + "")); + } + case "负累积流量" -> {} + case "净累积流量" -> {} + case "瞬时流量" -> {} + case "流速" -> {} + case "模拟量压力" -> {} + default -> {} + } + } + }); + + this.saveAll(logEntities.stream().filter(x -> x.getValue() != null).toList()); + } @Autowired MeterMonthLogService meterMonthLogService; @Autowired MeterDayLogService meterDayLogService; - } diff --git a/src/main/resources/config/dictionary.json b/src/main/resources/config/dictionary.json index 121160a1..1759bcdf 100644 --- a/src/main/resources/config/dictionary.json +++ b/src/main/resources/config/dictionary.json @@ -1675,6 +1675,24 @@ } ] }, + { + "code": "meter.log.source", + "name": "抄表数据来源", + "item": [ + { + "code": "0", + "name": "手动录入" + }, + { + "code": "1", + "name": "excel导入" + }, + { + "code": "2", + "name": "ybiot平台" + } + ] + }, { "code": "emp.politicalStatus", "name": "政治面貌", diff --git a/src/main/resources/db/migration/V50__meterLogType.sql b/src/main/resources/db/migration/V50__meterLogType.sql new file mode 100644 index 00000000..294b28de --- /dev/null +++ b/src/main/resources/db/migration/V50__meterLogType.sql @@ -0,0 +1 @@ +update t_meter_log set source = '0' where source is null;