feat: 支持公式属性自动计算(如面积=长*宽)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -105,7 +105,15 @@ public class ProductController {
|
|||||||
if (!"admin".equals(role)) {
|
if (!"admin".equals(role)) {
|
||||||
return Result.error("只有管理员可以操作");
|
return Result.error("只有管理员可以操作");
|
||||||
}
|
}
|
||||||
return Result.success(productService.createProduct(product));
|
Product created = productService.createProduct(product);
|
||||||
|
// 如果有属性,一并保存
|
||||||
|
if (product.getAttributes() != null && !product.getAttributes().isEmpty()) {
|
||||||
|
productAttributeService.saveProductAttributes(
|
||||||
|
created.getProductId(),
|
||||||
|
created.getCategoryId(),
|
||||||
|
product.getAttributes());
|
||||||
|
}
|
||||||
|
return Result.success(created);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,12 +188,14 @@ public class ProductController {
|
|||||||
@PostMapping("/{productId}/attributes")
|
@PostMapping("/{productId}/attributes")
|
||||||
public Result<Void> saveProductAttributes(
|
public Result<Void> saveProductAttributes(
|
||||||
@PathVariable String productId,
|
@PathVariable String productId,
|
||||||
@RequestBody List<Map<String, Object>> attrs,
|
@RequestBody Map<String, Object> body,
|
||||||
@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("只有管理员可以操作");
|
||||||
}
|
}
|
||||||
productAttributeService.saveProductAttributes(productId, attrs);
|
String categoryId = (String) body.get("categoryId");
|
||||||
|
List<Map<String, Object>> attrs = (List<Map<String, Object>>) body.get("attributes");
|
||||||
|
productAttributeService.saveProductAttributes(productId, categoryId, attrs);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import lombok.Data;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品实体类
|
* 商品实体类
|
||||||
@@ -71,6 +73,9 @@ public class Product {
|
|||||||
*/
|
*/
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
|
@TableField(exist = false)
|
||||||
|
private List<Map<String, Object>> attributes;
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT)
|
@TableField(fill = FieldFill.INSERT)
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,12 @@ public interface ProductAttributeService {
|
|||||||
List<ProductAttribute> getByProductId(String productId);
|
List<ProductAttribute> getByProductId(String productId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存商品属性值
|
* 保存商品属性值(自动计算公式属性)
|
||||||
|
* @param productId 商品ID
|
||||||
|
* @param categoryId 种类ID(用于获取公式定义)
|
||||||
|
* @param attrs 属性列表
|
||||||
*/
|
*/
|
||||||
void saveProductAttributes(String productId, List<Map<String, Object>> attrs);
|
void saveProductAttributes(String productId, String categoryId, List<Map<String, Object>> attrs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除商品的所有属性
|
* 删除商品的所有属性
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
package com.example.building.service.impl;
|
package com.example.building.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.example.building.entity.CategoryAttribute;
|
||||||
import com.example.building.entity.ProductAttribute;
|
import com.example.building.entity.ProductAttribute;
|
||||||
|
import com.example.building.mapper.CategoryAttributeMapper;
|
||||||
import com.example.building.mapper.ProductAttributeMapper;
|
import com.example.building.mapper.ProductAttributeMapper;
|
||||||
import com.example.building.service.ProductAttributeService;
|
import com.example.building.service.ProductAttributeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.regex.Matcher;
|
||||||
import java.util.UUID;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品属性服务实现
|
* 商品属性服务实现
|
||||||
@@ -21,6 +23,9 @@ public class ProductAttributeServiceImpl implements ProductAttributeService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ProductAttributeMapper productAttributeMapper;
|
private ProductAttributeMapper productAttributeMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CategoryAttributeMapper categoryAttributeMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ProductAttribute> getByProductId(String productId) {
|
public List<ProductAttribute> getByProductId(String productId) {
|
||||||
return productAttributeMapper.selectList(new LambdaQueryWrapper<ProductAttribute>()
|
return productAttributeMapper.selectList(new LambdaQueryWrapper<ProductAttribute>()
|
||||||
@@ -29,23 +34,176 @@ public class ProductAttributeServiceImpl implements ProductAttributeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void saveProductAttributes(String productId, List<Map<String, Object>> attrs) {
|
public void saveProductAttributes(String productId, String categoryId, List<Map<String, Object>> attrs) {
|
||||||
|
// 获取种类的属性定义(包括公式)
|
||||||
|
List<CategoryAttribute> categoryAttrs = categoryAttributeMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<CategoryAttribute>()
|
||||||
|
.eq(CategoryAttribute::getCategoryId, categoryId)
|
||||||
|
.orderByAsc(CategoryAttribute::getSortOrder));
|
||||||
|
|
||||||
|
// 构建属性名->属性定义的映射
|
||||||
|
Map<String, CategoryAttribute> attrDefMap = new HashMap<>();
|
||||||
|
for (CategoryAttribute ca : categoryAttrs) {
|
||||||
|
attrDefMap.put(ca.getName(), ca);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先把输入的属性保存到 Map 中(用于公式计算)
|
||||||
|
Map<String, Double> attrValues = new HashMap<>();
|
||||||
|
List<ProductAttribute> toSave = new ArrayList<>();
|
||||||
|
|
||||||
|
// 第一遍:保存所有普通属性
|
||||||
|
for (Map<String, Object> attr : attrs) {
|
||||||
|
String attrName = (String) attr.get("attrName");
|
||||||
|
String attrValue = String.valueOf(attr.get("attrValue"));
|
||||||
|
|
||||||
|
// 尝试转换为数字
|
||||||
|
try {
|
||||||
|
attrValues.put(attrName, Double.parseDouble(attrValue));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// 非数字类型不做计算
|
||||||
|
}
|
||||||
|
|
||||||
|
ProductAttribute pa = new ProductAttribute();
|
||||||
|
pa.setId(UUID.randomUUID().toString());
|
||||||
|
pa.setProductId(productId);
|
||||||
|
pa.setAttrId((String) attr.get("attrId"));
|
||||||
|
pa.setAttrName(attrName);
|
||||||
|
pa.setAttrValue(attrValue);
|
||||||
|
toSave.add(pa);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第二遍:处理公式属性
|
||||||
|
for (CategoryAttribute ca : categoryAttrs) {
|
||||||
|
if ("formula".equals(ca.getAttrType()) && ca.getFormula() != null && !ca.getFormula().isEmpty()) {
|
||||||
|
String formula = ca.getFormula();
|
||||||
|
try {
|
||||||
|
double result = evaluateFormula(formula, attrValues);
|
||||||
|
// 保留两位小数
|
||||||
|
result = Math.round(result * 100.0) / 100.0;
|
||||||
|
|
||||||
|
ProductAttribute pa = new ProductAttribute();
|
||||||
|
pa.setId(UUID.randomUUID().toString());
|
||||||
|
pa.setProductId(productId);
|
||||||
|
pa.setAttrId(ca.getAttrId());
|
||||||
|
pa.setAttrName(ca.getName());
|
||||||
|
pa.setAttrValue(String.valueOf(result));
|
||||||
|
toSave.add(pa);
|
||||||
|
|
||||||
|
System.out.println("公式计算: " + formula + " = " + result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("公式计算失败: " + formula + ", error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 删除旧属性
|
// 删除旧属性
|
||||||
productAttributeMapper.delete(new LambdaQueryWrapper<ProductAttribute>()
|
productAttributeMapper.delete(new LambdaQueryWrapper<ProductAttribute>()
|
||||||
.eq(ProductAttribute::getProductId, productId));
|
.eq(ProductAttribute::getProductId, productId));
|
||||||
|
|
||||||
// 保存新属性
|
// 保存新属性
|
||||||
if (attrs != null && !attrs.isEmpty()) {
|
for (ProductAttribute pa : toSave) {
|
||||||
for (Map<String, Object> attr : attrs) {
|
|
||||||
ProductAttribute pa = new ProductAttribute();
|
|
||||||
pa.setId(UUID.randomUUID().toString());
|
|
||||||
pa.setProductId(productId);
|
|
||||||
pa.setAttrId((String) attr.get("attrId"));
|
|
||||||
pa.setAttrName((String) attr.get("attrName"));
|
|
||||||
pa.setAttrValue(String.valueOf(attr.get("attrValue")));
|
|
||||||
productAttributeMapper.insert(pa);
|
productAttributeMapper.insert(pa);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析并计算公式
|
||||||
|
* 支持四则运算 + - * / 和括号 ()
|
||||||
|
* 变量名用实际值替换
|
||||||
|
*/
|
||||||
|
private double evaluateFormula(String formula, Map<String, Double> variables) {
|
||||||
|
// 替换变量为数值
|
||||||
|
String expr = formula;
|
||||||
|
Pattern pattern = Pattern.compile("[a-zA-Z_]+");
|
||||||
|
Matcher matcher = pattern.matcher(formula);
|
||||||
|
while (matcher.find()) {
|
||||||
|
String varName = matcher.group();
|
||||||
|
if (variables.containsKey(varName)) {
|
||||||
|
expr = expr.replace(varName, String.valueOf(variables.get(varName)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 JavaScript 引擎计算表达式
|
||||||
|
return evalExpression(expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 简单的表达式计算器(支持 + - * / 和括号)
|
||||||
|
*/
|
||||||
|
private double evalExpression(String expr) {
|
||||||
|
// 去除空格
|
||||||
|
expr = expr.replace(" ", "");
|
||||||
|
|
||||||
|
return parseExpression(expr, new int[]{0});
|
||||||
|
}
|
||||||
|
|
||||||
|
private double parseExpression(String expr, int[] pos) {
|
||||||
|
double result = parseTerm(expr, pos);
|
||||||
|
|
||||||
|
while (pos[0] < expr.length()) {
|
||||||
|
char op = expr.charAt(pos[0]);
|
||||||
|
if (op != '+' && op != '-') break;
|
||||||
|
pos[0]++;
|
||||||
|
double term = parseTerm(expr, pos);
|
||||||
|
if (op == '+') {
|
||||||
|
result += term;
|
||||||
|
} else {
|
||||||
|
result -= term;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double parseTerm(String expr, int[] pos) {
|
||||||
|
double result = parseFactor(expr, pos);
|
||||||
|
|
||||||
|
while (pos[0] < expr.length()) {
|
||||||
|
char op = expr.charAt(pos[0]);
|
||||||
|
if (op != '*' && op != '/') break;
|
||||||
|
pos[0]++;
|
||||||
|
double factor = parseFactor(expr, pos);
|
||||||
|
if (op == '*') {
|
||||||
|
result *= factor;
|
||||||
|
} else {
|
||||||
|
if (factor != 0) {
|
||||||
|
result /= factor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double parseFactor(String expr, int[] pos) {
|
||||||
|
// 处理括号
|
||||||
|
if (pos[0] < expr.length() && expr.charAt(pos[0]) == '(') {
|
||||||
|
pos[0]++; // 跳过 (
|
||||||
|
double result = parseExpression(expr, pos);
|
||||||
|
if (pos[0] < expr.length() && expr.charAt(pos[0]) == ')') {
|
||||||
|
pos[0]++; // 跳过 )
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理负数
|
||||||
|
boolean negative = false;
|
||||||
|
if (pos[0] < expr.length() && expr.charAt(pos[0]) == '-') {
|
||||||
|
negative = true;
|
||||||
|
pos[0]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析数字
|
||||||
|
double result = 0;
|
||||||
|
while (pos[0] < expr.length()) {
|
||||||
|
char c = expr.charAt(pos[0]);
|
||||||
|
if ((c >= '0' && c <= '9') || c == '.') {
|
||||||
|
result = result * 10 + (c - '0');
|
||||||
|
pos[0]++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return negative ? -result : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user