P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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;
    }
}