|
|
|
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
|
|
|
|
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductUnitDO;
|
|
|
|
|
import cn.iocoder.yudao.module.erp.service.product.ErpProductUnitService;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.JavaToTdengineTypeEnum;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.deviceattributetype.DeviceAttributeTypeMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants;
|
|
|
|
|
@ -234,7 +235,7 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) // 完全复用你的事务注解,异常回滚所有导入
|
|
|
|
|
// @Transactional(rollbackFor = Exception.class) // 完全复用你的事务注解,异常回滚所有导入
|
|
|
|
|
public DeviceModelAttributeImportRespVO importAttributeList(List<DeviceModelAttributeImportExcelVO> importAttributes, Long deviceModelId, boolean isUpdateSupport) {
|
|
|
|
|
// 1.1 参数校验 - 完全复刻 User 的空列表校验逻辑
|
|
|
|
|
if (CollUtil.isEmpty(importAttributes)) {
|
|
|
|
|
@ -250,6 +251,7 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
|
|
|
|
Map<String, String> typeNameToIdMap = attributeTypes.stream()
|
|
|
|
|
.collect(Collectors.toMap(DeviceAttributeTypeDO::getName, type -> String.valueOf(type.getId())));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 初始化返回 VO - 完全复刻 User 的 builder 方式,使用 LinkedHashMap 保持顺序
|
|
|
|
|
DeviceModelAttributeImportRespVO respVO = DeviceModelAttributeImportRespVO.builder()
|
|
|
|
|
.createCodes(new ArrayList<>())
|
|
|
|
|
@ -264,18 +266,40 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
|
|
|
|
// 4.2 处理点位类型转换
|
|
|
|
|
String typeName = importAttribute.getTypeName();
|
|
|
|
|
|
|
|
|
|
// 不能为空
|
|
|
|
|
if (StringUtils.isBlank(typeName)) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(), "点位类型名称不能为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String attributeTypeId = typeNameToIdMap.get(typeName);
|
|
|
|
|
if (StringUtils.isBlank(attributeTypeId)) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(), "点位类型名称无效: " + typeName);
|
|
|
|
|
// 必须存在于数据库类型列表中(防止乱填)
|
|
|
|
|
if (!typeNameToIdMap.containsKey(typeName)) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(),
|
|
|
|
|
"点位类型名称不存在: " + typeName);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String attributeTypeId = typeNameToIdMap.get(typeName);
|
|
|
|
|
// 设置点位类型ID
|
|
|
|
|
importAttribute.setAttributeType(attributeTypeId);
|
|
|
|
|
|
|
|
|
|
//dataType校验
|
|
|
|
|
String dataType = importAttribute.getDataType();
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(dataType)) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(),
|
|
|
|
|
"数据类型 不能为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dataType = StringUtils.trim(dataType);
|
|
|
|
|
|
|
|
|
|
// 必须在枚举中存在
|
|
|
|
|
JavaToTdengineTypeEnum typeEnum = JavaToTdengineTypeEnum.fromJavaType(dataType);
|
|
|
|
|
if (typeEnum == null) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(),
|
|
|
|
|
"数据类型 非法,仅支持: byte, short, int, long, float, double, boolean, char");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4.3.1 校验字段是否符合要求 - 完全复刻 User 的 ValidationUtils.validate 调用
|
|
|
|
|
try {
|
|
|
|
|
// 注意:如果有 DeviceModelAttributeSaveReqVO,替换为该 VO;无则直接校验 importAttribute
|
|
|
|
|
@ -293,27 +317,53 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4.4.1 判断如果不存在,进行插入 - 完全复刻 User 的 selectByUsername 逻辑
|
|
|
|
|
DeviceModelAttributeDO existAttribute = deviceModelAttributeMapper.selectByAttributeCode(importAttribute.getAttributeCode());
|
|
|
|
|
if (existAttribute == null) {
|
|
|
|
|
// 创建DO对象并设置点位类型名称
|
|
|
|
|
DeviceModelAttributeDO deviceModelAttribute = BeanUtils.toBean(importAttribute, DeviceModelAttributeDO.class);
|
|
|
|
|
deviceModelAttribute.setTypeName(typeName); // 设置点位类型名称
|
|
|
|
|
// 4.4 查询数据库是否已存在
|
|
|
|
|
DeviceModelAttributeDO existAttribute =
|
|
|
|
|
deviceModelAttributeMapper.selectOne(
|
|
|
|
|
Wrappers.<DeviceModelAttributeDO>lambdaQuery()
|
|
|
|
|
.eq(DeviceModelAttributeDO::getDeviceModelId, deviceModelId)
|
|
|
|
|
.eq(DeviceModelAttributeDO::getAttributeCode, importAttribute.getAttributeCode())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// ===== 不允许覆盖 =====
|
|
|
|
|
if (!isUpdateSupport) {
|
|
|
|
|
|
|
|
|
|
// 如果已存在 → 直接失败
|
|
|
|
|
if (existAttribute != null) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(),
|
|
|
|
|
"点位编码 已存在,不允许重复");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不存在 → 新增
|
|
|
|
|
DeviceModelAttributeDO deviceModelAttribute =
|
|
|
|
|
BeanUtils.toBean(importAttribute, DeviceModelAttributeDO.class);
|
|
|
|
|
deviceModelAttribute.setTypeName(typeName);
|
|
|
|
|
|
|
|
|
|
deviceModelAttributeMapper.insert(deviceModelAttribute);
|
|
|
|
|
respVO.getCreateCodes().add(importAttribute.getAttributeCode());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4.4.2 如果存在,判断是否允许更新 - 完全复刻 User 的更新逻辑
|
|
|
|
|
if (!isUpdateSupport) {
|
|
|
|
|
respVO.getFailureCodes().put(importAttribute.getAttributeCode(), DEVICE_MODEL_ATTRIBUTE_NOT_EXISTS.getMsg());
|
|
|
|
|
// ===== 允许覆盖 =====
|
|
|
|
|
|
|
|
|
|
// 不存在 → 新增
|
|
|
|
|
if (existAttribute == null) {
|
|
|
|
|
DeviceModelAttributeDO deviceModelAttribute =
|
|
|
|
|
BeanUtils.toBean(importAttribute, DeviceModelAttributeDO.class);
|
|
|
|
|
deviceModelAttribute.setTypeName(typeName);
|
|
|
|
|
|
|
|
|
|
deviceModelAttributeMapper.insert(deviceModelAttribute);
|
|
|
|
|
respVO.getCreateCodes().add(importAttribute.getAttributeCode());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建更新对象并设置点位类型名称
|
|
|
|
|
DeviceModelAttributeDO updateAttribute = BeanUtils.toBean(importAttribute, DeviceModelAttributeDO.class);
|
|
|
|
|
updateAttribute.setId(existAttribute.getId()); // 复用已有数据的 ID
|
|
|
|
|
updateAttribute.setTypeName(typeName); // 设置点位类型名称
|
|
|
|
|
// 已存在 → 更新
|
|
|
|
|
DeviceModelAttributeDO updateAttribute =
|
|
|
|
|
BeanUtils.toBean(importAttribute, DeviceModelAttributeDO.class);
|
|
|
|
|
updateAttribute.setId(existAttribute.getId());
|
|
|
|
|
updateAttribute.setTypeName(typeName);
|
|
|
|
|
|
|
|
|
|
deviceModelAttributeMapper.updateById(updateAttribute);
|
|
|
|
|
respVO.getUpdateCodes().add(importAttribute.getAttributeCode());
|
|
|
|
|
});
|
|
|
|
|
|