Browse Source

增加物关系的导入导出

2024年10月18日14:59:21
thing_master
lishuai 1 year ago
parent
commit
5a6e7d0553
  1. 4
      modules/thing/src/main/java/com/thing/device/deviceManagement/dto/IotDeviceManagementDTO.java
  2. 11
      modules/thing/src/main/java/com/thing/device/deviceManagement/entity/IotDeviceManagementEntity.java
  3. 28
      modules/thing/src/main/java/com/thing/thing/relation/detail/controller/IotThingRelationDetailController.java
  4. 62
      modules/thing/src/main/java/com/thing/thing/relation/detail/excel/IotThingRelationDetailExcel.java
  5. 9
      modules/thing/src/main/java/com/thing/thing/relation/detail/service/IotThingRelationDetailService.java
  6. 102
      modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java
  7. 2
      modules/thing/src/main/java/com/thing/thing/relation/root/service/IotThingRelationRootService.java
  8. 6
      modules/thing/src/main/java/com/thing/thing/relation/root/service/impl/IotThingRelationRootServiceImpl.java

4
modules/thing/src/main/java/com/thing/device/deviceManagement/dto/IotDeviceManagementDTO.java

@ -50,6 +50,10 @@ public class IotDeviceManagementDTO implements Serializable {
private String thingAttrBoundary;
@Schema(description = "英文逗号分割,如2,3,4 表示2天3小时4分钟")
private String timeInfo;
@Schema(description = "是否为数据处理: 0 否 1 是")
private String dataTreatingMark;
@Schema(description = "映射子集(子数据列表)")
private String childConfig;
@Schema(description = "控制详情,只有 configType是3得 控制得,这里才会有数据")
private IotDeviceControlEntity ctlInfo;

11
modules/thing/src/main/java/com/thing/device/deviceManagement/entity/IotDeviceManagementEntity.java

@ -84,4 +84,15 @@ public class IotDeviceManagementEntity implements Serializable {
*/
private String timeInfo;
/**
* 是否为数据处理: 0 1
*/
private String dataTreatingMark;
/**
* 映射子集(子数据列表)
*/
private String childConfig;
}

28
modules/thing/src/main/java/com/thing/thing/relation/detail/controller/IotThingRelationDetailController.java

@ -20,9 +20,14 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("v2/relation/detail")
@ -187,5 +192,28 @@ public class IotThingRelationDetailController {
*}
*/
@PostMapping("import")
@Operation(summary="物关系导入")
@LogOperation("物关系导入")
public Result<Void> importTenant(MultipartFile file, HttpServletRequest request) {
service.importExcel(file, request);
return new Result<>();
}
@PostMapping("export")
@Operation(summary="物关系导出")
@LogOperation("物关系导出")
public void export(@RequestBody Long[] ids, @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
if(ArrayUtils.isNotEmpty(ids)){
params.put("ids",ids);
}
service.exportExcel(params,response);
}
@PostMapping("template")
@Operation(summary="物关系模板下载")
public void exportTemplate(HttpServletResponse response) {
service.template(response);
}
}

62
modules/thing/src/main/java/com/thing/thing/relation/detail/excel/IotThingRelationDetailExcel.java

@ -0,0 +1,62 @@
package com.thing.thing.relation.detail.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.Serial;
import java.io.Serializable;
/**
* 物关系详情表
*
* @author xc/ls
* @since 3.0 2023-06-05
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@AllArgsConstructor
public class IotThingRelationDetailExcel implements Serializable {
@Excel(name = "关系名称",width = 25)
private String name;
@Excel(name = "上级物编码",orderNum = "1",width = 25)
private String fromCode;
@Excel(name = "上级物名称",orderNum = "2",width = 25 )
private String fromName;
@Excel(name = "下级物编码",orderNum = "3",width = 25 )
private String toCode;
@Excel(name = "下级物名称",orderNum = "4",width = 25 )
private String toName;
@Excel(name = "位置",orderNum = "5",width = 25 )
private String config;
@Excel(name = "备注",orderNum = "6",width = 25 )
private String remark;
@Excel(name = "排序",orderNum = "7",width = 25 )
private Long sort;
@Excel(name = "根物实体编码",orderNum = "8",width = 25 )
private String rootThingCode;
@Excel(name = "图片信息地址",orderNum = "9",width = 25 )
private String url;
@Excel(name = "标签",orderNum = "10",width = 25 )
private String tag;
}

9
modules/thing/src/main/java/com/thing/thing/relation/detail/service/IotThingRelationDetailService.java

@ -12,7 +12,10 @@ import com.thing.thing.relation.detail.dto.ThingTreeDTO;
import com.thing.thing.relation.detail.entity.IotThingRelationDetailEntity;
import com.thing.thing.relation.detail.param.IotThingRelationDetailParamDTO;
import com.thing.thing.relation.root.dto.ThingSortTreeDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;
import java.util.Collection;
import java.util.List;
@ -83,4 +86,10 @@ public interface IotThingRelationDetailService extends IBaseService<IotThingRela
void saveRelationNodes(ThingRelationDTO relationDTO);
List<IotThingRelationDetailDTO> findRootDetailChildNodeByRootIdAndToIdAndRootThingId(Long relationTypeId, Long id, Long relationTopId);
void exportExcel(Map<String, Object> params, HttpServletResponse response);
void importExcel(MultipartFile file, HttpServletRequest request);
void template(HttpServletResponse response);
}

102
modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java

@ -17,6 +17,7 @@ import com.thing.common.core.enumeration.DatasetThingRelationStatus;
import com.thing.common.core.enumeration.TreeNodeStatus;
import com.thing.common.core.exception.SysException;
import com.thing.common.core.utils.*;
import com.thing.common.core.utils.excel.ExcelUtils;
import com.thing.common.core.web.response.PageData;
import com.thing.common.orm.service.impl.BaseServiceImpl;
import com.thing.event.ThingRelationDetailSaveEvent;
@ -25,12 +26,16 @@ import com.thing.thing.api.dto.ApiEntityRelationDTO;
import com.thing.thing.cache.service.CacheInit;
import com.thing.thing.cache.service.ThingCache;
import com.thing.thing.entity.dto.IotThingEntityDTO;
import com.thing.thing.entity.dto.IotThingEntityInfoDTO;
import com.thing.thing.entity.entity.IotThingEntity;
import com.thing.thing.entity.excel.IotThingViewExcel;
import com.thing.thing.entity.service.IotThingEntityService;
import com.thing.thing.model.service.IotThingModelService;
import com.thing.thing.relation.detail.dto.IotThingRelationDetailDTO;
import com.thing.thing.relation.detail.dto.RelationDetailBatchSaveDTO;
import com.thing.thing.relation.detail.dto.ThingRelationDTO;
import com.thing.thing.relation.detail.entity.IotThingRelationDetailEntity;
import com.thing.thing.relation.detail.excel.IotThingRelationDetailExcel;
import com.thing.thing.relation.detail.mapper.IotThingRelationDetailMapper;
import com.thing.thing.relation.detail.param.IotThingRelationDetailParamDTO;
import com.thing.thing.relation.detail.service.IotThingRelationDetailService;
@ -40,13 +45,17 @@ import com.thing.thing.relation.root.entity.IotThingRelationRootEntity;
import com.thing.thing.relation.root.service.IotThingRelationRootService;
import com.thing.transport.api.adaptor.JsonConverter;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
@ -973,6 +982,99 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR
, IotThingRelationDetailDTO.class);
}
@Override
public void exportExcel(Map<String, Object> params, HttpServletResponse response) {
String orderField = MapUtils.getString(params, "orderField");
String order = MapUtils.getString(params, "order");
String rootIds = MapUtils.getString(params, "rootIds");
String name = MapUtils.getString(params, "name");
String entityName = MapUtils.getString(params, "entityName");
String groupName = MapUtils.getString(params, "groupName");
List<ObjectNode> list = findList(orderField, order, rootIds, name, entityName, groupName);
if(CollectionUtils.isEmpty(list)){
throw new SysException("当前关系无详情,无需导出物物关系列表");
}
List<IotThingRelationDetailExcel> resList = list.stream().map(node -> {
IotThingRelationDetailExcel excel = JacksonUtil.convertValue(node, IotThingRelationDetailExcel.class);
long rootThingId = node.get("rootThingId").asLong();
IotThingEntityInfoDTO entity = thingEntitiesService.findEntityById(rootThingId);
excel.setRootThingCode(entity.getCode());
return excel;
}).toList();
ExcelUtils.exportExcel(resList, "物关系详情信息", "物关系详情", IotThingViewExcel.class, "物关系详情模板.xls", response);
}
@Override
public void importExcel(MultipartFile file, HttpServletRequest request) {
List<IotThingRelationDetailExcel> sheetDataList = new ArrayList<>(ExcelUtils.importExcel(file, 1, 1, IotThingRelationDetailExcel.class, 0));
if (CollectionUtil.isEmpty(sheetDataList)) {
throw new SysException("导入数据是空的");
}
String rootName = sheetDataList.get(0).getName();
IotThingRelationRootDTO rootDTO = relationRootsService.findByName(rootName);
if(Objects.isNull(rootDTO)){
throw new SysException("导入的物关系名称不存在");
}
List<String> entityCodes = sheetDataList.stream().flatMap(s -> Stream.of(s.getFromCode(), s.getToCode(),s.getRootThingCode())).distinct().toList();
List<IotThingEntity> entities = thingEntitiesService.findAllByCodeInAndTenantCode(entityCodes, UserContext.getRealTenantCode());
if(CollectionUtils.isEmpty(entities)){
throw new SysException("导入的物关系实体不存在");
}
sheetDataList.forEach(excel -> {
//关系名称
if(!Objects.isNull(rootDTO)){
IotThingRelationDetailEntity entity = new IotThingRelationDetailEntity();
IotThingEntity fromEntity = entities.stream().filter(e -> e.getCode().equals(excel.getFromCode())).findFirst().get();
IotThingEntity toEntity = entities.stream().filter(e -> e.getCode().equals(excel.getToCode())).findFirst().get();
IotThingEntity rootEntity = entities.stream().filter(e -> e.getCode().equals(excel.getRootThingCode())).findFirst().get();
entity.setRootId(rootDTO.getId());
entity.setFromCode(excel.getFromCode());
entity.setFromCode(excel.getName());
entity.setFromId(fromEntity.getId());
entity.setToCode(excel.getToCode());
entity.setToName(excel.getToName());
entity.setToId(toEntity.getId());
entity.setConfig(excel.getConfig());
entity.setConfig(excel.getRemark());
entity.setSort(excel.getSort());
entity.setRootThingId(rootEntity.getId());
entity.setUrl(excel.getUrl());
entity.setTag(excel.getTag());
mapper.insert(entity);
ObjectNode node = JsonConverter.convertToJsonObjectObjectNode(ConvertUtils.sourceToTarget(entity, IotThingRelationDetailDTO.class));
List<ObjectNode> mapAccurateKey = cache.findMapAccurateKey(CacheNameEnum.THING_MODEL, entity.getToCode());
if (CollectionUtils.isNotEmpty(mapAccurateKey)) {
String status = mapAccurateKey.get(0).get(CacheNameEnum.ModelField.THING_MODEL_STATUS.getField()).asText();
node.put("thingStatus", status);
}
cache.updateAccurateKeyMap(CacheNameEnum.THING_DETAIL_RELATION,
entity.getRootId() + CacheInit.KEY + entity.getId() + CacheInit.KEY + entity.getRootThingId(), node);
}
});
}
@Override
public void template(HttpServletResponse response) {
IotThingRelationDetailExcel excel = new IotThingRelationDetailExcel();
excel.setFromCode("X_xxxxx_xx上级节点编码");
excel.setFromName("X_xxxxx_xx上级节点名称");
excel.setToCode("X_xxxxx_xx下级节点编码");
excel.setToName("X_xxxxx_xx下级节点名称");
excel.setName("关系名称");
excel.setRootThingCode("根关系物编码");
excel.setConfig("关系位置:默认{\"width\":120,\"height\":40,\"shape\":\"rect\"}");
excel.setTag("下级节点名称-标签");
excel.setRemark("备注");
excel.setUrl("图片地址");
ExcelUtils.exportExcel(Lists.newArrayList(excel), "物关系详情信息", "物关系详情", IotThingViewExcel.class, "物关系详情模板.xls", response);
}
@Override
public IotThingRelationDetailDTO findRootDetailParentNodeByRootIdAndToIdAndRootThingId(Long rootId, Long toId, Long rootThingId) {
return mapper.selectOneByQueryAs(QueryWrapper.create()

2
modules/thing/src/main/java/com/thing/thing/relation/root/service/IotThingRelationRootService.java

@ -24,6 +24,8 @@ public interface IotThingRelationRootService extends IBaseService<IotThingRelati
IotThingRelationRootDTO findById(Long id);
IotThingRelationRootDTO findByName(String name);
List<IotThingRelationRootDTO> findByIds(List<Long> id,boolean isAsc);
Long findMaxSort();

6
modules/thing/src/main/java/com/thing/thing/relation/root/service/impl/IotThingRelationRootServiceImpl.java

@ -236,6 +236,12 @@ public class IotThingRelationRootServiceImpl extends BaseServiceImpl<IotThingRel
return mapper.selectOneByQueryAs(QueryWrapper.create().eq(IotThingRelationRootEntity::getId,id),IotThingRelationRootDTO.class);
}
@Override
public IotThingRelationRootDTO findByName(String name) {
return mapper.selectOneByQueryAs(QueryWrapper.create().eq(IotThingRelationRootEntity::getName,name)
.eq(IotThingRelationRootEntity::getTenantCode,UserContext.getRealTenantCode()),IotThingRelationRootDTO.class);
}
@Override
public List<IotThingRelationRootDTO> findByIds(List<Long> ids,boolean isAsc) {
return mapper.selectListByQueryAs(QueryWrapper.create().in(IotThingRelationRootEntity::getId,ids).orderBy(IotThingRelationRootEntity::getSort,isAsc),IotThingRelationRootDTO.class);

Loading…
Cancel
Save