feat(plc): 添加 PLC 设备相关功能

- 新增 PLC 设备供应商、设备和数据相关实体、DTO 和服务
- 实现 PLC 数据采集和处理逻辑
- 添加数据模型字段类型字典- 优化智能摄像头白名单服务,使用当前组织 ID 进行查询
This commit is contained in:
2025-05-06 21:26:13 +08:00
parent 0cfceeb72a
commit 772a045002
50 changed files with 2072 additions and 3 deletions

View File

@@ -0,0 +1,190 @@
package cn.lihongjie.coal.dataCollector.listener;
import cn.lihongjie.coal.common.ReflectUtils;
import cn.lihongjie.coal.dataCollector.entity.DataCollectorEntity;
import cn.lihongjie.coal.dataCollector.service.DataCollectorService;
import cn.lihongjie.coal.dataCollectorLog.service.DataCollectorLogService;
import cn.lihongjie.coal.pdcDevice.service.PdcDeviceService;
import cn.lihongjie.coal.pdcDeviceRealTimeData.service.PdcDeviceRealTimeDataService;
import cn.lihongjie.coal.plcData.entity.PlcDataEntity;
import cn.lihongjie.coal.plcData.service.PlcDataService;
import cn.lihongjie.coal.plcDevice.entity.PlcDeviceEntity;
import cn.lihongjie.coal.plcDevice.service.PlcDeviceService;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import cn.lihongjie.coal.plcSchema.service.PlcSchemaService;
import cn.lihongjie.coal.plcSchemaDetail.entity.PlcSchemaDetailEntity;
import cn.lihongjie.coal.rabbitmq.RabbitMQConfiguration;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Component
@Slf4j
public class PlcListener {
@Autowired DataCollectorService dataCollectorService;
@Autowired DataCollectorLogService dataCollectorLogService;
@Autowired ObjectMapper objectMapper;
@Autowired PdcDeviceRealTimeDataService pdcDeviceRealTimeDataService;
@Autowired PdcDeviceService pdcDeviceService;
@Autowired PlcDeviceService plcDeviceService;
@Autowired PlcSchemaService plcSchemaService;
@Autowired private PlcDataService plcDataService;
@SneakyThrows
@RabbitListener(
bindings = {
@QueueBinding(
value =
@org.springframework.amqp.rabbit.annotation.Queue(
value = "plc.data",
durable = "true"),
exchange =
@Exchange(
value = RabbitMQConfiguration.SYS_EXCHANGE,
declare = Exchange.FALSE),
key = "plc.*")
})
@Transactional
public void handlePlcMessage(Message message) {
var body = new String(message.getBody(), StandardCharsets.UTF_8);
Map<String, Object> headers = message.getMessageProperties().getHeaders();
Object key = headers.get("appKey");
if (key == null) {
log.error("appKey is null");
return;
}
DataCollectorEntity dataCollector = dataCollectorService.findByAppKey(key.toString());
var sign =
new HmacUtils(HmacAlgorithms.HMAC_SHA_256, dataCollector.getAppSecret())
.hmacHex(body.getBytes(StandardCharsets.UTF_8));
if (!StringUtils.equals(sign, headers.get("sign").toString())) {
log.error("sign not match {} {}", sign, headers.get("sign"));
return;
}
JsonNode jsonNode = objectMapper.readTree(body);
String deviceCode = jsonNode.get("deviceCode").asText();
var devices =
plcDeviceService.findAll(
new Specification<PlcDeviceEntity>() {
@Override
public Predicate toPredicate(
Root<PlcDeviceEntity> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.and(
criteriaBuilder.equal(
root.get("dataCollector").get("id"),
dataCollector.getId()),
criteriaBuilder.equal(root.get("code"), deviceCode));
}
});
if (devices.isEmpty()) {
log.error(
"no device found for data collector: {} {}", dataCollector.getId(), deviceCode);
return;
}
PlcDeviceEntity device = devices.get(0);
PlcSchemaEntity schema = device.getSchema();
if (schema == null) {
log.error("no schema found for device: {}", device.getId());
return;
}
var schemaDetails = schema.getDetails();
if (schemaDetails.isEmpty()) {
log.error("no schema details found for schema: {}", schema.getId());
return;
}
PlcDataEntity plcData = new PlcDataEntity();
plcData.setOrganizationId(dataCollector.getOrganizationId());
plcData.setDevice(device);
plcData.setTime(
LocalDateTime.parse(
jsonNode.get("time").asText(),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
for (PlcSchemaDetailEntity schemaDetail : schemaDetails) {
JsonNode field = jsonNode.get(schemaDetail.getDataKey());
try {
if (field == null || field.isNull()) {
ReflectUtils.writeField(plcData, schemaDetail.getCode(), null, true);
} else {
switch (schemaDetail.getType()) {
case "int":
ReflectUtils.writeField(
plcData, schemaDetail.getCode(), field.asInt(), true);
break;
case "double":
ReflectUtils.writeField(
plcData, schemaDetail.getCode(), field.asDouble(), true);
break;
case "string":
ReflectUtils.writeField(
plcData, schemaDetail.getCode(), field.asText(), true);
break;
}
}
} catch (Exception e) {
log.warn("write field error {}", schemaDetail.getCode(), e);
}
}
plcDataService.save(plcData);
}
}

View File

@@ -0,0 +1,54 @@
package cn.lihongjie.coal.plcData.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.plcData.dto.CreatePlcDataDto;
import cn.lihongjie.coal.plcData.dto.PlcDataDto;
import cn.lihongjie.coal.plcData.dto.UpdatePlcDataDto;
import cn.lihongjie.coal.plcData.service.PlcDataService;
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("/plcData")
@SysLog(module = "")
@Slf4j
@OrgScope
public class PlcDataController {
@Autowired private PlcDataService service;
@PostMapping("/create")
public PlcDataDto create(@RequestBody CreatePlcDataDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public PlcDataDto update(@RequestBody UpdatePlcDataDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public PlcDataDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<PlcDataDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcData.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreatePlcDataDto extends OrgCommonDto {}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcData.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class PlcDataDto extends OrgCommonDto {}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcData.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdatePlcDataDto extends OrgCommonDto {}

View File

@@ -0,0 +1,242 @@
package cn.lihongjie.coal.plcData.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.plcDevice.entity.PlcDeviceEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Entity
@Table(
indexes =
@jakarta.persistence.Index(
name = "idx_plcData_org_id",
columnList = "organization_id"))
public class PlcDataEntity extends OrgCommonEntity {
@ManyToOne
private PlcDeviceEntity device;
private LocalDateTime time;
private String str1;
private String str2;
private String str3;
private String str4;
private String str5;
private String str6;
private String str7;
private String str8;
private String str9;
private String str10;
private String str11;
private String str12;
private String str13;
private String str14;
private String str15;
private String str16;
private String str17;
private String str18;
private String str19;
private String str20;
private String str21;
private String str22;
private String str23;
private String str24;
private String str25;
private String str26;
private String str27;
private String str28;
private String str29;
private String str30;
private String str31;
private String str32;
private String str33;
private String str34;
private String str35;
private String str36;
private String str37;
private String str38;
private String str39;
private String str40;
private String str41;
private String str42;
private String str43;
private String str44;
private String str45;
private String str46;
private String str47;
private String str48;
private String str49;
private String str50;
private Integer int1;
private Integer int2;
private Integer int3;
private Integer int4;
private Integer int5;
private Integer int6;
private Integer int7;
private Integer int8;
private Integer int9;
private Integer int10;
private Integer int11;
private Integer int12;
private Integer int13;
private Integer int14;
private Integer int15;
private Integer int16;
private Integer int17;
private Integer int18;
private Integer int19;
private Integer int20;
private Integer int21;
private Integer int22;
private Integer int23;
private Integer int24;
private Integer int25;
private Integer int26;
private Integer int27;
private Integer int28;
private Integer int29;
private Integer int30;
private Integer int31;
private Integer int32;
private Integer int33;
private Integer int34;
private Integer int35;
private Integer int36;
private Integer int37;
private Integer int38;
private Integer int39;
private Integer int40;
private Integer int41;
private Integer int42;
private Integer int43;
private Integer int44;
private Integer int45;
private Integer int46;
private Integer int47;
private Integer int48;
private Integer int49;
private Integer int50;
private Double double1;
private Double double2;
private Double double3;
private Double double4;
private Double double5;
private Double double6;
private Double double7;
private Double double8;
private Double double9;
private Double double10;
private Double double11;
private Double double12;
private Double double13;
private Double double14;
private Double double15;
private Double double16;
private Double double17;
private Double double18;
private Double double19;
private Double double20;
private Double double21;
private Double double22;
private Double double23;
private Double double24;
private Double double25;
private Double double26;
private Double double27;
private Double double28;
private Double double29;
private Double double30;
private Double double31;
private Double double32;
private Double double33;
private Double double34;
private Double double35;
private Double double36;
private Double double37;
private Double double38;
private Double double39;
private Double double40;
private Double double41;
private Double double42;
private Double double43;
private Double double44;
private Double double45;
private Double double46;
private Double double47;
private Double double48;
private Double double49;
private Double double50;
private Boolean bool1;
private Boolean bool2;
private Boolean bool3;
private Boolean bool4;
private Boolean bool5;
private Boolean bool6;
private Boolean bool7;
private Boolean bool8;
private Boolean bool9;
private Boolean bool10;
private Boolean bool11;
private Boolean bool12;
private Boolean bool13;
private Boolean bool14;
private Boolean bool15;
private Boolean bool16;
private Boolean bool17;
private Boolean bool18;
private Boolean bool19;
private Boolean bool20;
private Boolean bool21;
private Boolean bool22;
private Boolean bool23;
private Boolean bool24;
private Boolean bool25;
private Boolean bool26;
private Boolean bool27;
private Boolean bool28;
private Boolean bool29;
private Boolean bool30;
private Boolean bool31;
private Boolean bool32;
private Boolean bool33;
private Boolean bool34;
private Boolean bool35;
private Boolean bool36;
private Boolean bool37;
private Boolean bool38;
private Boolean bool39;
private Boolean bool40;
private Boolean bool41;
private Boolean bool42;
private Boolean bool43;
private Boolean bool44;
private Boolean bool45;
private Boolean bool46;
private Boolean bool47;
private Boolean bool48;
private Boolean bool49;
private Boolean bool50;
}

View File

@@ -0,0 +1,19 @@
package cn.lihongjie.coal.plcData.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.plcData.dto.CreatePlcDataDto;
import cn.lihongjie.coal.plcData.dto.PlcDataDto;
import cn.lihongjie.coal.plcData.dto.UpdatePlcDataDto;
import cn.lihongjie.coal.plcData.entity.PlcDataEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface PlcDataMapper
extends BaseMapper<PlcDataEntity, PlcDataDto, CreatePlcDataDto, UpdatePlcDataDto> {}

View File

@@ -0,0 +1,15 @@
package cn.lihongjie.coal.plcData.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.plcData.entity.PlcDataEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PlcDataRepository extends BaseRepository<PlcDataEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -0,0 +1,80 @@
package cn.lihongjie.coal.plcData.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.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.plcData.dto.CreatePlcDataDto;
import cn.lihongjie.coal.plcData.dto.PlcDataDto;
import cn.lihongjie.coal.plcData.dto.UpdatePlcDataDto;
import cn.lihongjie.coal.plcData.entity.PlcDataEntity;
import cn.lihongjie.coal.plcData.mapper.PlcDataMapper;
import cn.lihongjie.coal.plcData.repository.PlcDataRepository;
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;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional
public class PlcDataService extends BaseService<PlcDataEntity, PlcDataRepository> {
@Autowired private PlcDataRepository repository;
@Autowired private PlcDataMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
public PlcDataDto create(CreatePlcDataDto request) {
PlcDataEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public PlcDataDto update(UpdatePlcDataDto request) {
PlcDataEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public PlcDataDto getById(String id) {
PlcDataEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<PlcDataDto> list(CommonQuery query) {
Page<PlcDataEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}

View File

@@ -0,0 +1,54 @@
package cn.lihongjie.coal.plcDevice.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.plcDevice.dto.CreatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.PlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.UpdatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.service.PlcDeviceService;
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("/plcDevice")
@SysLog(module = "")
@Slf4j
@OrgScope
public class PlcDeviceController {
@Autowired private PlcDeviceService service;
@PostMapping("/create")
public PlcDeviceDto create(@RequestBody CreatePlcDeviceDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public PlcDeviceDto update(@RequestBody UpdatePlcDeviceDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public PlcDeviceDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<PlcDeviceDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,29 @@
package cn.lihongjie.coal.plcDevice.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import jakarta.persistence.ManyToOne;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class CreatePlcDeviceDto extends OrgCommonDto {
@ManyToOne
String supplier;
@ManyToOne
String schema;
@ManyToOne
private String dataCollector;
@Comment("品牌")
private String brand;
@Comment("型号")
private String model;
@Comment("安装位置")
private String installationPosition;
}

View File

@@ -0,0 +1,32 @@
package cn.lihongjie.coal.plcDevice.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.base.dto.SimpleDto;
import cn.lihongjie.coal.dataCollector.dto.DataCollectorDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.PlcDeviceSupplierDto;
import jakarta.persistence.ManyToOne;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class PlcDeviceDto extends OrgCommonDto {
@ManyToOne
PlcDeviceSupplierDto supplier;
@ManyToOne
SimpleDto schema;
@ManyToOne
private DataCollectorDto dataCollector;
@Comment("品牌")
private String brand;
@Comment("型号")
private String model;
@Comment("安装位置")
private String installationPosition;
}

View File

@@ -0,0 +1,30 @@
package cn.lihongjie.coal.plcDevice.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import jakarta.persistence.ManyToOne;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class UpdatePlcDeviceDto extends OrgCommonDto {
@ManyToOne
String supplier;
@ManyToOne
String schema;
@ManyToOne
private String dataCollector;
@Comment("品牌")
private String brand;
@Comment("型号")
private String model;
@Comment("安装位置")
private String installationPosition;
}

View File

@@ -0,0 +1,43 @@
package cn.lihongjie.coal.plcDevice.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.dataCollector.entity.DataCollectorEntity;
import cn.lihongjie.coal.plcDeviceSupplier.entity.PlcDeviceSupplierEntity;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
@Entity
@Table(
indexes =
@jakarta.persistence.Index(
name = "idx_plcDevice_org_id",
columnList = "organization_id"))
public class PlcDeviceEntity extends OrgCommonEntity {
@ManyToOne
PlcDeviceSupplierEntity supplier;
@ManyToOne
PlcSchemaEntity schema;
@ManyToOne
private DataCollectorEntity dataCollector;
@Comment("品牌")
private String brand;
@Comment("型号")
private String model;
@Comment("安装位置")
private String installationPosition;
}

View File

@@ -0,0 +1,19 @@
package cn.lihongjie.coal.plcDevice.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.plcDevice.dto.CreatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.PlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.UpdatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.entity.PlcDeviceEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface PlcDeviceMapper
extends BaseMapper<PlcDeviceEntity, PlcDeviceDto, CreatePlcDeviceDto, UpdatePlcDeviceDto> {}

View File

@@ -0,0 +1,15 @@
package cn.lihongjie.coal.plcDevice.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.plcDevice.entity.PlcDeviceEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PlcDeviceRepository extends BaseRepository<PlcDeviceEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -0,0 +1,80 @@
package cn.lihongjie.coal.plcDevice.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.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.plcDevice.dto.CreatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.PlcDeviceDto;
import cn.lihongjie.coal.plcDevice.dto.UpdatePlcDeviceDto;
import cn.lihongjie.coal.plcDevice.entity.PlcDeviceEntity;
import cn.lihongjie.coal.plcDevice.mapper.PlcDeviceMapper;
import cn.lihongjie.coal.plcDevice.repository.PlcDeviceRepository;
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;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional
public class PlcDeviceService extends BaseService<PlcDeviceEntity, PlcDeviceRepository> {
@Autowired private PlcDeviceRepository repository;
@Autowired private PlcDeviceMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
public PlcDeviceDto create(CreatePlcDeviceDto request) {
PlcDeviceEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public PlcDeviceDto update(UpdatePlcDeviceDto request) {
PlcDeviceEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public PlcDeviceDto getById(String id) {
PlcDeviceEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<PlcDeviceDto> list(CommonQuery query) {
Page<PlcDeviceEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}

View File

@@ -0,0 +1,54 @@
package cn.lihongjie.coal.plcDeviceSupplier.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.plcDeviceSupplier.dto.CreatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.PlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.UpdatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.service.PlcDeviceSupplierService;
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("/plcDeviceSupplier")
@SysLog(module = "")
@Slf4j
@OrgScope
public class PlcDeviceSupplierController {
@Autowired private PlcDeviceSupplierService service;
@PostMapping("/create")
public PlcDeviceSupplierDto create(@RequestBody CreatePlcDeviceSupplierDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public PlcDeviceSupplierDto update(@RequestBody UpdatePlcDeviceSupplierDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public PlcDeviceSupplierDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<PlcDeviceSupplierDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcDeviceSupplier.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreatePlcDeviceSupplierDto extends OrgCommonDto {}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcDeviceSupplier.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class PlcDeviceSupplierDto extends OrgCommonDto {}

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.plcDeviceSupplier.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdatePlcDeviceSupplierDto extends OrgCommonDto {}

View File

@@ -0,0 +1,17 @@
package cn.lihongjie.coal.plcDeviceSupplier.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(
indexes =
@jakarta.persistence.Index(
name = "idx_plcDeviceSupplier_org_id",
columnList = "organization_id"))
public class PlcDeviceSupplierEntity extends OrgCommonEntity {}

View File

@@ -0,0 +1,23 @@
package cn.lihongjie.coal.plcDeviceSupplier.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.plcDeviceSupplier.dto.CreatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.PlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.UpdatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.entity.PlcDeviceSupplierEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface PlcDeviceSupplierMapper
extends BaseMapper<
PlcDeviceSupplierEntity,
PlcDeviceSupplierDto,
CreatePlcDeviceSupplierDto,
UpdatePlcDeviceSupplierDto> {}

View File

@@ -0,0 +1,15 @@
package cn.lihongjie.coal.plcDeviceSupplier.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.plcDeviceSupplier.entity.PlcDeviceSupplierEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PlcDeviceSupplierRepository extends BaseRepository<PlcDeviceSupplierEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -0,0 +1,81 @@
package cn.lihongjie.coal.plcDeviceSupplier.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.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.plcDeviceSupplier.dto.CreatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.PlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.dto.UpdatePlcDeviceSupplierDto;
import cn.lihongjie.coal.plcDeviceSupplier.entity.PlcDeviceSupplierEntity;
import cn.lihongjie.coal.plcDeviceSupplier.mapper.PlcDeviceSupplierMapper;
import cn.lihongjie.coal.plcDeviceSupplier.repository.PlcDeviceSupplierRepository;
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;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional
public class PlcDeviceSupplierService
extends BaseService<PlcDeviceSupplierEntity, PlcDeviceSupplierRepository> {
@Autowired private PlcDeviceSupplierRepository repository;
@Autowired private PlcDeviceSupplierMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
public PlcDeviceSupplierDto create(CreatePlcDeviceSupplierDto request) {
PlcDeviceSupplierEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public PlcDeviceSupplierDto update(UpdatePlcDeviceSupplierDto request) {
PlcDeviceSupplierEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public PlcDeviceSupplierDto getById(String id) {
PlcDeviceSupplierEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<PlcDeviceSupplierDto> list(CommonQuery query) {
Page<PlcDeviceSupplierEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}

View File

@@ -0,0 +1,72 @@
package cn.lihongjie.coal.plcSchema.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.plcSchema.dto.CreatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.InitFromJsonDto;
import cn.lihongjie.coal.plcSchema.dto.PlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.UpdatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.service.PlcSchemaService;
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("/plcSchema")
@SysLog(module = "")
@Slf4j
@OrgScope
public class PlcSchemaController {
@Autowired private PlcSchemaService service;
@PostMapping("/create")
public PlcSchemaDto create(@RequestBody CreatePlcSchemaDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public PlcSchemaDto update(@RequestBody UpdatePlcSchemaDto request) {
return this.service.update(request);
}
@PostMapping("/resetSchema")
public Boolean resetSchema(@RequestBody IdRequest request) {
this.service.resetSchema(request);
return true;
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public PlcSchemaDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<PlcSchemaDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
@PostMapping("/initFromJson")
public Boolean initFromJson(@RequestBody InitFromJsonDto request) {
this.service.initFromJson(request);
return true;
}
}

View File

@@ -0,0 +1,14 @@
package cn.lihongjie.coal.plcSchema.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreatePlcSchemaDto extends OrgCommonDto {
}

View File

@@ -0,0 +1,15 @@
package cn.lihongjie.coal.plcSchema.dto;
import lombok.Data;
import java.util.*;
@Data
public class InitFromJsonDto {
private String id;
private String json;
private Boolean deleteOld = true;
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.plcSchema.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import java.util.*;
@Data
public class PlcSchemaDto extends OrgCommonDto {
}

View File

@@ -0,0 +1,13 @@
package cn.lihongjie.coal.plcSchema.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdatePlcSchemaDto extends OrgCommonDto {
}

View File

@@ -0,0 +1,33 @@
package cn.lihongjie.coal.plcSchema.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.plcSchemaDetail.entity.PlcSchemaDetailEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Data;
import java.util.List;
@Data
@Entity
@Table(
indexes =
@jakarta.persistence.Index(
name = "idx_plcSchema_org_id",
columnList = "organization_id"))
public class PlcSchemaEntity extends OrgCommonEntity {
@OneToMany(mappedBy = "plcSchema")
private List<PlcSchemaDetailEntity> details;
public void setDetails(List<PlcSchemaDetailEntity> details) {
this.details = details;
details.forEach(detail -> detail.setPlcSchema(this));
}
}

View File

@@ -0,0 +1,19 @@
package cn.lihongjie.coal.plcSchema.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.plcSchema.dto.CreatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.PlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.UpdatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface PlcSchemaMapper
extends BaseMapper<PlcSchemaEntity, PlcSchemaDto, CreatePlcSchemaDto, UpdatePlcSchemaDto> {}

View File

@@ -0,0 +1,15 @@
package cn.lihongjie.coal.plcSchema.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PlcSchemaRepository extends BaseRepository<PlcSchemaEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -0,0 +1,170 @@
package cn.lihongjie.coal.plcSchema.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.common.Ctx;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.plcSchema.dto.CreatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.InitFromJsonDto;
import cn.lihongjie.coal.plcSchema.dto.PlcSchemaDto;
import cn.lihongjie.coal.plcSchema.dto.UpdatePlcSchemaDto;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import cn.lihongjie.coal.plcSchema.mapper.PlcSchemaMapper;
import cn.lihongjie.coal.plcSchema.repository.PlcSchemaRepository;
import cn.lihongjie.coal.plcSchemaDetail.service.PlcSchemaDetailService;
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;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional
public class PlcSchemaService extends BaseService<PlcSchemaEntity, PlcSchemaRepository> {
@Autowired private PlcSchemaRepository repository;
@Autowired private PlcSchemaMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
@Autowired
private PlcSchemaDetailService plcSchemaDetailService;
public PlcSchemaDto create(CreatePlcSchemaDto request) {
PlcSchemaEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public PlcSchemaDto update(UpdatePlcSchemaDto request) {
PlcSchemaEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void resetSchema(IdRequest id){
PlcSchemaEntity entity = this.repository.get(id.getId());
this.repository.save(entity);
}
// private List<SchemaDetailEntity> getDefaultSchema() {
//
//
// List<SchemaDetailEntity> strList = IntStream.rangeClosed(1, 50).mapToObj(x -> {
// SchemaDetailEntity detail = new SchemaDetailEntity();
// detail.setName("");
// detail.setCode("str" + x);
// detail.setType(String.class.getSimpleName());
// detail.setSortKey(x);
// detail.setDisplayGroup("");
//
// return detail;
// }).toList();
//
//
//
// List<SchemaDetailEntity> intList = IntStream.rangeClosed(1, 50).mapToObj(x -> {
// SchemaDetailEntity detail = new SchemaDetailEntity();
// detail.setName("");
// detail.setCode("int" + x);
// detail.setType(Integer.class.getSimpleName());
// detail.setSortKey(x);
// detail.setDisplayGroup("");
//
// return detail;
// }).toList();
//
//
// List<SchemaDetailEntity> floatList = IntStream.rangeClosed(1, 50).mapToObj(x -> {
// SchemaDetailEntity detail = new SchemaDetailEntity();
// detail.setName("");
// detail.setCode("double" + x);
// detail.setType(Double.class.getSimpleName());
// detail.setSortKey(x);
// detail.setDisplayGroup("");
//
// return detail;
// }).toList();
//
//
//
// List<SchemaDetailEntity> boolList = IntStream.rangeClosed(1, 50).mapToObj(x -> {
// SchemaDetailEntity detail = new SchemaDetailEntity();
// detail.setName("");
// detail.setCode("bool" + x);
// detail.setType(Boolean.class.getSimpleName());
// detail.setSortKey(x);
// detail.setDisplayGroup("");
//
// return detail;
// }).toList();
//
//
// List<SchemaDetailEntity> all = List.of(strList, intList, floatList, boolList).stream().flatMap(List::stream).toList();
//
// return all;
// }
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public PlcSchemaDto getById(String id) {
PlcSchemaEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<PlcSchemaDto> list(CommonQuery query) {
Page<PlcSchemaEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
public void initFromJson(InitFromJsonDto request){
if(request.getDeleteOld()){
plcSchemaDetailService.deleteBySchemaId(request.getId());
}
plcSchemaDetailService.initFromJson(request.getJson(), request.getId(), Ctx.currentOrganizationId());
}
}

View File

@@ -0,0 +1,54 @@
package cn.lihongjie.coal.plcSchemaDetail.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.plcSchemaDetail.dto.CreatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.PlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.UpdatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.service.PlcSchemaDetailService;
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("/plcSchemaDetail")
@SysLog(module = "")
@Slf4j
@OrgScope
public class PlcSchemaDetailController {
@Autowired private PlcSchemaDetailService service;
@PostMapping("/create")
public PlcSchemaDetailDto create(@RequestBody CreatePlcSchemaDetailDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public PlcSchemaDetailDto update(@RequestBody UpdatePlcSchemaDetailDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public PlcSchemaDetailDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<PlcSchemaDetailDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,26 @@
package cn.lihongjie.coal.plcSchemaDetail.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class CreatePlcSchemaDetailDto extends OrgCommonDto {
private String plcSchema;
@Comment("数据标识")
private String dataKey;
@Comment("数据类型")
private String type;
@Comment("父级表头")
private String displayGroup;
}

View File

@@ -0,0 +1,27 @@
package cn.lihongjie.coal.plcSchemaDetail.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.base.dto.SimpleDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class PlcSchemaDetailDto extends OrgCommonDto {
private SimpleDto plcSchema;
@Comment("数据标识")
private String dataKey;
@Comment("数据类型")
private String type;
@Comment("父级表头")
private String displayGroup;
}

View File

@@ -0,0 +1,26 @@
package cn.lihongjie.coal.plcSchemaDetail.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
public class UpdatePlcSchemaDetailDto extends OrgCommonDto {
private String plcSchema;
@Comment("数据标识")
private String dataKey;
@Comment("数据类型")
private String type;
@Comment("父级表头")
private String displayGroup;
}

View File

@@ -0,0 +1,38 @@
package cn.lihongjie.coal.plcSchemaDetail.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.plcSchema.entity.PlcSchemaEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import org.hibernate.annotations.Comment;
@Data
@Entity
@Table(
indexes =
@jakarta.persistence.Index(
name = "idx_plcSchemaDetail_org_id",
columnList = "organization_id"))
public class PlcSchemaDetailEntity extends OrgCommonEntity {
@ManyToOne
private PlcSchemaEntity plcSchema;
@Comment("数据标识")
private String dataKey;
@Comment("数据类型")
private String type;
@Comment("父级表头")
private String displayGroup;
}

View File

@@ -0,0 +1,23 @@
package cn.lihongjie.coal.plcSchemaDetail.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.plcSchemaDetail.dto.CreatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.PlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.UpdatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.entity.PlcSchemaDetailEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface PlcSchemaDetailMapper
extends BaseMapper<
PlcSchemaDetailEntity,
PlcSchemaDetailDto,
CreatePlcSchemaDetailDto,
UpdatePlcSchemaDetailDto> {}

View File

@@ -0,0 +1,22 @@
package cn.lihongjie.coal.plcSchemaDetail.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.plcSchemaDetail.entity.PlcSchemaDetailEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PlcSchemaDetailRepository extends BaseRepository<PlcSchemaDetailEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
List<PlcSchemaDetailEntity> findAllByCodeLikeAndOrganizationId(String s, String organizationId);
@Modifying
@Query("delete from PlcSchemaDetailEntity where plcSchema.id = ?1")
void deleteBySchemaId(String id);
}

View File

@@ -0,0 +1,213 @@
package cn.lihongjie.coal.plcSchemaDetail.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.common.Ctx;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.plcSchema.service.PlcSchemaService;
import cn.lihongjie.coal.plcSchemaDetail.dto.CreatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.PlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.dto.UpdatePlcSchemaDetailDto;
import cn.lihongjie.coal.plcSchemaDetail.entity.PlcSchemaDetailEntity;
import cn.lihongjie.coal.plcSchemaDetail.mapper.PlcSchemaDetailMapper;
import cn.lihongjie.coal.plcSchemaDetail.repository.PlcSchemaDetailRepository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
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;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
@Transactional
public class PlcSchemaDetailService
extends BaseService<PlcSchemaDetailEntity, PlcSchemaDetailRepository> {
@Autowired private PlcSchemaDetailRepository repository;
@Autowired private PlcSchemaDetailMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
@Autowired private ObjectMapper objectMapper;
@Autowired
private PlcSchemaService plcSchemaService;
public PlcSchemaDetailDto create(CreatePlcSchemaDetailDto request) {
PlcSchemaDetailEntity entity = mapper.toEntity(request);
entity.setCode(getNextCode(entity.getType(), Ctx.currentOrganizationId()));
this.repository.save(entity);
return getById(entity.getId());
}
private String getNextCode(String type, String organizationId) {
List<PlcSchemaDetailEntity> all =
this.repository.findAllByCodeLikeAndOrganizationId(type + "%", organizationId);
if (all.isEmpty()) {
return type + "1";
} else {
Integer max =
all.stream()
.map(x -> Integer.parseInt(x.getCode().replace(type, "")))
.max(Integer::compare)
.get();
if (max == 50) {
for (int i = 1; i <= 50; i++) {
int finalI = i;
if (all.stream().noneMatch(x -> x.getCode().equals(type + finalI))) {
return type + i;
}
}
throw new BizException("数据已满");
} else {
return type + (max + 1);
}
}
}
public PlcSchemaDetailDto update(UpdatePlcSchemaDetailDto request) {
PlcSchemaDetailEntity entity = this.repository.get(request.getId());
if (ObjectUtils.notEqual(entity.getType(), request.getType())) {
throw new BizException("数据类型不允许修改");
}
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public PlcSchemaDetailDto getById(String id) {
PlcSchemaDetailEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<PlcSchemaDetailDto> list(CommonQuery query) {
Page<PlcSchemaDetailEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
public void deleteBySchemaId(String id) {
repository.deleteBySchemaId(id);
}
@SneakyThrows
public void initFromJson(String json, String id, String organizationId) {
JsonNode jsonNode = objectMapper.readTree(json);
List<PlcSchemaDetailEntity> entities = new java.util.ArrayList<>();
if (jsonNode.isObject()) {
ObjectNode node = (ObjectNode) jsonNode;
int cnt = 0;
while (node.fields().hasNext()) {
Map.Entry<String, JsonNode> next = node.fields().next();
PlcSchemaDetailEntity entity = new PlcSchemaDetailEntity();
entity.setPlcSchema(plcSchemaService.get(id));
entity.setName(next.getKey().replace(".Value", ""));
entity.setStatus(1);
entity.setDataKey(next.getKey());
entity.setDisplayGroup("");
entity.setSortKey((++cnt) * 10);
entity.setOrganizationId(organizationId);
entities.add(entity);
if (next.getValue().isTextual()) {
entity.setType("string");
entity.setCode(getNextCode("string", organizationId));
} else if (next.getValue().isFloatingPointNumber()) {
entity.setType("double");
entity.setCode(getNextCode("double", organizationId));
}else if (next.getValue().isBoolean()){
entity.setType("bool");
entity.setCode(getNextCode("bool", organizationId));
}else if (next.getValue().isIntegralNumber()){
entity.setType("int");
entity.setCode(getNextCode("int", organizationId));
}else {
throw new BizException("不支持的数据类型 " + next.getValue().getNodeType());
}
}
}
repository.saveAll(entities);
}
}

View File

@@ -3,6 +3,7 @@ package cn.lihongjie.coal.smartCamWhiteList.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.common.Ctx;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.smartCamWhiteList.dto.CreateSmartCamWhiteListDto;
@@ -38,9 +39,7 @@ public class SmartCamWhiteListService
public SmartCamWhiteListDto create(CreateSmartCamWhiteListDto request) {
SmartCamWhiteListEntity entity = mapper.toEntity(request);
long cnt = this.repository.countByCarNumberAndOrganizationId(
request.getCarNumber(), request.getOrganizationId());
long cnt = this.repository.countByCarNumberAndOrganizationId(request.getCarNumber(), Ctx.currentOrganizationId());
if (cnt > 0) {
return null;

View File

@@ -2880,6 +2880,31 @@
"name": "出口"
}
]
},
{
"code": "plc.schema.detail.type",
"name": "PLC数据模型字段类型",
"item": [
{
"code": "str",
"name": "字符串"
},
{
"code": "int",
"name": "整数"
},
{
"code": "double",
"name": "浮点数"
},
{
"code": "bool",
"name": "布尔值"
}
]
}
]

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcData.controller.PlcDataController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcDataController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcDevice.controller.PlcDeviceController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcDeviceController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcDeviceSupplier.controller.PlcDeviceSupplierController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcDeviceSupplierController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcSchemaDetail.controller.PlcSchemaDetailController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcSchemaDetailController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcSchemaDetailEntity.controller.PlcSchemaDetailEntityController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcSchemaDetailEntityController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())

View File

@@ -0,0 +1,19 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.plcSchema.controller.PlcSchemaController
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(PlcSchemaController.class)
def objectMapper = ioc.getBean(ObjectMapper.class) as ObjectMapper
return controller.list(params!=null ? objectMapper.convertValue(params, CommonQuery.class ) : new CommonQuery())