mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
增加excel导入功能
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -64,4 +64,12 @@ public class MeterLogController {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/excelImport")
|
||||
public Object excelImport(@RequestBody IdRequest request) {
|
||||
this.service.excelImport(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -28,6 +28,9 @@ public class MeterLogEntity extends OrgCommonEntity {
|
||||
@ColumnDefault("'0'")
|
||||
private String archiveStatus = "0";
|
||||
|
||||
@Comment("数据来源")
|
||||
private String source;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<MeterLogEntity, MeterLogReposit
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public MeterLogDto update(UpdateMeterLogDto request) {
|
||||
MeterLogEntity entity = this.repository.get(request.getId());
|
||||
|
||||
if (this.repository.containArchived(request.getId())){
|
||||
throw new BizException("部分数据已归档,无法编辑或删除");
|
||||
}
|
||||
|
||||
this.mapper.updateEntity(entity, request);
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
|
||||
if (this.repository.containArchived(request)){
|
||||
throw new BizException("部分数据已归档,无法编辑或删除");
|
||||
}
|
||||
|
||||
this.repository.deleteAllById(request.getIds());
|
||||
|
||||
}
|
||||
@Autowired MeterService meterService;
|
||||
@Autowired FileService fileService;
|
||||
|
||||
public MeterLogDto getById(String id) {
|
||||
MeterLogEntity entity = repository.get(id);
|
||||
@@ -95,10 +95,113 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
|
||||
this.repository.unArchive(dto);
|
||||
}
|
||||
|
||||
|
||||
public MeterLogDto update(UpdateMeterLogDto request) {
|
||||
MeterLogEntity entity = this.repository.get(request.getId());
|
||||
|
||||
if (this.repository.containArchived(request.getId())) {
|
||||
throw new BizException("部分数据已归档,无法编辑或删除");
|
||||
}
|
||||
|
||||
this.mapper.updateEntity(entity, request);
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
|
||||
if (this.repository.containArchived(request)) {
|
||||
throw new BizException("部分数据已归档,无法编辑或删除");
|
||||
}
|
||||
|
||||
this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void excelImport(IdRequest fileId) {
|
||||
|
||||
@Cleanup InputStream inputStream = fileService.download(fileId.getId());
|
||||
|
||||
Function1<Object, MeterEntity> meterGetter =
|
||||
Function2.of(
|
||||
(x, y) -> {
|
||||
return meterService
|
||||
.findAll(
|
||||
new Specification<MeterEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<MeterEntity> 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<MeterLogEntity> logEntities =
|
||||
ExcelUtils.readFirstSheetToObjectList(
|
||||
inputStream,
|
||||
() -> {
|
||||
MeterLogEntity entity = new MeterLogEntity();
|
||||
entity.setSource("1");
|
||||
return entity;
|
||||
},
|
||||
new ExcelUtils.CellValueToObjectSetter<MeterLogEntity>() {
|
||||
@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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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": "政治面貌",
|
||||
|
||||
1
src/main/resources/db/migration/V50__meterLogType.sql
Normal file
1
src/main/resources/db/migration/V50__meterLogType.sql
Normal file
@@ -0,0 +1 @@
|
||||
update t_meter_log set source = '0' where source is null;
|
||||
Reference in New Issue
Block a user