fix: 修复种类属性保存时List无泛型导致MyBatis报错
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -11,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品控制器
|
* 商品控制器
|
||||||
@@ -156,12 +155,12 @@ public class ProductController {
|
|||||||
@PostMapping("/categories/{categoryId}/attributes")
|
@PostMapping("/categories/{categoryId}/attributes")
|
||||||
public Result<Void> saveCategoryAttributes(
|
public Result<Void> saveCategoryAttributes(
|
||||||
@PathVariable String categoryId,
|
@PathVariable String categoryId,
|
||||||
@RequestBody List attributes,
|
@RequestBody List<Map<String, Object>> attrs,
|
||||||
@RequestHeader(value = "X-User-Role", required = false) String role) {
|
@RequestHeader(value = "X-User-Role", required = false) String role) {
|
||||||
if (!"admin".equals(role)) {
|
if (!"admin".equals(role)) {
|
||||||
return Result.error("只有管理员可以操作");
|
return Result.error("只有管理员可以操作");
|
||||||
}
|
}
|
||||||
categoryAttributeService.saveAttributes(categoryId, attributes);
|
categoryAttributeService.saveAttributes(categoryId, attrs);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.building.service;
|
|||||||
|
|
||||||
import com.example.building.entity.CategoryAttribute;
|
import com.example.building.entity.CategoryAttribute;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 种类属性服务接口
|
* 种类属性服务接口
|
||||||
@@ -16,7 +17,7 @@ public interface CategoryAttributeService {
|
|||||||
/**
|
/**
|
||||||
* 保存属性
|
* 保存属性
|
||||||
*/
|
*/
|
||||||
void saveAttributes(String categoryId, List<CategoryAttribute> attrs);
|
void saveAttributes(String categoryId, List<Map<String, Object>> attrs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除种类的所有属性
|
* 删除种类的所有属性
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,13 +31,10 @@ public class CategoryAttributeServiceImpl implements CategoryAttributeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void saveAttributes(String categoryId, List<CategoryAttribute> attrs) {
|
public void saveAttributes(String categoryId, List<Map<String, Object>> attrs) {
|
||||||
System.out.println("=== saveAttributes called ===");
|
System.out.println("=== saveAttributes called ===");
|
||||||
System.out.println("categoryId: " + categoryId);
|
System.out.println("categoryId: " + categoryId);
|
||||||
System.out.println("attrs size: " + (attrs != null ? attrs.size() : 0));
|
System.out.println("attrs size: " + (attrs != null ? attrs.size() : 0));
|
||||||
if (attrs != null) {
|
|
||||||
attrs.forEach(a -> System.out.println(" attr: " + a.getName() + ", type=" + a.getAttrType() + ", unit=" + a.getUnit()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除旧属性
|
// 删除旧属性
|
||||||
categoryAttributeMapper.delete(new LambdaQueryWrapper<CategoryAttribute>()
|
categoryAttributeMapper.delete(new LambdaQueryWrapper<CategoryAttribute>()
|
||||||
@@ -44,10 +43,29 @@ public class CategoryAttributeServiceImpl implements CategoryAttributeService {
|
|||||||
// 保存新属性
|
// 保存新属性
|
||||||
if (attrs != null && !attrs.isEmpty()) {
|
if (attrs != null && !attrs.isEmpty()) {
|
||||||
for (int i = 0; i < attrs.size(); i++) {
|
for (int i = 0; i < attrs.size(); i++) {
|
||||||
CategoryAttribute attr = attrs.get(i);
|
Map<String, Object> attrMap = attrs.get(i);
|
||||||
|
CategoryAttribute attr = new CategoryAttribute();
|
||||||
attr.setAttrId(UUID.randomUUID().toString());
|
attr.setAttrId(UUID.randomUUID().toString());
|
||||||
attr.setCategoryId(categoryId);
|
attr.setCategoryId(categoryId);
|
||||||
attr.setSortOrder(i);
|
attr.setSortOrder(i);
|
||||||
|
|
||||||
|
if (attrMap.get("name") != null) {
|
||||||
|
attr.setName(attrMap.get("name").toString());
|
||||||
|
}
|
||||||
|
if (attrMap.get("attrType") != null) {
|
||||||
|
attr.setAttrType(attrMap.get("attrType").toString());
|
||||||
|
}
|
||||||
|
if (attrMap.get("unit") != null) {
|
||||||
|
attr.setUnit(attrMap.get("unit").toString());
|
||||||
|
}
|
||||||
|
if (attrMap.get("required") != null) {
|
||||||
|
attr.setRequired((Boolean) attrMap.get("required"));
|
||||||
|
}
|
||||||
|
if (attrMap.get("formula") != null) {
|
||||||
|
attr.setFormula(attrMap.get("formula").toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(" attr: " + attr.getName() + ", type=" + attr.getAttrType() + ", unit=" + attr.getUnit());
|
||||||
categoryAttributeMapper.insert(attr);
|
categoryAttributeMapper.insert(attr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user