edi.rule.model.JSRuleDelete Maven / Gradle / Ivy
package edi.rule.model;
import edi.rule.annotation.JSRuleCheck;
import edi.rule.annotation.JSRuleInject;
import edi.rule.config.JSRuleMessage;
import edi.rule.core.JSRuleArgsVessel;
import edi.rule.core.JSRuleJdbc;
import edi.rule.core.JSRuleMethod;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import edi.rule.util.ZSString;
import edi.rule.work.enums.JSRuleCrudEnum;
import edi.rule.work.custom.JSRuleException;
import edi.rule.work.interfaces.IJSRuleCondition;
import edi.rule.work.interfaces.IJSRuleDmlModel;
import lombok.Data;
/**
* @author 摩拉克斯
* @date 2022年1月4日 下午1:42:10
* 删除数据模型
*/
@Data
public class JSRuleDelete> implements IJSRuleDmlModel,IJSRuleCondition{
/**
* 映射类的类名,别名class,必须存在的属性,用于指定关联要删除的表
* */
@JsonAlias({"class"})
@JSRuleCheck(required=true)
public String className;
/**
*
条件模型
* */
@JSRuleCheck
public JSRuleConditions condition;
/**
*
是否允许删除全部数据,默认为false
* */
public Boolean deleteAll;
/**
*
是否执行最终形成的sql语句,默认为true,为false时将返回sql语句,子查询或union时使用的仅是其解析后的sql,并不需要真正的执行,因此可将其设置为false以避免多余的计算
* */
public Boolean execute;
@JSRuleInject
@JsonIgnore
private JSRuleArgsVessel vessel;
@JSRuleInject
@JsonIgnore
private JSRuleJdbc jdbc;
public JSRuleDelete() {
this.deleteAll = false;
execute = true;
}
@Override
public final Object startCrudModel(A action) {
String conditions = JSRuleMethod.addConditions(getCondition(),action.cache, vessel.getActionArgs());
if (ZSString.isBlank(conditions)) {
if (!getDeleteAll()) {
throw new JSRuleException(action.cache.name, JSRuleMessage.read("condition.is.required"));
}
}else {
conditions = where + conditions;
}
String sql = beforeSql("delete from "+action.cache.getMappingInfo(this.getClassName()).tableName+conditions,action);
vessel.getActionArgs().get(action.cache.name).setSql(sql);
if (!getExecute()) {
return sql;
}
return jdbc.delete(sql);
}
/**
*
检查映射类信息以及对应的角色信息是否具有delete操作权限
* */
@Override
public final void checkCrudPermit(A action) {
checkCrudPermit(action.name,JSRuleCrudEnum.D, vessel.getGlobalArgs().getRoles());
}
/**
*
delete语句执行前执行
* @param sql delete语句
* */
protected String beforeSql(String sql,A action) {
return sql;
}
}