package grand;
|
|
import foundation.action.ActionProvider;
|
import foundation.alert.ActorType;
|
import foundation.alert.AlertRule;
|
import foundation.alert.AlertRuleBucket;
|
import foundation.alert.AlterNotifier;
|
import foundation.dao.DataPackage;
|
import foundation.dao.DataSource;
|
import foundation.data.entity.Entity;
|
import foundation.data.entity.EntitySet;
|
import foundation.data.object.DataObject;
|
import foundation.persist.NamedSQL;
|
import foundation.persist.SQLRunner;
|
import foundation.user.User;
|
import foundation.util.ID;
|
import foundation.util.MapList;
|
import foundation.util.Util;
|
|
public class NotifyHandler extends ActionProvider{
|
|
@Override
|
protected void publishMethod() {
|
//1. 发送通知
|
addMethod("sendNotifyMessage");
|
}
|
|
public void sendNotifyMessage() throws Exception {
|
DataPackage dataPackage = dataReader.getDataPackage();
|
Entity master = dataPackage.getMasterEntity(DataSource.DB);
|
String notifyCode = context.getStepParam();
|
|
//1. 获取提醒配置
|
AlertRuleBucket alertRuleBucket = AlertRuleBucket.getInstance();
|
AlertRule alertRule = alertRuleBucket.getAlertRule(notifyCode);
|
|
//2. 获取客户信息(id,名称)
|
String accountId = master.getString("account_id");
|
|
if ("md_org_account".equalsIgnoreCase(dataPackage.getName())) {
|
accountId = master.getId();
|
}
|
|
if (Util.isEmpty(accountId)){
|
accountId = master.getString("customer_id");
|
}
|
|
if (Util.isEmpty(accountId)){
|
dataWriter.addValue("sendNotify", "未获取到客户信息,不发送提醒");
|
return ;
|
}
|
|
DataObject dataObject = DataObject.getInstance("md_org_account");
|
Entity account = dataObject.getTableEntity(accountId);
|
|
SQLRunner.beginTrans();
|
try {
|
// 3. 保存提醒
|
String tableName = dataPackage.getMasterDataObject().getTableName();
|
|
NamedSQL namedSQL = NamedSQL.getInstance("insertNotifyMesaage");
|
String notifyId = ID.newValue();
|
namedSQL.setParam("id", notifyId);
|
namedSQL.setParam("account_id",account.getString("id"));
|
namedSQL.setParam("remark",account.getString("account_name"));
|
namedSQL.setParam("notify_id",alertRule.getId());
|
namedSQL.setParam("type_code",alertRule.getCode());
|
namedSQL.setParam("tableName", tableName);
|
String title = alertRule.calculateTitle();
|
namedSQL.setParam("title",title);
|
|
String filterSegment = alertRule.getDocIdField() + "=" + Util.quotedStr(dataPackage.getMasterId());
|
namedSQL.setParam("filterSegment", filterSegment);
|
|
namedSQL.execute();
|
|
//4. 设置通知对象
|
MapList<String, AlterNotifier> notifiers = alertRule.getNotifiers();
|
DataObject userDataObject = DataObject.getInstance("sys_notify_message_user");
|
|
EntitySet entitySet = userDataObject.createTableEntitySet();
|
|
for (AlterNotifier notifier: notifiers) {
|
EntitySet notifierSet = getNotifier(notifyId, accountId, master, notifier);
|
entitySet = EntitySet.appendAll(entitySet, notifierSet);
|
}
|
|
userDataObject.batchInsertEntitySet(entitySet);
|
}catch (Exception e) {
|
logger.info("发送通知失败");
|
e.printStackTrace();
|
SQLRunner.rollback();
|
}
|
|
SQLRunner.commit();
|
}
|
|
private EntitySet getNotifier(String notifyId, String accountId, Entity document, AlterNotifier notifier) throws Exception {
|
String actorId = notifier.getActorId();
|
String actorCode = notifier.getActorType().getCode();
|
|
DataObject dataObject = DataObject.getInstance("sys_notify_message_user");
|
EntitySet entityList = dataObject.createTableEntitySet();
|
|
EntitySet entitySet = null;
|
if(ActorType.User == notifier.getActorType()) {
|
entitySet = User.calculateUser(actorCode, actorId, notifier.getUserId());
|
}
|
else if(ActorType.Position == notifier.getActorType()) {
|
//1. 优先取单据内的岗位
|
String positionId = document.getString("position_id");
|
if (!Util.isEmpty(positionId)) {
|
entitySet = User.calculateUser(actorCode, actorId, positionId);
|
}
|
else {
|
EntitySet positionSet = getAccountEntity(accountId);
|
|
for (Entity position: positionSet) {
|
positionId = position.getString("position_id");
|
EntitySet positionEntitySet = User.calculateUser(actorCode, actorId, positionId);
|
entitySet = EntitySet.appendAll(entitySet, positionEntitySet);
|
}
|
}
|
}
|
else if(ActorType.Employee == notifier.getActorType()) {
|
entitySet = User.calculateUser(actorCode, actorId, null);
|
}
|
else if(ActorType.OrgAccount == notifier.getActorType()) {
|
entitySet = User.calculateUser(actorCode, actorId, accountId);
|
}
|
|
Entity entity;
|
|
if (entitySet == null) {
|
return null;
|
}
|
|
for (Entity user: entitySet) {
|
entity = dataObject.createTableEmptyEntity();
|
entity.setId(ID.newValue());
|
entity.set("parent_id", notifyId);
|
entity.set("actor_type", notifier.getActorType().getCode());
|
entity.set("actor_id", notifier.getActorId());
|
entity.set("is_show", "T");
|
entity.set("user_id", user.getString("sys_user__id"));
|
entityList.append(entity);
|
}
|
|
return entityList;
|
}
|
|
private EntitySet getAccountEntity(String accountId) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("getAccountEntity");
|
namedSQL.setParam("accountId", accountId);
|
return namedSQL.getEntitySet();
|
}
|
}
|