package biz.notify;
|
|
import foundation.dao.DataPackage;
|
import foundation.dao.DataSource;
|
import foundation.dao.Settings;
|
import foundation.data.entity.Entity;
|
import foundation.data.entity.EntitySet;
|
import foundation.data.entity.Filter;
|
import foundation.data.object.DataObject;
|
import foundation.persist.NamedSQL;
|
import foundation.persist.SQLRunner;
|
import foundation.util.DateTimeUtil;
|
import foundation.util.ID;
|
import foundation.util.MapList;
|
import foundation.util.Util;
|
import foundation.workflow.ActionProvider;
|
|
public class NotifyHandler extends ActionProvider{
|
|
@Override
|
protected void publishMethod() {
|
//1.1 发送通知
|
addMethod("sendNotifyMessage");
|
|
//1.2 关闭长期历史的通知
|
addMethod("closedNotifyMessage");
|
|
//2.1 批量发送预警
|
addMethod("sendAlertBatch");
|
|
//2.2 发送单条预警
|
addMethod("sendAlertOne");
|
}
|
|
public void sendNotifyMessage() throws Exception {
|
DataPackage dataPackage = dataReader.getDataPackage();
|
Entity master = dataPackage.getMasterEntity(DataSource.DB);
|
String notifyCode = step.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();
|
}
|
|
public void closedNotifyMessage() throws Exception {
|
DataObject dataObject = DataObject.getInstance("sys_notify_message_user");
|
Entity entity = dataObject.createTableEmptyEntity();
|
entity.set("is_show", "F");
|
dataObject.updateByEntity(entity, new Filter("end_date < now()" ));
|
}
|
|
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 = calculateUser(actorCode, actorId, notifier.getUserId());
|
}
|
else if(ActorType.Position == notifier.getActorType()) {
|
//1. 优先取单据内的岗位
|
String positionId = document.getString("position_id");
|
if (!Util.isEmpty(positionId)) {
|
entitySet = calculateUser(actorCode, actorId, positionId);
|
}
|
else {
|
EntitySet positionSet = getAccountEntity(accountId);
|
|
for (Entity position: positionSet) {
|
positionId = position.getString("position_id");
|
EntitySet positionEntitySet = calculateUser(actorCode, actorId, positionId);
|
entitySet = EntitySet.appendAll(entitySet, positionEntitySet);
|
}
|
}
|
}
|
else if(ActorType.Employee == notifier.getActorType()) {
|
entitySet = calculateUser(actorCode, actorId, null);
|
}
|
else if(ActorType.OrgAccount == notifier.getActorType()) {
|
entitySet = calculateUser(actorCode, actorId, accountId);
|
}
|
|
Entity entity;
|
|
if (entitySet == null) {
|
return null;
|
}
|
|
int messageLifeSpan = Settings.getInteger("MessageLifeSpan", 7);
|
String endDate = DateTimeUtil.addDays(Util.newDateStr(), messageLifeSpan);
|
|
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"));
|
entity.set("end_date", endDate);
|
entityList.append(entity);
|
}
|
|
return entityList;
|
}
|
|
private EntitySet getAccountEntity(String accountId) throws Exception {
|
DataObject dataObject = DataObject.getInstance("md_org_account");
|
return dataObject.getBrowseEntitySet(new Filter("md_org_account.id", accountId));
|
// NamedSQL namedSQL = NamedSQL.getInstance("getAccountEntity");
|
// namedSQL.setParam("accountId", accountId);
|
// return namedSQL.getEntitySet();
|
}
|
|
public void sendAlertBatch() throws Exception {
|
String ruleId;
|
|
if (dataReader != null) {
|
ruleId = dataReader.getString("ruleId");
|
}
|
else {
|
ruleId = step.getParam();
|
}
|
|
AlertRuleBucket alertRuleBucket = AlertRuleBucket.getInstance();
|
AlertRule alertRule = alertRuleBucket.getAlertRule(ruleId);
|
|
alertRule.executeBatch();
|
}
|
|
public void sendAlertOne() throws Exception {
|
String ruleId = dataReader.getString("ruleId");
|
String documentId = dataReader.getString("id");
|
|
AlertRuleBucket alertRuleBucket = AlertRuleBucket.getInstance();
|
AlertRule alertRule = alertRuleBucket.getAlertRule(ruleId);
|
|
alertRule.executeOne(documentId);
|
}
|
|
public static EntitySet calculateUser(String actorTypeCode, String actorId, String id) throws Exception {
|
ActorType actorType = ActorType.parse(actorTypeCode);
|
DataObject dataObject;
|
|
if (ActorType.User == actorType) {
|
dataObject = DataObject.getInstance("actor_target_user");
|
EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_user.id", id));
|
return entitySet;
|
}
|
else if (ActorType.OrgAccount == actorType) {
|
dataObject = DataObject.getInstance("md_org_account_user");
|
EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("md_org_account.id", id));
|
return entitySet;
|
|
}
|
else if (ActorType.Position == actorType) {
|
String positionId = id;
|
|
if ("Actor-Sales-Leader".equalsIgnoreCase(actorId)) {
|
dataObject = DataObject.getInstance("md_position_hierarchy");
|
Entity entity = dataObject.getTableEntity(new Filter("position_id", id));
|
positionId = entity.getString("level2");
|
}
|
else if ("Actor-Sales-Direction".equalsIgnoreCase(actorId)) {
|
dataObject = DataObject.getInstance("md_position_hierarchy");
|
Entity entity = dataObject.getTableEntity(new Filter("position_id", id));
|
positionId = entity.getString("level1");
|
}
|
|
dataObject = DataObject.getInstance("md_position_user");
|
EntitySet positionSet = dataObject.getBrowseEntitySet(new Filter("md_position_employee.position_id", positionId));
|
return positionSet;
|
}
|
else if(ActorType.Employee == actorType) {
|
// 运营, 商务, 总经理, 财务.....
|
dataObject = DataObject.getInstance("actor_target_position");
|
EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_right_actor_target.actor_id", actorId));
|
return entitySet;
|
}
|
|
return null;
|
}
|
}
|