package foundation.alert;
|
|
import foundation.data.entity.Entity;
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.data.object.DataObject;
|
import foundation.persist.NamedSQL;
|
import foundation.util.MapList;
|
import foundation.util.Util;
|
|
public class AlertRule {
|
|
private String id;
|
private String code;
|
private String name;
|
private String dataName;
|
private String docIdField;
|
private String sqlName;
|
private String titleTemplate;
|
private String message;
|
private String url;
|
private boolean isUpdateDocument;
|
private MapList<String, AlterNotifier> notifiers;
|
|
final static boolean clearCheckError = false;
|
final static boolean addCheckError = true;
|
|
public void load(Entity entity) throws Exception {
|
id = entity.getString("id");
|
code = entity.getString("code");
|
name = entity.getString("name");
|
dataName = entity.getString("data_name");
|
docIdField = entity.getString("doc_id_field", "id");
|
sqlName = entity.getString("sql_name");
|
titleTemplate = entity.getString("title_template");
|
message = entity.getString("message");
|
url = entity.getString("url");
|
isUpdateDocument = entity.getBoolean("is_update_document", false);
|
notifiers = new MapList<String, AlterNotifier>();
|
}
|
|
|
public void loadOneNotifier(Entity entity) {
|
AlterNotifier notifier = new AlterNotifier();
|
notifier.load(entity);
|
notifiers.add(notifier.getId(), notifier);
|
}
|
|
public void executeOne(String id) throws Exception {
|
//1. 获取目标表的结构,计算出 title
|
String title = calculateTitle();
|
String accountId = getAccountId(dataName);
|
String positionId = getPositionId(dataName);
|
|
//2. 插入 alert表
|
String filterSegment = docIdField + "=" + Util.quotedStr(id);
|
|
NamedSQL namedSQL = NamedSQL.getInstance("insertAlert");
|
namedSQL.setParam("ruleId", this.id);
|
namedSQL.setParam("dataName", dataName);
|
namedSQL.setParam("accountId", accountId);
|
namedSQL.setParam("positionId", positionId);
|
namedSQL.setParam("title", title);
|
namedSQL.setParam("message", message);
|
namedSQL.setParam("filterSegment", filterSegment);
|
|
namedSQL.execute();
|
|
//3. 根据 alert 给业务表增加标识
|
updateDocument(addCheckError,isUpdateDocument);
|
|
}
|
|
public void executeBatch() throws Exception {
|
//1. 获取目标表的结构,计算出 title
|
String title = calculateTitle();
|
String accountId = getAccountId(dataName);
|
String positionId = getPositionId(dataName);
|
String filterSegment = NamedSQL.getInstance(sqlName).toString();
|
|
//2. 更新 alert表
|
updateAlter(filterSegment, title);
|
|
//3. 插入 alert表
|
NamedSQL namedSQL = NamedSQL.getInstance("insertAlert");
|
namedSQL.setParam("ruleId", id);
|
namedSQL.setParam("dataName", dataName);
|
namedSQL.setParam("accountId", accountId);
|
namedSQL.setParam("positionId", positionId);
|
namedSQL.setParam("docId", docIdField);
|
namedSQL.setParam("title", title);
|
namedSQL.setParam("message", message);
|
namedSQL.setParam("filterSegment", filterSegment);
|
|
namedSQL.execute();
|
|
//4. 根据 alert 给业务表增加标识
|
updateDocument(addCheckError, isUpdateDocument);
|
|
//5. 清除 alert表
|
clearAlter(filterSegment);
|
|
//6. 根据 alert 清除业务表的标识
|
updateDocument(clearCheckError, isUpdateDocument);
|
}
|
|
private void updateAlter(String filterSegment, String title) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("updateAlert");
|
namedSQL.setParam("ruleId", id);
|
namedSQL.setParam("dataName", dataName);
|
namedSQL.setParam("docId", docIdField);
|
namedSQL.setParam("title", title);
|
namedSQL.setParam("filterSegment",filterSegment);
|
|
namedSQL.execute();
|
}
|
|
private void clearAlter(String filterSegment) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("deleteAlert");
|
namedSQL.setParam("ruleId", id);
|
namedSQL.setParam("dataName", dataName);
|
namedSQL.setParam("docId", docIdField);
|
namedSQL.setParam("filterSegment",filterSegment);
|
|
namedSQL.execute();
|
}
|
|
private void updateDocument(boolean isExistError, boolean isUpdateDocument) throws Exception {
|
if (!isUpdateDocument) {
|
return;
|
}
|
updateDocument(isExistError);
|
}
|
|
private void updateDocument(boolean isExistError) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("updateDocumentCheckError");
|
namedSQL.setParam("ruleId", id);
|
namedSQL.setParam("dataName", dataName);
|
namedSQL.setParam("docId", docIdField);
|
|
if(isExistError) {
|
namedSQL.setParam("checkError", "T");
|
namedSQL.setParam("isExist", "");
|
}else {
|
namedSQL.setParam("checkError", "");
|
namedSQL.setParam("isExist", "not");
|
}
|
|
namedSQL.execute();
|
}
|
|
public String calculateTitle() {
|
String title = titleTemplate;
|
if(Util.isEmpty(title)) {
|
return "";
|
}
|
title = " concat('" + title +"') ";
|
title = title.replace("@{", "',").replace("}", ",'");
|
return title;
|
}
|
|
private String getPositionId(String dataName) throws Exception {
|
if("md_position".equalsIgnoreCase(dataName)) {
|
return "id";
|
}
|
|
DataObject dataOject = DataObject.getInstance(dataName);
|
FieldsRuntime fieldRuntime = dataOject.getTableFieldMetas();
|
|
if (fieldRuntime.contains("position_id")) {
|
return "position_id";
|
}
|
else {
|
return "null";
|
}
|
}
|
|
private String getAccountId(String dataName) throws Exception {
|
if("md_org_account".equalsIgnoreCase(dataName)) {
|
return "id";
|
}
|
|
DataObject dataOject = DataObject.getInstance(dataName);
|
FieldsRuntime fieldRuntime = dataOject.getTableFieldMetas();
|
|
if (fieldRuntime.contains("account_id")) {
|
return "account_id";
|
}
|
else if(fieldRuntime.contains("customer_id")) {
|
return "customer_id";
|
}
|
else if(fieldRuntime.contains("org_id")) {
|
return "org_id";
|
}
|
else {
|
return "null";
|
}
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public String getCode() {
|
return code;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public String getDataName() {
|
return dataName;
|
}
|
|
public String getDocIdField() {
|
return docIdField;
|
}
|
|
public String getSqlName() {
|
return sqlName;
|
}
|
|
public String getTitleTemplate() {
|
return titleTemplate;
|
}
|
|
public String getUrl() {
|
return url;
|
}
|
|
public MapList<String, AlterNotifier> getNotifiers() {
|
return notifiers;
|
}
|
}
|