P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
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();
    }
}