Generate executable business action effects
This commit is contained in:
@@ -13,7 +13,7 @@ DELETE FROM sys_template_file WHERE template_id IN (9101, 9102, 9103) OR templat
|
||||
DELETE FROM sys_template WHERE template_id IN (9101, 9102, 9103);
|
||||
|
||||
INSERT INTO sys_template (template_id, template_name, template_path, template_desc, template_type, template_status, create_by, create_time, remark) VALUES
|
||||
(9101, 'Business Blueprint Backend', '/blueprint/backend', 'RuoYi backend with CRUD and blueprint action stubs', 'backend', 0, 'admin', sysdate(), 'business blueprint'),
|
||||
(9101, 'Business Blueprint Backend', '/blueprint/backend', 'RuoYi backend with CRUD and executable blueprint effects', 'backend', 0, 'admin', sysdate(), 'business blueprint'),
|
||||
(9102, 'Business Blueprint Admin Frontend', '/blueprint/admin_frontend', 'Vue2 Element UI admin pages with blueprint action buttons', 'admin_frontend', 0, 'admin', sysdate(), 'business blueprint'),
|
||||
(9103, 'Business Blueprint Portal Frontend', '/blueprint/frontend', 'Vue3 Element Plus portal pages with blueprint action buttons', 'frontend', 0, 'admin', sysdate(), 'business blueprint');
|
||||
|
||||
@@ -283,6 +283,7 @@ public interface I${ClassName}Service
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
@@ -298,6 +299,10 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
|
||||
{
|
||||
@Resource
|
||||
private ${ClassName}Mapper ${className}Mapper;
|
||||
#if($hasTableBusinessActions)
|
||||
@Resource
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
#end
|
||||
|
||||
@Override
|
||||
public ${ClassName} select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
|
||||
@@ -362,10 +367,93 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
|
||||
@Override
|
||||
public int ${action.code}(Map<String, Object> params)
|
||||
{
|
||||
throw new ServiceException("Business action not implemented: ${action.name}");
|
||||
if (params == null) {
|
||||
throw new ServiceException("业务动作参数不能为空");
|
||||
}
|
||||
#if($action.requestFields)
|
||||
#foreach($field in $action.requestFields)
|
||||
requireBusinessParam(params, "${field}");
|
||||
#end
|
||||
#end
|
||||
int rows = 0;
|
||||
#if($action.effects)
|
||||
#foreach($effect in $action.effects)
|
||||
#if($effect.type == "UPDATE_FIELD")
|
||||
#set($conditionFields = $effect.conditionFields)
|
||||
#if(!$conditionFields || $conditionFields.size() == 0)
|
||||
#set($conditionFields = $action.requestFields)
|
||||
#end
|
||||
rows += executeUpdateFieldEffect(params, "${effect.targetTable}", "${effect.targetField}", "${effect.value}", new String[] {#foreach($field in $conditionFields)"${field}"#if($foreach.hasNext), #end#end});
|
||||
#else
|
||||
// Unsupported effect type ${effect.type}: ${effect.description}
|
||||
#end
|
||||
#end
|
||||
#end
|
||||
return rows;
|
||||
}
|
||||
|
||||
#end
|
||||
private void requireBusinessParam(Map<String, Object> params, String field)
|
||||
{
|
||||
if (!params.containsKey(field) || params.get(field) == null) {
|
||||
throw new ServiceException("缺少业务动作参数:" + field);
|
||||
}
|
||||
}
|
||||
|
||||
private int executeUpdateFieldEffect(Map<String, Object> params, String tableName, String targetField,
|
||||
String valueExpression, String[] conditionFields)
|
||||
{
|
||||
String safeTableName = requireSafeIdentifier(tableName, "业务影响目标表不合法");
|
||||
String safeTargetField = requireSafeIdentifier(targetField, "业务影响目标字段不合法");
|
||||
String safeValueExpression = requireSafeExpression(valueExpression);
|
||||
String whereClause = buildWhereClause(params, conditionFields);
|
||||
Object[] values = conditionValues(params, conditionFields);
|
||||
String sql = "update " + safeTableName + " set " + safeTargetField + " = " + safeValueExpression + " where " + whereClause;
|
||||
return jdbcTemplate.update(sql, values);
|
||||
}
|
||||
|
||||
private String buildWhereClause(Map<String, Object> params, String[] conditionFields)
|
||||
{
|
||||
if (conditionFields == null || conditionFields.length == 0) {
|
||||
throw new ServiceException("业务影响缺少条件字段");
|
||||
}
|
||||
StringBuilder where = new StringBuilder();
|
||||
for (String field : conditionFields) {
|
||||
String safeField = requireSafeIdentifier(field, "业务影响条件字段不合法");
|
||||
requireBusinessParam(params, safeField);
|
||||
if (where.length() > 0) {
|
||||
where.append(" and ");
|
||||
}
|
||||
where.append(safeField).append(" = ?");
|
||||
}
|
||||
return where.toString();
|
||||
}
|
||||
|
||||
private Object[] conditionValues(Map<String, Object> params, String[] conditionFields)
|
||||
{
|
||||
Object[] values = new Object[conditionFields.length];
|
||||
for (int i = 0; i < conditionFields.length; i++) {
|
||||
values[i] = params.get(conditionFields[i]);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private String requireSafeIdentifier(String value, String message)
|
||||
{
|
||||
if (value == null || !value.matches("[A-Za-z][A-Za-z0-9_]*")) {
|
||||
throw new ServiceException(message);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String requireSafeExpression(String valueExpression)
|
||||
{
|
||||
if (valueExpression == null || !valueExpression.matches("[A-Za-z0-9_ +*/().-]{1,200}")
|
||||
|| valueExpression.contains(";") || valueExpression.contains("--")) {
|
||||
throw new ServiceException("业务影响字段表达式不合法");
|
||||
}
|
||||
return valueExpression;
|
||||
}
|
||||
#end
|
||||
}
|
||||
', 'admin', sysdate()),
|
||||
|
||||
Reference in New Issue
Block a user