/
zj
2025-05-02 9102aa7e0b42ce5b9667fa3b67fede889df60fc0
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package project.tip.internal;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import project.syspara.SysparaService;
import project.tip.TipConstants;
import project.tip.TipService;
import project.tip.model.Tip;
import security.SecUser;
import security.internal.SecUserService;
 
public class TipServiceImpl implements TipService {
    
    private SecUserService secUserService;
    
    private SysparaService sysparaService;
    
    private Map<String, Tip> tipCache = new ConcurrentHashMap<String, Tip>();
    
    private static final Logger logger=LoggerFactory.getLogger(TipServiceImpl.class);
    
    /**
     * 初始化数据
     */
    public void init() {
        List<Tip> list=ApplicationUtil.executeSelect(Tip.class);
        if(null==list || list.isEmpty()) return;
        list.forEach(tip->tipCache.put(tip.getBusiness_id(), tip));
    }
 
    /**
     * 新增通知
     * @param businessId 业务id(唯一性)
     * @param model 模块
     */
    public void saveTip(String businessId, String model) {
        try {
            Tip tip=tipCache.get(businessId);
            if (null==tip) tip=new Tip();
            
            tip.setBusiness_id(businessId);
            tip.setCreate_time(new Date());
            tip.setModel(model);
            tip.setTime_stamp(new Date().getTime());
            
            ApplicationUtil.executeSaveOrUpdate(tip);
            tipCache.put(businessId, tip);
        } catch (Exception e) {
            logger.error("fail put tip businessId:{" + businessId + "},model:{" + model + "},e:{}", e);
        }
    }
    
    /**
     * 新增通知
     * @param businessId 业务id(唯一性)
     * @param model      模块
     */
    public void saveNewTip(String businessId, String model,String remark) {
        try {
            Tip tip = tipCache.get(businessId);
            if (null == tip) tip = new Tip();
            
            tip.setBusiness_id(businessId);
            tip.setCreate_time(new Date());
            tip.setModel(model);
            tip.setTime_stamp(new Date().getTime());
            tip.setTarget_username(remark);
            
            ApplicationUtil.executeSaveOrUpdate(tip);
            tipCache.put(businessId, tip);
        } catch (Exception e) {
            logger.error("fail put tip businessId22:{" + businessId + "},model:{" + model + "},e:{}", e);
        }
    }
 
    /**
     * 新增通知
     * @param tip 消息通知
     */
    public void saveTip(Tip tip) {
        try {
            tip.setCreate_time(new Date());
            tip.setTime_stamp(new Date().getTime());
            
            ApplicationUtil.executeInsert(tip);
            tipCache.put(tip.getBusiness_id(), tip);
        } catch (Exception e) {
            logger.error("fail put tip businessId:{" + tip.getBusiness_id() + "},model:{" + tip.getModel() + "},e:{}", e);
        }
    }
 
    /**
     * 移除通知
     * @param businessId
     */
    public void deleteTip(String businessId) {
        try {
            ApplicationUtil.executeDelete(Tip.class,"WHERE BUSINESS_ID=?",new Object[] {businessId});
            tipCache.remove(businessId);
        } catch (Exception e) {
            logger.error("fail remove tip businessId:{" + businessId + "},e:{}", e);
        }
    }
 
    /**
     * 批量移除通知
     * @param businessId
     */
    public void deleteTip(List<String> businessIds) {
        if(null==businessIds || businessIds.isEmpty()) return;
        List<Object[]> paramList=businessIds.stream().map(id->new Object[] {id}).collect(Collectors.toList());
        ApplicationUtil.executeBatchDelete(Tip.class, "WHERE BUSINESS_ID=?", paramList);
        for (String id : businessIds) tipCache.remove(id);
    }
    
    /**
     * 获取总数 数据
     * @param username
     * @return
     */
    public List<Map<String, Object>> cacheSumTips(String username) {
        List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
 
        //获取参数用户的所有提示记录
        List<Tip> filterTips = filterTips(username, null);
        if (CollectionUtils.isEmpty(filterTips)) return result;
        
        //获取用户所有模块资源权限
        Set<String> resourceIds = getPrivileges(username);
        
        //将模块资源列表转换为模块到提示集合的映射字典
        Map<String, List<Tip>> modelMap = modelData(filterTips);
        for (Entry<String, List<Tip>> entry : modelMap.entrySet()) {
            if (resourceIds.contains(entry.getKey())) {//有权限则返回通知
                result.add(tipData(entry.getKey(), entry.getValue()));
            }
        }
        
        return result;
    }
 
    /**
     * 获取指定模块的新通知数据
     * @param username
     * @return
     */
    public LinkedList<Map<String, Object>> cacheNewTipsByModel(String username, Long lastTimeStamp, String model) {
        LinkedList<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
 
        //获取参数用户的所有提示记录
        List<Tip> filterNewTips = filterTips(username, lastTimeStamp);
        if (CollectionUtils.isEmpty(filterNewTips)) return result;
        
        //将模块资源列表转换为模块到提示集合的映射字典
        Map<String, List<Tip>> modelMap = modelData(filterNewTips);
        List<Tip> tipList = modelMap.get(model);
        
        if (CollectionUtils.isEmpty(tipList)) return result;
        
        result.add(tipNewData(model, tipList));
        return result;
    }
 
    /**
     * 获取新通知数据
     * @param username
     * @return 
     */
    public LinkedList<Map<String, Object>> cacheNewTips(String username, Long lastTimeStamp) {
        LinkedList<Map<String, Object>> result=new LinkedList<Map<String, Object>>();
 
        //获取参数用户的所有提示记录
        List<Tip> filterNewTips = filterTips(username, lastTimeStamp);
        if (CollectionUtils.isEmpty(filterNewTips)) return result;
        
        //获取用户所有模块资源权限
        Set<String> resourceIds = getPrivileges(username);
        
        //将模块资源列表转换为模块到提示集合的映射字典
        Map<String, List<Tip>> modelMap = modelData(filterNewTips);
        
        for (Entry<String, List<Tip>> entry : modelMap.entrySet()) {
            if (resourceIds.contains(entry.getKey())) { //有权限则返回通知
                result.add(tipNewData(entry.getKey(), entry.getValue()));
            }
        }
        
        return result;
    }
    
    
    /**
     * 获取用户所有权限集合
     * @param username 用户名
     * @return 权限字串集合
     */
    private Set<String> getPrivileges(String username) {
        SecUser user = this.secUserService.findUserByLoginName(username);
        return user.getRoles().stream().flatMap(role->role.getResources().stream()).map(resource->resource.getId().toString()).collect(Collectors.toSet());
    }
    
    /**
     * 过滤参数用户指定时间戳之后的提示记录
     * @param username 用户名
     * @param lastTimeStamp 时间戳
     * @return 提示列表
     */
    private List<Tip> filterTips(final String username, final Long lastTimeStamp) {
        ArrayList<Tip> tipList = new ArrayList<Tip>(tipCache.values());
        CollectionUtils.filter(tipList, new Predicate() { //过滤查找新生成的通知数据
            @Override
            public boolean evaluate(Object paramObject) {
                Tip tip = (Tip) paramObject;
                if (TipConstants.MUST_USERNAME_MODEL.containsKey(tip.getModel())) {
                    if (StringUtils.isNotEmpty(tip.getTarget_username()) && username.equals(tip.getTarget_username())) {
                        return lastTimeStamp == null || tip.getTime_stamp() > lastTimeStamp;
                    } else {
                        return false;
                    }
                }
                return lastTimeStamp == null || ((Tip) paramObject).getTime_stamp() > lastTimeStamp;
            }
        });
        return tipList;
    }
 
    /**
     * 每个模块对应的提醒数据列表
     * @param tips 提醒数据列表
     * @return 提醒数据字典
     */
    private Map<String, List<Tip>> modelData(Collection<Tip> tips) {
        HashMap<String, List<Tip>> modelMap = new HashMap<String, List<Tip>>();
        
        for (Tip tip : tips) {
            List<Tip> list = modelMap.get(tip.getModel());
            if (null==list) modelMap.put(tip.getModel(),list=new ArrayList<Tip>());
            list.add(tip);
        }
        
        return modelMap;
    }
 
    /**
     * 构建模块的通知数据(New)
     * @param model 模块
     * @param list 模块列表数据
     * @return 通知数据字典
     */
    private Map<String, Object> tipNewData(String model, final List<Tip> list) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 模块提示数量
        resultMap.put("tip_content_num", list.size());
        // 生成html通知
        String htmlMessage = MessageFormat.format(TipConstants.MESSAGE_MAP.get(model),MessageFormat.format("<span class=\"label label-danger\">{0}</span>", list.size()));
        // 模块提示消息
        resultMap.put("tip_message", list.size()>0? htmlMessage:"");
        // 模块请求url
        resultMap.put("tip_url", list.size()>0?TipConstants.ACTION_MAP.get(model) : "");
        // 是否右下角提示
        resultMap.put("tip_show",sysparaService.find("tip_noshow_models").getValue().indexOf(model)==-1);
        return resultMap;
    }
    
    /**
     * 构建模块的通知数据(New)
     * @param model 模块
     * @param list  模块列表数据
     * @return 通知数据字典
     */
    private Map<String, Object> tipData(String model, final List<Tip> list) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 页面上对应的classname(可自定义)
        resultMap.put("tip_dom_name", TipConstants.DOM_MAP.get(model));
        // 模块总数
        resultMap.put("tip_content_sum", list.size());
        // 模块新增数
        return resultMap;
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
}